问题
I'm a newbie to emacs. I'm working with emacs-24.1 on redhat linux, and trying to evaluate an elisp expression. What I want emacs to do is to evaluate the elisp expression without launching emacs itself. I'm trying different things
emacs --eval '(+ 2 3)'
I do not know if emacs is evaluating the expression, but the result is not shown on console and emacs window comes up. Next I tried this
emacsclient --eval '(+ 2 3)'
Emacs client is expecting a server. It could not find the server and hence throwing an error (can't find socket. start server etc). So I launched a server (server-name is SERVER) and ran emacsclient again
emacsclient --server-file=SERVER -e '(+ 2 3)'
This time, emacs evaluated the expression and printed the result on console. That is because emacs is using the existing server to evaluate the expression. Now I get a problem when the server is not running.
emacsclient --server-file=ANOTHER_SERVER -e '(+ 2 3)' -a emacs
This time, I'm not getting any error on console. Emacs is launching a new window, because of -a (my .emacs has (server-start) command in it and server-name set to ANOTHER_SERVER). But emacs then is trying to edit the file (+ 2 3). It is shown on the mode line. I'm confused. emacsclient --help showed me this
-e, --eval Evaluate the FILE arguments as ELisp expressions
and emacs manual says this.
'-e'
'--eval'
Tell Emacs to evaluate some Emacs Lisp code, instead of visiting some files.
When this option is given, the arguments to emacsclient are interpreted as a
list of expressions to evaluate, not as a list of files to visit.
I do not know how to proceed on this. As I said, my goal is to evaluate an elisp expression without launching emacs. Is it possible?
回答1:
After a bit of testing it looks like you can use --batch
to have emacs dump any messages to stderr. Then you can call message
to print things to stderr where you'll be able to see them. Your example would become emacs --batch --eval '(message (number-to-string (+ 2 3)))'
and the result would be printed to stderr.
If you're trying to redirect the output to a file you'll need to redirect stderr instead of stdout by using 2>
instead of just >
.
回答2:
Try
emacs --batch --eval '(print (+ 2 3))'
来源:https://stackoverflow.com/questions/11474774/evaluate-emacs-lisp-expression-on-command-line