我知道有解决方案可以让Emacs显示80列,但我不想那样干扰视觉。我只希望它在某一行超过80个字符时将其高亮显示。
我知道有解决方案可以让Emacs显示80列,但我不想那样干扰视觉。我只希望它在某一行超过80个字符时将其高亮显示。
另一个简单的选择就是在表达式 .\{81\}
上运行 highlight-lines-matching-regexp
。
每一行超过81个字符的文本将会被用您选择的颜色进行高亮显示。
(add-hook 'emacs-lisp-mode-hook '(lambda () (highlight-lines-matching-regexp ".\{81\}" "hi-green-b")))
。我没有找到一个通用的new-buffer-hook来处理所有缓冲区。 - Juancho(add-hook 'c++-mode-hook '(lambda () (highlight-lines-matching-regexp ".\\{81\\}" 'hi-yellow)))
- Ryan Oldsfill-paragraph
),则高亮显示仍将存在。有没有办法在每次更改时重新计算它? - ABu请查看whitespace-mode
——它现在是Emacs的一部分,可以做更多的事情而不仅仅是突出显示长行。(当然,也可以仅用于此。)
whitespace-line-column
。要获取更多选项,请使用 M-x customize-group RET whitespace RET
,然后您会看到所有选项。(我只是查看源代码并编写elisp代码...) - Eli Barzilaywhitespace-style
:你可以控制需要突出显示的内容。 - Eli Barzilay这是我从Emacs Dev Kit获取的配置:
;; whitespace-mode
;; free of trailing whitespace and to use 80-column width, standard indentation
(setq whitespace-style '(trailing lines space-before-tab
indentation space-after-tab)
whitespace-line-column 80)
.emacs
中:(add-to-list 'load-path "/path/to/highlight-80+")
(require 'highlight-80+)
您可以通过以下方式启用它:
M-x highlight-80+-mode
这可能不完全符合您的要求,但我曾经遇到了类似的问题,并找到了这个问答。我需要的只是在需要时插入一个视觉指南。这就是我最终得出的结果:
(defun insert-80 ()
"Insert an 80-character-wide guide at beginning of line."
(interactive)
(beginning-of-line)
(insert "0 1 2 3 4 5 6 7 |")
(newline)
(insert "01234567890123456789012345678901234567890123456789012345678901234567890123456789|")
(newline)
)
非常简单,但有时非常有效。
在emacs-goodies-el
中有一个内置包(建议在终端中安装),名为highlight-beyond-fill-column.el,将其添加到您的.emacs
或者init.el
文件中:
(setq-default fill-column 80)
(add-hook 'prog-mode-hook 'highlight-beyond-fill-column)
(custom-set-faces '(highlight-beyond-fill-column-face
((t (:foreground "red" )))))
在代码片段中,超过fill-column
(80)的文本将以红色
突出显示。您可以根据自己的喜好设置面板。
set-face-attribute
?custom-set-faces
不应该手动修改。 - xuhdev以下是一些示例代码,它将使用当前的“警告”面对超出第80列的文本进行突出显示,并包含一行代码,启用其在C++模式下的使用。
;; Turn on warn highlighting for characters outside of the 'width' char limit
(defun font-lock-width-keyword (width)
"Return a font-lock style keyword for a string beyond width WIDTH
that uses 'font-lock-warning-face'."
`((,(format "^%s\\(.+\\)" (make-string width ?.))
(1 font-lock-warning-face t))))
(font-lock-add-keywords 'c++-mode (font-lock-width-keyword 80))
它并不会突出整行,但我觉得它还是相当有帮助的。
*mode
吗?@acm - alperfill-column
的优点,并且在模式切换后运行。因此,每个模式都可以拥有自己的适当行宽,这样可以得到正确的高亮显示。(add-hook 'after-change-major-mode-hook
(lambda ()
(when (derived-mode-p 'prog-mode)
(let ((column-re (format "^[^\n]\\{%d\\}\\(.*\\)$" fill-column)))
(font-lock-add-keywords nil
`((,column-re 1 font-lock-warning-face prepend)))))))
如果您希望在文本文件中启用此功能,可以删除prog-mode
检查。