问题1: Emacs Shell 输出长行很可能导致 Emacs 卡死
如:cat 一个压缩成一行的 css 或 json 文件;编译时输出了大量过长的编译命令
解决办法就是检测到有输出长行时,自动关闭语法高亮
问题2: 一个回车把 Shell Buffer 中的内容当 Shell 命令执行了
如:误将 Shell Buffer 内容编辑过后、或者在 Shell Buffer 中粘贴了一些乱七八糟的内容后下意识按了一个回车(多次线上操作差点吓出心脏病)
解决办法就是输入多行内容并提交时将内容显示在一个临时 Buffer 中要求确认输入
配置如下:
(defun my/comint-font-lock-off-if-long-line (string)
(when (bound-and-true-p font-lock-mode)
(let ((long-line-found nil))
(mapc #'(lambda (line)
(if (> (length line) so-long-threshold)
(setq long-line-found t)))
(split-string string "\n"))
(when long-line-found
(font-lock-mode -1)
(message "disable `font-lock-mode' because of long line found in buffer '%s'" (buffer-name))))))
(defun my/shell--ask-if-multiline-input (input)
"Avoid accidentally INPUT too many commands to shell."
(let ((p1 (search "\n" input))
(p2 (search "\n" input :from-end t))
(buffer nil))
(when (and (not (eq nil p1)) (not (eq nil p2)) (not (eq p1 p2)))
(setq buffer (get-buffer-create "*Multiple Line Shell Input*"))
(with-current-buffer buffer
(read-only-mode -1)
(erase-buffer)
(insert input)
(read-only-mode t)
(let ((o (make-overlay (point-min) (point-max) buffer nil t)))
(overlay-put o 'face `(:background "#000" :foreground "#FFF")))
(display-buffer buffer))
(unless (yes-or-no-p "Input multiple line to shell:")
(kill-buffer buffer)
(error "Input multiple line to shell aborted"))
(kill-buffer buffer))))
(add-to-list 'comint-output-filter-functions 'my/comint-font-lock-off-if-long-line)
(add-to-list 'comint-input-filter-functions 'my/shell--ask-if-multiline-input)