3

It bothered me that sexp navigation commands didn't treat a quoted string as a single expression in text-mode. This is because the quote character " is in the punctuation syntax class in the text-mode syntax table. I decided to make it a string delimiter:

(modify-syntax-entry ?\" "\"" text-mode-syntax-table)

Similarly, I wanted sexp navigation commands in AUCTeX buffers to treat the initial backslash in a macro as part of the macro (as happens in the built-in latex-mode), so I changed the class of \ to expression prefix:

(modify-syntax-entry ?\\ "'" LaTeX-mode-syntax-table)

Now, I've been told that modifying syntax tables is "dangerous" and can have "far-reaching unintended consequences", but I've been using those for months and so far I haven't noticed any consequences other than improved sexp navigation.

So my question is: what can actually go wrong? A good answer would be a specific example of some text and some commands that have different outcomes with and without the syntax class change, where the outcome with the change is clearly inferior in some way. Is there any actual danger or is this just an abstract fear some people have?

2
  • It's hard to guess what the consequences might be, the syntax table is used in a lot of places. If you've been using this config for months without problems, it's possible your changes didn't break anything in this particular case. Just keep in mind that should anything break in future, one of the first things to check will be rolling back this change to see if you did finally find something.
    – Tyler
    Jun 14, 2021 at 13:41
  • 1
    @Tyler: I don't even need to remember that, since whenever I spot a bug or something weird, I first check if it also happens in emacs -Q, and if it doesn't, it's my fault and I bisect my configuration to see how I screwed up.
    – Omar
    Jun 14, 2021 at 21:55

1 Answer 1

0

I'm relatively inexperienced with writing and modifying major modes, but I've messed around a little. I can't give a definitive answer as to why this happened, but I had some unexpected behavior while working on a major mode for a language in which comments can be initiated with #n (case-insensitive) and, notably, must be terminated with a semicolon. If a semicolon is omitted the comment runs through subsequent lines with the comment ending only once a semicolon is found. To get the desired highlighting behavior, I modified the syntax table like this:

(modify-syntax-entry ?# ". 1" st) ; sets comments to start with:
(modify-syntax-entry ?n ". 2" st) ;   #n or
(modify-syntax-entry ?N ". 2" st) ;   #N
(modify-syntax-entry ?\; ">" st)  ; and run until terminated by a semicolon!

This turned out to have some weird and unexpected consequences when leveraging regular expressions for setting font lock keywords later. In particular, it seemed like the letter "n" was no longer matched by "\w". This, in turn, would cause "font-lock-variable-name-face" matches to begin mid-word on the letter "n", so this assignment:

#var strange 10;

would get highlighted as if the variable name was "nge" with "stra" getting the the default face:

depiction of highlighting behavior

The only way I could get things to work properly, while continuing to use modify-syntax-entry this way, was to avoid "\w" in regular expressions instead using "[a-zA-Z0-9_]" which still worked.

Again I'm no expert at lisp or major modes -- and I certainly might've been doing something really stupid with my font lock keywords -- but that's at least one scenario where messing with modify-syntax-entry was dangerous, leading to clearly worse behavior.

1
  • The syntax you specified for n is ". 2", i.e. punctuation and second character of a comment start. You need to make it "w 2". For complex cases, you can use dynamic character syntax properties (see emacs.stackexchange.com/questions/68757/…), but I don't think you need that for just #n as a comment start. Oct 12, 2021 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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