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.

drobnbobn
  • 575
  • 4
  • 15
  • Do you want this in all modes or only in a specific mode? – Gilles 'SO- stop being evil' Mar 06 '16 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 06 '16 at 15:22
  • Here is an example by Lindydancer to highlight anything in back-quote and single-quote within comments in `c-mode`. http://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 06 '16 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 06 '16 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 06 '16 at 17:50

1 Answers1

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)
lawlist
  • 18,826
  • 5
  • 37
  • 118