Skip to content

Commit 050624d

Browse files
committed
fix(lib): nested interpolation & order of args for fn! macro
This fixes a couple bugs with this macro: - Nested %-refs (in nested fn!'s) were interpolated as arguments of the outer-most fn!. E.g. (fn! (fn! %2)) would expand to: Before this fix: (lambda (_%1 %2) (lambda (_%1 %2) %2)) After this fix: (lambda () (lambda (_%1 %2) %2)) - Unused arguments were not only listed in the wrong order, they were off-by-one. E.g. (fn! %3 %5) expands to (lambda (_%4 _%3 %3 _%1 %5) %3 %5) This never caused any actual issues, but it was unexpected. I've also moved the lookup table to `fn!`, and removed unnecessary entries from it.
1 parent e038505 commit 050624d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lisp/doom-lib.el

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,21 @@ ARGLIST."
322322
(allow-other-keys arglist))
323323
,@body)))
324324

325-
(put 'doom--fn-crawl 'lookup-table
326-
'((_ . 0) (_ . 1) (%2 . 2) (%3 . 3) (%4 . 4)
327-
(%5 . 5) (%6 . 6) (%7 . 7) (%8 . 8) (%9 . 9)))
325+
(put 'fn! 'lookup-table
326+
'((%2 . 2) (%3 . 3) (%4 . 4) (%5 . 5)
327+
(%6 . 6) (%7 . 7) (%8 . 8) (%9 . 9)))
328328
(defun doom--fn-crawl (data args)
329329
(cond ((symbolp data)
330330
(when-let
331331
(pos (cond ((eq data '%*) 0)
332332
((memq data '(% %1)) 1)
333-
((cdr (assq data (get 'doom--fn-crawl 'lookup-table))))))
333+
((cdr (assq data (get 'fn! 'lookup-table))))))
334334
(when (and (= pos 1)
335335
(aref args 1)
336336
(not (eq data (aref args 1))))
337337
(error "%% and %%1 are mutually exclusive"))
338338
(aset args pos data)))
339-
((and (not (eq (car-safe data) '!))
339+
((and (not (eq (car-safe data) 'fn!))
340340
(or (listp data)
341341
(vectorp data)))
342342
(let ((len (length data))
@@ -369,7 +369,7 @@ which expands to:
369369
(if %1 %3 (cadr %*)))
370370
371371
This macro was adapted from llama.el (see https://git.sr.ht/~tarsius/llama),
372-
minus font-locking, the outer function call, and minor optimizations."
372+
minus font-locking and the outer function call, plus some minor optimizations."
373373
`(lambda ,(let ((argv (make-vector 10 nil)))
374374
(doom--fn-crawl args argv)
375375
`(,@(let ((i (1- (length argv)))
@@ -379,7 +379,7 @@ minus font-locking, the outer function call, and minor optimizations."
379379
(setq sym (aref argv i))
380380
(unless (and (= n -1) (null sym))
381381
(cl-incf n)
382-
(push (or sym (intern (format "_%%%d" (1+ n))))
382+
(push (or sym (intern (format "_%%%d" i)))
383383
arglist))
384384
(cl-decf i))
385385
arglist)

0 commit comments

Comments
 (0)