]> code.delx.au - gnu-emacs-elpa/blob - aggressive-indent.el
More style and doc changes
[gnu-emacs-elpa] / aggressive-indent.el
1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
2
3 ;; Copyright (C) 2014 Artur Malabarba <bruce.connor.am@gmail.com>
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
7 ;; Version: 0.3.5
8 ;; Package-Requires: ((emacs "24.1") (names "0.5") (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 command, 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 ;;;###autoload
93 (define-namespace aggressive-indent-
94 :group indent
95
96 (defconst version (lm-version) "Version of the aggressive-indent.el package.")
97 (defun 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."
100 (interactive)
101 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
102 Please include this in your report!"
103 version emacs-version)
104 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
105
106 \f
107 ;;; Start of actual Code:
108 (defcustom 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 excluded-modes
116 '(
117 bibtex-mode
118 cider-repl-mode
119 coffee-mode
120 conf-mode
121 Custom-mode
122 diff-mode
123 doc-view-mode
124 dos-mode
125 erc-mode
126 jabber-chat-mode
127 haml-mode
128 haskell-mode
129 image-mode
130 makefile-mode
131 makefile-gmake-mode
132 minibuffer-inactive-mode
133 netcmd-mode
134 python-mode
135 sass-mode
136 slim-mode
137 special-mode
138 shell-mode
139 snippet-mode
140 eshell-mode
141 tabulated-list-mode
142 term-mode
143 TeX-output-mode
144 text-mode
145 yaml-mode
146 )
147 "Modes in which `aggressive-indent-mode' should not be activated.
148 This variable is only used if `global-aggressive-indent-mode' is
149 active. If the minor mode is turned on with the local command,
150 `aggressive-indent-mode', this variable is ignored."
151 :type '(repeat symbol)
152 :package-version '(aggressive-indent . "0.3.1"))
153
154 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
155 "Commands after which indentation will NOT be performed.
156 Aggressive indentation could break things like `undo' by locking
157 the user in a loop, so this variable is used to control which
158 commands will NOT be followed by a re-indent."
159 :type '(repeat symbol)
160 :package-version '(aggressive-indent . "0.1"))
161
162 (defcustom comments-too nil
163 "If non-nil, aggressively indent in comments as well."
164 :type 'boolean
165 :package-version '(aggressive-indent . "0.3"))
166
167 (defvar -internal-dont-indent-if
168 '((memq this-command aggressive-indent-protected-commands)
169 (region-active-p)
170 buffer-read-only
171 (null (buffer-modified-p))
172 (and (boundp 'smerge-mode) smerge-mode)
173 (string-match "\\`[[:blank:]]*\n?\\'" (or (thing-at-point 'line) ""))
174 (let ((sp (syntax-ppss)))
175 ;; Comments.
176 (or (and (not aggresive-indent-comments-too) (elt sp 4))
177 ;; Strings.
178 (elt sp 3))))
179 "List of forms which prevent indentation when they evaluate to non-nil.
180 This is for internal use only. For user customization, use
181 `aggressive-indent-dont-indent-if' instead.")
182
183 (defcustom modes-to-prefer-defun
184 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
185 "List of major-modes in which indenting defun is preferred.
186 Add here any major modes with very good definitions of
187 `end-of-defun' and `beginning-of-defun', or modes which bug out
188 if you have `after-change-functions' (such as paredit).
189
190 If current major mode is derived from one of these,
191 `aggressive-indent' will call `aggressive-indent-indent-defun'
192 after every command. Otherwise, it will call
193 `aggressive-indent-indent-region-and-on' after every buffer
194 change."
195 :type '(repeat symbol)
196 :package-version '(aggressive-indent . "0.3"))
197
198 (eval-after-load 'yasnippet
199 '(when (boundp 'yas--active-field-overlay)
200 (add-to-list 'aggressive-indent--internal-dont-indent-if
201 '(and
202 (overlayp yas--active-field-overlay)
203 (overlay-end yas--active-field-overlay))
204 'append)))
205 (eval-after-load 'company
206 '(when (boundp 'company-candidates)
207 (add-to-list 'aggressive-indent--internal-dont-indent-if
208 'company-candidates)))
209 (eval-after-load 'auto-complete
210 '(when (boundp 'ac-completing)
211 (add-to-list 'aggressive-indent--internal-dont-indent-if
212 'ac-completing)))
213
214 (defcustom dont-indent-if '()
215 "List of variables and functions to prevent aggressive indenting.
216 This variable is a list where each element is a Lisp form.
217 As long as any one of these forms returns non-nil,
218 aggressive-indent will not perform any indentation.
219
220 See `aggressive-indent--internal-dont-indent-if' for usage examples."
221 :type '(repeat sexp)
222 :group 'aggressive-indent
223 :package-version '(aggressive-indent . "0.2"))
224
225 (defvar -error-message
226 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
227 "Error message thrown by `aggressive-indent-dont-indent-if'.")
228
229 (defvar -has-errored nil
230 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
231 This is used to prevent an infinite error loop on the user.")
232
233 (defun -run-user-hooks ()
234 "Safely run forms in `aggressive-indent-dont-indent-if'.
235 If any of them errors out, we only report it once until it stops
236 erroring again."
237 (and dont-indent-if
238 (condition-case er
239 (prog1 (eval (cons 'or dont-indent-if))
240 (setq -has-errored nil))
241 (error (unless -has-errored
242 (setq -has-errored t)
243 (message -error-message er))))))
244
245 :autoload
246 (defun indent-defun ()
247 "Indent current defun.
248 Throw an error if parentheses are unbalanced."
249 (interactive)
250 (let ((p (point-marker)))
251 (set-marker-insertion-type p t)
252 (indent-region
253 (save-excursion (beginning-of-defun 1) (point))
254 (save-excursion (end-of-defun 1) (point)))
255 (goto-char p)))
256
257 (defun -softly-indent-defun ()
258 "Indent current defun unobstrusively.
259 Like `aggressive-indent-indent-defun', but without errors or
260 messages."
261 (unless (or (run-hook-wrapped
262 'aggressive-indent--internal-dont-indent-if
263 #'eval)
264 (aggressive-indent--run-user-hooks))
265 (cl-letf (((symbol-function 'message) #'ignore))
266 (ignore-errors (indent-defun)))))
267
268 :autoload
269 (defun indent-region-and-on (l r)
270 "Indent region between L and R, and then some.
271 Call `indent-region' between L and R, and then keep indenting
272 until nothing more happens."
273 (interactive "r")
274 (let ((p (point-marker))
275 was-begining-of-line)
276 (set-marker-insertion-type p t)
277 (unwind-protect
278 (progn
279 (goto-char r)
280 (setq was-begining-of-line
281 (= r (line-beginning-position)))
282 ;; If L is at the end of a line, skip that line.
283 (unless (= l r)
284 (goto-char l)
285 (when (= l (line-end-position))
286 (cl-incf l)))
287 ;; Indent the affected region.
288 (unless (= l r) (indent-region l r))
289 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
290 (when was-begining-of-line
291 (indent-according-to-mode))
292 ;; And then we indent each following line until nothing happens.
293 (forward-line 1)
294 (while (and (null (eobp))
295 (/= (progn (skip-chars-forward "[:blank:]\n")
296 (point))
297 (progn (indent-according-to-mode)
298 (point))))
299 (forward-line 1)))
300 (goto-char p))))
301
302 (defun -softly-indent-region-and-on (l r &rest _)
303 "Indent region between L and R, and a bit more.
304 Like `aggressive-indent-indent-region-and-on', but without errors
305 or messages."
306 (unless (or (run-hook-wrapped
307 'aggressive-indent--internal-dont-indent-if
308 #'eval)
309 (aggressive-indent--run-user-hooks))
310 (cl-letf (((symbol-function 'message) #'ignore))
311 (ignore-errors (indent-region-and-on l r)))))
312
313 (defvar -changed-list-right nil
314 "List of right limit of regions changed in the last command loop.")
315
316 (defvar -changed-list-left nil
317 "List of left limit of regions changed in the last command loop.")
318
319 (defun -indent-if-changed ()
320 "Indent any region that changed in the last command loop."
321 (let ((inhibit-modification-hooks t))
322 (when -changed-list-left
323 (-softly-indent-region-and-on
324 (apply #'min -changed-list-left)
325 (apply #'max -changed-list-right))
326 (setq -changed-list-left nil
327 -changed-list-right nil))))
328
329 (defun -keep-track-of-changes (l r &rest _)
330 "Store the limits (L and R) of each change in the buffer."
331 (push l -changed-list-left)
332 (push r -changed-list-right))
333
334 \f
335 ;;; Minor modes
336 :autoload
337 (define-minor-mode mode
338 nil nil " =>"
339 '(("\ 3\11" . aggressive-indent-indent-defun)
340 ([backspace] menu-item "maybe-delete-indentation" ignore
341 :filter (lambda (&optional _)
342 (when (and (looking-back "^[[:blank:]]+")
343 ;; Wherever we don't want to indent, we probably also
344 ;; want the default backspace behavior.
345 (not (run-hook-wrapped
346 'aggressive-indent--internal-dont-indent-if
347 #'eval))
348 (not (aggressive-indent--run-user-hooks)))
349 #'delete-indentation))))
350 (if mode
351 (if (and global-aggressive-indent-mode
352 (or (cl-member-if #'derived-mode-p excluded-modes)
353 (memq major-mode '(text-mode fundamental-mode))
354 buffer-read-only))
355 (mode -1)
356 ;; Should electric indent be ON or OFF?
357 (if (or (eq dont-electric-modes t)
358 (cl-member-if #'derived-mode-p dont-electric-modes))
359 (-local-electric nil)
360 (-local-electric t))
361 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
362 (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
363 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
364 (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
365 ;; Clean the hooks
366 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
367 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
368 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
369
370 (defun -local-electric (on)
371 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
372 (if (fboundp 'electric-indent-local-mode)
373 (electric-indent-local-mode (if on 1 -1))
374 (set (make-local-variable 'electric-indent-mode) on)))
375
376 :autoload
377 (define-globalized-minor-mode global-aggressive-indent-mode
378 mode mode)
379
380 :autoload
381 (defalias 'aggressive-indent-global-mode
382 #'global-aggressive-indent-mode)
383 )
384
385 (provide 'aggressive-indent)
386 ;;; aggressive-indent.el ends here