1

I want to modify the highlighting of my JS files, so that every line starting with LOG gets shown in a different color.1

I wrote the following

(require 'font-core)

(font-lock-mode 1)

(defface mg-js-pseudo-face '((t (:background "White"))) "..." :group 'faces)

(defvar mg-js-pseudo-regex "^[ \t]*\\(LOG .*\\)")

(defun mg-js-pseudo-hook ()
  (font-lock-add-keywords
   nil
   `(
     (,mg-js-pseudo-regex 1 'mg-js-pseudo-face t)) ;;; <----- here
))

(add-hook 'js-mode-hook 'mg-js-pseudo-hook)

It works well when highlighting the whole line (using 0 instead of 1 in the marked line). It does nothing at all, when used as is. Why?

1It's invalid JS, I know. It gets preprocessed by the server to console.log with some added information.

  • I tried this in js2-mode and it seemed to work with 1 (it highlighted the whole line, because the regexp matches the rest of the line). Put the cursor over the text you think should be highlighted, then use C-u C-x = (what-cursor-position) to find out what faces are there. That often helps me find which other font-lock rule is setting the face. When I tried it with your regexp, it was telling me ms-js-pseudo-face, js2-external-variable, and js2-warning were all fighting to be there. (Do you have a :background for one of those other faces?) – amitp Aug 26 '16 at 15:59

Your Answer

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

Browse other questions tagged or ask your own question.