1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
;;; highlight-indentation.el --- Function for highlighting indentation
;; Author: Anton Johansson <anton.johansson@gmail.com> - http://antonj.se
;;
;; Source:
;; https://github.com/antonj/Highlight-Indentation-for-Emacs/blob/master/highlight-indentation.el
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;;; Commentary:
;; Customize `highlight-indent-face' to suit your theme.
;;; Code:
(defface highlight-indent-face
'((((class color) (min-colors 88) (background dark))
:background "grey22")
(((class color) (min-colors 88) (background light))
:background "grey88"))
"Basic face for highlighting indentation guides."
:group 'basic-faces)
;; Used buffer-local to toggle on-off
(setq-default highlight-indent-active nil)
;; Needed to to remove font-lock-keywords
(setq-default highlight-indent-offset 4)
(defun highlight-indentation (&optional indent-width)
"Toggle highlight indentation.
Optional argument INDENT-WIDTH specifies which indentation
level (spaces only) should be highlighted, if omitted
indent-width will be guessed from current major-mode"
(interactive "P")
(when (not highlight-indent-active)
(set (make-local-variable 'highlight-indent-offset)
(if indent-width
indent-width
;; Set indentation offset according to major mode
(cond ((eq major-mode 'python-mode)
(if (boundp 'python-indent)
python-indent
py-indent-offset))
((eq major-mode 'ruby-mode)
ruby-indent-level)
((eq major-mode 'nxml-mode)
nxml-child-indent)
((local-variable-p 'c-basic-offset)
c-basic-offset)
(t
(default-value 'highlight-indent-offset))))))
(let ((re (format "\\( \\) \\{%s\\}" (- highlight-indent-offset 1))))
(if highlight-indent-active
(progn ;; Toggle off
(set (make-local-variable 'highlight-indent-active) nil)
(font-lock-remove-keywords nil `((,re (1 'highlight-indent-face))))
(message "highlight-indentation OFF"))
(progn ;; Toggle on
(set (make-local-variable 'highlight-indent-active) t)
(font-lock-add-keywords nil `((,re (1 'highlight-indent-face))))
(message (format "highlight-indentation with indent-width %s"
highlight-indent-offset))))
(font-lock-fontify-buffer)))
(provide 'highlight-indentation)
;;; highlight-indentation.el ends here
|