lang/org: refactor tab/backtab/ret keybinds to be less intrusive

Turns out the native TAB/Backtab/RET functionality in org already does
what I've replaced them with (somewhat).

Also, I discovered that the canonical way to modify TAB behavior was
through org-tab-first-hook. So, instead of replacing native
functionality, I've rewritten these keybinds to leverage them.
This commit is contained in:
Henrik Lissner 2018-02-13 18:12:35 -05:00
parent a05c4cca6c
commit d84af58f77
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 46 additions and 53 deletions

View file

@ -112,60 +112,52 @@ If on a:
(set-window-start nil scroll-pt)))
;;;###autoload
(defun +org/indent ()
"Indent the current item (header or item). Otherwise, forward to
`self-insert-command'."
(defun +org|indent-maybe ()
"Indent the current item (header or item), if possible. Made for
`org-tab-first-hook'."
(interactive)
(cond ((org-at-item-p)
(org-indent-item-tree))
(org-indent-item-tree)
t)
((org-at-heading-p)
(ignore-errors (org-demote)))
(ignore-errors (org-demote))
t)
((org-in-src-block-p t)
(doom/dumb-indent))
(t
(call-interactively #'self-insert-command))))
(doom/dumb-indent)
t)))
;;;###autoload
(defun +org/indent-or-next-field-or-yas-expand ()
"Depending on the context either a) indent the current line, b) go the next
table field or c) run `yas-expand'."
(interactive)
(or (org-try-structure-completion)
(call-interactively
(cond ((and (bound-and-true-p yas-minor-mode)
(yas--templates-for-key-at-point))
#'yas-expand)
((org-at-table-p)
#'org-table-next-field)
(t
#'+org/indent)))))
(defun +org|yas-expand-maybe ()
"Tries to expand a yasnippet snippet, if one is available. Made for
`org-tab-first-hook'."
(when (and (if (bound-and-true-p evil-mode)
(eq evil-state 'insert)
t)
(bound-and-true-p yas-minor-mode)
(yas--templates-for-key-at-point))
(call-interactively #'yas-expand)
t))
;;;###autoload
(defun +org/dedent ()
"Dedent the current item (header or item). Otherwise, forward to
`self-insert-command'."
(defun +org/shifttab (&optional arg)
"An alternative to `org-shifttab' which performs smart indentation if in
insert mode (evil). Otherwise, forwards to the original `org-shifttab'."
(interactive)
(cond ((org-at-item-p)
(org-list-indent-item-generic
-1 nil
(save-excursion
(when (org-region-active-p)
(goto-char (region-beginning)))
(org-list-struct))))
((org-at-heading-p)
(ignore-errors (org-promote)))
(t
(call-interactively #'self-insert-command))))
;;;###autoload
(defun +org/dedent-or-prev-field ()
"Depending on the context either dedent the current item or go the previous
table field."
(interactive)
(call-interactively
(if (org-at-table-p)
#'org-table-previous-field
#'+org/dedent)))
(cond ((org-at-table-p)
(call-interactively #'org-table-previous-field))
((and (bound-and-true-p evil-mode)
(evil-insert-state-p))
(cond ((org-at-item-p)
(org-list-indent-item-generic
-1 nil
(save-excursion
(when (org-region-active-p)
(goto-char (region-beginning)))
(org-list-struct))))
((org-at-heading-p)
(ignore-errors (org-promote)))
(t (call-interactively #'self-insert-command))))
(t (org-shifttab arg))))
;;;###autoload
(defun +org/insert-item (direction)

View file

@ -207,12 +207,15 @@ unfold to point on startup."
(defun +org|setup-keybinds ()
"Sets up org-mode and evil keybindings. Tries to fix the idiosyncrasies
between the two."
(map! :map org-mode-map
"RET" #'org-return-indent
"C-c C-S-l" #'+org/remove-link
:n "C-c C-i" #'org-toggle-inline-images
(add-hook! 'org-tab-first-hook #'(+org|indent-maybe +org|yas-expand-maybe))
:n "RET" #'+org/dwim-at-point
(map! :map org-mode-map
[remap org-shifttab] #'+org/shifttab
"C-c C-S-l" #'+org/remove-link
"C-c C-i" #'org-toggle-inline-images
:i "RET" #'org-return-indent
:n "RET" #'+org/dwim-at-point
;; Navigate table cells (from insert-mode)
:i "C-l" #'+org/table-next-field
@ -225,9 +228,7 @@ between the two."
:ni "C-S-k" #'org-metaup
:ni "C-S-j" #'org-metadown
:n [tab] #'+org/toggle-fold
:i [tab] #'+org/indent-or-next-field-or-yas-expand
:i [backtab] #'+org/dedent-or-prev-field
:n [tab] #'+org/toggle-fold
:ni [M-return] (λ! (+org/insert-item 'below))
:ni [S-M-return] (λ! (+org/insert-item 'above))