]> code.delx.au - gnu-emacs-elpa/blob - aggressive-indent.el
Update Github username
[gnu-emacs-elpa] / aggressive-indent.el
1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/aggressive-indent-mode
7 ;; Version: 1.4.2
8 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
9 ;; Keywords: indent lisp maint tools
10 ;; Prefix: aggressive-indent
11 ;; Separator: -
12
13 ;;; Commentary:
14 ;;
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
18 ;; to go wrong.
19 ;;
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'.
23 ;;
24 ;; ### Instructions ###
25 ;;
26 ;; This package is available fom Melpa, you may install it by calling
27 ;;
28 ;; M-x package-install RET aggressive-indent
29 ;;
30 ;; Then activate it with
31 ;;
32 ;; (add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
33 ;; (add-hook 'css-mode-hook #'aggressive-indent-mode)
34 ;;
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:
38 ;;
39 ;; (global-aggressive-indent-mode 1)
40 ;; (add-to-list 'aggressive-indent-excluded-modes 'html-mode)
41 ;;
42 ;; ### Manual Installation ###
43 ;;
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
46 ;;
47 ;; (require 'aggressive-indent)
48
49 ;;; Instructions:
50 ;;
51 ;; INSTALLATION
52 ;;
53 ;; This package is available fom Melpa, you may install it by calling
54 ;; M-x package-install RET aggressive-indent.
55 ;;
56 ;; Then activate it with
57 ;; (add-hook 'emacs-lisp-mode-hook #'aggressive-indent-mode)
58 ;;
59 ;; You can also use an equivalent hook for another mode,
60 ;; `aggressive-indent' is not exclusive to emacs-lisp code.
61 ;;
62 ;; Alternatively, you can download it manually, place it in your
63 ;; `load-path' and require it with
64 ;;
65 ;; (require 'aggressive-indent)
66
67 ;;; License:
68 ;;
69 ;; This file is NOT part of GNU Emacs.
70 ;;
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.
75 ;;
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.
80 ;;
81
82 ;;; Change Log:
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.
88 ;;; Code:
89
90 (require 'cl-lib)
91
92 (defgroup aggressive-indent nil
93 "Customization group for aggressive-indent."
94 :prefix "aggressive-indent-"
95 :group 'electricity
96 :group 'indent)
97
98 (defun aggressive-indent-bug-report ()
99 "Opens github issues page in a web browser. Please send any bugs you find.
100 Please include your Emacs and `aggressive-indent' versions."
101 (interactive)
102 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
103 Please include this in your report!"
104 (eval-when-compile
105 (ignore-errors
106 (require 'lisp-mnt)
107 (lm-version)))
108 emacs-version)
109 (browse-url "https://github.com/Malabarba/aggressive-indent-mode/issues/new"))
110
111 (defvar aggressive-indent-mode)
112
113 ;;; Configuring indentarion
114 (defcustom aggressive-indent-dont-electric-modes '(ruby-mode)
115 "List of major-modes where `electric-indent' should be disabled."
116 :type '(choice
117 (const :tag "Never use `electric-indent-mode'." t)
118 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
119 :package-version '(aggressive-indent . "0.3.1"))
120
121 (defcustom aggressive-indent-excluded-modes
122 '(
123 bibtex-mode
124 cider-repl-mode
125 coffee-mode
126 comint-mode
127 conf-mode
128 Custom-mode
129 diff-mode
130 doc-view-mode
131 dos-mode
132 erc-mode
133 jabber-chat-mode
134 haml-mode
135 haskell-mode
136 image-mode
137 makefile-mode
138 makefile-gmake-mode
139 minibuffer-inactive-mode
140 netcmd-mode
141 python-mode
142 sass-mode
143 slim-mode
144 special-mode
145 shell-mode
146 snippet-mode
147 eshell-mode
148 tabulated-list-mode
149 term-mode
150 TeX-output-mode
151 text-mode
152 yaml-mode
153 )
154 "Modes in which `aggressive-indent-mode' should not be activated.
155 This variable is only used if `global-aggressive-indent-mode' is
156 active. If the minor mode is turned on with the local command,
157 `aggressive-indent-mode', this variable is ignored."
158 :type '(repeat symbol)
159 :package-version '(aggressive-indent . "0.3.1"))
160
161 (defcustom aggressive-indent-protected-commands '(undo undo-tree-undo undo-tree-redo whitespace-cleanup)
162 "Commands after which indentation will NOT be performed.
163 Aggressive indentation could break things like `undo' by locking
164 the user in a loop, so this variable is used to control which
165 commands will NOT be followed by a re-indent."
166 :type '(repeat symbol)
167 :package-version '(aggressive-indent . "0.1"))
168
169 (defcustom aggressive-indent-comments-too nil
170 "If non-nil, aggressively indent in comments as well."
171 :type 'boolean
172 :package-version '(aggressive-indent . "0.3"))
173
174 (defcustom aggressive-indent-modes-to-prefer-defun
175 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
176 "List of major-modes in which indenting defun is preferred.
177 Add here any major modes with very good definitions of
178 `end-of-defun' and `beginning-of-defun', or modes which bug out
179 if you have `after-change-functions' (such as paredit).
180
181 If current major mode is derived from one of these,
182 `aggressive-indent' will call `aggressive-indent-indent-defun'
183 after every command. Otherwise, it will call
184 `aggressive-indent-indent-region-and-on' after every buffer
185 change."
186 :type '(repeat symbol)
187 :package-version '(aggressive-indent . "0.3"))
188
189 ;;; Preventing indentation
190 (defvar aggressive-indent--internal-dont-indent-if
191 '((memq this-command aggressive-indent-protected-commands)
192 (region-active-p)
193 buffer-read-only
194 undo-in-progress
195 (null (buffer-modified-p))
196 (and (boundp 'smerge-mode) smerge-mode)
197 (let ((line (thing-at-point 'line)))
198 (when (stringp line)
199 (or (string-match "\\`[[:blank:]]*\n?\\'" line)
200 ;; If the user is starting to type a comment.
201 (and (stringp comment-start)
202 (string-match (concat "\\`[[:blank:]]*"
203 (substring comment-start 0 1)
204 "[[:blank:]]*$")
205 line)))))
206 (let ((sp (syntax-ppss)))
207 ;; Comments.
208 (or (and (not aggressive-indent-comments-too) (elt sp 4))
209 ;; Strings.
210 (elt sp 3))))
211 "List of forms which prevent indentation when they evaluate to non-nil.
212 This is for internal use only. For user customization, use
213 `aggressive-indent-dont-indent-if' instead.")
214
215 (eval-after-load 'yasnippet
216 '(when (boundp 'yas--active-field-overlay)
217 (add-to-list 'aggressive-indent--internal-dont-indent-if
218 '(and
219 (overlayp yas--active-field-overlay)
220 (overlay-end yas--active-field-overlay))
221 'append)))
222 (eval-after-load 'company
223 '(when (boundp 'company-candidates)
224 (add-to-list 'aggressive-indent--internal-dont-indent-if
225 'company-candidates)))
226 (eval-after-load 'auto-complete
227 '(when (boundp 'ac-completing)
228 (add-to-list 'aggressive-indent--internal-dont-indent-if
229 'ac-completing)))
230 (eval-after-load 'multiple-cursors-core
231 '(when (boundp 'multiple-cursors-mode)
232 (add-to-list 'aggressive-indent--internal-dont-indent-if
233 'multiple-cursors-mode)))
234 (eval-after-load 'iedit
235 '(when (boundp 'iedit-mode)
236 (add-to-list 'aggressive-indent--internal-dont-indent-if
237 'iedit-mode)))
238 (eval-after-load 'evil
239 '(when (boundp 'iedit-mode)
240 (add-to-list 'aggressive-indent--internal-dont-indent-if
241 'iedit-mode)))
242 (eval-after-load 'coq
243 '(add-to-list 'aggressive-indent--internal-dont-indent-if
244 '(and (derived-mode-p 'coq-mode)
245 (not (string-match "\\.[[:space:]]*$"
246 (thing-at-point 'line))))))
247
248 (defcustom aggressive-indent-dont-indent-if '()
249 "List of variables and functions to prevent aggressive indenting.
250 This variable is a list where each element is a Lisp form.
251 As long as any one of these forms returns non-nil,
252 aggressive-indent will not perform any indentation.
253
254 See `aggressive-indent--internal-dont-indent-if' for usage examples."
255 :type '(repeat sexp)
256 :package-version '(aggressive-indent . "0.2"))
257
258 (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"
259 "Error message thrown by `aggressive-indent-dont-indent-if'.")
260
261 (defvar aggressive-indent--has-errored nil
262 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
263 This is used to prevent an infinite error loop on the user.")
264
265 (defun aggressive-indent--run-user-hooks ()
266 "Safely run forms in `aggressive-indent-dont-indent-if'.
267 If any of them errors out, we only report it once until it stops
268 erroring again."
269 (and aggressive-indent-dont-indent-if
270 (condition-case er
271 (prog1 (eval (cons 'or aggressive-indent-dont-indent-if))
272 (setq aggressive-indent--has-errored nil))
273 (error (unless aggressive-indent--has-errored
274 (setq aggressive-indent--has-errored t)
275 (message aggressive-indent--error-message er))))))
276
277 ;;; Indenting defun
278 ;;;###autoload
279 (defun aggressive-indent-indent-defun (&optional l r)
280 "Indent current defun.
281 Throw an error if parentheses are unbalanced.
282 If L and R are provided, use them for finding the start and end of defun."
283 (interactive)
284 (let ((p (point-marker)))
285 (set-marker-insertion-type p t)
286 (indent-region
287 (save-excursion
288 (when l (goto-char l))
289 (beginning-of-defun 1) (point))
290 (save-excursion
291 (when r (goto-char r))
292 (end-of-defun 1) (point)))
293 (goto-char p)))
294
295 (defun aggressive-indent--softly-indent-defun (&optional l r)
296 "Indent current defun unobstrusively.
297 Like `aggressive-indent-indent-defun', but without errors or
298 messages. L and R passed to `aggressive-indent-indent-defun'."
299 (cl-letf (((symbol-function 'message) #'ignore))
300 (ignore-errors (aggressive-indent-indent-defun l r))))
301
302 ;;; Indenting region
303 ;;;###autoload
304 (defun aggressive-indent-indent-region-and-on (l r)
305 "Indent region between L and R, and then some.
306 Call `indent-region' between L and R, and then keep indenting
307 until nothing more happens."
308 (interactive "r")
309 (let ((p (point-marker))
310 was-begining-of-line)
311 (set-marker-insertion-type p t)
312 (unwind-protect
313 (progn
314 (unless (= l r)
315 (when (= (char-before r) ?\n)
316 (cl-decf r)))
317 ;; If L is at the end of a line, skip that line.
318 (unless (= l r)
319 (when (= (char-after l) ?\n)
320 (cl-incf l)))
321 ;; Indent the affected region.
322 (goto-char r)
323 (unless (= l r) (indent-region l r))
324 ;; And then we indent each following line until nothing happens.
325 (forward-line 1)
326 (skip-chars-forward "[:blank:]\n\r\xc")
327 (let* ((eod (ignore-errors
328 (save-excursion (end-of-defun)
329 (point-marker))))
330 (point-limit (if (and eod (< (point) eod))
331 eod (point-max-marker))))
332 (while (and (null (eobp))
333 (let ((op (point))
334 (np (progn (indent-according-to-mode)
335 (point))))
336 ;; As long as we're indenting things to the
337 ;; left, keep indenting.
338 (or (< np op)
339 ;; If we're indenting to the right, or
340 ;; not at all, stop at the limit.
341 (< (point) point-limit))))
342 (forward-line 1)
343 (skip-chars-forward "[:blank:]\n\r\xc"))))
344 (goto-char p))))
345
346 (defun aggressive-indent--softly-indent-region-and-on (l r &rest _)
347 "Indent region between L and R, and a bit more.
348 Like `aggressive-indent-indent-region-and-on', but without errors
349 or messages."
350 (cl-letf (((symbol-function 'message) #'ignore))
351 (ignore-errors (aggressive-indent-indent-region-and-on l r))))
352
353 ;;; Tracking changes
354 (defvar aggressive-indent--changed-list nil
355 "List of (left right) limit of regions changed in the last command loop.")
356 (make-variable-buffer-local 'aggressive-indent--changed-list)
357
358 (defun aggressive-indent--indent-if-changed ()
359 "Indent any region that changed in the last command loop."
360 (when aggressive-indent--changed-list
361 (unless (or (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval)
362 (aggressive-indent--run-user-hooks))
363 (while-no-input
364 (redisplay)
365 (let ((inhibit-modification-hooks t)
366 (inhibit-point-motion-hooks t)
367 (indent-function
368 (if (cl-member-if #'derived-mode-p aggressive-indent-modes-to-prefer-defun)
369 #'aggressive-indent--softly-indent-defun #'aggressive-indent--softly-indent-region-and-on)))
370 (while aggressive-indent--changed-list
371 (apply indent-function (car aggressive-indent--changed-list))
372 (setq aggressive-indent--changed-list
373 (cdr aggressive-indent--changed-list))))))))
374
375 (defun aggressive-indent--keep-track-of-changes (l r &rest _)
376 "Store the limits (L and R) of each change in the buffer."
377 (when aggressive-indent-mode
378 (push (list l r) aggressive-indent--changed-list)))
379
380 ;;; Minor modes
381 ;;;###autoload
382 (define-minor-mode aggressive-indent-mode
383 nil nil " =>"
384 '(("\ 3\11" . aggressive-indent-indent-defun)
385 ([backspace]
386 menu-item "maybe-delete-indentation" ignore :filter
387 (lambda (&optional _)
388 (when (and (looking-back "^[[:blank:]]+")
389 ;; Wherever we don't want to indent, we probably also
390 ;; want the default backspace behavior.
391 (not (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval))
392 (not (aggressive-indent--run-user-hooks)))
393 #'delete-indentation))))
394 (if aggressive-indent-mode
395 (if (and global-aggressive-indent-mode
396 (or (cl-member-if #'derived-mode-p aggressive-indent-excluded-modes)
397 (memq major-mode '(text-mode fundamental-mode))
398 buffer-read-only))
399 (aggressive-indent-mode -1)
400 ;; Should electric indent be ON or OFF?
401 (if (or (eq aggressive-indent-dont-electric-modes t)
402 (cl-member-if #'derived-mode-p aggressive-indent-dont-electric-modes))
403 (aggressive-indent--local-electric nil)
404 (aggressive-indent--local-electric t))
405 (add-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes nil 'local)
406 (add-hook 'post-command-hook #'aggressive-indent--indent-if-changed nil 'local))
407 ;; Clean the hooks
408 (remove-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes 'local)
409 (remove-hook 'post-command-hook #'aggressive-indent--indent-if-changed 'local)
410 (remove-hook 'post-command-hook #'aggressive-indent--softly-indent-defun 'local)))
411
412 (defun aggressive-indent--local-electric (on)
413 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
414 (if (fboundp 'electric-indent-local-mode)
415 (electric-indent-local-mode (if on 1 -1))
416 (set (make-local-variable 'electric-indent-mode) on)))
417
418 ;;;###autoload
419 (define-globalized-minor-mode global-aggressive-indent-mode
420 aggressive-indent-mode aggressive-indent-mode)
421
422 ;;;###autoload
423 (defalias 'aggressive-indent-global-mode
424 #'global-aggressive-indent-mode)
425
426 (provide 'aggressive-indent)
427 ;;; aggressive-indent.el ends here