Emacs:防弹上榜?

时间:2013-09-20 16:14:54

标签: emacs elisp s-expression

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

(foo "bar|")

来自up-list doc:

的代码段
  

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

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

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

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

2 个答案:

答案 0 :(得分: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个元素是字符串的开头(如果你在一个,否则为零)。

答案 1 :(得分: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))