From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: exposing the effective mode in a multi-mode Date: Tue, 19 Sep 2017 08:23:50 -0400 Message-ID: References: <874ls0c40s.fsf@tromey.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1505823932 16810 195.159.176.226 (19 Sep 2017 12:25:32 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 19 Sep 2017 12:25:32 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Sep 19 14:25:27 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 1duHae-00044M-QT for ged-emacs-devel@m.gmane.org; Tue, 19 Sep 2017 14:25:25 +0200 Original-Received: from localhost ([::1]:42547 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duHal-0001lb-Qb for ged-emacs-devel@m.gmane.org; Tue, 19 Sep 2017 08:25:31 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duHZO-0000y1-Fu for emacs-devel@gnu.org; Tue, 19 Sep 2017 08:24:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1duHZK-0001Ou-Gu for emacs-devel@gnu.org; Tue, 19 Sep 2017 08:24:06 -0400 Original-Received: from [195.159.176.226] (port=51495 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1duHZK-0001Mu-9h for emacs-devel@gnu.org; Tue, 19 Sep 2017 08:24:02 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1duHZ7-0008Et-JS for emacs-devel@gnu.org; Tue, 19 Sep 2017 14:23:49 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 58 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:LWcxlFWLnQhBBVbWcFwxbupql4s= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 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:218512 Archived-At: > +(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)) As usual, I recommend to avoid the "when non-nil, ..." for function variables (e.g. because it prevents use of add-function on it, and because removing the nil case simplifies the code, and because removing the nil case makes sure that the default behavior is not special). [ Also the doc shouldn't talk about who calls it, but what it does. ] (defvar-local prog-effective-mode-function #'ignore "Function to get the effective mode at point for embedded code chunks. Called with no argument, returns 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 () "Return 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 (funcall 'prog-effective-mode-function) major-mode)) Tho we can simplify it yet further: (defvar-local prog-effective-mode-function (lambda () major-mode) "Function to get the effective mode at point for embedded code chunks. Called with no argument, returns the effective major mode at point. This function should return a symbol naming a major mode, e.g. `css-mode'.") (defun prog-effective-mode () "Return 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." (funcall 'prog-effective-mode-function)) at which point we may as well drop `prog-effective-mode`. Stefan