Re-work org-dwim-char

This commit is contained in:
MitchMarq42 2023-05-29 16:20:23 -08:00
parent f527fc83b7
commit 479fc1436d

View File

@ -47,10 +47,13 @@
(bury-buffer) (bury-buffer)
(other-window 1)) (other-window 1))
(defvar org-vw-pre-hook nil
"Hook to run before org-vw-mode has been loaded.")
(define-minor-mode org-vw-mode (define-minor-mode org-vw-mode
"Org VimWiki mode." "Org VimWiki mode."
:lighter " VW" :lighter " VW"
:keymap (make-sparse-keymap) :keymap (make-sparse-keymap)
(run-hooks org-vw-pre-hook)
;; TODO: This is a hack, because hyperbole is scary. Ideal implementation ;; TODO: This is a hack, because hyperbole is scary. Ideal implementation
;; would add a proper case rather than this weird fallback. ;; would add a proper case rather than this weird fallback.
(require 'hyperbole) (require 'hyperbole)
@ -60,7 +63,12 @@
(general-define-key (general-define-key
:keymaps 'local :keymaps 'local
:states 'normal :states 'normal
"DEL" 'org-vw-back)) "DEL" 'org-vw-back)
(dolist (key mitch/org-dwim-char-chars)
(general-define-key
:keymaps 'local
key 'mitch/org-dwim-char))
)
;; (add-to-list 'auto-mode-alist `(,org-wiki-regex . org-vw-mode)) ;; (add-to-list 'auto-mode-alist `(,org-wiki-regex . org-vw-mode))
(add-hook 'org-mode-hook #'org-vw-mode) (add-hook 'org-mode-hook #'org-vw-mode)
@ -84,6 +92,7 @@ Start insert mode."
;; ------------------ ABANDON ALL SANITY, YE WHO ENTER HERE -------------------- ;; ------------------ ABANDON ALL SANITY, YE WHO ENTER HERE --------------------
(defvar mitch/org-dwim-char-chars '("_" "/" "*" "+" "~" "="))
(defun mitch/org-dwim-char (&optional char) (defun mitch/org-dwim-char (&optional char)
"If a region is active (visual mode), surround selection with CHAR. "If a region is active (visual mode), surround selection with CHAR.
@ -100,28 +109,49 @@ character CHAR.
Otherwise, insert two of CHAR and put point between them like `electric-pair'." Otherwise, insert two of CHAR and put point between them like `electric-pair'."
(interactive) ; TODO: can we make CHAR an arg to `interactive'? (interactive) ; TODO: can we make CHAR an arg to `interactive'?
(let ((char (or char (string-to-char (this-command-keys)))) (let* ((char (or char (string-to-char (this-command-keys))))
(word (thing-at-point 'word 'no-properties))) (word (thing-at-point 'word 'no-properties))
;;; test conditions here to be eval'd, separating flow from data
;; at beginning of word
(beginning-of-word-test '(or (bolp) (eq (char-before (point)) ?\ )))
;; word is surrounded by `char'
(surrounded-by-char-test
'(and (eq (char-before (beginning-of-thing 'word)) char)
(eq (char-after (end-of-thing 'word)) char)))
;; char before and char after are the same
(double-char-test (eq char (char-after)))
;; Weird Org exceptions because org
(org-exceptions-test
'(or (string-match-p (rx bol (or "#" "*"))
(thing-at-point 'line 'no-properties))
(org-in-block-p org-protecting-blocks)
(org-at-property-p))
)
)
(cond (cond
((eq evil-state 'visual) ((eq evil-state 'visual)
(let* ((beg (region-beginning)) (let* ((beg (region-beginning))
(end (region-end))) (end (region-end)))
(evil-surround-region beg end evil-visual-selection char nil))) (evil-surround-region beg end evil-visual-selection char nil)))
((eq evil-state 'normal) ((eq evil-state 'normal)
(if (or (bolp) (eq (char-before (point)) ?\ )) ; at beginning of word (if (eval beginning-of-word-test)
;; run original function bound to key ;; run original function bound to key
(let ((charstr (make-string 1 char))) (let ((charstr (make-string 1 char)))
(pcase charstr ;; (pcase charstr
;; TODO: can we not hard-code this? ;; ;; TODO: can we not hard-code this?
("_" (call-interactively 'evil-next-line-1-first-non-blank)) ;; ;; MAYBE: with Advice on each below function.
("/" (call-interactively 'evil-search-forward)) ;; ;; MAYBE: with `key-binding' fn
("*" (call-interactively 'evil-search-word-forward)) ;; ("_" (call-interactively 'evil-next-line-1-first-non-blank))
("+" (call-interactively 'evil-next-line-first-non-blank)) ;; ("/" (call-interactively 'evil-search-forward))
("~" (call-interactively 'evil-invert-char)) ;; ("*" (call-interactively 'evil-search-word-forward))
("=" (call-interactively 'evil-indent)))) ;; ("+" (call-interactively 'evil-next-line-first-non-blank))
;; ("~" (call-interactively 'evil-invert-char))
;; ("=" (call-interactively 'evil-indent)))
(call-interactively (alist-get charstr org-vw-old-binds-alist
nil nil #'string=))
)
(save-excursion (save-excursion
(if (and (eq (char-before (beginning-of-thing 'word)) char) (if (eval surrounded-by-char-test)
(eq (char-after (end-of-thing 'word)) char))
(progn (progn
(search-backward (char-to-string char)) (delete-char 1) (search-backward (char-to-string char)) (delete-char 1)
(search-forward (char-to-string char)) (delete-char -1)) (search-forward (char-to-string char)) (delete-char -1))
@ -130,16 +160,21 @@ Otherwise, insert two of CHAR and put point between them like `electric-pair'."
(insert char) (insert char)
(end-of-thing 'word) (end-of-thing 'word)
(insert char)))))) (insert char))))))
((eq char (char-after)) ((eval double-char-test) (right-char))
(right-char)) ((eval org-exceptions-test) (insert char))
((or (string-match-p (rx bol (or "#" "*" ""))
(thing-at-point 'line 'no-properties))
(org-in-block-p org-protecting-blocks)
(org-at-property-p))
(insert char))
(t (progn (t (progn
(insert (make-string 2 char)) (insert (make-string 2 char))
(left-char)))))) (left-char))))))
(defun org-vw-snapshot-bindings ()
"Make an alist of the functions formerly ran by keys to be bound to `mitch/org-dwim-char'.
Store it in the variable `org-vw-old-binds-alist'."
(unless org-vw-old-binds-alist
(defvar-local org-vw-old-binds-alist
(mapcar (lambda (key)
(cons key (key-binding key)))
mitch/org-dwim-char-chars))))
(add-hook 'org-vw-pre-hook #'org-vw-snapshot-bindings nil 'local)
;; (setq debug-on-error t) ;; (setq debug-on-error t)
;; --------- LET THY BRAIN NO LONGER TREMBLE, FOR I AM BECOME COMPLETE --------- ;; --------- LET THY BRAIN NO LONGER TREMBLE, FOR I AM BECOME COMPLETE ---------