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 (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")))
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")))))
71 ,(format "Hook called at the end of function `%s'." mode-name)
74 (defun ,mode (&optional arg)
79 (> (prefix-numeric-value arg) 0)
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.
86 (message ,(format "%s %%sabled" pretty-name)
87 (if ,mode "en" "dis")))
90 (defun easy-mmode-derive-name (mode)
91 (replace-regexp-in-string
92 "-Mode" " mode" (capitalize (symbol-name mode)) t))
95 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
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').
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))
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))
121 ;; Define the variable to enable or disable the mode.
123 `(defcustom ,mode ,init-value
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
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)))
136 ;; Define the minor-mode keymap.
139 (cond ((and ,keymap (keymapp ,keymap))
142 (easy-mmode-define-keymap ,keymap))
143 (t (error "Invalid keymap %S" ,keymap)))
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)))
151 ;; If the mode is global, call the function according to the default.
152 ,(if globalp `(if ,mode (,mode 1))))))
155 ;;; easy-mmode-defmap
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)
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)
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)
179 (let ((key (pop args))
182 ((eq key :dense) (setq dense val))
183 ((eq key :inherit) (setq inherit val))
185 ;;((eq key :suppress) (setq suppress val))
186 (t (message "Unknown argument %s in defmap" key)))))
188 (setq bs (append m bs))
189 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
193 (dolist (key (if (consp keys) keys (list keys)))
196 (substitute-key-definition key binding m global-map))
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))))))
203 ((keymapp inherit) (set-keymap-parent m inherit))
204 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
208 (defmacro easy-mmode-defmap (m bs doc &rest args)
210 (autoload 'easy-mmode-define-keymap "easy-mmode")
212 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
217 ;;; easy-mmode-defsyntax
220 (defun easy-mmode-define-syntax (css args)
221 (let ((st (make-syntax-table (cadr (memq :copy args)))))
223 (let ((char (car cs))
226 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
227 (modify-syntax-entry char syntax st))))
231 (defmacro easy-mmode-defsyntax (st css doc &rest args)
233 (autoload 'easy-mmode-define-syntax "easy-mmode")
234 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) doc)))
239 ;;; A "macro-only" reimplementation of define-derived-mode.
243 (defmacro define-derived-mode (child parent name &optional docstring &rest body)
244 "Create a new mode as a variant of an existing mode.
246 The arguments to this command are as follow:
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.
256 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
258 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
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.
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:
267 (define-derived-mode article-mode sgml-mode \"Article\"
268 \"Major mode for editing technical articles.\"
269 (setq case-fold-search nil))
271 Note that if the documentation string had been left out, it would have
272 been generated automatically, with a reference to the keymap."
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"))))
280 (unless parent (setq parent 'fundamental-mode))
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
303 (unless (eq parent 'fundamental-mode)
305 "\nAdditionally to any hooks its parent mode "
306 (if (string-match (regexp-quote (format "`%s'" parent))
308 (format "`%s' " parent))
310 (format "\nThis mode runs `%s' just before exiting." hook))))
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) "}")))
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)
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
343 (or (intern-soft (symbol-name symbol) ,abbrev)
344 (define-abbrev ,abbrev (symbol-name symbol)
345 (symbol-value symbol) (symbol-function symbol))))
349 (set-syntax-table ,syntax)
350 (setq local-abbrev-table ,abbrev)
351 ; Splice in the body (if any).
353 ; Run the hooks, if any.
354 (run-hooks ',hook)))))
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))))
367 ;;; easy-mmode-define-navigation
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)))
384 (defun ,next-sym (&optional count)
385 ,(format "Go to the next COUNT'th %s." name)
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)))
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))
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))))))))
407 (provide 'easy-mmode)
409 ;;; easy-mmode.el ends here