【发布时间】:2011-10-09 05:33:18
【问题描述】:
如何传递参数 -q -d -Q -t -L -fg -bg --color 等?
执行类似emacs --script -Q <scriptname> <arguments> 的操作不会传递参数,这些参数在 emacs 中使用。那么该怎么做呢?
【问题讨论】:
如何传递参数 -q -d -Q -t -L -fg -bg --color 等?
执行类似emacs --script -Q <scriptname> <arguments> 的操作不会传递参数,这些参数在 emacs 中使用。那么该怎么做呢?
【问题讨论】:
根据您对 Rafael Ibraim 的回答comment,我添加了第二个答案,因为我认为我的第一个答案也误解了您的问题(如果是这样,您可能希望编辑问题以提供说明)。
您可以使用“空”参数的常用方法来阻止 Emacs 处理命令行参数:--
所以如果你运行这个:
emacs --script (filename) -- -Q
Emacs 不会吃掉-Q 参数(或者实际上是--),它可以用于您的脚本。您可以使用以下脚本轻松验证这一点:
(print argv)
【讨论】:
":"; exec emacs --no-site-file --script "$0" -- "$@" # -*-emacs-lisp-*-,所以这件事做得很好。再次感谢。
-- 以防止 Emacs 在脚本执行之前处理 option 参数,而在脚本退出之前需要(setq argv nil) 以防止 Emacs 处理 非选项 参数作为文件名,and visiting them.
argv 更好的是在脚本末尾使用(kill-emacs 0) 显式退出(假设0 是此时的适当状态),这确保(a ) Emacs 不做任何其他事情(处理剩余的参数,或其他); (b) 你保证你的脚本返回哪个退出状态(这是你在任何情况下都应该做的事情)。参考lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
如果您在脚本模式下运行 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”会消除“未知选项”错误消息
【讨论】:
./test-emacs-script.el -Q 示例的输出为 nil。使用 -- 参数的技巧可以避免该问题以及您正在解决的问题,因此我相信只有在您实际上不希望这些参数到达您的脚本时才推荐您的方法。
-- 以防止 Emacs 在脚本执行之前处理 option 参数,而在脚本退出之前需要(setq argv nil) 以防止 Emacs 处理 non-option 参数作为文件名,and visiting them.
argv 更好的是在脚本末尾使用(kill-emacs 0) 显式退出(假设0 是此时的适当状态),这确保(a ) Emacs 不做任何其他事情(处理剩余的参数,或其他); (b) 您保证您的脚本返回哪个退出状态(这是您在任何情况下都应该做的事情)。参考lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
我认为你有两个选择:
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 中只有动作参数可用,这对你没有多大帮助。
【讨论】:
如果您在 shell 中手动输入命令应该没有问题(除了在您的示例中您的脚本名称是 -Q),所以我假设您正在尝试使用 emacs 创建可执行脚本作为 shebang 命令?
这是我见过的用于创建包含附加参数的可移植可执行 elisp 脚本的最佳解决方案:
Emacs shell scripts - how to put initial options into the script?
【讨论】: