Mercurial > emacs
annotate lisp/emacs-lisp/easy-mmode.el @ 28753:582ba7ff96f7
(realize_x_face): Fix the argument of the second
xassert. BASE_FACE may not be a face for ASCII.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Fri, 28 Apr 2000 12:57:52 +0000 |
parents | 21cd65af5197 |
children | d8bafae6beb2 |
rev | line source |
---|---|
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
1 ;;; easy-mmode.el --- easy definition for major and minor modes. |
18388 | 2 |
3 ;; Copyright (C) 1997 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr> | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org> |
18388 | 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,<mode>-on-hook,<mode>-off-hook and <mode>-mode: | |
37 ;; see `easy-mmode-define-minor-mode' documentation | |
38 ;; | |
39 ;; eval | |
40 ;; (pp (macroexpand '(easy-mmode-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 | |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
49 ;; Additionally to `define-minor-mode', the package provides convenient |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
50 ;; ways to define keymaps, and other helper functions for major and minor modes. |
18388 | 51 |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
52 ;;; Code: |
18388 | 53 |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
54 (defmacro easy-mmode-define-toggle (mode &optional doc &rest body) |
18388 | 55 "Define a one arg toggle mode MODE function and associated hooks. |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
56 MODE is the so defined function that toggles the mode. |
18388 | 57 optional DOC is its associated documentation. |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
58 BODY is executed after the toggling and before running the hooks. |
18388 | 59 |
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. | |
65 | |
66 When the mode is effectively toggled, two hooks may run. | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
67 If so MODE-hook is guaranteed to be the first." |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
68 (let* ((mode-name (symbol-name mode)) |
18388 | 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"))) | |
72 (toggle-doc (or doc | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
73 (format "With no argument, toggle %s. |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
74 With universal prefix ARG turn mode on. |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
75 With zero or negative ARG turn mode off. |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
76 \\{%s}" mode-name (concat mode-name "-map"))))) |
18388 | 77 `(progn |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
78 (defcustom ,hook nil |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
79 ,(format "Hook called when `%s' is toggled" mode-name) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
80 :type 'hook) |
18388 | 81 |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
82 (defcustom ,hook-on nil |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
83 ,(format "Hook called when `%s' is turned on" mode-name) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
84 :type 'hook) |
18388 | 85 |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
86 (defcustom ,hook-off nil |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
87 ,(format "Hook called when `%s' is turned off" mode-name) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
88 :type 'hook) |
18388 | 89 |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
90 (defun ,mode (&optional arg) |
18388 | 91 ,toggle-doc |
92 (interactive "P") | |
93 (let ((old-mode ,mode)) | |
94 (setq ,mode | |
95 (if arg | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
96 (> (prefix-numeric-value arg) 0) |
18388 | 97 (not ,mode))) |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
98 ,@body |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
99 (unless (equal old-mode ,mode) (run-hooks ',hook)) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
100 (run-hooks (if ,mode ',hook-on ',hook-off))))))) |
18388 | 101 |
102 ;;;###autoload | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
103 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
104 ;;;###autoload |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
105 (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body) |
18388 | 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'). | |
109 | |
110 DOC is the documentation for the mode toggle command. | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
111 Optional INIT-VALUE is the initial value of the mode's variable. |
18388 | 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. | |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
116 BODY contains code that will be executed each time the mode is (dis)activated. |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
117 It will be executed after any toggling but before running the hooks." |
18388 | 118 (let* ((mode-name (symbol-name mode)) |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
119 (mode-doc (format "Non-nil if mode is enabled. |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
120 Use the function `%s' to change this variable." mode-name)) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
121 (keymap-sym (intern (concat mode-name "-map"))) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
122 (keymap-doc (format "Keymap for `%s'." mode-name))) |
18388 | 123 `(progn |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
124 ;; Define the variable to enable or disable the mode. |
18388 | 125 (defvar ,mode ,init-value ,mode-doc) |
126 (make-variable-buffer-local ',mode) | |
127 | |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
128 ;; Define the minor-mode keymap. |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
129 ,(when keymap |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
130 `(defvar ,keymap-sym |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
131 (cond ((and ,keymap (keymapp ,keymap)) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
132 ,keymap) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
133 ((listp ,keymap) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
134 (easy-mmode-define-keymap ,keymap)) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
135 (t (error "Invalid keymap %S" ,keymap))) |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
136 ,keymap-doc)) |
18388 | 137 |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
138 ;; Define the toggle and the hooks. |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
139 (easy-mmode-define-toggle ,mode ,doc ,@body) |
18388 | 140 |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
141 ;; Update the mode line. |
18388 | 142 (or (assq ',mode minor-mode-alist) |
143 (setq minor-mode-alist | |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
144 (cons (list ',mode nil) minor-mode-alist))) |
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
145 (setcar (cdr (assq ',mode minor-mode-alist)) ,lighter) |
18388 | 146 |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
147 ;; Update the minor mode map. |
18388 | 148 (or (assq ',mode minor-mode-map-alist) |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
149 (setq minor-mode-map-alist |
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
150 (cons (cons ',mode nil) minor-mode-map-alist))) |
25408
68c51b1a537f
(easy-mmode-define-minor-mode):
Karl Heuer <kwzh@gnu.org>
parents:
24935
diff
changeset
|
151 (setcdr (assq ',mode minor-mode-map-alist) |
26551
271f77895660
Changed maintainer.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
25408
diff
changeset
|
152 ,keymap-sym)) )) |
18388 | 153 |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
154 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
155 ;;; |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
156 ;;; easy-mmode-defmap |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
157 ;;; |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
158 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
159 (if (fboundp 'set-keymap-parents) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
160 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
161 (defun easy-mmode-set-keymap-parents (m parents) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
162 (set-keymap-parent |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
163 m |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
164 (cond |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
165 ((not (consp parents)) parents) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
166 ((not (cdr parents)) (car parents)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
167 (t (let ((m (copy-keymap (pop parents)))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
168 (easy-mmode-set-keymap-parents m parents) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
169 m)))))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
170 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
171 (defun easy-mmode-define-keymap (bs &optional name m args) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
172 "Return a keymap built from bindings BS. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
173 BS must be a list of (KEY . BINDING) where |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
174 KEY and BINDINGS are suited as for define-key. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
175 optional NAME is passed to `make-sparse-keymap'. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
176 optional map M can be used to modify an existing map. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
177 ARGS is a list of additional arguments." |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
178 (let (inherit dense suppress) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
179 (while args |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
180 (let ((key (pop args)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
181 (val (pop args))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
182 (cond |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
183 ((eq key :dense) (setq dense val)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
184 ((eq key :inherit) (setq inherit val)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
185 ((eq key :group) ) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
186 ;;((eq key :suppress) (setq suppress val)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
187 (t (message "Unknown argument %s in defmap" key))))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
188 (unless (keymapp m) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
189 (setq bs (append m bs)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
190 (setq m (if dense (make-keymap name) (make-sparse-keymap name)))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
191 (dolist (b bs) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
192 (let ((keys (car b)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
193 (binding (cdr b))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
194 (dolist (key (if (consp keys) keys (list keys))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
195 (cond |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
196 ((symbolp key) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
197 (substitute-key-definition key binding m global-map)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
198 ((null binding) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
199 (unless (keymapp (lookup-key m key)) (define-key m key binding))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
200 ((let ((o (lookup-key m key))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
201 (or (null o) (numberp o) (eq o 'undefined))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
202 (define-key m key binding)))))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
203 (cond |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
204 ((keymapp inherit) (set-keymap-parent m inherit)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
205 ((consp inherit) (easy-mmode-set-keymap-parents m inherit))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
206 m)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
207 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
208 ;;;###autoload |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
209 (defmacro easy-mmode-defmap (m bs doc &rest args) |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
210 `(progn |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
211 (autoload 'easy-mmode-define-keymap "easy-mmode") |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
212 (defconst ,m |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
213 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args)) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
214 ,doc))) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
215 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
216 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
217 ;;; |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
218 ;;; easy-mmode-defsyntax |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
219 ;;; |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
220 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
221 (defun easy-mmode-define-syntax (css args) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
222 (let ((st (make-syntax-table (cadr (memq :copy args))))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
223 (dolist (cs css) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
224 (let ((char (car cs)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
225 (syntax (cdr cs))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
226 (if (sequencep char) |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
227 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
228 (modify-syntax-entry char syntax st)))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
229 st)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
230 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
231 ;;;###autoload |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
232 (defmacro easy-mmode-defsyntax (st css doc &rest args) |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
233 `(progn |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
234 (autoload 'easy-mmode-define-syntax "easy-mmode") |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
235 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) doc))) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
236 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
237 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
238 |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
239 ;;; |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
240 ;;; A "macro-only" reimplementation of define-derived-mode. |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
241 ;;; |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
242 |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
243 (defalias 'easy-mmode-define-derived-mode 'define-derived-mode) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
244 ;;;###autoload |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
245 (defmacro define-derived-mode (child parent name &optional docstring &rest body) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
246 "Create a new mode as a variant of an existing mode. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
247 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
248 The arguments to this command are as follow: |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
249 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
250 CHILD: the name of the command for the derived mode. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
251 PARENT: the name of the command for the parent mode (e.g. `text-mode'). |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
252 NAME: a string which will appear in the status line (e.g. \"Hypertext\") |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
253 DOCSTRING: an optional documentation string--if you do not supply one, |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
254 the function will attempt to invent something useful. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
255 BODY: forms to execute just before running the |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
256 hooks for the new mode. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
257 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
258 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode: |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
259 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
260 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\") |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
261 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
262 You could then make new key bindings for `LaTeX-thesis-mode-map' |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
263 without changing regular LaTeX mode. In this example, BODY is empty, |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
264 and DOCSTRING is generated by default. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
265 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
266 On a more complicated level, the following command uses `sgml-mode' as |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
267 the parent, and then sets the variable `case-fold-search' to nil: |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
268 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
269 (define-derived-mode article-mode sgml-mode \"Article\" |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
270 \"Major mode for editing technical articles.\" |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
271 (setq case-fold-search nil)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
272 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
273 Note that if the documentation string had been left out, it would have |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
274 been generated automatically, with a reference to the keymap." |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
275 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
276 (let* ((child-name (symbol-name child)) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
277 (map (intern (concat child-name "-map"))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
278 (syntax (intern (concat child-name "-syntax-table"))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
279 (abbrev (intern (concat child-name "-abbrev-table"))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
280 (hook (intern (concat child-name "-hook")))) |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
281 |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
282 (when (and docstring (not (stringp docstring))) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
283 ;; DOCSTRING is really the first command and there's no docstring |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
284 (push docstring body) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
285 (setq docstring nil)) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
286 |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
287 (unless (stringp docstring) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
288 ;; Use a default docstring. |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
289 (setq docstring |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
290 (format "Major mode derived from `%s' by `define-derived-mode'. |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
291 Inherits all of the parent's attributes, but has its own keymap, |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
292 abbrev table and syntax table: |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
293 |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
294 `%s', `%s' and `%s' |
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
295 |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
296 which more-or-less shadow %s's corresponding tables." |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
297 parent map syntax abbrev parent))) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
298 |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
299 (unless (string-match (regexp-quote (symbol-name hook)) docstring) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
300 ;; Make sure the docstring mentions the mode's hook |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
301 (setq docstring (format "%s |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
302 This mode runs (additionally to any hooks his parent might have run) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
303 its own `%s' just before exiting." |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
304 docstring hook))) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
305 |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
306 (unless (string-match "\\\\[{[]" docstring) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
307 ;; And don't forget to put the mode's keymap |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
308 (setq docstring (concat docstring "\n\\{" (symbol-name map) "}"))) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
309 |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
310 `(progn |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
311 (defvar ,map (make-sparse-keymap)) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
312 (defvar ,syntax (make-char-table 'syntax-table nil)) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
313 (defvar ,abbrev (progn (define-abbrev-table ',abbrev nil) ,abbrev)) |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
314 |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
315 (defun ,child () |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
316 ,docstring |
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
317 (interactive) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
318 ; Run the parent. |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
319 (combine-run-hooks |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
320 |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
321 (,parent) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
322 ; Identify special modes. |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
323 (put ',child 'special (get ',parent 'special)) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
324 ; Identify the child mode. |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
325 (setq major-mode ',child) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
326 (setq mode-name ,name) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
327 ; Set up maps and tables. |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
328 (unless (keymap-parent ,map) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
329 (set-keymap-parent ,map (current-local-map))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
330 (let ((parent (char-table-parent ,syntax))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
331 (unless (and parent (not (eq parent (standard-syntax-table)))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
332 (set-char-table-parent ,syntax (syntax-table)))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
333 (when local-abbrev-table |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
334 (mapatoms |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
335 (lambda (symbol) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
336 (or (intern-soft (symbol-name symbol) ,abbrev) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
337 (define-abbrev ,abbrev (symbol-name symbol) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
338 (symbol-value symbol) (symbol-function symbol)))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
339 local-abbrev-table)) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
340 |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
341 (use-local-map ,map) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
342 (set-syntax-table ,syntax) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
343 (setq local-abbrev-table ,abbrev) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
344 ; Splice in the body (if any). |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
345 ,@body) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
346 ; Run the hooks, if any. |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
347 (run-hooks ',hook))))) |
28081
6360842e5962
(easy-mmode-define-keymap): Extend to allow more flexibility.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26551
diff
changeset
|
348 |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
349 |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
350 ;;; |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
351 ;;; easy-mmode-define-navigation |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
352 ;;; |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
353 |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
354 (defmacro easy-mmode-define-navigation (base re &optional name endfun) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
355 "Define BASE-next and BASE-prev to navigate in the buffer. |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
356 RE determines the places the commands should move point to. |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
357 NAME should describe the entities matched by RE and is used to build |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
358 the docstrings of the two functions. |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
359 BASE-next also tries to make sure that the whole entry is visible by |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
360 searching for its end (by calling ENDFUN if provided or by looking for |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
361 the next entry) and recentering if necessary. |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
362 ENDFUN should return the end position (with or without moving point)." |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
363 (let* ((base-name (symbol-name base)) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
364 (prev-sym (intern (concat base-name "-prev"))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
365 (next-sym (intern (concat base-name "-next")))) |
28239
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
366 (unless name (setq name (symbol-name base-name))) |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
367 `(progn |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
368 (defun ,next-sym (&optional count) |
28239
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
369 ,(format "Go to the next COUNT'th %s." name) |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
370 (interactive) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
371 (unless count (setq count 1)) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
372 (if (< count 0) (,prev-sym (- count)) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
373 (if (looking-at ,re) (incf count)) |
28239
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
374 (unless (re-search-forward ,re nil t count) |
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
375 (if (interactive-p) (ding) (error ,(format "No next %s" name)))) |
28235
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
376 (goto-char (match-beginning 0)) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
377 (when (eq (current-buffer) (window-buffer (selected-window))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
378 (let ((endpt (or (save-excursion |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
379 ,(if endfun `(,endfun) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
380 `(re-search-forward ,re nil t 2))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
381 (point-max)))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
382 (unless (<= endpt (window-end)) (recenter)))))) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
383 (defun ,prev-sym (&optional count) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
384 ,(format "Go to the previous COUNT'th %s" (or name base-name)) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
385 (interactive) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
386 (unless count (setq count 1)) |
963f1d516e92
* derived.el (define-derived-mode): Don't autoload anymore.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28086
diff
changeset
|
387 (if (< count 0) (,next-sym (- count)) |
28239
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
388 (unless (re-search-backward ,re nil t count) |
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
389 (if (interactive-p) (ding) |
21cd65af5197
(easy-mmode-define-navigation): Only use `ding' for interactive use
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28235
diff
changeset
|
390 (error ,(format "No previous %s" name))))))))) |
28086
9ff6e6a6c6b5
(easy-mmode-defmap, easy-mmode-defsyntax): Autoload the functions used.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
28081
diff
changeset
|
391 |
18388 | 392 (provide 'easy-mmode) |
393 | |
394 ;;; easy-mmode.el ends here |