I'm using the tco.el
package, which provides tail-call optimisation for functions in elisp that call themselves in tail-position. The way such functions are identified is by using defun-tco
instead of defun
. But this means tail-call optimised functions no longer are styled like functions in terms of font-locking. How do I specify that defun-tco
should be styled like defun
?
Add a comment
|
1 Answer
You need to set font-lock keywords, e.g.,
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\(defun-tco\\)\\_>\\s *\\(\\(?:\\sw\\|\\s_\\)+\\)?"
(1 font-lock-keyword-face nil t)
(2 font-lock-function-name-face nil t))))
the above is adapted from https://github.com/emacs-mirror/emacs/blob/8fe21b03/lisp/emacs-lisp/generator.el#L796. I suggest you to write an issue or pull request to the tco.el project, so you don't have to put the above to your own init file.
-
Thanks for this - I figured it was
font-lock-add-keywords
, but I couldn't find wheredefun
was configured, so wasn't sure what the content should look like. Unfortunately, while the above correctly styles thedefun-tco
keyword itself and the following function name, it does not change the description's font-locking. I'm not sure wheredefun
's description font-locking is described. Commented Jan 12, 2019 at 17:07 -
1@emacsomancer I don't think the color of
defun
is from font-lock keywords (i.e., searching via regexp, so it's slow). The core syntax highlighting is from syntactic parsing, see (elisp) Font Lock Mode. I guess you mean docstring by "description",defun-tco
doesn't support docstring at all, if it does, it will probably include(declare (doc-string 3))
then you'll get the highlight out-of-box. Commented Jan 12, 2019 at 19:02 -
Yes, sorry I mean the docstring. It looks like that is
font-lock-doc-face
is applied infont-lock.el
by the functionfont-lock-compile-keywords
. Unfortunately, it's not exactly clear how to hack that to get it to apply afterdefun-tco
as well asdefun
. I wonder if there's a different way to approach this - to redefine how font-locking applies todefun
so that it also applies todefun-tco
, so that other font-locking could essential just be 'inherited'? [Edit: Ah, I see what you mean - docstring support simply isn't defined fordefun-tco
.] Commented Jan 12, 2019 at 19:40 -
1@emacsomancer you only need to worry about
font-lock-function-name-face
.defun-tco
is already colorized because it is defined as a macro. And ifdefun-tco
supports docstring, the docstring will also be colorized because of(declare (doc-string ...))
, the author will add it to follow the convention of defining macro. Commented Jan 12, 2019 at 20:01