1 ;;; mmm-mode.el --- Allow Multiple Major Modes in a buffer
3 ;; Copyright (C) 1999, 2004 by Michael Abraham Shulman
5 ;; Emacs Lisp Archive Entry
7 ;; Author: Michael Abraham Shulman <viritrilbia@users.sourceforge.net>
8 ;; Keywords: convenience, faces, languages, tools
11 ;; Revision: $Id: mmm-mode.el,v 1.17 2004/06/16 14:14:18 alanshutko Exp $
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.
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.
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.
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.
40 ;;{{{ Parameter Naming
42 ;;; Since version 0.3.7, I've tried to use a uniform scheme for naming
43 ;;; parameters. Here's a brief summary.
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.
52 ;;{{{ CL and Parameters
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'.
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.
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'.
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.
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.
95 ;; This file is set up to autoload by `mmm-auto.el'.
96 ;; (require 'mmm-cmds)
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.
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:
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.
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."
131 (if (if arg (> (prefix-numeric-value arg) 0) (not mmm-mode))
135 (add-to-list 'minor-mode-alist (list 'mmm-mode mmm-mode-string))
140 (defun mmm-mode-on ()
141 "Turn on MMM Mode. See `mmm-mode'."
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.
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)
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)
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))))
181 (defun mmm-mode-off ()
182 "Turn off MMM Mode. See `mmm-mode'."
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)
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)))
206 (defvar mmm-mode-map (make-sparse-keymap)
207 "Keymap for MMM Minor Mode.")
209 (defvar mmm-mode-prefix-map (make-sparse-keymap)
210 "Keymap for MMM Minor Mode after `mmm-mode-prefix-key'.")
212 (defvar mmm-mode-menu-map (make-sparse-keymap "MMM")
213 "Keymap for MMM Minor Mode menu.")
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)))
220 (when mmm-use-old-command-keys
221 (mmm-use-old-command-keys))
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)
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)
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)
236 (mmm-define-key ?z 'mmm-narrow-to-submode-region)
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)
241 ;; Default bindings to do insertion (dynamic)
242 (mmm-set-keymap-default mmm-mode-prefix-map 'mmm-insert-region)
244 ;; Set up the prefix help command, since otherwise the default binding
246 (define-key mmm-mode-prefix-map (vector help-char) prefix-help-command)
248 ;; And put it all onto the prefix key
249 (define-key mmm-mode-map mmm-mode-prefix-key mmm-mode-prefix-map)
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 "----"))
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))
265 (define-key mmm-mode-menu-map [sep10] '(menu-item "----"))
267 (define-key mmm-mode-menu-map [ins-help]
268 '("List Insertion Keys" . mmm-insertion-help))
270 (define-key mmm-mode-menu-map [sep20] '(menu-item "----"))
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))
279 (define-key mmm-mode-menu-map [sep30] '(menu-item "----"))
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))
288 (define-key mmm-mode-map [menu-bar mmm] (cons "MMM" mmm-mode-menu-map))
290 (add-to-list 'minor-mode-map-alist (cons 'mmm-mode mmm-mode-map))
296 ;;; mmm-mode.el ends here