2010-01-21 70 views
16

从文档中我可以看到我可以访问命令行参数(命令行参数)。 我想添加自己的参数,但Emacs在启动时抱怨它无法识别它们。Emacs自定义命令行参数

E.g.

emacs -my_argument 

我得到:

 
command-line-1: Unknown option `-my_argument' 

什么来定义我的自定义参数,并以我的Emacs会话提供信息的正确方法? 有没有办法从命令行弹出一个参数?

回答

26

添加这样的事情你~/.emacs~/.emacs.el,或~/.emacs.d/init.el文件:

(defun my-argument-fn (switch) 
    (message "i was passed -my_argument")) 

(add-to-list 'command-switch-alist '("-my_argument" . my-argument-fn)) 

则可以执行emacs -my_argument,它应该打印i was passed -my_argument的小缓冲区。您可以在GNU elisp reference中找到更多信息。

8

正如另一篇文章中所述,您可以将您的自定义开关添加到command-switch-alist,emacs将调用处理函数来处理在命令行中传入的任何匹配开关。但是,此操作在您的.emacs文件已被评估后完成。这在大多数情况下都可以,但是您可能希望通过命令行参数来更改您的评估的执行路径或行为;我经常这样做来启用/禁用配置块(主要用于调试)。

要达到此目的,您可以阅读command-line-args并手动检查您的开关,然后将其从列表中删除,这将停止emacs抱怨未知参数。

(setq my-switch-found (member "-myswitch" command-line-args)) 
(setq command-line-args (delete "-myswitch" command-line-args)) 

能改变你的.emacs评价像这样:

(unless my-switch-found 
    (message "Didn't find inhibit switch, loading some config.") 
    ...) 

而且可以构建成一个单一的步骤如下:

;; This was written in SO text-box, not been tested. 
(defun found-custom-arg (switch) 
    (let ((found-switch (member switch command-line-args))) 
    (setq command-line-args (delete switch command-line-args)) 
    found-switch)) 

(unless (found-custom-arg "-myswitch") 
    (message "Loading config...") 
    ...) 
+0

测试的代码标记为 “未经过测试”。它的工作原理与上述相同。 –