Initial Commit
[packages] / xemacs-packages / mmm-mode / mmm-auto.el
1 ;;; mmm-auto.el --- loading and enabling MMM Mode automatically
2
3 ;; Copyright (C) 2000 by Michael Abraham Shulman
4
5 ;; Author: Michael Abraham Shulman <mas@kurukshetra.cjb.net>
6 ;; Version: $Id: mmm-auto.el,v 1.2 2008-12-22 14:02:24 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 contains functions and hooks to load and enable MMM Mode
30 ;; automatically. It sets up autoloads for the main MMM Mode functions
31 ;; and interactive commands, and also sets up MMM Global Mode.
32
33 ;;{{{ Comments on MMM Global Mode
34
35 ;; This is a kludge borrowed from `global-font-lock-mode'.  The idea
36 ;; is the same: we have a function (here `mmm-mode-on-maybe') that we
37 ;; want to be run whenever a major mode starts.  Unfortunately, there
38 ;; is no hook (like, say `major-mode-hook') that all major modes run
39 ;; when they are finished.  `post-command-hook', however, is run after
40 ;; *every* command, so we do our work in there.  (Actually, using
41 ;; `post-command-hook' is even better than being run by major mode
42 ;; functions, since it is run after all local variables and text are
43 ;; loaded, which may not be true in certain cases for the other.)
44
45 ;; In order to do this magic, we rely on the fact that there *is* a
46 ;; hook that all major modes run when *beginning* their work. They
47 ;; call `kill-all-local-variables' (unless they are broken), which in
48 ;; turn runs `change-major-mode-hook'.  So we add a function to *that*
49 ;; hook which saves the current buffer and temporarily adds a function
50 ;; to `post-command-hook' which processes that buffer.
51
52 ;; Actually, in the interests of generality, what that function does
53 ;; is run the hook `mmm-major-mode-hook'. Our desired function
54 ;; `mmm-mode-on-maybe' is then added to that hook. This way, if the
55 ;; user wants to run something else on every major mode, they can just
56 ;; add it to `mmm-major-mode-hook' and take advantage of this hack.
57
58 ;;}}}
59
60 ;;; Code:
61
62 (require 'cl)
63 (require 'mmm-vars)
64
65 ;;{{{ Autoload Submode Classes
66
67 (defvar mmm-autoloaded-classes
68   '((mason "mmm-mason" nil)
69     (embedded-css "mmm-sample" nil)
70     (html-js "mmm-sample" nil)
71     (here-doc "mmm-sample" nil)
72     (embperl "mmm-sample" nil)
73     (eperl "mmm-sample" nil)
74     (jsp "mmm-sample" nil)
75     (file-variables "mmm-sample" nil)
76     (rpm-sh "mmm-rpm" t)
77     (rpm "mmm-rpm" nil)
78     (cweb "mmm-cweb" nil)
79     (sgml-dtd "mmm-sample" nil)
80     (noweb "mmm-noweb" nil)
81     (html-php "mmm-sample" nil)
82     )
83   "Alist of submode classes autoloaded from files.
84 Elements look like \(CLASS FILE PRIVATE) where CLASS is a submode
85 class symbol, FILE is a string suitable for passing to `load', and
86 PRIVATE is non-nil if the class is invisible to the user.  Classes can
87 be added to this list with `mmm-autoload-class'.")
88
89 (defun mmm-autoload-class (class file &optional private)
90   "Autoload submode class CLASS from file FILE.
91 PRIVATE, if non-nil, means the class is user-invisible.  In general,
92 private classes need not be autoloaded, since they will usually be
93 invoked by a public class in the same file."
94   ;; Don't autoload already defined classes
95   (unless (assq class mmm-classes-alist)
96     (add-to-list 'mmm-autoloaded-classes
97                  (list class file private))))
98
99 ;;}}}
100 ;;{{{ Autoload Functions
101
102 ;; To shut up the byte compiler.
103 (eval-and-compile
104   (autoload 'mmm-mode-on "mmm-mode" "Turn on MMM Mode. See `mmm-mode'.")
105   (autoload 'mmm-mode-off "mmm-mode" "Turn off MMM Mode. See `mmm-mode'.")
106   (autoload 'mmm-update-font-lock-buffer "mmm-region")
107   (autoload 'mmm-ensure-fboundp "mmm-utils")
108   (autoload 'mmm-mode "mmm-mode"
109     "Minor mode to allow multiple major modes in one buffer.
110 Without ARG, toggle MMM Mode. With ARG, turn MMM Mode on iff ARG is
111 positive and off otherwise." t))
112
113 ;; These may actually be used.
114 (autoload 'mmm-ify-by-class "mmm-cmds" "" t)
115 (autoload 'mmm-ify-by-regexp "mmm-cmds" "" t)
116 (autoload 'mmm-ify-region "mmm-cmds" "" t)
117 (autoload 'mmm-parse-buffer "mmm-cmds" "" t)
118 (autoload 'mmm-parse-region "mmm-cmds" "" t)
119 (autoload 'mmm-parse-block "mmm-cmds" "" t)
120 (autoload 'mmm-clear-current-region "mmm-cmds" "" t)
121 (autoload 'mmm-reparse-current-region "mmm-cmds" "" t)
122 (autoload 'mmm-end-current-region "mmm-cmds" "" t)
123 (autoload 'mmm-insertion-help "mmm-cmds" "" t)
124 (autoload 'mmm-insert-region "mmm-cmds" "" t)
125
126 ;;}}}
127 ;;{{{ MMM Global Mode
128
129 (defvar mmm-changed-buffers-list ()
130   "Buffers that need to be checked for running the major mode hook.")
131
132 (defun mmm-major-mode-change ()
133   "Add this buffer to `mmm-changed-buffers-list' for checking.
134 When the current command is over, MMM Mode will be turned on in this
135 buffer depending on the value of `mmm-global-mode'.  Actually,
136 everything in `mmm-major-mode-hook' will be run."
137   (and (boundp 'mmm-mode)
138        mmm-mode
139        (mmm-mode-off))
140   (add-to-list 'mmm-changed-buffers-list (current-buffer))
141   (add-hook 'post-command-hook 'mmm-check-changed-buffers))
142
143 (add-hook 'change-major-mode-hook 'mmm-major-mode-change)
144
145 (defun mmm-check-changed-buffers ()
146   "Run major mode hook for the buffers in `mmm-changed-buffers-list'."
147   (remove-hook 'post-command-hook 'mmm-check-changed-buffers)
148   (dolist (buffer mmm-changed-buffers-list)
149     (when (buffer-live-p buffer)
150       (save-excursion
151         (set-buffer buffer)
152         (mmm-run-major-mode-hook))))
153   (setq mmm-changed-buffers-list '()))
154
155 (defun mmm-mode-on-maybe ()
156   "Conditionally turn on MMM Mode.
157 Turn on MMM Mode if `global-mmm-mode' is non-nil and there are classes
158 to apply, or always if `global-mmm-mode' is t."
159   (cond ((eq mmm-global-mode t) (mmm-mode-on))
160         ((not mmm-global-mode))
161         ((mmm-get-all-classes nil) (mmm-mode-on)))
162   (when mmm-mode
163     (mmm-update-font-lock-buffer)))
164
165 (add-hook 'mmm-major-mode-hook 'mmm-mode-on-maybe)
166
167 (defalias 'mmm-add-find-file-hooks 'mmm-add-find-file-hook)
168 (defun mmm-add-find-file-hook ()
169   "Equivalent to \(setq mmm-global-mode 'maybe).
170 This function is deprecated and may be removed in future."
171   (message "Warning: `mmm-add-find-file-hook' is deprecated.")
172   (setq mmm-global-mode 'maybe))
173
174 ;;}}}
175
176 (provide 'mmm-auto)
177
178 ;;; mmm-auto.el ends here