emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master f1bfef8 048/433: Changed mmm-global-mode to use post-comma


From: Dmitry Gutov
Subject: [elpa] master f1bfef8 048/433: Changed mmm-global-mode to use post-command-hook method rather than
Date: Thu, 15 Mar 2018 19:43:32 -0400 (EDT)

branch: master
commit f1bfef8aa9622227de9dd62b37eb4168669a72aa
Author: mas <mas>
Commit: mas <mas>

    Changed mmm-global-mode to use post-command-hook method rather than
    stack-walk method.
---
 NEWS          | 14 +++++++++
 TODO          | 36 +++++++++-------------
 mmm-auto.el   | 97 +++++++++++++++++++++--------------------------------------
 mmm-region.el | 19 +++---------
 mmm-vars.el   |  9 ++++--
 mmm.texinfo   | 55 +++++++++++----------------------
 6 files changed, 90 insertions(+), 140 deletions(-)

diff --git a/NEWS b/NEWS
index f376b93..53d19c0 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,20 @@ both globally, per-buffer, or per-submode region.  This 
facility is
 now used to save the font-lock cache state, possible improving the
 font-lock support.  See the docs for `mmm-save-local-variables'.
 
+** Get and Set Class Parameters
+
+The functions `mmm-[get,set]-class-parameters' do just that.  The
+latter modifies the definition of a submode class, affecting all
+subsequent applications of that class.
+
+** New Implementation for MMM Global Mode
+
+The implementation of MMM Global Mode has been changed from the
+"stack-walk" method to the "post-command-hook" method used by
+global-font-lock-mode.  This is arguably cleaner, but more
+importantly, waits until after all local variables and text are loaded
+before trying to enabling MMM Mode.
+
 
 * Changes in MMM Mode 0.3.8
 
diff --git a/TODO b/TODO
index 7de2c20..93c09df 100644
--- a/TODO
+++ b/TODO
@@ -3,39 +3,31 @@ Hey Emacs, this is a -*-text-*- file!
                        To Do List for MMM Mode
                        =======================
 
-Allow setting of region-creation hooks et. al. by submode class.  Make
-sure to run `mmm-save-changed-local-variables' afterwards, in case
-they set some things.
-
-Saving local map by buffer, along with post-command-hook (for after
-`vm-mail' done), should fix the vm-mail problem w/o advice.  (Could
-also try to get VM to create its own mode instead of piggybacking on
-mail-mode.)
-
-Use the post-command-hook way instead of stack-walk, to catch
-default-major-mode buffers which don't get re-set by normal-mode after
-the contents are available.
-
 Add a flag to specify with more detail where submode regions start and
 stop with respect to the matches.  Perhaps a flag that takes a
 function as argument, with several supplied functions to choose from.
 `next-newline' and `previous-newline' are possible ones, as are
 beginning, end, and current default.  They should be passed the
