Emacs 中的相对行号 [英] Relative Line Numbers In Emacs
问题描述
有谁知道像这样的 Vim 相对行号 是否存在对于 emacs?我使用 vimpulse,伙计,这将非常方便!我知道一些 lisp,所以如果它不知道,我可以尝试制作自己的,如果我在正确的方向上找到了一点.
Does anyone know how if something like this Vim Relative Line Numbers exists for emacs? I use vimpulse, and man, that would be super handy to have! I know some lisp, so if it doesn't, I could try to make my own, if I got a point in the right direction.
更新:感谢正确的响应,我想出了这个,当前行显示 1,上一行显示 -1,用于结合 vimpulse yanks 和 deletes.
Update: Thanks to the correct response, I came up with this, that will show 1 for the current line, and -1 for the previous line, for combining with vimpulse yanks and deletes.
非常感谢所有帮助过的人!我知道这不完全是 Vim 所做的,但是 vim 中从零开始的相对行号有什么好处?愚蠢的vim.
Thanks a ton to all who helped! I know it is not exactly what Vim does, but what good is the Relative line numbers in vim that start at zero?? Silly vim.
(defvar my-linum-current-line-number 0)
(setq linum-format 'my-linum-relative-line-numbers)
(defun my-linum-relative-line-numbers (line-number)
(let ((test2 (1+ (- line-number my-linum-current-line-number))))
(propertize
(number-to-string (cond ((<= test2 0) (1- test2))
((> test2 0) test2)))
'face 'linum)))
(defadvice linum-update (around my-linum-update)
(let ((my-linum-current-line-number (line-number-at-pos)))
ad-do-it))
(ad-activate 'linum-update)
推荐答案
(2012-03-16:行号现在右对齐,并显示在正确的面上.)
这里的问题是,在调用自定义 linum-format
函数时,点已被移动(通过 linum-update-window
)到该行有问题,所以我们不能再用它来确定两条线之间的区别;它只会为每一行打印一个零.
The problem here is that by the time a custom linum-format
function is called, point has already been moved (by linum-update-window
) to the line in question, so we can no longer use it to establish the difference between the two lines; it would just print a zero for every line.
有一个linum-before-numbering-hook
,但这是在之后点被移动到缓冲区的开始处运行的,所以这对我们的目的.
There is a linum-before-numbering-hook
, but this is run after point has been moved to the start of the buffer, so that's not useful for our purpose.
以下代码通过使用通知为 linum-update
存储当前行号来解决该问题,以便它可用于自定义 linum-format
函数.
The following code solves the problem by using advice for linum-update
to store the current line number, so that it will be available to the custom linum-format
function.
为了使数字右对齐,我最初使用了 %3d
的硬编码格式字符串,因为单个窗口显示超过 100 行代码的可能性不大.但是,如果您是 follow-mode
的粉丝(或者只是在同一个缓冲区上有多个窗口),那么这种情况就变得极有可能;所以代码现在动态计算所需的列数.linum-before-numbering-hook
的使用使其比默认 dynamic
linum 格式采用的方法更有效.
To right-align the numbers I initially used a hard-coded format string of %3d
on the basis that a single window showing more than 100 lines of code was not terribly likely. If you're a fan of follow-mode
, however (or simply have multiple windows on the same buffer), that circumstance becomes exceedingly likely; so the code now calculates the number of columns required dynamically. The use of linum-before-numbering-hook
makes this more efficient than the approach taken by the default dynamic
linum format.
请注意,如果您注释掉 add-hook
,将使用更快的非动态方法.
Note that if you comment out the add-hook
, the faster non-dynamic approach is used.
(defvar my-linum-format-string "%3d")
(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)
(defun my-linum-get-format-string ()
(let* ((width (1+ (length (number-to-string
(count-lines (point-min) (point-max))))))
(format (concat "%" (number-to-string width) "d")))
(setq my-linum-format-string format)))
(defvar my-linum-current-line-number 0)
(setq linum-format 'my-linum-relative-line-numbers)
(defun my-linum-relative-line-numbers (line-number)
(let ((offset (- line-number my-linum-current-line-number)))
(propertize (format my-linum-format-string offset) 'face 'linum)))
(defadvice linum-update (around my-linum-update)
(let ((my-linum-current-line-number (line-number-at-pos)))
ad-do-it))
(ad-activate 'linum-update)
这篇关于Emacs 中的相对行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!