emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] trunk r113298: lisp/simple.el (alternatives-define): New m


From: Juanma Barranquero
Subject: [Emacs-diffs] trunk r113298: lisp/simple.el (alternatives-define): New macro.
Date: Sat, 06 Jul 2013 09:35:53 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 113298
revision-id: address@hidden
parent: address@hidden
committer: Juanma Barranquero <address@hidden>
branch nick: trunk
timestamp: Sat 2013-07-06 11:35:37 +0200
message:
  lisp/simple.el (alternatives-define): New macro.
  etc/NEWS: Document new "generic commands" support.
modified:
  etc/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1485
  etc/NEWS                       news-20100311060928-aoit31wvzf25yr1z-1
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/simple.el                 simple.el-20091113204419-o5vbwnq5f7feedwu-403
=== modified file 'etc/ChangeLog'
--- a/etc/ChangeLog     2013-06-27 09:08:14 +0000
+++ b/etc/ChangeLog     2013-07-06 09:35:37 +0000
@@ -1,3 +1,7 @@
+2013-07-06  Juanma Barranquero  <address@hidden>
+
+       * NEWS: Document new "generic commands" support.
+
 2013-06-27  Juanma Barranquero  <address@hidden>
 
        * NEWS: Document new Desktop option `desktop-save-windows'.

=== modified file 'etc/NEWS'
--- a/etc/NEWS  2013-07-04 10:25:54 +0000
+++ b/etc/NEWS  2013-07-06 09:35:37 +0000
@@ -133,6 +133,10 @@
 `x-display-pixel-width', `x-display-pixel-height', `display-mm-width',
 `display-mm-height', `x-display-mm-width', and `x-display-mm-height'.
 
+** New macro `alternatives-define' can be used to define generic commands.
+Generic commands are interactive functions whose implementation can be
+selected among several alternatives, as a matter of user preference.
+
 
 * Editing Changes in Emacs 24.4
 

=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2013-07-06 00:10:54 +0000
+++ b/lisp/ChangeLog    2013-07-06 09:35:37 +0000
@@ -1,3 +1,7 @@
+2013-07-06  Juanma Barranquero  <address@hidden>
+
+       * simple.el (alternatives-define): New macro.
+
 2013-07-06  Stefan Monnier  <address@hidden>
 
        * subr.el (read-quoted-char): Use read-key.

=== modified file 'lisp/simple.el'
--- a/lisp/simple.el    2013-06-26 00:50:50 +0000
+++ b/lisp/simple.el    2013-07-06 09:35:37 +0000
@@ -7346,6 +7346,67 @@
     (with-eval-after-load pkg
       (bad-package-check pkg))))
 
+
+;;; Generic dispatcher commands
+
+;; Macro `alternatives-define' is used to create generic commands.
+;; Generic commands are these (like web, mail, news, encrypt, irc, etc.)
+;; that can have different alternative implementations where choosing
+;; among them is exclusively a matter of user preference.
+
+;; (alternatives-define COMMAND) creates a new interactive command
+;; M-x COMMAND and a customizable variable COMMAND-alternatives.
+;; Typically, the user will not need to customize this variable; packages
+;; wanting to add alternative implementations should use
+;;
+;; ;;;###autoload (push '("My impl name" . my-impl-symbol) COMMAND-alternatives
+
+(defmacro alternatives-define (command &rest customizations)
+  "Define new command `COMMAND'.
+The variable `COMMAND-alternatives' will contain alternative
+implementations of COMMAND, so that running `C-u M-x COMMAND'
+will allow the user to chose among them.
+CUSTOMIZATIONS, if non-nil, should be composed of alternating
+`defcustom' keywords and values to add to the declaration of
+`COMMAND-alternatives' (typically to add new groups)."
+  (let* ((command-name (symbol-name command))
+         (varalt-name (concat command-name "-alternatives"))
+         (varalt-sym (intern varalt-name))
+         (varimp-sym (intern (concat command-name "--implementation"))))
+    `(progn
+
+       (defcustom ,varalt-sym nil
+         ,(format "Alist of alternative implementations for the `%s' command.
+
+Each entry must be a pair (ALTNAME . ALTFUN), where:
+ALTNAME - The name shown at user to describe the alternative implementation.
+ALTFUN  - The function called to implement this alternative."
+                  command-name)
+         :type '(alist :key-type string :value-type function)
+         :group 'dispatcher
+         ,@customizations)
+
+       (defvar ,varimp-sym nil "Internal use only.")
+
+       (defun ,command (&optional arg)
+         ,(format "Run generic command `%s'.
+If used for the first time, or with interactive ARG, ask the user which
+implementation to use for `%s'.  The variable `%s'
+contains the list of implementations currently supported for this command."
+                  command-name command-name varalt-name)
+         (interactive "P")
+         (when (or arg (null ,varimp-sym))
+           (let ((val (completing-read
+                       ,(format "Select implementation for command `%s': " 
command-name)
+                       ,varalt-sym nil t)))
+             (unless (string-equal val "")
+               (customize-save-variable ',varimp-sym
+                                        (cdr (assoc-string val 
,varalt-sym))))))
+         (if ,varimp-sym
+             (funcall ,varimp-sym)
+           (message ,(format "No implementation selected for command `%s'"
+                             command-name)))))))
+
 (provide 'simple)
 
 ;;; simple.el ends here


reply via email to

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