2

I'm interested in seeing if it is possible to match just 1 backslash, and just 2 backslashes, and a tilde plus two backslashes. I'm using Emacs in latex-mode and am setting up keywords for font-lock. Defining a single backslash as a keyword wreaks havoc on a variety of other definitions. I'd like one backslash to be red; two backslashes to be blue; and a tilde+two-back-slashes to be green. I don't think the tilde will pose a problem, but I'd like that to be red all by itself. I've got the font-lock-add-keywords format, but not the special regex for this type of a situation. This is similar to the situation that we would use \b for before and after, but that won't work with backslashes as far as I know.

~ -- red

\ -- red, except when touching alphanumeric characters.

\\ -- blue

~\\ -- green

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :foreground "red" :bold t)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :foreground "blue" :bold t)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :foreground "green" :bold t)

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

("~\\|\\\\" 0 lawlist-face-a prepend)

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

("~\\\\\\\\" 0 lawlist-face-c prepend)

))

In the context of the example mentioned above, defining a single backslash nullifies the predefined warnings of font-latex.el within auctex-11.86 at lines 280-285. Removing the "\\" from the fourth line of code doesn't remedy the situation. Typing \newpage, for example, no longer is associated with font-latex-warning-face -- instead, it comes up as undefined, which is assigned to font-latex-sedate-face.

(defvar font-latex-built-in-keyword-classes
  '(("warning"
     ("nopagebreak" "pagebreak" "newpage" "clearpage" "cleardoublepage"
      "enlargethispage" "nolinebreak" "linebreak" "newline" "-" "\\*" "\\"
      "appendix" "displaybreak" "allowdisplaybreaks" "include")
     'font-latex-warning-face 1 noarg)

BUFFER EXAMPLE -- latex-mode:

\newpage -- font face should be font-latex-warning-face

\newpage -- font face erroneously appears as font-latex-sedate-face when defining a single backslash as noted hereinabove.


EDIT -- troubleshooting -- testing -- screenshots of re-builder and a LaTeX document:

\\(\\\\\\)[^a-zA-Z@]

re-builder
(source: lawlist.com)

test01
(source: lawlist.com)

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :background "black" :foreground "red" :bold t)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :foreground "blue" :bold t)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :foreground "green" :bold t)

(font-lock-add-keywords 'latex-mode
   '(("~\\|\\(\\\\\\)[^a-zA-Z@]" 0 lawlist-face-a prepend)
     ("\\\\\\\\" 0 lawlist-face-b prepend)
     ("~\\\\\\\\" 0 lawlist-face-c prepend)))

test02
(source: lawlist.com)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
lawlist
  • 13,099
  • 3
  • 49
  • 158
  • I run your code and it gives me the results I expected from your description. Can you give an example of text with what you expect and what goes wrong. – Nicolas Dudebout May 15 '13 at 14:00
  • I've added one specific example of errors caused by defining a single backslash. Yesterday, I had a situation where the single backslash nullified the definition of a double backslash -- I'll see if I can recreate that and post that as a second example. – lawlist May 15 '13 at 14:31
  • Please add a box containing the text you have in your buffer and explain your desired output. It is easier to reproduce. – Nicolas Dudebout May 15 '13 at 14:36
  • Perhaps if there were a way to define a single backslash to `exclude` situations where it is `touching alphanumeric characters` that would solve the problem. I'll add some clarification to the error example above. – lawlist May 15 '13 at 14:41
  • The goal is to be able to use a single red backslash all by itself to insert a blank space in LaTeX documents (or call my attention to it if it was inadvertently inserted); and also to call attention to a situation where there is an uneven number of backslashes -- e.g., three backslashes should show up as two blue followed by one red. `Excluding` situations where a single backslash is `touching alphanumeric characters` would avoid conflicts with virtually every LaTeX definition I can think of -- e.g., \begin... \end... \1234 – lawlist May 15 '13 at 14:50
  • Are you using AUCTeX. This changes things. – Nicolas Dudebout May 15 '13 at 15:12
  • Yes, AUCTeX is active. – lawlist May 15 '13 at 15:15

1 Answers1

1

LaTeX commands can only be composed of letters (and @ symbol in libraries). It is therefore sufficient to do the following to distinguish between a single slash and the beginning of a command:

