Режим оболочки Emacs: Предотвратить отправку RET-ввода откуда угодно
В документации сказано, что RET будет comint-send-input
в любом месте в режиме оболочки. Проблема заключается в том, что если вы по ошибке нажали клавишу ввода в любой строке, и у вас нет запроса, он выполнит весь произвольный текст до следующего запроса. Как я могу предотвратить это? Было бы неплохо, если бы ударить Enter
в любом месте из подсказки отправит вас на новую подсказку внизу.
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)
вставить, но не отправлять старый ввод в новом приглашении; но это все еще может вызвать проблемы, когда вставленный ввод выглядит как вывод.
Тем не менее, также обратите внимание на комментарии и ссылку в ChF comint-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)))))))))