[hydra] refactor

This commit is contained in:
contrapunctus 2021-08-01 13:27:08 +05:30
parent 4dc09324b3
commit 80b9874d49
1 changed files with 94 additions and 249 deletions

343
init.org
View File

@ -1301,7 +1301,7 @@ PR ideas
("M-d" . easy-kill-delete-region)))
#+END_SRC
** TODO hydra [66%]
** TODO hydra [83%]
I started off using Hydra for programming modes, when I noticed that Elisp, Common Lisp, and Scheme all had some semantically-analogous operations with different names, which could be abstracted away behind a generic interface. Then, around the time I got into using Org for literate programs, I added an Org hydra, and then a general hydra for frequently-used operations.
Add these common operations to the hydra -
@ -1311,9 +1311,41 @@ Add these common operations to the hydra -
* Trickier to implement than I thought.
4. [X] =toggle-debug-on-error=
5. [X] "insert" hydra, for timestamps, dates; in Elisp, insert as strings; in Org, in angular brackets; etc
6. [ ] remove duplication
6. [X] remove duplication
#+BEGIN_SRC emacs-lisp
(use-package hydra :commands defhydra)
(defvar my-hydra-common-heads
'(("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window")))
(defmacro my-defhydra (name body docstring &rest unique-forms)
(declare (indent defun))
`(defhydra ,name ,body
,docstring
,@unique-forms
,@my-hydra-common-heads))
#+END_SRC
*** Line display
#+BEGIN_SRC emacs-lisp
@ -1392,35 +1424,10 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
#+END_SRC
*** General
#+BEGIN_SRC emacs-lisp
(defhydra contrapunctus-general-hydra (:color blue)
(my-defhydra contrapunctus-general-hydra (:color blue)
"What command?"
("O" cp-org/body "org")
("p" contrapunctus-programming-hydra-dispatch-language "programming")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("E" toggle-debug-on-error "tdoe")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
*** applications
#+BEGIN_SRC emacs-lisp
@ -1433,7 +1440,7 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
("m" magit-status "magit")
("p" list-packages "packages")
;; less-used
("J" cp-jabber/body "Jabber")
("J" my-jabber-hydra/body "Jabber")
("E" contrapunctus-emms-hydra/body "emms")
;; external
@ -1451,54 +1458,33 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
#+END_SRC
*** org
#+BEGIN_SRC emacs-lisp
(defhydra cp-org (:color blue)
(my-defhydra my-org-hydra (:color blue)
"Org"
("p" org-set-property "property")
("b" cp-org-block/body "source block")
("t" cp-org-set-tags "tags")
("n" cp-org-nav/body "navigation")
("b" my-org-hydra-block/body "source block")
("t" my-org-hydra-set-tags "tags")
("n" my-org-hydra-nav/body "navigation")
("B" contrapunctus-async-tangle "babel-tangle")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("E" toggle-debug-on-error "tdoe")
("G" contrapunctus-general-hydra/body "up")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
**** org block
#+BEGIN_SRC emacs-lisp
(defhydra cp-org-block (:color blue)
(defhydra my-org-hydra-block (:color blue)
"Org source block"
("e" (cp-org-insert-block "SRC" "emacs-lisp") "Emacs Lisp")
("t" (cp-org-insert-block "SRC" "emacs-lisp :tangle test :load test") "Emacs Lisp test")
("E" (cp-org-insert-block "SRC" "emacs-lisp :tangle no :load no") "Emacs Lisp example")
("s" (cp-org-insert-block "SRC" "shell :tangle no") "Shell")
("o" (cp-org-insert-block "QUOTE") "quote")
("v" (cp-org-insert-block "VERSE") "verse"))
("e" (my-org-hydra-insert-block "SRC" "emacs-lisp") "Emacs Lisp")
("t" (my-org-hydra-insert-block "SRC" "emacs-lisp :tangle test :load test") "Emacs Lisp test")
("E" (my-org-hydra-insert-block "SRC" "emacs-lisp :tangle no :load no") "Emacs Lisp example")
("s" (my-org-hydra-insert-block "SRC" "shell :tangle no") "Shell")
("o" (my-org-hydra-insert-block "QUOTE") "quote")
("v" (my-org-hydra-insert-block "VERSE") "verse"))
#+END_SRC
**** org navigation
#+BEGIN_SRC emacs-lisp
(defhydra cp-org-nav (:color red)
(defhydra my-org-hydra-nav (:color red)
"Navigation"
;; movement
("c" org-previous-visible-heading "previous heading")
@ -1518,60 +1504,30 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
#+END_SRC
*** jabber
#+BEGIN_SRC emacs-lisp
(defhydra cp-jabber (:color blue)
(my-defhydra my-jabber-hydra (:color blue)
"Jabber"
("c" jabber-connect "connect")
("r" jabber-display-roster "roster")
("n" jabber-activity-switch-to "next" :color red)
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("E" toggle-debug-on-error "tdoe")
("w" contrapunctus-window-hydra/body "window")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("G" contrapunctus-general-hydra/body "up"))
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
*** Emacs Lisp
#+BEGIN_SRC emacs-lisp
(defhydra cp-el (:color blue)
(my-defhydra my-elisp-hydra (:color blue)
"Emacs Lisp"
("r" ielm "REPL")
("E" cp-el-debug/body "Debug")
("e" cp-el-eval/body "Eval")
("h" my-help-hydra/body "Help")
("B" contrapunctus-async-tangle "babel-tangle")
("e" my-elisp-hydra-eval/body "Eval")
("E" my-elisp-hydra-debug/body "Debug")
("j" xref-find-definitions "Jump to definition")
("J" org-babel-tangle-jump-to-org "Jump to definition (Org)")
("t" (contrapunctus-el-test/body) "Test")
("B" contrapunctus-async-tangle "babel-tangle")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("r" ielm "REPL")
("t" contrapunctus-el-test/body "Test"))
#+END_SRC
**** eval
#+BEGIN_SRC emacs-lisp
(defhydra cp-el-eval (:color blue)
(defhydra my-elisp-hydra-eval (:color blue)
("b" eval-buffer "buffer")
("e" eval-defun "defun")
("l" eval-last-sexp "last sexp"))
@ -1584,7 +1540,7 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
#+END_SRC
**** debug
#+BEGIN_SRC emacs-lisp
(defhydra cp-el-debug (:color blue)
(defhydra my-elisp-hydra-debug (:color blue)
"Debug"
("e" (funcall-interactively #'eval-defun t) "edebug")
("o" toggle-debug-on-error "tdoe")
@ -1592,46 +1548,24 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
#+END_SRC
*** Common Lisp
#+BEGIN_SRC emacs-lisp
(defhydra cp-cl (:color blue)
(my-defhydra my-cl-hydra (:color blue)
"Common Lisp"
("C" slime-connect "connect")
("d" cp-cl-doc/body "Documentation")
("e" cp-cl-eval/body "Eval")
("r" slime "REPL")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("d" my-cl-hydra-doc/body "Documentation")
("e" my-cl-hydra-eval/body "Eval")
("r" slime "REPL"))
#+END_SRC
**** eval
#+BEGIN_SRC emacs-lisp
(defhydra cp-cl-eval (:color blue)
(defhydra my-cl-hydra-eval (:color blue)
("b" slime-eval-buffer "buffer")
("e" slime-eval-defun "defun"))
("e" slime-eval-defun "defun")
("E" toggle-debug-on-error "tdoe")
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
**** documentation
#+BEGIN_SRC emacs-lisp
(defhydra cp-cl-doc (:color blue)
(defhydra my-cl-hydra-doc (:color blue)
("d" slime-documentation "slime")
("e" slime-documentation-lookup "CLHS"))
#+END_SRC
@ -1641,131 +1575,43 @@ _u_: next word ^_l_: edit lines^ _s_: next whole symbol
(defhydra cp-scm-eval (:color blue)
("b" geiser-eval-buffer "buffer")
("e" geiser-eval-definition "defun")
("l" geiser-eval-last-sexp "last sexp"))
("l" geiser-eval-last-sexp "last sexp")
("E" toggle-debug-on-error "tdoe")
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
**** CHICKEN Scheme
#+BEGIN_SRC emacs-lisp
(defhydra cp-cs (:color blue)
(my-defhydra my-chicken-hydra (:color blue)
"CHICKEN Scheme"
("e" cp-scm-eval/body "Eval")
("r" run-chicken "REPL")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("E" toggle-debug-on-error "tdoe")
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
**** Guile
#+BEGIN_SRC emacs-lisp
(defhydra cp-guile (:color blue)
(my-defhydra cp-guile (:color blue)
"Guile"
("e" cp-scm-eval/body "Eval")
("r" run-guile "REPL")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("c" chronometrist "chronometrist")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("E" toggle-debug-on-error "tdoe")
("G" contrapunctus-general-hydra/body "up")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("m" magit-status "magit")
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
*** Lilypond
#+BEGIN_SRC emacs-lisp
(defhydra cp-ly (:color blue)
(my-defhydra my-lilypond-hydra (:color blue)
"Lilypond"
("b" (cp/compile-project "main.ly" "./mkly dev") "Compile")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("E" toggle-debug-on-error "tdoe")
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
*** Prolog
#+BEGIN_SRC emacs-lisp
(defhydra cp-prolog (:color blue)
(my-defhydra my-prolog-hydra (:color blue)
"Prolog"
("r" ediprolog-dwim "REPL")
("0" delete-window "delete this" :color red)
("1" delete-other-windows "delete others" :color red)
("2" split-window-below "split below" :color red)
("3" split-window-right "split right" :color red)
("a" my-app-hydra/body "applications")
("z" my-app-hydra/body "applications")
("C" contrapunctus-mc-hydra/body "multiple cursors")
("d" dired-jump "dired-jump")
("D" (cp-insert-timestamp t) "date")
("h" my-help-hydra/body "Help")
("i" (find-file "~/.emacs.d/init.org") "open init")
("I" contrapunctus-info-hydra/body "Info")
("k" (kill-buffer (current-buffer)) "kill" :color red)
("N" contrapunctus-line-display-hydra/body "line display")
("o" save-buffer "save")
("Q" toggle-debug-on-quit "tdoq")
("s" imenu-list "sidebar")
("T" cp-insert-timestamp "timestamp")
("u" find-file "new")
("U" launch-file "launch-file")
("v" volume "volume")
("C-v" find-alternate-file "revert")
("w" contrapunctus-window-hydra/body "window"))
("E" toggle-debug-on-error "tdoe")
("Q" toggle-debug-on-quit "tdoq"))
#+END_SRC
*** Programming
Don't try to check if there are files with a certain extension...it will lead to false positives.
@ -1775,24 +1621,24 @@ Don't try to check if there are files with a certain extension...it will lead to
(cond ((derived-mode-p 'emacs-lisp-mode
'inferior-emacs-lisp-mode
'debugger-mode)
(cp-el/body))
(my-elisp-hydra/body))
(;; (or (locate-dominating-file default-directory "build.scm")
;; (locate-dominating-file default-directory "main.ly"))
(derived-mode-p 'LilyPond-mode)
(cp-ly/body))
(my-lilypond-hydra/body))
((and (featurep 'geiser)
(bound-and-true-p geiser-mode))
(cond ((equal 'chicken geiser-impl--implementation)
(cp-cs/body))
(my-chicken-hydra/body))
((equal 'guile geiser-impl--implementation)
(cp-guile/body))
(t (error "Couldn't detect Scheme implementation."))))
((derived-mode-p 'lisp-mode 'slime-repl-mode)
(cp-cl/body))
(my-cl-hydra/body))
((derived-mode-p 'prolog-mode)
(cp-prolog/body))
(my-prolog-hydra/body))
((derived-mode-p 'org-mode)
(cp-org/body))
(my-org-hydra/body))
(t (contrapunctus-general-hydra/body))))
(define-key boon-command-map (kbd "m") #'my-dispatch-hydra)
@ -2524,7 +2370,7 @@ SLIME opens CLHS links in Firefox, but I'd rather open them in Tor Browser; Tor
(defun contrapunctus-disable-nameless-key ()
(define-key nameless-mode-map (kbd "C-c C--") nil)))
(defun cp-org-insert-block (type &optional lang)
(defun my-org-hydra-insert-block (type &optional lang)
"Insert block of TYPE at point, or at beginning and end of region.
TYPE should be an Org block type, e.g. SRC, QUOTE, etc.
@ -2556,7 +2402,7 @@ If TYPE is SRC, LANG should be the name of the language as a string, e.g. \"emac
;; (use-package org-src-mode
;; :hook (org-src-mode . (lambda () (when (derived-mode-p 'emacs-lisp-mode)))))
;; (defun cp-org-expand-all ()
;; (defun my-org-hydra-expand-all ()
;; (interactive)
;; ;; todo - define inner recursive function
;; (beginning-of-buffer)
@ -2634,7 +2480,7 @@ BUG - improper behaviour with checkboxes.
(remove-hook 'before-save-hook 'delete-trailing-whitespace)
(write-file (read-from-minibuffer "Output filename: "))))
(defun cp-org-set-tags ()
(defun my-org-hydra-set-tags ()
(interactive)
(let ((all-tags (org-get-buffer-tags))
(current-tags (org-get-tags)))
@ -2694,7 +2540,6 @@ BUG - improper behaviour with checkboxes.
(defun my-org-publish-html (plist filename pub-dir)
)
#+END_SRC
*** markdown-mode :editing:
#+BEGIN_SRC emacs-lisp
(use-package markdown-mode