检查Emacs Lisp中的forward-sexp是否成功

时间:2013-02-25 23:53:21

标签: emacs lisp elisp emacs24

我想检查(forward-sexp 1)是否成功,或以错误结束。 问题是它返回nil,即使它成功了。怎么做这个检查?

3 个答案:

答案 0 :(得分:8)

请尝试使用scan-sexps。 它是forward-sexp背后的主力并返回一个有意义的值。

当然,如果parens不平衡,那么您将收到错误 - 因此您需要ignore-errorscondition-case

答案 1 :(得分:5)

如果

forward-sexp无法向前移动,则表示错误。如果需要,您可以使用condition-case来捕获此信息。以下是测试它的示例命令:

(defun test-forward-sexp ()
  "simple command testing (forward-sexp 1)"
  (interactive)
  (condition-case nil
      (progn
        (forward-sexp 1)
        (message "succeeded"))
    (error (message "failed"))))

您可以在信息页Writing Code to Handle Errors上阅读有关处理错误的更多信息。

答案 2 :(得分:3)

如果出现错误,

forward-sexp不会返回nil(它不会返回任何内容,实际上在这种情况下;它会发出错误信号)。