1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
3 ;; Copyright (C) 2014 Artur Malabarba <bruce.connor.am@gmail.com>
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
6 ;; URL: http://github.com/Bruce-Connor/aggressive-indent-mode
8 ;; Package-Requires: ((emacs "24.1") (names "0.5") (cl-lib "0.5"))
9 ;; Keywords: indent lisp maint tools
10 ;; Prefix: aggressive-indent
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
20 ;; `aggressive-indent-mode' is a minor mode that keeps your code always
21 ;; indented. It reindents after every command, making it more reliable
22 ;; than `electric-indent-mode'.
24 ;; ### Instructions ###
26 ;; This package is available fom Melpa, you may install it by calling
28 ;; M-x package-install RET aggressive-indent
30 ;; Then activate it with
32 ;; (add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
33 ;; (add-hook 'css-mode-hook #'aggressive-indent-mode)
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:
39 ;; (global-aggressive-indent-mode 1)
40 ;; (add-to-list 'aggressive-indent-excluded-modes 'html-mode)
42 ;; ### Manual Installation ###
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
47 ;; (require 'aggressive-indent)
53 ;; This package is available fom Melpa, you may install it by calling
54 ;; M-x package-install RET aggressive-indent.
56 ;; Then activate it with
57 ;; (add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
59 ;; You can also use an equivalent hook for another mode,
60 ;; `aggressive-indent' is not exclusive to emacs-lisp code.
62 ;; Alternatively, you can download it manually, place it in your
63 ;; `load-path' and require it with
65 ;; (require 'aggressive-indent)
69 ;; This file is NOT part of GNU Emacs.
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.
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.
83 ;; 0.3 - 2014/10/23 - Implement a smarter engine for non-lisp modes.
84 ;; 0.2 - 2014/10/20 - Reactivate `electric-indent-mode'.
85 ;; 0.2 - 2014/10/19 - Add variable `aggressive-indent-dont-indent-if', so the user can prevent indentation.
86 ;; 0.1 - 2014/10/15 - Release.
93 (define-namespace aggressive-indent- :group indent
95 (defconst version "0.3.1" "Version of the aggressive-indent.el package.")
97 "Opens github issues page in a web browser. Please send any bugs you find.
98 Please include your emacs and aggressive-indent versions."
100 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
101 Please include this in your report!"
102 version emacs-version)
103 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
106 ;;; Start of actual Code:
107 (defcustom excluded-modes
108 '(text-mode tabulated-list-mode special-mode
109 minibuffer-inactive-mode
110 yaml-mode jabber-chat-mode)
111 "Modes in which `aggressive-indent-mode' should not be activated.
112 This variable is only used if `global-aggressive-indent-mode' is
113 active. If the minor mode is turned on with the local command,
114 `aggressive-indent-mode', this variable is ignored."
115 :type '(repeat symbol)
116 :package-version '(aggressive-indent . "0.2"))
118 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
119 "Commands after which indentation will NOT be performed.
120 Aggressive indentation could break things like `undo' by locking
121 the user in a loop, so this variable is used to control which
122 commands will NOT be followed by a re-indent."
123 :type '(repeat symbol)
124 :package-version '(aggressive-indent . "0.1"))
126 (defcustom comments-too nil
127 "If non-nil, aggressively indent in comments as well."
129 :package-version '(aggressive-indent . "0.3"))
131 (defvar -internal-dont-indent-if
132 '((memq this-command aggressive-indent-protected-commands)
135 (null (buffer-modified-p))
136 (string-match "\\`[[:blank:]]*\n?\\'" (thing-at-point 'line))
137 (and (not aggressive-indent-comments-too)
138 (aggressive-indent--in-comment-p)))
139 "List of forms which prevent indentation when they evaluate to non-nil.
140 This is for internal use only. For user customization, use
141 `aggressive-indent-dont-indent-if' instead.")
143 (defcustom modes-to-prefer-defun '(emacs-lisp-mode lisp-mode scheme-mode)
144 "List of major-modes in which indenting defun is preferred.
145 Add here any major modes with very good definitions of
146 `end-of-defun' and `beginning-of-defun', or modes which bug out
147 if you have `after-change-functions' (such as paredit).
149 If current major mode is derived from one of these,
150 `aggressive-indent-mode' will call
151 `aggressive-indent-indent-defun' after every command. Otherwise,
152 it will call `aggressive-indent-indent-region-and-on' after every
154 :type '(repeat symbol)
155 :package-version '(aggressive-indent . "0.3"))
157 (eval-after-load 'yasnippet
158 '(when (boundp 'yas--active-field-overlay)
159 (add-to-list 'aggressive-indent--internal-dont-indent-if
161 (overlayp yas--active-field-overlay)
162 (overlay-end yas--active-field-overlay))
164 (eval-after-load 'company
165 '(when (boundp 'company-candidates)
166 (add-to-list 'aggressive-indent--internal-dont-indent-if
167 'company-candidates)))
168 (eval-after-load 'auto-complete
169 '(when (boundp 'ac-completing)
170 (add-to-list 'aggressive-indent--internal-dont-indent-if
173 (eval-after-load 'css-mode
176 (lambda () (unless defun-prompt-regexp
177 (setq-local defun-prompt-regexp "^[^[:blank:]].*")))))
179 (defcustom dont-indent-if '()
180 "List of variables and functions to prevent aggressive indenting.
181 This variable is a list where each element is a lisp form.
182 As long as any one of these forms returns non-nil,
183 aggressive-indent will not perform any indentation.
185 See `aggressive-indent--internal-dont-indent-if' for usage examples."
187 :group 'aggressive-indent
188 :package-version '(aggressive-indent . "0.2"))
190 (defvar -error-message
191 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
192 "Error message thrown by `aggressive-indent-dont-indent-if'.")
194 (defvar -has-errored nil
195 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
196 This is used to prevent an infinite error loop on the user.")
198 (defun -run-user-hooks ()
199 "Safely run forms in `aggressive-indent-dont-indent-if'.
200 If any of them errors out, we only report it once until it stops
204 (prog1 (eval (cons 'or dont-indent-if))
205 (setq -has-errored nil))
208 (setq -has-errored t)
209 (message -error-message er))))))
211 (defmacro -do-softly (&rest body)
212 "Execute body unobstrusively.
213 This means: do nothing if mark is active (to avoid deactivaing
214 it), or if buffer is not modified (to avoid creating accidental
215 modifications), or if any of the forms in
216 `aggressive-indent-dont-indent-if' evaluates to non-nil.
218 Also, never throw errors nor messages.
219 Meant for use in functions which go in hooks."
221 `(unless (or (run-hook-wrapped
222 'aggressive-indent--internal-dont-indent-if
224 (aggressive-indent--run-user-hooks))
226 (cl-letf (((symbol-function 'message) #'ignore))
230 (defun indent-defun ()
231 "Indent current defun.
232 Throw an error if parentheses are unbalanced."
234 (let ((p (point-marker)))
235 (set-marker-insertion-type p t)
237 (save-excursion (beginning-of-defun 1) (point))
238 (save-excursion (end-of-defun 1) (point)))
241 (defun -softly-indent-defun ()
242 "Indent current defun unobstrusively.
243 Like `aggressive-indent-indent-defun', but wrapped in a
244 `aggressive-indent--do-softly'."
245 (unless (or (run-hook-wrapped
246 'aggressive-indent--internal-dont-indent-if
248 (aggressive-indent--run-user-hooks))
250 (cl-letf (((symbol-function 'message) #'ignore))
254 (defun indent-region-and-on (l r)
255 "Indent region between L and R, and then some.
256 Call `indent-region' between L and R, and then keep indenting
257 until nothing more happens."
259 (let ((p (point-marker))
260 was-begining-of-line)
261 (set-marker-insertion-type p t)
263 (setq was-begining-of-line
264 (= r (line-beginning-position)))
265 ;; If L is at the end of a line, skip that line.
268 (when (= l (line-end-position))
270 ;; Indent the affected region.
271 (unless (= l r) (indent-region l r))
272 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
273 (when was-begining-of-line
274 (indent-according-to-mode))
275 ;; And then we indent each following line until nothing happens.
277 (while (/= (progn (skip-chars-forward "[:blank:]\n")
279 (progn (indent-according-to-mode)
284 (defun -softly-indent-region-and-on (l r &rest _)
285 "Indent current defun unobstrusively.
286 Like `aggressive-indent-indent-region-and-on', but wrapped in a
287 `aggressive-indent--do-softly'."
288 (unless (or (run-hook-wrapped
289 'aggressive-indent--internal-dont-indent-if
291 (aggressive-indent--run-user-hooks))
293 (cl-letf (((symbol-function 'message) #'ignore))
294 (indent-region-and-on l r)))))
296 (defvar changed-list-right nil
297 "List of right limit of regions changed in the last command loop.")
299 (defvar changed-list-left nil
300 "List of left limit of regions changed in the last command loop.")
302 (defun -indent-if-changed ()
303 "Indent any region that changed in the last command loop."
304 (let ((inhibit-modification-hooks t))
305 (when changed-list-left
306 (-softly-indent-region-and-on
307 (apply #'min changed-list-left)
308 (apply #'max changed-list-right))
309 (setq changed-list-left nil
310 changed-list-right nil))))
312 (defun -keep-track-of-changes (l r &rest _)
313 "Store the limits of each change that happens in the buffer."
314 (push l changed-list-left)
315 (push r changed-list-right))
317 (defun -in-comment-p ()
318 "Return non-nil if point is inside a comment.
319 Assumes that the syntax table is sufficient to find comments."
320 (nth 4 (syntax-ppss)))
325 (define-minor-mode mode nil nil " =>"
326 '(("\C-c\C-q" . aggressive-indent-indent-defun))
328 (if (and global-aggressive-indent-mode
329 (or (cl-member-if #'derived-mode-p excluded-modes)
332 (when (fboundp 'electric-indent-local-mode)
333 (electric-indent-local-mode 1))
334 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
335 (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
336 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
337 (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
339 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
340 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
341 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
344 (define-globalized-minor-mode global-aggressive-indent-mode
348 (defalias 'aggressive-indent-global-mode
349 #'global-aggressive-indent-mode)
352 (provide 'aggressive-indent)
353 ;;; aggressive-indent.el ends here.