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:
parent
a05c4cca6c
commit
d84af58f77
2 changed files with 46 additions and 53 deletions
|
@ -112,39 +112,41 @@ 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)
|
||||
(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))
|
||||
#'yas-expand)
|
||||
((org-at-table-p)
|
||||
#'org-table-next-field)
|
||||
(t
|
||||
#'+org/indent)))))
|
||||
(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-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
|
||||
|
@ -154,18 +156,8 @@ table field or c) run `yas-expand'."
|
|||
(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)))
|
||||
(t (call-interactively #'self-insert-command))))
|
||||
(t (org-shifttab arg))))
|
||||
|
||||
;;;###autoload
|
||||
(defun +org/insert-item (direction)
|
||||
|
|
|
@ -207,11 +207,14 @@ 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))
|
||||
|
||||
(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)
|
||||
|
@ -226,8 +229,6 @@ between the two."
|
|||
: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
|
||||
|
||||
:ni [M-return] (λ! (+org/insert-item 'below))
|
||||
:ni [S-M-return] (λ! (+org/insert-item 'above))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue