将参数传递给elisp脚本。再次

时间:2011-07-20 09:18:48

标签: emacs elisp

如何传递参数-q -d -Q -t -L -fg -bg -color等?

执行emacs --script -Q <scriptname> <arguments> definetely之类的操作不会传递在emacs中使用的参数。那怎么办呢?

4 个答案:

答案 0 :(得分:4)

根据您对{Rmael Ibraim的答案的comment,我正在添加第二个答案,因为我认为我的第一个答案也是错误地解释了您的问题(如果是,您可能希望将问题编辑为提供澄清)。

您可以使用“空”参数的常用方法阻止Emacs处理命令行参数:--

所以,如果你运行这个:

emacs --script (filename) -- -Q

Emacs不会使用-Q参数(或事实上--),它将可用于您的脚本。您可以使用以下脚本轻松验证:

(print argv)

答案 1 :(得分:2)

如果您在脚本模式下运行emacs,我建议在脚本结束时将“argv”变量重置为nil,否则emacs将在脚本完成后尝试解释“argv”。

假设您有一个名为“test-emacs-script.el”的文件,其中包含以下内容:

#!/usr/bin/emacs --script
(print argv)
(setq argv nil)

尝试将此脚本作为“./test-emacs-script.el -a”运行。如果您运行此脚本而不重置“argv”(脚本中的最后一行),则输出将为:

("-a")
Unknown option `-a'

重置“argv”会删除“未知选项”错误消息

答案 2 :(得分:1)

我认为你有两种选择:

  • 创建自己的参数,如解释here
  • 使用变量command-line-args。唯一的问题是这个变量可能并不完全符合您的要求(来自manual):

Most options specify how to initialize Emacs, or set parameters for the Emacs session. We call them initial options. A few options specify things to do, such as loading libraries or calling Lisp functions. These are called action options. These and file names together are called action arguments. The action arguments are stored as a list of strings in the variable command-line-args. (Actually, when Emacs starts up, command-line-args contains all the arguments passed from the command line; during initialization, the initial arguments are removed from this list when they are processed, leaving only the action arguments.)

换句话说,command-line-args中只有行动参数可用,这对你没什么帮助。

答案 3 :(得分:1)

如果你在shell上手动输入命令应该没有问题(除了在你的例子中你的脚本名称是-Q),所以我假设你正在尝试创建一个可执行脚本用emacs作为shebang命令?

这是我见过的最好的解决方案,用于创建包含其他参数的可移植可执行elisp脚本:

Emacs shell scripts - how to put initial options into the script?

相关问题