emacs - 为什么Emacs中的 “goto-line”仅用于交互式使用?

标签 emacs elisp

如果在非交互式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/

相关文章:

shell - 如何加速异步 shell 命令的 emacs 输出?

emacs - Emacs elisp 回溯中奇怪的转义序列

emacs - 重新定义 AUCTeX 字体和编译键绑定(bind)

deployment - Emacs-Lisp : How to package emacs-lisp program as PC application?

emacs - ELisp:如何在 'else' block 中执行多个表达式?

emacs - 如何使用 emacs 缩进一个 block ?

eclipse - Eclipse 编辑器是否具有等效于 Emacs 的 "align-regex"?

python - 如何突出显示 Emacs 中未使用的 Python 模块

emacs - 使用来自多个(虚拟)机器的单个 emacs 实例

visual-studio - 如何使用 Emacs 仿真扩展将文本从另一个应用程序粘贴到 Visual Studio?