Emacs shell模式:防止RET从任何地方发送输入

时间:2018-09-06 14:22:18

标签: emacs comint-mode

正如文档所述,RET将在外壳模式下的任何地方comint-send-input。问题是,如果您错误地在任何行上按回车键,而您不在提示符处,它将执行整个随机文本,直到下一个提示符。如何防止这种情况发生?如果在提示中的任意位置点击Enter,将使您转到底部的新提示,那就很好了。

2 个答案:

答案 0 :(得分:3)

像这样吗?

(defun my-comint-send-input-maybe ()
  "Only `comint-send-input' when point is after the latest prompt.

Otherwise move to the end of the buffer."
  (interactive)
  (let ((proc (get-buffer-process (current-buffer))))
    (if (and proc (>= (point) (marker-position (process-mark proc))))
        (comint-send-input)
      (goto-char (point-max)))))

(with-eval-after-load "comint"
  (define-key shell-mode-map [remap comint-send-input] 'my-comint-send-input-maybe))

您可以将(goto-char (point-max))替换为(comint-copy-old-input),以插入,但不能发送,在新提示符下输入旧输入;但这在插入的输入看起来像输出时仍然容易引起问题。

不过,还请注意关于comint-send-input Ch f comint-get-old-input中的注释和链接-可用于实现以下内容的自定义逻辑确定在调用comint-send-input时,过程标记之前的点应该是“旧输入”。

答案 1 :(得分:1)

防弹:

(defun comint-send-input-or-insert-previous-input ()
  "Call `comint-send-input' if point is after the process output marker.
Otherwise, move point to the process mark and try to insert a previous input
from `comint-input-ring' (if any) returned by `comint-previous-input-string'
and affected by the current value of `comint-input-ring-index'.

Implementation is synthesized from and inspired by the `comint-after-pmark-p',
`comint-goto-process-mark', and `comint-copy-old-input' functions."
  (interactive)
  (let ((process (get-buffer-process (current-buffer))))
    (if (not process)
        (user-error "Current buffer has no process")
      (let ((pmark (process-mark process)))
        (if (<= (marker-position pmark) (point))
            (comint-send-input)
          (goto-char pmark)
          (when (and (eolp) comint-input-ring)
            (let ((input (comint-previous-input-string 0)))
              (when (char-or-string-p input)
                (insert input)))))))))