如何在`nlinum-mode`中突出显示当前行号

law*_*ist 2 emacs elisp

我正在编写一个次要模式以在使用时突出显示当前行号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))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Run Code Online (Sandbox Code Playgroud)

Kau*_*odi 5

您可以nlinum通过在配置中进行设置来启用在包的1.7 版中突出显示当前行号:

(setq nlinum-highlight-current-line t)
Run Code Online (Sandbox Code Playgroud)