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

View file

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