Initial Commit
[packages] / xemacs-packages / mmm-mode / mmm-vars.el
1 ;;; mmm-vars.el --- Variables for MMM Mode
2
3 ;; Copyright (C) 2000, 2004 by Michael Abraham Shulman
4
5 ;; Author: Michael Abraham Shulman <viritrilbia@users.sourceforge.net>
6 ;; Version: $Id: mmm-vars.el,v 1.2 2008-12-22 14:02:25 mharnisch Exp $
7
8 ;;{{{ GPL
9
10 ;; This file 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 ;; This file 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
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;}}}
26
27 ;;; Commentary:
28
29 ;; This file provides the definitions for the variables used by MMM
30 ;; Mode, as well as several functions to manipulate them. It also
31 ;; defines the errors that MMM Mode can signal.
32
33 ;;; Code:
34
35 (require 'mmm-compat)
36
37 ;; MISCELLANEOUS
38 ;;{{{ Shut up the Byte Compiler
39
40 ;; Otherwise it complains about undefined variables.
41 (eval-when-compile
42   (defvar mmm-current-submode)
43   (defvar mmm-save-local-variables)
44   (defvar mmm-mode-string)
45   (defvar mmm-submode-mode-line-format)
46   (defvar mmm-mode-ext-classes-alist)
47   (defvar mmm-mode-prefix-key)
48   (defvar mmm-global-mode)
49   (defvar mmm-primary-mode)
50   (defvar mmm-classes-alist))
51
52 ;;}}}
53 ;;{{{ Error Conditions
54
55 ;; Most of these should be caught internally and never seen by the
56 ;; user, except when the user is creating submode regions manually.
57
58 ;; Signalled when we try to put a submode region inside one where it
59 ;; isn't meant to go.
60 (put 'mmm-subregion-invalid-parent
61      'error-conditions
62      '(mmm-subregion-invalid-parent mmm-error error))
63 (put 'mmm-subregion-invalid-parent
64      'error-message
65      "Invalid submode region parent")
66
67 ;; Signalled when we try to put a submode region overlapping others in
68 ;; an invalid way.
69 (put 'mmm-subregion-invalid-placement
70      'error-conditions
71      '(mmm-subregion-invalid-placement mmm-error error))
72 (put 'mmm-subregion-invalid-placement
73      'error-message
74      "Submode region placement invalid")
75
76 ;; Signalled when we try to apply a submode class that doesn't exist.
77 (put 'mmm-invalid-submode-class
78      'error-conditions
79      '(mmm-invalid-submode-class mmm-error error))
80 (put 'mmm-invalid-submode-class
81      'error-message
82      "Invalid or undefined submode class")
83
84 ;; Signalled by :match-submode functions when they are unable to
85 ;; resolve a submode.  This error should *always* be caught internally
86 ;; and never seen by the user.
87 (put 'mmm-no-matching-submode
88      'error-conditions
89      '(mmm-no-matching-submode mmm-error error))
90 (put 'mmm-no-matching-submode
91      'error-message
92      "Internal error: no matching submode.")
93
94 ;;}}}
95
96 ;; USER VARIABLES
97 ;;{{{ Customization Group
98
99 (defgroup mmm nil
100   "Multiple Major Modes in one buffer."
101   :group 'tools)
102
103 ;;}}}
104 ;;{{{ Save Local Variables
105
106 (defvar mmm-c-derived-modes
107   '(c-mode c++-mode objc-mode pike-mode java-mode jde-mode javascript-mode
108     php-mode))
109
110 (defvar mmm-save-local-variables 
111   `(;; Don't use `function' (#') here!!  We're already inside `quote'!
112     major-mode
113     comment-start
114     comment-end
115     (comment-line-start-skip buffer (fortran-mode))
116     comment-start-skip
117     (comment-column buffer)
118     comment-indent-function
119     comment-line-break-function
120     sentence-end
121     ,@(when mmm-xemacs
122         '(mode-popup-menu
123           (((lambda () current-menubar) . set-buffer-menubar))
124           ))
125     font-lock-keywords
126     font-lock-keywords-only
127     font-lock-keywords-case-fold-search
128     font-lock-syntax-table
129     font-lock-mark-block-function       ; Override this?
130     font-lock-syntactic-keywords
131     indent-line-function
132     parse-sexp-ignore-comments  ; Fixes indentation in PHP-mode?
133     ;; Can be different in different buffers
134     (c-basic-offset
135      buffer (c-mode c++-mode objc-mode pike-mode java-mode jde-mode))
136     ;; These are necessary for C syntax parsing
137     (c-class-key nil ,mmm-c-derived-modes)
138     (c-extra-toplevel-key nil ,mmm-c-derived-modes)
139     (c-inexpr-class-key nil ,mmm-c-derived-modes)
140     (c-conditional-key nil ,mmm-c-derived-modes)
141     semantic-bovinate-toplevel-override
142     semantic-toplevel-bovine-table
143     ;; Indentation style control variables.
144     ;; These have to be localized in Emacs: see `mmm-mode-on'.
145     ,@(mapcar
146        #'(lambda (var) (list var nil mmm-c-derived-modes))
147        '(c++-template-syntax-table
148          c-<-op-cont-regexp 
149          c->-op-cont-regexp 
150          c-after-suffixed-type-decl-key
151          c-after-suffixed-type-maybe-decl-key
152          c-any-class-key
153          c-any-class-key 
154          c-asm-stmt-kwds
155          c-assignment-op-regexp
156          c-backslash-column
157          c-basic-offset
158          c-bitfield-kwds
159          c-block-comment-prefix
160          c-block-decls-with-vars
161          c-block-stmt-1-key
162          c-block-stmt-1-key 
163          c-block-stmt-1-kwds
164          c-block-stmt-2-key
165          c-block-stmt-2-key 
166          c-block-stmt-2-kwds
167          c-brace-list-key 
168          c-cast-parens 
169          c-class-key
170          c-class-key 
171          c-class-kwds
172          c-cleanup-list
173          c-colon-type-list-re 
174          c-comment-only-line-offset
175          c-comment-prefix-regexp
176          c-comment-start-regexp
177          c-comment-start-regexp 
178          c-cpp-defined-fns
179          c-current-comment-prefix
180          c-decl-block-key
181          c-decl-block-key 
182          c-decl-prefix-re 
183          c-decl-spec-kwds
184          c-doc-comment-start-regexp
185          c-expr-kwds
186          c-file-offsets
187          c-file-style
188          c-hanging-braces-alist
189          c-hanging-colons-alist
190          c-hanging-comment-ender-p
191          c-hanging-comment-starter-p
192          c-hanging-semi\&comma-criteria
193          c-identifier-key 
194          c-identifier-last-sym-match
195          c-identifier-start 
196          c-identifier-syntax-modifications
197          c-identifier-syntax-table
198          c-in-comment-lc-prefix
199          c-indent-comment-alist
200          c-indent-comments-syntactically-p
201          c-indentation-style
202          c-inexpr-block-kwds
203          c-inexpr-class-kwds
204          c-keywords
205          c-keywords-obarray
206          c-keywords-regexp
207          c-keywords-regexp 
208          c-known-type-key
209          c-label-key
210          c-label-key 
211          c-label-kwds
212          c-label-kwds-regexp
213          c-label-kwds-regexp 
214          c-label-minimum-indentation
215          c-lambda-kwds
216          c-literal-start-regexp 
217          c-nonsymbol-chars 
218          c-nonsymbol-token-regexp
219          c-not-decl-init-keywords
220          c-offsets-alist
221          c-opt-<>-arglist-start 
222          c-opt-<>-arglist-start-in-paren
223          c-opt-<>-sexp-key 
224          c-opt-access-key
225          c-opt-access-key 
226          c-opt-asm-stmt-key
227          c-opt-asm-stmt-key 
228          c-opt-bitfield-key
229          c-opt-bitfield-key 
230          c-opt-block-decls-with-vars-key
231          c-opt-block-stmt-key
232          c-opt-block-stmt-key 
233          c-opt-cpp-prefix 
234          c-opt-cpp-start 
235          c-opt-decl-spec-key
236          c-opt-friend-key
237          c-opt-friend-key 
238          c-opt-identifier-concat-key
239          c-opt-inexpr-block-key
240          c-opt-inexpr-block-key 
241          c-opt-inexpr-brace-list-key
242          c-opt-inexpr-class-key
243          c-opt-inexpr-class-key 
244          c-opt-lambda-key
245          c-opt-lambda-key 
246          c-opt-method-key
247          c-opt-method-key 
248          c-opt-postfix-decl-spec-key
249          c-opt-type-component-key
250          c-opt-type-concat-key 
251          c-opt-type-modifier-key 
252          c-opt-type-suffix-key 
253          c-other-decl-block-key
254          c-other-decl-block-key 
255          c-other-decl-block-kwds
256          c-other-decl-kwds
257          c-overloadable-operators-regexp
258          c-paragraph-separate 
259          c-paragraph-start 
260          c-paren-stmt-key 
261          c-primary-expr-regexp 
262          c-primitive-type-key 
263          c-primitive-type-kwds
264          c-protection-kwds
265          c-recognize-<>-arglists 
266          c-recognize-knr-p
267          c-recognize-knr-p 
268          c-recognize-paren-inits 
269          c-recognize-typeless-decls
270          c-regular-keywords-regexp
271          c-simple-stmt-key 
272          c-simple-stmt-kwds
273          c-special-brace-lists
274          c-special-brace-lists 
275          c-specifier-key 
276          c-specifier-kwds
277          c-stmt-delim-chars 
278          c-stmt-delim-chars-with-comma
279          c-symbol-key
280          c-symbol-key 
281          c-symbol-start 
282          c-syntactic-eol
283          c-syntactic-ws-end 
284          c-syntactic-ws-start 
285          c-type-decl-prefix-key 
286          c-type-decl-suffix-key 
287          c-type-prefix-key 
288          comment-end 
289          comment-start 
290          comment-start-skip))
291     ;; Skeleton insertion
292     skeleton-transformation
293     ;; Abbrev mode
294     abbrev-mode
295     local-abbrev-table
296     ;; And finally the syntax table and local map.
297     ((syntax-table . set-syntax-table))
298     ((current-local-map . use-local-map) buffer)
299     paragraph-separate
300     paragraph-start
301     )
302   "Which local variables to save for major mode regions.
303 Each element has the form \(VARIABLE [TYPE [MODES]]), causing VARIABLE
304 to be saved for all major modes in the list MODES.  If MODES is t or
305 absent, the variable is saved for all major modes.  MODES can also be
306 a function of no arguments which returns non-nil whenever the variable
307 should be saved.
308
309 TYPE should be either the symbol `global', meaning to save the
310 variable globally, the symbol `buffer', meaning to save it per buffer,
311 or the symbol `region', meaning to save it for each submode region.
312 If TYPE has any other value, such as nil, or is absent, the variable
313 is saved globally.  If all optional parameters are omitted, the
314 element may be simply VARIABLE instead of \(VARIABLE).
315
316 It is possible for VARIABLE to be not a symbol but a cons cell of the
317 form \(GETTER . SETTER), thus specifying special functions to set and
318 get the value of the \"variable\".  This is used for objects like
319 local maps, syntax tables, etc. which need to be installed in a
320 special way.  GETTER should be a function of no arguments, and SETTER
321 a function of one.  In this case, even if TYPE and MODES are omitted,
322 the list cannot be flattened--it must be \((GETTER . SETTER)).
323 \"Variables\" of this type cannot be seen with `mmm-get-saved-local'.
324
325 A single variable may appear more than once in this list, with
326 different modes and/or types.  If the same mode appears more than once
327 for the same variable with different types, the behavior is undefined.
328 Changing the value of this variable after MMM Mode has been activated
329 in some buffer may produce unpredictable results.
330
331 Globally saved variables are saved in the mmm-local-variables property
332 of the mode symbol.  Buffer saved variables are saved in the alist
333 `mmm-buffer-saved-locals'.  Region saved variables are saved in the
334 mmm-local-variables property of the overlay.")
335
336 (defvar mmm-buffer-saved-locals ()
337   "Stores saved local variables for this buffer, by mode.
338 Each element looks like \(MODE \(VAR VALUE) ...).")
339 (make-variable-buffer-local 'mmm-buffer-saved-locals)
340
341 (defvar mmm-region-saved-locals-defaults ()
342   "Stores saved defaults for region-saved locals, by mode.
343 Each element looks like \(MODE \(VAR VALUE) ...).  Used to initialize
344 new submode regions.")
345 (make-variable-buffer-local 'mmm-region-saved-locals-defaults)
346
347 (defvar mmm-region-saved-locals-for-dominant ()
348   "Stores saved region locals for the dominant major mode.
349 The dominant major mode is considered to be one region for purposes of
350 saving region variables.  Region-saved variables for submode regions
351 are saved as overlay properties.")
352 (make-variable-buffer-local 'mmm-region-saved-locals-for-dominant)
353
354 ;;}}}
355 ;;{{{ Submode Faces
356
357 (defgroup mmm-faces nil
358   "Faces and coloring for submode regions.
359 In general, only background colors should be set, to avoid interfering
360 with font-lock."
361   :group 'mmm)
362
363 (defcustom mmm-submode-decoration-level 1
364   "*Amount of coloring to use in submode regions.
365 Should be either 0, 1, or 2, representing None, Low, and High amounts
366 of coloring respectively.
367 * None (0) means to use no coloring at all.
368 * Low (1) means to use `mmm-default-submode-face' for all submode
369   regions \(except for \"non-submode\" regions, i.e. those that are of
370   the primary mode) and `mmm-delimiter-face' for region delimiters.
371 * High (2) means to use different faces for different types of submode
372   regions and delimiters, such as initialization code, expressions that
373   are output, declarations, and so on, as specified by the submode
374   class.  The default faces are still used for regions that do not
375   specify a face."
376   :group 'mmm-faces
377   :type '(choice (const :tag "None" 0)
378                  (const :tag "Low" 1)
379                  (const :tag "High" 2)))
380
381 (defface mmm-init-submode-face '((t (:background "Pink")))
382   "Face used for submodes containing initialization code."
383   :group 'mmm-faces)
384
385 (defface mmm-cleanup-submode-face '((t (:background "Wheat")))
386   "Face used for submodes containing cleanup code."
387   :group 'mmm-faces)
388
389 (defface mmm-declaration-submode-face '((t (:background "Aquamarine")))
390   "Face used for submodes containing declarations."
391   :group 'mmm-faces)
392
393 (defface mmm-comment-submode-face '((t (:background "SkyBlue")))
394   "Face used for submodes containing comments and documentation."
395   :group 'mmm-faces)
396
397 (defface mmm-output-submode-face '((t (:background "Plum")))
398   "Face used for submodes containing expression that are output."
399   :group 'mmm-faces)
400
401 (defface mmm-special-submode-face '((t (:background "MediumSpringGreen")))
402   "Face used for special submodes not fitting any other category."
403   :group 'mmm-faces)
404
405 (defface mmm-code-submode-face '((t (:background "LightGray")))
406   "Face used for submodes containing ordinary code."
407   :group 'mmm-faces)
408
409 (defface mmm-default-submode-face '((t (:background "gray85")))
410   "Face used for all submodes at decoration level 1.
411 Also used at decoration level 2 for submodes not specifying a type."
412   :group 'mmm-faces)
413
414 (defface mmm-delimiter-face nil
415   "Face used to mark submode delimiters."
416   :group 'mmm-faces)
417
418 ;;}}}
419 ;;{{{ Mode Line Format
420
421 (defcustom mmm-mode-string " MMM"
422   "*String to display in mode line as MMM minor mode indicator."
423   :group 'mmm
424   :type 'string)
425
426 (defcustom mmm-submode-mode-line-format "~M[~m]"
427   "*Format of the mode-line display when point is in a submode region.
428
429 ~M is replaced by the name of the primary major mode \(which may be
430 replaced by a combined-mode function, see the info documentation).
431
432 ~m is replaced by the submode region overlay's `display-name'
433 property, if it has one.  Otherwise it is replaced by the mode name of
434 the submode region.
435
436 If `mmm-primary-mode-display-name' is non-nil, then this variable is
437 used even when point is not in a submode region \(i.e. it is in a
438 primary mode region), with ~m being replaced by the value of that
439 variable."
440   :group 'mmm
441   :type 'string)
442
443 (defvar mmm-primary-mode-display-name nil
444   "If non-nil, displayed as the primary mode name in the mode line.
445 See also `mmm-buffer-mode-display-name'.")
446 (make-variable-buffer-local 'mmm-primary-mode-display-name)
447
448 (defvar mmm-buffer-mode-display-name nil
449   "If non-nil, displayed in the mode line instead of the primary mode
450 name, which is then shown next to it as if it were a submode when in a
451 primary mode region, i.e. outside all submode regions.")
452 (make-variable-buffer-local 'mmm-buffer-mode-display-name)
453
454 (defun mmm-set-mode-line ()
455   "Set the mode line display correctly for the current submode,
456 according to `mmm-submode-mode-line-format'."
457   (let ((primary (or mmm-primary-mode-display-name
458                      (get mmm-primary-mode 'mmm-mode-name)))
459         (submode (and mmm-current-overlay
460                       (or (overlay-get mmm-current-overlay 'display-name)
461                           (get mmm-current-submode 'mmm-mode-name)))))
462     (if mmm-buffer-mode-display-name
463         (setq mode-name
464               (mmm-format-string mmm-submode-mode-line-format
465                                  `(("~M" . ,mmm-buffer-mode-display-name)
466                                    ("~m" . ,(or submode primary)))))
467       (if submode
468           (setq mode-name
469                 (mmm-format-string mmm-submode-mode-line-format
470                                    `(("~M" . ,primary)
471                                      ("~m" . ,submode))))
472         (setq mode-name primary))))
473   (force-mode-line-update))
474
475 ;;}}}
476 ;;{{{ Submode Classes
477
478 (defvar mmm-classes nil
479   "*List of submode classes that apply to a buffer.
480 Generally set in a file local variables list.  Can either be one
481 symbol, or a list of symbols.  Automatically buffer-local.")
482 (make-variable-buffer-local 'mmm-classes)
483
484 (defvar mmm-global-classes '(universal)
485   "*List of submode classes that apply to all buffers.
486 Can be overridden in a file local variables list.")
487
488 ;;}}}
489 ;;{{{ Modes and Extensions
490
491 (defcustom mmm-mode-ext-classes-alist nil
492   "Alist of submode classes for major modes and/or file extensions.
493 This variable can now be directly modified.
494
495 Elements look like \(MODE EXT CLASS), where MODE is a major mode, EXT
496 is a regexp to match a filename such as in `auto-mode-alist', and
497 CLASS is a submode class. CLASS is activated in all buffers in mode
498 MODE \(if non-nil) and whose filenames match EXT \(if non-nil). If
499 both MODE and EXT are nil, CLASS is activated in all buffers. If CLASS
500 is the symbol t, MMM Mode is turned on in all buffers matching MODE
501 and EXT, but no classes are activated.
502
503 See `mmm-global-mode'."
504   :group 'mmm
505   :type '(repeat (list (symbol :tag "Major Mode")
506                        (string :tag "Filename Regexp")
507                        (symbol :tag "Class")))
508   :require 'mmm-mode)
509
510 (defun mmm-add-mode-ext-class (mode ext class)
511   "Add an element to `mmm-mode-ext-classes-alist', which see.
512 That variable can now be directly modified, so this function is
513 unnecessary. It probably won't go away, though."
514   (add-to-list 'mmm-mode-ext-classes-alist (list mode ext class)))
515
516 ;;}}}
517 ;;{{{ Preferred Major Modes
518
519 (defcustom mmm-major-mode-preferences
520   '((perl cperl-mode perl-mode)
521     (javascript javascript-mode c++-mode)
522     (java jde-mode java-mode c++-mode)
523     (css css-mode c++-mode))
524   "User preferences about what major modes to use.
525 Each element has the form \(LANGUAGE . MODES) where LANGUAGE is the
526 name of a programming language such as `perl' as a symbol, and MODES
527 is a list of possible major modes to use, such as `cperl-mode' or
528 `perl-mode'.  The first element of MODES which is `fboundp' is used
529 for submodes of LANGUAGE.  The last element of MODES should be a mode
530 which will always be available."
531   :group 'mmm
532   :type '(repeat (cons symbol
533                        (repeat
534                         (restricted-sexp :match-alternatives
535                                          (fboundp))))))
536
537 (defun mmm-add-to-major-mode-preferences (language mode &optional default)
538   "Set the preferred major mode for LANGUAGE to MODE.
539 This sets the value of `mmm-major-mode-preferences'.  If DEFAULT is
540 nil or unsupplied, MODE is added at the front of the list of modes for
541 LANGUAGE.  If DEFAULT is non-nil, then it is added at the end.  This
542 may be used by packages to ensure that some mode is present, but not
543 override any user-specified mode."
544   (let ((pair (assq language mmm-major-mode-preferences)))
545     (if pair
546         ;; Existing mode preferences
547         (if default
548             (setcdr pair (cons mode (cdr pair)))
549           (setcdr pair (append (cdr pair) (list mode))))
550       ;; No existing mode preference
551       (add-to-list 'mmm-major-mode-preferences (list language mode)))))
552
553 (defun mmm-ensure-modename (symbol)
554   "Return SYMBOL if it is a valid submode name, else nil.
555 Valid submode names are either `fboundp' or present as the `car' of an
556 element in `mmm-major-mode-preferences'."
557   (if (or (fboundp symbol)
558           (assq symbol mmm-major-mode-preferences))
559       symbol
560     nil))
561
562 (defun mmm-modename->function (mode)
563   "Convert MODE to a mode function, nil if impossible.
564 Valid submode names are either `fboundp' or present as the `car' of an
565 element in `mmm-major-mode-preferences'.  In the latter case, the
566 first `fboundp' element of the `cdr' is returned, or nil if none."
567   (if (fboundp mode)
568       mode
569     (car (remove-if-not
570           #'fboundp
571           (cdr (assq mode mmm-major-mode-preferences))))))
572
573 ;;}}}
574 ;;{{{ Delimiter Regions
575
576 (defcustom mmm-delimiter-mode 'fundamental-mode
577   "Major mode used by default for delimiter regions.
578 Classes are encouraged to override this by providing a delimiter-mode
579 parameter-- see `mmm-classes-alist'."
580   :group 'mmm
581   :type 'function)
582
583 ;;}}}
584 ;;{{{ Key Bindings
585
586 (defcustom mmm-mode-prefix-key [(control ?c) ?%]
587   "Prefix key for the MMM Minor Mode Keymap."
588   :group 'mmm
589   :type 'vector)
590
591 (defcustom mmm-command-modifiers '(control)
592   "List of key modifiers for MMM command keys.
593 The MMM commands in the MMM Mode map, after `mmm-mode-prefix-key',
594 are bound to default keys with these modifiers added. This variable
595 must be set before MMM Mode is loaded to have an effect.
596
597 It is suggested that the value of this variable be either nil or
598 \(control), as the default keys are either plain keys or have only a
599 meta modifier. The shift modifier is not particularly portable between
600 Emacsen. The values of this variable and `mmm-insert-modifiers' should
601 be disjoint."
602   :group 'mmm
603   :type '(repeat (symbol :tag "Modifier")))
604
605 (defcustom mmm-insert-modifiers '()
606   "List of key modifiers for MMM submode insertion keys.
607 When a key pressed after `mmm-mode-prefix-key' has no MMM Mode command
608 binding, and its modifiers include these, then its basic type, plus any
609 modifiers in addition to these, is looked up in classes' :insert
610 specifications.
611
612 It is suggested that the value of this variable be either nil or
613 \(control), allowing submode classes to specify the presence or
614 absence of the meta modifier. The shift modifier is not particularly
615 portable between Emacsen. The values of `mmm-command-modifiers' and
616 this variable should be disjoint."
617   :group 'mmm
618   :type '(repeat (symbol :tag "Modifier")))
619
620 (defcustom mmm-use-old-command-keys nil
621   "Non-nil means to Use the old command keys for MMM Mode.
622 MMM Mode commands then have no modifier while insertion commands have
623 a control modifier, i.e. `mmm-command-modifiers' is set to nil and
624 `mmm-insert-modifiers' is set to \(control). If nil, the values of
625 these variables are as the default, or whatever the user has set them
626 to. This variable must be set before MMM Mode is loaded."
627   :group 'mmm
628   :type 'boolean)
629
630 (defun mmm-use-old-command-keys ()
631   "Use the old command keys \(no control modifer) in MMM Mode."
632   (setq mmm-command-modifiers '()
633         mmm-insert-modifiers '(control)))
634
635 ;;}}}
636 ;;{{{ MMM Hooks
637
638 (defcustom mmm-mode-hook ()
639   "Hook run when MMM Mode is enabled in a buffer.
640
641 A hook named mmm-<major-mode>-hook is also run, if it exists. For
642 example, `mmm-html-mode-hook' is run whenever MMM Mode is entered with
643 HTML mode the dominant mode.
644
645 A hook named mmm-<submode>-submode-hook is run when a submode region
646 of a given mode is created. For example, `mmm-cperl-mode-submode-hook'
647 is run whenever a CPerl mode submode region is created, in any buffer.
648 When this hooks are run, point is guaranteed to be at the start of
649 the newly created submode region.
650
651 Finally, a hook named mmm-<class>-class-hook is run whenever a buffer
652 is first mmm-ified with a given submode class. For example,
653 `mmm-mason-class-hook' is run whenever the `mason' class is first
654 applied in a buffer."
655   :group 'mmm
656   :type 'hook)
657
658 (defun mmm-run-constructed-hook (body &optional suffix)
659   "Run the hook named `mmm-<BODY>-<SUFFIX>-hook', if it exists.
660 If SUFFIX is nil or unsupplied, run `mmm-<BODY>-hook' instead."
661   (let ((hook (intern-soft (if suffix
662                                (format "mmm-%s-%s-hook" body suffix)
663                              (format "mmm-%s-hook" body)))))
664     (if hook (run-hooks hook))))
665
666 (defun mmm-run-major-hook ()
667   (mmm-run-constructed-hook mmm-primary-mode))
668
669 (defun mmm-run-submode-hook (submode)
670   (mmm-run-constructed-hook submode "submode"))
671
672 (defvar mmm-class-hooks-run ()
673   "List of submode classes for which hooks have already been run in
674 the current buffer.")
675 (make-variable-buffer-local 'mmm-class-hooks-run)
676
677 (defun mmm-run-class-hook (class)
678   (unless (member class mmm-class-hooks-run)
679     (mmm-run-constructed-hook class "class")
680     (add-to-list 'mmm-class-hooks-run class)))
681
682 (defvar mmm-primary-mode-entry-hook nil
683   "Hook run when point moves into a region of the primary mode.
684 Each submode region can have an `entry-hook' property which is run
685 when they are entered, but since primary mode regions have no overlay
686 to store properties, this is a buffer-local variable.
687
688 N.B. This variable is not a standard Emacs hook.  Unlike Emacs'
689 \"local hooks\" it has *no* global value, only a local one.  Its value
690 should always be a list of functions \(possibly empty) and never a
691 single function.  It may be used with `add-hook', however.")
692 (make-variable-buffer-local 'mmm-primary-mode-entry-hook)
693
694 ;;}}}
695 ;;{{{ Major Mode Hook
696
697 (defcustom mmm-major-mode-hook ()
698   "Hook run whenever a new major mode is finished starting up.
699 MMM Mode implements this with a hack \(see comments in the source) so
700 that `mmm-global-mode' will function correctly, but makes this hook
701 available so that others can take advantage of the hack as well.
702
703 Note that file local variables have *not* been processed by the time
704 this hook is run. If a function needs to inspect them, it should also
705 be added to `find-file-hooks'. However, `find-file-hooks' is not run
706 when creating a non-file-based buffer, or when changing major modes in
707 an existing buffer."
708   :group 'mmm
709   :type 'hook)
710
711 (defun mmm-run-major-mode-hook ()
712   (dolist (func mmm-major-mode-hook)
713     (ignore-errors (funcall func))))
714
715 ;;}}}
716 ;;{{{ MMM Global Mode
717
718 ;;; There's a point to be made that this variable should default to
719 ;;; `maybe' (i.e. not nil and not t), because that's what practically
720 ;;; everyone wants.  I subscribe, however, to the view that simply
721 ;;; *loading* a lisp extension should not change the (user-visible)
722 ;;; behavior of Emacs, until it is configured or turned on in some
723 ;;; way, which dictates that the default for this must be nil.
724 (defcustom mmm-global-mode nil
725   "*Specify in which buffers to turn on MMM Mode automatically.
726
727 - If nil, MMM Mode is never enabled automatically.
728 - If t, MMM Mode is enabled automatically in all buffers.
729 - If any other symbol, MMM mode is enabled only in those buffers that
730   have submode classes associated with them. See `mmm-classes' and
731   `mmm-mode-ext-classes-alist' for more information."
732   :group 'mmm
733   :type '(choice (const :tag "Always" t)
734                  (const :tag "Never" nil)
735                  (other :tag "Maybe" maybe))
736   :require 'mmm-mode)
737
738 ;; These are not traditional editing modes, so mmm makes no sense, and
739 ;; can mess things up seriously if it doesn't know not to try.
740 (defcustom mmm-never-modes
741   '(
742     help-mode
743     Info-mode
744     dired-mode
745     comint-mode
746     telnet-mode
747     shell-mode
748     eshell-mode
749     forms-mode
750     )
751   "List of modes in which MMM Mode is never activated."
752   :group 'mmm
753   :type '(repeat (symbol :tag "Mode")))
754
755 ;;}}}
756 ;;{{{ Buffer File Name
757
758 (defvar mmm-set-file-name-for-modes '(mew-draft-mode)
759   "List of modes for which the temporary buffers MMM creates have a
760 file name.  In these modes, this file name is the same as that of the
761 parent buffer.  In general, this has been found to cause more problems
762 than it solves, but some modes require it.")
763
764 ;;}}}
765
766 ;; NON-USER VARIABLES
767 ;;{{{ Mode Variable
768
769 (defvar mmm-mode nil
770   "Non-nil means MMM Mode is turned on in this buffer.
771 Do not set this variable directly; use the function `mmm-mode'.")
772 (make-variable-buffer-local 'mmm-mode)
773
774 ;;}}}
775 ;;{{{ Primary Mode
776
777 (defvar mmm-primary-mode nil
778   "The primary major mode in the current buffer.")
779 (make-variable-buffer-local 'mmm-primary-mode)
780
781 ;;}}}
782 ;;{{{ Classes Alist
783
784 ;; Notes:
785 ;; 1. :parent could be an all-class argument.  Same with :keymap.
786 ;; 2. :match-submode really does have to be distinct from :submode,
787 ;; because 'functionp' isn't enough to distinguish which is meant.
788 (defvar mmm-classes-alist nil
789   "Alist containing all defined mmm submode classes.
790 A submode class is a named recipe for parsing a document into submode
791 regions, and sometimes for inserting new ones while editing.
792
793 Each element of this alist looks like \(CLASS . ARGS) where CLASS is a
794 symbol naming the submode class and ARGS is a list of keyword
795 arguments, called a \"class specifier\". There are a large number of
796 accepted keyword arguments in the class specifier.
797
798 The argument CLASSES, if supplied, must be a list of other submode
799 class names, or class specifiers, representing other classes to call
800 recursively.  The FACE arguments of these classes are overridden by
801 the FACE argument of this class.  If the argument CLASSES is supplied,
802 all other arguments to this class are ignored.  That is, \"grouping\"
803 classes can do nothing but group other classes.
804
805 The argument HANDLER, if supplied, also overrides any other processing.
806 It must be a function, and all the arguments are passed to it as
807 keywords, and it must do everything. See `mmm-ify' for what sorts of
808 things it must do. This back-door interface should be cleaned up.
809
810 The optional argument FACE gives the display face of the submode
811 regions under high decoration (see `mmm-submode-decoration-level').
812 It must be a valid face.  The standard faces used for submode regions
813 are `mmm-*-submode-face' where * is one of `init', `cleanup',
814 `declaration', `comment', `output', `special', or `code'.  A more
815 flexible alternative is the argument MATCH-FACE.  MATCH-FACE can be a
816 function, which is called with one argument, the form of the front
817 delimiter \(found from FRONT-FORM, below), and should return the face
818 to use.  It can also be an alist, with each element of the form
819 \(DELIM . FACE).
820
821 If neither CLASSES nor HANDLER are supplied, either SUBMODE or
822 MATCH-SUBMODE must be.  SUBMODE specifies the submode to use for the
823 submode regions, a symbol such as `cperl-mode' or `emacs-lisp-mode',
824 while MATCH-SUBMODE must be a function to be called immediately after
825 a match is found for FRONT, which is passed one argument, the form of
826 the front delimiter \(found from FRONT-FORM, below), and return a
827 symbol such as SUBMODE would be set to.  If MATCH-SUBMODE detects an
828 invalid match--for example a specified mode which is not `fboundp'--it
829 should \(signal 'mmm-no-matching-submode nil).
830
831 FRONT and BACK are the means to find the submode regions, and can be
832 either buffer positions \(number-or-markers), regular expressions, or
833 functions. If they are absolute buffer positions, only one submode
834 region is created, from FRONT to BACK. This is generally not used in
835 named classes. \(Unnamed classes are created by interactive commands
836 in `mmm-interactive-history').
837
838 If FRONT is a regexp, then that regexp is searched for, and the end of
839 its FRONT-MATCH'th match \(or the beginning thereof, if INCLUDE-FRONT
840 is non-nil), plus FRONT-OFFSET, becomes the beginning of the submode
841 region.  If FRONT is a function, that function is called instead, and
842 must act somewhat like a search, in that it should start at point,
843 take one argument as a search bound, and set the match data.  A
844 similar pattern is followed for BACK \(the search starts at the
845 beginning of the submode region), save that the beginning of its
846 BACK-MATCH'th match \(or the end, if INCLUDE-BACK is non-nil) becomes
847 the end of the submode region, plus BACK-OFFSET.
848
849 If SAVE-MATCHES is non-nil, then BACK, if it is a regexp, is formatted
850 by replacing strings of the form \"~N\" by the corresponding value of
851 \(match-string n) after matching FRONT.
852
853 FRONT-MATCH and BACK-MATCH default to zero.  They specify which
854 sub-match of the FRONT and BACK regexps to treat as the delimiter.
855 This number will be passed to any calls to `match-beginning' and
856 company.
857
858 FRONT- and BACK-OFFSET default to 0.  In addition to numbers, they can
859 also be functions to call which should move point to the correct
860 position for the beginning or end of the submode region.  Common
861 choices include `beginning-of-line' and `end-of-line', and new
862 functions can of course be written.  They can also be lists which will
863 be applied in sequence, such as \(end-of-line 1) meaning move to end
864 of line and then forward one character.
865
866 FRONT-VERIFY and BACK-VERIFY, if supplied, must be functions that
867 inspect the match data to see if a match found by FRONT or BACK
868 respectively is valid.
869
870 FRONT-DELIM \(resp. BACK-DELIM), if supplied, can take values like
871 those of FRONT-OFFSET \(resp. BACK-OFFSET), specifying the offset from
872 the start \(resp. end) of the match for FRONT \(resp. BACK) to use as
873 the starting \(resp. ending) point for the front \(resp. back)
874 delimiter.  If nil, it means not to make a region for the respective
875 delimiter at all.
876
877 DELIMITER-MODE, if supplied, specifies what submode to use for the
878 delimiter regions, if any.  If `nil', the primary mode is used.  If
879 not supplied, `mmm-delimiter-mode' is used.
880
881 FRONT-FACE and BACK-FACE specify faces to use for displaying the
882 delimiter regions, under high decoration.
883
884 FRONT-FORM and BACK-FORM, if given, must supply a regexp used to match
885 the *actual* delimiter.  If they are strings, they are used as-is.  If
886 they are functions, they are called and must inspect the match data.
887 If they are lists, their `car' is taken as the delimiter.  The default
888 for both is \(regexp-quote \(match-string 0)).
889
890 The last case--them being a list--is usually used to set the delimiter
891 to a function.  Such a function must take 1-2 arguments, the first
892 being the overlay in question, and the second meaning to insert the
893 delimiter and adjust the overlay rather than just matching the
894 delimiter.  See `mmm-match-front', `mmm-match-back', and
895 `mmm-end-current-region'.
896
897 CASE-FOLD-SEARCH, if specified, controls whether the search is
898 case-insensitive. See `case-fold-search'. It defaults to `t'.
899
900 CREATION-HOOK, if specified, should be a function which is run
901 whenever a submode region is created, with point at the beginning of
902 the new region.  One use for it is to set region-saved local variables
903 \(see `mmm-save-local-variables').
904
905 INSERT specifies the keypress insertion spec for such submode regions.
906 INSERT's value should be list of elements of the form \(KEY NAME .
907 SPEC). Each KEY should be either a character, a function key symbol,
908 or a dotted list \(MOD . KEY) where MOD is a symbol for a modifier
909 key. The use of any other modifier than meta is discouraged, as
910 `mmm-insert-modifiers' is sometimes set to \(control), and other
911 modifiers are not very portable. Each NAME should be a symbol
912 representing the insertion for that key. Each SPEC can be either a
913 skeleton, suitable for passing to `skeleton-insert' to create a
914 submode region, or a dotted pair \(OTHER-KEY . ARG) meaning to use the
915 skeleton defined for OTHER-KEY but pass it the argument ARG as the
916 `str' variable, possible replacing a prompt string. Skeletons for
917 insertion should have the symbol `_' where point \(or wrapped text)
918 should go, and the symbol `@' in four different places: at the
919 beginning of the front delimiter, the beginning of the submode region,
920 the end of the submode region, and the end of the back delimiter.
921
922 If END-NOT-BEGIN is non-nil, it specifies that a BACK delimiter cannot
923 begin a new submode region.
924
925 MATCH-NAME, if supplied, specifies how to determine the \"name\" for
926 each submode region.  It must be a string or a function.  If it is a
927 function, it is passed the value of FRONT-FORM and must return the
928 name to use.  If it is a string, it is used as-is unless SAVE-NAME has
929 a non-nil value, in which case, the string is interpreted the same as
930 BACK when SAVE-MATCHES is non-nil.  If MATCH-NAME is not specified,
931 the regions are unnamed.  Regions with the same name are considered
932 part of the same chunk of code, and formatted as such, while unnamed
933 regions are not grouped with any others.
934
935 As a special optimization for insertion, if SKEL-NAME is non-nil, the
936 insertion code will use the user-prompted string value as the region
937 name, instead of going through the normal matching procedure.
938
939 PRIVATE, if supplied and non-nil, means that this class is a private
940 or internal class, usually one invoked by another class via :classes,
941 and is not for the user to see.")
942
943 (defun mmm-add-classes (classes)
944   "Add the submode classes CLASSES to `mmm-classes-alist'."
945   (dolist (class classes)
946     (add-to-list 'mmm-classes-alist class)))
947
948 (defun mmm-add-group (group classes)
949   "Add CLASSES and a \"grouping class\" named GROUP which calls them all.
950 The CLASSES are all made private, i.e. non-user-visible."
951   (mmm-add-classes (mapcar #'(lambda (class)
952                                (append class
953                                        '(:private t)))
954                            classes))
955   (add-to-list 'mmm-classes-alist
956                (list group :classes (mapcar #'first classes))))
957
958 (defun mmm-add-to-group (group classes)
959   "Add CLASSES to the \"grouping class\" named GROUP.
960 The CLASSES are all made private, i.e. non-user-visible."
961   (mmm-add-classes (mapcar #'(lambda (class)
962                                (append class
963                                        '(:private t)))
964                            classes))
965   (mmm-set-class-parameter group :classes
966                            (append  (mmm-get-class-parameter group :classes)
967                                     (mapcar #'first classes))))
968
969 ;;}}}
970 ;;{{{ Version Number
971
972 (defconst mmm-version "0.4.8"
973   "Current version of MMM Mode.")
974
975 (defun mmm-version ()
976   (interactive)
977   (message "MMM Mode version %s by Michael Abraham Shulman" mmm-version))
978
979 ;;}}}
980 ;;{{{ Temp Buffer Name
981
982 (defvar mmm-temp-buffer-name " *mmm-temp*"
983   "Name for temporary buffers created by MMM Mode.")
984
985 ;;}}}
986 ;;{{{ Interactive History
987
988 (defvar mmm-interactive-history nil
989   "History of interactive mmm-ification in the current buffer.
990 Elements are either submode class symbols or class specifications. See
991 `mmm-classes-alist' for more information.")
992 (make-variable-buffer-local 'mmm-interactive-history)
993
994 (defun mmm-add-to-history (class)
995   (add-to-list 'mmm-interactive-history class))
996
997 (defun mmm-clear-history ()
998   "Clears history of interactive mmm-ification in current buffer."
999   (interactive)
1000   (setq mmm-interactive-history nil))
1001
1002 ;;}}}
1003 ;;{{{ Mode/Ext Manipulation
1004
1005 (defvar mmm-mode-ext-classes ()
1006   "List of classes associated with current buffer by mode and filename.
1007 Set automatically from `mmm-mode-ext-classes-alist'.")
1008 (make-variable-buffer-local 'mmm-mode-ext-classes)
1009
1010 (defun mmm-get-mode-ext-classes ()
1011   "Return classes for current buffer from major mode and filename.
1012 Uses `mmm-mode-ext-classes-alist' to find submode classes."
1013   (or mmm-mode-ext-classes
1014       (setq mmm-mode-ext-classes
1015             (mapcar #'third
1016                     (remove-if-not #'mmm-mode-ext-applies
1017                                    mmm-mode-ext-classes-alist)))))
1018
1019 (defun mmm-clear-mode-ext-classes ()
1020   "Clear classes added by major mode and filename."
1021   (setq mmm-mode-ext-classes nil))
1022
1023 (defun mmm-mode-ext-applies (element)
1024   (destructuring-bind (mode ext class) element
1025     (and (if mode
1026              (eq mode
1027                  ;; If MMM is on in this buffer, use the primary mode,
1028                  ;; otherwise use the normal indicator.
1029                  (or mmm-primary-mode major-mode))
1030            t)
1031          (if ext
1032              (and (buffer-file-name)
1033                   (save-match-data
1034                     (string-match ext (buffer-file-name))))
1035            t))))
1036
1037 (defun mmm-get-all-classes (global)
1038   "Return a list of all classes applicable to the current buffer.
1039 These come from mode/ext associations, `mmm-classes', and interactive
1040 history, as well as `mmm-global-classes' if GLOBAL is non-nil."
1041   (append mmm-interactive-history
1042           (if (listp mmm-classes) mmm-classes (list mmm-classes))
1043           (if global mmm-global-classes ())
1044           (mmm-get-mode-ext-classes)))
1045
1046 ;;}}}
1047
1048 (provide 'mmm-vars)
1049
1050 ;;; mmm-vars.el ends here