Emacs shell-mode:阻止RET从任何地方发送输入



正如文档所说,RET将在shell模式下comint-send-input任何地方。问题是,如果您错误地在任何行上按了 Enter 并且您没有出现提示,它将执行整个随机文本,直到下一个提示。如何防止这种情况发生?如果在提示之外的任何地方点击Enter会将您发送到底部的新提示,那就太好了。

像这样的东西?

(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)插入,但不在新提示符下发送旧输入;但是当插入的输入看起来像输出时,这仍然容易引起问题。

但是,还要注意C-hfcomint-send-input中关于comint-get-old-input的注释和链接 - 这可用于实现自定义逻辑,用于确定在进程标记之前用点调用comint-send-input时"旧输入"应该是什么。

防弹:

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

相关内容

  • 没有找到相关文章

最新更新