From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tom Tromey Newsgroups: gmane.emacs.devel Subject: Re: exposing the effective mode in a multi-mode Date: Tue, 19 Sep 2017 22:21:13 -0600 Message-ID: <87h8vy9f0m.fsf@tromey.com> References: <874ls0c40s.fsf@tromey.com> <20170918093053.GB15839@ACM> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1505881347 32392 195.159.176.226 (20 Sep 2017 04:22:27 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 20 Sep 2017 04:22:27 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Tom Tromey , Emacs discussions To: Alan Mackenzie Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 20 06:22:21 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duWWi-000866-21 for ged-emacs-devel@m.gmane.org; Wed, 20 Sep 2017 06:22:20 +0200 Original-Received: from localhost ([::1]:46609 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duWWp-0002GV-9l for ged-emacs-devel@m.gmane.org; Wed, 20 Sep 2017 00:22:27 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46917) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duWVr-0001zl-4d for emacs-devel@gnu.org; Wed, 20 Sep 2017 00:21:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1duWVn-000573-TH for emacs-devel@gnu.org; Wed, 20 Sep 2017 00:21:27 -0400 Original-Received: from gproxy8-pub.mail.unifiedlayer.com ([67.222.33.93]:45532) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1duWVn-00052E-LA for emacs-devel@gnu.org; Wed, 20 Sep 2017 00:21:23 -0400 Original-Received: from cmgw2 (unknown [10.0.90.83]) by gproxy8.mail.unifiedlayer.com (Postfix) with ESMTP id A264F1AB0EA for ; Tue, 19 Sep 2017 22:21:21 -0600 (MDT) Original-Received: from box522.bluehost.com ([74.220.219.122]) by cmgw2 with id BgMF1w01y2f2jeq01gMJDR; Tue, 19 Sep 2017 22:21:21 -0600 X-Authority-Analysis: v=2.2 cv=dZfw5Tfe c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=2JCJgTwv5E4A:10 a=6rBk-4IEp4V4yMESHJYA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date: References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=uqxf5gS3VDkgrfT+6t20jeRQ7/KRRMx8kmMUwTjNirY=; b=gmRFUgrXKMericjfUUARzJnS2i ZO8rXEfIivTvZQBR1vCpaZdWRSr92sKZeUHdR/vtEY/ugIzYwyKnQilt1OjBRA+pUxfYIJYcQ8wHJ OVN61kwvvhkmgb/UGQGkg2ryL; Original-Received: from 75-166-76-94.hlrn.qwest.net ([75.166.76.94]:34662 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1duWVf-00445k-HM; Tue, 19 Sep 2017 22:21:15 -0600 X-Attribution: Tom In-Reply-To: <20170918093053.GB15839@ACM> (Alan Mackenzie's message of "Mon, 18 Sep 2017 09:30:53 +0000") X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box522.bluehost.com X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.76.94 X-Exim-ID: 1duWVf-00445k-HM X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-76-94.hlrn.qwest.net (bapiya) [75.166.76.94]:34662 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 67.222.33.93 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:218564 Archived-At: >>>>> "Alan" == Alan Mackenzie 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.