Is it possible to display relative line numbers on the right margin of an Emacs buffer, and keep normal line numbers on the left margin?
This can be a starting point. Load this definitions and do 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))))))))
See the screenshot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With