help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Macro for defining a lineup of commands


From: Narendra Joshi
Subject: Macro for defining a lineup of commands
Date: Mon, 26 Jun 2017 14:29:31 +0530

Hi, 

I want to share a macro that I wrote recently. I really like it. I would
like to have suggestions to improve it and new use cases. :)
If there are better and more general alternatives to this, it would be
great to know. :)

,----
| (defmacro def-lineup (command docstring &rest commands)
|   "Make a COMMAND with DOCSTRING to run COMMANDS in order on repeated usage.
| Currently works for commands that need no interactive input.  It
| automatically makes the sequence a circular sequence
| logically.  That's because `(car nil)' equals nil.
| 
| Argument DOCSTRING would serve as the docstring for COMMAND.
| 
| Example usage:
| \(def-lineup fill-unfill
|             \"Cycle between filling a line, filling a para and unfill.\"
|             #'auto-fill-current-line #'fill-paragraph #'unfill-paragraph)
| 
| This would define a new command with the name fill-unfill that
| would run commands in '(auto-fill-current-line fill-paragraph 
unfill-paragraph)
| one after the other in a circular fashion, when called repeatedly."
|   `(defun ,command () ,docstring
|      (interactive)
|      (let* ((command-sequence
|              (if-let ((cseq (get this-command 'command-sequence))
|                       (_ (eq last-command this-command)))
|                  cseq
|                (list ,@commands)))
|             (next-command (car command-sequence)))
|        (message (format "-> %s" next-command))
|        (call-interactively next-command)
|        (put this-command 'command-sequence (cdr command-sequence)))))
`----


Best,
-- 
Narendra Joshi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]