Skip to content

Instantly share code, notes, and snippets.

@Johniel
Created November 10, 2012 04:08
Show Gist options
  • Save Johniel/4049848 to your computer and use it in GitHub Desktop.
Save Johniel/4049848 to your computer and use it in GitHub Desktop.
(require 'linum)
(require 'whitespace)
(defface linum-hidden-face '((t :inherit (shadow default))) nil)
(set-face-attribute
'linum-hidden-face nil
:foreground (assoc-default 'background-color default-frame-alist))
(defun ytl-hide-spaces (str)
(replace-regexp-in-string
(regexp-quote (make-string 1 ? ))
(propertize (make-string 1 ?a) 'face 'linum-hidden-face)
(propertize str 'face 'linum)
t t))
;; emacs/lisp/linum.el, line 140
(defun ytl-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 (ytl-hide-spaces
(if fmt
(format fmt line)
(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)))
(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)))))
(defadvice linum-update-window (around ytl)
(ytl-linum-update-window (ad-get-arg 0)))
(defun ytl-integrate-linum-and-whitespace ()
(ad-activate 'linum-update-window)
(global-whitespace-mode 1)
(global-linum-mode 1))
(provide 'ytl-integrate-linum-and-whitespace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment