Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; electric.el --- window maker and Command loop for `electric' modes |
c0274f38 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1985-1986, 1995, 2001-2012 Free Software Foundation, Inc. |
9750e079 | 4 | |
e5167999 ER |
5 | ;; Author: K. Shane Hartman |
6 | ;; Maintainer: FSF | |
fd7fa35a | 7 | ;; Keywords: extensions |
e5167999 | 8 | |
0d20f9a0 JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
0d20f9a0 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
0d20f9a0 JB |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
0d20f9a0 | 23 | |
c8472948 | 24 | ;;; Commentary: |
0d20f9a0 | 25 | |
3b843809 SM |
26 | ;; "Electric" has been used in Emacs to refer to different things. |
27 | ;; Among them: | |
28 | ;; | |
29 | ;; - electric modes and buffers: modes that typically pop-up in a modal kind of | |
30 | ;; way a transient buffer that automatically disappears as soon as the user | |
31 | ;; is done with it. | |
32 | ;; | |
33 | ;; - electric keys: self inserting keys which additionally perform some side | |
34 | ;; operation which happens to be often convenient at that time. Examples of | |
35 | ;; such side operations are: reindenting code, inserting a newline, | |
36 | ;; ... auto-fill-mode and abbrev-mode can be considered as built-in forms of | |
37 | ;; electric key behavior. | |
0d20f9a0 | 38 | |
c8472948 ER |
39 | ;;; Code: |
40 | ||
0d20f9a0 | 41 | ;; This loop is the guts for non-standard modes which retain control |
f135afd3 RS |
42 | ;; until some event occurs. It is a `do-forever', the only way out is |
43 | ;; to throw. It assumes that you have set up the keymap, window, and | |
0d20f9a0 JB |
44 | ;; everything else: all it does is read commands and execute them - |
45 | ;; providing error messages should one occur (if there is no loop | |
46 | ;; function - which see). The required argument is a tag which should | |
f135afd3 RS |
47 | ;; expect a value of nil if the user decides to punt. The second |
48 | ;; argument is the prompt to be used: if nil, use "->", if 'noprompt, | |
49 | ;; don't use a prompt, if a string, use that string as prompt, and if | |
50 | ;; a function of no variable, it will be evaluated in every iteration | |
51 | ;; of the loop and its return value, which can be nil, 'noprompt or a | |
52 | ;; string, will be used as prompt. Given third argument non-nil, it | |
53 | ;; INHIBITS quitting unless the user types C-g at toplevel. This is | |
54 | ;; so user can do things like C-u C-g and not get thrown out. Fourth | |
55 | ;; argument, if non-nil, should be a function of two arguments which | |
56 | ;; is called after every command is executed. The fifth argument, if | |
57 | ;; provided, is the state variable for the function. If the | |
0d20f9a0 JB |
58 | ;; loop-function gets an error, the loop will abort WITHOUT throwing |
59 | ;; (moral: use unwind-protect around call to this function for any | |
60 | ;; critical stuff). The second argument for the loop function is the | |
61 | ;; conditions for any error that occurred or nil if none. | |
62 | ||
63 | (defun Electric-command-loop (return-tag | |
06b60517 | 64 | &optional prompt inhibit-quitting |
0d20f9a0 | 65 | loop-function loop-state) |
f135afd3 | 66 | |
71296446 JB |
67 | (let (cmd |
68 | (err nil) | |
06b60517 | 69 | (inhibit-quit inhibit-quitting) |
f135afd3 | 70 | (prompt-string prompt)) |
0d20f9a0 | 71 | (while t |
ba5bf5f0 | 72 | (if (functionp prompt) |
f135afd3 RS |
73 | (setq prompt-string (funcall prompt))) |
74 | (if (not (stringp prompt-string)) | |
ba5bf5f0 | 75 | (setq prompt-string (unless (eq prompt-string 'noprompt) "->"))) |
f135afd3 | 76 | (setq cmd (read-key-sequence prompt-string)) |
8989a920 | 77 | (setq last-command-event (aref cmd (1- (length cmd))) |
f867d8d3 | 78 | this-command (key-binding cmd t) |
0d20f9a0 | 79 | cmd this-command) |
f867d8d3 RS |
80 | ;; This makes universal-argument-other-key work. |
81 | (setq universal-argument-num-events 0) | |
0d20f9a0 | 82 | (if (or (prog1 quit-flag (setq quit-flag nil)) |
1e4bd40d | 83 | (eq last-input-event ?\C-g)) |
dbc4e1c1 | 84 | (progn (setq unread-command-events nil |
0d20f9a0 | 85 | prefix-arg nil) |
c80e3b4a | 86 | ;; If it wasn't canceling a prefix character, then quit. |
0d20f9a0 JB |
87 | (if (or (= (length (this-command-keys)) 1) |
88 | (not inhibit-quit)) ; safety | |
89 | (progn (ding) | |
90 | (message "Quit") | |
91 | (throw return-tag nil)) | |
92 | (setq cmd nil)))) | |
93 | (setq current-prefix-arg prefix-arg) | |
94 | (if cmd | |
95 | (condition-case conditions | |
96 | (progn (command-execute cmd) | |
17c17ec9 | 97 | (setq last-command this-command) |
0d20f9a0 | 98 | (if (or (prog1 quit-flag (setq quit-flag nil)) |
1e4bd40d | 99 | (eq last-input-event ?\C-g)) |
dbc4e1c1 | 100 | (progn (setq unread-command-events nil) |
0d20f9a0 JB |
101 | (if (not inhibit-quit) |
102 | (progn (ding) | |
103 | (message "Quit") | |
104 | (throw return-tag nil)) | |
105 | (ding))))) | |
106 | (buffer-read-only (if loop-function | |
107 | (setq err conditions) | |
108 | (ding) | |
109 | (message "Buffer is read-only") | |
110 | (sit-for 2))) | |
111 | (beginning-of-buffer (if loop-function | |
112 | (setq err conditions) | |
113 | (ding) | |
114 | (message "Beginning of Buffer") | |
115 | (sit-for 2))) | |
116 | (end-of-buffer (if loop-function | |
117 | (setq err conditions) | |
118 | (ding) | |
119 | (message "End of Buffer") | |
120 | (sit-for 2))) | |
121 | (error (if loop-function | |
122 | (setq err conditions) | |
123 | (ding) | |
124 | (message "Error: %s" | |
125 | (if (eq (car conditions) 'error) | |
126 | (car (cdr conditions)) | |
127 | (prin1-to-string conditions))) | |
128 | (sit-for 2)))) | |
129 | (ding)) | |
130 | (if loop-function (funcall loop-function loop-state err)))) | |
131 | (ding) | |
132 | (throw return-tag nil)) | |
133 | ||
71296446 | 134 | ;; This function is like pop-to-buffer, sort of. |
0d20f9a0 JB |
135 | ;; The algorithm is |
136 | ;; If there is a window displaying buffer | |
137 | ;; Select it | |
138 | ;; Else if there is only one window | |
139 | ;; Split it, selecting the window on the bottom with height being | |
140 | ;; the lesser of max-height (if non-nil) and the number of lines in | |
141 | ;; the buffer to be displayed subject to window-min-height constraint. | |
142 | ;; Else | |
143 | ;; Switch to buffer in the current window. | |
144 | ;; | |
145 | ;; Then if max-height is nil, and not all of the lines in the buffer | |
f98955ea | 146 | ;; are displayed, grab the whole frame. |
0d20f9a0 JB |
147 | ;; |
148 | ;; Returns selected window on buffer positioned at point-min. | |
149 | ||
150 | (defun Electric-pop-up-window (buffer &optional max-height) | |
151 | (let* ((win (or (get-buffer-window buffer) (selected-window))) | |
152 | (buf (get-buffer buffer)) | |
153 | (one-window (one-window-p t)) | |
154 | (pop-up-windows t) | |
c45c149c | 155 | (pop-up-frames nil)) |
0d20f9a0 JB |
156 | (if (not buf) |
157 | (error "Buffer %s does not exist" buffer) | |
0d20f9a0 JB |
158 | (cond ((and (eq (window-buffer win) buf)) |
159 | (select-window win)) | |
160 | (one-window | |
0d20f9a0 | 161 | (pop-to-buffer buffer) |
c45c149c | 162 | (setq win (selected-window))) |
0d20f9a0 JB |
163 | (t |
164 | (switch-to-buffer buf))) | |
48238d1d | 165 | ;; Don't shrink the window, but expand it if necessary. |
0d20f9a0 | 166 | (goto-char (point-min)) |
0e87e61e | 167 | (unless (= (point-max) (window-end win t)) |
48238d1d | 168 | (fit-window-to-buffer win max-height)) |
0d20f9a0 | 169 | win))) |
49116ac0 | 170 | |
3b843809 SM |
171 | ;;; Electric keys. |
172 | ||
173 | (defgroup electricity () | |
174 | "Electric behavior for self inserting keys." | |
175 | :group 'editing) | |
176 | ||
7100ff98 SM |
177 | (defun electric--after-char-pos () |
178 | "Return the position after the char we just inserted. | |
179 | Returns nil when we can't find this char." | |
180 | (let ((pos (point))) | |
181 | (when (or (eq (char-before) last-command-event) ;; Sanity check. | |
182 | (save-excursion | |
183 | (or (progn (skip-chars-backward " \t") | |
184 | (setq pos (point)) | |
185 | (eq (char-before) last-command-event)) | |
186 | (progn (skip-chars-backward " \n\t") | |
187 | (setq pos (point)) | |
188 | (eq (char-before) last-command-event))))) | |
189 | pos))) | |
190 | ||
3b843809 SM |
191 | ;; Electric indentation. |
192 | ||
2122161f SM |
193 | ;; Autoloading variables is generally undesirable, but major modes |
194 | ;; should usually set this variable by adding elements to the default | |
195 | ;; value, which only works well if the variable is preloaded. | |
196 | ;;;###autoload | |
3b843809 | 197 | (defvar electric-indent-chars '(?\n) |
6f5e57e7 SM |
198 | "Characters that should cause automatic reindentation.") |
199 | ||
200 | (defvar electric-indent-functions nil | |
201 | "Special hook run to decide whether to auto-indent. | |
202 | Each function is called with one argument (the inserted char), with | |
203 | point right after that char, and it should return t to cause indentation, | |
204 | `no-indent' to prevent indentation or nil to let other functions decide.") | |
3b843809 SM |
205 | |
206 | (defun electric-indent-post-self-insert-function () | |
207 | ;; FIXME: This reindents the current line, but what we really want instead is | |
208 | ;; to reindent the whole affected text. That's the current line for simple | |
209 | ;; cases, but not all cases. We do take care of the newline case in an | |
210 | ;; ad-hoc fashion, but there are still missing cases such as the case of | |
211 | ;; electric-pair-mode wrapping a region with a pair of parens. | |
212 | ;; There might be a way to get it working by analyzing buffer-undo-list, but | |
213 | ;; it looks challenging. | |
7100ff98 | 214 | (let (pos) |
6f5e57e7 | 215 | (when (and |
cfc7d5da | 216 | electric-indent-mode |
6f5e57e7 SM |
217 | ;; Don't reindent while inserting spaces at beginning of line. |
218 | (or (not (memq last-command-event '(?\s ?\t))) | |
219 | (save-excursion (skip-chars-backward " \t") (not (bolp)))) | |
220 | (setq pos (electric--after-char-pos)) | |
221 | (save-excursion | |
222 | (goto-char pos) | |
223 | (let ((act (or (run-hook-with-args-until-success | |
224 | 'electric-indent-functions | |
225 | last-command-event) | |
226 | (memq last-command-event electric-indent-chars)))) | |
227 | (not | |
228 | (or (memq act '(nil no-indent)) | |
229 | ;; In a string or comment. | |
230 | (unless (eq act 'do-indent) (nth 8 (syntax-ppss)))))))) | |
7100ff98 SM |
231 | ;; For newline, we want to reindent both lines and basically behave like |
232 | ;; reindent-then-newline-and-indent (whose code we hence copied). | |
a733fc37 | 233 | (when (< (1- pos) (line-beginning-position)) |
7100ff98 SM |
234 | (let ((before (copy-marker (1- pos) t))) |
235 | (save-excursion | |
a733fc37 | 236 | (unless (memq indent-line-function |
74a10be5 | 237 | '(indent-relative indent-to-left-margin |
6f5e57e7 | 238 | indent-relative-maybe)) |
a733fc37 SM |
239 | ;; Don't reindent the previous line if the indentation function |
240 | ;; is not a real one. | |
241 | (goto-char before) | |
242 | (indent-according-to-mode)) | |
7100ff98 SM |
243 | ;; We are at EOL before the call to indent-according-to-mode, and |
244 | ;; after it we usually are as well, but not always. We tried to | |
245 | ;; address it with `save-excursion' but that uses a normal marker | |
246 | ;; whereas we need `move after insertion', so we do the | |
247 | ;; save/restore by hand. | |
248 | (goto-char before) | |
249 | ;; Remove the trailing whitespace after indentation because | |
250 | ;; indentation may (re)introduce the whitespace. | |
251 | (delete-horizontal-space t)))) | |
74a10be5 SM |
252 | (unless (memq indent-line-function '(indent-to-left-margin)) |
253 | (indent-according-to-mode))))) | |
3b843809 SM |
254 | |
255 | ;;;###autoload | |
256 | (define-minor-mode electric-indent-mode | |
06e21633 CY |
257 | "Toggle on-the-fly reindentation (Electric Indent mode). |
258 | With a prefix argument ARG, enable Electric Indent mode if ARG is | |
259 | positive, and disable it otherwise. If called from Lisp, enable | |
260 | the mode if ARG is omitted or nil. | |
261 | ||
a075a2c5 GM |
262 | This is a global minor mode. When enabled, it reindents whenever |
263 | the hook `electric-indent-functions' returns non-nil, or you | |
264 | insert a character from `electric-indent-chars'." | |
3b843809 SM |
265 | :global t |
266 | :group 'electricity | |
24f3d7b9 SM |
267 | (if (not electric-indent-mode) |
268 | (remove-hook 'post-self-insert-hook | |
269 | #'electric-indent-post-self-insert-function) | |
270 | ;; post-self-insert-hooks interact in non-trivial ways. | |
271 | ;; It turns out that electric-indent-mode generally works better if run | |
272 | ;; late, but still before blink-paren. | |
273 | (add-hook 'post-self-insert-hook | |
274 | #'electric-indent-post-self-insert-function | |
275 | 'append) | |
276 | ;; FIXME: Ugly! | |
277 | (let ((bp (memq #'blink-paren-post-self-insert-function | |
278 | (default-value 'post-self-insert-hook)))) | |
279 | (when (memq #'electric-indent-post-self-insert-function bp) | |
280 | (setcar bp #'electric-indent-post-self-insert-function) | |
281 | (setcdr bp (cons #'blink-paren-post-self-insert-function | |
282 | (delq #'electric-indent-post-self-insert-function | |
283 | (cdr bp)))))))) | |
3b843809 SM |
284 | |
285 | ;; Electric pairing. | |
286 | ||
c51bb5d2 SM |
287 | (defcustom electric-pair-pairs |
288 | '((?\" . ?\")) | |
289 | "Alist of pairs that should be used regardless of major mode." | |
a075a2c5 GM |
290 | :group 'electricity |
291 | :version "24.1" | |
c51bb5d2 SM |
292 | :type '(repeat (cons character character))) |
293 | ||
3b843809 SM |
294 | (defcustom electric-pair-skip-self t |
295 | "If non-nil, skip char instead of inserting a second closing paren. | |
296 | When inserting a closing paren character right before the same character, | |
297 | just skip that character instead, so that hitting ( followed by ) results | |
298 | in \"()\" rather than \"())\". | |
299 | This can be convenient for people who find it easier to hit ) than C-f." | |
a075a2c5 GM |
300 | :group 'electricity |
301 | :version "24.1" | |
3b843809 SM |
302 | :type 'boolean) |
303 | ||
b1d39ccc SL |
304 | (defun electric-pair-syntax (command-event) |
305 | (and electric-pair-mode | |
306 | (let ((x (assq command-event electric-pair-pairs))) | |
307 | (cond | |
308 | (x (if (eq (car x) (cdr x)) ?\" ?\()) | |
309 | ((rassq command-event electric-pair-pairs) ?\)) | |
310 | (t (char-syntax command-event)))))) | |
311 | ||
3b843809 SM |
312 | (defun electric-pair-post-self-insert-function () |
313 | (let* ((syntax (and (eq (char-before) last-command-event) ; Sanity check. | |
b1d39ccc | 314 | (electric-pair-syntax last-command-event))) |
3b843809 SM |
315 | ;; FIXME: when inserting the closer, we should maybe use |
316 | ;; self-insert-command, although it may prove tricky running | |
317 | ;; post-self-insert-hook recursively, and we wouldn't want to trigger | |
318 | ;; blink-matching-open. | |
319 | (closer (if (eq syntax ?\() | |
c51bb5d2 SM |
320 | (cdr (or (assq last-command-event electric-pair-pairs) |
321 | (aref (syntax-table) last-command-event))) | |
3b843809 SM |
322 | last-command-event))) |
323 | (cond | |
324 | ;; Wrap a pair around the active region. | |
325 | ((and (memq syntax '(?\( ?\" ?\$)) (use-region-p)) | |
326 | (if (> (mark) (point)) | |
327 | (goto-char (mark)) | |
b5cf7fc4 CY |
328 | ;; We already inserted the open-paren but at the end of the |
329 | ;; region, so we have to remove it and start over. | |
330 | (delete-char -1) | |
331 | (save-excursion | |
3b843809 | 332 | (goto-char (mark)) |
b5cf7fc4 CY |
333 | ;; Do not insert after `save-excursion' marker (Bug#11520). |
334 | (insert-before-markers last-command-event))) | |
3b843809 SM |
335 | (insert closer)) |
336 | ;; Backslash-escaped: no pairing, no skipping. | |
337 | ((save-excursion | |
338 | (goto-char (1- (point))) | |
339 | (not (zerop (% (skip-syntax-backward "\\") 2)))) | |
340 | nil) | |
341 | ;; Skip self. | |
342 | ((and (memq syntax '(?\) ?\" ?\$)) | |
343 | electric-pair-skip-self | |
344 | (eq (char-after) last-command-event)) | |
345 | ;; This is too late: rather than insert&delete we'd want to only skip (or | |
346 | ;; insert in overwrite mode). The difference is in what goes in the | |
347 | ;; undo-log and in the intermediate state which might be visible to other | |
348 | ;; post-self-insert-hook. We'll just have to live with it for now. | |
349 | (delete-char 1)) | |
350 | ;; Insert matching pair. | |
351 | ((not (or (not (memq syntax `(?\( ?\" ?\$))) | |
352 | overwrite-mode | |
353 | ;; I find it more often preferable not to pair when the | |
354 | ;; same char is next. | |
355 | (eq last-command-event (char-after)) | |
356 | (eq last-command-event (char-before (1- (point)))) | |
357 | ;; I also find it often preferable not to pair next to a word. | |
358 | (eq (char-syntax (following-char)) ?w))) | |
359 | (save-excursion (insert closer)))))) | |
360 | ||
c77d37e2 SM |
361 | (defun electric-pair-will-use-region () |
362 | (and (use-region-p) | |
363 | (memq (electric-pair-syntax last-command-event) '(?\( ?\" ?\$)))) | |
b1d39ccc | 364 | |
3b843809 SM |
365 | ;;;###autoload |
366 | (define-minor-mode electric-pair-mode | |
06e21633 CY |
367 | "Toggle automatic parens pairing (Electric Pair mode). |
368 | With a prefix argument ARG, enable Electric Pair mode if ARG is | |
369 | positive, and disable it otherwise. If called from Lisp, enable | |
370 | the mode if ARG is omitted or nil. | |
371 | ||
372 | Electric Pair mode is a global minor mode. When enabled, typing | |
373 | an open parenthesis automatically inserts the corresponding | |
a075a2c5 GM |
374 | closing parenthesis. \(Likewise for brackets, etc.) |
375 | ||
376 | See options `electric-pair-pairs' and `electric-pair-skip-self'." | |
3b843809 SM |
377 | :global t |
378 | :group 'electricity | |
379 | (if electric-pair-mode | |
b1d39ccc | 380 | (progn |
b1d39ccc SL |
381 | (add-hook 'post-self-insert-hook |
382 | #'electric-pair-post-self-insert-function) | |
c77d37e2 SM |
383 | (add-hook 'self-insert-uses-region-functions |
384 | #'electric-pair-will-use-region)) | |
3b843809 | 385 | (remove-hook 'post-self-insert-hook |
b1d39ccc | 386 | #'electric-pair-post-self-insert-function) |
c77d37e2 SM |
387 | (remove-hook 'self-insert-uses-region-functions |
388 | #'electric-pair-will-use-region))) | |
7100ff98 SM |
389 | |
390 | ;; Automatically add newlines after/before/around some chars. | |
391 | ||
392 | (defvar electric-layout-rules '() | |
393 | "List of rules saying where to automatically insert newlines. | |
394 | Each rule has the form (CHAR . WHERE) where CHAR is the char | |
395 | that was just inserted and WHERE specifies where to insert newlines | |
a075a2c5 GM |
396 | and can be: nil, `before', `after', `around', or a function of no |
397 | arguments that returns one of those symbols.") | |
7100ff98 SM |
398 | |
399 | (defun electric-layout-post-self-insert-function () | |
400 | (let* ((rule (cdr (assq last-command-event electric-layout-rules))) | |
401 | pos) | |
402 | (when (and rule | |
403 | (setq pos (electric--after-char-pos)) | |
404 | ;; Not in a string or comment. | |
405 | (not (nth 8 (save-excursion (syntax-ppss pos))))) | |
406 | (let ((end (copy-marker (point) t))) | |
407 | (goto-char pos) | |
f58e0fd5 | 408 | (pcase (if (functionp rule) (funcall rule) rule) |
7100ff98 SM |
409 | ;; FIXME: we used `newline' down here which called |
410 | ;; self-insert-command and ran post-self-insert-hook recursively. | |
411 | ;; It happened to make electric-indent-mode work automatically with | |
412 | ;; electric-layout-mode (at the cost of re-indenting lines | |
413 | ;; multiple times), but I'm not sure it's what we want. | |
f58e0fd5 | 414 | (`before (goto-char (1- pos)) (skip-chars-backward " \t") |
c51bb5d2 | 415 | (unless (bolp) (insert "\n"))) |
f58e0fd5 SM |
416 | (`after (insert "\n")) ; FIXME: check eolp before inserting \n? |
417 | (`around (save-excursion | |
c51bb5d2 SM |
418 | (goto-char (1- pos)) (skip-chars-backward " \t") |
419 | (unless (bolp) (insert "\n"))) | |
420 | (insert "\n"))) ; FIXME: check eolp before inserting \n? | |
7100ff98 SM |
421 | (goto-char end))))) |
422 | ||
423 | ;;;###autoload | |
424 | (define-minor-mode electric-layout-mode | |
e1ac4066 GM |
425 | "Automatically insert newlines around some chars. |
426 | With a prefix argument ARG, enable Electric Layout mode if ARG is | |
427 | positive, and disable it otherwise. If called from Lisp, enable | |
a075a2c5 GM |
428 | the mode if ARG is omitted or nil. |
429 | The variable `electric-layout-rules' says when and how to insert newlines." | |
7100ff98 SM |
430 | :global t |
431 | :group 'electricity | |
432 | (if electric-layout-mode | |
433 | (add-hook 'post-self-insert-hook | |
434 | #'electric-layout-post-self-insert-function) | |
435 | (remove-hook 'post-self-insert-hook | |
436 | #'electric-layout-post-self-insert-function))) | |
437 | ||
c0274f38 ER |
438 | (provide 'electric) |
439 | ||
c8472948 | 440 | ;;; electric.el ends here |