From 58d2b5c1135cd30612a2ef30cc046afaf16beb42 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Wed, 22 Jun 2016 20:00:21 -0700 Subject: [PATCH] Make imalison:compose handle undefined functions Because help-function-arglist returns t when it encounters an undefined function, imalison:compose was choking when it was evaluated on functions that are not defined at the time of macro expansion. --- dotfiles/emacs.d/README.org | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/dotfiles/emacs.d/README.org b/dotfiles/emacs.d/README.org index 86e006b6..9b5823ce 100644 --- a/dotfiles/emacs.d/README.org +++ b/dotfiles/emacs.d/README.org @@ -418,25 +418,20 @@ The packages in this section provide no functionality on their own, but provide #+END_SRC **** A Version Supporting Macros #+BEGIN_SRC emacs-lisp - (defun imalison:args-expander (function-name) - (lambda (args) - (apply function-name args))) + (defun imalison:help-function-arglist (function) + (let ((result (help-function-arglist function ))) + (if (eq result t) '(&rest args) result))) (defmacro imalison:compose (&rest funcs) (let* ((last-function (car (last funcs))) - (arguments (help-function-arglist last-function)) - (call-arguments (delq '&optional arguments))) + (arguments (imalison:help-function-arglist last-function)) + (call-arguments (delq '&optional arguments))) (when (memq '&rest arguments) (setq arguments '(&rest args)) - (setq call-arguments '(args)) - (setq funcs - (-replace-at (- (length arguments) 1) - (imalison:args-expander last-function) - funcs))) + (setq call-arguments '(args))) `(imalison:compose-argspec ,arguments ,call-arguments ,@funcs))) - (defmacro imalison:compose-argspec - (arguments call-arguments &rest funcs) + (defmacro imalison:compose-argspec (arguments call-arguments &rest funcs) "Build a new function with NAME that is the composition of FUNCS." `(lambda ,arguments (imalison:compose-helper ,funcs ,call-arguments))) @@ -444,7 +439,10 @@ The packages in this section provide no functionality on their own, but provide (defmacro imalison:compose-helper (funcs arguments) "Builds funcalls of FUNCS applied to the arg." (if (equal (length funcs) 1) - `(,(car funcs) ,@arguments) + (let ((last-function (car funcs))) + (when (memq '&rest (imalison:help-function-arglist last-function)) + (setq last-function (apply-partially 'apply last-function))) + `(,last-function ,@arguments)) `(,(car funcs) (imalison:compose-helper ,(cdr funcs) ,arguments)))) #+END_SRC @@ -814,7 +812,7 @@ A macro for composing functions together to build an interactive command to copy *** Copy the current branch using magit #+BEGIN_SRC emacs-lisp (imalison:compose-copy-builder imalison:copy-current-git-branch - 'magit-get-current-branch) + magit-get-current-branch) #+END_SRC ** Advice Add Around Builder For composing functions with an apply so that they can be used with the ~:around~ keyword of advice-add