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