Search code examples
emacselisps-expression

Emacs: bulletproof up-list?


I'm getting up-list: Scan error: "Unbalanced parentheses" from this position:

(foo "bar|")

Snippet from up-list doc:

This command assumes point is not in a string or comment.

So this is the expected behavior. But I don't care. I just want to go upwards from a list. Could someone suggest an up-list clone that does the proper thing?

I'm looking for something better than this naive code:

(defun up-list-naive ()
  (interactive)
  (while (not (ignore-errors (up-list) t))
    (forward-char)))

Solution

  • EDIT: incorporated Andreas Rohler's suggestion:

    This works for me in your test case:

    (defun my-up-list ()
      (interactive)
      (let ((s (syntax-ppss)))
        (when (nth 3 s)
          (goto-char (nth 8 s))))
      (ignore-errors (up-list)))
    

    syntax-ppss returns a list, the third element of which exists if you're inside a string, and the 8th element is the beginning of the string (if you're in one, otherwise nil).