2

Because I often have to preserve this from getting overwritten in closures, I like to do: var self = this; at the top of my constructors. I'd like to subsequently highlight self exactly how and when this is currently highlighted. However after studying the emacs documentation, websites, and examples here, and adapting the examples, it's not working. I'm evaluating this expression:

(font-lock-add-keywords 'javascript-mode
        '(("self" . 'font-lock-keyword-face)))

both in my .emacs file and in the javascript buffer, and also toggling font-lock-mode to force a refresh. I've tried variations with the confusing "\\<self\\>" syntax but I'm clearly throwing darts because that also fails. I see many variations of font-lock-add-keywords but being only an Emacs power user and not a developer, it seems byzantine.

As context, this is GNU Emacs 24.5.1 running as a native Mac OS X 10.11.5 App, not in terminal.

BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • emacs.stackexchange.com may be a better place for questions like this, although since your question is specific to using it as an IDE, SO may be OK as well. – Barmar Apr 01 '16 at 02:05
  • Using self is almost never necessary these days. Arrow functions will usually get the job doing done. You may want to take this opportunity to experiment with a non-self programming style. –  Apr 01 '16 at 07:15
  • Very selfless of you to point that out. – BaseZen Apr 01 '16 at 07:38

1 Answers1

3

Interesting how posting here improves my own troubleshooting. :-)

JavaScript mode is actually js-mode, not javascript-mode. I don't have the patience to figure out why there's two, probably historical. So this worked, fixing two niceties (word boundary, and correct face) along the way:

(add-hook 'js-mode-hook (lambda() 
          (font-lock-add-keywords 'js-mode '(("\\<self\\>" . 'font-lock-constant-face)))))
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • `javascript-mode` is an alias for `js-mode`, but other names do have to be correct (`js-mode-hook`, etc), so it does cause confusion from time to time... – phils Apr 01 '16 at 03:03