【问题标题】:Emacs shell-mode: Prevent RET sending input from anywhereEmacs shell-mode:防止RET从任何地方发送输入
【发布时间】:2018-09-06 14:22:17
【问题描述】:

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

【问题讨论】:

  • 你可以使用 C-h v comint-input-filter-functions 吗?
  • @aartis Idk,你有什么想法吗?
  • 不,我碰巧看到它在这种情况下可能很有用。

标签: emacs comint-mode


【解决方案1】:

这样的?

(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 的 cmets 和链接——这可用于实现自定义逻辑以建立“旧输入”应该是当comint-send-input在进程标记之前被调用时。

【讨论】:

  • 这就像一个魅力。谢谢你。我确实替换了comint-get-old-input,它更方便。如果我误击了RET,我可以轻松删除它,如果我想执行一些以前的输入,我可以这样做。
  • 我只是在想,如何测试当前行是某个命令的输出还是旧提示符。因为更好的可能是这个逻辑(if (command output) (goto-char (point-max)) (comint-send-input))
  • 我猜关键是你需要区分命令结束和结果输出开始之间的点(如果有的话)。我不知道目前是否支持。如果没有,您需要自己跟踪这些位置。
  • M-x apropos RET comint-last RET 会很有趣,但您需要阅读代码以确认这些变量的使用方式以及它们何时可靠可用。
  • @phils,看看我对此的看法。
【解决方案2】:

防弹:

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

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多