(defvar lawlist-face-a (make-face 'lawlist-face-a))
(set-face-attribute 'lawlist-face-a nil :foreground "red" :bold t)

(defvar lawlist-face-b (make-face 'lawlist-face-b))
(set-face-attribute 'lawlist-face-b nil :foreground "blue" :bold t)

(defvar lawlist-face-c (make-face 'lawlist-face-c))
(set-face-attribute 'lawlist-face-c nil :foreground "green" :bold t)

(font-lock-add-keywords 'latex-mode
                        '(("~" 0 lawlist-face-a prepend)
                          ("\\(\\\\\\)[^a-zA-Z@]" 1 lawlist-face-a prepend)
                          ("\\\\\\\\" 0 lawlist-face-b prepend)
                          ("~\\\\\\\\" 0 lawlist-face-c prepend)))
Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
  • I think that did the trick -- the built-in warning classes are working again and the other `\commands` are not getting trumped anymore. I'll use the new configuration today and report back with a final confirmation (along with a green check-mark for the answer) :). Thank you so very much for helping me to isolate the issue and come up with a solution -- greatly appreciated !!!! – lawlist May 15 '13 at 15:44
  • If the background of the single backslash is `nil`, everything works. If the background of the single backslash contains a color (e.g., black) `and` the single backslash is at the end of the line, then the background is highlighted from the end of the line all the way to the right window edge. I've tried inserting into your code some periods and dollar signs at various places, but that didn't completely resolve the issue. It's not a major issue, but if there is a dollar sign or something that could fix it, that would be great. – lawlist May 15 '13 at 17:13
  • When is there a background color on the backslash? What is causing it? – Nicolas Dudebout May 15 '13 at 18:21
  • You can see the same effect by running `re-builder` (with `"\\\\[^a-zA-Z@]"`) and a few backslashes in text-mode in the other buffer. To duplicate the issue with `lawlist-face-a`, insert `:background "black"`. – lawlist May 15 '13 at 18:57
  • Essentially, it appears that `\\\\[^a-zA-Z@]` is matching spaces and line endings to the right of the backslash. It also matches an underscore and a hyphen to the right of the backslash, and a few other special characters. – lawlist May 15 '13 at 19:08
  • The change fixes your problem. It uses the alphanumeric characters to select the appropriate backslashed but apply the font only to the backslash. – Nicolas Dudebout May 15 '13 at 19:56
  • I've posted a screenshot of how Emacs regex `re-builder` interprets `\\(\\\\\\)[^a-zA-Z@]`, which is the same result with a LaTeX document assigning a background to the single backslash (a screenshot of which is also posted). If we can get an expression to work using `re-builder`, then it should also work with a `LaTeX` document. For example, `re-builder` will not display a blue highlight to the right edge of the window when we find the correct regex for this particular issue. – lawlist May 15 '13 at 21:49
  • Have you tried with LaTeX? It works. The thing is that re-builder is using two shades of blue, one for what is in the parentheses and one for the rest. What is in the parentheses will be colored in latex, what is not in parentheses will not. Therefore the long trailing blue thingy seen on re-builder is not a problem. – Nicolas Dudebout May 16 '13 at 04:22
  • The screen shot of LaTeX is using the new code `\\(\\\\\\)[^a-zA-Z@]`. I just turned off all other definitions and double checked it. I'll post the revised code I'm using underneath the screenshots so that you can duplicate the issue. To duplicate the issue, it is necessary to have a background color associated with the single backslash. – lawlist May 16 '13 at 04:28
  • Of course, your background screen color would need to be different than the background color of the single backslash to appreciate the difference. – lawlist May 16 '13 at 04:37
  • I think we need some more regex magic to the right of the backslash to handle things like line endings, spaces, dashes, underscores, and the works . . . . – lawlist May 16 '13 at 04:52
  • @lawlist you have not copied the new code completely, you just took parts of it. In the code I put, I split the line in two and replaced the number 0 by a number 1 for one of the lines. This is why it works for me. – Nicolas Dudebout May 16 '13 at 15:06
  • Ah . . . I missed that part -- thank you for helping to point out the difference -- that may do the trick. If we make lawlist-face-a background `black`, and a `white` background for the other two (lawlist-face-b and lawlist-face-c), this may work okay. I'll try it out today and report back . . . so far so good. – lawlist May 16 '13 at 16:48
  • Boy-oh-boy -- you had to work pretty hard for that green check-mark :) Thank you so very much for all your help. I learned a few new things with this thread. Greatly appreciated !!!! – lawlist May 16 '13 at 18:35
  • I just wanted to add a note for anyone looking to increase the level of highlighting for sub-expressions using an example similar to the one in this thread -- `legoscia` helped me to understand in the following thread that the `'prepend` atom needs to be outside the quoted list, as the last argument to `font-lock-add-keywords`: http://stackoverflow.com/questions/16637594/latex-mode-font-lock-any-way-to-obtain-more-than-three-3-levels-of-highligh – lawlist May 19 '13 at 20:35