由于fill-column-indicator
非常沉重,因此此解决方案在当前行的右侧显示一个字符。
因此,当您键入内容时,您可以在超出限制之前看到行数限制。
这定义了次要模式hl-line-margin-mode
:
;; Global, ensures one active margin for the active buffer.
(defvar hl-line-margin--overlay nil)
(defun hl-line-margin--overlay-clear ()
"Clear the overlays."
(when hl-line-margin--overlay
(delete-overlay hl-line-margin--overlay)
(setq hl-line-margin--overlay nil)))
(defun hl-line-margin--overlay ()
"Create the line highlighting overlay."
;; Remove in the event of a changed buffer,
;; ensures we update for a modified fill-column.
(when (and hl-line-margin--overlay
(not (eq (current-buffer)
(overlay-buffer hl-line-margin--overlay))))
(hl-line-margin--overlay-clear))
(unless hl-line-margin--overlay
(setq hl-line-margin--overlay (make-overlay 0 0))
(let ((space `((space :align-to ,fill-column)
(space :width 0))))
(overlay-put hl-line-margin--overlay 'after-string
(concat (propertize " " 'display space 'cursor t)
(propertize " " 'face '(:inverse-video t))))))
(let ((eol (line-end-position)))
(unless (eql eol (overlay-start hl-line-margin--overlay))
(move-overlay hl-line-margin--overlay eol eol))))
(defun hl-line-margin-mode-enable ()
"Turn on `hl-line-margin-mode' for the current buffer."
(add-hook 'post-command-hook #'hl-line-margin--overlay nil t))
(defun hl-line-margin-mode-disable ()
"Turn off `hl-line-margin-mode' for the current buffer."
(hl-line-margin--overlay-clear)
(remove-hook 'post-command-hook #'hl-line-margin--overlay t))
;;;###autoload
(define-minor-mode hl-line-margin-mode
"Show a character at the fill column of the current line."
:lighter ""
(cond (hl-line-margin-mode
(jit-lock-unregister #'hl-line-margin-mode-enable)
(hl-line-margin-mode-enable))
(t
(jit-lock-unregister #'hl-line-margin-mode-disable)
(hl-line-margin-mode-disable))))
如果使用evil-mode
并希望将此模式限制为插入模式,则可以添加以下挂钩:
(add-hook 'evil-insert-state-entry-hook #'hl-line-margin-mode-enable)
(add-hook 'evil-insert-state-exit-hook #'hl-line-margin-mode-disable)