1

I would like to have my dollar sign yellow coloured.

So after searching around, I learnt that the Emacs regex's are different from default regexs. After a while, I got this worked:

(defface font-lock-sigil-face '((t (:foreground "#BD9800"))) "Face to display sigils in.") (font-lock-add-keywords 'php-mode '(("\\(\\$\\)[_a-zA-Z]" 1 'font-lock-sigil-face)))

So the dollar sign for $variable, gets a yellow color.

But I would like to make the numbers red.

So for example

var $variable = 0123; (this gets red).

But not the strings would be red.

var $variable = "0123"; (So the regex will not makes this red).

I tried many ways, but I don't understand the backslashes in '(("\(\$\)[_a-zA-Z] clearly enough. I would really appreciate it if anyone could explain this?

Thanks in advance.

1

"\\(\\$\\)[_a-zA-Z]" <-- anything that begins by a dollar sign and followed by and underscore character or a (from a to z: lowercase letter or uppercase letter) or underscore and lowercaser/uppercase in any order.

I tried many ways, but I don't understand the backslashes in '(("(\$)[_a-zA-Z] clearly enough

\\ <-- do not interpret the next following character. (be careful, it's exactly 2 backslashes, not one)

() <-- memorize the matched group

1 <-- apply the font lock to the first matched group, in our case the dollar sign.

To finish, you should use an emacs hook to tell emacs when to apply your font rules, the doc.

Finally, in your case, you probably want a regex to catch a dollar sign followed by a word, a space, an equal sign, and a digit, something close to this (just an example):

(add-hook 'php-mode-hook
          (lambda ()
            (font-lock-add-keywords nil
                                    '(("\\(\\$\\)[[:word:]]+ = \\([[:digit:]]+\\)"
                                       (1 font-lock-type-face t)
                                       (2 font-lock-warning-face t))))))

I hope I did not miss something, and I hope it will help.

  • Your solution works. And I learnt a lot from your post, thanks for that. It's really appreciated... – ReneFroger Apr 5 '15 at 17:56
2

How about highlight-numbers-mode for the number highlighting portion of your question:

https://github.com/Fanael/highlight-numbers
  • Never knew that mode. Just what I was looking for. Thanks for that, I like your help. – ReneFroger Apr 5 '15 at 17:57

Your Answer

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

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