代码之家  ›  专栏  ›  技术社区  ›  Yuki

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

  •  2
  • Yuki  · 技术社区  · 6 年前

    如文件所述,RET将 comint-send-input Enter 任何超出提示的地方都会将您发送到底部的新提示。

    2 回复  |  直到 6 年前
        1
  •  5
  •   phils    6 年前

    像这样的?

    (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) 插入 在新的提示符下显示旧的输入;但是当插入的输入看起来像输出时,这仍然可能导致问题。

    不过,也请注意中的评论和链接 f comint-send-input comint-get-old-input --这可以用来实现定制逻辑,以确定“旧输入”应该是什么 comint发送输入 以进程标记前的点调用。

        2
  •  1
  •   Alexander Shukaev    6 年前

    防弹:

    (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)))))))))
    
    推荐文章