Elisp: Ban Syntax Table
emacs syntax table is truely, really, greatly annoying. We need to write a parsing lib without syntax table.
every time i thought of using it and relying on it, i run into bunch of problems.
for example, recently i rewrote “xah-select-text-in-quote” so it's based on syntax table.
(defun xah-select-text-in-quote () "Select text between ASCII quotes, single or double." (interactive) (let (p1 p2) (if (nth 3 (syntax-ppss)) (progn (backward-up-list 1 "ESCAPE-STRINGS" "NO-SYNTAX-CROSSING") (setq p1 (point)) (forward-sexp 1) (setq p2 (point)) (goto-char (1+ p1)) (set-mark (1- p2))) (progn (error "Cursor not inside quote")))))
(note: backward-up-list
changed in emacs 24.x. I'm using 24.4)
try the command in nxml-mode
. Doesn't work. Why? probably because it has complex use of syntax table.
but syntax table is useful, no? No. It's not that emacs syntax table is useful. It's the underlying builtin emacs parsing lib that's useful.
For example, the select text in quote was like this, without using syntax table:
(defun select-text-in-quote () "Select text between the nearest left and right delimiters. Delimiters are paired characters: ()[]<>«»“”‘’「」, including \"\"." (interactive) (let (b1 b2) (skip-chars-backward "^<>(“{[「«\"‘") (setq b1 (point)) (skip-chars-forward "^<>)”}]」»\"’") (setq b2 (point)) (set-mark b1)))
The problem was that, it couldn't deal with nested quotes or backslash escaped quotes. Nor can it deal with 'single quotes', as used in {Python, Ruby, HTML, …}. (If you include single quote as delimiter, it's a problem because single quote is also used as apostrophe, and happens often (For example, “it's so!”).) But at least, for any double quoted string that doesn't contain backslash escaped quotes, it always works, reliably.
so, the solution is to add perhaps 50 lines of code to do parsing, or, rely on emacs builtin parser. ((info "(elisp) Parsing Expressions"))
but if you rely on emacs parsing engine (such as the syntax-ppss
function), it'll save you time writing the parser, but it relies on syntax table, meaning, your command's behavior is unpredictable, depending on each buffer/major-mode's syntax table.
Theoretically, the idea of syntax table is useful, because each major mode can have its own concept of quote, suitable for each language the major mode is designed for. Great. But in reality, it doesn't work out that way, as in this example of nxml-mode
(which is written by the world's top xml expert James Clark, also a top emacs lisp expert. (who also wrote the classic html-mode
and xml-mode
in 1990s, all part of emacs.))
All these html modes had complex use of syntax table, because syntax table is not flexible. (For example, in HTML, the apostrophy isn't normally a quoting character, except when inside a tag (and not already inside a quote).)
<div class='wow' title="it's">complex</div>
Emacs's syntax table is mostly designed for just 3 languages of the 1990s: {C, Lisp, TeX}.
https://plus.google.com/113859563190964307534/posts/PWchD5VtnpZ
- Emacs: Inconsistency of Search Features
- Emacs Lisp Suggestion: Function to Copy/Delete a Directory Recursively
- Emacs Lisp Problems: Trim String, Regex Match Data, Lacking Namespace
- Emacs Lisp Mode Syntax Coloring Problem
- Thoughts on Common Lisp Scheme Lisp Based Emacs
- Why Lisp Do Not Have A Generic Copy-List Function
- Emacs GNU Texinfo Problems; Invalid HTML
Emacs Modernization
- Simple Changes Emacs Should Adopt
- Why Emacs Keys are Painful
- Ban Scratch Buffer
- M-x vs Alt+x Notation
- Menu Idiocy
- Mode Line Problem
- cua-mode Problem
- Inconsistency of Search
- grep in emacs Pain
- Problems of describe-mode
- Problems of Emacs Manual
- Emacs Manual Sucks by Examples
- kill-buffer Problem
- Emacs Spell Checker Pain
- Form Feed ^L
- Single Key Delete Whole Line
- Emacs HTML Mode Sucks
- Emacs No View Image on Windows
- HTML should replace Texinfo
- Support HTML Mail
- Problems of “man”
- Emacs Lisp Mode Syntax Coloring Problem
- Emacs AHK Mode Problems
- Ban Syntax Table
- Make elisp-index-search use Current Symbol
- Texinfo Invalid HTML
- Disappearing FSF URLs, 2006
- Emacs Manual Node Persistency Issues
- Emacs: dired-do-query-replace-regex Replace ALL (fixed)
- Problems of Emacs Supporting Obsolete Systems
- Elisp: Function to Copy/Delete a Dir Recursively (fixed)
- CommonLisp/SchemeLisp Emacs
- Text Editors Popularity
- Cursor Movement Behavior Survey
- Letter-Case Commands Problem
- Select Line/Block/Quote, Extend
- isearch Current Word
- Reformat Line Wrap