1 ;;; easy-mmode.el --- easy definition for major and minor modes.
3 ;; Copyright (C) 1997 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,<mode>-on-hook,<mode>-off-hook and <mode>-mode:
37 ;; see `easy-mmode-define-minor-mode' documentation
40 ;; (pp (macroexpand '(easy-mmode-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 (defmacro easy-mmode-define-toggle (mode &optional doc &rest body)
55 "Define a one arg toggle mode MODE function and associated hooks.
56 MODE is the so defined function that toggles the mode.
57 optional DOC is its associated documentation.
58 BODY is executed after the toggling and before running the hooks.
60 Hooks are checked for run, each time MODE-mode is called.
61 They run under the followings conditions:
62 MODE-hook: if the mode is toggled.
63 MODE-on-hook: if the mode is on.
64 MODE-off-hook: if the mode is off.
66 When the mode is effectively toggled, two hooks may run.
67 If so MODE-hook is guaranteed to be the first."
68 (let* ((mode-name (symbol-name mode))
69 (hook (intern (concat mode-name "-hook")))
70 (hook-on (intern (concat mode-name "-on-hook")))
71 (hook-off (intern (concat mode-name "-off-hook")))
73 (format "With no argument, toggle %s.
74 With universal prefix ARG turn mode on.
75 With zero or negative ARG turn mode off.
76 \\{%s}" mode-name (concat mode-name "-map")))))
79 ,(format "Hook called when `%s' is toggled" mode-name)
82 (defcustom ,hook-on nil
83 ,(format "Hook called when `%s' is turned on" mode-name)
86 (defcustom ,hook-off nil
87 ,(format "Hook called when `%s' is turned off" mode-name)
90 (defun ,mode (&optional arg)
93 (let ((old-mode ,mode))
96 (> (prefix-numeric-value arg) 0)
99 (unless (equal old-mode ,mode) (run-hooks ',hook))
100 (run-hooks (if ,mode ',hook-on ',hook-off)))))))
103 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
105 (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
106 "Define a new minor mode MODE.
107 This function defines the associated control variable, keymap,
108 toggle command, and hooks (see `easy-mmode-define-toggle').
110 DOC is the documentation for the mode toggle command.
111 Optional INIT-VALUE is the initial value of the mode's variable.
112 Optional LIGHTER is displayed in the mode-bar when the mode is on.
113 Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
114 If it is a list, it is passed to `easy-mmode-define-keymap'
115 in order to build a valid keymap.
116 BODY contains code that will be executed each time the mode is (dis)activated.
117 It will be executed after any toggling but before running the hooks."
118 (let* ((mode-name (symbol-name mode))
119 (mode-doc (format "Non-nil if mode is enabled.
120 Use the function `%s' to change this variable." mode-name))
121 (keymap-sym (intern (concat mode-name "-map")))
122 (keymap-doc (format "Keymap for `%s'." mode-name)))
124 ;; Define the variable to enable or disable the mode.
125 (defvar ,mode ,init-value ,mode-doc)
126 (make-variable-buffer-local ',mode)
128 ;; Define the minor-mode keymap.
131 (cond ((and ,keymap (keymapp ,keymap))
134 (easy-mmode-define-keymap ,keymap))
135 (t (error "Invalid keymap %S" ,keymap)))
138 ;; Define the toggle and the hooks.
139 (easy-mmode-define-toggle ,mode ,doc ,@body)
141 ;; Update the mode line.
142 (or (assq ',mode minor-mode-alist)
143 (setq minor-mode-alist
144 (cons (list ',mode nil) minor-mode-alist)))
145 (setcar (cdr (assq ',mode minor-mode-alist)) ,lighter)
147 ;; Update the minor mode map.
148 (or (assq ',mode minor-mode-map-alist)
149 (setq minor-mode-map-alist
150 (cons (cons ',mode nil) minor-mode-map-alist)))
151 (setcdr (assq ',mode minor-mode-map-alist)
156 ;;; easy-mmode-defmap
159 (if (fboundp 'set-keymap-parents)
160 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
161 (defun easy-mmode-set-keymap-parents (m parents)
165 ((not (consp parents)) parents)
166 ((not (cdr parents)) (car parents))
167 (t (let ((m (copy-keymap (pop parents))))
168 (easy-mmode-set-keymap-parents m parents)
171 (defun easy-mmode-define-keymap (bs &optional name m args)
172 "Return a keymap built from bindings BS.
173 BS must be a list of (KEY . BINDING) where
174 KEY and BINDINGS are suited as for define-key.
175 optional NAME is passed to `make-sparse-keymap'.
176 optional map M can be used to modify an existing map.
177 ARGS is a list of additional arguments."
178 (let (inherit dense suppress)
180 (let ((key (pop args))
183 ((eq key :dense) (setq dense val))
184 ((eq key :inherit) (setq inherit val))
186 ;;((eq key :suppress) (setq suppress val))
187 (t (message "Unknown argument %s in defmap" key)))))
189 (setq bs (append m bs))
190 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
194 (dolist (key (if (consp keys) keys (list keys)))
197 (substitute-key-definition key binding m global-map))
199 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
200 ((let ((o (lookup-key m key)))
201 (or (null o) (numberp o) (eq o 'undefined)))
202 (define-key m key binding))))))
204 ((keymapp inherit) (set-keymap-parent m inherit))
205 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
209 (defmacro easy-mmode-defmap (m bs doc &rest args)
211 (autoload 'easy-mmode-define-keymap "easy-mmode")
213 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
218 ;;; easy-mmode-defsyntax
221 (defun easy-mmode-define-syntax (css args)
222 (let ((st (make-syntax-table (cadr (memq :copy args)))))
224 (let ((char (car cs))
227 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
228 (modify-syntax-entry char syntax st))))
232 (defmacro easy-mmode-defsyntax (st css doc &rest args)
234 (autoload 'easy-mmode-define-syntax "easy-mmode")
235 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) doc)))
240 ;;; A "macro-only" reimplementation of define-derived-mode.
243 (defalias 'easy-mmode-define-derived-mode 'define-derived-mode)
245 (defmacro define-derived-mode (child parent name &optional docstring &rest body)
246 "Create a new mode as a variant of an existing mode.
248 The arguments to this command are as follow:
250 CHILD: the name of the command for the derived mode.
251 PARENT: the name of the command for the parent mode (e.g. `text-mode').
252 NAME: a string which will appear in the status line (e.g. \"Hypertext\")
253 DOCSTRING: an optional documentation string--if you do not supply one,
254 the function will attempt to invent something useful.
255 BODY: forms to execute just before running the
256 hooks for the new mode.
258 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
260 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
262 You could then make new key bindings for `LaTeX-thesis-mode-map'
263 without changing regular LaTeX mode. In this example, BODY is empty,
264 and DOCSTRING is generated by default.
266 On a more complicated level, the following command uses `sgml-mode' as
267 the parent, and then sets the variable `case-fold-search' to nil:
269 (define-derived-mode article-mode sgml-mode \"Article\"
270 \"Major mode for editing technical articles.\"
271 (setq case-fold-search nil))
273 Note that if the documentation string had been left out, it would have
274 been generated automatically, with a reference to the keymap."
276 (let* ((child-name (symbol-name child))
277 (map (intern (concat child-name "-map")))
278 (syntax (intern (concat child-name "-syntax-table")))
279 (abbrev (intern (concat child-name "-abbrev-table")))
280 (hook (intern (concat child-name "-hook"))))
282 (when (and docstring (not (stringp docstring)))
283 ;; DOCSTRING is really the first command and there's no docstring
284 (push docstring body)
285 (setq docstring nil))
287 (unless (stringp docstring)
288 ;; Use a default docstring.
290 (format "Major mode derived from `%s' by `define-derived-mode'.
291 Inherits all of the parent's attributes, but has its own keymap,
292 abbrev table and syntax table:
296 which more-or-less shadow %s's corresponding tables."
297 parent map syntax abbrev parent)))
299 (unless (string-match (regexp-quote (symbol-name hook)) docstring)
300 ;; Make sure the docstring mentions the mode's hook
301 (setq docstring (format "%s
302 This mode runs (additionally to any hooks his parent might have run)
303 its own `%s' just before exiting."
306 (unless (string-match "\\\\[{[]" docstring)
307 ;; And don't forget to put the mode's keymap
308 (setq docstring (concat docstring "\n\\{" (symbol-name map) "}")))
311 (defvar ,map (make-sparse-keymap))
312 (defvar ,syntax (make-char-table 'syntax-table nil))
313 (defvar ,abbrev (progn (define-abbrev-table ',abbrev nil) ,abbrev))
322 ; Identify special modes.
323 (put ',child 'special (get ',parent 'special))
324 ; Identify the child mode.
325 (setq major-mode ',child)
326 (setq mode-name ,name)
327 ; Set up maps and tables.
328 (unless (keymap-parent ,map)
329 (set-keymap-parent ,map (current-local-map)))
330 (let ((parent (char-table-parent ,syntax)))
331 (unless (and parent (not (eq parent (standard-syntax-table))))
332 (set-char-table-parent ,syntax (syntax-table))))
333 (when local-abbrev-table
336 (or (intern-soft (symbol-name symbol) ,abbrev)
337 (define-abbrev ,abbrev (symbol-name symbol)
338 (symbol-value symbol) (symbol-function symbol))))
342 (set-syntax-table ,syntax)
343 (setq local-abbrev-table ,abbrev)
344 ; Splice in the body (if any).
346 ; Run the hooks, if any.
347 (run-hooks ',hook)))))
351 ;;; easy-mmode-define-navigation
354 (defmacro easy-mmode-define-navigation (base re &optional name endfun)
355 "Define BASE-next and BASE-prev to navigate in the buffer.
356 RE determines the places the commands should move point to.
357 NAME should describe the entities matched by RE and is used to build
358 the docstrings of the two functions.
359 BASE-next also tries to make sure that the whole entry is visible by
360 searching for its end (by calling ENDFUN if provided or by looking for
361 the next entry) and recentering if necessary.
362 ENDFUN should return the end position (with or without moving point)."
363 (let* ((base-name (symbol-name base))
364 (prev-sym (intern (concat base-name "-prev")))
365 (next-sym (intern (concat base-name "-next"))))
366 (unless name (setq name (symbol-name base-name)))
368 (defun ,next-sym (&optional count)
369 ,(format "Go to the next COUNT'th %s." name)
371 (unless count (setq count 1))
372 (if (< count 0) (,prev-sym (- count))
373 (if (looking-at ,re) (incf count))
374 (unless (re-search-forward ,re nil t count)
375 (if (interactive-p) (ding) (error ,(format "No next %s" name))))
376 (goto-char (match-beginning 0))
377 (when (eq (current-buffer) (window-buffer (selected-window)))
378 (let ((endpt (or (save-excursion
379 ,(if endfun `(,endfun)
380 `(re-search-forward ,re nil t 2)))
382 (unless (<= endpt (window-end)) (recenter))))))
383 (defun ,prev-sym (&optional count)
384 ,(format "Go to the previous COUNT'th %s" (or name base-name))
386 (unless count (setq count 1))
387 (if (< count 0) (,next-sym (- count))
388 (unless (re-search-backward ,re nil t count)
389 (if (interactive-p) (ding)
390 (error ,(format "No previous %s" name)))))))))
392 (provide 'easy-mmode)
394 ;;; easy-mmode.el ends here