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") (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 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.
91 (eval-when-compile (require 'names))
94 (define-namespace aggressive-indent-
98 "Opens github issues page in a web browser. Please send any bugs you find.
99 Please include your Emacs and `aggressive-indent' versions."
102 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
103 Please include this in your report!"
104 (lm-version (find-library-name "aggressive-indent"))
106 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
109 ;;; Start of actual Code:
110 (defcustom dont-electric-modes '(ruby-mode)
111 "List of major-modes where `electric-indent' should be disabled."
113 (const :tag "Never use `electric-indent-mode'." t)
114 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
115 :package-version '(aggressive-indent . "0.3.1"))
117 (defcustom excluded-modes
135 minibuffer-inactive-mode
150 "Modes in which `aggressive-indent-mode' should not be activated.
151 This variable is only used if `global-aggressive-indent-mode' is
152 active. If the minor mode is turned on with the local command,
153 `aggressive-indent-mode', this variable is ignored."
154 :type '(repeat symbol)
155 :package-version '(aggressive-indent . "0.3.1"))
157 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
158 "Commands after which indentation will NOT be performed.
159 Aggressive indentation could break things like `undo' by locking
160 the user in a loop, so this variable is used to control which
161 commands will NOT be followed by a re-indent."
162 :type '(repeat symbol)
163 :package-version '(aggressive-indent . "0.1"))
165 (defcustom comments-too nil
166 "If non-nil, aggressively indent in comments as well."
168 :package-version '(aggressive-indent . "0.3"))
170 (defvar -internal-dont-indent-if
171 '((memq this-command aggressive-indent-protected-commands)
175 (null (buffer-modified-p))
176 (and (boundp 'smerge-mode) smerge-mode)
177 (let ((line (thing-at-point 'line)))
179 (or (string-match "\\`[[:blank:]]*\n?\\'" line)
180 ;; If the user is starting to type a comment.
181 (and (stringp comment-start)
182 (string-match (concat "\\`[[:blank:]]*"
183 (substring comment-start 0 1)
186 (let ((sp (syntax-ppss)))
188 (or (and (not aggressive-indent-comments-too) (elt sp 4))
191 "List of forms which prevent indentation when they evaluate to non-nil.
192 This is for internal use only. For user customization, use
193 `aggressive-indent-dont-indent-if' instead.")
195 (defcustom modes-to-prefer-defun
196 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
197 "List of major-modes in which indenting defun is preferred.
198 Add here any major modes with very good definitions of
199 `end-of-defun' and `beginning-of-defun', or modes which bug out
200 if you have `after-change-functions' (such as paredit).
202 If current major mode is derived from one of these,
203 `aggressive-indent' will call `aggressive-indent-indent-defun'
204 after every command. Otherwise, it will call
205 `aggressive-indent-indent-region-and-on' after every buffer
207 :type '(repeat symbol)
208 :package-version '(aggressive-indent . "0.3"))
210 (eval-after-load 'yasnippet
211 '(when (boundp 'yas--active-field-overlay)
212 (add-to-list 'aggressive-indent--internal-dont-indent-if
214 (overlayp yas--active-field-overlay)
215 (overlay-end yas--active-field-overlay))
217 (eval-after-load 'company
218 '(when (boundp 'company-candidates)
219 (add-to-list 'aggressive-indent--internal-dont-indent-if
220 'company-candidates)))
221 (eval-after-load 'auto-complete
222 '(when (boundp 'ac-completing)
223 (add-to-list 'aggressive-indent--internal-dont-indent-if
225 (eval-after-load 'multiple-cursors-core
226 '(when (boundp 'multiple-cursors-mode)
227 (add-to-list 'aggressive-indent--internal-dont-indent-if
228 'multiple-cursors-mode)))
229 (eval-after-load 'iedit
230 '(when (boundp 'iedit-mode)
231 (add-to-list 'aggressive-indent--internal-dont-indent-if
233 (eval-after-load 'coq
234 '(add-to-list 'aggressive-indent--internal-dont-indent-if
235 '(and (derived-mode-p 'coq-mode)
236 (not (string-match "\\.[[:space:]]*$"
237 (thing-at-point 'line))))))
239 (defcustom dont-indent-if '()
240 "List of variables and functions to prevent aggressive indenting.
241 This variable is a list where each element is a Lisp form.
242 As long as any one of these forms returns non-nil,
243 aggressive-indent will not perform any indentation.
245 See `aggressive-indent--internal-dont-indent-if' for usage examples."
247 :group 'aggressive-indent
248 :package-version '(aggressive-indent . "0.2"))
250 (defvar -error-message
251 "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'.")
254 (defvar -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.")
258 (defun -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
264 (prog1 (eval (cons 'or dont-indent-if))
265 (setq -has-errored nil))
266 (error (unless -has-errored
267 (setq -has-errored t)
268 (message -error-message er))))))
272 (defun 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."
277 (let ((p (point-marker)))
278 (set-marker-insertion-type p t)
281 (when l (goto-char l))
282 (beginning-of-defun 1) (point))
284 (when r (goto-char r))
285 (end-of-defun 1) (point)))
288 (defun -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 (indent-defun l r))))
296 (defun indent-region-and-on (l r)
297 "Indent region between L and R, and then some.
298 Call `indent-region' between L and R, and then keep indenting
299 until nothing more happens."
301 (let ((p (point-marker))
302 was-begining-of-line)
303 (set-marker-insertion-type p t)
307 (setq was-begining-of-line
308 (= r (line-beginning-position)))
309 ;; If L is at the end of a line, skip that line.
312 (when (= l (line-end-position))
314 ;; Indent the affected region.
315 (unless (= l r) (indent-region l r))
316 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
317 (when was-begining-of-line
318 (indent-according-to-mode))
319 ;; And then we indent each following line until nothing happens.
321 (skip-chars-forward "[:blank:]\n")
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"))))
341 (defun -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 (indent-region-and-on l r))))
348 (defvar -changed-list nil
349 "List of (left right) limit of regions changed in the last command loop.")
351 (defun -indent-if-changed ()
352 "Indent any region that changed in the last command loop."
354 (unless (or (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval)
355 (aggressive-indent--run-user-hooks))
357 (let ((inhibit-modification-hooks t)
358 (inhibit-point-motion-hooks t)
360 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
361 #'-softly-indent-defun
362 #'-softly-indent-region-and-on)))
364 (apply indent-function (car -changed-list))
365 (setq -changed-list (cdr -changed-list))))))))
367 (defun -keep-track-of-changes (l r &rest _)
368 "Store the limits (L and R) of each change in the buffer."
369 (push (list l r) -changed-list))
374 (define-minor-mode mode
376 '(("
\ 3\11" . aggressive-indent-indent-defun)
377 ([backspace] menu-item "maybe-delete-indentation" ignore
378 :filter (lambda (&optional _)
379 (when (and (looking-back "^[[:blank:]]+")
380 ;; Wherever we don't want to indent, we probably also
381 ;; want the default backspace behavior.
382 (not (run-hook-wrapped
383 'aggressive-indent--internal-dont-indent-if
385 (not (aggressive-indent--run-user-hooks)))
386 #'delete-indentation))))
388 (if (and global-aggressive-indent-mode
389 (or (cl-member-if #'derived-mode-p excluded-modes)
390 (memq major-mode '(text-mode fundamental-mode))
393 ;; Should electric indent be ON or OFF?
394 (if (or (eq dont-electric-modes t)
395 (cl-member-if #'derived-mode-p dont-electric-modes))
396 (-local-electric nil)
398 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
399 ;; (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
400 (add-hook 'post-command-hook #'-indent-if-changed nil 'local))
402 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
403 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
404 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
406 (defun -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
417 (defalias 'aggressive-indent-global-mode
418 #'global-aggressive-indent-mode)
421 (provide 'aggressive-indent)
422 ;;; aggressive-indent.el ends here