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)
"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
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 ()
(assoc-string
@@ -46,7 +46,7 @@ success. The specific return value is unused outside of the test for nil.")
((stringp backend-fn)
(browse-url (format backend-fn
(url-encode-url
(read-string (format "Search for (on %s): "
(read-string (format "Search %s for: "
name)
query-string)))))
(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)
:query-string (when (use-region-p)
(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))
;;;###autoload
(defun syd-lookup-documentation (identifier)
"Try to find documentation on IDENTIFIER, and "
(interactive (list (syd-thing-at-point-or-region)))
(if-let ((r (syd-lookup--jump-to 'documentation identifier
:display-fn #'pop-to-buffer)))
r
((user-error "Couldn't find documentation on %S"
(substring-no-properties identifier)))))
(or (syd-lookup--jump-to 'documentation identifier
:display-fn #'pop-to-buffer)
(user-error "Couldn't find documentation on %S"
(substring-no-properties identifier))))
(defvar syd-lookup--handlers-by-category
'((documentation . syd-lookup-documentation-handlers)))
@@ -80,7 +79,6 @@ success. The specific return value is unused outside of the test for nil.")
;; handler.
(result (run-hook-wrapped handlers #'syd-lookup--run-handler
identifier origin)))
(message "result wrap hok: %S" result)
(unwind-protect
(when (cond ((null result)
(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
(message "Lookup handler %S threw an error: %s" handler e)
'fail))))
(message "result %S" result)
(cond ((eq result 'fail)
(set-window-configuration wconf)
nil)

View File

@@ -6,22 +6,21 @@
(defun syd--set-popup-rules-for-repls-h ()
(require 'doom-popup)
(set-popup-rule!
(lambda (bufname _)
(when (boundp 'syd-repl-mode)
(buffer-local-value 'syd-repl-mode (get-buffer bufname))))
:ttl (lambda (buf)
(unless (plist-get syd-repl-plist :persist)
(when-let (process (get-buffer-process buf))
(set-process-query-on-exit-flag process nil)
(kill-process process)
(kill-buffer buf))))
:size 0.25
:quit nil))
(add-hook 'on-init-ui-hook #'syd--set-popup-rules-for-repls-h 'append)
(syd-add-hook 'on-init-ui-hook
(defun syd--set-popup-rules-for-repls-h ()
(require 'doom-popup)
(set-popup-rule!
(lambda (bufname _)
(when (boundp 'syd-repl-mode)
(buffer-local-value 'syd-repl-mode (get-buffer bufname))))
:ttl (lambda (buf)
(unless (plist-get syd-repl-plist :persist)
(when-let (process (get-buffer-process buf))
(set-process-query-on-exit-flag process nil)
(kill-process process)
(kill-buffer buf))))
:size 0.25
:quit nil)))
;;; State & settings

View File

@@ -34,13 +34,10 @@ KEYS is an alist of the parsed keywords."
(push lag parsed-rest))))
(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
;; list for "affine" use with unquote-splicing.
(-let (((forms . (&alist :with-each with-each
:with-all with-all))
(syd-parse-rest-and-keys args))
(call-cont (lambda (cont arg)
(-let ((call-cont (lambda (cont arg)
(if cont
(list (funcall cont arg))
nil)))
@@ -48,9 +45,10 @@ KEYS is an alist of the parsed keywords."
`(progn
,@(cl-loop
for form in forms
appending (cond ((and (symbolp form) (functionp form))
(push form names)
(funcall call-cont with-each form))
appending (cond ((syd-hform-symbol form)
(let ((name (nth 1 form)))
(push name names)
(funcall call-cont with-each name)))
((syd-hform-defun form)
(let ((name (nth 1 form)))
(push name names)
@@ -65,6 +63,12 @@ KEYS is an alist of the parsed keywords."
(t (error "IDK!"))))
,@(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)
"If HFORM is a defun form, return the defun's name. Otherwise, return nil"
(when-let* ((sym (car-safe hform)))
@@ -109,11 +113,19 @@ not mutated; a new plist is returned."
(list prop* old-val))))
;; 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))
(dolist (hook (ensure-list hooks))
(dolist (fn functions)
(add-hook hook fn))))
(syd-lift-lambdas
:forms hforms
: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)
`(error "TODO: syd-silently"))

View File

@@ -30,7 +30,7 @@
"Lookup IDENTIFIER with `describe-symbol'"
;; 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
;; 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
;; already open.
(describe-symbol (intern identifier))

View File

@@ -17,17 +17,16 @@
org-agenda-new-buffers))
(run-hooks 'find-file-hook))
(defun syd-org-exclude-agenda-buffers-from-workspace-h ()
"Don't associate temporary agenda buffers with current workspace."
(when (and org-agenda-new-buffers
(bound-and-true-p persp-mode)
(not org-agenda-sticky))
(let (persp-autokill-buffer-on-remove)
(persp-remove-buffer org-agenda-new-buffers
(get-current-persp)
nil))))
(add-hook 'org-agenda-finalize-hook
#'syd-org-exclude-agenda-buffers-from-workspace-h)
(syd-add-hook 'org-agenda-finalize-hook
(defun syd-org-exclude-agenda-buffers-from-workspace-h ()
"Don't associate temporary agenda buffers with current workspace."
(when (and org-agenda-new-buffers
(bound-and-true-p persp-mode)
(not org-agenda-sticky))
(let (persp-autokill-buffer-on-remove)
(persp-remove-buffer org-agenda-new-buffers
(get-current-persp)
nil)))))
(defun syd-org--restart-mode-before-indirect-buffer-a (&optional buffer _)
"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)
(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.
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,
install a hook to restart `org-mode' when they're switched to so they can grow
up to be fully-fledged org-mode buffers."
:around #'org-get-agenda-file-buffer
(if-let* ((buf (org-find-base-buffer-visiting file)))
buf
(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
nil 'local))
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.
See https://lists.gnu.org/archive/html/emacs-orgmode/2019-07/msg00081.html."
:filter-return #'org-id-new
(if (eq org-id-method 'uuid)
(downcase uuid)
uuid))
(advice-add #'org-id-new
:filter-return #'syd-org--fix-inconsistent-uuidgen-case-a))
uuid)))
(defun syd-org-init-faces ()
(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)
(org-directory "~/org"))
:preface
;; Speed up initialisation by disabling modules we don't need.
(defvar org-modules
'(;; ol-w3m
;; ol-bbdb
@@ -164,18 +162,20 @@ See https://lists.gnu.org/archive/html/emacs-orgmode/2019-07/msg00081.html."
(org-roam-completion-everywhere t))
:config
(defun syd-org-init-roam-h ()
"Setup `org-roam' but don't immediately initialize its database.
Instead, initialize it when it will be actually needed."
"Setup `org-roam' but don't immediately initialize its database. Instead,
initialize it when it will be actually needed."
(cl-letf (((symbol-function #'org-roam-db-sync) #'ignore))
(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.
In case of failure, fail gracefully."
:before #'org-roam-db-query
(message "Initializing org-roam database...")
(advice-remove 'org-roam-db-query #'syd-org-roam-try-init-db-a)
(org-roam-db-sync))
(advice-add #'org-roam-db-query
:before #'syd-org-roam-try-init-db-a))
(org-roam-db-sync)))
(provide 'syd-org)
;;; syd-org.el ends here