* lisp/url/url-vars.el (url-automatic-caching): Remove unused variable.
[emacs.git] / lisp / electric.el
blob8e9d23be231500ce99abea7b4b96ca78c48989e5
1 ;;; electric.el --- window maker and Command loop for `electric' modes
3 ;; Copyright (C) 1985, 1986, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: K. Shane Hartman
7 ;; Maintainer: FSF
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; "Electric" has been used in Emacs to refer to different things.
28 ;; Among them:
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.
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.
40 ;;; Code:
42 (eval-when-compile (require 'cl))
44 ;; This loop is the guts for non-standard modes which retain control
45 ;; until some event occurs. It is a `do-forever', the only way out is
46 ;; to throw. It assumes that you have set up the keymap, window, and
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
50 ;; expect a value of nil if the user decides to punt. The second
51 ;; argument is the prompt to be used: if nil, use "->", if 'noprompt,
52 ;; don't use a prompt, if a string, use that string as prompt, and if
53 ;; a function of no variable, it will be evaluated in every iteration
54 ;; of the loop and its return value, which can be nil, 'noprompt or a
55 ;; string, will be used as prompt. Given third argument non-nil, it
56 ;; INHIBITS quitting unless the user types C-g at toplevel. This is
57 ;; so user can do things like C-u C-g and not get thrown out. Fourth
58 ;; argument, if non-nil, should be a function of two arguments which
59 ;; is called after every command is executed. The fifth argument, if
60 ;; provided, is the state variable for the function. If the
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.
66 (defun Electric-command-loop (return-tag
67 &optional prompt inhibit-quit
68 loop-function loop-state)
70 (let (cmd
71 (err nil)
72 (prompt-string prompt))
73 (while t
74 (if (functionp prompt)
75 (setq prompt-string (funcall prompt)))
76 (if (not (stringp prompt-string))
77 (setq prompt-string (unless (eq prompt-string 'noprompt) "->")))
78 (setq cmd (read-key-sequence prompt-string))
79 (setq last-command-event (aref cmd (1- (length cmd)))
80 this-command (key-binding cmd t)
81 cmd this-command)
82 ;; This makes universal-argument-other-key work.
83 (setq universal-argument-num-events 0)
84 (if (or (prog1 quit-flag (setq quit-flag nil))
85 (eq last-input-event ?\C-g))
86 (progn (setq unread-command-events nil
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)
99 (setq last-command this-command)
100 (if (or (prog1 quit-flag (setq quit-flag nil))
101 (eq last-input-event ?\C-g))
102 (progn (setq unread-command-events nil)
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))
136 ;; This function is like pop-to-buffer, sort of.
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.
147 ;; Then if max-height is nil, and not all of the lines in the buffer
148 ;; are displayed, grab the whole frame.
150 ;; Returns selected window on buffer positioned at point-min.
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)
157 (pop-up-frames nil))
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)
164 (setq win (selected-window)))
166 (switch-to-buffer buf)))
167 ;; Don't shrink the window, but expand it if necessary.
168 (goto-char (point-min))
169 (unless (= (point-max) (window-end win t))
170 (fit-window-to-buffer win max-height))
171 win)))
173 ;;; Electric keys.
175 (defgroup electricity ()
176 "Electric behavior for self inserting keys."
177 :group 'editing)
179 ;; Electric indentation.
181 (defvar electric-indent-chars '(?\n)
182 "Characters that should cause automatic reindentation.")
184 (defun electric-indent-post-self-insert-function ()
185 ;; FIXME: This reindents the current line, but what we really want instead is
186 ;; to reindent the whole affected text. That's the current line for simple
187 ;; cases, but not all cases. We do take care of the newline case in an
188 ;; ad-hoc fashion, but there are still missing cases such as the case of
189 ;; electric-pair-mode wrapping a region with a pair of parens.
190 ;; There might be a way to get it working by analyzing buffer-undo-list, but
191 ;; it looks challenging.
192 (when (and (memq last-command-event electric-indent-chars)
193 ;; Don't reindent while inserting spaces at beginning of line.
194 (or (not (memq last-command-event '(?\s ?\t)))
195 (save-excursion (skip-chars-backward " \t") (not (bolp))))
196 ;; Not in a string or comment.
197 (not (nth 8 (syntax-ppss))))
198 ;; For newline, we want to reindent both lines and basically behave like
199 ;; reindent-then-newline-and-indent (whose code we hence copied).
200 (when (and (eq last-command-event ?\n)
201 ;; Don't reindent the previous line if the indentation function
202 ;; is not a real one.
203 (not (memq indent-line-function
204 '(indent-relative indent-relative-maybe)))
205 ;; Sanity check.
206 (eq (char-before) last-command-event))
207 (let ((pos (copy-marker (1- (point)) t)))
208 (save-excursion
209 (goto-char pos)
210 (indent-according-to-mode)
211 ;; We are at EOL before the call to indent-according-to-mode, and
212 ;; after it we usually are as well, but not always. We tried to
213 ;; address it with `save-excursion' but that uses a normal marker
214 ;; whereas we need `move after insertion', so we do the
215 ;; save/restore by hand.
216 (goto-char pos)
217 ;; Remove the trailing whitespace after indentation because
218 ;; indentation may (re)introduce the whitespace.
219 (delete-horizontal-space t))))
220 (indent-according-to-mode)))
222 ;;;###autoload
223 (define-minor-mode electric-indent-mode
224 "Automatically reindent lines of code when inserting particular chars.
225 `electric-indent-chars' specifies the set of chars that should cause reindentation."
226 :global t
227 :group 'electricity
228 (if electric-indent-mode
229 (add-hook 'post-self-insert-hook
230 #'electric-indent-post-self-insert-function)
231 (remove-hook 'post-self-insert-hook
232 #'electric-indent-post-self-insert-function)))
234 ;; Electric pairing.
236 (defcustom electric-pair-skip-self t
237 "If non-nil, skip char instead of inserting a second closing paren.
238 When inserting a closing paren character right before the same character,
239 just skip that character instead, so that hitting ( followed by ) results
240 in \"()\" rather than \"())\".
241 This can be convenient for people who find it easier to hit ) than C-f."
242 :type 'boolean)
244 (defun electric-pair-post-self-insert-function ()
245 (let* ((syntax (and (eq (char-before) last-command-event) ; Sanity check.
246 (char-syntax last-command-event)))
247 ;; FIXME: when inserting the closer, we should maybe use
248 ;; self-insert-command, although it may prove tricky running
249 ;; post-self-insert-hook recursively, and we wouldn't want to trigger
250 ;; blink-matching-open.
251 (closer (if (eq syntax ?\()
252 (cdr (aref (syntax-table) last-command-event))
253 last-command-event)))
254 (cond
255 ;; Wrap a pair around the active region.
256 ((and (memq syntax '(?\( ?\" ?\$)) (use-region-p))
257 (if (> (mark) (point))
258 (goto-char (mark))
259 ;; We already inserted the open-paren but at the end of the region,
260 ;; so we have to remove it and start over.
261 (delete-char -1)
262 (save-excursion
263 (goto-char (mark))
264 (insert last-command-event)))
265 (insert closer))
266 ;; Backslash-escaped: no pairing, no skipping.
267 ((save-excursion
268 (goto-char (1- (point)))
269 (not (zerop (% (skip-syntax-backward "\\") 2))))
270 nil)
271 ;; Skip self.
272 ((and (memq syntax '(?\) ?\" ?\$))
273 electric-pair-skip-self
274 (eq (char-after) last-command-event))
275 ;; This is too late: rather than insert&delete we'd want to only skip (or
276 ;; insert in overwrite mode). The difference is in what goes in the
277 ;; undo-log and in the intermediate state which might be visible to other
278 ;; post-self-insert-hook. We'll just have to live with it for now.
279 (delete-char 1))
280 ;; Insert matching pair.
281 ((not (or (not (memq syntax `(?\( ?\" ?\$)))
282 overwrite-mode
283 ;; I find it more often preferable not to pair when the
284 ;; same char is next.
285 (eq last-command-event (char-after))
286 (eq last-command-event (char-before (1- (point))))
287 ;; I also find it often preferable not to pair next to a word.
288 (eq (char-syntax (following-char)) ?w)))
289 (save-excursion (insert closer))))))
291 ;;;###autoload
292 (define-minor-mode electric-pair-mode
293 "Automatically pair-up parens when inserting an open paren."
294 :global t
295 :group 'electricity
296 (if electric-pair-mode
297 (add-hook 'post-self-insert-hook
298 #'electric-pair-post-self-insert-function)
299 (remove-hook 'post-self-insert-hook
300 #'electric-pair-post-self-insert-function)))
302 (provide 'electric)
304 ;; arch-tag: dae045eb-dc2d-4fb7-9f27-9cc2ce277be8
305 ;;; electric.el ends here