4

I want to use emacsclient to let my running instance of emacs evaluate a function and print the result to a terminal.

That is, I want to do something like this:

$ emacsclient --eval "(frame-parameter (car (frame-list)) 'name)"
*info*

However, instead, I get output like this:

"*info*"

If I use message, the output is displayed in some frame's minibuffer and I still get quoted output ("*info*" instead of *info*).

How do I get princ-like output to be output to my emacsclient terminal?

2

It seems that emacsclient is the wrong way to do what I want. I thought I could not use emacs --batch because I wanted to get "live" info from my running emacs instance.

The missing piece of the puzzle was the function server-eval-at:

$ emacs --batch --eval "(progn (require 'server) (princ (format \"%s\\n\" (server-eval-at \"server\" '(frame-parameter (car (frame-list)) 'name)))))"
*info*

Or, a more significant example:

$ emacs --batch --eval "(progn (require 'server) (princ (format \"%s\\n\" (server-eval-at \"server\" '(mapconcat (lambda (f) (frame-parameter f 'name)) (frame-list) \"\\n\")))))"
*info*
CAPTURE-3-foo-service.org
SomeClass.java
SomeOtherClass.java
0

I believe what is happening is emacsclient --eval does not direct the standard output stream to the invoking shell; the only output is the resulting value of (frame-parameter (car (frame-list)) 'name) (message prints its argument as a side-effect and also returns that value). Compare this behavior to emacs --batch --eval.

Since you probably have a good reason for using emacsclient, what you can do as a hack is return a symbol- with which emacs allows you surprising freedom. Something like:

$ emacsclient --eval "(progn (defun foo (arg) (make-symbol arg))(foo \"blah\")"
blah

(just make-symbol on the return value you want to see without quotes)

Also- I personally haven't tried using it, but epipe may be exactly what you want. It does support emacsclient.

  • Thanks! I am using emacsclient because I want to get information about my running emacs process. epipe might be a good solution. I'm not sure make-symbol is right for what I want. Imagine I want output like "You have 37 buffers:\n- 2 are elisp-mode\n- 2 are info-mode\n...". – Colin Fraizer Nov 15 '16 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.