]> code.delx.au - gnu-emacs-elpa/blob - aggressive-indent.el
Only track changes if the mode is active
[gnu-emacs-elpa] / aggressive-indent.el
1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
7 ;; Version: 1.3
8 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
9 ;; Keywords: indent lisp maint tools
10 ;; Prefix: aggressive-indent
11 ;; Separator: -
12
13 ;;; Commentary:
14 ;;
15 ;; `electric-indent-mode' is enough to keep your code nicely aligned when
16 ;; all you do is type. However, once you start shifting blocks around,
17 ;; transposing lines, or slurping and barfing sexps, indentation is bound
18 ;; to go wrong.
19 ;;
20 ;; `aggressive-indent-mode' is a minor mode that keeps your code always
21 ;; indented. It reindents after every change, making it more reliable
22 ;; than `electric-indent-mode'.
23 ;;
24 ;; ### Instructions ###
25 ;;
26 ;; This package is available fom Melpa, you may install it by calling
27 ;;
28 ;; M-x package-install RET aggressive-indent
29 ;;
30 ;; Then activate it with
31 ;;
32 ;; (add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
33 ;; (add-hook 'css-mode-hook #'aggressive-indent-mode)
34 ;;
35 ;; You can use this hook on any mode you want, `aggressive-indent' is not
36 ;; exclusive to emacs-lisp code. In fact, if you want to turn it on for
37 ;; every programming mode, you can do something like:
38 ;;
39 ;; (global-aggressive-indent-mode 1)
40 ;; (add-to-list 'aggressive-indent-excluded-modes 'html-mode)
41 ;;
42 ;; ### Manual Installation ###
43 ;;
44 ;; If you don't want to install from Melpa, you can download it manually,
45 ;; place it in your `load-path' and require it with
46 ;;
47 ;; (require 'aggressive-indent)
48
49 ;;; Instructions:
50 ;;
51 ;; INSTALLATION
52 ;;
53 ;; This package is available fom Melpa, you may install it by calling
54 ;; M-x package-install RET aggressive-indent.
55 ;;
56 ;; Then activate it with
57 ;; (add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
58 ;;
59 ;; You can also use an equivalent hook for another mode,
60 ;; `aggressive-indent' is not exclusive to emacs-lisp code.
61 ;;
62 ;; Alternatively, you can download it manually, place it in your
63 ;; `load-path' and require it with
64 ;;
65 ;; (require 'aggressive-indent)
66
67 ;;; License:
68 ;;
69 ;; This file is NOT part of GNU Emacs.
70 ;;
71 ;; This program is free software; you can redistribute it and/or
72 ;; modify it under the terms of the GNU General Public License
73 ;; as published by the Free Software Foundation; either version 2
74 ;; of the License, or (at your option) any later version.
75 ;;
76 ;; This program is distributed in the hope that it will be useful,
77 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
78 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 ;; GNU General Public License for more details.
80 ;;
81
82 ;;; Change Log:
83 ;; 0.3.1 - 2014/10/30 - Define new delete-backward bound to backspace.
84 ;; 0.3 - 2014/10/23 - Implement a smarter engine for non-lisp modes.
85 ;; 0.2 - 2014/10/20 - Reactivate `electric-indent-mode'.
86 ;; 0.2 - 2014/10/19 - Add variable `aggressive-indent-dont-indent-if', so the user can prevent indentation.
87 ;; 0.1 - 2014/10/15 - Release.
88 ;;; Code:
89
90 (require 'cl-lib)
91
92 (defgroup aggressive-indent nil
93 "Customization group for aggressive-indent."
94 :prefix "aggressive-indent-"
95 :group 'indent)
96
97 (defun aggressive-indent-bug-report ()
98 "Opens github issues page in a web browser. Please send any bugs you find.
99 Please include your Emacs and `aggressive-indent' versions."
100 (interactive)
101 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
102 Please include this in your report!"
103 (eval-when-compile
104 (require 'lisp-mnt)
105 (require 'find-func)
106 (lm-version (find-library-name "aggressive-indent")))
107 emacs-version)
108 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
109
110 ;;; Configuring indentarion
111 (defcustom aggressive-indent-dont-electric-modes '(ruby-mode)
112 "List of major-modes where `electric-indent' should be disabled."
113 :type '(choice
114 (const :tag "Never use `electric-indent-mode'." t)
115 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
116 :package-version '(aggressive-indent . "0.3.1"))
117
118 (defcustom aggressive-indent-excluded-modes
119 '(
120 bibtex-mode
121 cider-repl-mode
122 coffee-mode
123 comint-mode
124 conf-mode
125 Custom-mode
126 diff-mode
127 doc-view-mode
128 dos-mode
129 erc-mode
130 jabber-chat-mode
131 haml-mode
132 haskell-mode
133 image-mode
134 makefile-mode
135 makefile-gmake-mode
136 minibuffer-inactive-mode
137 netcmd-mode
138 python-mode
139 sass-mode
140 slim-mode
141 special-mode
142 shell-mode
143 snippet-mode
144 eshell-mode
145 tabulated-list-mode
146 term-mode
147 TeX-output-mode
148 text-mode
149 yaml-mode
150 )
151 "Modes in which `aggressive-indent-mode' should not be activated.
152 This variable is only used if `global-aggressive-indent-mode' is
153 active. If the minor mode is turned on with the local command,
154 `aggressive-indent-mode', this variable is ignored."
155 :type '(repeat symbol)
156 :package-version '(aggressive-indent . "0.3.1"))
157
158 (defcustom aggressive-indent-protected-commands '(undo undo-tree-undo undo-tree-redo)
159 "Commands after which indentation will NOT be performed.
160 Aggressive indentation could break things like `undo' by locking
161 the user in a loop, so this variable is used to control which
162 commands will NOT be followed by a re-indent."
163 :type '(repeat symbol)
164 :package-version '(aggressive-indent . "0.1"))
165
166 (defcustom aggressive-indent-comments-too nil
167 "If non-nil, aggressively indent in comments as well."
168 :type 'boolean
169 :package-version '(aggressive-indent . "0.3"))
170
171 (defcustom aggressive-indent-modes-to-prefer-defun
172 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
173 "List of major-modes in which indenting defun is preferred.
174 Add here any major modes with very good definitions of
175 `end-of-defun' and `beginning-of-defun', or modes which bug out
176 if you have `after-change-functions' (such as paredit).
177
178 If current major mode is derived from one of these,
179 `aggressive-indent' will call `aggressive-indent-indent-defun'
180 after every command. Otherwise, it will call
181 `aggressive-indent-indent-region-and-on' after every buffer
182 change."
183 :type '(repeat symbol)
184 :package-version '(aggressive-indent . "0.3"))
185
186 ;;; Preventing indentation
187 (defvar aggressive-indent--internal-dont-indent-if
188 '((memq this-command aggressive-indent-protected-commands)
189 (region-active-p)
190 buffer-read-only
191 undo-in-progress
192 (null (buffer-modified-p))
193 (and (boundp 'smerge-mode) smerge-mode)
194 (let ((line (thing-at-point 'line)))
195 (when (stringp line)
196 (or (string-match "\\`[[:blank:]]*\n?\\'" line)
197 ;; If the user is starting to type a comment.
198 (and (stringp comment-start)
199 (string-match (concat "\\`[[:blank:]]*"
200 (substring comment-start 0 1)
201 "[[:blank:]]*$")
202 line)))))
203 (let ((sp (syntax-ppss)))
204 ;; Comments.
205 (or (and (not aggressive-indent-comments-too) (elt sp 4))
206 ;; Strings.
207 (elt sp 3))))
208 "List of forms which prevent indentation when they evaluate to non-nil.
209 This is for internal use only. For user customization, use
210 `aggressive-indent-dont-indent-if' instead.")
211
212 (eval-after-load 'yasnippet
213 '(when (boundp 'yas--active-field-overlay)
214 (add-to-list 'aggressive-indent--internal-dont-indent-if
215 '(and
216 (overlayp yas--active-field-overlay)
217 (overlay-end yas--active-field-overlay))
218 'append)))
219 (eval-after-load 'company
220 '(when (boundp 'company-candidates)
221 (add-to-list 'aggressive-indent--internal-dont-indent-if
222 'company-candidates)))
223 (eval-after-load 'auto-complete
224 '(when (boundp 'ac-completing)
225 (add-to-list 'aggressive-indent--internal-dont-indent-if
226 'ac-completing)))
227 (eval-after-load 'multiple-cursors-core
228 '(when (boundp 'multiple-cursors-mode)
229 (add-to-list 'aggressive-indent--internal-dont-indent-if
230 'multiple-cursors-mode)))
231 (eval-after-load 'iedit
232 '(when (boundp 'iedit-mode)
233 (add-to-list 'aggressive-indent--internal-dont-indent-if
234 'iedit-mode)))
235 (eval-after-load 'coq
236 '(add-to-list 'aggressive-indent--internal-dont-indent-if
237 '(and (derived-mode-p 'coq-mode)
238 (not (string-match "\\.[[:space:]]*$"
239 (thing-at-point 'line))))))
240
241 (defcustom aggressive-indent-dont-indent-if '()
242 "List of variables and functions to prevent aggressive indenting.
243 This variable is a list where each element is a Lisp form.
244 As long as any one of these forms returns non-nil,
245 aggressive-indent will not perform any indentation.
246
247 See `aggressive-indent--internal-dont-indent-if' for usage examples."
248 :type '(repeat sexp)
249 :package-version '(aggressive-indent . "0.2"))
250
251 (defvar aggressive-indent--error-message "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
252 "Error message thrown by `aggressive-indent-dont-indent-if'.")
253
254 (defvar aggressive-indent--has-errored nil
255 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
256 This is used to prevent an infinite error loop on the user.")
257
258 (defun aggressive-indent--run-user-hooks ()
259 "Safely run forms in `aggressive-indent-dont-indent-if'.
260 If any of them errors out, we only report it once until it stops
261 erroring again."
262 (and aggressive-indent-dont-indent-if
263 (condition-case er
264 (prog1 (eval (cons 'or aggressive-indent-dont-indent-if))
265 (setq aggressive-indent--has-errored nil))
266 (error (unless aggressive-indent--has-errored
267 (setq aggressive-indent--has-errored t)
268 (message aggressive-indent--error-message er))))))
269
270 ;;; Indenting defun
271 ;;;###autoload
272 (defun aggressive-indent-indent-defun (&optional l r)
273 "Indent current defun.
274 Throw an error if parentheses are unbalanced.
275 If L and R are provided, use them for finding the start and end of defun."
276 (interactive)
277 (let ((p (point-marker)))
278 (set-marker-insertion-type p t)
279 (indent-region
280 (save-excursion
281 (when l (goto-char l))
282 (beginning-of-defun 1) (point))
283 (save-excursion
284 (when r (goto-char r))
285 (end-of-defun 1) (point)))
286 (goto-char p)))
287
288 (defun aggressive-indent--softly-indent-defun (&optional l r)
289 "Indent current defun unobstrusively.
290 Like `aggressive-indent-indent-defun', but without errors or
291 messages. L and R passed to `aggressive-indent-indent-defun'."
292 (cl-letf (((symbol-function 'message) #'ignore))
293 (ignore-errors (aggressive-indent-indent-defun l r))))
294
295 ;;; Indenting region
296 ;;;###autoload
297 (defun aggressive-indent-indent-region-and-on (l r)
298 "Indent region between L and R, and then some.
299 Call `indent-region' between L and R, and then keep indenting
300 until nothing more happens."
301 (interactive "r")
302 (let ((p (point-marker))
303 was-begining-of-line)
304 (set-marker-insertion-type p t)
305 (unwind-protect
306 (progn
307 (goto-char r)
308 (setq was-begining-of-line
309 (= r (line-beginning-position)))
310 ;; If L is at the end of a line, skip that line.
311 (unless (= l r)
312 (goto-char l)
313 (when (= l (line-end-position))
314 (cl-incf l)))
315 ;; Indent the affected region.
316 (unless (= l r) (indent-region l r))
317 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
318 (when was-begining-of-line
319 (indent-according-to-mode))
320 ;; And then we indent each following line until nothing happens.
321 (forward-line 1)
322 (skip-chars-forward "[:blank:]\n")
323 (let* ((eod (ignore-errors
324 (save-excursion (end-of-defun)
325 (point-marker))))
326 (point-limit (if (and eod (< (point) eod))
327 eod (point-max-marker))))
328 (while (and (null (eobp))
329 (let ((op (point))
330 (np (progn (indent-according-to-mode)
331 (point))))
332 ;; As long as we're indenting things to the
333 ;; left, keep indenting.
334 (or (< np op)
335 ;; If we're indenting to the right, or
336 ;; not at all, stop at the limit.
337 (< (point) point-limit))))
338 (forward-line 1)
339 (skip-chars-forward "[:blank:]\n"))))
340 (goto-char p))))
341
342 (defun aggressive-indent--softly-indent-region-and-on (l r &rest _)
343 "Indent region between L and R, and a bit more.
344 Like `aggressive-indent-indent-region-and-on', but without errors
345 or messages."
346 (cl-letf (((symbol-function 'message) #'ignore))
347 (ignore-errors (aggressive-indent-indent-region-and-on l r))))
348
349 ;;; Tracking changes
350 (defvar aggressive-indent--changed-list nil
351 "List of (left right) limit of regions changed in the last command loop.")
352
353 (defun aggressive-indent--indent-if-changed ()
354 "Indent any region that changed in the last command loop."
355 (when aggressive-indent--changed-list
356 (unless (or (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval)
357 (aggressive-indent--run-user-hooks))
358 (while-no-input
359 (let ((inhibit-modification-hooks t)
360 (inhibit-point-motion-hooks t)
361 (indent-function
362 (if (cl-member-if #'derived-mode-p aggressive-indent-modes-to-prefer-defun)
363 #'aggressive-indent--softly-indent-defun #'aggressive-indent--softly-indent-region-and-on)))
364 (while aggressive-indent--changed-list
365 (apply indent-function (car aggressive-indent--changed-list))
366 (setq aggressive-indent--changed-list
367 (cdr aggressive-indent--changed-list))))))))
368
369 (defun aggressive-indent--keep-track-of-changes (l r &rest _)
370 "Store the limits (L and R) of each change in the buffer."
371 (when aggressive-indent-mode
372 (push (list l r) aggressive-indent--changed-list)))
373
374 ;;; Minor modes
375 ;;;###autoload
376 (define-minor-mode aggressive-indent-mode
377 nil nil " =>"
378 '(("\ 3\11" . aggressive-indent-indent-defun)
379 ([backspace]
380 menu-item "maybe-delete-indentation" ignore :filter
381 (lambda (&optional _)
382 (when (and (looking-back "^[[:blank:]]+")
383 ;; Wherever we don't want to indent, we probably also
384 ;; want the default backspace behavior.
385 (not (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval))
386 (not (aggressive-indent--run-user-hooks)))
387 #'delete-indentation))))
388 (if aggressive-indent-mode
389 (if (and global-aggressive-indent-mode
390 (or (cl-member-if #'derived-mode-p aggressive-indent-excluded-modes)
391 (memq major-mode '(text-mode fundamental-mode))
392 buffer-read-only))
393 (aggressive-indent-mode -1)
394 ;; Should electric indent be ON or OFF?
395 (if (or (eq aggressive-indent-dont-electric-modes t)
396 (cl-member-if #'derived-mode-p aggressive-indent-dont-electric-modes))
397 (aggressive-indent--local-electric nil)
398 (aggressive-indent--local-electric t))
399 (add-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes nil 'local)
400 (add-hook 'post-command-hook #'aggressive-indent--indent-if-changed nil 'local))
401 ;; Clean the hooks
402 (remove-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes 'local)
403 (remove-hook 'post-command-hook #'aggressive-indent--indent-if-changed 'local)
404 (remove-hook 'post-command-hook #'aggressive-indent--softly-indent-defun 'local)))
405
406 (defun aggressive-indent--local-electric (on)
407 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
408 (if (fboundp 'electric-indent-local-mode)
409 (electric-indent-local-mode (if on 1 -1))
410 (set (make-local-variable 'electric-indent-mode) on)))
411
412 ;;;###autoload
413 (define-globalized-minor-mode global-aggressive-indent-mode
414 aggressive-indent-mode aggressive-indent-mode)
415
416 ;;;###autoload
417 (defalias 'aggressive-indent-global-mode
418 #'global-aggressive-indent-mode)
419
420 (provide 'aggressive-indent)
421 ;;; aggressive-indent.el ends here