Creating a new buffer with text using EmacsClient

一个人想着一个人 提交于 2019-12-03 07:23:48
Trey Jackson

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 customize open-buffer-with to do what you want.

There's a similar question you might want to look at: How do I get basic App<->Emacs integration?.

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've inserted newlines to break up this very long line for display purposes.

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

The hex-escaping (which can probably be just as easily accomplished with any other high-level language, not just Perl) is for escaping quotes that would otherwise terminate the string constant being fed to (insert).

This approach feeds text to Emacs via Emacsclient on the command line, so very long input text might give it a problem. A more general solution might be able to break up long input data and feed it to Emacs across several Emacsclient invocations.

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