1 ;;; easy-mmode.el --- easy definition for major and minor modes.
3 ;; Copyright (C) 1997,2000 Free Software Foundation, Inc.
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Minor modes are useful and common. This package makes defining a
28 ;; minor mode easy, by focusing on the writing of the minor mode
29 ;; functionalities themselves. Moreover, this package enforces a
30 ;; conventional naming of user interface primitives, making things
31 ;; natural for the minor-mode end-users.
33 ;; For each mode, easy-mmode defines the following:
34 ;; <mode> : The minor mode predicate. A buffer-local variable.
35 ;; <mode>-map : The keymap possibly associated to <mode>.
36 ;; <mode>-hook : The hook run at the end of the toggle function.
37 ;; see `define-minor-mode' documentation
40 ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
41 ;; to check the result before using it.
43 ;; The order in which minor modes are installed is important. Keymap
44 ;; lookup proceeds down minor-mode-map-alist, and the order there
45 ;; tends to be the reverse of the order in which the modes were
46 ;; installed. Perhaps there should be a feature to let you specify
49 ;; Additionally to `define-minor-mode', the package provides convenient
50 ;; ways to define keymaps, and other helper functions for major and minor modes.
54 (eval-when-compile (require 'cl
))
56 (defun easy-mmode-pretty-mode-name (mode &optional lighter
)
57 "Turn the symbol MODE into a string intended for the user.
58 If provided LIGHTER will be used to help choose capitalization."
59 (let* ((case-fold-search t
)
60 (name (concat (capitalize (replace-regexp-in-string
61 "-mode\\'" "" (symbol-name mode
)))
63 (if (not (stringp lighter
)) name
64 (setq lighter
(replace-regexp-in-string "\\`\\s-+\\|\\-s+\\'" "" lighter
))
65 (replace-regexp-in-string lighter lighter name t t
))))
68 (defalias 'easy-mmode-define-minor-mode
'define-minor-mode
)
70 (defmacro define-minor-mode
(mode doc
&optional init-value lighter keymap
&rest body
)
71 "Define a new minor mode MODE.
72 This function defines the associated control variable MODE, keymap MODE-map,
73 toggle command MODE, and hook MODE-hook.
75 DOC is the documentation for the mode toggle command.
76 Optional INIT-VALUE is the initial value of the mode's variable.
77 Optional LIGHTER is displayed in the modeline when the mode is on.
78 Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
79 If it is a list, it is passed to `easy-mmode-define-keymap'
80 in order to build a valid keymap.
81 BODY contains code that will be executed each time the mode is (dis)activated.
82 It will be executed after any toggling but before running the hooks.
83 BODY can start with a list of CL-style keys specifying additional arguments.
84 Currently two such keyword arguments are supported:
85 :group followed by the group name to use for any generated `defcustom'.
86 :global if non-nil specifies that the minor mode is not meant to be
87 buffer-local. By default, the variable is made buffer-local."
88 (let* ((mode-name (symbol-name mode
))
89 (pretty-name (easy-mmode-pretty-mode-name mode lighter
))
91 ;; We might as well provide a best-guess default group.
94 (intern (replace-regexp-in-string "-mode\\'" "" mode-name
))))
95 (keymap-sym (if (and keymap
(symbolp keymap
)) keymap
96 (intern (concat mode-name
"-map"))))
97 (hook (intern (concat mode-name
"-hook")))
98 (hook-on (intern (concat mode-name
"-on-hook")))
99 (hook-off (intern (concat mode-name
"-off-hook"))))
101 ;; FIXME: compatibility that should be removed.
102 (when (and (consp init-value
) (eq (car init-value
) 'global
))
103 (setq init-value
(cdr init-value
) globalp t
))
106 (while (keywordp (car body
))
108 (:global
(setq globalp
(pop body
)))
109 (:group
(setq group
(pop body
)))
110 (t (setq body
(cdr body
)))))
112 ;; Add default properties to LIGHTER.
113 (unless (or (not (stringp lighter
)) (get-text-property 0 'local-map lighter
)
114 (get-text-property 0 'keymap lighter
))
116 (apply 'propertize lighter
117 'local-map
(make-mode-line-mouse2-map mode
)
118 (unless (get-text-property 0 'help-echo lighter
)
120 (format "mouse-2: turn off %s" pretty-name
))))))
123 ;; Define the variable to enable or disable the mode.
126 (defvar ,mode
,init-value
,(format "Non-nil if %s is enabled.
127 Use the function `%s' to change this variable." pretty-name mode
))
128 (make-variable-buffer-local ',mode
))
130 (let ((curfile (or (and (boundp 'byte-compile-current-file
)
131 byte-compile-current-file
)
133 `(defcustom ,mode
,init-value
135 Setting this variable directly does not take effect;
136 use either \\[customize] or the function `%s'."
138 :set
(lambda (symbol value
) (funcall symbol
(or value
0)))
139 :initialize
'custom-initialize-default
146 (intern (file-name-nondirectory
147 (file-name-sans-extension curfile
)))))))))
149 ;; The toggle's hook. Wrapped in `progn' to prevent autoloading.
152 ,(format "Hook run at the end of function `%s'." mode-name
)
156 ;; The actual function.
157 (defun ,mode
(&optional arg
)
159 (format "With no argument, toggle %s.
160 With universal prefix ARG turn mode on.
161 With zero or negative ARG turn mode off.
162 \\{%s}" pretty-name keymap-sym
))
166 (> (prefix-numeric-value arg
) 0)
169 ;; The on/off hooks are here for backward compatibility only.
170 (run-hooks ',hook
(if ,mode
',hook-on
',hook-off
))
171 ;; Return the new setting.
173 (message ,(format "%s %%sabled" pretty-name
)
174 (if ,mode
"en" "dis")))
177 ;; Autoloading an easy-mmode-define-minor-mode autoloads
178 ;; everything up-to-here.
181 ;; Define the minor-mode keymap.
182 ,(unless (symbolp keymap
) ;nil is also a symbol.
185 (cond ((keymapp m
) m
)
186 ((listp m
) (easy-mmode-define-keymap m
))
187 (t (error "Invalid keymap %S" ,keymap
))))
188 ,(format "Keymap for `%s'." mode-name
)))
190 (add-minor-mode ',mode
',lighter
191 ,(if keymap keymap-sym
192 `(if (boundp ',keymap-sym
) ,keymap-sym
)))
194 ;; If the mode is global, call the function according to the default.
195 ,(if globalp
`(if ,mode
(,mode
1))))))
198 ;;; make global minor mode
202 (defmacro easy-mmode-define-global-mode
(global-mode mode turn-on
204 "Make GLOBAL-MODE out of the MODE buffer-local minor mode.
205 TURN-ON is a function that will be called with no args in every buffer
206 and that should try to turn MODE on if applicable for that buffer.
207 KEYS is a list of CL-style keyword arguments:
208 :group to specify the custom group."
209 (let* ((mode-name (symbol-name mode
))
210 (global-mode-name (symbol-name global-mode
))
211 (pretty-name (easy-mmode-pretty-mode-name mode
))
212 (pretty-global-name (easy-mmode-pretty-mode-name global-mode
))
213 ;; We might as well provide a best-guess default group.
216 (intern (replace-regexp-in-string "-mode\\'" "" mode-name
))))
217 (buffers (intern (concat global-mode-name
"-buffers")))
218 (cmmh (intern (concat global-mode-name
"-cmmh"))))
221 (while (keywordp (car keys
))
223 (:group
(setq group
(pop keys
)))
224 (t (setq keys
(cdr keys
)))))
227 ;; The actual global minor-mode
228 (define-minor-mode ,global-mode
229 ,(format "Toggle %s in every buffer.
230 With prefix ARG, turn %s on if and only if ARG is positive.
231 %s is actually not turned on in every buffer but only in those
232 in which `%s' turns it on."
233 pretty-name pretty-global-name pretty-name turn-on
)
234 nil nil nil
:global t
:group
,group
236 ;; Setup hook to handle future mode changes and new buffers.
239 (add-hook 'find-file-hooks
',buffers
)
240 (add-hook 'change-major-mode-hook
',cmmh
))
241 (remove-hook 'find-file-hooks
',buffers
)
242 (remove-hook 'change-major-mode-hook
',cmmh
))
244 ;; Go through existing buffers.
245 (dolist (buf (buffer-list))
246 (with-current-buffer buf
247 (if ,global-mode
(,turn-on
) (,mode -
1)))))
249 ;; Autoloading easy-mmode-define-global-mode
250 ;; autoloads everything up-to-here.
253 ;; List of buffers left to process.
254 (defvar ,buffers nil
)
256 ;; The function that calls TURN-ON in each buffer.
258 (remove-hook 'post-command-hook
',buffers
)
260 (let ((buf (pop ,buffers
)))
261 (when (buffer-live-p buf
)
262 (with-current-buffer buf
(,turn-on
))))))
264 ;; The function that catches kill-all-local-variables.
266 (add-to-list ',buffers
(current-buffer))
267 (add-hook 'post-command-hook
',buffers
)))))
270 ;;; easy-mmode-defmap
273 (if (fboundp 'set-keymap-parents
)
274 (defalias 'easy-mmode-set-keymap-parents
'set-keymap-parents
)
275 (defun easy-mmode-set-keymap-parents (m parents
)
279 ((not (consp parents
)) parents
)
280 ((not (cdr parents
)) (car parents
))
281 (t (let ((m (copy-keymap (pop parents
))))
282 (easy-mmode-set-keymap-parents m parents
)
285 (defun easy-mmode-define-keymap (bs &optional name m args
)
286 "Return a keymap built from bindings BS.
287 BS must be a list of (KEY . BINDING) where
288 KEY and BINDINGS are suitable for `define-key'.
289 Optional NAME is passed to `make-sparse-keymap'.
290 Optional map M can be used to modify an existing map.
291 ARGS is a list of additional arguments."
292 (let (inherit dense suppress
)
294 (let ((key (pop args
))
297 (:dense
(setq dense val
))
298 (:inherit
(setq inherit val
))
300 ;;((eq key :suppress) (setq suppress val))
301 (t (message "Unknown argument %s in defmap" key
)))))
303 (setq bs
(append m bs
))
304 (setq m
(if dense
(make-keymap name
) (make-sparse-keymap name
))))
308 (dolist (key (if (consp keys
) keys
(list keys
)))
311 (substitute-key-definition key binding m global-map
))
313 (unless (keymapp (lookup-key m key
)) (define-key m key binding
)))
314 ((let ((o (lookup-key m key
)))
315 (or (null o
) (numberp o
) (eq o
'undefined
)))
316 (define-key m key binding
))))))
318 ((keymapp inherit
) (set-keymap-parent m inherit
))
319 ((consp inherit
) (easy-mmode-set-keymap-parents m inherit
)))
323 (defmacro easy-mmode-defmap
(m bs doc
&rest args
)
325 (autoload 'easy-mmode-define-keymap
"easy-mmode")
327 (easy-mmode-define-keymap ,bs nil
(if (boundp ',m
) ,m
) ,(cons 'list args
))
332 ;;; easy-mmode-defsyntax
335 (defun easy-mmode-define-syntax (css args
)
336 (let ((st (make-syntax-table (cadr (memq :copy args
)))))
338 (let ((char (car cs
))
341 (mapcar (lambda (c) (modify-syntax-entry c syntax st
)) char
)
342 (modify-syntax-entry char syntax st
))))
346 (defmacro easy-mmode-defsyntax
(st css doc
&rest args
)
348 (autoload 'easy-mmode-define-syntax
"easy-mmode")
349 (defconst ,st
(easy-mmode-define-syntax ,css
,(cons 'list args
)) doc
)))
354 ;;; A "macro-only" reimplementation of define-derived-mode.
358 (defmacro define-derived-mode
(child parent name
&optional docstring
&rest body
)
359 "Create a new mode as a variant of an existing mode.
361 The arguments to this command are as follow:
363 CHILD: the name of the command for the derived mode.
364 PARENT: the name of the command for the parent mode (e.g. `text-mode').
365 NAME: a string which will appear in the status line (e.g. \"Hypertext\")
366 DOCSTRING: an optional documentation string--if you do not supply one,
367 the function will attempt to invent something useful.
368 BODY: forms to execute just before running the
369 hooks for the new mode.
371 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
373 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
375 You could then make new key bindings for `LaTeX-thesis-mode-map'
376 without changing regular LaTeX mode. In this example, BODY is empty,
377 and DOCSTRING is generated by default.
379 On a more complicated level, the following command uses `sgml-mode' as
380 the parent, and then sets the variable `case-fold-search' to nil:
382 (define-derived-mode article-mode sgml-mode \"Article\"
383 \"Major mode for editing technical articles.\"
384 (setq case-fold-search nil))
386 Note that if the documentation string had been left out, it would have
387 been generated automatically, with a reference to the keymap."
389 (let* ((child-name (symbol-name child
))
390 (map (intern (concat child-name
"-map")))
391 (syntax (intern (concat child-name
"-syntax-table")))
392 (abbrev (intern (concat child-name
"-abbrev-table")))
393 (hook (intern (concat child-name
"-hook"))))
395 (unless parent
(setq parent
'fundamental-mode
))
397 (when (and docstring
(not (stringp docstring
)))
398 ;; DOCSTRING is really the first command and there's no docstring
399 (push docstring body
)
400 (setq docstring nil
))
402 (unless (stringp docstring
)
403 ;; Use a default docstring.
405 (format "Major mode derived from `%s' by `define-derived-mode'.
406 Inherits all of the parent's attributes, but has its own keymap,
407 abbrev table and syntax table:
411 which more-or-less shadow %s's corresponding tables."
412 parent map syntax abbrev parent
)))
414 (unless (string-match (regexp-quote (symbol-name hook
)) docstring
)
415 ;; Make sure the docstring mentions the mode's hook
418 (unless (eq parent
'fundamental-mode
)
420 "\nAdditionally to any hooks its parent mode "
421 (if (string-match (regexp-quote (format "`%s'" parent
))
423 (format "`%s' " parent
))
425 (format "\nThis mode runs `%s' just before exiting." hook
))))
427 (unless (string-match "\\\\[{[]" docstring
)
428 ;; And don't forget to put the mode's keymap
429 (setq docstring
(concat docstring
"\n\\{" (symbol-name map
) "}")))
432 (defvar ,map
(make-sparse-keymap))
433 (defvar ,syntax
(make-char-table 'syntax-table nil
))
435 (define-abbrev-table ',abbrev nil
)
436 (put ',child
'derived-mode-parent
',parent
)
445 ; Identify special modes.
446 (put ',child
'special
(get ',parent
'special
))
447 ; Identify the child mode.
448 (setq major-mode
',child
)
449 (setq mode-name
,name
)
450 ; Set up maps and tables.
451 (unless (keymap-parent ,map
)
452 (set-keymap-parent ,map
(current-local-map)))
453 (let ((parent (char-table-parent ,syntax
)))
454 (unless (and parent
(not (eq parent
(standard-syntax-table))))
455 (set-char-table-parent ,syntax
(syntax-table))))
456 (when local-abbrev-table
459 (or (intern-soft (symbol-name symbol
) ,abbrev
)
460 (define-abbrev ,abbrev
(symbol-name symbol
)
461 (symbol-value symbol
) (symbol-function symbol
))))
465 (set-syntax-table ,syntax
)
466 (setq local-abbrev-table
,abbrev
)
467 ; Splice in the body (if any).
469 ; Run the hooks, if any.
470 (run-hooks ',hook
)))))
472 ;; Inspired from derived-mode-class in derived.el
473 (defun easy-mmode-derived-mode-p (mode)
474 "Non-nil if the current major mode is derived from MODE.
475 Uses the `derived-mode-parent' property of the symbol to trace backwards."
476 (let ((parent major-mode
))
477 (while (and (not (eq parent mode
))
478 (setq parent
(get parent
'derived-mode-parent
))))
483 ;;; easy-mmode-define-navigation
486 (defmacro easy-mmode-define-navigation
(base re
&optional name endfun
)
487 "Define BASE-next and BASE-prev to navigate in the buffer.
488 RE determines the places the commands should move point to.
489 NAME should describe the entities matched by RE and is used to build
490 the docstrings of the two functions.
491 BASE-next also tries to make sure that the whole entry is visible by
492 searching for its end (by calling ENDFUN if provided or by looking for
493 the next entry) and recentering if necessary.
494 ENDFUN should return the end position (with or without moving point)."
495 (let* ((base-name (symbol-name base
))
496 (prev-sym (intern (concat base-name
"-prev")))
497 (next-sym (intern (concat base-name
"-next"))))
498 (unless name
(setq name
(symbol-name base-name
)))
500 (add-to-list 'debug-ignored-errors
501 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name
)))
502 (defun ,next-sym
(&optional count
)
503 ,(format "Go to the next COUNT'th %s." name
)
505 (unless count
(setq count
1))
506 (if (< count
0) (,prev-sym
(- count
))
507 (if (looking-at ,re
) (incf count
))
508 (unless (re-search-forward ,re nil t count
)
509 (error ,(format "No next %s" name
)))
510 (goto-char (match-beginning 0))
511 (when (eq (current-buffer) (window-buffer (selected-window)))
512 (let ((endpt (or (save-excursion
513 ,(if endfun
`(,endfun
)
514 `(re-search-forward ,re nil t
2)))
516 (unless (<= endpt
(window-end)) (recenter))))))
517 (defun ,prev-sym
(&optional count
)
518 ,(format "Go to the previous COUNT'th %s" (or name base-name
))
520 (unless count
(setq count
1))
521 (if (< count
0) (,next-sym
(- count
))
522 (unless (re-search-backward ,re nil t count
)
523 (error ,(format "No previous %s" name
))))))))
525 (provide 'easy-mmode
)
527 ;;; easy-mmode.el ends here