Find file
Fetching contributors…
Cannot retrieve contributors at this time
86 lines (67 sloc) 2.98 KB
;;; visual-indentation-mode.el --- minor mode for visualizing indentation
;; This is free and unencumbered software released into the public domain.
;; Author: Christopher Wellons <>
;; Version: 1.0
;;; Commentary:
;; This does not operate properly with tabs. Tabs are for losers
;; anyway. :-) Since this is only a proof of concept I don't plan on
;; fixing this.
;; Indentation size can be adjusted by `visual-indentation-width'. I'd
;; like to detect this automatically from the mode (2 for Lisp, 4 for
;; Java, c-basic-offset, etc.) but that problem turns out to be highly
;; non-trivial.
;; The idea came from example code this video:
;;; Code:
(defgroup visual-indentation-mode nil
"Group for visual-indentation-mode."
:group 'applications)
(defcustom visual-indentation-width 2
"Width of indentation -- different than tab-width."
:group 'visual-indentation-mode
:type 'integer)
(defface visual-indentation-light-face
'((((background light)) (:background "#AFAFAF"))
(((background dark)) (:background "#4F4F4F")))
"Face for highlighting odd indents."
:group 'visual-indentation-mode)
(defface visual-indentation-dark-face
'((((background light)) (:background "#DFDFDF"))
(((background dark)) (:background "#101010")))
"Face for highlighting even indents."
:group 'visual-indentation-mode)
(defun visual-indentation-choose-color (pos)
"Determine the color for the given buffer position."
(let ((col (save-excursion (goto-char pos) (current-column))))
(if (= 0 (mod (/ col visual-indentation-width) 2))
(defun visual-indentation-propertize (pos)
"Color the indentation at POS."
(add-text-properties pos (1+ pos)
`(font-lock-face ,(visual-indentation-choose-color pos)
rear-nonsticky t))))
(defun visual-indentation-propertize-region (start end)
"Color all indentation in the region."
(goto-char start)
(while (< (point) end)
(dotimes (i (- (current-indentation) (current-column)))
(visual-indentation-propertize (+ i (point))))
(defun visual-indentation-unpropertize-region (start end)
"Remove all indentation coloring from the region."
(remove-text-properties start end '(font-lock-face nil rear-nonsticky nil)))
(define-minor-mode visual-indentation-mode
"Visualize indentation in the current buffer."
:lighter "" ; should be obvious enough already
(if (not visual-indentation-mode)
(jit-lock-unregister 'visual-indentation-propertize-region)
(visual-indentation-unpropertize-region (point-min) (point-max)))
(jit-lock-register 'visual-indentation-propertize-region)
(make-local-variable 'visual-indentation-width)))
(provide 'visual-indentation-mode)
;;; visual-indentation-mode.el ends here