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