-submode class and inspect the match data.
+submode class and inspect the match data.  Maybe they return info in
+the match data as well.
 
 Add `mmm-global-classes' variable, specifying submode classes that
-apply to all buffers in which MMM Mode is on.  Doesn't trigger 'maybe,
-I guess.  Not buffer-local by default, but can be turned off file
-locally.  Supply a class which lets you delimit regions and specify
-the mode, e.g. %[cperl]% ... %[/cperl]%.  For this, need a class
-parameter which specifies the submode dynamically rather than
-statically, say :find-submode?
-
-`mmm-set-class-parameter' and same for `*-get-*'.
+apply to all buffers in which MMM Mode is on.  Not buffer-local by
+default, but can be turned off file locally.  Supply a class which
+lets you delimit regions and specify the mode, e.g. %[cperl]%
+... %[/cperl]%.  For this, need a class parameter which specifies the
+submode dynamically rather than statically, say :find-submode?
+If mmm-global-mode is 'maybe, it would be nice to detect whether a
+buffer contains such strings to conditionally turn MMM on.
 
 Improve font-locking somehow.  Maybe narrow the region before
 fontifying, maybe trap or advise `parse-partial-sexp' (and friends?).
 
+The local-variables improvements can probably be used to set minor
+modes locally to submode regions.  This could replace tmmofl,
+especially if we search for regions other than by regexps, say by
+syntax properties.
+
 Inserting one-line perl sections isn't sticky.  Other sections are
 getting un-stickified too easily, and re-parsing current region
 doesn't work as it should.
diff --git a/mmm-auto.el b/mmm-auto.el
index a39f383..a2d7abd 100644
--- a/mmm-auto.el
+++ b/mmm-auto.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2000 by Michael Abraham Shulman
 
 ;; Author: Michael Abraham Shulman <address@hidden>
-;; Version: $Id: mmm-auto.el,v 1.6 2000/06/27 00:35:02 mas Exp $
+;; Version: $Id: mmm-auto.el,v 1.7 2000/06/27 19:22:56 mas Exp $
 
 ;;{{{ GPL
 
@@ -32,43 +32,29 @@
 
 ;;{{{ Comments on MMM Global Mode
 
-;; This is a kludge partially borrowed from `global-font-lock-mode'.
-;; The idea is the same: we have a function (here `mmm-mode-on-maybe')
-;; that we want to be run whenever a major mode starts. Unfortunately,
-;; there is no hook (like, say `major-mode-hook') that all major modes
-;; run when they are finished. They just run their own, specific hook.
-;; So what we're going to do is find a way to insinuate our function
-;; into *all* those hooks. (This is a bit different from what global
-;; font-lock mode does--it uses `post-command-hook').
+;; This is a kludge borrowed from `global-font-lock-mode'.  The idea
+;; is the same: we have a function (here `mmm-mode-on-maybe') that we
+;; want to be run whenever a major mode starts.  Unfortunately, there
+;; is no hook (like, say `major-mode-hook') that all major modes run
+;; when they are finished.  `post-command-hook', however, is run after
+;; *every* command, so we do our work in there.  (Actually, using
+;; `post-command-hook' is even better than being run by major mode
+;; functions, since it is run after all local variables and text are
+;; loaded, which may not be true in certain cases for the other.)
 
 ;; In order to do this magic, we rely on the fact that there *is* a
 ;; hook that all major modes run when *beginning* their work. They
-;; must call `kill-all-local-variables', which in turn runs
-;; `change-major-mode-hook'. So we add a function to *that* hook which
-;; inspects the call stack to find the mode function which is calling
-;; it (mode functions are recognizable by ending in "-mode"), and add
-;; our function to that mode's hook.
-
-;; Actually, in the interests of generality, what it adds to that
-;; mode's hook is the function `mmm-run-major-mode-hook', which in
-;; turn runs the hook `mmm-major-mode-hook'. Our desired function
+;; call `kill-all-local-variables' (unless they are broken), which in
+;; turn runs `change-major-mode-hook'.  So we add a function to *that*
+;; hook which saves the current buffer and temporarily adds a function
+;; to `post-command-hook' which processes that buffer.
+
+;; Actually, in the interests of generality, what that function does
+;; is run the hook `mmm-major-mode-hook'. Our desired function
 ;; `mmm-mode-on-maybe' is then added to that hook. This way, if the
 ;; user wants to run something else on every major mode, they can just
 ;; add it to `mmm-major-mode-hook' and take advantage of this hack.
 
-;; In out-of-the box Emacs, almost all major modes will be four frames
-;; back. The frames are:
-;; 1. mmm-major-mode-change
-;; 2. run-hooks(change-major-mode-hook)
-;; 3. kill-all-local-variables
-;; 4. DESIRED-MAJOR-mode
-;; When gnuserv is loaded, it adds an extra layer (a function called
-;; `server-kill-all-local-variables'), making five. I can imagine
-;; other packages doing the same thing, so for safety's sake, if we
-;; don't find a function whose name ends in `-mode', we keep looking
-;; until we run out of frames. I'm 99% sure that there will always be
-;; at least four frames, though.
-
 ;;}}}
 
 ;;; Code:
