Passing arguments to elisp script. Again

2019-01-28 09:44发布

问题:

How can I pass arguments -q -d -Q -t -L -fg -bg --color etc?

Doing something like emacs --script -Q <scriptname> <arguments> definetely will not pass arguments, which are used in emacs. So how to do it?

回答1:

Based on your comment on Rafael Ibraim's answer, I'm adding this second answer, as I think my first answer is also mis-interpreting your question (and if so, you may wish to edit the question to provide clarification).

You can prevent Emacs from processing command line arguments using the usual approach of an 'empty' argument: --

So if you run this:

emacs --script (filename) -- -Q

Emacs will not eat the -Q argument (or the -- in fact), and it will be available to your script. You can easily verify this with the following script:

(print argv)


回答2:

If you run emacs in script mode, I would recommend to reset "argv" variable to nil in the end of your script, otherwise emacs will try interpret "argv" after script is finished.

Let's assuming you have a file named "test-emacs-script.el" with the following content:

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

Try running this script as "./test-emacs-script.el -a". If you run this script without resettings "argv" (last line in the script) then the output will be:

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

Resetting "argv" gets rid of "unknown option" error message



回答3:

I think you have two options here:

  • Create your own argument like explained here.
  • Use the variable command-line-args. The only problem is that this variable probably doesn't do exactly what you want(from the 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.)

In other words, only action arguments will be available in command-line-args, wich doesn't really help you much.



回答4:

If you're typing the command manually at the shell there shouldn't be a problem (excepting that in your example your script name is -Q), so I assume you're trying to create an executable script with emacs as the shebang command?

This is the best solution I've seen for creating portable executable elisp scripts which include additional arguments:

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



标签: emacs elisp