3

up-list: Scan error: "Unbalanced parentheses"从这个位置得到:

(foo "bar|")

up-list来自文档的片段:

此命令假定 point 不在字符串或注释中。

所以这是预期的行为。但我不在乎。我只想从列表中向上。有人可以建议一个up-list做正确事情的克隆吗?

我正在寻找比这个天真的代码更好的东西:

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

2 回答 2

3

编辑:纳入 Andreas Rohler 的建议:

这在您的测试用例中对我有用:

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

syntax-ppss返回一个列表,如果您在字符串中,则该列表的第三个元素存在,第 8 个元素是字符串的开头(如果您在其中,则为 nil)。

于 2013-09-20T17:23:27.777 回答
1

扩展给出的答案:也处理评论,当没有找到更多列表时发送“nil”。交互调用时,消息结果。

(defun ar-up-list (arg)
  "Move forward out of one level of parentheses.
With ARG, do this that many times.

A negative argument means move backward but still to a less deep spot."
  (interactive "p")
  (let ((orig (point))
        (pps (syntax-ppss))
        erg)
    (and (nth 8 pps) (goto-char (nth 8 pps)))
    (ignore-errors (up-list arg))
    (and (< orig (point)) (setq erg (point)))
    (when (interactive-p) (message "%s" erg))
    erg))

它是补充:

(defun ar-down-list (arg)
"Move forward down one level of parentheses.
With ARG, do this that many times.

A negative argument means move backward but still go down a level. "
  (interactive "p")
  (let ((orig (point))
        (pps (syntax-ppss))
        erg)
    (and (nth 8 pps) (goto-char (nth 8 pps)))
    (ignore-errors (down-list arg))
    (and (< orig (point)) (setq erg (point)))
    (when (interactive-p) (message "%s" erg))
    erg))
于 2013-09-21T07:19:07.020 回答