1 ;;; mmm-vars.el --- Variables for MMM Mode
3 ;; Copyright (C) 2000, 2004 by Michael Abraham Shulman
5 ;; Author: Michael Abraham Shulman <viritrilbia@users.sourceforge.net>
6 ;; Version: $Id: mmm-vars.el,v 1.56 2004/06/16 14:14:18 alanshutko Exp $
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)
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.
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.
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.
38 ;;{{{ Shut up the Byte Compiler
40 ;; Otherwise it complains about undefined variables.
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))
53 ;;{{{ Error Conditions
55 ;; Most of these should be caught internally and never seen by the
56 ;; user, except when the user is creating submode regions manually.
58 ;; Signalled when we try to put a submode region inside one where it
60 (put 'mmm-subregion-invalid-parent
62 '(mmm-subregion-invalid-parent mmm-error error))
63 (put 'mmm-subregion-invalid-parent
65 "Invalid submode region parent")
67 ;; Signalled when we try to put a submode region overlapping others in
69 (put 'mmm-subregion-invalid-placement
71 '(mmm-subregion-invalid-placement mmm-error error))
72 (put 'mmm-subregion-invalid-placement
74 "Submode region placement invalid")
76 ;; Signalled when we try to apply a submode class that doesn't exist.
77 (put 'mmm-invalid-submode-class
79 '(mmm-invalid-submode-class mmm-error error))
80 (put 'mmm-invalid-submode-class
82 "Invalid or undefined submode class")
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
89 '(mmm-no-matching-submode mmm-error error))
90 (put 'mmm-no-matching-submode
92 "Internal error: no matching submode.")
97 ;;{{{ Customization Group
100 "Multiple Major Modes in one buffer."
104 ;;{{{ Save Local Variables
106 (defvar mmm-c-derived-modes
107 '(c-mode c++-mode objc-mode pike-mode java-mode jde-mode javascript-mode
110 (defvar mmm-save-local-variables
111 `(;; Don't use `function' (#') here!! We're already inside `quote'!
115 (comment-line-start-skip buffer (fortran-mode))
117 (comment-column buffer)
118 comment-indent-function
119 comment-line-break-function
123 (((lambda () current-menubar) . set-buffer-menubar))
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
132 parse-sexp-ignore-comments ; Fixes indentation in PHP-mode?
133 ;; Can be different in different buffers
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'.
146 #'(lambda (var) (list var nil mmm-c-derived-modes))
147 '(c++-template-syntax-table
150 c-after-suffixed-type-decl-key
151 c-after-suffixed-type-maybe-decl-key
155 c-assignment-op-regexp
159 c-block-comment-prefix
160 c-block-decls-with-vars
174 c-comment-only-line-offset
175 c-comment-prefix-regexp
176 c-comment-start-regexp
177 c-comment-start-regexp
179 c-current-comment-prefix
184 c-doc-comment-start-regexp
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
194 c-identifier-last-sym-match
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
214 c-label-minimum-indentation
216 c-literal-start-regexp
218 c-nonsymbol-token-regexp
219 c-not-decl-init-keywords
221 c-opt-<>-arglist-start
222 c-opt-<>-arglist-start-in-paren
230 c-opt-block-decls-with-vars-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
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
257 c-overloadable-operators-regexp
261 c-primary-expr-regexp
263 c-primitive-type-kwds
265 c-recognize-<>-arglists
268 c-recognize-paren-inits
269 c-recognize-typeless-decls
270 c-regular-keywords-regexp
273 c-special-brace-lists
274 c-special-brace-lists
278 c-stmt-delim-chars-with-comma
285 c-type-decl-prefix-key
286 c-type-decl-suffix-key
291 ;; Skeleton insertion
292 skeleton-transformation
296 ;; And finally the syntax table and local map.
297 ((syntax-table . set-syntax-table))
298 ((current-local-map . use-local-map) buffer)
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
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).
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'.
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.
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.")
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)
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)
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)
357 (defgroup mmm-faces nil
358 "Faces and coloring for submode regions.
359 In general, only background colors should be set, to avoid interfering
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
377 :type '(choice (const :tag "None" 0)
379 (const :tag "High" 2)))
381 (defface mmm-init-submode-face '((t (:background "Pink")))
382 "Face used for submodes containing initialization code."
385 (defface mmm-cleanup-submode-face '((t (:background "Wheat")))
386 "Face used for submodes containing cleanup code."
389 (defface mmm-declaration-submode-face '((t (:background "Aquamarine")))
390 "Face used for submodes containing declarations."
393 (defface mmm-comment-submode-face '((t (:background "SkyBlue")))
394 "Face used for submodes containing comments and documentation."
397 (defface mmm-output-submode-face '((t (:background "Plum")))
398 "Face used for submodes containing expression that are output."
401 (defface mmm-special-submode-face '((t (:background "MediumSpringGreen")))
402 "Face used for special submodes not fitting any other category."
405 (defface mmm-code-submode-face '((t (:background "LightGray")))
406 "Face used for submodes containing ordinary code."
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."
414 (defface mmm-delimiter-face nil
415 "Face used to mark submode delimiters."
419 ;;{{{ Mode Line Format
421 (defcustom mmm-mode-string " MMM"
422 "*String to display in mode line as MMM minor mode indicator."
426 (defcustom mmm-submode-mode-line-format "~M[~m]"
427 "*Format of the mode-line display when point is in a submode region.
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).
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
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
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)
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)
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
464 (mmm-format-string mmm-submode-mode-line-format
465 `(("~M" . ,mmm-buffer-mode-display-name)
466 ("~m" . ,(or submode primary)))))
469 (mmm-format-string mmm-submode-mode-line-format
472 (setq mode-name primary))))
473 (force-mode-line-update))
476 ;;{{{ Submode Classes
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)
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.")
489 ;;{{{ Modes and Extensions
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.
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.
503 See `mmm-global-mode'."
505 :type '(repeat (list (symbol :tag "Major Mode")
506 (string :tag "Filename Regexp")
507 (symbol :tag "Class")))
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)))
517 ;;{{{ Preferred Major Modes
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."
532 :type '(repeat (cons symbol
534 (restricted-sexp :match-alternatives
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)))
546 ;; Existing mode preferences
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)))))
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))
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."
571 (cdr (assq mode mmm-major-mode-preferences))))))
574 ;;{{{ Delimiter Regions
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'."
586 (defcustom mmm-mode-prefix-key [(control ?c) ?%]
587 "Prefix key for the MMM Minor Mode Keymap."
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.
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
603 :type '(repeat (symbol :tag "Modifier")))
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
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."
618 :type '(repeat (symbol :tag "Modifier")))
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."
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)))
638 (defcustom mmm-mode-hook ()
639 "Hook run when MMM Mode is enabled in a buffer.
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.
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.
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."
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))))
666 (defun mmm-run-major-hook ()
667 (mmm-run-constructed-hook mmm-primary-mode))
669 (defun mmm-run-submode-hook (submode)
670 (mmm-run-constructed-hook submode "submode"))
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)
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)))
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.
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)
695 ;;{{{ Major Mode Hook
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.
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
711 (defun mmm-run-major-mode-hook ()
712 (dolist (func mmm-major-mode-hook)
713 (ignore-errors (funcall func))))
716 ;;{{{ MMM Global Mode
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.
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."
733 :type '(choice (const :tag "Always" t)
734 (const :tag "Never" nil)
735 (other :tag "Maybe" maybe))
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
751 "List of modes in which MMM Mode is never activated."
753 :type '(repeat (symbol :tag "Mode")))
756 ;;{{{ Buffer File Name
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.")
766 ;; NON-USER VARIABLES
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)
777 (defvar mmm-primary-mode nil
778 "The primary major mode in the current buffer.")
779 (make-variable-buffer-local 'mmm-primary-mode)
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.
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.
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.
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.
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
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).
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').
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.
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.
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
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.
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.
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
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.
881 FRONT-FACE and BACK-FACE specify faces to use for displaying the
882 delimiter regions, under high decoration.
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)).
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'.
897 CASE-FOLD-SEARCH, if specified, controls whether the search is
898 case-insensitive. See `case-fold-search'. It defaults to `t'.
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').
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.
922 If END-NOT-BEGIN is non-nil, it specifies that a BACK delimiter cannot
923 begin a new submode region.
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.
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.
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.")
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)))
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)
955 (add-to-list 'mmm-classes-alist
956 (list group :classes (mapcar #'first classes))))
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)
965 (mmm-set-class-parameter group :classes
966 (append (mmm-get-class-parameter group :classes)
967 (mapcar #'first classes))))
972 (defconst mmm-version "0.4.8"
973 "Current version of MMM Mode.")
975 (defun mmm-version ()
977 (message "MMM Mode version %s by Michael Abraham Shulman" mmm-version))
980 ;;{{{ Temp Buffer Name
982 (defvar mmm-temp-buffer-name " *mmm-temp*"
983 "Name for temporary buffers created by MMM Mode.")
986 ;;{{{ Interactive History
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)
994 (defun mmm-add-to-history (class)
995 (add-to-list 'mmm-interactive-history class))
997 (defun mmm-clear-history ()
998 "Clears history of interactive mmm-ification in current buffer."
1000 (setq mmm-interactive-history nil))
1003 ;;{{{ Mode/Ext Manipulation
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)
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
1016 (remove-if-not #'mmm-mode-ext-applies
1017 mmm-mode-ext-classes-alist)))))
1019 (defun mmm-clear-mode-ext-classes ()
1020 "Clear classes added by major mode and filename."
1021 (setq mmm-mode-ext-classes nil))
1023 (defun mmm-mode-ext-applies (element)
1024 (destructuring-bind (mode ext class) element
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))
1032 (and (buffer-file-name)
1034 (string-match ext (buffer-file-name))))
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)))
1050 ;;; mmm-vars.el ends here