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