aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuanma Barranquero2013-07-06 11:35:37 +0200
committerJuanma Barranquero2013-07-06 11:35:37 +0200
commit61e56e2c8ae166d7a4430b0ba717f71faae78ff9 (patch)
tree0d375c7b824cf1a894f507f15d2063b16ebe120e
parent3323c263c78e74385bc03d5270cdd44399b0a8d1 (diff)
downloademacs-61e56e2c8ae166d7a4430b0ba717f71faae78ff9.tar.gz
emacs-61e56e2c8ae166d7a4430b0ba717f71faae78ff9.zip
lisp/simple.el (alternatives-define): New macro.
etc/NEWS: Document new "generic commands" support.
-rw-r--r--etc/ChangeLog4
-rw-r--r--etc/NEWS4
-rw-r--r--lisp/ChangeLog4
-rw-r--r--lisp/simple.el61
4 files changed, 73 insertions, 0 deletions
diff --git a/etc/ChangeLog b/etc/ChangeLog
index 93ba05bc377..9dbcc70ee01 100644
--- a/etc/ChangeLog
+++ b/etc/ChangeLog
@@ -1,3 +1,7 @@
12013-07-06 Juanma Barranquero <lekktu@gmail.com>
2
3 * NEWS: Document new "generic commands" support.
4
12013-06-27 Juanma Barranquero <lekktu@gmail.com> 52013-06-27 Juanma Barranquero <lekktu@gmail.com>
2 6
3 * NEWS: Document new Desktop option `desktop-save-windows'. 7 * NEWS: Document new Desktop option `desktop-save-windows'.
diff --git a/etc/NEWS b/etc/NEWS
index 9c76c2e0165..077ee1c6196 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -133,6 +133,10 @@ monitor, use the new functions above. Similar notes also apply to
133`x-display-pixel-width', `x-display-pixel-height', `display-mm-width', 133`x-display-pixel-width', `x-display-pixel-height', `display-mm-width',
134`display-mm-height', `x-display-mm-width', and `x-display-mm-height'. 134`display-mm-height', `x-display-mm-width', and `x-display-mm-height'.
135 135
136** New macro `alternatives-define' can be used to define generic commands.
137Generic commands are interactive functions whose implementation can be
138selected among several alternatives, as a matter of user preference.
139
136 140
137* Editing Changes in Emacs 24.4 141* Editing Changes in Emacs 24.4
138 142
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index dc554bc542d..901e582d15e 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,7 @@
12013-07-06 Juanma Barranquero <lekktu@gmail.com>
2
3 * simple.el (alternatives-define): New macro.
4
12013-07-06 Stefan Monnier <monnier@iro.umontreal.ca> 52013-07-06 Stefan Monnier <monnier@iro.umontreal.ca>
2 6
3 * subr.el (read-quoted-char): Use read-key. 7 * subr.el (read-quoted-char): Use read-key.
diff --git a/lisp/simple.el b/lisp/simple.el
index 61f32363dbe..9e83a224f5d 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -7346,6 +7346,67 @@ warning using STRING as the message.")
7346 (with-eval-after-load pkg 7346 (with-eval-after-load pkg
7347 (bad-package-check pkg)))) 7347 (bad-package-check pkg))))
7348 7348
7349
7350;;; Generic dispatcher commands
7351
7352;; Macro `alternatives-define' is used to create generic commands.
7353;; Generic commands are these (like web, mail, news, encrypt, irc, etc.)
7354;; that can have different alternative implementations where choosing
7355;; among them is exclusively a matter of user preference.
7356
7357;; (alternatives-define COMMAND) creates a new interactive command
7358;; M-x COMMAND and a customizable variable COMMAND-alternatives.
7359;; Typically, the user will not need to customize this variable; packages
7360;; wanting to add alternative implementations should use
7361;;
7362;; ;;;###autoload (push '("My impl name" . my-impl-symbol) COMMAND-alternatives
7363
7364(defmacro alternatives-define (command &rest customizations)
7365 "Define new command `COMMAND'.
7366The variable `COMMAND-alternatives' will contain alternative
7367implementations of COMMAND, so that running `C-u M-x COMMAND'
7368will allow the user to chose among them.
7369CUSTOMIZATIONS, if non-nil, should be composed of alternating
7370`defcustom' keywords and values to add to the declaration of
7371`COMMAND-alternatives' (typically to add new groups)."
7372 (let* ((command-name (symbol-name command))
7373 (varalt-name (concat command-name "-alternatives"))
7374 (varalt-sym (intern varalt-name))
7375 (varimp-sym (intern (concat command-name "--implementation"))))
7376 `(progn
7377
7378 (defcustom ,varalt-sym nil
7379 ,(format "Alist of alternative implementations for the `%s' command.
7380
7381Each entry must be a pair (ALTNAME . ALTFUN), where:
7382ALTNAME - The name shown at user to describe the alternative implementation.
7383ALTFUN - The function called to implement this alternative."
7384 command-name)
7385 :type '(alist :key-type string :value-type function)
7386 :group 'dispatcher
7387 ,@customizations)
7388
7389 (defvar ,varimp-sym nil "Internal use only.")
7390
7391 (defun ,command (&optional arg)
7392 ,(format "Run generic command `%s'.
7393If used for the first time, or with interactive ARG, ask the user which
7394implementation to use for `%s'. The variable `%s'
7395contains the list of implementations currently supported for this command."
7396 command-name command-name varalt-name)
7397 (interactive "P")
7398 (when (or arg (null ,varimp-sym))
7399 (let ((val (completing-read
7400 ,(format "Select implementation for command `%s': " command-name)
7401 ,varalt-sym nil t)))
7402 (unless (string-equal val "")
7403 (customize-save-variable ',varimp-sym
7404 (cdr (assoc-string val ,varalt-sym))))))
7405 (if ,varimp-sym
7406 (funcall ,varimp-sym)
7407 (message ,(format "No implementation selected for command `%s'"
7408 command-name)))))))
7409
7349(provide 'simple) 7410(provide 'simple)
7350 7411
7351;;; simple.el ends here 7412;;; simple.el ends here