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"))