]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/easy-mmode.el
(easy-mmode-derive-name): New function.
[gnu-emacs] / lisp / emacs-lisp / easy-mmode.el
1 ;;; easy-mmode.el --- easy definition for major and minor modes.
2
3 ;; Copyright (C) 1997,2000 Free Software Foundation, Inc.
4
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
32
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
38 ;;
39 ;; eval
40 ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
41 ;; to check the result before using it.
42
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
47 ;; orderings.
48
49 ;; Additionally to `define-minor-mode', the package provides convenient
50 ;; ways to define keymaps, and other helper functions for major and minor modes.
51
52 ;;; Code:
53
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 MODE-hook."
59 (let* ((mode-name (symbol-name mode))
60 (pretty-name (easy-mmode-derive-name mode-name))
61 (hook (intern (concat mode-name "-hook")))
62 (hook-on (intern (concat mode-name "-on-hook")))
63 (hook-off (intern (concat mode-name "-off-hook")))
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
94 ;;;###autoload
95 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
96 ;;;###autoload
97 (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
98 "Define a new minor mode MODE.
99 This function defines the associated control variable, keymap,
100 toggle command, and hooks (see `easy-mmode-define-toggle').
101
102 DOC is the documentation for the mode toggle command.
103 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.
107 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'
109 in order to build a valid keymap.
110 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."
112 (let* ((mode-name (symbol-name mode))
113 (globalp nil)
114 (keymap-sym (intern (concat mode-name "-map")))
115 (keymap-doc (format "Keymap for `%s'." mode-name)))
116 ;; Check if the mode should be global.
117 (when (and (consp init-value) (eq (car init-value) 'global))
118 (setq init-value (cdr init-value) globalp t))
119
120 `(progn
121 ;; Define the variable to enable or disable the mode.
122 ,(if globalp
123 `(defcustom ,mode ,init-value
124 ,(format "Toggle %s.
125 Setting this variable directly does not take effect;
126 use either \\[customize] or the function `%s'."
127 (easy-mmode-derive-name mode) mode)
128 :set (lambda (symbol value) (funcall symbol (or value 0)))
129 :initialize 'custom-initialize-default
130 :type 'boolean)
131 `(progn
132 (defvar ,mode ,init-value ,(format "Non-nil if mode is enabled.
133 Use the function `%s' to change this variable." mode))
134 (make-variable-buffer-local ',mode)))
135
136 ;; Define the minor-mode keymap.
137 ,(when keymap
138 `(defvar ,keymap-sym
139 (cond ((and ,keymap (keymapp ,keymap))
140 ,keymap)
141 ((listp ,keymap)
142 (easy-mmode-define-keymap ,keymap))
143 (t (error "Invalid keymap %S" ,keymap)))
144 ,keymap-doc))
145
146 ;; Define the toggle and the hooks.
147 (easy-mmode-define-toggle ,mode ,doc ,@body)
148 (add-minor-mode ',mode ,lighter
149 (if (boundp ',keymap-sym) (symbol-value ',keymap-sym)))
150
151 ;; If the mode is global, call the function according to the default.
152 ,(if globalp `(if ,mode (,mode 1))))))
153 \f
154 ;;;
155 ;;; easy-mmode-defmap
156 ;;;
157
158 (if (fboundp 'set-keymap-parents)
159 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
160 (defun easy-mmode-set-keymap-parents (m parents)
161 (set-keymap-parent
162 m
163 (cond
164 ((not (consp parents)) parents)
165 ((not (cdr parents)) (car parents))
166 (t (let ((m (copy-keymap (pop parents))))
167 (easy-mmode-set-keymap-parents m parents)
168 m))))))
169
170 (defun easy-mmode-define-keymap (bs &optional name m args)
171 "Return a keymap built from bindings BS.
172 BS must be a list of (KEY . BINDING) where
173 KEY and BINDINGS are suitable for `define-key'.
174 Optional NAME is passed to `make-sparse-keymap'.
175 Optional map M can be used to modify an existing map.
176 ARGS is a list of additional arguments."
177 (let (inherit dense suppress)
178 (while args
179 (let ((key (pop args))
180 (val (pop args)))
181 (cond
182 ((eq key :dense) (setq dense val))
183 ((eq key :inherit) (setq inherit val))
184 ((eq key :group) )
185 ;;((eq key :suppress) (setq suppress val))
186 (t (message "Unknown argument %s in defmap" key)))))
187 (unless (keymapp m)
188 (setq bs (append m bs))
189 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
190 (dolist (b bs)
191 (let ((keys (car b))
192 (binding (cdr b)))
193 (dolist (key (if (consp keys) keys (list keys)))
194 (cond
195 ((symbolp key)
196 (substitute-key-definition key binding m global-map))
197 ((null binding)
198 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
199 ((let ((o (lookup-key m key)))
200 (or (null o) (numberp o) (eq o 'undefined)))
201 (define-key m key binding))))))
202 (cond
203 ((keymapp inherit) (set-keymap-parent m inherit))
204 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
205 m))
206
207 ;;;###autoload
208 (defmacro easy-mmode-defmap (m bs doc &rest args)
209 `(progn
210 (autoload 'easy-mmode-define-keymap "easy-mmode")
211 (defconst ,m
212 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
213 ,doc)))
214
215 \f
216 ;;;
217 ;;; easy-mmode-defsyntax
218 ;;;
219
220 (defun easy-mmode-define-syntax (css args)
221 (let ((st (make-syntax-table (cadr (memq :copy args)))))
222 (dolist (cs css)
223 (let ((char (car cs))
224 (syntax (cdr cs)))
225 (if (sequencep char)
226 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
227 (modify-syntax-entry char syntax st))))
228 st))
229
230 ;;;###autoload
231 (defmacro easy-mmode-defsyntax (st css doc &rest args)
232 `(progn
233 (autoload 'easy-mmode-define-syntax "easy-mmode")
234 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) doc)))
235
236
237 \f
238 ;;;
239 ;;; A "macro-only" reimplementation of define-derived-mode.
240 ;;;
241
242 ;;;###autoload
243 (defmacro define-derived-mode (child parent name &optional docstring &rest body)
244 "Create a new mode as a variant of an existing mode.
245
246 The arguments to this command are as follow:
247
248 CHILD: the name of the command for the derived mode.
249 PARENT: the name of the command for the parent mode (e.g. `text-mode').
250 NAME: a string which will appear in the status line (e.g. \"Hypertext\")
251 DOCSTRING: an optional documentation string--if you do not supply one,
252 the function will attempt to invent something useful.
253 BODY: forms to execute just before running the
254 hooks for the new mode.
255
256 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
257
258 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
259
260 You could then make new key bindings for `LaTeX-thesis-mode-map'
261 without changing regular LaTeX mode. In this example, BODY is empty,
262 and DOCSTRING is generated by default.
263
264 On a more complicated level, the following command uses `sgml-mode' as
265 the parent, and then sets the variable `case-fold-search' to nil:
266
267 (define-derived-mode article-mode sgml-mode \"Article\"
268 \"Major mode for editing technical articles.\"
269 (setq case-fold-search nil))
270
271 Note that if the documentation string had been left out, it would have
272 been generated automatically, with a reference to the keymap."
273
274 (let* ((child-name (symbol-name child))
275 (map (intern (concat child-name "-map")))
276 (syntax (intern (concat child-name "-syntax-table")))
277 (abbrev (intern (concat child-name "-abbrev-table")))
278 (hook (intern (concat child-name "-hook"))))
279
280 (unless parent (setq parent 'fundamental-mode))
281
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))
286
287 (unless (stringp docstring)
288 ;; Use a default docstring.
289 (setq 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:
293
294 `%s', `%s' and `%s'
295
296 which more-or-less shadow %s's corresponding tables."
297 parent map syntax abbrev parent)))
298
299 (unless (string-match (regexp-quote (symbol-name hook)) docstring)
300 ;; Make sure the docstring mentions the mode's hook
301 (setq docstring
302 (concat docstring
303 (unless (eq parent 'fundamental-mode)
304 (concat
305 "\nAdditionally to any hooks its parent mode "
306 (if (string-match (regexp-quote (format "`%s'" parent))
307 docstring) nil
308 (format "`%s' " parent))
309 "might have run),"))
310 (format "\nThis mode runs `%s' just before exiting." hook))))
311
312 (unless (string-match "\\\\[{[]" docstring)
313 ;; And don't forget to put the mode's keymap
314 (setq docstring (concat docstring "\n\\{" (symbol-name map) "}")))
315
316 `(progn
317 (defvar ,map (make-sparse-keymap))
318 (defvar ,syntax (make-char-table 'syntax-table nil))
319 (defvar ,abbrev (progn (define-abbrev-table ',abbrev nil) ,abbrev))
320 (put ',child 'derived-mode-parent ',parent)
321
322 (defun ,child ()
323 ,docstring
324 (interactive)
325 ; Run the parent.
326 (combine-run-hooks
327
328 (,parent)
329 ; Identify special modes.
330 (put ',child 'special (get ',parent 'special))
331 ; Identify the child mode.
332 (setq major-mode ',child)
333 (setq mode-name ,name)
334 ; Set up maps and tables.
335 (unless (keymap-parent ,map)
336 (set-keymap-parent ,map (current-local-map)))
337 (let ((parent (char-table-parent ,syntax)))
338 (unless (and parent (not (eq parent (standard-syntax-table))))
339 (set-char-table-parent ,syntax (syntax-table))))
340 (when local-abbrev-table
341 (mapatoms
342 (lambda (symbol)
343 (or (intern-soft (symbol-name symbol) ,abbrev)
344 (define-abbrev ,abbrev (symbol-name symbol)
345 (symbol-value symbol) (symbol-function symbol))))
346 local-abbrev-table))
347
348 (use-local-map ,map)
349 (set-syntax-table ,syntax)
350 (setq local-abbrev-table ,abbrev)
351 ; Splice in the body (if any).
352 ,@body)
353 ; Run the hooks, if any.
354 (run-hooks ',hook)))))
355
356 ;; Inspired from derived-mode-class in derived.el
357 (defun easy-mmode-derived-mode-p (mode)
358 "Non-nil if the current major mode is derived from MODE.
359 Uses the `derived-mode-parent' property of the symbol to trace backwards."
360 (let ((parent major-mode))
361 (while (and (not (eq parent mode))
362 (setq parent (get parent 'derived-mode-parent))))
363 parent))
364
365 \f
366 ;;;
367 ;;; easy-mmode-define-navigation
368 ;;;
369
370 (defmacro easy-mmode-define-navigation (base re &optional name endfun)
371 "Define BASE-next and BASE-prev to navigate in the buffer.
372 RE determines the places the commands should move point to.
373 NAME should describe the entities matched by RE and is used to build
374 the docstrings of the two functions.
375 BASE-next also tries to make sure that the whole entry is visible by
376 searching for its end (by calling ENDFUN if provided or by looking for
377 the next entry) and recentering if necessary.
378 ENDFUN should return the end position (with or without moving point)."
379 (let* ((base-name (symbol-name base))
380 (prev-sym (intern (concat base-name "-prev")))
381 (next-sym (intern (concat base-name "-next"))))
382 (unless name (setq name (symbol-name base-name)))
383 `(progn
384 (defun ,next-sym (&optional count)
385 ,(format "Go to the next COUNT'th %s." name)
386 (interactive)
387 (unless count (setq count 1))
388 (if (< count 0) (,prev-sym (- count))
389 (if (looking-at ,re) (incf count))
390 (unless (re-search-forward ,re nil t count)
391 (error ,(format "No next %s" name)))
392 (goto-char (match-beginning 0))
393 (when (eq (current-buffer) (window-buffer (selected-window)))
394 (let ((endpt (or (save-excursion
395 ,(if endfun `(,endfun)
396 `(re-search-forward ,re nil t 2)))
397 (point-max))))
398 (unless (<= endpt (window-end)) (recenter))))))
399 (defun ,prev-sym (&optional count)
400 ,(format "Go to the previous COUNT'th %s" (or name base-name))
401 (interactive)
402 (unless count (setq count 1))
403 (if (< count 0) (,next-sym (- count))
404 (unless (re-search-backward ,re nil t count)
405 (error ,(format "No previous %s" name))))))))
406
407 (provide 'easy-mmode)
408
409 ;;; easy-mmode.el ends here