[Top][All Lists]

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

exposing the effective mode in a multi-mode

From: Tom Tromey
Subject: exposing the effective mode in a multi-mode
Date: Sun, 17 Sep 2017 17:13:39 -0600

I noticed that company couldn't do CSS completion in a <style> element
in mhtml-mode.

company-css understands how web-mode handles sub-modes, by calling a
web-mode function ("web-mode-language-at-pos") directly, but it doesn't
understand mhtml-mode.

So, I'd like to propose this patch for Emacs 26.  It adds a new function
to prog-mode.el so that multi-modes can expose the sub-mode at point.
It also changes mhtml-mode to set the new variable to make this work.


diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el
index f727e45..88d6e59 100644
--- a/lisp/progmodes/prog-mode.el
+++ b/lisp/progmodes/prog-mode.el
@@ -96,6 +96,24 @@ prog-indentation-context
    A typical use case are literate programming sources -- the
    function would successively return the previous code chunks.")
+(defvar-local prog-effective-mode-function nil
+  "When non-nil, provides the effective mode at point for embedded code chunks.
+When non-nil, this is a function that will be called by
+`prog-effective-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 prog-effective-mode ()
+  "When non-nil, 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 prog-effective-mode-function
+          (funcall 'prog-effective-mode-function))
+      major-mode))
 (defun prog-indent-sexp (&optional defun)
   "Indent the expression after point.
 When interactively called with prefix, indent the enclosing defun
diff --git a/lisp/textmodes/mhtml-mode.el b/lisp/textmodes/mhtml-mode.el
index b6cd157..2953355 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.
   ;; HTML 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)))
       (funcall mode)
       ;; Make sure font lock is all set up.
@@ -348,6 +350,14 @@ mhtml--flyspell-check-word
+(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)))
+    (if submode
+        (mhtml--submode-mode submode)
+      'html-mode)))
 (define-derived-mode mhtml-mode html-mode
   '((sgml-xml-mode "XHTML+" "HTML+") (:eval (mhtml--submode-lighter)))
@@ -365,6 +375,7 @@ mhtml-mode
   (setq-local font-lock-extend-region-functions
+  (setq-local prog-effective-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]