Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Last active August 29, 2015 14:01
Show Gist options
  • Save tam17aki/213e25e93c7835826b2c to your computer and use it in GitHub Desktop.
Save tam17aki/213e25e93c7835826b2c to your computer and use it in GitHub Desktop.
(defun linum-update-window (win)
"Update line numbers for the portion visible in window WIN."
(goto-char (window-start win))
(let ((line (line-number-at-pos))
(limit (window-end win t))
(fmt (cond ((stringp linum-format) linum-format)
((eq linum-format 'dynamic)
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%" (number-to-string w) "d")))))
(width 0))
(run-hooks 'linum-before-numbering-hook)
;; Create an overlay (or reuse an existing one) for each
;; line visible in this window, if necessary.
(while (and (not (eobp)) (<= (point) limit))
(let* ((str (if fmt
(propertize (format fmt line) 'face 'linum)
(funcall linum-format line)))
(visited (catch 'visited
(dolist (o (overlays-in (point) (point)))
(when (equal-including-properties
(overlay-get o 'linum-str) str)
(unless (memq o linum-overlays)
(push o linum-overlays))
(setq linum-available (delq o linum-available))
(throw 'visited t))))))
;; (setq width (max width (length str)))
(setq width (max width (linum-my-margin-width str)))
(unless visited
(let ((ov (if (null linum-available)
(make-overlay (point) (point))
(move-overlay (pop linum-available) (point) (point)))))
(push ov linum-overlays)
(overlay-put ov 'before-string
(propertize " " 'display `((margin left-margin) ,str)))
(overlay-put ov 'linum-str str))))
;; Text may contain those nasty intangible properties, but that
;; shouldn't prevent us from counting those lines.
(let ((inhibit-point-motion-hooks t))
(forward-line))
(setq line (1+ line)))
(set-window-margins win width (cdr (window-margins win)))))
(defun linum-my-margin-width (string)
(let ((width (length string)))
(if (and (display-graphic-p)
(or (featurep 'face-remap) (require 'face-remap nil t)))
(ceiling (* width (expt text-scale-mode-step text-scale-mode-amount)))
width)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment