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

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