0

I ran into this behavior by accident and it seems quite unusual to me. Why do Emacs Lisp regular expressions match the percentage and dollar sign as word characters? One theory I had was that this is because both of those signs are valid in function names in Emacs Lisp but ^ seems also to be valid in function names and doesn't match.

(string-match "\\w" "%") ;; => 0
(string-match "\\w" "$") ;; => 0
(string-match "\\w" "^") ;; => nil

1 Answer 1

1

What syntax category a character belongs to depends on the major mode: in text mode % has syntax word but in say C-mode it has syntax punctuation. What you want interpreted as a word varies depending on the "language" of a buffer (which is partly reflected in its mode - that's why syntax tables are mode specific): the syntax tables try to account for that, not always satisfactorily from every POV, but they provide a decent compromise (and one which you can modify - with care). See Syntax tables in the Elisp Reference manual.

E.g. in text mode, when you write "I am 100% certain that this is a bug", you probably want forward-word to go past the % sign: the 100% is a single "word". Similarly in "I paid $2.00 for this lousy coffee?", the $2.00 should be considered a single "word".

IOW, it has nothing to do with what characters can be used in function names in Emacs Lisp.

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.