emacs-devel
[Top][All Lists]
Advanced

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

Re: exposing the effective mode in a multi-mode


From: Tom Tromey
Subject: Re: exposing the effective mode in a multi-mode
Date: Tue, 19 Sep 2017 22:21:13 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

>>>>> "Alan" == Alan Mackenzie <address@hidden> writes:

Alan> The facility will surely be needed by modes which aren't derived from
Alan> prog-mode.

That's a good point, and I've updated the patch accordingly.  It's
appended; let me know what you think.

Alan> Is the current implementation going to be applicable to all
Alan> these various ways of doing multi-mode?  Would it not be better to have
Alan> this as a hook function (in global namespace), where the pertinent
Alan> multi-mode could set the hook to its own function?

I don't really understand this critique, because that's what the patch
did.  Is there something I should change?

Tom

diff --git a/lisp/subr.el b/lisp/subr.el
index 79ae1f4..5c63f4f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1906,6 +1906,24 @@ derived-mode-p
   "Non-nil if the current major mode is derived from one of MODES.
 Uses the `derived-mode-parent' property of the symbol to trace backwards."
   (apply #'provided-mode-derived-p major-mode modes))
+
+(defvar-local effective-major-mode-function nil
+  "When non-nil, provides the effective mode at point for embedded chunks.
+When non-nil, this is a function that will be called by
+`effective-major-mode' to find the effective major mode at point.
+This function should return a symbol naming a major mode, e.g. `css-mode'.
+It may return nil to mean the \"outer\" major mode.")
+
+(defun effective-major-mode ()
+  "Returns the effective mode at point.
+
+There are languages where part of the code is actually written in
+a sub language, e.g., a Yacc/Bison or ANTLR grammar also consists
+of plain C code.  This function returns the effective value of
+`major-mode' at point."
+  (or (if effective-major-mode-function
+          (funcall effective-major-mode-function))
+      major-mode))
 
 ;;;; Minor modes.
 
diff --git a/lisp/textmodes/mhtml-mode.el b/lisp/textmodes/mhtml-mode.el
index b6cd157..326cee4 100644
--- a/lisp/textmodes/mhtml-mode.el
+++ b/lisp/textmodes/mhtml-mode.el
@@ -56,7 +56,9 @@ mhtml-tag-relative-indent
   :version "26.1")
 
 (cl-defstruct mhtml--submode
-  ;; Name of this submode.
+  ;; The submode's symbol, like 'css-mode.
+  mode
+  ;; Name of this submode, a string.  Used for the mode-line lighter.
   name
   ;; HTML end tag.
   end-tag
@@ -86,7 +88,7 @@ mhtml--construct-submode
   "A wrapper for make-mhtml--submode that computes the buffer-local variables."
   (let ((captured-locals nil)
         (crucial-captured-locals nil)
-        (submode (apply #'make-mhtml--submode args)))
+        (submode (apply #'make-mhtml--submode :mode mode args)))
     (with-temp-buffer
       (funcall mode)
       ;; Make sure font lock is all set up.
@@ -348,6 +350,13 @@ mhtml--flyspell-check-word
         (flyspell-generic-progmode-verify)
       t)))
 
+(defun mhtml--effective-mode ()
+  "Return the current mode (a symbol) at point.
+If point is not in a submode section, returns 'html-mode."
+  (let ((submode (get-text-property (point) 'mhtml-submode)))
+    (when submode
+      (mhtml--submode-mode submode))))
+
 ;;;###autoload
 (define-derived-mode mhtml-mode html-mode
   '((sgml-xml-mode "XHTML+" "HTML+") (:eval (mhtml--submode-lighter)))
@@ -365,6 +374,7 @@ mhtml-mode
   (setq-local font-lock-extend-region-functions
               '(mhtml--extend-font-lock-region
                 font-lock-extend-region-multiline))
+  (setq-local effective-major-mode-function 'mhtml--effective-mode)
 
   ;; Attach this to both pre- and post- hooks just in case it ever
   ;; changes a key binding that might be accessed from the menu bar.



reply via email to

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