added textile-mode and mmm-mode. xpath stuff
[emacs-init.git] / mmm-mode-0.4.8 / mmm-mode.el
1 ;;; mmm-mode.el --- Allow Multiple Major Modes in a buffer
2
3 ;; Copyright (C) 1999, 2004 by Michael Abraham Shulman
4
5 ;; Emacs Lisp Archive Entry
6 ;; Package: mmm-mode
7 ;; Author: Michael Abraham Shulman <viritrilbia@users.sourceforge.net>
8 ;; Keywords: convenience, faces, languages, tools
9 ;; Version: 0.4.8
10
11 ;; Revision: $Id: mmm-mode.el,v 1.17 2004/06/16 14:14:18 alanshutko Exp $
12
13 ;;{{{ GPL
14
15 ;; This file is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published
17 ;; by the Free Software Foundation; either version 2, or (at your
18 ;; option) any later version.
19
20 ;; This file is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;}}}
31
32 ;;; Commentary:
33
34 ;;; MMM Mode is a minor mode that allows multiple major modes to
35 ;;; coexist in a single buffer. Refer to the documentation of the
36 ;;; function `mmm-mode' for more detailed information. This file
37 ;;; contains mode on/off functions and the mode keymap, but mostly
38 ;;; just loads all the subsidiary files.
39
40 ;;{{{ Parameter Naming
41
42 ;;; Since version 0.3.7, I've tried to use a uniform scheme for naming
43 ;;; parameters. Here's a brief summary.
44
45 ;;; BEG and END refer to the beginning and end of a region.
46 ;;; FRONT and BACK refer to the respective delimiters of a region.
47 ;;; FRONT- and BACK-OFFSET are the offsets from delimiter matches.
48 ;;; FRONT-BEG through BACK-END are the endings of the delimiters.
49 ;;; START and STOP bound actions, like searching, fontification, etc.
50
51 ;;}}}
52 ;;{{{ CL and Parameters
53
54 ;;; Keyword parameters can be nice because it makes it easier to see
55 ;;; what's getting passed as what. But I try not to use them in user
56 ;;; functions, because CL doesn't make good documentation strings.
57 ;;; Similarly, any hook or callback function can't take keywords,
58 ;;; since Emacs as a whole doesn't use them. And for small parameter
59 ;;; lists, they are overkill. So I use them only for a large number of
60 ;;; optional parameters, such as `mmm-make-region'.
61
62 ;;; An exception is the various submode class application functions,
63 ;;; which all take all their arguments as keywords, for consistency
64 ;;; and so the classes alist looks nice.
65
66 ;;; When using keyword arguments, defaults should *always* be supplied
67 ;;; in all arglists. (This pertains mostly to :start and :stop
68 ;;; arguments, usually defaulting to (point-min) and (point-max)
69 ;;; respectively.) `mmm-save-keywords' should only be used for lists
70 ;;; with more than four arguments, such as in `mmm-ify-by-regexp'.
71
72 ;;; In general, while I have no qualms about using things from CL like
73 ;;; `mapl', `loop' and `destructuring-bind', I try not to use `defun*'
74 ;;; more than I have to. For one, it sometimes makes bad documentation
75 ;;; strings. Furthermore, to a `defun'ned function, a nil argument is
76 ;;; the same as no argument, so it will use its (manual) default, but
77 ;;; to a `defun*'ned function, a nil argument *is* the argument, so
78 ;;; any default specified in the arglist will be ignored. Confusion of
79 ;;; this type should be avoided when at all possible.
80
81 ;;}}}
82
83 ;;; Code:
84
85 (require 'cl)
86 ;; If we don't load font-lock now, but it is loaded later, the
87 ;; necessary mmm-font-lock-* properties may not be there.
88 (require 'font-lock)
89 (require 'mmm-compat)
90 (require 'mmm-utils)
91 (require 'mmm-vars)
92 (require 'mmm-auto)
93 (require 'mmm-region)
94 (require 'mmm-class)
95 ;; This file is set up to autoload by `mmm-auto.el'.
96 ;; (require 'mmm-cmds)
97 (require 'mmm-univ)
98
99 ;;{{{ Toggle Function
100
101 (defun mmm-mode (&optional arg)
102   "Minor mode to allow multiple major modes in one buffer.
103 Without ARG, toggle MMM Mode. With ARG, turn MMM Mode on iff ARG is
104 positive and off otherwise.
105
106 Commands Available:
107 \\<mmm-mode-map>
108 \\{mmm-mode-map}
109
110 BASIC CONCEPTS
111
112 The idea of MMM Mode is to allow multiple major modes to coexist in
113 the same buffer.  There is one \"primary\" major mode that controls
114 most of the buffer, and a number of \"submodes\" that each hold sway
115 over certain regions.  The submode regions are usually highlighted by
116 a background color for ease of recognition.  While the point is in a
117 submode region, the following changes \(are supposed to) occur:
118
119 1. The local keymap is that of the submode.
120 2. The mode line changes to show what submode region is active.
121 3. The major mode menu and mouse popup menu are that of the submode.
122 4. Some local variables of the submode shadow the default mode's.
123 5. The syntax table and indentation are those of the submode.
124 6. Font-lock fontifies correctly for the submode.
125
126 For further information, including installation and configuration
127 instructions, see the Info file mmm.info which is included with the
128 distribution of MMM Mode.  Many of MMM's configuration variables are
129 available through M-x customize under Programming | Tools | Mmm."
130   (interactive "P")
131   (if (if arg (> (prefix-numeric-value arg) 0) (not mmm-mode))
132       (mmm-mode-on)
133     (mmm-mode-off)))
134
135 (add-to-list 'minor-mode-alist (list 'mmm-mode mmm-mode-string))
136
137 ;;}}}
138 ;;{{{ Mode On
139
140 (defun mmm-mode-on ()
141   "Turn on MMM Mode. See `mmm-mode'."
142   (interactive)
143   ;; This function is called from mode hooks, so we need to make sure
144   ;; we're not in a temporary buffer.  We don't need to worry about
145   ;; recursively ending up in ourself, however, since by that time the
146   ;; variable `mmm-mode' will already be set.
147   (mmm-valid-buffer
148    (unless mmm-mode
149      (setq mmm-primary-mode major-mode)
150      (when (fboundp 'c-make-styles-buffer-local)
151        (c-make-styles-buffer-local t))
152      (mmm-update-mode-info major-mode)
153      (setq mmm-region-saved-locals-for-dominant
154            (list* (list 'font-lock-cache-state nil)
155                   (list 'font-lock-cache-position (make-marker))
156                   (copy-tree (cdr (assq major-mode mmm-region-saved-locals-defaults)))))
157      ;; Without the next line, the (make-marker) above gets replaced
158      ;; with the starting value of nil, and all comes to naught.
159      (mmm-set-local-variables major-mode)
160      (mmm-add-hooks)
161      (mmm-fixup-skeleton)
162      (make-local-variable 'font-lock-fontify-region-function)
163      (make-local-variable 'font-lock-beginning-of-syntax-function)
164      (setq font-lock-fontify-region-function 'mmm-fontify-region
165            font-lock-beginning-of-syntax-function 'mmm-beginning-of-syntax)
166      (setq mmm-mode t)
167      (condition-case err
168          (mmm-apply-all)
169        (mmm-error
170         ;; Complain, but don't die, since we want files to go ahead
171         ;; and be opened anyway, and the mode to go ahead and be
172         ;; turned on. Should we delete all previously made submode
173         ;; regions when we find an invalid one?
174         (message "%s" (error-message-string err))))
175      (run-hooks 'mmm-mode-hook)
176      (mmm-run-major-hook))))
177
178 ;;}}}
179 ;;{{{ Mode Off
180
181 (defun mmm-mode-off ()
182   "Turn off MMM Mode. See `mmm-mode'."
183   (interactive)
184   (when mmm-mode
185     (mmm-remove-hooks)
186     (mmm-clear-overlays)
187     (mmm-clear-history)
188     (mmm-clear-mode-ext-classes)
189     (mmm-clear-local-variables)
190     (mmm-update-submode-region)
191     (setq font-lock-fontify-region-function
192           (get mmm-primary-mode 'mmm-fontify-region-function)
193           font-lock-beginning-of-syntax-function
194           (get mmm-primary-mode 'mmm-beginning-of-syntax-function))
195     (mmm-update-font-lock-buffer)
196     (mmm-refontify-maybe)
197     (setq mmm-mode nil)
198     ;; Restore the mode line
199     (setq mmm-primary-mode-display-name nil
200           mmm-buffer-mode-display-name nil)
201     (mmm-set-mode-line)))
202
203 ;;}}}
204 ;;{{{ Mode Keymap
205
206 (defvar mmm-mode-map (make-sparse-keymap)
207   "Keymap for MMM Minor Mode.")
208
209 (defvar mmm-mode-prefix-map (make-sparse-keymap)
210   "Keymap for MMM Minor Mode after `mmm-mode-prefix-key'.")
211
212 (defvar mmm-mode-menu-map (make-sparse-keymap "MMM")
213   "Keymap for MMM Minor Mode menu.")
214
215 (defun mmm-define-key (key binding &optional keymap)
216   (define-key (or keymap mmm-mode-prefix-map)
217     (vector (append mmm-command-modifiers (list key)))
218     binding))
219
220 (when mmm-use-old-command-keys
221   (mmm-use-old-command-keys))
222
223 (mmm-define-key ?c 'mmm-ify-by-class)
224 (mmm-define-key ?x 'mmm-ify-by-regexp)
225 (mmm-define-key ?r 'mmm-ify-region)
226
227 (mmm-define-key ?b 'mmm-parse-buffer)
228 (mmm-define-key ?g 'mmm-parse-region)
229 (mmm-define-key ?% 'mmm-parse-block)
230 (mmm-define-key ?5 'mmm-parse-block)
231
232 (mmm-define-key ?k 'mmm-clear-current-region)
233 (mmm-define-key ?\  'mmm-reparse-current-region)
234 (mmm-define-key ?e 'mmm-end-current-region)
235
236 (mmm-define-key ?z 'mmm-narrow-to-submode-region)
237
238 ;; This one is exact, since C-h is (usually) already used for help.
239 (define-key mmm-mode-prefix-map [?h] 'mmm-insertion-help)
240
241 ;; Default bindings to do insertion (dynamic)
242 (mmm-set-keymap-default mmm-mode-prefix-map 'mmm-insert-region)
243
244 ;; Set up the prefix help command, since otherwise the default binding
245 ;; overrides it.
246 (define-key mmm-mode-prefix-map (vector help-char) prefix-help-command)
247
248 ;; And put it all onto the prefix key
249 (define-key mmm-mode-map mmm-mode-prefix-key mmm-mode-prefix-map)
250
251 ;; Order matters for the menu bar.
252 (define-key mmm-mode-menu-map [off]
253   '("MMM Mode Off" . mmm-mode-off))
254 (define-key mmm-mode-menu-map [sep0] '(menu-item "----"))
255
256 (define-key mmm-mode-menu-map [clhist]
257   '("Clear History" . mmm-clear-history))
258 (define-key mmm-mode-menu-map [end]
259   '("End Current" . mmm-end-current-region))
260 (define-key mmm-mode-menu-map [clear]
261   '("Clear Current" . mmm-clear-current-region))
262 (define-key mmm-mode-menu-map [reparse]
263   '("Reparse Current" . mmm-reparse-current-region))
264
265 (define-key mmm-mode-menu-map [sep10] '(menu-item "----"))
266
267 (define-key mmm-mode-menu-map [ins-help]
268   '("List Insertion Keys" . mmm-insertion-help))
269
270 (define-key mmm-mode-menu-map [sep20] '(menu-item "----"))
271
272 (define-key mmm-mode-menu-map [region]
273   '(menu-item "MMM-ify Region" mmm-ify-region :enable mark-active))
274 (define-key mmm-mode-menu-map [regexp]
275   '("MMM-ify by Regexp" . mmm-ify-by-regexp))
276 (define-key mmm-mode-menu-map [class]
277   '("Apply Submode Class" . mmm-ify-by-class))
278
279 (define-key mmm-mode-menu-map [sep30] '(menu-item "----"))
280
281 (define-key mmm-mode-menu-map [parse-region]
282   '(menu-item "Parse Region" mmm-parse-region :enable mark-active))
283 (define-key mmm-mode-menu-map [parse-buffer]
284   '("Parse Buffer" . mmm-parse-buffer))
285 (define-key mmm-mode-menu-map [parse-block]
286   '("Parse Block" . mmm-parse-block))
287
288 (define-key mmm-mode-map [menu-bar mmm] (cons "MMM" mmm-mode-menu-map))
289
290 (add-to-list 'minor-mode-map-alist (cons 'mmm-mode mmm-mode-map))
291
292 ;;}}}
293
294 (provide 'mmm-mode)
295
296 ;;; mmm-mode.el ends here