unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* exposing the effective mode in a multi-mode
@ 2017-09-17 23:13 Tom Tromey
  2017-09-18  9:01 ` Dmitry Gutov
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Tom Tromey @ 2017-09-17 23:13 UTC (permalink / raw)
  To: Emacs discussions

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.

Tom

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.
   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,14 @@ 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)))
+    (if submode
+        (mhtml--submode-mode submode)
+      'html-mode)))
+
 ;;;###autoload
 (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
               '(mhtml--extend-font-lock-region
                 font-lock-extend-region-multiline))
+  (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.



^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-10-08 21:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2017-09-19 12:23 ` Stefan Monnier
2017-10-08 21:20   ` Tom Tromey

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).