unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: exposing the effective mode in a multi-mode
Date: Tue, 19 Sep 2017 08:23:50 -0400	[thread overview]
Message-ID: <jwvshfiann0.fsf-monnier+gmane.emacs.devel@gnu.org> (raw)
In-Reply-To: 874ls0c40s.fsf@tromey.com

> +(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




  parent reply	other threads:[~2017-09-19 12:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvshfiann0.fsf-monnier+gmane.emacs.devel@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --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 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).