Search code examples
shellemacscomint-mode

Silently send command to comint without printing prompt


I want to send a command to a comint shell-mode without it printing an additional prompt. I'm trying to use the comint-redirect-* API, but I am still getting an additional prompt. What would be an easy way to either avoid the prompt printing altogether, or to track back and delete it?

My redirect,

(defun my-comint-redirect-silently (proc string)
  (let (comint-redirect-perform-sanity-check)
    (with-temp-buffer
      ;; possible to have shell not print prompt?
      (comint-redirect-send-command-to-process
       string (current-buffer) proc nil 'no-display)))
  (with-current-buffer (process-buffer proc)
    ;; necessary to track back and delete here?
    (comint-redirect-cleanup)))

Example of a call in a shell-hook,

(add-hook 'shell-mode-hook
          (lambda ()
            (my-comint-redirect-silently
             (get-buffer-process (current-buffer)) "TERM=xterm-256color")))

But, the comint shell then prints the following (notice the double prompt)

me@me-M51AC: ~
$ me@me-M51AC: ~ 
$ 

Not directly relevant, but to show it is printing twice, the prompt here is set as

$ echo $PS1
${debian_chroot:+($debian_chroot)}\[\e[32m\]\u@\h: \[\e[33m\]\w\[\e[0m\]\n\$

Solution

  • I took at look at how comint-redirect-results-list-from-process worked and bodged this.

    (defun comint-run-thing-process (process command)
      "Send COMMAND to PROCESS."
      (let ((output-buffer " *Comint Redirect Work Buffer*"))
        (with-current-buffer (get-buffer-create output-buffer)
          (erase-buffer)
          (comint-redirect-send-command-to-process command
                                                   output-buffer process nil t)
          ;; Wait for the process to complete
          (set-buffer (process-buffer process))
          (while (and (null comint-redirect-completed)
                      (accept-process-output process)))
          ;; Collect the output
          (set-buffer output-buffer)
          (goto-char (point-min))
          ;; Skip past the command, if it was echoed
          (and (looking-at command)
               (forward-line))
          ;; Grab the rest of the buffer
          (buffer-substring-no-properties (point) (- (point-max) 1)))))
    

    Hope it helps