【问题标题】:Silently send command to comint without printing prompt在没有打印提示的情况下静默发送命令到 comint
【发布时间】:2017-10-08 14:01:04
【问题描述】:

我想向 comint shell-mode 发送命令,而不打印额外的提示。我正在尝试使用comint-redirect-* API,但我仍然收到额外的提示。有什么简单的方法可以完全避免提示打印,或者回溯并删除它?

我的重定向,

(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)))

shell-hook 中的调用示例,

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

但是,comint shell 会打印以下内容(注意双重提示)

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

不直接相关,但是为了显示打印两次,这里的提示设置为

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

【问题讨论】:

  • 旁注:如果我edebug my-comint-redirect-silently 并启动一个shell,提示只会打印一次
  • 我认为您对comint-send-stringcomint-send-input 之类的东西感兴趣....有关高级内容,另请参阅process-send-string
  • @lawlist 不是真的,我试图暂时重定向输出,因此是“静默”。我已经有一个通用的劣质进程重定向库,类似于ess 的静默重定向,但我宁愿使用 new(?) comint 一个。
  • err 不是新的,我在 git log 中看到提到它可以追溯到 1999 年

标签: shell emacs comint-mode


【解决方案1】:

我查看了comint-redirect-results-list-from-process 的工作原理并对此进行了验证。

(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)))))

希望对你有帮助

【讨论】:

  • 非常感谢,我错过了accept-process-output。我的场景实际上并不需要重定向输出,但这为我清除了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
相关资源
最近更新 更多