From: Tom Tromey <tom@tromey.com>
To: Emacs discussions <emacs-devel@gnu.org>
Subject: exposing the effective mode in a multi-mode
Date: Sun, 17 Sep 2017 17:13:39 -0600 [thread overview]
Message-ID: <874ls0c40s.fsf@tromey.com> (raw)
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.
next reply other threads:[~2017-09-17 23:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-17 23:13 Tom Tromey [this message]
2017-09-18 9:01 ` exposing the effective mode in a multi-mode 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
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=874ls0c40s.fsf@tromey.com \
--to=tom@tromey.com \
--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.