all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Alan Mackenzie <acm@muc.de>
Cc: Tom Tromey <tom@tromey.com>, Emacs discussions <emacs-devel@gnu.org>
Subject: Re: exposing the effective mode in a multi-mode
Date: Tue, 19 Sep 2017 22:21:13 -0600	[thread overview]
Message-ID: <87h8vy9f0m.fsf@tromey.com> (raw)
In-Reply-To: <20170918093053.GB15839@ACM> (Alan Mackenzie's message of "Mon, 18 Sep 2017 09:30:53 +0000")

>>>>> "Alan" == Alan Mackenzie <acm@muc.de> 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))
 \f
 ;;;; 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	other threads:[~2017-09-20  4:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-17 23:13 exposing the effective mode in a multi-mode Tom Tromey
2017-09-18  9:01 ` Dmitry Gutov
2017-09-20  4:18   ` Tom Tromey
2017-09-21 14:19     ` Dmitry Gutov
2017-09-18  9:30 ` Alan Mackenzie
2017-09-20  4:21   ` Tom Tromey [this message]
2017-09-19 12:23 ` Stefan Monnier
2017-10-08 21:20   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h8vy9f0m.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=acm@muc.de \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.