Creating a new text buffer using EmacsClient

I have a program that can send text to any other program for further analysis (e.g. sed, grep, etc.). I would like him to send data to Emacs and do the analysis there. How can I do it? EmacsClient takes the default file name, this is a data string, not a file, and I really don't want to create and delete files just to send data to Emacs.

EmacsClient has a command-line option "eval" that allows you to execute lisp code instead of open files. Is there a simple lisp function that will open a new buffer with the given text?

Edit: I'm looking for something like:

emacsclientw.exe -eval (open-new-buffer 'hello world')

And a new buffer will automatically appear with the word "hello world". I do not know how the buffer name will be set. Hope something is automatically numbered.

+7
emacs elisp
source share
2 answers

This does what you ask for:

 emacsclient -e '(open-buffer-with "some\nstuff\nhere")' (defun open-buffer-with (txt) "create a new buffer, insert txt" (pop-to-buffer (get-buffer-create (generate-new-buffer-name "something"))) (insert txt)) 

Obviously, you can configure open-buffer-with to do what you want.

There is a similar question to which you can pay attention: How to get the basic integration of App ↔ Emacs? .

+6
source share

How about this approach?

 emacsclient -e ' (progn (pop-to-buffer (generate-new-buffer "Piped")) (insert (decode-hex-string " '$(perl -e 'print unpack "H*", qq("Hello, World!")' )'"))) ' 

I inserted new lines to break this very long line for display.

When I run this from a terminal window, a new buffer called Piped opens in my Emacs window containing the text "Hello, World!" (complete with quotation marks). When I run it again, another buffer opens with the name Piped<2> with the same text.

Hexadecimal escaping (which may also be just as easy with any other high-level language, not just Perl) is to speed up the quotation marks, which would otherwise complete the string constant supplied to (insert) .

This approach feeds text to Emacs via Emacsclient on the command line, so very long input text can give it a problem. A more general solution is to split the long input and pass it to Emacs over several Emacsclient calls.

+2
source share

All Articles