[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: exposing the effective mode in a multi-mode
From: |
Stefan Monnier |
Subject: |
Re: exposing the effective mode in a multi-mode |
Date: |
Tue, 19 Sep 2017 08:23:50 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) |
> +(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