6

I have created a comint mode for using mongo. The mode works fine in the buffer, and now I am trying to use it programmatically where I need to get the output from commands.

Getting input to the process works fine. Getting output from the process though has remained tricky. What I want is to send a cmd to the mongo process, and have the function that sent the command return the output.

I have setup a filter function where I save the output into a variable:

(defun mongo-output-filter (output)
  "Hook function for handling process output." 
  (setq mongo-output output))

I setup a function to take a command like this:

(defun ts (cmd)
  (comint-simple-send (get-buffer-process "*Mongo*") cmd)
  mongo-output)

The trouble is that sometimes it works, and sometimes it doesn't. What does work is I see the output in the Mongo buffer, but it isn't always returned by the function.

I tried adding (accept-process-output (get-buffer-process "Mongo")), but that does not always give me the output. Sometimes it does, and sometimes I just get a prompt. It doesn't seem to matter if I use any optional arguments.

Is this the right way to go about this? None of the other questions on SO seem to answer this question.

Drew
  • 75,699
  • 9
  • 109
  • 225
John Kitchin
  • 11,555
  • 1
  • 19
  • 41
  • The output from the process arrives buffered (split into chunks), if, instead of appending it, you reset it every time, you may end up with just the final chunk from the process. `accept-process-output` serves a different purpose - it allows the function waiting to read process output to proceed, suspending other Elisp code execution, but this will only happen if there is any unprocessed output. – wvxvw Jan 17 '17 at 05:34
  • 2
    I think this question is answered there: http://emacs.stackexchange.com/questions/692/asynchronously-wait-for-output-from-a-comint-process Essentially, you have to watch the output in the comint buffer until you recieve the prompt from the process. – Tobias Jan 17 '17 at 11:06
  • 2
    Oh I see. Thanks for the clarification. It's hard to believe there isn't a built in for this. – John Kitchin Jan 17 '17 at 19:15

0 Answers0