* symbols, operators highlighting (+, -, =, <, >, etc)
@ 2009-05-02 6:43 ex
2009-05-02 10:58 ` Anselm Helbig
0 siblings, 1 reply; 2+ messages in thread
From: ex @ 2009-05-02 6:43 UTC (permalink / raw)
To: help-gnu-emacs
Hi there!
I'm pretty new to emacs and I was wondering how to add highlighting to
symbols and operators in my code
(I' ve been really used to that in other editors), but I couldn't find
anything in the web or emacs-wiki
I could figure out the next kludge in my emacs file:
;;------------------------------------------------------------------------------
;; Highlight operators
(font-lock-add-keywords 'python-mode
'(("\\." . font-lock-warning-face)
("else:" . font-lock-keyword-face)
("\\+" . font-lock-warning-face)
("\\-" . font-lock-warning-face)
("\=" . font-lock-warning-face)
(":" . font-lock-warning-face)
("\\[" . font-lock-warning-face)
("\\]" . font-lock-warning-face)
("," . font-lock-warning-face)
("!" . font-lock-warning-face)
("(" . font-lock-warning-face)
(")" . font-lock-warning-face)
("\<" . font-lock-warning-face)
("\>" . font-lock-warning-face)))
the problem is that this only works for python, and I don't want to
copy/paste this to other modes, there must be a more elegant
solution...
the other problem is that I'm abusing the warning face (because I
don't know how to create a face)
Any help or advice?
Many thanks.
Laurens
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: symbols, operators highlighting (+, -, =, <, >, etc)
2009-05-02 6:43 symbols, operators highlighting (+, -, =, <, >, etc) ex
@ 2009-05-02 10:58 ` Anselm Helbig
0 siblings, 0 replies; 2+ messages in thread
From: Anselm Helbig @ 2009-05-02 10:58 UTC (permalink / raw)
To: help-gnu-emacs
Hi Laurens!
> ;;------------------------------------------------------------------------------
> ;; Highlight operators
> (font-lock-add-keywords 'python-mode
> '(("\\." . font-lock-warning-face)
> ("else:" . font-lock-keyword-face)
> ("\\+" . font-lock-warning-face)
> ("\\-" . font-lock-warning-face)
> ("\=" . font-lock-warning-face)
> (":" . font-lock-warning-face)
> ("\\[" . font-lock-warning-face)
> ("\\]" . font-lock-warning-face)
> ("," . font-lock-warning-face)
> ("!" . font-lock-warning-face)
> ("(" . font-lock-warning-face)
> (")" . font-lock-warning-face)
> ("\<" . font-lock-warning-face)
> ("\>" . font-lock-warning-face)))
Well, that doesn't look too bad. You can condense this code a bit:
(font-lock-add-keywords
'python-mode
(cons
(regexp-opt '("." "else:" "+" "-" "=" ":" "[" "]" "," "!" "(" ")" "<" ">"))
'font-lock-warning-face))
> the problem is that this only works for python, and I don't want to
> copy/paste this to other modes, there must be a more elegant
> solution...
You can not copy this for other modes, anyway, as other modes have
different keywords. But of course, it can be done:
font-lock-add-keywords can take nil as the first argument which causes
it to add the keywords for the current buffer. We only need to make
sure it runs every time you open a file, this can be done with the
find-file-hook:
(defun my-font-lock-keywords-hook ()
(font-lock-add-keywords
nil
(cons
(regexp-opt '("." "else:" "+" "-" "=" ":" "[" "]" "," "!" "(" ")" "<" ">"))
'font-lock-warning-face)))
(add-hook 'find-file-hook 'my-font-lock-keywords-hook)
> the other problem is that I'm abusing the warning face (because I
> don't know how to create a face)
Using something like font-lock-warning-face has the benefit that it
works with different color themes. You can define your own faces with
"defface", look it up in the elisp manual: (info "(elisp)Defining Faces")
Happy hacking!
--
Anselm Helbig
mailto:anselm.helbig+news2009@googlemail.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-05-02 10:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-02 6:43 symbols, operators highlighting (+, -, =, <, >, etc) ex
2009-05-02 10:58 ` Anselm Helbig
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.