我工作(快乐)工作通过介绍Emacs Lisp编程并已解决了第一个8.7 Searching Exercise。它指出,定义变量本地功能
编写一个搜索字符串的交互功能。如果 搜索找到该字符串,则在该字符后面留下点,并显示一条消息 ,其中显示“找到!”。
我的解决办法是
(defun test-search (string)
"Searches for STRING in document.
Displays message 'Found!' or 'Not found...'"
(interactive "sEnter search word: ")
(save-excursion
(beginning-of-buffer)
(setq found (search-forward string nil t nil)))
(if found
(progn
(goto-char found)
(message "Found!"))
(message "Not found...")))
我如何found
是本地的功能?我知道let
语句定义了一个局部变量。但是,如果找到string
,我只想移动点。我不清楚如何在本地定义found
,但如果未找到string
,则没有将该点设置为beginning-of-buffer
。 let
是否适合这种情况?
对于临时范围变量(它是“let”形式的范围本地,而不是本地的*函数),你应该确实使用'let',除非正在使用词法绑定,变量是否则标记为动态)。 – phils
例如:'(let((found(save-excursion(goto-char(point-min))(search-forward string nil t nil))))(if found ...))' – phils
或者,您可以离开成功搜索后单独指向,但在失败后恢复原始位置。 – phils