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?


You need to set font-lock keywords, e.g.,

 '(("(\\(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.

| improve this answer | |
  • Thanks for this - I figured it was font-lock-add-keywords, but I couldn't find where defun was configured, so wasn't sure what the content should look like. Unfortunately, while the above correctly styles the defun-tco keyword itself and the following function name, it does not change the description's font-locking. I'm not sure where defun's description font-locking is described. – emacsomancer Jan 12 '19 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. – xuchunyang Jan 12 '19 at 19:02
  • Yes, sorry I mean the docstring. It looks like that is font-lock-doc-face is applied in font-lock.el by the function font-lock-compile-keywords. Unfortunately, it's not exactly clear how to hack that to get it to apply after defun-tco as well as defun. I wonder if there's a different way to approach this - to redefine how font-locking applies to defun so that it also applies to defun-tco, so that other font-locking could essential just be 'inherited'? [Edit: Ah, I see what you mean - docstring support simply isn't defined for defun-tco.] – emacsomancer Jan 12 '19 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 if defun-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. – xuchunyang Jan 12 '19 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.