2

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

4

2 回答 2

5

像这样的东西?

(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-hf comint-send-input但是,还要注意关于的注释和链接comint-get-old-input——这可用于实现自定义逻辑,以建立comint-send-input在流程标记之前使用点调用时“旧输入”应该是什么。

于 2018-09-06T21:47:26.053 回答
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)))))))))
于 2019-01-10T12:54:58.123 回答