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