From f44379e7feb79dd734318706abe5a000cff34c9b Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Tue, 4 May 2010 22:08:25 -0400 Subject: [PATCH] Use define-minor-mode for less obvious cases. * emacs-lisp/easy-mmode.el (define-minor-mode): Add :variable keyword. * emacs-lisp/cl-macs.el (terminal-parameter, eq): Add setf method. * international/iso-ascii.el (iso-ascii-mode): * frame.el (auto-raise-mode, auto-lower-mode): * composite.el (global-auto-composition-mode): Use define-minor-mode. --- etc/NEWS | 2 ++ lisp/ChangeLog | 8 +++++++ lisp/composite.el | 9 +++----- lisp/emacs-lisp/cl-macs.el | 13 ++++++++--- lisp/emacs-lisp/easy-mmode.el | 40 ++++++++++++++++++++------------- lisp/frame.el | 26 +++++++-------------- lisp/international/iso-ascii.el | 13 +++++------ lisp/progmodes/idlwave.el | 1 + 8 files changed, 62 insertions(+), 50 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 899c09a3cc5..22d66c2de5c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -186,6 +186,8 @@ Secret Service API requires D-Bus for communication. * Lisp changes in Emacs 24.1 +** define-minor-mode accepts a new keyword :variable. + ** delete-file now accepts an optional second arg, FORCE, which says to always delete and ignore the value of delete-by-moving-to-trash. diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 11648ed1abd..3edaea5d623 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,11 @@ +2010-05-05 Stefan Monnier + + * emacs-lisp/easy-mmode.el (define-minor-mode): Add :variable keyword. + * emacs-lisp/cl-macs.el (terminal-parameter, eq): Add setf method. + * international/iso-ascii.el (iso-ascii-mode): + * frame.el (auto-raise-mode, auto-lower-mode): + * composite.el (global-auto-composition-mode): Use define-minor-mode. + 2010-05-04 Michael Albinus * net/tramp.el (tramp-methods): Remove "-q" from `tramp-login-args' diff --git a/lisp/composite.el b/lisp/composite.el index 2b65839b93f..2204b351a36 100644 --- a/lisp/composite.el +++ b/lisp/composite.el @@ -764,16 +764,13 @@ You can use `global-auto-composition-mode' to turn on Auto Composition mode in all buffers (this is the default).") ;;;###autoload -(defun global-auto-composition-mode (&optional arg) +(define-minor-mode global-auto-composition-mode "Toggle Auto-Composition mode in every possible buffer. With prefix arg, turn Global-Auto-Composition mode on if and only if arg is positive. See `auto-composition-mode' for more information on Auto-Composition mode." - (interactive "P") - (setq-default auto-composition-mode - (if arg - (or (not (integerp arg)) (> arg 0)) - (not (default-value 'auto-composition-mode))))) + :variable (default-value 'auto-composition-mode)) + (defalias 'toggle-auto-composition 'auto-composition-mode) diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index 7d8108bcd87..e48835adeb1 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -1769,6 +1769,7 @@ Example: (defsetf frame-visible-p cl-set-frame-visible-p) (defsetf frame-width set-screen-width t) (defsetf frame-parameter set-frame-parameter t) +(defsetf terminal-parameter set-terminal-parameter) (defsetf getenv setenv t) (defsetf get-register set-register) (defsetf global-key-binding global-set-key) @@ -1821,10 +1822,16 @@ Example: (defsetf x-get-secondary-selection x-own-secondary-selection t) (defsetf x-get-selection x-own-selection t) +;; This is a hack that allows (setf (eq a 7) B) to mean either +;; (setq a 7) or (setq a nil) depending on whether B is nil or not. +;; This is useful when you have control over the PLACE but not over +;; the VALUE, as is the case in define-minor-mode's :variable. +(defsetf eq (a b) (v) `(setf ,a (if ,v ,b (not ,b)))) + ;;; More complex setf-methods. -;;; These should take &environment arguments, but since full arglists aren't -;;; available while compiling cl-macs, we fake it by referring to the global -;;; variable cl-macro-environment directly. +;; These should take &environment arguments, but since full arglists aren't +;; available while compiling cl-macs, we fake it by referring to the global +;; variable cl-macro-environment directly. (define-setf-method apply (func arg1 &rest rest) (or (and (memq (car-safe func) '(quote function function*)) diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index bebff6adae8..238f2fa551a 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el @@ -116,6 +116,8 @@ BODY contains code to execute each time the mode is activated or deactivated. :lighter SPEC Same as the LIGHTER argument. :keymap MAP Same as the KEYMAP argument. :require SYM Same as in `defcustom'. +:variable PLACE The location (as can be used with `setf') to use instead + of the variable MODE to store the state of the mode. For example, you could write (define-minor-mode foo-mode \"If enabled, foo on you!\" @@ -147,6 +149,8 @@ For example, you could write (type nil) (extra-args nil) (extra-keywords nil) + (variable nil) + (modefun mode) (require t) (hook (intern (concat mode-name "-hook"))) (hook-on (intern (concat mode-name "-on-hook"))) @@ -167,6 +171,7 @@ For example, you could write (:type (setq type (list :type (pop body)))) (:require (setq require (pop body))) (:keymap (setq keymap (pop body))) + (:variable (setq variable (setq mode (pop body)))) (t (push keyw extra-keywords) (push (pop body) extra-keywords)))) (setq keymap-sym (if (and keymap (symbolp keymap)) keymap @@ -187,12 +192,16 @@ For example, you could write `(progn ;; Define the variable to enable or disable the mode. - ,(if (not globalp) - `(progn - (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled. + ,(cond + ;; If :variable is specified, then the var will be + ;; declared elsewhere. + (variable nil) + ((not globalp) + `(progn + (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled. Use the command `%s' to change this variable." pretty-name mode)) - (make-variable-buffer-local ',mode)) - + (make-variable-buffer-local ',mode))) + (t (let ((base-doc-string (concat "Non-nil if %s is enabled. See the command `%s' for a description of this minor mode." @@ -207,10 +216,10 @@ or call the function `%s'.")))) ,@group ,@type ,@(unless (eq require t) `(:require ,require)) - ,@(nreverse extra-keywords)))) + ,@(nreverse extra-keywords))))) ;; The actual function. - (defun ,mode (&optional arg ,@extra-args) + (defun ,modefun (&optional arg ,@extra-args) ,(or doc (format (concat "Toggle %s on or off. Interactively, with no prefix argument, toggle the mode. @@ -221,11 +230,11 @@ With zero or negative ARG turn mode off. ;; repeat-command still does the toggling correctly. (interactive (list (or current-prefix-arg 'toggle))) (let ((,last-message (current-message))) - (setq ,mode - (if (eq arg 'toggle) - (not ,mode) - ;; A nil argument also means ON now. - (> (prefix-numeric-value arg) 0))) + (,(if (symbolp mode) 'setq 'setf) ,mode + (if (eq arg 'toggle) + (not ,mode) + ;; A nil argument also means ON now. + (> (prefix-numeric-value arg) 0))) ,@body ;; The on/off hooks are here for backward compatibility only. (run-hooks ',hook (if ,mode ',hook-on ',hook-off)) @@ -256,9 +265,10 @@ With zero or negative ARG turn mode off. (t (error "Invalid keymap %S" ,keymap)))) ,(format "Keymap for `%s'." mode-name))) - (add-minor-mode ',mode ',lighter - ,(if keymap keymap-sym - `(if (boundp ',keymap-sym) ,keymap-sym)))))) + ,(unless variable + `(add-minor-mode ',mode ',lighter + ,(if keymap keymap-sym + `(if (boundp ',keymap-sym) ,keymap-sym))))))) ;;; ;;; make global minor mode diff --git a/lisp/frame.el b/lisp/frame.el index 0628db7ee38..7456db4021c 100644 --- a/lisp/frame.el +++ b/lisp/frame.el @@ -24,6 +24,7 @@ ;;; Commentary: ;;; Code: +(eval-when-compile (require 'cl)) (defvar frame-creation-function-alist (list (cons nil @@ -1132,37 +1133,26 @@ To get the frame's current border color, use `frame-parameters'." (modify-frame-parameters (selected-frame) (list (cons 'border-color color-name)))) -(defun auto-raise-mode (arg) +(define-minor-mode auto-raise-mode "Toggle whether or not the selected frame should auto-raise. With ARG, turn auto-raise mode on if and only if ARG is positive. Note that this controls Emacs's own auto-raise feature. Some window managers allow you to enable auto-raise for certain windows. You can use that for Emacs windows if you wish, but if you do, that is beyond the control of Emacs and this command has no effect on it." - (interactive "P") - (if (null arg) - (setq arg - (if (cdr (assq 'auto-raise (frame-parameters (selected-frame)))) - -1 1))) - (if (> arg 0) - (raise-frame (selected-frame))) - (modify-frame-parameters (selected-frame) - (list (cons 'auto-raise (> arg 0))))) + :variable (frame-parameter nil 'auto-raise) + (if (frame-parameter nil 'auto-raise) + (raise-frame))) -(defun auto-lower-mode (arg) +(define-minor-mode auto-lower-mode "Toggle whether or not the selected frame should auto-lower. With ARG, turn auto-lower mode on if and only if ARG is positive. Note that this controls Emacs's own auto-lower feature. Some window managers allow you to enable auto-lower for certain windows. You can use that for Emacs windows if you wish, but if you do, that is beyond the control of Emacs and this command has no effect on it." - (interactive "P") - (if (null arg) - (setq arg - (if (cdr (assq 'auto-lower (frame-parameters (selected-frame)))) - -1 1))) - (modify-frame-parameters (selected-frame) - (list (cons 'auto-lower (> arg 0))))) + :variable (frame-parameter nil 'auto-lower)) + (defun set-frame-name (name) "Set the name of the selected frame to NAME. When called interactively, prompt for the name of the frame. diff --git a/lisp/international/iso-ascii.el b/lisp/international/iso-ascii.el index 268f4c89900..bbfc494a46d 100644 --- a/lisp/international/iso-ascii.el +++ b/lisp/international/iso-ascii.el @@ -33,6 +33,7 @@ ;;; Code: (require 'disp-table) +(eval-when-compile (require 'cl)) (defgroup iso-ascii nil "Set up char tables for ISO 8859/1 on ASCII terminals." @@ -162,15 +163,11 @@ (iso-ascii-display 254 "th") ; small thorn, Icelandic (iso-ascii-display 255 "\"y") ; small y with diaeresis or umlaut mark -(defun iso-ascii-mode (arg) +(define-minor-mode iso-ascii-mode "Toggle ISO-ASCII mode." - (interactive "P") - (unless arg - (setq arg (eq standard-display-table iso-ascii-standard-display-table))) - (setq standard-display-table - (if arg - iso-ascii-display-table - iso-ascii-standard-display-table))) + :variable (eq standard-display-table iso-ascii-display-table) + (unless standard-display-table + (setq standard-display-table iso-ascii-standard-display-table))) (provide 'iso-ascii) diff --git a/lisp/progmodes/idlwave.el b/lisp/progmodes/idlwave.el index e6228286a19..402893c5946 100644 --- a/lisp/progmodes/idlwave.el +++ b/lisp/progmodes/idlwave.el @@ -1370,6 +1370,7 @@ list order matters since matching an assignment statement exactly is not possible without parsing. Thus assignment statement become just the leftover unidentified statements containing an equal sign.") +;; FIXME: This var seems to only ever be set, but never actually used! (defvar idlwave-fill-function 'auto-fill-function "IDL mode auto fill function.") -- 2.39.2