1

I would like to write a small script in ELisp that would:

  1. send a command to a given buffer
  2. get its output
  3. parse it
  4. send it to another buffer

I am struggling with point 2: I cant get the output of a command. For example, if I have a shell buffer on, I can use

(process-send-string "shell" "help\n")

to send "help" to my shell buffer. It will then show the list of the commands available. But how can I get this list to use it somewhere else?

Thanks,

S4m

3 Answers 3

1

(buffer-string) returns the contents of the current buffer, so (with-current-buffer <buf> (buffer-string)) will return the contents of <buf>.

Sign up to request clarification or add additional context in comments.

5 Comments

this works but the content of <buf> can be very big and then slow down my emacs.
I'm not sure whether you are saying that my response does not answer your question or that your original question was misguided and you no longer wish to send the output of a command to a buffer as one of the steps. If you wish to avoid buffers entirely, you could try using a process filter function, but I'm not sure how much it will help; I'm not convinced that the buffer is the bottleneck here.
dfan, your response ansewrs to my question. I just tried: (send-command-to-buffer "shell" "help" (with-current-buffer "shell" (buffer-string)) But it returns the whole text in the buffer. Of course I could parse it to remove what was before the last command, but I find it suboptimal as I have no control on the size of the text in my buffer. Also, (with-current-buffer "shell" (buffer-string)) doesnt work, I have to use "shell" instead.
I see. Then I would clear the buffer before sending your process output into it, or use a process filter function.
Or you could save the size of the buffer before you outputted into it into old-point-max, and use (buffer-substring old-point-max (point-max)) to retrieve just the text after that point.
1

I don't know the exact emacs commands for this off the top of my head, but one option would be to do the following:

  • Set the mark in the shell buffer right below the command line
  • Execute the command.
  • Move the point to the end of the file and kill the text between there and the mark.
  • Move to the destination buffer and yank the text into there.

Comments

1

Have you considered using the shell-command or shell-command-to-string functions?

The don't "send a command to a buffer" like you asked, but they do both allow running a command through a process that will be started just for that purpose and either dumping the output into a target buffer or collecting it into a string.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.