首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在“`nlinum mode`”中突出显示当前行号

如何在“`nlinum mode`”中突出显示当前行号
EN

Stack Overflow用户
提问于 2014-08-20 17:37:33
回答 2查看 2K关注 0票数 2

当使用nlinum-mode (由@Stefan编写)时,我正在编写一种次要模式来突出显示当前行号,并且当光标位于窗口顶部的第一行时,除了之外,所有东西都可以工作。

nlinum-mode的代码可以在这里找到:http://elpa.gnu.org/packages/nlinum.html

任何帮助您找出为什么不起作用(即,当光标位于窗口顶部的第一行时),我们将不胜感激。

代码语言:javascript
复制
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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-now
  • 然后在当前行上查找nlinum的覆盖
  • 如果这是一个与最后一个不同的覆盖(你把它藏在一个hl-nlinum--last-overlay中),那么取消修改最后一个覆盖。
  • 最后,用下面的代码修改当前行的覆盖。

如果ol是nlinum当前行的覆盖,您应该可以使用以下内容来修改它:

代码语言:javascript
复制
(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))
票数 2
EN

Stack Overflow用户

发布于 2017-06-07 12:06:35

通过在配置中设置当前行号,您可以启用nlinum包1.7版本中的当前行号:

代码语言:javascript
复制
(setq nlinum-highlight-current-line t)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25411108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档