diff options
| author | Stefan Monnier | 2000-06-04 20:59:47 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2000-06-04 20:59:47 +0000 |
| commit | b5bbbb761254076f89f7ba9dec151b288358e2ac (patch) | |
| tree | b0b6f1374b3d77f6ab7fa58eeed4e72c6f17f3d2 /lisp | |
| parent | 703af3d57eaa462ed2211173f94f42717958f1ab (diff) | |
| download | emacs-b5bbbb761254076f89f7ba9dec151b288358e2ac.tar.gz emacs-b5bbbb761254076f89f7ba9dec151b288358e2ac.zip | |
(easy-mmode-define-toggle): Remove (inline into define-minor-mode).
(easy-mmode-pretty-mode-name): Rename from easy-mmode-derive-name
and improve to use the lighter to guess the capitalization.
(define-minor-mode): Inline code from easy-mmode-define-toggle.
Add keyword arguments to specify global-ness or the custom group.
Add local-map and help-echo properties to the lighter.
(easy-mmode-define-navigation): Add the errors to debug-ignored-errors.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/ChangeLog | 14 | ||||
| -rw-r--r-- | lisp/emacs-lisp/easy-mmode.el | 134 |
2 files changed, 91 insertions, 57 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 6a253e00f1f..a3baeb08f70 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,14 @@ | |||
| 1 | 2000-06-04 Stefan Monnier <monnier@cs.yale.edu> | ||
| 2 | |||
| 3 | * emacs-lisp/easy-mmode.el (easy-mmode-define-toggle): | ||
| 4 | Remove (inline into define-minor-mode). | ||
| 5 | (easy-mmode-pretty-mode-name): Rename from easy-mmode-derive-name | ||
| 6 | and improve to use the lighter to guess the capitalization. | ||
| 7 | (define-minor-mode): Inline code from easy-mmode-define-toggle. | ||
| 8 | Add keyword arguments to specify global-ness or the custom group. | ||
| 9 | Add local-map and help-echo properties to the lighter. | ||
| 10 | (easy-mmode-define-navigation): Add the errors to debug-ignored-errors. | ||
| 11 | |||
| 1 | 2000-06-02 Dave Love <fx@gnu.org> | 12 | 2000-06-02 Dave Love <fx@gnu.org> |
| 2 | 13 | ||
| 3 | * wid-edit.el: byte-compile-dynamic since we typically don't use | 14 | * wid-edit.el: byte-compile-dynamic since we typically don't use |
| @@ -19,8 +30,7 @@ | |||
| 19 | (widget-convert): Use keywordp. | 30 | (widget-convert): Use keywordp. |
| 20 | (widget-leave-text, widget-children-value-delete): Use mapc. | 31 | (widget-leave-text, widget-children-value-delete): Use mapc. |
| 21 | (widget-keymap): Remove XEmacs stuff. | 32 | (widget-keymap): Remove XEmacs stuff. |
| 22 | (widget-field-keymap, widget-text-keymap): Define all inside | 33 | (widget-field-keymap, widget-text-keymap): Define all inside defvar. |
| 23 | defvar. | ||
| 24 | (widget-button-click): Don't set point at the click, but re-centre | 34 | (widget-button-click): Don't set point at the click, but re-centre |
| 25 | if we scroll out of window. Rewritten for images v. glyphs &c. | 35 | if we scroll out of window. Rewritten for images v. glyphs &c. |
| 26 | (widget-tabable-at): Use POS arg, not point. | 36 | (widget-tabable-at): Use POS arg, not point. |
diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index 130cc6877a4..8f8fcf49184 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el | |||
| @@ -51,72 +51,68 @@ | |||
| 51 | 51 | ||
| 52 | ;;; Code: | 52 | ;;; Code: |
| 53 | 53 | ||
| 54 | (defmacro easy-mmode-define-toggle (mode &optional doc &rest body) | 54 | (defun easy-mmode-pretty-mode-name (mode &optional lighter) |
| 55 | "Define a one arg toggle mode MODE function and associated hooks. | 55 | "Turn the symbol MODE into a string intended for the user. |
| 56 | MODE is the so defined function that toggles the mode. | 56 | If provided LIGHTER will be used to help choose capitalization." |
| 57 | optional DOC is its associated documentation. | 57 | (let* ((case-fold-search t) |
| 58 | BODY is executed after the toggling and before running MODE-hook." | 58 | (name (concat (capitalize (replace-regexp-in-string |
| 59 | (let* ((mode-name (symbol-name mode)) | 59 | "-mode\\'" "" (symbol-name mode))) |
| 60 | (pretty-name (easy-mmode-derive-name mode-name)) | 60 | " mode"))) |
| 61 | (hook (intern (concat mode-name "-hook"))) | 61 | (if (not (stringp lighter)) name |
| 62 | (hook-on (intern (concat mode-name "-on-hook"))) | 62 | (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\-s+\\'" "" lighter)) |
| 63 | (hook-off (intern (concat mode-name "-off-hook"))) | 63 | (replace-regexp-in-string lighter lighter name t t)))) |
| 64 | (toggle-doc (or doc | ||
| 65 | (format "With no argument, toggle %s. | ||
| 66 | With universal prefix ARG turn mode on. | ||
| 67 | With zero or negative ARG turn mode off. | ||
| 68 | \\{%s}" pretty-name (concat mode-name "-map"))))) | ||
| 69 | `(progn | ||
| 70 | (defcustom ,hook nil | ||
| 71 | ,(format "Hook called at the end of function `%s'." mode-name) | ||
| 72 | :type 'hook) | ||
| 73 | |||
| 74 | (defun ,mode (&optional arg) | ||
| 75 | ,toggle-doc | ||
| 76 | (interactive "P") | ||
| 77 | (setq ,mode | ||
| 78 | (if arg | ||
| 79 | (> (prefix-numeric-value arg) 0) | ||
| 80 | (not ,mode))) | ||
| 81 | ,@body | ||
| 82 | ;; The on/off hooks are here for backward compatibility. | ||
| 83 | (run-hooks ',hook (if ,mode ',hook-on ',hook-off)) | ||
| 84 | ;; Return the new setting. | ||
| 85 | (if (interactive-p) | ||
| 86 | (message ,(format "%s %%sabled" pretty-name) | ||
| 87 | (if ,mode "en" "dis"))) | ||
| 88 | ,mode)))) | ||
| 89 | |||
| 90 | (defun easy-mmode-derive-name (mode) | ||
| 91 | (replace-regexp-in-string | ||
| 92 | "-Mode" " mode" (capitalize (symbol-name mode)) t)) | ||
| 93 | 64 | ||
| 94 | ;;;###autoload | 65 | ;;;###autoload |
| 95 | (defalias 'easy-mmode-define-minor-mode 'define-minor-mode) | 66 | (defalias 'easy-mmode-define-minor-mode 'define-minor-mode) |
| 96 | ;;;###autoload | 67 | ;;;###autoload |
| 97 | (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body) | 68 | (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body) |
| 98 | "Define a new minor mode MODE. | 69 | "Define a new minor mode MODE. |
| 99 | This function defines the associated control variable, keymap, | 70 | This function defines the associated control variable MODE, keymap MODE-map, |
| 100 | toggle command, and hooks (see `easy-mmode-define-toggle'). | 71 | toggle command MODE, and hook MODE-hook. |
| 101 | 72 | ||
| 102 | DOC is the documentation for the mode toggle command. | 73 | DOC is the documentation for the mode toggle command. |
| 103 | Optional INIT-VALUE is the initial value of the mode's variable. | 74 | Optional INIT-VALUE is the initial value of the mode's variable. |
| 104 | By default, the variable is made buffer-local. This can be overridden | ||
| 105 | by specifying an initial value of (global . INIT-VALUE). | ||
| 106 | Optional LIGHTER is displayed in the modeline when the mode is on. | 75 | Optional LIGHTER is displayed in the modeline when the mode is on. |
| 107 | Optional KEYMAP is the default (defvar) keymap bound to the mode keymap. | 76 | Optional KEYMAP is the default (defvar) keymap bound to the mode keymap. |
| 108 | If it is a list, it is passed to `easy-mmode-define-keymap' | 77 | If it is a list, it is passed to `easy-mmode-define-keymap' |
| 109 | in order to build a valid keymap. | 78 | in order to build a valid keymap. |
| 110 | BODY contains code that will be executed each time the mode is (dis)activated. | 79 | BODY contains code that will be executed each time the mode is (dis)activated. |
| 111 | It will be executed after any toggling but before running the hooks." | 80 | It will be executed after any toggling but before running the hooks. |
| 81 | BODY can start with a list of CL-style keys specifying additional arguments. | ||
| 82 | Currently two such keyword arguments are supported: | ||
| 83 | :group followed by the group name to use for any generated `defcustom'. | ||
| 84 | :global if non-nil specifies that the minor mode is not meant to be | ||
| 85 | buffer-local. By default, the variable is made buffer-local." | ||
| 112 | (let* ((mode-name (symbol-name mode)) | 86 | (let* ((mode-name (symbol-name mode)) |
| 87 | (pretty-name (easy-mmode-pretty-mode-name mode lighter)) | ||
| 113 | (globalp nil) | 88 | (globalp nil) |
| 89 | ;; We might as well provide a best-guess default group. | ||
| 90 | (group (intern (replace-regexp-in-string "-mode\\'" "" mode-name))) | ||
| 114 | (keymap-sym (intern (concat mode-name "-map"))) | 91 | (keymap-sym (intern (concat mode-name "-map"))) |
| 115 | (keymap-doc (format "Keymap for `%s'." mode-name))) | 92 | (hook (intern (concat mode-name "-hook"))) |
| 116 | ;; Check if the mode should be global. | 93 | (hook-on (intern (concat mode-name "-on-hook"))) |
| 94 | (hook-off (intern (concat mode-name "-off-hook")))) | ||
| 95 | |||
| 96 | ;; FIXME: compatibility that should be removed. | ||
| 117 | (when (and (consp init-value) (eq (car init-value) 'global)) | 97 | (when (and (consp init-value) (eq (car init-value) 'global)) |
| 118 | (setq init-value (cdr init-value) globalp t)) | 98 | (setq init-value (cdr init-value) globalp t)) |
| 119 | 99 | ||
| 100 | ;; Check keys. | ||
| 101 | (while | ||
| 102 | (case (car body) | ||
| 103 | (:global (setq body (cdr body)) (setq globalp (pop body))) | ||
| 104 | (:group (setq body (cdr body)) (setq group (pop body))))) | ||
| 105 | |||
| 106 | ;; Add default properties to LIGHTER. | ||
| 107 | (unless (or (not (stringp lighter)) (get-text-property 0 'local-map lighter) | ||
| 108 | (get-text-property 0 'keymap lighter)) | ||
| 109 | (setq lighter | ||
| 110 | (apply 'propertize lighter | ||
| 111 | 'local-map (make-mode-line-mouse2-map mode) | ||
| 112 | (unless (get-text-property 0 'help-echo lighter) | ||
| 113 | (list 'help-echo | ||
| 114 | (format "mouse-2: turn off %s" pretty-name)))))) | ||
| 115 | |||
| 120 | `(progn | 116 | `(progn |
| 121 | ;; Define the variable to enable or disable the mode. | 117 | ;; Define the variable to enable or disable the mode. |
| 122 | ,(if globalp | 118 | ,(if globalp |
| @@ -124,13 +120,14 @@ It will be executed after any toggling but before running the hooks." | |||
| 124 | ,(format "Toggle %s. | 120 | ,(format "Toggle %s. |
| 125 | Setting this variable directly does not take effect; | 121 | Setting this variable directly does not take effect; |
| 126 | use either \\[customize] or the function `%s'." | 122 | use either \\[customize] or the function `%s'." |
| 127 | (easy-mmode-derive-name mode) mode) | 123 | pretty-name mode) |
| 128 | :set (lambda (symbol value) (funcall symbol (or value 0))) | 124 | :set (lambda (symbol value) (funcall symbol (or value 0))) |
| 129 | :initialize 'custom-initialize-default | 125 | :initialize 'custom-initialize-default |
| 126 | :group ',group | ||
| 130 | :type 'boolean) | 127 | :type 'boolean) |
| 131 | `(progn | 128 | `(progn |
| 132 | (defvar ,mode ,init-value ,(format "Non-nil if mode is enabled. | 129 | (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled. |
| 133 | Use the function `%s' to change this variable." mode)) | 130 | Use the function `%s' to change this variable." pretty-name mode)) |
| 134 | (make-variable-buffer-local ',mode))) | 131 | (make-variable-buffer-local ',mode))) |
| 135 | 132 | ||
| 136 | ;; Define the minor-mode keymap. | 133 | ;; Define the minor-mode keymap. |
| @@ -141,11 +138,36 @@ Use the function `%s' to change this variable." mode)) | |||
| 141 | ((listp ,keymap) | 138 | ((listp ,keymap) |
| 142 | (easy-mmode-define-keymap ,keymap)) | 139 | (easy-mmode-define-keymap ,keymap)) |
| 143 | (t (error "Invalid keymap %S" ,keymap))) | 140 | (t (error "Invalid keymap %S" ,keymap))) |
| 144 | ,keymap-doc)) | 141 | ,(format "Keymap for `%s'." mode-name))) |
| 142 | |||
| 143 | ;; The toggle's hook. | ||
| 144 | (defcustom ,hook nil | ||
| 145 | ,(format "Hook run at the end of function `%s'." mode-name) | ||
| 146 | :group ',group | ||
| 147 | :type 'hook) | ||
| 148 | |||
| 149 | ;; The actual function. | ||
| 150 | (defun ,mode (&optional arg) | ||
| 151 | ,(or doc | ||
| 152 | (format "With no argument, toggle %s. | ||
| 153 | With universal prefix ARG turn mode on. | ||
| 154 | With zero or negative ARG turn mode off. | ||
| 155 | \\{%s}" pretty-name keymap-sym)) | ||
| 156 | (interactive "P") | ||
| 157 | (setq ,mode | ||
| 158 | (if arg | ||
| 159 | (> (prefix-numeric-value arg) 0) | ||
| 160 | (not ,mode))) | ||
| 161 | ,@body | ||
| 162 | ;; The on/off hooks are here for backward compatibility only. | ||
| 163 | (run-hooks ',hook (if ,mode ',hook-on ',hook-off)) | ||
| 164 | ;; Return the new setting. | ||
| 165 | (if (interactive-p) | ||
| 166 | (message ,(format "%s %%sabled" pretty-name) | ||
| 167 | (if ,mode "en" "dis"))) | ||
| 168 | ,mode) | ||
| 145 | 169 | ||
| 146 | ;; Define the toggle and the hooks. | 170 | (add-minor-mode ',mode ',lighter |
| 147 | (easy-mmode-define-toggle ,mode ,doc ,@body) | ||
| 148 | (add-minor-mode ',mode ,lighter | ||
| 149 | (if (boundp ',keymap-sym) (symbol-value ',keymap-sym))) | 171 | (if (boundp ',keymap-sym) (symbol-value ',keymap-sym))) |
| 150 | 172 | ||
| 151 | ;; If the mode is global, call the function according to the default. | 173 | ;; If the mode is global, call the function according to the default. |
| @@ -381,6 +403,8 @@ ENDFUN should return the end position (with or without moving point)." | |||
| 381 | (next-sym (intern (concat base-name "-next")))) | 403 | (next-sym (intern (concat base-name "-next")))) |
| 382 | (unless name (setq name (symbol-name base-name))) | 404 | (unless name (setq name (symbol-name base-name))) |
| 383 | `(progn | 405 | `(progn |
| 406 | (add-to-list 'debug-ignored-errors | ||
| 407 | ,(concat "^No \\(previous\\|next\\) " (regexp-quote name))) | ||
| 384 | (defun ,next-sym (&optional count) | 408 | (defun ,next-sym (&optional count) |
| 385 | ,(format "Go to the next COUNT'th %s." name) | 409 | ,(format "Go to the next COUNT'th %s." name) |
| 386 | (interactive) | 410 | (interactive) |