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