我正在为 emacs 编写一个次要模式,它至少会计算缓冲区中每一行的数值。我想以视觉方式显示这一点,最好在每行之前整齐地显示。
我知道一些次要模式会吸引到边缘,而且我知道叠加也是一种选择(这些相关吗?),但我在任何地方都找不到我想要的很好的例子。
基本上,我想要类似 linum-mode 中的行号的东西,但每次修改缓冲区时它们都需要更改(实际上,只有当它们所在的行发生更改时)。每行的字符计数器之类的东西就是一个很好的例子。如果可能的话,我希望它不会破坏 linum 模式,但不依赖它等等。
这是一个简单的示例,说明了在之后放置叠加层的一种方法linum-mode
数字和文本行之前。 我需要考虑一下字符数的右对齐。
NOTE:此方法考虑到linum-mode
生成数字before本示例中的代码。如果post-command-hook
or the widow-scroll-functions
钩子用于实现这个建议的方法,那么对钩子的那些添加将需要随后及时跟进 to the linum-mode
附加到这些相同的钩子的函数。
下面的例子可以用post-command-hook
和window-scroll-functions
钩。有关如何确定的示例,请参阅以下链接window-start
and window-end
之前redisplay
发生:https://stackoverflow.com/a/24216247/2112489 https://stackoverflow.com/a/24216247/2112489
EDIT:添加了字符数右对齐 - 考虑最多三位数字(即每行最多 999 个字符)。字符计数覆盖后的文本现在左对齐。
(save-excursion
(let* (
(window-start (window-start))
(window-end (window-end)))
(goto-char window-end)
(while (re-search-backward "\n" window-start t)
(let* (
(pbol (point-at-bol))
(peol (point-at-eol))
(raw-char-count (abs (- peol pbol)))
(starting-column
(propertize (char-to-string ?\uE001)
'display
`((space :align-to 1)
(space :width 0))))
(colored-char-count
(propertize (number-to-string raw-char-count)
'face '(:background "gray50" :foreground "black")
'cursor t))
(one-spacer
(propertize (char-to-string ?\uE001)
'display
`((space :width 1))))
(two-spacers
(propertize (char-to-string ?\uE001)
'display
`((space :width 2))))
(final-char-count
(cond
((and
(< raw-char-count 100)
(> raw-char-count 9))
(concat one-spacer colored-char-count))
((< raw-char-count 10)
(concat two-spacers colored-char-count))
(t colored-char-count))) )
(overlay-put (make-overlay pbol pbol)
'before-string
(concat starting-column final-char-count two-spacers) )))))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)