]> code.delx.au - gnu-emacs-elpa/blob - packages/aggressive-indent/aggressive-indent.el
Merge commit '00920450d83ffe7a02bbe98997e266726819efc2'
[gnu-emacs-elpa] / packages / aggressive-indent / aggressive-indent.el
1 ;;; aggressive-indent.el --- Minor mode to aggressively keep your code always indented -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2014, 2015 Free Software Foundation, Inc
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
7 ;; Version: 1.4
8 ;; Package-Requires: ((emacs "24.1") (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 3
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 (defgroup aggressive-indent nil
93 "Customization group for aggressive-indent."
94 :prefix "aggressive-indent-"
95 :group 'indent)
96
97 (defun aggressive-indent-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 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
102 Please include this in your report!"
103 (eval-when-compile
104 (ignore-errors
105 (require 'lisp-mnt)
106 (lm-version)))
107 emacs-version)
108 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
109
110 (defvar aggressive-indent-mode)
111
112 ;;; Configuring indentarion
113 (defcustom aggressive-indent-dont-electric-modes '(ruby-mode)
114 "List of major-modes where `electric-indent' should be disabled."
115 :type '(choice
116 (const :tag "Never use `electric-indent-mode'." t)
117 (repeat :tag "List of major-modes to avoid `electric-indent-mode'." symbol))
118 :package-version '(aggressive-indent . "0.3.1"))
119
120 (defcustom aggressive-indent-excluded-modes
121 '(
122 bibtex-mode
123 cider-repl-mode
124 coffee-mode
125 comint-mode
126 conf-mode
127 Custom-mode
128 diff-mode
129 doc-view-mode
130 dos-mode
131 erc-mode
132 jabber-chat-mode
133 haml-mode
134 haskell-mode
135 image-mode
136 makefile-mode
137 makefile-gmake-mode
138 minibuffer-inactive-mode
139 netcmd-mode
140 python-mode
141 sass-mode
142 slim-mode
143 special-mode
144 shell-mode
145 snippet-mode
146 eshell-mode
147 tabulated-list-mode
148 term-mode
149 TeX-output-mode
150 text-mode
151 yaml-mode
152 )
153 "Modes in which `aggressive-indent-mode' should not be activated.
154 This variable is only used if `global-aggressive-indent-mode' is
155 active. If the minor mode is turned on with the local command,
156 `aggressive-indent-mode', this variable is ignored."
157 :type '(repeat symbol)
158 :package-version '(aggressive-indent . "0.3.1"))
159
160 (defcustom aggressive-indent-protected-commands '(undo undo-tree-undo undo-tree-redo whitespace-cleanup)
161 "Commands after which indentation will NOT be performed.
162 Aggressive indentation could break things like `undo' by locking
163 the user in a loop, so this variable is used to control which
164 commands will NOT be followed by a re-indent."
165 :type '(repeat symbol)
166 :package-version '(aggressive-indent . "0.1"))
167
168 (defcustom aggressive-indent-comments-too nil
169 "If non-nil, aggressively indent in comments as well."
170 :type 'boolean
171 :package-version '(aggressive-indent . "0.3"))
172
173 (defcustom aggressive-indent-modes-to-prefer-defun
174 '(emacs-lisp-mode lisp-mode scheme-mode clojure-mode)
175 "List of major-modes in which indenting defun is preferred.
176 Add here any major modes with very good definitions of
177 `end-of-defun' and `beginning-of-defun', or modes which bug out
178 if you have `after-change-functions' (such as paredit).
179
180 If current major mode is derived from one of these,
181 `aggressive-indent' will call `aggressive-indent-indent-defun'
182 after every command. Otherwise, it will call
183 `aggressive-indent-indent-region-and-on' after every buffer
184 change."
185 :type '(repeat symbol)
186 :package-version '(aggressive-indent . "0.3"))
187
188 ;;; Preventing indentation
189 (defvar aggressive-indent--internal-dont-indent-if
190 '((memq this-command aggressive-indent-protected-commands)
191 (region-active-p)
192 buffer-read-only
193 undo-in-progress
194 (null (buffer-modified-p))
195 (and (boundp 'smerge-mode) smerge-mode)
196 (let ((line (thing-at-point 'line)))
197 (when (stringp line)
198 (or (string-match "\\`[[:blank:]]*\n?\\'" line)
199 ;; If the user is starting to type a comment.
200 (and (stringp comment-start)
201 (string-match (concat "\\`[[:blank:]]*"
202 (substring comment-start 0 1)
203 "[[:blank:]]*$")
204 line)))))
205 (let ((sp (syntax-ppss)))
206 ;; Comments.
207 (or (and (not aggressive-indent-comments-too) (elt sp 4))
208 ;; Strings.
209 (elt sp 3))))
210 "List of forms which prevent indentation when they evaluate to non-nil.
211 This is for internal use only. For user customization, use
212 `aggressive-indent-dont-indent-if' instead.")
213
214 (eval-after-load 'yasnippet
215 '(when (boundp 'yas--active-field-overlay)
216 (add-to-list 'aggressive-indent--internal-dont-indent-if
217 '(and
218 (overlayp yas--active-field-overlay)
219 (overlay-end yas--active-field-overlay))
220 'append)))
221 (eval-after-load 'company
222 '(when (boundp 'company-candidates)
223 (add-to-list 'aggressive-indent--internal-dont-indent-if
224 'company-candidates)))
225 (eval-after-load 'auto-complete
226 '(when (boundp 'ac-completing)
227 (add-to-list 'aggressive-indent--internal-dont-indent-if
228 'ac-completing)))
229 (eval-after-load 'multiple-cursors-core
230 '(when (boundp 'multiple-cursors-mode)
231 (add-to-list 'aggressive-indent--internal-dont-indent-if
232 'multiple-cursors-mode)))
233 (eval-after-load 'iedit
234 '(when (boundp 'iedit-mode)
235 (add-to-list 'aggressive-indent--internal-dont-indent-if
236 'iedit-mode)))
237 (eval-after-load 'coq
238 '(add-to-list 'aggressive-indent--internal-dont-indent-if
239 '(and (derived-mode-p 'coq-mode)
240 (not (string-match "\\.[[:space:]]*$"
241 (thing-at-point 'line))))))
242
243 (defcustom aggressive-indent-dont-indent-if '()
244 "List of variables and functions to prevent aggressive indenting.
245 This variable is a list where each element is a Lisp form.
246 As long as any one of these forms returns non-nil,
247 aggressive-indent will not perform any indentation.
248
249 See `aggressive-indent--internal-dont-indent-if' for usage examples."
250 :type '(repeat sexp)
251 :package-version '(aggressive-indent . "0.2"))
252
253 (defvar aggressive-indent--error-message "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
254 "Error message thrown by `aggressive-indent-dont-indent-if'.")
255
256 (defvar aggressive-indent--has-errored nil
257 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
258 This is used to prevent an infinite error loop on the user.")
259
260 (defun aggressive-indent--run-user-hooks ()
261 "Safely run forms in `aggressive-indent-dont-indent-if'.
262 If any of them errors out, we only report it once until it stops
263 erroring again."
264 (and aggressive-indent-dont-indent-if
265 (condition-case er
266 (prog1 (eval (cons 'or aggressive-indent-dont-indent-if))
267 (setq aggressive-indent--has-errored nil))
268 (error (unless aggressive-indent--has-errored
269 (setq aggressive-indent--has-errored t)
270 (message aggressive-indent--error-message er))))))
271
272 ;;; Indenting defun
273 ;;;###autoload
274 (defun aggressive-indent-indent-defun (&optional l r)
275 "Indent current defun.
276 Throw an error if parentheses are unbalanced.
277 If L and R are provided, use them for finding the start and end of defun."
278 (interactive)
279 (let ((p (point-marker)))
280 (set-marker-insertion-type p t)
281 (indent-region
282 (save-excursion
283 (when l (goto-char l))
284 (beginning-of-defun 1) (point))
285 (save-excursion
286 (when r (goto-char r))
287 (end-of-defun 1) (point)))
288 (goto-char p)))
289
290 (defun aggressive-indent--softly-indent-defun (&optional l r)
291 "Indent current defun unobstrusively.
292 Like `aggressive-indent-indent-defun', but without errors or
293 messages. L and R passed to `aggressive-indent-indent-defun'."
294 (cl-letf (((symbol-function 'message) #'ignore))
295 (ignore-errors (aggressive-indent-indent-defun l r))))
296
297 ;;; Indenting region
298 ;;;###autoload
299 (defun aggressive-indent-indent-region-and-on (l r)
300 "Indent region between L and R, and then some.
301 Call `indent-region' between L and R, and then keep indenting
302 until nothing more happens."
303 (interactive "r")
304 (let ((p (point-marker))
305 was-begining-of-line)
306 (set-marker-insertion-type p t)
307 (unwind-protect
308 (progn
309 (unless (= l r)
310 (when (= (char-before r) ?\n)
311 (cl-decf r)))
312 ;; If L is at the end of a line, skip that line.
313 (unless (= l r)
314 (when (= (char-after l) ?\n)
315 (cl-incf l)))
316 ;; Indent the affected region.
317 (goto-char r)
318 (unless (= l r) (indent-region l r))
319 ;; And then we indent each following line until nothing happens.
320 (forward-line 1)
321 (skip-chars-forward "[:blank:]\n\r\xc")
322 (let* ((eod (ignore-errors
323 (save-excursion (end-of-defun)
324 (point-marker))))
325 (point-limit (if (and eod (< (point) eod))
326 eod (point-max-marker))))
327 (while (and (null (eobp))
328 (let ((op (point))
329 (np (progn (indent-according-to-mode)
330 (point))))
331 ;; As long as we're indenting things to the
332 ;; left, keep indenting.
333 (or (< np op)
334 ;; If we're indenting to the right, or
335 ;; not at all, stop at the limit.
336 (< (point) point-limit))))
337 (forward-line 1)
338 (skip-chars-forward "[:blank:]\n\r\xc"))))
339 (goto-char p))))
340
341 (defun aggressive-indent--softly-indent-region-and-on (l r &rest _)
342 "Indent region between L and R, and a bit more.
343 Like `aggressive-indent-indent-region-and-on', but without errors
344 or messages."
345 (cl-letf (((symbol-function 'message) #'ignore))
346 (ignore-errors (aggressive-indent-indent-region-and-on l r))))
347
348 ;;; Tracking changes
349 (defvar aggressive-indent--changed-list nil
350 "List of (left right) limit of regions changed in the last command loop.")
351 (make-variable-buffer-local 'aggressive-indent--changed-list)
352
353 (defun aggressive-indent--indent-if-changed ()
354 "Indent any region that changed in the last command loop."
355 (when aggressive-indent--changed-list
356 (unless (or (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval)
357 (aggressive-indent--run-user-hooks))
358 (while-no-input
359 (let ((inhibit-modification-hooks t)
360 (inhibit-point-motion-hooks t)
361 (indent-function
362 (if (cl-member-if #'derived-mode-p aggressive-indent-modes-to-prefer-defun)
363 #'aggressive-indent--softly-indent-defun #'aggressive-indent--softly-indent-region-and-on)))
364 (while aggressive-indent--changed-list
365 (apply indent-function (car aggressive-indent--changed-list))
366 (setq aggressive-indent--changed-list
367 (cdr aggressive-indent--changed-list))))))))
368
369 (defun aggressive-indent--keep-track-of-changes (l r &rest _)
370 "Store the limits (L and R) of each change in the buffer."
371 (when aggressive-indent-mode
372 (push (list l r) aggressive-indent--changed-list)))
373
374 ;;; Minor modes
375 ;;;###autoload
376 (define-minor-mode aggressive-indent-mode
377 nil nil " =>"
378 '(("\ 3\11" . aggressive-indent-indent-defun)
379 ([backspace]
380 menu-item "maybe-delete-indentation" ignore :filter
381 (lambda (&optional _)
382 (when (and (looking-back "^[[:blank:]]+")
383 ;; Wherever we don't want to indent, we probably also
384 ;; want the default backspace behavior.
385 (not (run-hook-wrapped 'aggressive-indent--internal-dont-indent-if #'eval))
386 (not (aggressive-indent--run-user-hooks)))
387 #'delete-indentation))))
388 (if aggressive-indent-mode
389 (if (and global-aggressive-indent-mode
390 (or (cl-member-if #'derived-mode-p aggressive-indent-excluded-modes)
391 (memq major-mode '(text-mode fundamental-mode))
392 buffer-read-only))
393 (aggressive-indent-mode -1)
394 ;; Should electric indent be ON or OFF?
395 (if (or (eq aggressive-indent-dont-electric-modes t)
396 (cl-member-if #'derived-mode-p aggressive-indent-dont-electric-modes))
397 (aggressive-indent--local-electric nil)
398 (aggressive-indent--local-electric t))
399 (add-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes nil 'local)
400 (add-hook 'post-command-hook #'aggressive-indent--indent-if-changed nil 'local))
401 ;; Clean the hooks
402 (remove-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes 'local)
403 (remove-hook 'post-command-hook #'aggressive-indent--indent-if-changed 'local)
404 (remove-hook 'post-command-hook #'aggressive-indent--softly-indent-defun 'local)))
405
406 (defun aggressive-indent--local-electric (on)
407 "Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
408 (if (fboundp 'electric-indent-local-mode)
409 (electric-indent-local-mode (if on 1 -1))
410 (set (make-local-variable 'electric-indent-mode) on)))
411
412 ;;;###autoload
413 (define-globalized-minor-mode global-aggressive-indent-mode
414 aggressive-indent-mode aggressive-indent-mode)
415
416 ;;;###autoload
417 (defalias 'aggressive-indent-global-mode
418 #'global-aggressive-indent-mode)
419
420 (provide 'aggressive-indent)
421 ;;; aggressive-indent.el ends here