[Tex/LaTex] font-lock defining keywords containing special characters and whole-words

auctexemacs

I found the reference to "\\" on line 283 of font-latex.el of auctex-11.86, and I'd like to remove it as a warning (which is easy enough to delete manually). I have not figured out a way to define certain special keywords so that font-lock knows that I am referring to a whole-word, rather than a potential sub-word. For example, if I define the word not as a keyword and then type the word nothing, the first part of nothing gets higlighted. The same thing happens with slashes and so forth.

Set forth below is a list of special character keywords that I'd like to please define as whole-words so that I can classify them as different colors using variations of the method described below. Some of the following would be defined as a warning color (i.e., because they are missing the backslash). When corrected within the *.tex document, the symbol with the backslash would be a different color — e.g., # could be red, and \# could be blue. For that to work, there would two variations of the sample code below, each defined with a different set of colors.

"
    ~
\\
~\\
\S
#
\#
$
\$
%
\%
&
\&
not
(defvar lawlist-face-red (make-face 'lawlist-face-red))
(set-face-attribute 'lawlist-face-red nil :background "white" :foreground "red" :bold t)
(font-lock-add-keywords 'latex-mode '(
("
"
\\|
#
\\|
$
\\|
%
\\|
&
" 0 lawlist-face-red prepend)

(defvar lawlist-face-blue (make-face 'lawlist-face-blue))
(set-face-attribute 'lawlist-face-blue nil :background "white" :foreground "blue" :bold t)
(font-lock-add-keywords 'latex-mode '(
("
\\\\
\\|
~
\\|
\\\\\\\\
\\|
~\\\\\\\\
\\|
\\\\S
\\|
\\\\#
\\|
\\\\$
\\|
\\\\%
\\|
\\\\&
\\|
not
" 0 lawlist-face-blue prepend)

EDIT

Here is a working draft that incorporates the the answer of A.Ellett. AUCTeX already has a predefined warning mechanism for $, &, and \\. % is a comment without the backslash, which is already colorized. I've overridden the \\ and # with the code below — the Emacs built-in warning for # is italics. AUCTeX has auto-correct for quotations when typing them in latex-mode, but I frequently copy / paste data containing improper LaTeX quotations and I needed a warning mechanism such as is outlined below:

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :background "white" :foreground "orange" :bold t :underline nil :font "Courier" :height 200)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :background "white" :foreground "cyan" :bold t :underline nil :font "Courier" :height 200)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :background "white" :foreground "blue" :bold t :underline nil :font "Courier" :height 200)

(font-lock-add-keywords 'latex-mode '(

("#\\|\"\\|~" 0 lawlist-face-a t)

("\\(\\\\\\)[^a-zA-Z@]" 1 lawlist-face-a t)

("\\\\\\\\" 0 lawlist-face-b t)

("\\bnot\\b\\|~\\\\\\\\\\|\\\\#\\|\\\\\\$\\|\\\\S\\|\\\\%\\|\\\\&" 0 lawlist-face-c t)

) 'prepend)

Best Answer

If you want emacs to only match on a whole word, you need to write something like

\bnot\b

In terms of defining this for font-locking, you may want to try something along the lines of

"\\bnot\\b"

The following macro can show you how this will match only "not" as a word.

(defun example-search ()
  (interactive)
  (re-search-forward "\\bnot\\b"))

If you've turned on hi-lock mode, this next function can help:

(defun example-hi-lock ()
  (interactive)
  (highlight-regexp "\\bnot\\b" 'hi-lock-word-not))

though you'll have to define the face hi-lock-word-not.

Note The syntax \b only matches at word boundaries. So if you try to use it with nonword characters, you're not likely to be able to match the expressions you're interested in.