@@ -99,39 +85,29 @@ positive and off otherwise." t))
 (autoload 'mmm-insert-region "mmm-cmds" "" t)
 
 ;;}}}
-;;{{{ Automatic Hook Adding
+;;{{{ MMM Global Mode
+
+(defvar mmm-changed-buffers-list ()
+  "Buffers that need to be checked for running the major mode hook.")
 
 (defun mmm-major-mode-change ()
-  "Add mode hooks to turn MMM Mode on where appropriate.
-Actually adds `mmm-run-major-mode-hook' to all major mode hooks."
+  "Add this buffer to `mmm-changed-buffers-list' for checking.
+When the current command is over, MMM Mode will be turned on in this
+buffer depending on the value of `mmm-global-mode'.  Actually,
+everything in `mmm-major-mode-hook' will be run."
   (and (boundp 'mmm-mode)
        mmm-mode
        (mmm-mode-off))
-  (unless (window-minibuffer-p (selected-window))
-    (loop for lookback from 4
-          for frame = (backtrace-frame lookback)
-          while frame
-          if (mmm-get-mode-hook (cadr frame))
-          do (add-hook it 'mmm-run-major-mode-hook)
-          and return t)))
-(add-hook 'change-major-mode-hook 'mmm-major-mode-change)
-
-(defun mmm-get-mode-hook (function)
-  "If FUNCTION is a mode function, get its hook variable.
-Otherwise, return nil."
-  (when (symbolp function)
-    (let ((name (symbol-name function)))
-      (and (> (length name) 5)
-           (string= (substring name -5) "-mode")
-           (intern (format "%s-hook" name))))))
+  (add-to-list 'mmm-changed-buffers-list (current-buffer))
+  (add-hook 'post-command-hook 'mmm-check-changed-buffers))
 
-;; Some modes that derive from text-mode, such as mh-letter-mode and
-;; AucTeX latex-mode, aren't well-behaved.  They do generally run
-;; text-mode-hook, though, so we can pre-initialize that.
-(add-hook 'text-mode-hook 'mmm-run-major-mode-hook)
+(add-hook 'change-major-mode-hook 'mmm-major-mode-change)
 
-;;}}}
-;;{{{ MMM Global Mode
+(defun mmm-check-changed-buffers ()
+  "Run major mode hook for the buffers in `mmm-changed-buffers-list'."
+  (remove-hook 'post-command-hook 'mmm-check-changed-buffers)
+  (mapc #'mmm-run-major-mode-hook mmm-changed-buffers-list)
+  (setq mmm-changed-buffers-list '()))
 
 (defun mmm-mode-on-maybe ()
   "Conditionally turn on MMM Mode.
@@ -141,18 +117,13 @@ to apply, or always if `global-mmm-mode' is t."
         ((not mmm-global-mode))
         ((mmm-get-all-classes) (mmm-mode-on))))
 
-;; Add our function to our hook.
 (add-hook 'mmm-major-mode-hook 'mmm-mode-on-maybe)
 
-;; File Local variables don't get set by the time the major mode is
-;; starting up, apparently. So we need to add the hook here too.
-(add-hook 'find-file-hooks 'mmm-mode-on-maybe)
-
 (defalias 'mmm-add-find-file-hooks 'mmm-add-find-file-hook)
-
 (defun mmm-add-find-file-hook ()
   "Equivalent to \(setq mmm-global-mode 'maybe).
 This function is deprecated and may be removed in future."
+  (message "Warning: `mmm-add-find-file-hook' is deprecated.")
   (setq mmm-global-mode 'maybe))
 
 ;;}}}
diff --git a/mmm-region.el b/mmm-region.el
index cbc29d7..55f99b9 100644
--- a/mmm-region.el
+++ b/mmm-region.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2000 by Michael Abraham Shulman
 
 ;; Author: Michael Abraham Shulman <address@hidden>
-;; Version: $Id: mmm-region.el,v 1.11 2000/06/27 03:25:20 mas Exp $
+;; Version: $Id: mmm-region.el,v 1.12 2000/06/27 19:22:56 mas Exp $
 
 ;;{{{ GPL
 
@@ -58,7 +58,7 @@ i.e. whether text inserted at the marker should be inside the 
region."
 (defun* mmm-make-region
     (submode beg end &rest rest &key (front "") (back "")
              (beg-sticky t) (end-sticky t) face creation-hook
-             ;&allow-other-keys
+             &allow-other-keys
              )
   "Make a submode region from BEG to END of SUBMODE in FACE.
 FACE defaults to `mmm-default-submode-face'.  FRONT and BACK are
@@ -99,6 +99,7 @@ arguments are stored as properties of the overlay, 
un-keyword-ified."
               ))
     (save-excursion
       (goto-char (overlay-start ovl))
+      (mmm-set-current-submode submode)
       (mmm-set-local-variables submode)
       (mmm-run-submode-hook submode)
       (when creation-hook
@@ -106,7 +107,7 @@ arguments are stored as properties of the overlay, 
un-keyword-ified."
       (mmm-save-changed-local-variables ovl submode))
     (setq mmm-previous-submode submode
           mmm-previous-overlay ovl)
-    (mmm-update-current-submode)
+    (mmm-update-submode-region)
     ovl))
 
 ;;}}}
@@ -484,18 +485,6 @@ Looks up both global, buffer, and region saves."
                 (overlay-get ovl 'mmm-local-variables)
               mmm-region-saved-locals-for-dominant))))
 
-; (defun mmm-set-for-region (var value)
-;   "Set the variable VAR to VALUE in the current submode region only.
-; VAR must be in `mmm-save-local-variables' with a type of region."
-;   (unless (eq (cadr (assq var mmm-save-local-variables)) 'region)
-;     (error "Variable %s must have type `region' in 
`mmm-save-local-variables'."
-;            var))
-;   (make-local-variable var)     ; Sanity check
-;   (set var value)
-;   (setcar (cdr (assq var (overlay-get (mmm-overlay-at (point))
-;                                       'mmm-local-variables)))
-;           value))
-
 (defun mmm-save-changed-local-variables (ovl mode)
   "Save by-buffer and by-region variables for OVL and MODE.
 Called when we move to a new submode region, with OVL and MODE the
diff --git a/mmm-vars.el b/mmm-vars.el
index 94adaf3..59985d8 100644
--- a/mmm-vars.el
+++ b/mmm-vars.el
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2000 by Michael Abraham Shulman
 
 ;; Author: Michael Abraham Shulman <address@hidden>
-;; Version: $Id: mmm-vars.el,v 1.9 2000/06/27 02:47:45 mas Exp $
+;; Version: $Id: mmm-vars.el,v 1.10 2000/06/27 19:22:56 mas Exp $
 
 ;;{{{ GPL
 
@@ -340,8 +340,11 @@ an existing buffer."
   :group 'mmm
   :type 'hook)
 
-(defun mmm-run-major-mode-hook ()
-  (run-hooks 'mmm-major-mode-hook))
+(defun mmm-run-major-mode-hook (&optional buffer)
+  (save-excursion
+    (if (buffer-live-p buffer)
+        (set-buffer buffer))
+    (run-hooks 'mmm-major-mode-hook)))
 
 ;;}}}
 ;;{{{ MMM Global Mode
diff --git a/mmm.texinfo b/mmm.texinfo
index 572d07f..eb5fd4b 100644
--- a/mmm.texinfo
+++ b/mmm.texinfo
@@ -757,7 +757,7 @@ matching buffers, but supply no submode classes to be 
applied.
 @end defopt
 
 @menu
-* Major Mode Hook::                                Using MMM's Major Mode Hook
+* Major Mode Hook::             Using MMM's Major Mode Hook
 @end menu
 
 
@@ -773,52 +773,33 @@ know how MMM Global Mode is implemented, and perhaps use 
the same
 technique. In fact, MMM Mode exports a hook variable that you can use
 easily, without understanding any of the details---see below.
 
-If we only wanted to enable MMM Mode in buffers visiting files, it would
-be sufficient to use @code{find-file-hooks}, which is run whenever a
-file is visited. Earlier version on MMM Mode did exactly this, and the
-current version also uses this hook; see below.
-
 In order to enable itself in @emph{all} buffers, however, MMM Mode has
-to hook itself into all major modes. It would be nice if there were a
-variable such as @code{major-mode-hook} that all major mode functions
-ran at the end, along with their specific hook. Unfortunately, such a
-variable does not exist (as of FSF Emacs 20.5), so we have to do some
-hacking.
-
-Global Font Lock Mode from the standard Emacs distribution (@pxref{Font
-Lock, , , emacs, The Emacs Manual}) has a similar problem, and solves it
-by adding a function to @code{change-major-mode-hook}, which is run by
+to hook itself into all major modes.  Global Font Lock Mode from the
+standard Emacs distribution (@pxref{Font Lock, , , emacs, The Emacs
+Manual}) has a similar problem, and solves it by adding a function to
address@hidden, which is run by
 @code{kill-all-local-variables}, which is run in turn by all major mode
-functions at the @emph{beginning}. This function stores a list of which
-buffers need fontification. It then adds a different function to
+functions at the @emph{beginning}.  This function stores a list of which
+buffers need fontification.  It then adds a different function to
 @code{post-command-hook}, which checks if the current buffer needs
-fontification, and if so performs it. While this works, I think it is
-kind of kludgy.
-
-MMM Mode also adds a function to @code{change-major-mode-hook}, but what
-this function (@code{mmm-major-mode-change}) does is to look back
-through the call stack until it finds a mode function (recognizable
-because its name ends in @samp{-mode}) which is calling it, add
address@hidden to that function's name to get the conventional major mode
-hook name, then add a function to that hook. I think this is cleaner.
+fontification, and if so performs it.  MMM Global Mode uses the same
+technique.
 
 In the interests of generality, and for your use, the function that MMM
-Mode adds to the major mode hooks (@code{mmm-run-major-mode-hook}) is
-not specific to MMM Mode, but rather runs the hook variable
address@hidden, which by default contains a function which
-possibly turns MMM Mode on, depending on the value of
address@hidden Thus, to run another function in all major
-modes, all you need to do is add it to this hook. For example, the
-following line in an initialization file will turn on Auto Fill Mode
-(@pxref{Auto Fill, , , emacs, The Emacs Manual}) in all buffers:
+Mode runs in @code{post-command-hook} (@code{mmm-run-major-mode-hook})
+is not specific to MMM Mode, but rather runs the hook variable
address@hidden, which by default contains a function
+(@code{mmm-mode-on-maybe}) which possibly turns MMM Mode on, depending
+on the value of @code{mmm-global-mode}.  Thus, to run another function
+in all major modes, all you need to do is add it to this hook.  For
+example, the following line in an initialization file will turn on Auto
+Fill Mode (@pxref{Auto Fill, , , emacs, The Emacs Manual}) in all
+buffers:
 
 @lisp
 (add-hook 'mmm-major-mode-hook 'turn-on-auto-fill)
 @end lisp
 
-Perhaps in some future version of Emacs, a major mode hook will become
-standard, but until then, we've got to live with hacks.
-
 
 @node Submode Faces, Mode Line, Global Mode, Top
 @comment  node-name,  next,  previous,  up



reply via email to

[Prev in Thread] Current Thread [Next in Thread]