3

Since back-ticks are often used for literals, is there a way I can highlight these in code comments?

So this:

# This is a comment, this is a literal: `4 + 4`.
# let's make things interesting (`literal_a`,`literal_b`).

Can display like this:

enter image description here

2
  • 1
    It depends upon the method used by the major-mode for highlighting comments, e.g., emacs-lisp-mode has a nice built-in system and it is just a matter of adding a few lines of code here and there. cc-mode can use a different approach -- see emacs.stackexchange.com/a/20132/2287 and emacs.stackexchange.com/a/20228/2287
    – lawlist
    Jan 30, 2021 at 6:00
  • Assume this is a format that doesn't do anything special with comments (Bash/Makefiles/Python)
    – ideasman42
    Jan 30, 2021 at 7:11

1 Answer 1

3

Use font-lock-add-keywords for to add extra fontification. See the documentation of font-lock-keywords. You especially need the OVERRIDE flag to override the normal fontification of the comment.

Use a function as MATCHER. That function should search for the next comment or make sure that you are in one. Afterwards it should search for your literals.

The following lisp code demonstrates the procedure. It defines a minor mode clit-mode that highlights the literals more-or-less as you specified them. (Currently, it does not highlight `4 + 4`. But I am confident that you can fiddle around with the code until it does exactly what you want.)

Note that this code assumes that the character syntax is used for delimiting comments. It works on the basis of syntax-ppss.

(defcustom clit-regexp "`\\_<[^[:space:]]+\\_>`"
  "Regular expression matching literals in comments."
  :type 'regexp
  :group 'comment)

(defcustom clit-face 'hi-red-b
  "Face for matching literals in comments."
  :type 'face
  :group 'comment)

(defun clit-matcher (limit)
  "Search for `literal` in comments up to LIMIT."
  (let (found)
    (while (and
        (null found)
        (or
         (ppss-comment-depth (syntax-ppss))
         (comment-search-forward limit 1)))
      (unless (setq found
                    (re-search-forward clit-regexp
                      (min (line-end-position) limit)
                      t))
         (forward-line)))
      found))

(defvar clit-keywords
  '((clit-matcher
     (0 clit-face t)))
  "Font lock keywords for `clit-mode'.")

(define-minor-mode clit-mode
  "Minor mode for highlighting literals in comments."
  :lighter " `"
  (if clit-mode
      (font-lock-add-keywords nil clit-keywords t)
    (font-lock-remove-keywords nil clit-keywords))
  (font-lock-flush)
  (font-lock-ensure))
1
  • Thanks for the answer, it works well. Although for each instance I want to do this kind of highlighting I'd rather not have this overhead of writing custom functions, so I've generalized this into a mode minor mode with multiple configurable regex/context/font combinations: gitlab.com/ideasman42/emacs-hl-prog-extra
    – ideasman42
    Apr 9, 2021 at 0:21

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.