【问题标题】:Emacs relative line numbers on the right marginEmacs 右边距的相对行号
【发布时间】:2012-07-23 12:17:12
【问题描述】:

是否可以在 Emacs 缓冲区的右边距显示相对的行号,而在左边距保持正常的行号?

【问题讨论】:

  • 对于那些不熟悉该功能的人:Relative line numbers in VIM;
  • 我从来没有听说 emacs 有相对的行号。
  • stackoverflow.com/questions/6874516/… 只是无法将它们放在屏幕右侧。
  • 是的,你当然可以这样做,尽管你可能需要一个自定义版本的 linum.el 来做到这一点(或至少重新定义它的一些功能),因为假设只在左边距是非常硬编码的。让它使用右边距而不是左边距是一个相当微不足道的改变,但在每个边距中写入不同的值肯定需要更多的工作。
  • 这不是您问题的答案,但我建议您关闭行号。真的不会花很长时间习惯,而且你会节省宝贵的水平空间。 M-g M-g 将带你到任何你想去的行,如果你从compile 编译或运行你的程序,你可以在不显式输入行号的情况下浏览错误消息。

标签: emacs


【解决方案1】:

这可以作为一个起点。加载此定义并执行 M-x linum-mode.

(defun linum-relative-right-set-margin ()
  "Make width of right margin the same as left margin"
  (let* ((win (get-buffer-window))
     (width (car (window-margins win))))
    (set-window-margins win width width)))

(defadvice linum-update-current (after linum-left-right-update activate)
  "Advice to run right margin update"
  (linum-relative-right-set-margin)
  (linum-relative-right-update (line-number-at-pos)))

(defadvice linum-delete-overlays (after linum-relative-right-delete activate)
  "Set margins width to 0"
  (set-window-margins (get-buffer-window) 0 0))

(defun linum-relative-right-update (line)
  "Put relative numbers to the right margin"
  (dolist (ov (overlays-in (window-start) (window-end)))
    (let ((str (overlay-get ov 'linum-str)))
      (if str
      (let ((nstr (number-to-string
               (abs (- (string-to-number str) line)))))
        ;; copy string properties
        (set-text-properties 0 (length nstr) (text-properties-at 0 str) nstr)
        (overlay-put ov 'after-string
             (propertize " " 'display `((margin right-margin) ,nstr))))))))

看截图

【讨论】:

  • 这看起来很符合要求。非常值得等待!干得好!
  • 这很好。但是,我希望绝对 linum 显示在右侧,相对 linum 显示在左侧(这样我就可以将真实的行号与来自 ELPA 的 linum-relative 插件一起显示)。我该怎么做?
  • 两个左边会更好 imo
  • 我收到以下调试错误:gist.github.com/avatar-lavventura/…
  • @alper 您使用哪个版本的 emacs?我刚刚用 26.1 试了一下,效果很好。
猜你喜欢
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
相关资源
最近更新 更多