0

I wanna pipe the output of an elisp function (emacs-version) to a shell command (xclip). How can I do this?

I accept vanilla emacs and evil solutions.

2 Answers 2

3

Something like this should work:

(with-temp-buffer
  (emacs-version t)
  (call-process-region (point-min) (point-max) "foo"))

It creates a temp buffer, inserts the output of emacs-version into the temp buffer (the t argument of emacs-version) and then calls the script foo passing it the contents of the buffer on its stdin. You may have to season to taste.

To call foo with arguments, you need to read the doc string of call-process-region (do C-h f call-process-region RET). It takes a bunch of optional arguments that it interprets followed by any number of optional string arguments that it passes to the script as its arguments:

(call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS)

...

Send text from START to END to a synchronous process running PROGRAM.

...

Remaining arguments ARGS are passed to PROGRAM at startup as command-line arguments.

So you need to change the above like this:

(with-temp-buffer
  (emacs-version t)
  (call-process-region (point-min) (point-max) "foo" nil nil nil "-i" "-h"))

And again, depending on your needs, you may have to season to taste.

2
  • this works almost perfectly. If I could call "foo" with arguments like foo -i -h it would be perfect Mar 10, 2021 at 5:25
  • You can: see the doc string of call-process-region.
    – NickD
    Mar 10, 2021 at 13:49
1
Is this what you want(?):
❯ emacs --batch --eval '(prin1 (emacs-version))' | xclip

How about this:

(async-shell-command (format "echo '%s' | xclip" (emacs-version)))

;;; The following freezes don't know why...

(shell-command-to-string (format "echo '%s' | xclip" (emacs-version)))
6
  • I can use this, though I would prefer to do it from a running emacs, inside the graphical interphase Mar 10, 2021 at 3:07
  • @honestSalami Edited. See if it matches your needs.
    – TerryTsao
    Mar 10, 2021 at 3:20
  • I've tried both async and shell. async executes well as you wrote it, but it fails with xclip. shell freezes and I must stop the process with C-g. But I'm sure its not your fault. I think emacs and xclip do not play nice together. Mar 10, 2021 at 5:19
  • since you installed xclip, can you tell me if you use evil mode? I'm submitting an error just about xclip freezing over and it'd be nice to know if its evil mode or emacs itself that has trouble with xclip Mar 10, 2021 at 5:36
  • @honestSalami Sorry, I was being misleading before. I've deleted the previous comment. (I see. Beyond my capabilities it seems. One more option just to put it out there: run terminal inside emacs with sth like vterm. Then it's possible to use elisp to generate & automatically run stuff in terminal instead of typing it manually. )
    – TerryTsao
    Mar 10, 2021 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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