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 <emacs@endlessparentheses.com>
6 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
8 ;; Package-Requires: ((emacs "24.1") (names "20150125.9") (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.
91 (eval-when-compile (require 'names))
94 (define-namespace aggressive-indent-
97 (defconst version (eval-when-compile
100 "Version of the aggressive-indent.el package.")
102 "Opens github issues page in a web browser. Please send any bugs you find.
103 Please include your Emacs and `aggressive-indent' versions."
105 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
106 Please include this in your report!"
107 version emacs-version)
108 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
111 ;;; Start of actual Code:
112 (defcustom dont-electric-modes '(ruby-mode)
113 "List of major-modes where `electric-indent' should be disabled."
115 (const :tag "Never use `electric-indent-mode'." t)
116 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
117 :package-version '(aggressive-indent . "0.3.1"))
119 (defcustom excluded-modes
136 minibuffer-inactive-mode
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"))
158 (defcustom 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"))
166 (defcustom comments-too nil
167 "If non-nil, aggressively indent in comments as well."
169 :package-version '(aggressive-indent . "0.3"))
171 (defvar -internal-dont-indent-if
172 '((memq this-command aggressive-indent-protected-commands)
175 (null (buffer-modified-p))
176 (and (boundp 'smerge-mode) smerge-mode)
177 (string-match "\\`[[:blank:]]*\n?\\'" (or (thing-at-point 'line) ""))
178 (let ((sp (syntax-ppss)))
180 (or (and (not aggressive-indent-comments-too) (elt sp 4))
183 "List of forms which prevent indentation when they evaluate to non-nil.
184 This is for internal use only. For user customization, use
185 `aggressive-indent-dont-indent-if' instead.")
187 (defcustom modes-to-prefer-defun
188 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
189 "List of major-modes in which indenting defun is preferred.
190 Add here any major modes with very good definitions of
191 `end-of-defun' and `beginning-of-defun', or modes which bug out
192 if you have `after-change-functions' (such as paredit).
194 If current major mode is derived from one of these,
195 `aggressive-indent' will call `aggressive-indent-indent-defun'
196 after every command. Otherwise, it will call
197 `aggressive-indent-indent-region-and-on' after every buffer
199 :type '(repeat symbol)
200 :package-version '(aggressive-indent . "0.3"))
202 (eval-after-load 'yasnippet
203 '(when (boundp 'yas--active-field-overlay)
204 (add-to-list 'aggressive-indent--internal-dont-indent-if
206 (overlayp yas--active-field-overlay)
207 (overlay-end yas--active-field-overlay))
209 (eval-after-load 'company
210 '(when (boundp 'company-candidates)
211 (add-to-list 'aggressive-indent--internal-dont-indent-if
212 'company-candidates)))
213 (eval-after-load 'auto-complete
214 '(when (boundp 'ac-completing)
215 (add-to-list 'aggressive-indent--internal-dont-indent-if
218 (defcustom dont-indent-if '()
219 "List of variables and functions to prevent aggressive indenting.
220 This variable is a list where each element is a Lisp form.
221 As long as any one of these forms returns non-nil,
222 aggressive-indent will not perform any indentation.
224 See `aggressive-indent--internal-dont-indent-if' for usage examples."
226 :group 'aggressive-indent
227 :package-version '(aggressive-indent . "0.2"))
229 (defvar -error-message
230 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
231 "Error message thrown by `aggressive-indent-dont-indent-if'.")
233 (defvar -has-errored nil
234 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
235 This is used to prevent an infinite error loop on the user.")
237 (defun -run-user-hooks ()
238 "Safely run forms in `aggressive-indent-dont-indent-if'.
239 If any of them errors out, we only report it once until it stops
243 (prog1 (eval (cons 'or dont-indent-if))
244 (setq -has-errored nil))
245 (error (unless -has-errored
246 (setq -has-errored t)
247 (message -error-message er))))))
250 (defun indent-defun ()
251 "Indent current defun.
252 Throw an error if parentheses are unbalanced."
254 (let ((p (point-marker)))
255 (set-marker-insertion-type p t)
257 (save-excursion (beginning-of-defun 1) (point))
258 (save-excursion (end-of-defun 1) (point)))
261 (defun -softly-indent-defun ()
262 "Indent current defun unobstrusively.
263 Like `aggressive-indent-indent-defun', but without errors or
265 (unless (or (run-hook-wrapped
266 'aggressive-indent--internal-dont-indent-if
268 (aggressive-indent--run-user-hooks))
269 (cl-letf (((symbol-function 'message) #'ignore))
270 (ignore-errors (indent-defun)))))
273 (defun indent-region-and-on (l r)
274 "Indent region between L and R, and then some.
275 Call `indent-region' between L and R, and then keep indenting
276 until nothing more happens."
278 (let ((p (point-marker))
279 was-begining-of-line)
280 (set-marker-insertion-type p t)
284 (setq was-begining-of-line
285 (= r (line-beginning-position)))
286 ;; If L is at the end of a line, skip that line.
289 (when (= l (line-end-position))
291 ;; Indent the affected region.
292 (unless (= l r) (indent-region l r))
293 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
294 (when was-begining-of-line
295 (indent-according-to-mode))
296 ;; And then we indent each following line until nothing happens.
298 (while (and (null (eobp))
299 (/= (progn (skip-chars-forward "[:blank:]\n")
301 (progn (indent-according-to-mode)
306 (defun -softly-indent-region-and-on (l r &rest _)
307 "Indent region between L and R, and a bit more.
308 Like `aggressive-indent-indent-region-and-on', but without errors
310 (unless (or (run-hook-wrapped
311 'aggressive-indent--internal-dont-indent-if
313 (aggressive-indent--run-user-hooks))
314 (cl-letf (((symbol-function 'message) #'ignore))
315 (ignore-errors (indent-region-and-on l r)))))
317 (defvar -changed-list-right nil
318 "List of right limit of regions changed in the last command loop.")
320 (defvar -changed-list-left nil
321 "List of left limit of regions changed in the last command loop.")
323 (defun -indent-if-changed ()
324 "Indent any region that changed in the last command loop."
325 (let ((inhibit-modification-hooks t))
326 (when -changed-list-left
327 (-softly-indent-region-and-on
328 (apply #'min -changed-list-left)
329 (apply #'max -changed-list-right))
330 (setq -changed-list-left nil
331 -changed-list-right nil))))
333 (defun -keep-track-of-changes (l r &rest _)
334 "Store the limits (L and R) of each change in the buffer."
335 (push l -changed-list-left)
336 (push r -changed-list-right))
341 (define-minor-mode mode
343 '(("
\ 3\11" . aggressive-indent-indent-defun)
344 ([backspace] menu-item "maybe-delete-indentation" ignore
345 :filter (lambda (&optional _)
346 (when (and (looking-back "^[[:blank:]]+")
347 ;; Wherever we don't want to indent, we probably also
348 ;; want the default backspace behavior.
349 (not (run-hook-wrapped
350 'aggressive-indent--internal-dont-indent-if
352 (not (aggressive-indent--run-user-hooks)))
353 #'delete-indentation))))
355 (if (and global-aggressive-indent-mode
356 (or (cl-member-if #'derived-mode-p excluded-modes)
357 (memq major-mode '(text-mode fundamental-mode))
360 ;; Should electric indent be ON or OFF?
361 (if (or (eq dont-electric-modes t)
362 (cl-member-if #'derived-mode-p dont-electric-modes))
363 (-local-electric nil)
365 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
366 (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
367 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
368 (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
370 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
371 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
372 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
374 (defun -local-electric (on)
375 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
376 (if (fboundp 'electric-indent-local-mode)
377 (electric-indent-local-mode (if on 1 -1))
378 (set (make-local-variable 'electric-indent-mode) on)))
381 (define-globalized-minor-mode global-aggressive-indent-mode
385 (defalias 'aggressive-indent-global-mode
386 #'global-aggressive-indent-mode)
389 (provide 'aggressive-indent)
390 ;;; aggressive-indent.el ends here