syntaxemacsmajor-mode

How to get the character corresponding to syntax in a major-mode


How can I get the character (or set thereof) corresponding to a given syntax character in a major-mode?

For example, I want to be able to get the escape character, eg '\' in most modes like elisp or C. I can't find a function that would return the character(s) corresponding to a syntax class -- in this case the escape syntax class, ie. (string-to-syntax "\\") ;; => (9) in Lisp/C modes.

So, how can I go the other way, eg. (some-function '(9) major-mode) ;; => "\\"


Solution

  • Syntax tables are char-tables, you can use map-char-table to check every rule in the syntax talbe, for example,

    (defun foo (syntax-code syntax-table)
      "Return chars (or char range) with SYNTAX-CODE in SYNTAX-TABLE."
      (let (result)
        (map-char-table
         (lambda (k v)
           ;; k is either a char or (FROM . TO)
           ;; v is (SYNTAX-CODE . MATCHING-CHAR)
           (when (= (car v) syntax-code)
             (push (pcase k
                     (`(,from . ,to) (list (string from) (string to)))
                     (_ (string k)))
                   result)))
         syntax-table)
        (nreverse result)))
    
    (foo 9 emacs-lisp-mode-syntax-table)
    ;; => ("\\")