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