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