1

Most of what I do with Emacs is in perl, and for that cperl-mode is mostly excellent; the one major annoyance is that a simple my $whatever introduction is given the property font-lock-keyword-face, which nixes the advantage of having keywords pop. I'm new to elisp, and so have been trying to play around with adding or removing keywords from font-lock. While both EmacsWiki and GNU give the syntax, the regex is driving me crazy. I got the following not to whine in my init:

(font-lock-remove-keywords 'cperl-mode '(("\\<\\(my\\)")))

And it does nothing. The \\<\\(my\\) bit works in scratch with re-builder so I think I have a syntax issue here. How do I add and remove keywords?

Drew
  • 29,895
  • 7
  • 74
  • 104
Amory
  • 758
  • 2
  • 19
  • 31
  • 1
    Fortunately comments can't receive demerits, so here goes: You could define the offending keyword as your regular base font -- e.g., black foreground with white background. I don't modify the source of Emacs itself, because I'm frequently building new versions from the Emacs Trunk. What Lindydancer is suggesting makes since, but I haven't researched that before so I don't have a solution based thereon. – lawlist Aug 26 '13 at 04:23
  • 1
    You may also want to take a look a the source `.../lisp/progmodes/cperl-mode.el` to see how the offending keyword is defined. `my` is listed numerous times beginning at line 5129 of Emacs Trunk. – lawlist Aug 26 '13 at 04:27

1 Answers1

2

The font-lock-remove-keywords function is designed to remove an existing font-lock rule, which, in font-lock jargon is a font-lock keyword.

If you apply it to something that is not an exact match of an existing font-lock rule, then it does nothing.

It is not designed to remove language keywords.

They best way to do this is to look at the variable font-lock-keywords and remove the rule that you don't like using font-lock-remove-keywords and then add a modified version of the rule using font-lock-add-keywords.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • Do you mean `font-lock-remove-keywords` instead of `remove-font-lock-keywords`? – xuhdev Oct 25 '16 at 08:51
  • @xuhdev: Yes, of course I do -- I've updated the answer. (Gee -- I even wrote the **** function, once upon a time, and I can't even remember its name. I guess it's time to roll me into a nursing home...) – Lindydancer Oct 25 '16 at 19:04