refactor: Code tidying

This commit is contained in:
Madeleine Sydney
2025-02-13 14:17:16 -07:00
parent 03491612f2
commit e1709b2969
5 changed files with 71 additions and 63 deletions

View File

@@ -28,7 +28,7 @@ string.")
(defvar syd-lookup-documentation-handlers '(syd-lookup-online-documentation) (defvar syd-lookup-documentation-handlers '(syd-lookup-online-documentation)
"A list of lookup handlers used to find documentation. A lookup handler "A list of lookup handlers used to find documentation. A lookup handler
receives an identifier, and is expected to return nil on failure, and non-nil on receives an identifier, and is expected to return nil on failure, and non-nil on
success. The specific return value is unused outside of the test for nil.") success. When a handler returns a marker, the marker will be jumped to.")
(defun syd-lookup--prompt-for-online-backend () (defun syd-lookup--prompt-for-online-backend ()
(assoc-string (assoc-string
@@ -46,7 +46,7 @@ success. The specific return value is unused outside of the test for nil.")
((stringp backend-fn) ((stringp backend-fn)
(browse-url (format backend-fn (browse-url (format backend-fn
(url-encode-url (url-encode-url
(read-string (format "Search for (on %s): " (read-string (format "Search %s for: "
name) name)
query-string))))) query-string)))))
(t (signal 'wrong-type-argument `("backend" ,backend-fn)))))) (t (signal 'wrong-type-argument `("backend" ,backend-fn))))))
@@ -56,18 +56,17 @@ success. The specific return value is unused outside of the test for nil.")
(interactive (list (syd-lookup--prompt-for-online-backend) (interactive (list (syd-lookup--prompt-for-online-backend)
:query-string (when (use-region-p) :query-string (when (use-region-p)
(syd-thing-at-point-or-region)))) (syd-thing-at-point-or-region))))
(syd-lookup--call-online-backend (syd-lookup--prompt-for-online-backend) (syd-lookup--call-online-backend backend
:query-string query-string)) :query-string query-string))
;;;###autoload ;;;###autoload
(defun syd-lookup-documentation (identifier) (defun syd-lookup-documentation (identifier)
"Try to find documentation on IDENTIFIER, and " "Try to find documentation on IDENTIFIER, and "
(interactive (list (syd-thing-at-point-or-region))) (interactive (list (syd-thing-at-point-or-region)))
(if-let ((r (syd-lookup--jump-to 'documentation identifier (or (syd-lookup--jump-to 'documentation identifier
:display-fn #'pop-to-buffer))) :display-fn #'pop-to-buffer)
r (user-error "Couldn't find documentation on %S"
((user-error "Couldn't find documentation on %S" (substring-no-properties identifier))))
(substring-no-properties identifier)))))
(defvar syd-lookup--handlers-by-category (defvar syd-lookup--handlers-by-category
'((documentation . syd-lookup-documentation-handlers))) '((documentation . syd-lookup-documentation-handlers)))
@@ -80,7 +79,6 @@ success. The specific return value is unused outside of the test for nil.")
;; handler. ;; handler.
(result (run-hook-wrapped handlers #'syd-lookup--run-handler (result (run-hook-wrapped handlers #'syd-lookup--run-handler
identifier origin))) identifier origin)))
(message "result wrap hok: %S" result)
(unwind-protect (unwind-protect
(when (cond ((null result) (when (cond ((null result)
(message "No lookup handler could find %S" identifier) (message "No lookup handler could find %S" identifier)
@@ -105,7 +103,6 @@ success. The specific return value is unused outside of the test for nil.")
(error (error
(message "Lookup handler %S threw an error: %s" handler e) (message "Lookup handler %S threw an error: %s" handler e)
'fail)))) 'fail))))
(message "result %S" result)
(cond ((eq result 'fail) (cond ((eq result 'fail)
(set-window-configuration wconf) (set-window-configuration wconf)
nil) nil)

View File

@@ -6,7 +6,8 @@
(defun syd--set-popup-rules-for-repls-h () (syd-add-hook 'on-init-ui-hook
(defun syd--set-popup-rules-for-repls-h ()
(require 'doom-popup) (require 'doom-popup)
(set-popup-rule! (set-popup-rule!
(lambda (bufname _) (lambda (bufname _)
@@ -19,9 +20,7 @@
(kill-process process) (kill-process process)
(kill-buffer buf)))) (kill-buffer buf))))
:size 0.25 :size 0.25
:quit nil)) :quit nil)))
(add-hook 'on-init-ui-hook #'syd--set-popup-rules-for-repls-h 'append)
;;; State & settings ;;; State & settings

View File

@@ -34,13 +34,10 @@ KEYS is an alist of the parsed keywords."
(push lag parsed-rest)))) (push lag parsed-rest))))
(cons (reverse parsed-rest) parsed-keys))) (cons (reverse parsed-rest) parsed-keys)))
(cl-defun syd-lift-lambdas (&rest args) (cl-defun syd-lift-lambdas (&key with-each with-all forms)
;; Call the continuation if non-nil. Wraps the return value in a singleton ;; Call the continuation if non-nil. Wraps the return value in a singleton
;; list for "affine" use with unquote-splicing. ;; list for "affine" use with unquote-splicing.
(-let (((forms . (&alist :with-each with-each (-let ((call-cont (lambda (cont arg)
:with-all with-all))
(syd-parse-rest-and-keys args))
(call-cont (lambda (cont arg)
(if cont (if cont
(list (funcall cont arg)) (list (funcall cont arg))
nil))) nil)))
@@ -48,9 +45,10 @@ KEYS is an alist of the parsed keywords."
`(progn `(progn
,@(cl-loop ,@(cl-loop
for form in forms for form in forms
appending (cond ((and (symbolp form) (functionp form)) appending (cond ((syd-hform-symbol form)
(push form names) (let ((name (nth 1 form)))
(funcall call-cont with-each form)) (push name names)
(funcall call-cont with-each name)))
((syd-hform-defun form) ((syd-hform-defun form)
(let ((name (nth 1 form))) (let ((name (nth 1 form)))
(push name names) (push name names)
@@ -65,6 +63,12 @@ KEYS is an alist of the parsed keywords."
(t (error "IDK!")))) (t (error "IDK!"))))
,@(funcall call-cont with-all names)))) ,@(funcall call-cont with-all names))))
(defun syd-hform-symbol (hform)
(and (listp hform)
(= 2 (length hform))
(symbolp (nth 1 hform))
(memq (nth 0 hform) '(quote function))))
(defun syd-hform-defun (hform) (defun syd-hform-defun (hform)
"If HFORM is a defun form, return the defun's name. Otherwise, return nil" "If HFORM is a defun form, return the defun's name. Otherwise, return nil"
(when-let* ((sym (car-safe hform))) (when-let* ((sym (car-safe hform)))
@@ -109,11 +113,19 @@ not mutated; a new plist is returned."
(list prop* old-val)))) (list prop* old-val))))
;; TODO: Support (syd-add-hook 'hook (defun my-hook () ...)) ;; TODO: Support (syd-add-hook 'hook (defun my-hook () ...))
(defun syd-add-hook (hooks &rest functions) (defmacro syd-add-hook (hooks &rest hforms)
(declare (indent defun)) (declare (indent defun))
(dolist (hook (ensure-list hooks)) (syd-lift-lambdas
(dolist (fn functions) :forms hforms
(add-hook hook fn)))) :with-all (lambda (fns)
(let ((fn* (gensym "fn"))
(fns* (gensym "fns"))
(hook* (gensym "hook")))
`(let ((,fns* (list ,@(--map `(function ,it)
fns))))
(dolist (,hook* (ensure-list ,hooks))
(dolist (,fn* ,fns*)
(add-hook ,hook* ,fn*))))))))
(defmacro syd-silently (&rest body) (defmacro syd-silently (&rest body)
`(error "TODO: syd-silently")) `(error "TODO: syd-silently"))

View File

@@ -30,7 +30,7 @@
"Lookup IDENTIFIER with `describe-symbol'" "Lookup IDENTIFIER with `describe-symbol'"
;; HACK: Much to my frustration, `describe-symbol' has no defined return ;; HACK: Much to my frustration, `describe-symbol' has no defined return
;; value. To test if the call was successful or not, we check if any window ;; value. To test if the call was successful or not, we check if any window
;; is displaying the help buffer. This probably breaks if ;; is displaying the help buffer. This breaks if
;; `syd-emacs-lisp-lookup-documentation' is called while the help buffer is ;; `syd-emacs-lisp-lookup-documentation' is called while the help buffer is
;; already open. ;; already open.
(describe-symbol (intern identifier)) (describe-symbol (intern identifier))

View File

@@ -17,6 +17,7 @@
org-agenda-new-buffers)) org-agenda-new-buffers))
(run-hooks 'find-file-hook)) (run-hooks 'find-file-hook))
(syd-add-hook 'org-agenda-finalize-hook
(defun syd-org-exclude-agenda-buffers-from-workspace-h () (defun syd-org-exclude-agenda-buffers-from-workspace-h ()
"Don't associate temporary agenda buffers with current workspace." "Don't associate temporary agenda buffers with current workspace."
(when (and org-agenda-new-buffers (when (and org-agenda-new-buffers
@@ -25,9 +26,7 @@
(let (persp-autokill-buffer-on-remove) (let (persp-autokill-buffer-on-remove)
(persp-remove-buffer org-agenda-new-buffers (persp-remove-buffer org-agenda-new-buffers
(get-current-persp) (get-current-persp)
nil)))) nil)))))
(add-hook 'org-agenda-finalize-hook
#'syd-org-exclude-agenda-buffers-from-workspace-h)
(defun syd-org--restart-mode-before-indirect-buffer-a (&optional buffer _) (defun syd-org--restart-mode-before-indirect-buffer-a (&optional buffer _)
"Restart `org-mode' in buffers in which the mode has been deferred (see "Restart `org-mode' in buffers in which the mode has been deferred (see
@@ -42,13 +41,15 @@ via an indirect buffer."
:before #'syd-org--restart-mode-before-indirect-buffer-a) :before #'syd-org--restart-mode-before-indirect-buffer-a)
(defvar recentf-exclude) (defvar recentf-exclude)
(defun syd-org--optimize-backgrounded-agenda-buffers-a (fn file)
(syd-defadvice syd-org--optimize-backgrounded-agenda-buffers-a (fn file)
"Disable `org-mode's startup processes for temporary agenda buffers. "Disable `org-mode's startup processes for temporary agenda buffers.
Prevents recentf pollution as well. However, if the user tries to visit one of Prevents recentf pollution as well. However, if the user tries to visit one of
these buffers they'll see a gimped, half-broken org buffer, so to avoid that, these buffers they'll see a gimped, half-broken org buffer, so to avoid that,
install a hook to restart `org-mode' when they're switched to so they can grow install a hook to restart `org-mode' when they're switched to so they can grow
up to be fully-fledged org-mode buffers." up to be fully-fledged org-mode buffers."
:around #'org-get-agenda-file-buffer
(if-let* ((buf (org-find-base-buffer-visiting file))) (if-let* ((buf (org-find-base-buffer-visiting file)))
buf buf
(let ((recentf-exclude '(always)) (let ((recentf-exclude '(always))
@@ -63,18 +64,14 @@ up to be fully-fledged org-mode buffers."
(add-hook 'on-switch-buffer-hook #'syd-org--restart-mode-h (add-hook 'on-switch-buffer-hook #'syd-org--restart-mode-h
nil 'local)) nil 'local))
buf)))) buf))))
(advice-add #'org-get-agenda-file-buffer
:around #'syd-org--optimize-backgrounded-agenda-buffers-a)
(defun syd-org--fix-inconsistent-uuidgen-case-a (uuid) (syd-defadvice syd-org--fix-inconsistent-uuidgen-case-a (uuid)
"Ensure uuidgen is always lowercase (consistent) regardless of system. "Ensure uuidgen is always lowercase (consistent) regardless of system.
See https://lists.gnu.org/archive/html/emacs-orgmode/2019-07/msg00081.html." See https://lists.gnu.org/archive/html/emacs-orgmode/2019-07/msg00081.html."
:filter-return #'org-id-new :filter-return #'org-id-new
(if (eq org-id-method 'uuid) (if (eq org-id-method 'uuid)
(downcase uuid) (downcase uuid)
uuid)) uuid)))
(advice-add #'org-id-new
:filter-return #'syd-org--fix-inconsistent-uuidgen-case-a))
(defun syd-org-init-faces () (defun syd-org-init-faces ()
(let ((headline `(:weight bold))) (let ((headline `(:weight bold)))
@@ -123,6 +120,7 @@ See https://lists.gnu.org/archive/html/emacs-orgmode/2019-07/msg00081.html."
:custom ((org-startup-folded 'content) :custom ((org-startup-folded 'content)
(org-directory "~/org")) (org-directory "~/org"))
:preface :preface
;; Speed up initialisation by disabling modules we don't need.
(defvar org-modules (defvar org-modules
'(;; ol-w3m '(;; ol-w3m
;; ol-bbdb ;; ol-bbdb
@@ -164,18 +162,20 @@ See https://lists.gnu.org/archive/html/emacs-orgmode/2019-07/msg00081.html."
(org-roam-completion-everywhere t)) (org-roam-completion-everywhere t))
:config :config
(defun syd-org-init-roam-h () (defun syd-org-init-roam-h ()
"Setup `org-roam' but don't immediately initialize its database. "Setup `org-roam' but don't immediately initialize its database. Instead,
Instead, initialize it when it will be actually needed." initialize it when it will be actually needed."
(cl-letf (((symbol-function #'org-roam-db-sync) #'ignore)) (cl-letf (((symbol-function #'org-roam-db-sync) #'ignore))
(org-roam-db-autosync-enable))) (org-roam-db-autosync-enable)))
(defun syd-org-roam-try-init-db-a (&rest _)
(syd-org--init-roam-keybinds)
(syd-defadvice syd-org-roam-try-init-db-a (&rest _)
"Try to initialize org-roam database at the last possible safe moment. "Try to initialize org-roam database at the last possible safe moment.
In case of failure, fail gracefully." In case of failure, fail gracefully."
:before #'org-roam-db-query
(message "Initializing org-roam database...") (message "Initializing org-roam database...")
(advice-remove 'org-roam-db-query #'syd-org-roam-try-init-db-a) (advice-remove 'org-roam-db-query #'syd-org-roam-try-init-db-a)
(org-roam-db-sync)) (org-roam-db-sync)))
(advice-add #'org-roam-db-query
:before #'syd-org-roam-try-init-db-a))
(provide 'syd-org) (provide 'syd-org)
;;; syd-org.el ends here ;;; syd-org.el ends here