]> code.delx.au - gnu-emacs-elpa/blob - aggressive-indent.el
Fix typo
[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: 1.0
8 ;; Package-Requires: ((emacs "24.1") (names "20150125.9") (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
92 ;;;###autoload
93 (define-namespace aggressive-indent-
94 :group indent
95
96 (defconst version (eval-when-compile
97 (require 'lisp-mnt)
98 (lm-version))
99 "Version of the aggressive-indent.el package.")
100 (defun bug-report ()
101 "Opens github issues page in a web browser. Please send any bugs you find.
102 Please include your Emacs and `aggressive-indent' versions."
103 (interactive)
104 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
105 Please include this in your report!"
106 version emacs-version)
107 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
108
109 \f
110 ;;; Start of actual Code:
111 (defcustom dont-electric-modes '(ruby-mode)
112 "List of major-modes where `electric-indent' should be disabled."
113 :type '(choice
114 (const :tag "Never use `electric-indent-mode'." t)
115 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
116 :package-version '(aggressive-indent . "0.3.1"))
117
118 (defcustom excluded-modes
119 '(
120 bibtex-mode
121 cider-repl-mode
122 coffee-mode
123 conf-mode
124 Custom-mode
125 diff-mode
126 doc-view-mode
127 dos-mode
128 erc-mode
129 jabber-chat-mode
130 haml-mode
131 haskell-mode
132 image-mode
133 makefile-mode
134 makefile-gmake-mode
135 minibuffer-inactive-mode
136 netcmd-mode
137 python-mode
138 sass-mode
139 slim-mode
140 special-mode
141 shell-mode
142 snippet-mode
143 eshell-mode
144 tabulated-list-mode
145 term-mode
146 TeX-output-mode
147 text-mode
148 yaml-mode
149 )
150 "Modes in which `aggressive-indent-mode' should not be activated.
151 This variable is only used if `global-aggressive-indent-mode' is
152 active. If the minor mode is turned on with the local command,
153 `aggressive-indent-mode', this variable is ignored."
154 :type '(repeat symbol)
155 :package-version '(aggressive-indent . "0.3.1"))
156
157 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
158 "Commands after which indentation will NOT be performed.
159 Aggressive indentation could break things like `undo' by locking
160 the user in a loop, so this variable is used to control which
161 commands will NOT be followed by a re-indent."
162 :type '(repeat symbol)
163 :package-version '(aggressive-indent . "0.1"))
164
165 (defcustom comments-too nil
166 "If non-nil, aggressively indent in comments as well."
167 :type 'boolean
168 :package-version '(aggressive-indent . "0.3"))
169
170 (defvar -internal-dont-indent-if
171 '((memq this-command aggressive-indent-protected-commands)
172 (region-active-p)
173 buffer-read-only
174 (null (buffer-modified-p))
175 (and (boundp 'smerge-mode) smerge-mode)
176 (string-match "\\`[[:blank:]]*\n?\\'" (or (thing-at-point 'line) ""))
177 (let ((sp (syntax-ppss)))
178 ;; Comments.
179 (or (and (not aggressive-indent-comments-too) (elt sp 4))
180 ;; Strings.
181 (elt sp 3))))
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
187 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
188 "List of major-modes in which indenting defun is preferred.
189 Add here any major modes with very good definitions of
190 `end-of-defun' and `beginning-of-defun', or modes which bug out
191 if you have `after-change-functions' (such as paredit).
192
193 If current major mode is derived from one of these,
194 `aggressive-indent' will call `aggressive-indent-indent-defun'
195 after every command. Otherwise, it will call
196 `aggressive-indent-indent-region-and-on' after every buffer
197 change."
198 :type '(repeat symbol)
199 :package-version '(aggressive-indent . "0.3"))
200
201 (eval-after-load 'yasnippet
202 '(when (boundp 'yas--active-field-overlay)
203 (add-to-list 'aggressive-indent--internal-dont-indent-if
204 '(and
205 (overlayp yas--active-field-overlay)
206 (overlay-end yas--active-field-overlay))
207 'append)))
208 (eval-after-load 'company
209 '(when (boundp 'company-candidates)
210 (add-to-list 'aggressive-indent--internal-dont-indent-if
211 'company-candidates)))
212 (eval-after-load 'auto-complete
213 '(when (boundp 'ac-completing)
214 (add-to-list 'aggressive-indent--internal-dont-indent-if
215 'ac-completing)))
216
217 (defcustom dont-indent-if '()
218 "List of variables and functions to prevent aggressive indenting.
219 This variable is a list where each element is a Lisp form.
220 As long as any one of these forms returns non-nil,
221 aggressive-indent will not perform any indentation.
222
223 See `aggressive-indent--internal-dont-indent-if' for usage examples."
224 :type '(repeat sexp)
225 :group 'aggressive-indent
226 :package-version '(aggressive-indent . "0.2"))
227
228 (defvar -error-message
229 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
230 "Error message thrown by `aggressive-indent-dont-indent-if'.")
231
232 (defvar -has-errored nil
233 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
234 This is used to prevent an infinite error loop on the user.")
235
236 (defun -run-user-hooks ()
237 "Safely run forms in `aggressive-indent-dont-indent-if'.
238 If any of them errors out, we only report it once until it stops
239 erroring again."
240 (and dont-indent-if
241 (condition-case er
242 (prog1 (eval (cons 'or dont-indent-if))
243 (setq -has-errored nil))
244 (error (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 without errors or
263 messages."
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 region between L and R, and a bit more.
307 Like `aggressive-indent-indent-region-and-on', but without errors
308 or messages."
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 (L and R) of each change in the buffer."
334 (push l -changed-list-left)
335 (push r -changed-list-right))
336
337 \f
338 ;;; Minor modes
339 :autoload
340 (define-minor-mode mode
341 nil nil " =>"
342 '(("\ 3\11" . aggressive-indent-indent-defun)
343 ([backspace] menu-item "maybe-delete-indentation" ignore
344 :filter (lambda (&optional _)
345 (when (and (looking-back "^[[:blank:]]+")
346 ;; Wherever we don't want to indent, we probably also
347 ;; want the default backspace behavior.
348 (not (run-hook-wrapped
349 'aggressive-indent--internal-dont-indent-if
350 #'eval))
351 (not (aggressive-indent--run-user-hooks)))
352 #'delete-indentation))))
353 (if mode
354 (if (and global-aggressive-indent-mode
355 (or (cl-member-if #'derived-mode-p excluded-modes)
356 (memq major-mode '(text-mode fundamental-mode))
357 buffer-read-only))
358 (mode -1)
359 ;; Should electric indent be ON or OFF?
360 (if (or (eq dont-electric-modes t)
361 (cl-member-if #'derived-mode-p dont-electric-modes))
362 (-local-electric nil)
363 (-local-electric t))
364 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
365 (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
366 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
367 (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
368 ;; Clean the hooks
369 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
370 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
371 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
372
373 (defun -local-electric (on)
374 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
375 (if (fboundp 'electric-indent-local-mode)
376 (electric-indent-local-mode (if on 1 -1))
377 (set (make-local-variable 'electric-indent-mode) on)))
378
379 :autoload
380 (define-globalized-minor-mode global-aggressive-indent-mode
381 mode mode)
382
383 :autoload
384 (defalias 'aggressive-indent-global-mode
385 #'global-aggressive-indent-mode)
386 )
387
388 (provide 'aggressive-indent)
389 ;;; aggressive-indent.el ends here