Skip to content
Newer
Older
100644 244 lines (216 sloc) 10.1 KB
5f4736a @antonj Function -> Minor mode
authored
1 ;;; highlight-indentation.el --- Minor modes for highlighting indentation
9ef754c @antonj init
authored
2 ;; Author: Anton Johansson <anton.johansson@gmail.com> - http://antonj.se
3 ;; Created: Dec 15 23:42:04 2010
cd6d816 Bump 'n' tag
Anton Johansson authored
4 ;; Version: 0.7.0
ba6b4c3 @antonj Added URL to github repo
authored
5 ;; URL: https://github.com/antonj/Highlight-Indentation-for-Emacs
9ef754c @antonj init
authored
6 ;;
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation; either version 2 of
10 ;; the License, or (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be
13 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
14 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 ;; PURPOSE. See the GNU General Public License for more details.
16 ;;
17 ;;; Commentary:
bd03fd8 @antonj Documentation update
authored
18 ;; Customize `highlight-indentation-face', and
19 ;; `highlight-indentation-current-column-face' to suit your theme.
9ef754c @antonj init
authored
20
21 ;;; Code:
22
5f4736a @antonj Function -> Minor mode
authored
23 (defgroup highlight-indentation nil
24 "Highlight Indentation"
25 :prefix "highlight-indentation-"
26 :group 'basic-faces)
27
28 (defface highlight-indentation-face
b0121dd @antonj highlight-indent-face inherits from fringe. Face of fringe has non in…
authored
29 ;; Fringe has non intrusive color in most color-themes
7d97528 @antonj Fix face
authored
30 '((t :inherit fringe))
2c1116a @antonj Changed to defface instead of make-face
authored
31 "Basic face for highlighting indentation guides."
5f4736a @antonj Function -> Minor mode
authored
32 :group 'highlight-indentation)
9ef754c @antonj init
authored
33
5f4736a @antonj Function -> Minor mode
authored
34 (defcustom highlight-indentation-offset 4
35 "Default indentation offset, used if no other can be found from
36 major mode. This value is always used by
37 `highlight-indentation-mode' if set buffer local. Set buffer
38 local with `highlight-indentation-set-offset'"
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
39 :group 'highlight-indentation)
9ef754c @antonj init
authored
40
db884ee @antonj Use overlays
authored
41 (defconst highlight-indentation-hooks
76980d8 @antonj Prepare for current col rewrite
authored
42 '((after-change-functions (lambda (start end length)
43 (highlight-indentation-redraw-region
44 start end
45 'highlight-indentation-overlay
46 'highlight-indentation-put-overlays-region))
47 t t)
48 (window-scroll-functions (lambda (win start)
49 (highlight-indentation-redraw-window
50 win
51 'highlight-indentation-overlay
52 'highlight-indentation-put-overlays-region
53 start))
54 nil t)))
db884ee @antonj Use overlays
authored
55
56 (defun highlight-indentation-get-buffer-windows (&optional all-frames)
57 "Return a list of windows displaying the current buffer."
58 (get-buffer-window-list (current-buffer) 'no-minibuf all-frames))
59
76980d8 @antonj Prepare for current col rewrite
authored
60 (defun highlight-indentation-delete-overlays-buffer (overlay)
db884ee @antonj Use overlays
authored
61 "Delete all overlays in the current buffer."
62 (save-restriction
63 (widen)
76980d8 @antonj Prepare for current col rewrite
authored
64 (highlight-indentation-delete-overlays-region (point-min) (point-max) overlay)))
db884ee @antonj Use overlays
authored
65
76980d8 @antonj Prepare for current col rewrite
authored
66 (defun highlight-indentation-delete-overlays-region (start end overlay)
db884ee @antonj Use overlays
authored
67 "Delete overlays between START and END."
68 (mapc #'(lambda (o)
76980d8 @antonj Prepare for current col rewrite
authored
69 (if (overlay-get o overlay) (delete-overlay o)))
db884ee @antonj Use overlays
authored
70 (overlays-in start end)))
71
76980d8 @antonj Prepare for current col rewrite
authored
72 (defun highlight-indentation-redraw-window (win overlay func &optional start)
73 "Redraw win starting from START."
74 (highlight-indentation-redraw-region (or start (window-start win)) (window-end win t) overlay func))
75
76 (defun highlight-indentation-redraw-region (start end overlay func)
77 "Erease and read overlays between START and END."
78 (save-match-data
79 (save-excursion
956d831 @antonj Current column rewrite
authored
80 (let ((inhibit-point-motion-hooks t)
81 (end (save-excursion (goto-char end) (line-beginning-position 2))))
76980d8 @antonj Prepare for current col rewrite
authored
82 (highlight-indentation-delete-overlays-region start end overlay)
83 (funcall func start end overlay)))))
84
85 (defun highlight-indentation-redraw-all-windows (overlay func &optional all-frames)
86 "Redraw the all windows showing the current buffer."
87 (dolist (win (highlight-indentation-get-buffer-windows all-frames))
88 (highlight-indentation-redraw-window win overlay func)))
89
90 (defun highlight-indentation-put-overlays-region (start end overlay)
db884ee @antonj Use overlays
authored
91 "Place overlays between START and END."
92 (goto-char start)
93 (let (o ;; overlay
94 (last-indent 0)
95 (pos start))
96 (while (< pos end)
97 (beginning-of-line)
98 (while (and (integerp (char-after))
99 (not (= 10 (char-after))) ;; newline
100 (= 32 (char-after))) ;; space
101 (when (= 0 (% (current-column) highlight-indentation-offset))
102 (setq pos (point)
103 last-indent pos
104 o (make-overlay pos (+ pos 1)))
76980d8 @antonj Prepare for current col rewrite
authored
105 (overlay-put o overlay t)
956d831 @antonj Current column rewrite
authored
106 (overlay-put o 'priority highlight-indentation-overlay-priority)
db884ee @antonj Use overlays
authored
107 (overlay-put o 'face 'highlight-indentation-face))
108 (forward-char))
109 (forward-line) ;; Next line
110 (setq pos (point)))))
111
956d831 @antonj Current column rewrite
authored
112 (defun highlight-indentation-guess-offset ()
113 "Get indentation offset of current buffer"
114 (cond ((and (eq major-mode 'python-mode) (boundp 'python-indent))
115 python-indent)
116 ((and (eq major-mode 'python-mode) (boundp 'py-indent-offset))
117 py-indent-offset)
118 ((and (eq major-mode 'python-mode) (boundp 'python-indent-offset))
119 python-indent-offset)
120 ((eq major-mode 'ruby-mode)
121 ruby-indent-level)
122 ((and (eq major-mode 'scala-mode) (boundp 'scala-indent:step))
123 scala-indent:step)
124 ((and (eq major-mode 'scala-mode) (boundp 'scala-mode-indent:step))
125 scala-mode-indent:step)
126 ((or (eq major-mode 'scss-mode) (eq major-mode 'css-mode))
127 css-indent-offset)
128 ((eq major-mode 'nxml-mode)
129 nxml-child-indent)
130 ((eq major-mode 'coffee-mode)
131 coffee-tab-width)
132 ((eq major-mode 'js-mode)
133 js-indent-level)
134 ((eq major-mode 'js2-mode)
135 js2-basic-offset)
136 ((and (fboundp 'derived-mode-class) (eq (derived-mode-class major-mode) 'sws-mode))
137 sws-tab-width)
138 ((eq major-mode 'web-mode)
139 web-mode-html-offset) ; other similar vars: web-mode-{css-indent,scripts}-offset
140 ((local-variable-p 'c-basic-offset)
141 c-basic-offset)
142 (t
143 (default-value 'highlight-indentation-offset))))
144
5f4736a @antonj Function -> Minor mode
authored
145 ;;;###autoload
146 (define-minor-mode highlight-indentation-mode
db884ee @antonj Use overlays
authored
147 "Highlight indentation minor mode highlights indentation based on spaces"
5f4736a @antonj Function -> Minor mode
authored
148 :lighter " ||"
db884ee @antonj Use overlays
authored
149 (when (not highlight-indentation-mode) ;; OFF
76980d8 @antonj Prepare for current col rewrite
authored
150 (highlight-indentation-delete-overlays-buffer 'highlight-indentation-overlay)
db884ee @antonj Use overlays
authored
151 (dolist (hook highlight-indentation-hooks)
152 (remove-hook (car hook) (nth 1 hook) (nth 3 hook))))
5f4736a @antonj Function -> Minor mode
authored
153
154 (when highlight-indentation-mode ;; ON
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
155 (when (not (local-variable-p 'highlight-indentation-offset))
156 (set (make-local-variable 'highlight-indentation-offset)
956d831 @antonj Current column rewrite
authored
157 (highlight-indentation-guess-offset)))
158
db884ee @antonj Use overlays
authored
159 ;; Setup hooks
160 (dolist (hook highlight-indentation-hooks)
161 (apply 'add-hook hook))
76980d8 @antonj Prepare for current col rewrite
authored
162 (highlight-indentation-redraw-all-windows 'highlight-indentation-overlay
163 'highlight-indentation-put-overlays-region)))
5f4736a @antonj Function -> Minor mode
authored
164
165 ;;;###autoload
166 (defun highlight-indentation-set-offset (offset)
167 "Set indentation offset localy in buffer, will prevent
168 highlight-indentation from trying to guess indentation offset
169 from major mode"
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
170 (interactive
171 (if (and current-prefix-arg (not (consp current-prefix-arg)))
172 (list (prefix-numeric-value current-prefix-arg))
173 (list (read-number "Indentation offset: "))))
174 (set (make-local-variable 'highlight-indentation-offset) offset)
175 (when highlight-indentation-mode
176 (highlight-indentation-mode)))
177
178 ;;; This minor mode will highlight the indentation of the current line
179 ;;; as a vertical bar (grey background color) aligned with the column of the
180 ;;; first character of the current line.
181 (defface highlight-indentation-current-column-face
182 ;; Fringe has non intrusive color in most color-themes
956d831 @antonj Current column rewrite
authored
183 '((t (:background "black")))
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
184 "Basic face for highlighting indentation guides."
185 :group 'highlight-indentation)
186
956d831 @antonj Current column rewrite
authored
187 (defvar highlight-indentation-overlay-priority 1)
188 (defvar highlight-indentation-current-column-overlay-priority 2)
189
190 (defconst highlight-indentation-current-column-hooks
f306b12 @antonj Buffer local hook
authored
191 '((post-command-hook (lambda ()
956d831 @antonj Current column rewrite
authored
192 (highlight-indentation-redraw-all-windows 'highlight-indentation-current-column-overlay
f306b12 @antonj Buffer local hook
authored
193 'highlight-indentation-current-column-put-overlays-region)) nil t)))
956d831 @antonj Current column rewrite
authored
194
195 (defun highlight-indentation-current-column-put-overlays-region (start end overlay)
196 "Place overlays between START and END."
197 (let (o ;; overlay
198 (last-indent 0)
199 (indent (save-excursion (back-to-indentation) (current-column)))
200 (pos start))
201 (goto-char start)
202 ;; (message "doing it %d" indent)
203 (while (< pos end)
204 (beginning-of-line)
205 (while (and (integerp (char-after))
206 (not (= 10 (char-after))) ;; newline
207 (= 32 (char-after))) ;; space
208 (when (= (current-column) indent)
209 (setq pos (point)
210 last-indent pos
211 o (make-overlay pos (+ pos 1)))
212 (overlay-put o overlay t)
213 (overlay-put o 'priority highlight-indentation-current-column-overlay-priority)
214 (overlay-put o 'face 'highlight-indentation-current-column-face))
215 (forward-char))
216 (forward-line) ;; Next line
217 (setq pos (point)))))
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
218
219 ;;;###autoload
956d831 @antonj Current column rewrite
authored
220 (define-minor-mode highlight-indentation-current-column-mode
221 "Hilight Indentation minor mode displays a vertical bar
222 corresponding to the indentation of the current line"
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
223 :lighter " |"
956d831 @antonj Current column rewrite
authored
224
225 (when (not highlight-indentation-current-column-mode) ;; OFF
226 (highlight-indentation-delete-overlays-buffer 'highlight-indentation-current-column-overlay)
227 (dolist (hook highlight-indentation-current-column-hooks)
228 (remove-hook (car hook) (nth 1 hook) (nth 3 hook))))
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
229
956d831 @antonj Current column rewrite
authored
230 (when highlight-indentation-current-column-mode ;; ON
231 (when (not (local-variable-p 'highlight-indentation-offset))
232 (set (make-local-variable 'highlight-indentation-offset)
233 (highlight-indentation-guess-offset)))
234
235 ;; Setup hooks
236 (dolist (hook highlight-indentation-current-column-hooks)
237 (apply 'add-hook hook))
238 (highlight-indentation-redraw-all-windows 'highlight-indentation-current-column-overlay
1abc633 @antonj Clean
authored
239 'highlight-indentation-current-column-put-overlays-region)))
5f4736a @antonj Function -> Minor mode
authored
240
241 (provide 'highlight-indentation)
d751d12 @antonj Incorporate hindent-mode @krestenkrab + name changes and refactor, bu…
authored
242
9ef754c @antonj init
authored
243 ;;; highlight-indentation.el ends here
Something went wrong with that request. Please try again.