]> code.delx.au - gnu-emacs-elpa/blob - aggressive-indent.el
Don't indent if the user is starting to type a comment. Fix #51
[gnu-emacs-elpa] / aggressive-indent.el
1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
7 ;; Version: 1.1.1
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 change, 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 (eval-when-compile (require 'names))
92
93 ;;;###autoload
94 (define-namespace aggressive-indent-
95 :group indent
96
97 (defun bug-report ()
98 "Opens github issues page in a web browser. Please send any bugs you find.
99 Please include your Emacs and `aggressive-indent' versions."
100 (interactive)
101 (require 'lisp-mnt)
102 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
103 Please include this in your report!"
104 (lm-version (find-library-name "aggressive-indent"))
105 emacs-version)
106 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
107
108 \f
109 ;;; Start of actual Code:
110 (defcustom dont-electric-modes '(ruby-mode)
111 "List of major-modes where `electric-indent' should be disabled."
112 :type '(choice
113 (const :tag "Never use `electric-indent-mode'." t)
114 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
115 :package-version '(aggressive-indent . "0.3.1"))
116
117 (defcustom excluded-modes
118 '(
119 bibtex-mode
120 cider-repl-mode
121 coffee-mode
122 comint-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 (let ((line (thing-at-point 'line)))
177 (when (stringp line)
178 (or (string-match "\\`[[:blank:]]*\n?\\'" line)
179 ;; If the user is starting to type a comment.
180 (and (stringp comment-start)
181 (string-match (concat "\\`[[:blank:]]*"
182 (substring comment-start 0 1)
183 "[[:blank:]]*$")
184 line)))))
185 (let ((sp (syntax-ppss)))
186 ;; Comments.
187 (or (and (not aggressive-indent-comments-too) (elt sp 4))
188 ;; Strings.
189 (elt sp 3))))
190 "List of forms which prevent indentation when they evaluate to non-nil.
191 This is for internal use only. For user customization, use
192 `aggressive-indent-dont-indent-if' instead.")
193
194 (defcustom modes-to-prefer-defun
195 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
196 "List of major-modes in which indenting defun is preferred.
197 Add here any major modes with very good definitions of
198 `end-of-defun' and `beginning-of-defun', or modes which bug out
199 if you have `after-change-functions' (such as paredit).
200
201 If current major mode is derived from one of these,
202 `aggressive-indent' will call `aggressive-indent-indent-defun'
203 after every command. Otherwise, it will call
204 `aggressive-indent-indent-region-and-on' after every buffer
205 change."
206 :type '(repeat symbol)
207 :package-version '(aggressive-indent . "0.3"))
208
209 (eval-after-load 'yasnippet
210 '(when (boundp 'yas--active-field-overlay)
211 (add-to-list 'aggressive-indent--internal-dont-indent-if
212 '(and
213 (overlayp yas--active-field-overlay)
214 (overlay-end yas--active-field-overlay))
215 'append)))
216 (eval-after-load 'company
217 '(when (boundp 'company-candidates)
218 (add-to-list 'aggressive-indent--internal-dont-indent-if
219 'company-candidates)))
220 (eval-after-load 'auto-complete
221 '(when (boundp 'ac-completing)
222 (add-to-list 'aggressive-indent--internal-dont-indent-if
223 'ac-completing)))
224 (eval-after-load 'iedit
225 '(when (boundp 'iedit-mode)
226 (add-to-list 'aggressive-indent--internal-dont-indent-if
227 'iedit-mode)))
228 (eval-after-load 'coq
229 '(add-to-list 'aggressive-indent--internal-dont-indent-if
230 '(and (derived-mode-p 'coq-mode)
231 (not (string-match "\\.[[:space:]]*$"
232 (thing-at-point 'line))))))
233
234 (defcustom dont-indent-if '()
235 "List of variables and functions to prevent aggressive indenting.
236 This variable is a list where each element is a Lisp form.
237 As long as any one of these forms returns non-nil,
238 aggressive-indent will not perform any indentation.
239
240 See `aggressive-indent--internal-dont-indent-if' for usage examples."
241 :type '(repeat sexp)
242 :group 'aggressive-indent
243 :package-version '(aggressive-indent . "0.2"))
244
245 (defvar -error-message
246 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
247 "Error message thrown by `aggressive-indent-dont-indent-if'.")
248
249 (defvar -has-errored nil
250 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
251 This is used to prevent an infinite error loop on the user.")
252
253 (defun -run-user-hooks ()
254 "Safely run forms in `aggressive-indent-dont-indent-if'.
255 If any of them errors out, we only report it once until it stops
256 erroring again."
257 (and dont-indent-if
258 (condition-case er
259 (prog1 (eval (cons 'or dont-indent-if))
260 (setq -has-errored nil))
261 (error (unless -has-errored
262 (setq -has-errored t)
263 (message -error-message er))))))
264
265 \f
266 :autoload
267 (defun indent-defun (&optional l r)
268 "Indent current defun.
269 Throw an error if parentheses are unbalanced.
270 If L and R are provided, use them for finding the start and end of defun."
271 (interactive)
272 (let ((p (point-marker)))
273 (set-marker-insertion-type p t)
274 (indent-region
275 (save-excursion
276 (when l (goto-char l))
277 (beginning-of-defun 1) (point))
278 (save-excursion
279 (when r (goto-char r))
280 (end-of-defun 1) (point)))
281 (goto-char p)))
282
283 (defun -softly-indent-defun (&optional l r)
284 "Indent current defun unobstrusively.
285 Like `aggressive-indent-indent-defun', but without errors or
286 messages. L and R passed to `aggressive-indent-indent-defun'."
287 (unless (or (run-hook-wrapped
288 'aggressive-indent--internal-dont-indent-if
289 #'eval)
290 (aggressive-indent--run-user-hooks))
291 (cl-letf (((symbol-function 'message) #'ignore))
292 (ignore-errors (indent-defun l r)))))
293
294 :autoload
295 (defun indent-region-and-on (l r)
296 "Indent region between L and R, and then some.
297 Call `indent-region' between L and R, and then keep indenting
298 until nothing more happens."
299 (interactive "r")
300 (let ((p (point-marker))
301 was-begining-of-line)
302 (set-marker-insertion-type p t)
303 (unwind-protect
304 (progn
305 (goto-char r)
306 (setq was-begining-of-line
307 (= r (line-beginning-position)))
308 ;; If L is at the end of a line, skip that line.
309 (unless (= l r)
310 (goto-char l)
311 (when (= l (line-end-position))
312 (cl-incf l)))
313 ;; Indent the affected region.
314 (unless (= l r) (indent-region l r))
315 ;; `indent-region' doesn't do anything if R was the beginning of a line, so we indent manually there.
316 (when was-begining-of-line
317 (indent-according-to-mode))
318 ;; And then we indent each following line until nothing happens.
319 (forward-line 1)
320 (skip-chars-forward "[:blank:]\n")
321 (let* ((eod (ignore-errors
322 (save-excursion (end-of-defun)
323 (point-marker))))
324 (point-limit (if (and eod (< (point) eod))
325 eod (point-max-marker))))
326 (while (and (null (eobp))
327 (< (point) point-limit)
328 (/= (point)
329 (progn (indent-according-to-mode)
330 (point))))
331 (forward-line 1)
332 (skip-chars-forward "[:blank:]\n"))))
333 (goto-char p))))
334
335 (defun -softly-indent-region-and-on (l r &rest _)
336 "Indent region between L and R, and a bit more.
337 Like `aggressive-indent-indent-region-and-on', but without errors
338 or messages."
339 (unless (or (run-hook-wrapped
340 'aggressive-indent--internal-dont-indent-if
341 #'eval)
342 (aggressive-indent--run-user-hooks))
343 (cl-letf (((symbol-function 'message) #'ignore))
344 (ignore-errors (indent-region-and-on l r)))))
345
346 (defvar -changed-list nil
347 "List of (left right) limit of regions changed in the last command loop.")
348
349 (defun -indent-if-changed ()
350 "Indent any region that changed in the last command loop."
351 (when -changed-list
352 (while-no-input
353 (let ((inhibit-modification-hooks t)
354 (inhibit-point-motion-hooks t)
355 (indent-function
356 (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
357 #'-softly-indent-defun
358 #'-softly-indent-region-and-on)))
359 (while -changed-list
360 (apply indent-function (car -changed-list))
361 (setq -changed-list (cdr -changed-list)))))))
362
363 (defun -keep-track-of-changes (l r &rest _)
364 "Store the limits (L and R) of each change in the buffer."
365 (push (list l r) -changed-list))
366
367 \f
368 ;;; Minor modes
369 :autoload
370 (define-minor-mode mode
371 nil nil " =>"
372 '(("\ 3\11" . aggressive-indent-indent-defun)
373 ([backspace] menu-item "maybe-delete-indentation" ignore
374 :filter (lambda (&optional _)
375 (when (and (looking-back "^[[:blank:]]+")
376 ;; Wherever we don't want to indent, we probably also
377 ;; want the default backspace behavior.
378 (not (run-hook-wrapped
379 'aggressive-indent--internal-dont-indent-if
380 #'eval))
381 (not (aggressive-indent--run-user-hooks)))
382 #'delete-indentation))))
383 (if mode
384 (if (and global-aggressive-indent-mode
385 (or (cl-member-if #'derived-mode-p excluded-modes)
386 (memq major-mode '(text-mode fundamental-mode))
387 buffer-read-only))
388 (mode -1)
389 ;; Should electric indent be ON or OFF?
390 (if (or (eq dont-electric-modes t)
391 (cl-member-if #'derived-mode-p dont-electric-modes))
392 (-local-electric nil)
393 (-local-electric t))
394 (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
395 ;; (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
396 (add-hook 'post-command-hook #'-indent-if-changed nil 'local))
397 ;; Clean the hooks
398 (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
399 (remove-hook 'post-command-hook #'-indent-if-changed 'local)
400 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
401
402 (defun -local-electric (on)
403 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
404 (if (fboundp 'electric-indent-local-mode)
405 (electric-indent-local-mode (if on 1 -1))
406 (set (make-local-variable 'electric-indent-mode) on)))
407
408 :autoload
409 (define-globalized-minor-mode global-aggressive-indent-mode
410 mode mode)
411
412 :autoload
413 (defalias 'aggressive-indent-global-mode
414 #'global-aggressive-indent-mode)
415 )
416
417 (provide 'aggressive-indent)
418 ;;; aggressive-indent.el ends here