Emacs 右边距的相对行号

标签 emacs

是否可以在 Emacs 缓冲区的右侧边缘显示相对行号,并在左侧边缘保留正常行号?

最佳答案

这可以作为一个起点。加载此定义并执行 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))))))))

看截图

emacs screenshot

关于Emacs 右边距的相对行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612364/

相关文章:

list - 如何在 emacs 中使用 defvar 构建列表

用于 isearch-forward 的 Emacs 键绑定(bind)

emacs - 如何在 Emacs 中启用 Zen Coding?

emacs - 如何设置 Emacs 主题?

emacs - 如何控制 Emacs 缩写扩展中的光标位置?

git - 在 Magit 中查看文件的历史记录?

emacs - 如何在 emacs 中重置 smtp+gnutls 的缓存密码

Emacs,使用Delete键删除选择

emacs - 在 Elisp 中将整数转换为 float

emacs - 如何摆脱Emacs中的大黄色感叹号