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