1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented -*- lexical-binding:t -*-
3 ;; Copyright (C) 2014, 2015 Free Software Foundation, Inc
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
8 ;; Package-Requires: ((emacs "24.1") (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 change, 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 3
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.
92 (defgroup aggressive-indent nil
93 "Customization group for aggressive-indent."
94 :prefix "aggressive-indent-"
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."
101 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
102 Please include this in your report!"
108 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
110 (defvar aggressive-indent-mode)
112 ;;; Configuring indentarion
113 (defcustom aggressive-indent-dont-electric-modes '(ruby-mode)
114 "List of major-modes where `electric-indent' should be disabled."
116 (const :tag "Never use `electric-indent-mode'." t)
117 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
118 :package-version '(aggressive-indent . "0.3.1"))
120 (defcustom aggressive-indent-excluded-modes
138 minibuffer-inactive-mode
153 "Modes in which `aggressive-indent-mode' should not be activated.
154 This variable is only used if `global-aggressive-indent-mode' is
155 active. If the minor mode is turned on with the local command,
156 `aggressive-indent-mode', this variable is ignored."
157 :type '(repeat symbol)
158 :package-version '(aggressive-indent . "0.3.1"))
160 (defcustom aggressive-indent-protected-commands '(undo undo-tree-undo undo-tree-redo whitespace-cleanup)
161 "Commands after which indentation will NOT be performed.
162 Aggressive indentation could break things like `undo' by locking
163 the user in a loop, so this variable is used to control which
164 commands will NOT be followed by a re-indent."
165 :type '(repeat symbol)
166 :package-version '(aggressive-indent . "0.1"))
168 (defcustom aggressive-indent-comments-too nil
169 "If non-nil, aggressively indent in comments as well."
171 :package-version '(aggressive-indent . "0.3"))
173 (defcustom aggressive-indent-modes-to-prefer-defun
174 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
175 "List of major-modes in which indenting defun is preferred.
176 Add here any major modes with very good definitions of
177 `end-of-defun' and `beginning-of-defun', or modes which bug out
178 if you have `after-change-functions' (such as paredit).
180 If current major mode is derived from one of these,
181 `aggressive-indent' will call `aggressive-indent-indent-defun'
182 after every command. Otherwise, it will call
183 `aggressive-indent-indent-region-and-on' after every buffer
185 :type '(repeat symbol)
186 :package-version '(aggressive-indent . "0.3"))
188 ;;; Preventing indentation
189 (defvar aggressive-indent--internal-dont-indent-if
190 '((memq this-command aggressive-indent-protected-commands)
194 (null (buffer-modified-p))
195 (and (boundp 'smerge-mode) smerge-mode)
196 (let ((line (thing-at-point 'line)))
198 (or (string-match "\\`[[:blank:]]*\n?\\'" line)
199 ;; If the user is starting to type a comment.
200 (and (stringp comment-start)
201 (string-match (concat "\\`[[:blank:]]*"
202 (substring comment-start 0 1)
205 (let ((sp (syntax-ppss)))
207 (or (and (not aggressive-indent-comments-too) (elt sp 4))
210 "List of forms which prevent indentation when they evaluate to non-nil.
211 This is for internal use only. For user customization, use
212 `aggressive-indent-dont-indent-if' instead.")
214 (eval-after-load 'yasnippet
215 '(when (boundp 'yas--active-field-overlay)
216 (add-to-list 'aggressive-indent--internal-dont-indent-if
218 (overlayp yas--active-field-overlay)
219 (overlay-end yas--active-field-overlay))
221 (eval-after-load 'company
222 '(when (boundp 'company-candidates)
223 (add-to-list 'aggressive-indent--internal-dont-indent-if
224 'company-candidates)))
225 (eval-after-load 'auto-complete
226 '(when (boundp 'ac-completing)
227 (add-to-list 'aggressive-indent--internal-dont-indent-if
229 (eval-after-load 'multiple-cursors-core
230 '(when (boundp 'multiple-cursors-mode)
231 (add-to-list 'aggressive-indent--internal-dont-indent-if
232 'multiple-cursors-mode)))
233 (eval-after-load 'iedit
234 '(when (boundp 'iedit-mode)
235 (add-to-list 'aggressive-indent--internal-dont-indent-if
237 (eval-after-load 'coq
238 '(add-to-list 'aggressive-indent--internal-dont-indent-if
239 '(and (derived-mode-p 'coq-mode)
240 (not (string-match "\\.[[:space:]]*$"
241 (thing-at-point 'line))))))
243 (defcustom aggressive-indent-dont-indent-if '()
244 "List of variables and functions to prevent aggressive indenting.
245 This variable is a list where each element is a Lisp form.
246 As long as any one of these forms returns non-nil,
247 aggressive-indent will not perform any indentation.
249 See `aggressive-indent--internal-dont-indent-if' for usage examples."
251 :package-version '(aggressive-indent . "0.2"))
253 (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"
254 "Error message thrown by `aggressive-indent-dont-indent-if'.")
256 (defvar aggressive-indent--has-errored nil
257 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
258 This is used to prevent an infinite error loop on the user.")
260 (defun aggressive-indent--run-user-hooks ()
261 "Safely run forms in `aggressive-indent-dont-indent-if'.
262 If any of them errors out, we only report it once until it stops
264 (and aggressive-indent-dont-indent-if
266 (prog1 (eval (cons 'or aggressive-indent-dont-indent-if))
267 (setq aggressive-indent--has-errored nil))
268 (error (unless aggressive-indent--has-errored
269 (setq aggressive-indent--has-errored t)
270 (message aggressive-indent--error-message er))))))
274 (defun aggressive-indent-indent-defun (&optional l r)
275 "Indent current defun.
276 Throw an error if parentheses are unbalanced.
277 If L and R are provided, use them for finding the start and end of defun."
279 (let ((p (point-marker)))
280 (set-marker-insertion-type p t)
283 (when l (goto-char l))
284 (beginning-of-defun 1) (point))
286 (when r (goto-char r))
287 (end-of-defun 1) (point)))
290 (defun aggressive-indent--softly-indent-defun (&optional l r)
291 "Indent current defun unobstrusively.
292 Like `aggressive-indent-indent-defun', but without errors or
293 messages. L and R passed to `aggressive-indent-indent-defun'."
294 (cl-letf (((symbol-function 'message) #'ignore))
295 (ignore-errors (aggressive-indent-indent-defun l r))))
299 (defun aggressive-indent-indent-region-and-on (l r)
300 "Indent region between L and R, and then some.
301 Call `indent-region' between L and R, and then keep indenting
302 until nothing more happens."
304 (let ((p (point-marker))
305 was-begining-of-line)
306 (set-marker-insertion-type p t)
310 (when (= (char-before r) ?\n)
312 ;; If L is at the end of a line, skip that line.
314 (when (= (char-after l) ?\n)
316 ;; Indent the affected region.
318 (unless (= l r) (indent-region l r))
319 ;; And then we indent each following line until nothing happens.
321 (skip-chars-forward "[:blank:]\n\r\xc")
322 (let* ((eod (ignore-errors
323 (save-excursion (end-of-defun)
325 (point-limit (if (and eod (< (point) eod))
326 eod (point-max-marker))))
327 (while (and (null (eobp))
329 (np (progn (indent-according-to-mode)
331 ;; As long as we're indenting things to the
332 ;; left, keep indenting.
334 ;; If we're indenting to the right, or
335 ;; not at all, stop at the limit.
336 (< (point) point-limit))))
338 (skip-chars-forward "[:blank:]\n\r\xc"))))
341 (defun aggressive-indent--softly-indent-region-and-on (l r &rest _)
342 "Indent region between L and R, and a bit more.
343 Like `aggressive-indent-indent-region-and-on', but without errors
345 (cl-letf (((symbol-function 'message) #'ignore))
346 (ignore-errors (aggressive-indent-indent-region-and-on l r))))
349 (defvar aggressive-indent--changed-list nil
350 "List of (left right) limit of regions changed in the last command loop.")
351 (make-variable-buffer-local 'aggressive-indent--changed-list)
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))
359 (let ((inhibit-modification-hooks t)
360 (inhibit-point-motion-hooks t)
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))))))))
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)))
376 (define-minor-mode aggressive-indent-mode
378 '(("
\ 3\11" . aggressive-indent-indent-defun)
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))
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))
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)))
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)))
413 (define-globalized-minor-mode global-aggressive-indent-mode
414 aggressive-indent-mode aggressive-indent-mode)
417 (defalias 'aggressive-indent-global-mode
418 #'global-aggressive-indent-mode)
420 (provide 'aggressive-indent)
421 ;;; aggressive-indent.el ends here