I defined a new face in Emacs, but the coloring does not take effect. Here is the face and mode definition in ~/.emacs:
(defface sml-highlight-operator-face
'((t (:foreground "red")))
"SML operator highlighting"
:group 'basic-faces)
(defvar sml-font-lock-keywords
((,(regexp-opt '("fun" "fn" "let" "val" "datatype" "type" "case" "of" "end" "structure" "struct" "signature" "sig"))
(0 font-lock-keyword-face))
("[][=|><-+;,{}():]" (0 sml-highlight-operator-face))))
;;;###autoload
(define-derived-mode sml-mode prog-mode "SML"
"SML major mode."
(set (make-local-variable 'comment-start) "(* ")
(set (make-local-variable 'comment-end) " *)")
(set (make-local-variable 'font-lock-defaults)
'(sml-font-lock-keywords)))
However, when I use font-lock-builtin-face instead of sml-highlight-operator-face those characters are highlighted (albeit with a color I don't want). What am I doing incorrectly?
The element (0 sml-highlight-operator-face) in your font-lock-keywords does not say "use face sml-highlight-operator-face for sub-match 0" but "use the result of evaluating the expression sml-highlight-operator-face as the face to put on sub-match 0".
IOW, you need to use (0 'sml-highlight-operator-face).
BTW, the convention nowadays is to not use a -face suffix for faces (tho such a suffix is still used of course for variables holding faces), tho we haven't bothered to rename the font-lock-foo-face faces to font-lock-foo yet (even though it would very much help the confusion you're seeing where a lot of font-lock rules say things like (0 font-lock-foo-face) and people think it refers to the font-lock-foo-face face whereas it refers to the font-lock-foo-face variable (whose value holds the font-lock-foo-face face).
Related
In emacs lisp mode, I want to highlight all function calls one color, and all quoted symbols another color. For example, in (foo 1 2 'bar), foo should be highlighted color 1 and 'bar should be highlighted color 2.
I was able to do this with the code below, however, it is also highlighting the first symbol in a list when the list is quoted. For example, in '(nil a b c), nil should not be highlighted as a function call, and all items in that list should be highlighted as quoted symbols (color 2).
(defface font-lock-func-face
'((nil (:foreground "#6fc2ef"))
(t (:bold t :italic t)))
"Font Lock mode face used for function calls."
:group 'font-lock-highlighting-faces)
(defface font-lock-quoted-face
'((nil (:foreground "#e1a3ee"))
(t (:bold t :italic t)))
"Font Lock mode face used for function calls."
:group 'font-lock-highlighting-faces)
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1 'font-lock-func-face)))
(font-lock-add-keywords
'emacs-lisp-mode
'(("'[-a-zA-Z_][-a-zA-Z0-9_]*\\>" 0 'font-lock-quoted-face)))
There's also an image of what I'm looking at here: http://imgur.com/Iluku05
First part of the answer: The package lisp-extra-font-lock highlights, among else, quoted expressions.
It also highlights:
variables in the parameter lists of functions and variables bound by let, pcase etc. It distinguish between normal local variables and global variables defined by a defvar by using different colors.
Back-quoted expressions (and un-highlights all ,- and ,#-escaped subexpressions)
For example:
Second part of the answer:
If you use the lisp-extra-font-lock package, your rule for highlighting functions work as you posted it, if you add it as the last rule, after you have enabled the package. That way it will not overwrite any other highlight:
(font-lock-add-keywords
'emacs-lisp-mode
'(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
1 'font-lock-func-face))
'append) ;; <-- Add after all other rules
In a new and hopefully very simple emacs mode I would like every instance of 'a' to be #0000FF and 'b' to be #DF0101. Thus far I haven't found a way to do this despite lots of googling. Ideally it would happen in realtime, as you typed.
http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/emacs/Highlight-Interactively.html
http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/emacs/Font-Lock.html#Font-Lock
I use this for my Navi minor mode:
;; Adding Navi to the syntax highlighting of emacs mode.
First to make new faces, with their colors, in my case green for the success font for letter "t", and cyan for "Navi" and "navi".
If needed, read about font-lock.
(make-face 'font-lock-Navi-face)
(set-face-foreground 'font-lock-Navi-face "cyan")
(make-face 'font-lock-success-face)
(set-face-attribute 'font-lock-success-face nil :foreground "green")
Now to add the 'keywords' (regexp) to which to attach:
(defun add-custom-keywords()
"adds a few keywords for emacs mode"
;
(font-lock-add-keywords nil
'(
("Navi\\|navi" . 'font-lock-Navi-face)
;; here you can see that I highlight the letter "t" in " t " when spaced,
;; or with a parenthesis\newline around it
("\\s-t\\s-\\|\\s-t)\\|\\s-t\n" . 'font-lock-success-face)
)
)
)
You can replace Navi ("or" is here "\\|") or navi with simply your letter, "a" or "b", so "a\\|b" and then give the face to it.
; This is the hook to activate when the mode is triggered
(add-hook 'emacs-lisp-mode-hook 'add-custom-keywords)
The last part ensures that this font will be "real-time", and every time you open the file.
add-font-lock-keywords is for user customizations, mostly. If you're writing the mode yourself, it's much better to just set font-lock-defaults with an appropriate value.
Check sample-mode on the emacswiki for some example.
I'm setting up emacs to be my python IDE, and I've found plenty of material online that explain auto-completion, among a variety of other features. What I can't figure out, though, is how to get the syntax highlighter to do its highlighting magic on operators.
How can I customize my emacs in python mode to make + - different colors? I'd also like it to make integers, floats, and parentheses different colors as well.
i actually have something like this setup for my programming modes. this defines 2 separate faces, one for operators and one for the "end statement" symbol (not so useful in python, obviously). you can modify the "\\([][|!.+=&/%*,<>(){}:^~-]+\\)" regex to match the operators you are interested in and customize the faces to be the color you want.
(defvar font-lock-operator-face 'font-lock-operator-face)
(defface font-lock-operator-face
'((((type tty) (class color)) nil)
(((class color) (background light))
(:foreground "dark red"))
(t nil))
"Used for operators."
:group 'font-lock-faces)
(defvar font-lock-end-statement-face 'font-lock-end-statement-face)
(defface font-lock-end-statement-face
'((((type tty) (class color)) nil)
(((class color) (background light))
(:foreground "DarkSlateBlue"))
(t nil))
"Used for end statement symbols."
:group 'font-lock-faces)
(defvar font-lock-operator-keywords
'(("\\([][|!.+=&/%*,<>(){}:^~-]+\\)" 1 font-lock-operator-face)
(";" 0 font-lock-end-statement-face)))
then, you just enable this by adding a hook to the relevant modes (this example assumes you are using "python-mode"):
(add-hook 'python-mode-hook
'(lambda ()
(font-lock-add-keywords nil font-lock-operator-keywords t))
t t)
Put the following in your ~/.emacs file:
;
; Python operator and integer highlighting
; Integers are given font lock's "constant" face since that's unused in python
; Operators, brackets are given "widget-inactive-face" for convenience to end-user
;
(font-lock-add-keywords 'python-mode
'(("\\<\\(object\\|str\\|else\\|except\\|finally\\|try\\|\\)\\>" 0 py-builtins-face) ; adds object and str and fixes it so that keywords that often appear with : are assigned as builtin-face
("\\<[\\+-]?[0-9]+\\(.[0-9]+\\)?\\>" 0 'font-lock-constant-face) ; FIXME: negative or positive prefixes do not highlight to this regexp but does to one below
("\\([][{}()~^<>:=,.\\+*/%-]\\)" 0 'widget-inactive-face)))
Adding my own answer since I couldn't get the others working (as of emacs 25.3.1, 2017).
The following elisp can be used to highlight operators:
;; Operator Fonts
(defface font-lock-operator-face
'((t (:foreground "#8b8bcd"))) "Basic face for operator." :group 'basic-faces)
;; C-Like
(dolist (mode-iter '(c-mode c++-mode glsl-mode java-mode javascript-mode rust-mode))
(font-lock-add-keywords mode-iter
'(("\\([~^&\|!<>=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep))))
;; Scripting
(dolist (mode-iter '(python-mode lua-mode))
(font-lock-add-keywords mode-iter
'(("\\([#~^&\|!<>:=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep))))
In Emacs is there a way to syntax-highlight the parentheses of quoted and backquoted sexps differently than the parens of other sexps so they stand out? e.g. I want these parens to be a different color than other parens:
(foo `(bar (baz)) quux)
^ ^
Have a look at mic-paren, a minor mode built as an extension to the packages paren.el and stig-paren.el for Emacs. It features recognition of "escaped" sexps.
Now for special highlighting : if you look at the code, the behaviour of quoted sexp matching is governed by the variable paren-match-quoted-paren. When finding a couple of match sexps, the typeface change is made using statements such as:
(mic-overlay-put mic-paren-backw-overlay 'face paren-mismatch-face)
(with similar alternatives for matched, unmatched). It shouldn't be too hard to define an alternative font (similarly to what is done with paren-mismatch-face), and replace those typeface-changing statements by functions that use your alternative font if paren-match-quoted-paren is true.
Note: updated links to refer to latest version
You can apply the following patch to mic-paren (follow link for latest version, 3.8) to get what you want. Customize the newly created face paren-face-quoted-match which is glaringly set up to have a green foreground and orange background for testing purposes.
Now when you're next to a matched set of parenthesis preceded by a single open quote `, you'll get the quoted face. This example uses an orange background and green foreground - most likely colors you'll want to change.
Here's a picture of it in action:
alt text http://img262.imageshack.us/img262/8866/quoted.png
--- orig-mic-paren.el 2009-11-11 17:02:42.000000000 -0800
+++ mic-paren.el 2009-11-11 17:05:35.306263000 -0800
## -561,4 +561,16 ##
:group 'mic-paren-matching)
+(defface paren-face-quoted-match
+ '((((class color)) (:foreground "green" :background "orange"))
+ (t (:reverse-video t)))
+ ""
+ :group 'faces
+ :group 'mic-paren-matching)
+
+(defcustom paren-quoted-match-face 'paren-face-quoted-match
+ "Mic-paren face used for a quoted paren"
+ :type 'face
+ :group 'mic-paren-matching)
+
;;; End of User Options
;;; ======================================================================
## -1052,5 +1064,9 ##
face (if mismatch
paren-mismatch-face
- paren-match-face)
+ (save-excursion
+ (if (progn (goto-char (- (min (point) opos) 1))
+ (looking-at "`"))
+ paren-quoted-match-face
+ paren-match-face)))
visible (when (pos-visible-in-window-p opos)
(save-excursion
To apply the patch, cut/paste the patch chunk to a file named mic.patch, and run the following:
patch mic-paren.el mic.patch
When I type the following code in Emacs ruby-mode, the "#{foo}" is fontified in a different color than the enclosing string. How do I do this in my own Emacs mode? I tried to decipher the ruby-mode source code but couldn't understand it in a reasonable amount of time.
"a #{foo} a"
Finally figured it out. The answer is that the "override" parameter in a fontification rule should be set to t, which means that the given face will override the string face. See the documentation for the variable "font-lock-keywords" for details. Here's an example:
(define-derived-mode temp-mode fundamental-mode "Temp"
"Temporary major mode."
(set (make-local-variable 'font-lock-defaults)
'((temp-mode-font-lock-keywords) nil nil nil nil)))
(defconst temp-mode-font-lock-keywords
(list (list "$[A-Za-z0-9]+" 0 font-lock-variable-name-face t)))
Search for where ruby-mode.el sets font-lock-syntactic-keywords:
(setq ruby-font-lock-syntactic-keywords
'(
;; #{ }, #$hoge, ##foo are not comments
("\\(#\\)[{$#]" 1 (1 . nil))
Here's some documentation on the similar font-lock-keywords variable, which is what you should use to accomplish the same type of fontification.