Emacs custom command line argument

荒凉一梦 提交于 2019-11-28 19:31:59

Add something like this to your ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el file:

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

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

Then you can execute emacs -my_argument and it should print i was passed -my_argument to the minibuffer. You can find more information in the GNU elisp reference.

As stated in another post you can add your custom switches to command-switch-alist and emacs will call the handler function for any matching switch passed in on the command line. However, this operation is done after your .emacs file has been evaluated. This is fine for most cases but you may wish for a command line argument to alter the execution path or behaviour of your .emacs evaluation; I often do this to enable/disable configuration chunks (mainly for debugging).

To achieve this you can read command-line-args and check for your switch manually and then delete it from the list, this will stop emacs complaining about an unknown argument.

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

Which can alter your .emacs evaluation like so:

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

And you could build this into a single step:

;; 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...")
  ...)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!