2

my-test.el contains the following:

(defun test01 ()
    "scan for end of entry, trapping error."
    (interactive)
    (condition-case nil
        (setq my-pt (scan-lists (point) 1 0))
        (error (setq my-pt nil)))
    (if my-pt
        (message "my pt is %s" my-pt)
    ;;else
        (message "my pt is nil")))

Line 1: abcdfn(def)ghi
Line 2: abcdfn@{def}ghi

I placed the cursor just before the "(" on Line 1 and then did M-x test01. This worked okay. Trying a similar test on Line 2, with the cursor just before the "{" failed, with a "my pt is nil" message. Experimenting I tried the same test in a my-test.txt file (text major mode instead of emacs-lisp major mode). In the text major mode, both Lines 1 and 2 worked okay.

Researching, I inferred that the syntax table for emacs-lisp major mode must be different from the syntax table for text major mode. However, from the my-test.el local buffer, I executed M-x describe-syntax and found the following lines:

The parent syntax table is:
...
{               (}  which means: open, matches }
}               ){  which means: close, matches {
...

Experimenting, I placed the following line in the my-test.el local buffer, placed the cursor at the end of the modify-syntax-entry command, and then typed C-xC-e to execute the command.

(modify-syntax-entry ?{ "(}")

I then re-executed test01 on Line 2; again it failed with a "my pt is nil" message.

Questions:

  1. What syntax table oriented command will permit the test01 function to work correctly on Line 2, within an emacs-lisp major mode local buffer?

  2. Once I execute the syntax command from question 1, and find test01 working correctly on Line 2, what corresponding syntax table oriented command should I use to BACK-OUT the change made from the syntax command from question 1?

2 Answers 2

3

Your approach almost works, but you can't only modify the syntax for the opening { if you want that to match the closing }, so you need both of:

(modify-syntax-entry ?{ "(}")
(modify-syntax-entry ?} "){")

For the second part of your question, see:

  • C-hf with-syntax-table
  • C-hig (elisp)Syntax Table Functions
0

I have verified that phils' answer worked perfectly. Below is my corresponding altered function.

(defun test01 ()
    "scan for end of entry, trapping error."
    (interactive)
    (setq my-syntax-table (make-syntax-table))
    (modify-syntax-entry ?{ "(}" my-syntax-table)
    (modify-syntax-entry ?} "){" my-syntax-table)
    (with-syntax-table my-syntax-table
        (condition-case nil
            (setq my-pt (scan-lists (point) 1 0))
            (error (setq my-pt nil)))
        (if my-pt
            (message "my pt is %s" my-pt)
        ;;else
            (message "my pt is nil")))
    (makunbound 'my-syntax-table))

Your Answer

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

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