如果在非交互式elisp程序中使用goto-line
函数,会发生什么问题?它的文档字符串给出警告说:
This function is usually the wrong thing to use in a Lisp program. What you probably want instead is something like:
(goto-char (point-min)) (forward-line (1- N))
此外,当我尝试对包括
byte-compile-file
的初始化文件进行goto-line
时,我再次收到类似这样的不愉快警告:.emacs:170:19:Warning: `goto-line' used from Lisp code
That command is designed for interactive use only
在非交互式程序中使用
goto-line
真的那么危险吗?相关地,为什么建议的forward-line
解决方案更可取?
最佳答案
首先,这可以防止Elisp程序员养成不良习惯-编写
以行号为中心的低效率代码。即而不是使用(forward-line 1)
计算当前行号,递增并使用goto-line
。
从this邮件列表文章:
In a nutshell, the reason why goto-line should not be a frequently used command is that normally there's no reason to want to get to line number N unless you have a program that told you there's something interesting on that line.
其次,goto-line除了移动外还操纵用户的环境
点(即
push-mark
)。对于非交互使用,可能不是你要。另一方面,如果考虑到所有这些,您相信
goto-line正是您需要的,然后像这样称呼它:
(defun foo ()
(interactive)
(with-no-warnings
(goto-line N)))
而且您不会收到任何编译器警告。
关于emacs - 为什么Emacs中的 “goto-line”仅用于交互式使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805572/