]> code.delx.au - gnu-emacs-elpa/blob - aggressive-indent.el
Whitespace
[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.2
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/19 - Add variable `aggressive-indent-dont-indent-if', so the user can prevent indentation.
84 ;; 0.1 - 2014/10/15 - Release.
85 ;;; Code:
86
87 (require 'cl-lib)
88 (require 'names)
89
90 ;;;###autoload
91 (define-namespace aggressive-indent- :group indent
92
93 (defconst version "0.1" "Version of the aggressive-indent.el package.")
94 (defun bug-report ()
95 "Opens github issues page in a web browser. Please send any bugs you find.
96 Please include your emacs and aggressive-indent versions."
97 (interactive)
98 (message "Your `aggressive-indent-version' is: %s, and your emacs version is: %s.
99 Please include this in your report!"
100 aggressive-indent-version emacs-version)
101 (browse-url "https://github.com/Bruce-Connor/aggressive-indent-mode/issues/new"))
102
103 \f
104 ;;; Start of actual Code:
105 (defcustom excluded-modes
106 '(text-mode tabulated-list-mode special-mode
107 minibuffer-inactive-mode)
108 "Modes in which `aggressive-indent-mode' should not be activated.
109 This variable is only used if `global-aggressive-indent-mode' is
110 active. If the minor mode is turned on with the local command,
111 `aggressive-indent-mode', this variable is ignored."
112 :type '(repeat symbol)
113 :package-version '(aggressive-indent . "0.1"))
114
115 (defcustom protected-commands '(undo undo-tree-undo undo-tree-redo)
116 "Commands after which indentation will NOT be performed.
117 Aggressive indentation could break things like `undo' by locking
118 the user in a loop, so this variable is used to control which
119 commands will NOT be followed by a re-indent."
120 :type '(repeat symbol)
121 :package-version '(aggressive-indent . "0.1"))
122
123 (defvar -internal-dont-indent-if
124 '((memq last-command aggressive-indent-protected-commands)
125 (region-active-p)
126 buffer-read-only
127 (null (buffer-modified-p)))
128 "List of forms which prevent indentation when they evaluate to non-nil.
129 This is for internal use only. For user customization, use
130 `aggressive-indent-dont-indent-if' instead.")
131
132 (eval-after-load 'yasnippet
133 '(when (boundp 'yas--active-field-overlay)
134 (add-to-list 'aggressive-indent--internal-dont-indent-if
135 '(and
136 (overlayp yas--active-field-overlay)
137 (overlay-end yas--active-field-overlay))
138 'append)))
139 (eval-after-load 'company
140 '(when (boundp 'company-candidates)
141 (add-to-list 'aggressive-indent--internal-dont-indent-if
142 'company-candidates)))
143 (eval-after-load 'auto-complete
144 '(when (boundp 'ac-completing)
145 (add-to-list 'aggressive-indent--internal-dont-indent-if
146 'ac-completing)))
147
148 (defcustom dont-indent-if '()
149 "List of variables and functions to prevent aggressive indenting.
150 This variable is a list where each element is a lisp form.
151 As long as any one of these forms returns non-nil,
152 aggressive-indent will not perform any indentation.
153
154 See `aggressive-indent--internal-dont-indent-if' for usage examples."
155 :type '(repeat sexp)
156 :group 'aggressive-indent
157 :package-version '(aggressive-indent . "0.2"))
158
159 (defvar -error-message
160 "One of the forms in `aggressive-indent-dont-indent-if' had the following error, I've disabled it until you fix it: %S"
161 "Error message thrown by `aggressive-indent-dont-indent-if'.")
162
163 (defvar -has-errored nil
164 "Keep track of whether `aggressive-indent-dont-indent-if' is throwing.
165 This is used to prevent an infinite error loop on the user.")
166
167 (defun -softly-indent-defun ()
168 "Indent current defun unobstrusively.
169 Like `aggressive-indent-indent-defun', except do nothing if
170 mark is active (to avoid deactivaing it), or if buffer is not
171 modified (to avoid creating accidental modifications).
172 Also, never throw errors nor messages.
173
174 Meant for use in hooks. Interactively, use the other one.
175 Indentation is not performed if any of the forms in
176 `dont-indent-if' evaluates to non-nil."
177 (unless (or (run-hook-wrapped
178 'aggressive-indent--internal-dont-indent-if
179 #'eval)
180 (-run-user-hooks))
181 (ignore-errors
182 (cl-letf (((symbol-function 'message) #'ignore))
183 (indent-defun)))))
184
185 (defun -run-user-hooks ()
186 "Safely run forms in `aggressive-indent-dont-indent-if'.
187 If any of them errors out, we only report it once until it stops
188 erroring again."
189 (and dont-indent-if
190 (condition-case er
191 (prog1 (eval (cons 'or dont-indent-if))
192 (setq -has-errored nil))
193 (error
194 (unless -has-errored
195 (setq -has-errored t)
196 (message -error-message er))))))
197
198 :autoload
199 (defun indent-defun ()
200 "Indent current defun.
201 Throw an error if parentheses are unbalanced."
202 (interactive)
203 (let ((p (point-marker)))
204 (set-marker-insertion-type p t)
205 (indent-region
206 (save-excursion (beginning-of-defun 1) (point))
207 (save-excursion (end-of-defun 1) (point)))
208 (goto-char p)))
209
210 \f
211 ;;; Minor modes
212 :autoload
213 (define-minor-mode mode nil nil " =>"
214 '(("\C-c\C-q" . aggressive-indent-indent-defun))
215 (if mode
216 (if (and global-aggressive-indent-mode
217 (or (cl-member-if #'derived-mode-p excluded-modes)
218 buffer-read-only))
219 (mode -1)
220 (setq-local electric-indent-mode nil)
221 (add-hook 'post-command-hook #'-softly-indent-defun nil 'local))
222 (remove-hook 'post-command-hook #'-softly-indent-defun 'local)))
223
224 :autoload
225 (define-globalized-minor-mode global-aggressive-indent-mode
226 mode mode)
227
228 :autoload
229 (defalias 'aggressive-indent-global-mode
230 #'global-aggressive-indent-mode)
231 )
232
233 (provide 'aggressive-indent)
234 ;;; aggressive-indent.el ends here.