3

How to make colored some part of comment? Let comment be ;; This is James Bond diary. And I want to make "James Bond" in green.

There will be only a few such strings and no places where it will be repeated, so it is OK to define exact symbols-insertion in dot-emacs file.

5
  • Do you want this in all modes or only in a specific mode? Mar 6, 2016 at 15:17
  • @Gilles in fact I just want to put a small ASCII art (as a commented region) at the beginning of my .emacs file, but want it's symbols be colored in green.
    – drobnbobn
    Mar 6, 2016 at 15:22
  • Here is an example by Lindydancer to highlight anything in back-quote and single-quote within comments in c-mode. emacs.stackexchange.com/a/20228/2287 You could surely adapt that example to a specific word or you could define your own way to recognize what needs a special color.
    – lawlist
    Mar 6, 2016 at 15:48
  • @lawlist should I use emacs-lisp-mode-common-hook instead of c-mode-common-hook and "James Bond" instead of ``\([a-zA-Z0-9_-]+\)'`? doesn't work
    – drobnbobn
    Mar 6, 2016 at 16:35
  • 1
    It is emacs-lisp-mode-hook instead of emacs-lisp-mode-common-hook -- the latter does not exist. I've posted a working answer that you can further modify to suit your needs.
    – lawlist
    Mar 6, 2016 at 17:50

1 Answer 1

3

This answer is just a slight modification of Lindydancer's answer in the following link: https://emacs.stackexchange.com/a/20228/2287

It has been modified to highlight a double-quoted name -- e.g., ;; Hello "James Bond". The major-mode is emacs-lisp-mode.

(defface drobnbobn-face
  '((t (:background "black" :foreground "red")))
"Face for `drobnbobn-face'.")
(defvar drobnbobn-face 'drobnbobn-face)

(defun my-highlight-symbol-font-lock-keywords-matcher (limit)
  "Search for doubled-quoted things in comments.
Match group 0 is the entire construct, 1 the symbol."
  (let (res)
    (while
        (and (setq res (re-search-forward "\"\\([a-zA-Z0-9\s_-]+\\)\"" limit t))
             (not (nth 4 (syntax-ppss)))))  ; Continue, unless in a comment
    res))

(defun my-highlight-symbol-activate ()
  "Highlight double-quoted things in comments."
  (font-lock-add-keywords nil
                          '((my-highlight-symbol-font-lock-keywords-matcher
                             (1 drobnbobn-face prepend)))))

(add-hook 'emacs-lisp-mode-hook #'my-highlight-symbol-activate)
2
  • and how to set a certain color (for example, green) instead of font-lock-constant-face?
    – drobnbobn
    Mar 6, 2016 at 17:58
  • I've added your very own face. :) Change the colors and/or other attributes as you see fit.
    – lawlist
    Mar 6, 2016 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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