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.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.
93 (defmacro aggressive-indent--do-softly (&rest body)
94 "Execute body unobstrusively.
96 1. Do nothing in several situations, specified by
97 `aggressive-indent-dont-indent-if' and
98 `aggressive-indent--internal-dont-indent-if'.
99 2. Silence all messages.
100 3. Never throw errors.
101 Meant for use in functions which go in hooks."
103 `(unless (or (run-hook-wrapped
104 'aggressive-indent--internal-dont-indent-if
106 (aggressive-indent--run-user-hooks))
107 (cl-letf (((symbol-function 'message) #'ignore))
108 (ignore-errors ,@body))))
111 (define-namespace aggressive-indent- :group indent
113 (defconst version "0.3.2" "Version of the aggressive-indent.el package.")
115 "Opens github issues page in a web browser. Please send any bugs you find.
116 Please include your emacs and aggressive-indent versions."
118 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
119 Please include this in your report!"
120 version emacs-version)
121 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
124 ;;; Start of actual Code:
125 (defcustom dont-electric-modes '(ruby-mode)
126 "List of major-modes where `electric-indent-mode' should be disabled."
128 (const :tag "Never use `electric-indent-mode'." t)
129 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
130 :package-version '(aggressive-indent . "0.3.1"))
132 (defcustom excluded-modes
145 minibuffer-inactive-mode
160 "Modes in which `aggressive-indent-mode' should not be activated.
161 This variable is only used if `global-aggressive-indent-mode' is
162 active. If the minor mode is turned on with the local command,
163 `aggressive-indent-mode', this variable is ignored."
164 :type '(repeat symbol)
165 :package-version '(aggressive-indent . "0.3.1"))
167 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
168 "Commands after which indentation will NOT be performed.
169 Aggressive indentation could break things like `undo' by locking
170 the user in a loop, so this variable is used to control which
171 commands will NOT be followed by a re-indent."
172 :type '(repeat symbol)
173 :package-version '(aggressive-indent . "0.1"))
175 (defcustom comments-too nil
176 "If non-nil, aggressively indent in comments as well."
178 :package-version '(aggressive-indent . "0.3"))
180 (defvar -internal-dont-indent-if
181 '((memq this-command aggressive-indent-protected-commands)
184 (null (buffer-modified-p))
185 (and (boundp 'smerge-mode) smerge-mode)
186 (string-match "\\`[[:blank:]]*\n?\\'" (or (thing-at-point 'line) ""))
187 (and (not aggressive-indent-comments-too)
188 (aggressive-indent--in-comment-p))
189 (aggressive-indent--in-string-p))
190 "List of forms which prevent indentation when they evaluate to non-nil.
191 This is for internal use only. For user customization, use
192 `aggressive-indent-dont-indent-if' instead.")
194 (defcustom modes-to-prefer-defun '(emacs-lisp-mode lisp-mode scheme-mode)
195 "List of major-modes in which indenting defun is preferred.
196 Add here any major modes with very good definitions of
197 `end-of-defun' and `beginning-of-defun', or modes which bug out
198 if you have `after-change-functions' (such as paredit).
200 If current major mode is derived from one of these,
201 `aggressive-indent-mode' will call
202 `aggressive-indent-indent-defun' after every command. Otherwise,
203 it will call `aggressive-indent-indent-region-and-on' after every
205 :type '(repeat symbol)
206 :package-version '(aggressive-indent . "0.3"))
208 (eval-after-load 'yasnippet
209 '(when (boundp 'yas--active-field-overlay)
210 (add-to-list 'aggressive-indent--internal-dont-indent-if
212 (overlayp yas--active-field-overlay)
213 (overlay-end yas--active-field-overlay))
215 (eval-after-load 'company
216 '(when (boundp 'company-candidates)
217 (add-to-list 'aggressive-indent--internal-dont-indent-if
218 'company-candidates)))
219 (eval-after-load 'auto-complete
220 '(when (boundp 'ac-completing)
221 (add-to-list 'aggressive-indent--internal-dont-indent-if
224 (defcustom dont-indent-if '()
225 "List of variables and functions to prevent aggressive indenting.
226 This variable is a list where each element is a lisp form.
227 As long as any one of these forms returns non-nil,
228 aggressive-indent will not perform any indentation.
230 See `aggressive-indent--internal-dont-indent-if' for usage examples."
232 :group 'aggressive-indent
233 :package-version '(aggressive-indent . "0.2"))
235 (defvar -error-message
236 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
237 "Error message thrown by `aggressive-indent-dont-indent-if'.")
239 (defvar -has-errored nil
240 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
241 This is used to prevent an infinite error loop on the user.")
243 (defun -run-user-hooks ()
244 "Safely run forms in `aggressive-indent-dont-indent-if'.
245 If any of them errors out, we only report it once until it stops
249 (prog1 (eval (cons 'or dont-indent-if))
250 (setq -has-errored nil))
253 (setq -has-errored t)
254 (message -error-message er))))))
257 (defun indent-defun ()
258 "Indent current defun.
259 Throw an error if parentheses are unbalanced."
261 (let ((p (point-marker)))
262 (set-marker-insertion-type p t)
264 (save-excursion (beginning-of-defun 1) (point))
265 (save-excursion (end-of-defun 1) (point)))
268 (defun -softly-indent-defun ()
269 "Indent current defun unobstrusively.
270 Like `aggressive-indent-indent-defun', but wrapped in a
271 `aggressive-indent--do-softly'."
272 (unless (or (run-hook-wrapped
273 'aggressive-indent--internal-dont-indent-if
275 (aggressive-indent--run-user-hooks))
276 (cl-letf (((symbol-function 'message) #'ignore))
277 (ignore-errors (indent-defun)))))
280 (defun indent-region-and-on (l r)
281 "Indent region between L and R, and then some.
282 Call `indent-region' between L and R, and then keep indenting
283 until nothing more happens."
285 (let ((p (point-marker))
286 was-begining-of-line)
287 (set-marker-insertion-type p t)
291 (setq was-begining-of-line
292 (= r (line-beginning-position)))
293 ;; If L is at the end of a line, skip that line.
296 (when (= l (line-end-position))
298 ;; Indent the affected region.
299 (unless (= l r) (indent-region l r))
300 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
301 (when was-begining-of-line
302 (indent-according-to-mode))
303 ;; And then we indent each following line until nothing happens.
305 (while (and (null (eobp))
306 (/= (progn (skip-chars-forward "[:blank:]\n")
308 (progn (indent-according-to-mode)
313 (defun -softly-indent-region-and-on (l r &rest _)
314 "Indent current defun unobstrusively.
315 Like `aggressive-indent-indent-region-and-on', but wrapped in a
316 `aggressive-indent--do-softly'."
317 (unless (or (run-hook-wrapped
318 'aggressive-indent--internal-dont-indent-if
320 (aggressive-indent--run-user-hooks))
321 (cl-letf (((symbol-function 'message) #'ignore))
322 (ignore-errors (indent-region-and-on l r)))))
324 (defvar -changed-list-right nil
325 "List of right limit of regions changed in the last command loop.")
327 (defvar -changed-list-left nil
328 "List of left limit of regions changed in the last command loop.")
330 (defun -indent-if-changed ()
331 "Indent any region that changed in the last command loop."
332 (let ((inhibit-modification-hooks t))
333 (when -changed-list-left
334 (-softly-indent-region-and-on
335 (apply #'min -changed-list-left)
336 (apply #'max -changed-list-right))
337 (setq -changed-list-left nil
338 -changed-list-right nil))))
340 (defun -keep-track-of-changes (l r &rest _)
341 "Store the limits of each change that happens in the buffer."
342 (push l -changed-list-left)
343 (push r -changed-list-right))
345 (defun -in-comment-p ()
346 "Return non-nil if point is inside a comment.
347 Assumes that the syntax table is sufficient to find comments."
348 (nth 4 (syntax-ppss)))
350 (defun -in-string-p ()
351 "Return non-nil if point is inside a string.
352 Assumes that the syntax table is sufficient for recognizing
354 (nth 3 (syntax-ppss)))
359 (define-minor-mode mode nil nil " =>"
360 '(("
\ 3\11" . aggressive-indent-indent-defun)
361 ([backspace] menu-item "maybe-delete-indentation" ignore
362 :filter (lambda (&optional _)
363 (when (and (looking-back "^[[:blank:]]+")
364 ;; Wherever we don't want to indent, we probably also
365 ;; want the default backspace behavior.
366 (not (run-hook-wrapped
367 'aggressive-indent--internal-dont-indent-if
369 (not (aggressive-indent--run-user-hooks)))
370 #'delete-indentation))))
372 (if (and global-aggressive-indent-mode
373 (or (cl-member-if #'derived-mode-p excluded-modes)
374 (memq major-mode '(text-mode fundamental-mode))
377 ;; Should electric indent be ON or OFF?
378 (if (or (eq dont-electric-modes t)
379 (cl-member-if #'derived-mode-p dont-electric-modes))
380 (-local-electric nil)
382 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
383 (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
384 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
385 (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
387 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
388 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
389 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
391 (defun -local-electric (on)
392 "Turn `electric-indent-mode' on or off locally, as given by boolean ON."
393 (if (fboundp 'electric-indent-local-mode)
394 (electric-indent-local-mode (if on 1 -1))
395 (set (make-local-variable 'electric-indent-mode) on)))
398 (define-globalized-minor-mode global-aggressive-indent-mode
402 (defalias 'aggressive-indent-global-mode
403 #'global-aggressive-indent-mode)
406 (provide 'aggressive-indent)
407 ;;; aggressive-indent.el ends here.