6

I know that emacs can open a file while evaluating an expression:

emacs file --eval "(toggle-frame-maximized)"

However I failed to replicate this with emacsclient:

1.emacsclient can open a file:

emacsclient file

2.And it can also evaluate an expression:

emacsclient --eval "(toggle-frame-maximized)"

3.But a problem happens when the two are put together:

emacsclient file --eval "(toggle-frame-maximized)"

It starts to report error and does not open the file. So is it possible to use emacsclient to open a file while still do eval?

2 Answers2

2

After several attempts I think I found the solution, basically just put everything into eval and concatenate them use progn:

emacsclient --eval "(progn (find-file \"file\") (toggle-frame-maximized))"
  • 4
    According emacsclient's manual, when `--eval` is given, the arguments are interpreted as a list of expressions to evaluate, so you can also use `emacsclient --eval "(find-file \"file\")" "(toggle-frame-maximized)"`. – xuchunyang Dec 23 '16 at 08:10
  • 1
    This method of just evaluating `find-file "file"` is great and simple (which is important). However, it has the disadvantage that the buffer is not considered a "server-editing-buffer", so some of the nice in-built emacsclient machinery does not work — for example, when you call `emacsclient -c --eval "(find-file \"file\")" "(other-elisp-functions)" `, and then close the frame, the buffer you just opened is not killed (irrespective of whether it was open before or not), unlike with `emacsclient -c file`. – aplaice Feb 12 '17 at 18:05
  • I think that using `server-visit-files` — e.g. `(server-visit-files '(("file" . nil )) proc)` — might be a solution, except that we'd need to know the process id of the client, which I have no idea how to get. – aplaice Feb 12 '17 at 19:16
1

Instead of using the --eval option for emacsclient you could just add your elisp code to server-visit-hook in its customization buffer. This way you still get the normal behaviour of returning to the shell after exiting the server buffer.

Ben
  • 111
  • 1