我想检查(forward-sexp 1)
是否成功,或以错误结束。
问题是它返回nil,即使它成功了。怎么做这个检查?
答案 0 :(得分:8)
请尝试使用scan-sexps
。
它是forward-sexp
背后的主力并返回一个有意义的值。
当然,如果parens不平衡,那么您将收到错误 - 因此您需要ignore-errors
或condition-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(它不会返回任何内容,实际上在这种情况下;它会发出错误信号)。