Add cmd!, cmd!!, cmds! convenience macros

It's kind of silly that our command lambda macros (λ! and λ!!) need a
snippet, special key sequence or copy-paste to insert, so in the spirit
of fn! -- and to make sure they take up less space than `lambda!` --
I've added `cmd!` and `cmd!!` aliases. `lambda!` and `lambda!!` are now
deprecated. λ! and λ!! will remain.

I've also added `cmds!` as a convenience wrapper around
general-predicate-dispatch.
This commit is contained in:
Henrik Lissner 2020-05-27 16:12:45 -04:00
parent d4d8c48265
commit a9bf0b8985
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
5 changed files with 68 additions and 62 deletions

View file

@ -139,24 +139,6 @@ at the values with which this function was called."
;;
;;; Sugars
(defmacro λ! (&rest body)
"Expands to (lambda () (interactive) ,@body).
A factory for quickly producing interaction commands, particularly for keybinds
or aliases."
(declare (doc-string 1) (pure t) (side-effect-free t))
`(lambda (&rest _) (interactive) ,@body))
(defalias 'lambda! 'λ!)
(defun λ!! (command &optional arg)
"Expands to a command that interactively calls COMMAND with prefix ARG.
A factory for quickly producing interactive, prefixed commands for keybinds or
aliases."
(declare (doc-string 1) (pure t) (side-effect-free t))
(lambda (&rest _) (interactive)
(let ((current-prefix-arg arg))
(call-interactively command))))
(defalias 'lambda!! 'λ!!)
(defun dir! ()
"Returns the directory of the emacs lisp file this macro is called from."
(when-let (path (file!))
@ -256,6 +238,40 @@ See `if!' for details on this macro's purpose."
(declare (indent defun) (doc-string 1) (pure t) (side-effect-free t))
`(cl-function (lambda ,arglist ,@body)))
(defmacro cmd! (&rest body)
"Expands to (lambda () (interactive) ,@body).
A factory for quickly producing interaction commands, particularly for keybinds
or aliases."
(declare (doc-string 1) (pure t) (side-effect-free t))
`(lambda (&rest _) (interactive) ,@body))
(defmacro cmd!! (command &rest args)
"Expands to a closure that interactively calls COMMAND with ARGS.
A factory for quickly producing interactive, prefixed commands for keybinds or
aliases."
(declare (doc-string 1) (pure t) (side-effect-free t))
`(lambda (&rest _) (interactive)
(funcall-interactively ,command ,@args)))
(defmacro cmds! (&rest branches)
"Expands to a `menu-item' dispatcher for keybinds."
(declare (doc-string 1))
(let ((docstring (if (stringp (car branches)) (pop branches) ""))
fallback)
(when (cl-oddp (length branches))
(setq fallback (car (last branches))
branches (butlast branches)))
`(general-predicate-dispatch ,fallback
:docstring ,docstring
,@branches)))
;; For backwards compatibility
(defalias 'λ! 'cmd!)
(defalias 'λ!! 'cmd!!)
;; DEPRECATED These have been superseded by `cmd!' and `cmd!!'
(define-obsolete-function-alias 'lambda! 'cmd! "3.0.0")
(define-obsolete-function-alias 'lambda!! 'cmd!! "3.0.0")
;;; Mutation
(defmacro appendq! (sym &rest lists)