当使用nlinum-mode (由@Stefan编写)时,我正在编写一种次要模式来突出显示当前行号,并且当光标位于窗口顶部的第一行时,除了之外,所有东西都可以工作。
nlinum-mode的代码可以在这里找到:http://elpa.gnu.org/packages/nlinum.html
任何帮助您找出为什么不起作用(即,当光标位于窗口顶部的第一行时),我们将不胜感激。
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; M-x hl-nlinum-mode
(defvar nlinum-highlight-current-line-number-string nil
"An overlay string used to highlight the current line number.")
(make-variable-buffer-local 'nlinum-highlight-current-line-number-string)
(defvar nlinum-highlight-p nil
"Set to non-`nil` when overlay is present, and `nil` when not present.")
(make-variable-buffer-local 'nlinum-highlight-p)
(defface ln-active-face
'((t (:foreground "black" :background "#eab700" :bold nil :italic nil
:underline nil :box nil :overline nil)))
"Face for `ln-active-face`."
:group 'linum)
(defface ln-inactive-face
'((t (:foreground "SteelBlue" :background "black" :bold t :italic nil
:underline nil :box nil :overline nil)))
"Face for `ln-active-face`."
:group 'linum)
(defun hl-nlinum-remove-overlay ()
(when nlinum-highlight-p
(remove-overlays (point-min) (point-max)
'before-string nlinum-highlight-current-line-number-string)
(setq nlinum-highlight-p nil)))
(defun nlinum-highlight-current-line-number ()
(when nlinum-mode
(hl-nlinum-remove-overlay)
(let* (
(pbol (point-at-bol))
(line (nlinum--line-number-at-pos))
(line-mod
(cond
((eq nlinum--width 2)
(cond
((< line 10)
(concat " " (format "%s" line)))
(t (format "%s" line))))
((eq nlinum--width 3)
(cond
((< line 10)
(concat " " (format "%s" line)))
((and
(> line 9)
(< line 100))
(concat " " (format "%s" line)))
(t (format "%s" line))))
((eq nlinum--width 4)
(cond
((< line 10)
(concat " " (format "%s" line)))
((and
(> line 9)
(< line 100))
(concat " " (format "%s" line)))
((and
(> line 99)
(< line 1000))
(concat " " (format "%s" line)))
(t (format "%s" line))))
(t (format "%s" line))))
(str (propertize line-mod 'face 'ln-active-face) )
(final-line-number (propertize " " 'display `((margin left-margin) ,str))))
(setq nlinum-highlight-current-line-number-string final-line-number)
(overlay-put (make-overlay pbol pbol)
'before-string nlinum-highlight-current-line-number-string)
(setq nlinum-highlight-p t))))
(define-minor-mode hl-nlinum-mode
"A minor-mode for highlighting the current line number when nlinum-mode is active."
:init-value nil
:lighter " HL#"
:keymap nil
:global nil
:group 'linum
(cond
(hl-nlinum-mode
(when (not nlinum-mode)
(nlinum-mode 1))
(add-hook 'post-command-hook #'nlinum-highlight-current-line-number nil t))
(t
(remove-hook 'post-command-hook #'nlinum-highlight-current-line-number t)
(remove-overlays (point-min) (point-max)
'before-string nlinum-highlight-current-line-number-string))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;发布于 2014-08-21 02:09:17
问题可能在于您调用(hl-nlinum-remove-overlay)。我不能完全理解您的代码的工作方式,但我的分析是:您主要的问题是您希望您的覆盖层取代nlinum的,但是从post-command-hook执行它并不总是有效的,因为nlinum通过jit-lock添加它的覆盖,jit-lock通常是通过重新显示,即post命令-钩子之后运行的。
我认为您可以通过在jit-lock-fontify-now中的当前行调用nlinum-highlight-current-line-number来解决这个问题。我不确定这会不会修正你的“第一行错了”。
这就是说,我认为您的代码不应该试图替换nlinum的覆盖。相反,它应该去修改nlinum的覆盖。即:
jit-lock-fontify-nowhl-nlinum--last-overlay中),那么取消修改最后一个覆盖。如果ol是nlinum当前行的覆盖,您应该可以使用以下内容来修改它:
(let* ((spc (overlay-get ol 'before-string))
(disp (get-text-property 0 'display spc))
(str (nth 1 disp)))
(put-text-property 0 (length str) 'face 'foo str))发布于 2017-06-07 12:06:35
通过在配置中设置当前行号,您可以启用nlinum包1.7版本中的当前行号:
(setq nlinum-highlight-current-line t)https://stackoverflow.com/questions/25411108
复制相似问题