2

Is there a way to make forward-list and backward-listsearch for specific symbols (such as 「」『』) only?

I have code that make use of these functions to iterate through 「」『』 pairs in plain text. But forward-list would apply to ()《》〈〉 etc. as well, which makes the code very inefficient.

(re-search-forward "「\\|『" nil t) is not the optimal solution because I am dealing with balanced pairs of quotes, and only have to deal with the outer-most paretheses. For instance, forward-list would stop 2 times in the scenario below, while re-search-forward would take 4.

forward-list:

「『』『』」1「」2

re-search-forward:

「1『2』『3』」「4」

3

You can define your own temporary syntax table. Just start with a newly created character table with default nil entries and modify only those syntax entries you need.

The following Elisp function demonstrates that. It creates a temporary syntax table with the syntax you want and sends you into recursive edit.

You can then interactively try commands like forward-list with the binding C-M-n.

Exit recursive edit with C-M-c when you are done.

(defun my-stops ()
  "Test modified syntax table with `recursive-edit'."
  (interactive)
  (with-syntax-table (make-char-table 'syntax-table)
    (modify-syntax-entry ?\「 "(」")
    (modify-syntax-entry ?\」 ")「")
    (modify-syntax-entry ?\『 "(』")
    (modify-syntax-entry ?\』 ")『")
    (let ((header-line-format "Test the modified syntax in recursive edit. Leave with M-C-c."))
      (recursive-edit))))

If you want to avoid the creation of a syntax table at each function call just store it in a global variable and re-use it a la

(defvar my-syntax-table nil)

(defun my-stops ()
 (with-syntax-table (or my-syntax-table
                        (setq my-syntax-table (let ((table (make-char-table 'syntax-table)))
                                                 (modify-syntax-entry ?\「 "(」" table)
                                                 ...
                                                 table)))
  ...))

Independently of what I wrote above I would actually use a combination of re-search-forward and forward-sexp instead of modifying the syntax table for a task like that.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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