Mercurial > emacs
annotate lisp/electric.el @ 110211:6cdcc53703e4
mail-source.el (mail-source-fetch): Don't message if we're fetching mail from a file, and the file doesn't exist.
author | Katsumi Yamaoka <yamaoka@jpl.org> |
---|---|
date | Sun, 05 Sep 2010 23:38:33 +0000 |
parents | 92e4fa270c92 |
children | e6399f46aefa |
rev | line source |
---|---|
38412
253f761ad37b
Some fixes to follow coding conventions in files maintained by FSF.
Pavel Janík <Pavel@Janik.cz>
parents:
14941
diff
changeset
|
1 ;;; electric.el --- window maker and Command loop for `electric' modes |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
74439 | 3 ;; Copyright (C) 1985, 1986, 1995, 2001, 2002, 2003, 2004, |
106815 | 4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
845 | 5 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
6 ;; Author: K. Shane Hartman |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
7 ;; Maintainer: FSF |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
8 ;; Keywords: extensions |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
9 |
35 | 10 ;; This file is part of GNU Emacs. |
11 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
35 | 13 ;; it under the terms of the GNU General Public License as published by |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; (at your option) any later version. |
35 | 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 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
35 | 24 |
2229
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
25 ;;; Commentary: |
35 | 26 |
110163
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
27 ;; "Electric" has been used in Emacs to refer to different things. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
28 ;; Among them: |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
29 ;; |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
30 ;; - electric modes and buffers: modes that typically pop-up in a modal kind of |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
31 ;; way a transient buffer that automatically disappears as soon as the user |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
32 ;; is done with it. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
33 ;; |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
34 ;; - electric keys: self inserting keys which additionally perform some side |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
35 ;; operation which happens to be often convenient at that time. Examples of |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
36 ;; such side operations are: reindenting code, inserting a newline, |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
37 ;; ... auto-fill-mode and abbrev-mode can be considered as built-in forms of |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
38 ;; electric key behavior. |
35 | 39 |
2229
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
40 ;;; Code: |
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
41 |
110163
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
42 (eval-when-compile (require 'cl)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
43 |
35 | 44 ;; This loop is the guts for non-standard modes which retain control |
13260
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
45 ;; until some event occurs. It is a `do-forever', the only way out is |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
46 ;; to throw. It assumes that you have set up the keymap, window, and |
35 | 47 ;; everything else: all it does is read commands and execute them - |
48 ;; providing error messages should one occur (if there is no loop | |
49 ;; function - which see). The required argument is a tag which should | |
13260
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
50 ;; expect a value of nil if the user decides to punt. The second |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
51 ;; argument is the prompt to be used: if nil, use "->", if 'noprompt, |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
52 ;; don't use a prompt, if a string, use that string as prompt, and if |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
53 ;; a function of no variable, it will be evaluated in every iteration |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
54 ;; of the loop and its return value, which can be nil, 'noprompt or a |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
55 ;; string, will be used as prompt. Given third argument non-nil, it |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
56 ;; INHIBITS quitting unless the user types C-g at toplevel. This is |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
57 ;; so user can do things like C-u C-g and not get thrown out. Fourth |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
58 ;; argument, if non-nil, should be a function of two arguments which |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
59 ;; is called after every command is executed. The fifth argument, if |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
60 ;; provided, is the state variable for the function. If the |
35 | 61 ;; loop-function gets an error, the loop will abort WITHOUT throwing |
62 ;; (moral: use unwind-protect around call to this function for any | |
63 ;; critical stuff). The second argument for the loop function is the | |
64 ;; conditions for any error that occurred or nil if none. | |
65 | |
66 (defun Electric-command-loop (return-tag | |
67 &optional prompt inhibit-quit | |
68 loop-function loop-state) | |
13260
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
69 |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38412
diff
changeset
|
70 (let (cmd |
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38412
diff
changeset
|
71 (err nil) |
13260
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
72 (prompt-string prompt)) |
35 | 73 (while t |
108309
8e422499f0ff
Misc code tweaks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
106815
diff
changeset
|
74 (if (functionp prompt) |
13260
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
75 (setq prompt-string (funcall prompt))) |
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
76 (if (not (stringp prompt-string)) |
108309
8e422499f0ff
Misc code tweaks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
106815
diff
changeset
|
77 (setq prompt-string (unless (eq prompt-string 'noprompt) "->"))) |
13260
a10616f0ad17
(Electric-command-loop): Don't display a prompt
Richard M. Stallman <rms@gnu.org>
parents:
3439
diff
changeset
|
78 (setq cmd (read-key-sequence prompt-string)) |
101010
4efc7ca085ce
Replace last-command-char with last-command-event.
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
79 (setq last-command-event (aref cmd (1- (length cmd))) |
14941
7e8ee35d0fb9
(Electric-command-loop): Set universal-argument-other-key to 0.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
80 this-command (key-binding cmd t) |
35 | 81 cmd this-command) |
14941
7e8ee35d0fb9
(Electric-command-loop): Set universal-argument-other-key to 0.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
82 ;; This makes universal-argument-other-key work. |
7e8ee35d0fb9
(Electric-command-loop): Set universal-argument-other-key to 0.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
83 (setq universal-argument-num-events 0) |
35 | 84 (if (or (prog1 quit-flag (setq quit-flag nil)) |
101013
dbe70c3aa01d
Replace last-input-char with last-input-event.
Glenn Morris <rgm@gnu.org>
parents:
101010
diff
changeset
|
85 (eq last-input-event ?\C-g)) |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1609
diff
changeset
|
86 (progn (setq unread-command-events nil |
35 | 87 prefix-arg nil) |
88 ;; If it wasn't cancelling a prefix character, then quit. | |
89 (if (or (= (length (this-command-keys)) 1) | |
90 (not inhibit-quit)) ; safety | |
91 (progn (ding) | |
92 (message "Quit") | |
93 (throw return-tag nil)) | |
94 (setq cmd nil)))) | |
95 (setq current-prefix-arg prefix-arg) | |
96 (if cmd | |
97 (condition-case conditions | |
98 (progn (command-execute cmd) | |
703
e2780d9a814c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
99 (setq last-command this-command) |
35 | 100 (if (or (prog1 quit-flag (setq quit-flag nil)) |
101013
dbe70c3aa01d
Replace last-input-char with last-input-event.
Glenn Morris <rgm@gnu.org>
parents:
101010
diff
changeset
|
101 (eq last-input-event ?\C-g)) |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1609
diff
changeset
|
102 (progn (setq unread-command-events nil) |
35 | 103 (if (not inhibit-quit) |
104 (progn (ding) | |
105 (message "Quit") | |
106 (throw return-tag nil)) | |
107 (ding))))) | |
108 (buffer-read-only (if loop-function | |
109 (setq err conditions) | |
110 (ding) | |
111 (message "Buffer is read-only") | |
112 (sit-for 2))) | |
113 (beginning-of-buffer (if loop-function | |
114 (setq err conditions) | |
115 (ding) | |
116 (message "Beginning of Buffer") | |
117 (sit-for 2))) | |
118 (end-of-buffer (if loop-function | |
119 (setq err conditions) | |
120 (ding) | |
121 (message "End of Buffer") | |
122 (sit-for 2))) | |
123 (error (if loop-function | |
124 (setq err conditions) | |
125 (ding) | |
126 (message "Error: %s" | |
127 (if (eq (car conditions) 'error) | |
128 (car (cdr conditions)) | |
129 (prin1-to-string conditions))) | |
130 (sit-for 2)))) | |
131 (ding)) | |
132 (if loop-function (funcall loop-function loop-state err)))) | |
133 (ding) | |
134 (throw return-tag nil)) | |
135 | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38412
diff
changeset
|
136 ;; This function is like pop-to-buffer, sort of. |
35 | 137 ;; The algorithm is |
138 ;; If there is a window displaying buffer | |
139 ;; Select it | |
140 ;; Else if there is only one window | |
141 ;; Split it, selecting the window on the bottom with height being | |
142 ;; the lesser of max-height (if non-nil) and the number of lines in | |
143 ;; the buffer to be displayed subject to window-min-height constraint. | |
144 ;; Else | |
145 ;; Switch to buffer in the current window. | |
146 ;; | |
147 ;; Then if max-height is nil, and not all of the lines in the buffer | |
778 | 148 ;; are displayed, grab the whole frame. |
35 | 149 ;; |
150 ;; Returns selected window on buffer positioned at point-min. | |
151 | |
152 (defun Electric-pop-up-window (buffer &optional max-height) | |
153 (let* ((win (or (get-buffer-window buffer) (selected-window))) | |
154 (buf (get-buffer buffer)) | |
155 (one-window (one-window-p t)) | |
156 (pop-up-windows t) | |
59315
c5ee87e89147
(Electric-pop-up-window): Use fit-window-to-buffer
Richard M. Stallman <rms@gnu.org>
parents:
54575
diff
changeset
|
157 (pop-up-frames nil)) |
35 | 158 (if (not buf) |
159 (error "Buffer %s does not exist" buffer) | |
160 (cond ((and (eq (window-buffer win) buf)) | |
161 (select-window win)) | |
162 (one-window | |
163 (pop-to-buffer buffer) | |
59315
c5ee87e89147
(Electric-pop-up-window): Use fit-window-to-buffer
Richard M. Stallman <rms@gnu.org>
parents:
54575
diff
changeset
|
164 (setq win (selected-window))) |
35 | 165 (t |
166 (switch-to-buffer buf))) | |
99234
770ae8d8f7f4
(Electric-pop-up-window): Don't shrink the window if it's already big
Chong Yidong <cyd@stupidchicken.com>
parents:
94678
diff
changeset
|
167 ;; Don't shrink the window, but expand it if necessary. |
35 | 168 (goto-char (point-min)) |
99362
b162bc232856
(Electric-pop-up-window): Call window-end with UPDATE set to t.
Martin Rudalics <rudalics@gmx.at>
parents:
99234
diff
changeset
|
169 (unless (= (point-max) (window-end win t)) |
99234
770ae8d8f7f4
(Electric-pop-up-window): Don't shrink the window if it's already big
Chong Yidong <cyd@stupidchicken.com>
parents:
94678
diff
changeset
|
170 (fit-window-to-buffer win max-height)) |
35 | 171 win))) |
584 | 172 |
110163
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
173 ;;; Electric keys. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
174 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
175 (defgroup electricity () |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
176 "Electric behavior for self inserting keys." |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
177 :group 'editing) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
178 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
179 ;; Electric indentation. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
180 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
181 (defvar electric-indent-chars '(?\n) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
182 "Characters that should cause automatic reindentation.") |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
183 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
184 (defun electric-indent-post-self-insert-function () |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
185 ;; FIXME: This reindents the current line, but what we really want instead is |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
186 ;; to reindent the whole affected text. That's the current line for simple |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
187 ;; cases, but not all cases. We do take care of the newline case in an |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
188 ;; ad-hoc fashion, but there are still missing cases such as the case of |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
189 ;; electric-pair-mode wrapping a region with a pair of parens. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
190 ;; There might be a way to get it working by analyzing buffer-undo-list, but |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
191 ;; it looks challenging. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
192 (when (and (memq last-command-event electric-indent-chars) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
193 ;; Don't reindent while inserting spaces at beginning of line. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
194 (or (not (memq last-command-event '(?\s ?\t))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
195 (save-excursion (skip-chars-backward " \t") (not (bolp)))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
196 ;; Not in a string or comment. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
197 (not (nth 8 (syntax-ppss)))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
198 ;; For newline, we want to reindent both lines and basically behave like |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
199 ;; reindent-then-newline-and-indent (whose code we hence copied). |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
200 (when (and (eq last-command-event ?\n) |
110188
92e4fa270c92
* lisp/electric.el (electric-indent-post-self-insert-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
110163
diff
changeset
|
201 ;; Don't reindent the previous line if the indentation function |
92e4fa270c92
* lisp/electric.el (electric-indent-post-self-insert-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
110163
diff
changeset
|
202 ;; is not a real one. |
92e4fa270c92
* lisp/electric.el (electric-indent-post-self-insert-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
110163
diff
changeset
|
203 (not (memq indent-line-function |
92e4fa270c92
* lisp/electric.el (electric-indent-post-self-insert-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
110163
diff
changeset
|
204 '(indent-relative indent-relative-maybe))) |
110163
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
205 ;; Sanity check. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
206 (eq (char-before) last-command-event)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
207 (let ((pos (copy-marker (1- (point)) t))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
208 (save-excursion |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
209 (goto-char pos) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
210 (indent-according-to-mode) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
211 ;; We are at EOL before the call to indent-according-to-mode, and |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
212 ;; after it we usually are as well, but not always. We tried to |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
213 ;; address it with `save-excursion' but that uses a normal marker |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
214 ;; whereas we need `move after insertion', so we do the |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
215 ;; save/restore by hand. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
216 (goto-char pos) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
217 ;; Remove the trailing whitespace after indentation because |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
218 ;; indentation may (re)introduce the whitespace. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
219 (delete-horizontal-space t)))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
220 (indent-according-to-mode))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
221 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
222 ;;;###autoload |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
223 (define-minor-mode electric-indent-mode |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
224 "Automatically reindent lines of code when inserting particular chars. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
225 `electric-indent-chars' specifies the set of chars that should cause reindentation." |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
226 :global t |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
227 :group 'electricity |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
228 (if electric-indent-mode |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
229 (add-hook 'post-self-insert-hook |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
230 #'electric-indent-post-self-insert-function) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
231 (remove-hook 'post-self-insert-hook |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
232 #'electric-indent-post-self-insert-function))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
233 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
234 ;; Electric pairing. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
235 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
236 (defcustom electric-pair-skip-self t |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
237 "If non-nil, skip char instead of inserting a second closing paren. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
238 When inserting a closing paren character right before the same character, |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
239 just skip that character instead, so that hitting ( followed by ) results |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
240 in \"()\" rather than \"())\". |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
241 This can be convenient for people who find it easier to hit ) than C-f." |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
242 :type 'boolean) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
243 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
244 (defun electric-pair-post-self-insert-function () |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
245 (let* ((syntax (and (eq (char-before) last-command-event) ; Sanity check. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
246 (char-syntax last-command-event))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
247 ;; FIXME: when inserting the closer, we should maybe use |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
248 ;; self-insert-command, although it may prove tricky running |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
249 ;; post-self-insert-hook recursively, and we wouldn't want to trigger |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
250 ;; blink-matching-open. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
251 (closer (if (eq syntax ?\() |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
252 (cdr (aref (syntax-table) last-command-event)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
253 last-command-event))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
254 (cond |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
255 ;; Wrap a pair around the active region. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
256 ((and (memq syntax '(?\( ?\" ?\$)) (use-region-p)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
257 (if (> (mark) (point)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
258 (goto-char (mark)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
259 ;; We already inserted the open-paren but at the end of the region, |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
260 ;; so we have to remove it and start over. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
261 (delete-char -1) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
262 (save-excursion |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
263 (goto-char (mark)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
264 (insert last-command-event))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
265 (insert closer)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
266 ;; Backslash-escaped: no pairing, no skipping. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
267 ((save-excursion |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
268 (goto-char (1- (point))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
269 (not (zerop (% (skip-syntax-backward "\\") 2)))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
270 nil) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
271 ;; Skip self. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
272 ((and (memq syntax '(?\) ?\" ?\$)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
273 electric-pair-skip-self |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
274 (eq (char-after) last-command-event)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
275 ;; This is too late: rather than insert&delete we'd want to only skip (or |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
276 ;; insert in overwrite mode). The difference is in what goes in the |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
277 ;; undo-log and in the intermediate state which might be visible to other |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
278 ;; post-self-insert-hook. We'll just have to live with it for now. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
279 (delete-char 1)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
280 ;; Insert matching pair. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
281 ((not (or (not (memq syntax `(?\( ?\" ?\$))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
282 overwrite-mode |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
283 ;; I find it more often preferable not to pair when the |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
284 ;; same char is next. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
285 (eq last-command-event (char-after)) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
286 (eq last-command-event (char-before (1- (point)))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
287 ;; I also find it often preferable not to pair next to a word. |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
288 (eq (char-syntax (following-char)) ?w))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
289 (save-excursion (insert closer)))))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
290 |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
291 ;;;###autoload |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
292 (define-minor-mode electric-pair-mode |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
293 "Automatically pair-up parens when inserting an open paren." |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
294 :global t |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
295 :group 'electricity |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
296 (if electric-pair-mode |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
297 (add-hook 'post-self-insert-hook |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
298 #'electric-pair-post-self-insert-function) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
299 (remove-hook 'post-self-insert-hook |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
300 #'electric-pair-post-self-insert-function))) |
c9b982e5aefd
* lisp/electric.el (electricity): New group.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
108309
diff
changeset
|
301 |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
302 (provide 'electric) |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
303 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
79721
diff
changeset
|
304 ;; arch-tag: dae045eb-dc2d-4fb7-9f27-9cc2ce277be8 |
2229
bd3c525fa6fc
Added standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
305 ;;; electric.el ends here |