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