Evaluate emacs lisp expression on command line

孤街醉人 提交于 2020-01-01 05:08:17

问题


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

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