Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / emacs-lisp / lisp.el
1 ;;; lisp.el --- Lisp editing commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, languages
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Lisp editing commands to go with Lisp major mode. More-or-less
27 ;; applicable in other modes too.
28
29 ;;; Code:
30
31 ;; Note that this variable is used by non-lisp modes too.
32 (defcustom defun-prompt-regexp nil
33 "*If non-nil, a regexp to ignore before a defun.
34 This is only necessary if the opening paren or brace is not in column 0.
35 See function `beginning-of-defun'."
36 :type '(choice (const nil)
37 regexp)
38 :group 'lisp)
39 (make-variable-buffer-local 'defun-prompt-regexp)
40
41 (defcustom parens-require-spaces t
42 "If non-nil, add whitespace as needed when inserting parentheses.
43 This affects `insert-parentheses' and `insert-pair'."
44 :type 'boolean
45 :group 'lisp)
46
47 (defvar forward-sexp-function nil
48 "If non-nil, `forward-sexp' delegates to this function.
49 Should take the same arguments and behave similarly to `forward-sexp'.")
50
51 (defun forward-sexp (&optional arg)
52 "Move forward across one balanced expression (sexp).
53 With ARG, do it that many times. Negative arg -N means
54 move backward across N balanced expressions."
55 (interactive "p")
56 (or arg (setq arg 1))
57 (if forward-sexp-function
58 (funcall forward-sexp-function arg)
59 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
60 (if (< arg 0) (backward-prefix-chars))))
61
62 (defun backward-sexp (&optional arg)
63 "Move backward across one balanced expression (sexp).
64 With ARG, do it that many times. Negative arg -N means
65 move forward across N balanced expressions."
66 (interactive "p")
67 (or arg (setq arg 1))
68 (forward-sexp (- arg)))
69
70 (defun mark-sexp (&optional arg allow-extend)
71 "Set mark ARG sexps from point.
72 The place mark goes is the same place \\[forward-sexp] would
73 move to with the same argument.
74 Interactively, if this command is repeated
75 or (in Transient Mark mode) if the mark is active,
76 it marks the next ARG sexps after the ones already marked."
77 (interactive "P\np")
78 (cond ((and allow-extend
79 (or (and (eq last-command this-command) (mark t))
80 (and transient-mark-mode mark-active)))
81 (setq arg (if arg (prefix-numeric-value arg)
82 (if (< (mark) (point)) -1 1)))
83 (set-mark
84 (save-excursion
85 (goto-char (mark))
86 (forward-sexp arg)
87 (point))))
88 (t
89 (push-mark
90 (save-excursion
91 (forward-sexp (prefix-numeric-value arg))
92 (point))
93 nil t))))
94
95 (defun forward-list (&optional arg)
96 "Move forward across one balanced group of parentheses.
97 With ARG, do it that many times.
98 Negative arg -N means move backward across N groups of parentheses."
99 (interactive "p")
100 (or arg (setq arg 1))
101 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
102
103 (defun backward-list (&optional arg)
104 "Move backward across one balanced group of parentheses.
105 With ARG, do it that many times.
106 Negative arg -N means move forward across N groups of parentheses."
107 (interactive "p")
108 (or arg (setq arg 1))
109 (forward-list (- arg)))
110
111 (defun down-list (&optional arg)
112 "Move forward down one level of parentheses.
113 With ARG, do this that many times.
114 A negative argument means move backward but still go down a level."
115 (interactive "p")
116 (or arg (setq arg 1))
117 (let ((inc (if (> arg 0) 1 -1)))
118 (while (/= arg 0)
119 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
120 (setq arg (- arg inc)))))
121
122 (defun backward-up-list (&optional arg)
123 "Move backward out of one level of parentheses.
124 With ARG, do this that many times.
125 A negative argument means move forward but still to a less deep spot."
126 (interactive "p")
127 (up-list (- (or arg 1))))
128
129 (defun up-list (&optional arg)
130 "Move forward out of one level of parentheses.
131 With ARG, do this that many times.
132 A negative argument means move backward but still to a less deep spot."
133 (interactive "p")
134 (or arg (setq arg 1))
135 (let ((inc (if (> arg 0) 1 -1)))
136 (while (/= arg 0)
137 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
138 (setq arg (- arg inc)))))
139
140 (defun kill-sexp (&optional arg)
141 "Kill the sexp (balanced expression) following point.
142 With ARG, kill that many sexps after point.
143 Negative arg -N means kill N sexps before point."
144 (interactive "p")
145 (let ((opoint (point)))
146 (forward-sexp (or arg 1))
147 (kill-region opoint (point))))
148
149 (defun backward-kill-sexp (&optional arg)
150 "Kill the sexp (balanced expression) preceding point.
151 With ARG, kill that many sexps before point.
152 Negative arg -N means kill N sexps after point."
153 (interactive "p")
154 (kill-sexp (- (or arg 1))))
155
156 ;; After Zmacs:
157 (defun kill-backward-up-list (&optional arg)
158 "Kill the form containing the current sexp, leaving the sexp itself.
159 A prefix argument ARG causes the relevant number of surrounding
160 forms to be removed."
161 (interactive "*p")
162 (let ((current-sexp (thing-at-point 'sexp)))
163 (if current-sexp
164 (save-excursion
165 (backward-up-list arg)
166 (kill-sexp)
167 (insert current-sexp))
168 (error "Not at a sexp"))))
169 \f
170 (defvar beginning-of-defun-function nil
171 "If non-nil, function for `beginning-of-defun-raw' to call.
172 This is used to find the beginning of the defun instead of using the
173 normal recipe (see `beginning-of-defun'). Major modes can define this
174 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
175 needs.
176
177 The function takes the same argument as `beginning-of-defun' and should
178 behave similarly, returning non-nil if it found the beginning of a defun.
179 Ideally it should move to a point right before an open-paren which encloses
180 the body of the defun.")
181
182 (defun beginning-of-defun (&optional arg)
183 "Move backward to the beginning of a defun.
184 With ARG, do it that many times. Negative arg -N
185 means move forward to Nth following beginning of defun.
186 Returns t unless search stops due to beginning or end of buffer.
187
188 If variable `beginning-of-defun-function' is non-nil, its value
189 is called as a function to find the defun's beginning.
190
191 Normally a defun is assumed to start where there is a char with
192 open-parenthesis syntax at the beginning of a line. If
193 `defun-prompt-regexp' is non-nil, then a string which matches
194 that regexp may precede the open-parenthesis, and point ends up
195 at the beginning of the line.
196
197 If `defun-prompt-regexp' and `open-paren-in-column-0-is-defun-start'
198 are both nil, the function instead finds an open-paren at the
199 outermost level."
200 (interactive "p")
201 (or (not (eq this-command 'beginning-of-defun))
202 (eq last-command 'beginning-of-defun)
203 (and transient-mark-mode mark-active)
204 (push-mark))
205 (and (beginning-of-defun-raw arg)
206 (progn (beginning-of-line) t)))
207
208 (defun beginning-of-defun-raw (&optional arg)
209 "Move point to the character that starts a defun.
210 This is identical to function `beginning-of-defun', except that point
211 does not move to the beginning of the line when `defun-prompt-regexp'
212 is non-nil.
213
214 If variable `beginning-of-defun-function' is non-nil, its value
215 is called as a function to find the defun's beginning."
216 (interactive "p") ; change this to "P", maybe, if we ever come to pass ARG
217 ; to beginning-of-defun-function.
218 (unless arg (setq arg 1))
219 (cond
220 (beginning-of-defun-function
221 (condition-case nil
222 (funcall beginning-of-defun-function arg)
223 ;; We used to define beginning-of-defun-function as taking no argument
224 ;; but that makes it impossible to implement correct forward motion:
225 ;; we used to use end-of-defun for that, but it's not supposed to do
226 ;; the same thing (it moves to the end of a defun not to the beginning
227 ;; of the next).
228 ;; In case the beginning-of-defun-function uses the old calling
229 ;; convention, fallback on the old implementation.
230 (wrong-number-of-arguments
231 (if (> arg 0)
232 (dotimes (i arg)
233 (funcall beginning-of-defun-function))
234 ;; Better not call end-of-defun-function directly, in case
235 ;; it's not defined.
236 (end-of-defun (- arg))))))
237
238 ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start)
239 (and (< arg 0) (not (eobp)) (forward-char 1))
240 (and (re-search-backward (if defun-prompt-regexp
241 (concat (if open-paren-in-column-0-is-defun-start
242 "^\\s(\\|" "")
243 "\\(?:" defun-prompt-regexp "\\)\\s(")
244 "^\\s(")
245 nil 'move arg)
246 (progn (goto-char (1- (match-end 0)))) t))
247
248 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
249 ;; are both nil, column 0 has no significance - so scan forward
250 ;; from BOB to see how nested point is, then carry on from there.
251 ;;
252 ;; It is generally not a good idea to land up here, because the
253 ;; call to scan-lists below can be extremely slow. This is because
254 ;; back_comment in syntax.c may have to scan from bob to find the
255 ;; beginning of each comment. Fixing this is not trivial -- cyd.
256
257 ((eq arg 0))
258 (t
259 (let ((floor (point-min))
260 (ceiling (point-max))
261 (arg-+ve (> arg 0)))
262 (save-restriction
263 (widen)
264 (let ((ppss (let (syntax-begin-function
265 font-lock-beginning-of-syntax-function)
266 (syntax-ppss)))
267 ;; position of least enclosing paren, or nil.
268 encl-pos)
269 ;; Back out of any comment/string, so that encl-pos will always
270 ;; become nil if we're at top-level.
271 (when (nth 8 ppss)
272 (goto-char (nth 8 ppss))
273 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
274 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
275 (if encl-pos (goto-char encl-pos))
276
277 (and encl-pos arg-+ve (setq arg (1- arg)))
278 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
279 (setq arg (1+ arg)))
280
281 (condition-case nil ; to catch crazy parens.
282 (progn
283 (goto-char (scan-lists (point) (- arg) 0))
284 (if arg-+ve
285 (if (>= (point) floor)
286 t
287 (goto-char floor)
288 nil)
289 ;; forward to next (, or trigger the c-c
290 (goto-char (1- (scan-lists (point) 1 -1)))
291 (if (<= (point) ceiling)
292 t
293 (goto-char ceiling)
294 nil)))
295 (error
296 (goto-char (if arg-+ve floor ceiling))
297 nil))))))))
298
299 (defvar end-of-defun-function #'forward-sexp
300 "Function for `end-of-defun' to call.
301 This is used to find the end of the defun.
302 It is called with no argument, right after calling `beginning-of-defun-raw'.
303 So the function can assume that point is at the beginning of the defun body.")
304
305 (defun buffer-end (arg)
306 "Return the \"far end\" position of the buffer, in direction ARG.
307 If ARG is positive, that's the end of the buffer.
308 Otherwise, that's the beginning of the buffer."
309 (if (> arg 0) (point-max) (point-min)))
310
311 (defun end-of-defun (&optional arg)
312 "Move forward to next end of defun.
313 With argument, do it that many times.
314 Negative argument -N means move back to Nth preceding end of defun.
315
316 An end of a defun occurs right after the close-parenthesis that
317 matches the open-parenthesis that starts a defun; see function
318 `beginning-of-defun'.
319
320 If variable `end-of-defun-function' is non-nil, its value
321 is called as a function to find the defun's end."
322 (interactive "p")
323 (or (not (eq this-command 'end-of-defun))
324 (eq last-command 'end-of-defun)
325 (and transient-mark-mode mark-active)
326 (push-mark))
327 (if (or (null arg) (= arg 0)) (setq arg 1))
328 (while (> arg 0)
329 (let ((pos (point)))
330 (end-of-line 1)
331 (beginning-of-defun-raw 1)
332 (while (unless (eobp)
333 (funcall end-of-defun-function)
334 (skip-chars-forward " \t")
335 (if (looking-at "\\s<\\|\n")
336 (forward-line 1))
337 ;; If we started after the end of the previous function, then
338 ;; try again with the next one.
339 (when (<= (point) pos)
340 (or (bobp) (forward-char -1))
341 (beginning-of-defun-raw -1)
342 'try-again))))
343 (setq arg (1- arg)))
344 (while (< arg 0)
345 (let ((pos (point)))
346 (while (unless (bobp)
347 (beginning-of-line 1)
348 (beginning-of-defun-raw 1)
349 (let ((beg (point)))
350 (funcall end-of-defun-function)
351 (skip-chars-forward " \t")
352 (if (looking-at "\\s<\\|\n")
353 (forward-line 1))
354 ;; If we started from within the function just found, then
355 ;; try again with the previous one.
356 (when (>= (point) pos)
357 (goto-char beg)
358 'try-again)))))
359 (setq arg (1+ arg))))
360
361 (defun mark-defun (&optional allow-extend)
362 "Put mark at end of this defun, point at beginning.
363 The defun marked is the one that contains point or follows point.
364
365 Interactively, if this command is repeated
366 or (in Transient Mark mode) if the mark is active,
367 it marks the next defun after the ones already marked."
368 (interactive "p")
369 (cond ((and allow-extend
370 (or (and (eq last-command this-command) (mark t))
371 (and transient-mark-mode mark-active)))
372 (set-mark
373 (save-excursion
374 (goto-char (mark))
375 (end-of-defun)
376 (point))))
377 (t
378 (let ((opoint (point))
379 beg end)
380 (push-mark opoint)
381 ;; Try first in this order for the sake of languages with nested
382 ;; functions where several can end at the same place as with
383 ;; the offside rule, e.g. Python.
384 (beginning-of-defun)
385 (setq beg (point))
386 (end-of-defun)
387 (setq end (point))
388 (while (looking-at "^\n")
389 (forward-line 1))
390 (if (> (point) opoint)
391 (progn
392 ;; We got the right defun.
393 (push-mark beg nil t)
394 (goto-char end)
395 (exchange-point-and-mark))
396 ;; beginning-of-defun moved back one defun
397 ;; so we got the wrong one.
398 (goto-char opoint)
399 (end-of-defun)
400 (push-mark (point) nil t)
401 (beginning-of-defun))
402 (re-search-backward "^\n" (- (point) 1) t)))))
403
404 (defun narrow-to-defun (&optional arg)
405 "Make text outside current defun invisible.
406 The defun visible is the one that contains point or follows point.
407 Optional ARG is ignored."
408 (interactive)
409 (save-excursion
410 (widen)
411 (let ((opoint (point))
412 beg end)
413 ;; Try first in this order for the sake of languages with nested
414 ;; functions where several can end at the same place as with
415 ;; the offside rule, e.g. Python.
416 (beginning-of-defun)
417 (setq beg (point))
418 (end-of-defun)
419 (setq end (point))
420 (while (looking-at "^\n")
421 (forward-line 1))
422 (unless (> (point) opoint)
423 ;; beginning-of-defun moved back one defun
424 ;; so we got the wrong one.
425 (goto-char opoint)
426 (end-of-defun)
427 (setq end (point))
428 (beginning-of-defun)
429 (setq beg (point)))
430 (goto-char end)
431 (re-search-backward "^\n" (- (point) 1) t)
432 (narrow-to-region beg end))))
433
434 (defvar insert-pair-alist
435 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
436 "Alist of paired characters inserted by `insert-pair'.
437 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
438 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
439 of the pair whose key is equal to the last input character with
440 or without modifiers, are inserted by `insert-pair'.")
441
442 (defun insert-pair (&optional arg open close)
443 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
444 Leave point after the first character.
445 A negative ARG encloses the preceding ARG sexps instead.
446 No argument is equivalent to zero: just insert characters
447 and leave point between.
448 If `parens-require-spaces' is non-nil, this command also inserts a space
449 before and after, depending on the surrounding characters.
450 If region is active, insert enclosing characters at region boundaries.
451
452 If arguments OPEN and CLOSE are nil, the character pair is found
453 from the variable `insert-pair-alist' according to the last input
454 character with or without modifiers. If no character pair is
455 found in the variable `insert-pair-alist', then the last input
456 character is inserted ARG times."
457 (interactive "P")
458 (if (not (and open close))
459 (let ((pair (or (assq last-command-char insert-pair-alist)
460 (assq (event-basic-type last-command-event)
461 insert-pair-alist))))
462 (if pair
463 (if (nth 2 pair)
464 (setq open (nth 1 pair) close (nth 2 pair))
465 (setq open (nth 0 pair) close (nth 1 pair))))))
466 (if (and open close)
467 (if (and transient-mark-mode mark-active)
468 (progn
469 (save-excursion (goto-char (region-end)) (insert close))
470 (save-excursion (goto-char (region-beginning)) (insert open)))
471 (if arg (setq arg (prefix-numeric-value arg))
472 (setq arg 0))
473 (cond ((> arg 0) (skip-chars-forward " \t"))
474 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
475 (and parens-require-spaces
476 (not (bobp))
477 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
478 (insert " "))
479 (insert open)
480 (save-excursion
481 (or (eq arg 0) (forward-sexp arg))
482 (insert close)
483 (and parens-require-spaces
484 (not (eobp))
485 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
486 (insert " "))))
487 (insert-char (event-basic-type last-command-event)
488 (prefix-numeric-value arg))))
489
490 (defun insert-parentheses (&optional arg)
491 "Enclose following ARG sexps in parentheses.
492 Leave point after open-paren.
493 A negative ARG encloses the preceding ARG sexps instead.
494 No argument is equivalent to zero: just insert `()' and leave point between.
495 If `parens-require-spaces' is non-nil, this command also inserts a space
496 before and after, depending on the surrounding characters.
497 If region is active, insert enclosing characters at region boundaries."
498 (interactive "P")
499 (insert-pair arg ?\( ?\)))
500
501 (defun delete-pair ()
502 "Delete a pair of characters enclosing the sexp that follows point."
503 (interactive)
504 (save-excursion (forward-sexp 1) (delete-char -1))
505 (delete-char 1))
506
507 (defun raise-sexp (&optional arg)
508 "Raise ARG sexps higher up the tree."
509 (interactive "p")
510 (let ((s (if (and transient-mark-mode mark-active)
511 (buffer-substring (region-beginning) (region-end))
512 (buffer-substring
513 (point)
514 (save-excursion (forward-sexp arg) (point))))))
515 (backward-up-list 1)
516 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
517 (save-excursion (insert s))))
518
519 (defun move-past-close-and-reindent ()
520 "Move past next `)', delete indentation before it, then indent after it."
521 (interactive)
522 (up-list 1)
523 (forward-char -1)
524 (while (save-excursion ; this is my contribution
525 (let ((before-paren (point)))
526 (back-to-indentation)
527 (and (= (point) before-paren)
528 (progn
529 ;; Move to end of previous line.
530 (beginning-of-line)
531 (forward-char -1)
532 ;; Verify it doesn't end within a string or comment.
533 (let ((end (point))
534 state)
535 (beginning-of-line)
536 ;; Get state at start of line.
537 (setq state (list 0 nil nil
538 (null (calculate-lisp-indent))
539 nil nil nil nil
540 nil))
541 ;; Parse state across the line to get state at end.
542 (setq state (parse-partial-sexp (point) end nil nil
543 state))
544 ;; Check not in string or comment.
545 (and (not (elt state 3)) (not (elt state 4))))))))
546 (delete-indentation))
547 (forward-char 1)
548 (newline-and-indent))
549
550 (defun check-parens () ; lame name?
551 "Check for unbalanced parentheses in the current buffer.
552 More accurately, check the narrowed part of the buffer for unbalanced
553 expressions (\"sexps\") in general. This is done according to the
554 current syntax table and will find unbalanced brackets or quotes as
555 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
556 found, an error is signaled and point is left at the first unbalanced
557 character."
558 (interactive)
559 (condition-case data
560 ;; Buffer can't have more than (point-max) sexps.
561 (scan-sexps (point-min) (point-max))
562 (scan-error (goto-char (nth 2 data))
563 ;; Could print (nth 1 data), which is either
564 ;; "Containing expression ends prematurely" or
565 ;; "Unbalanced parentheses", but those may not be so
566 ;; accurate/helpful, e.g. quotes may actually be
567 ;; mismatched.
568 (error "Unmatched bracket or quote"))))
569 \f
570 (defun field-complete (table &optional predicate)
571 (let* ((pattern (field-string-no-properties))
572 (completion (try-completion pattern table predicate)))
573 (cond ((eq completion t))
574 ((null completion)
575 (message "Can't find completion for \"%s\"" pattern)
576 (ding))
577 ((not (string= pattern completion))
578 (delete-region (field-beginning) (field-end))
579 (insert completion)
580 ;; Don't leave around a completions buffer that's out of date.
581 (let ((win (get-buffer-window "*Completions*" 0)))
582 (if win (with-selected-window win (bury-buffer)))))
583 (t
584 (let ((minibuf-is-in-use
585 (eq (minibuffer-window) (selected-window))))
586 (unless minibuf-is-in-use
587 (message "Making completion list..."))
588 (let ((list (all-completions pattern table predicate)))
589 (setq list (sort list 'string<))
590 (or (eq predicate 'fboundp)
591 (let (new)
592 (while list
593 (setq new (cons (if (fboundp (intern (car list)))
594 (list (car list) " <f>")
595 (car list))
596 new))
597 (setq list (cdr list)))
598 (setq list (nreverse new))))
599 (if (> (length list) 1)
600 (with-output-to-temp-buffer "*Completions*"
601 (display-completion-list list pattern))
602 ;; Don't leave around a completions buffer that's
603 ;; out of date.
604 (let ((win (get-buffer-window "*Completions*" 0)))
605 (if win (with-selected-window win (bury-buffer))))))
606 (unless minibuf-is-in-use
607 (message "Making completion list...%s" "done")))))))
608
609 (defun lisp-complete-symbol (&optional predicate)
610 "Perform completion on Lisp symbol preceding point.
611 Compare that symbol against the known Lisp symbols.
612 If no characters can be completed, display a list of possible completions.
613 Repeating the command at that point scrolls the list.
614
615 When called from a program, optional arg PREDICATE is a predicate
616 determining which symbols are considered, e.g. `commandp'.
617 If PREDICATE is nil, the context determines which symbols are
618 considered. If the symbol starts just after an open-parenthesis, only
619 symbols with function definitions are considered. Otherwise, all
620 symbols with function definitions, values or properties are
621 considered."
622 (interactive)
623 (let ((window (get-buffer-window "*Completions*" 0)))
624 (if (and (eq last-command this-command)
625 window (window-live-p window) (window-buffer window)
626 (buffer-name (window-buffer window)))
627 ;; If this command was repeated, and
628 ;; there's a fresh completion window with a live buffer,
629 ;; and this command is repeated, scroll that window.
630 (with-current-buffer (window-buffer window)
631 (if (pos-visible-in-window-p (point-max) window)
632 (set-window-start window (point-min))
633 (save-selected-window
634 (select-window window)
635 (scroll-up))))
636
637 ;; Do completion.
638 (let* ((end (point))
639 (beg (with-syntax-table emacs-lisp-mode-syntax-table
640 (save-excursion
641 (backward-sexp 1)
642 (while (= (char-syntax (following-char)) ?\')
643 (forward-char 1))
644 (point))))
645 (pattern (buffer-substring-no-properties beg end))
646 (predicate
647 (or predicate
648 (save-excursion
649 (goto-char beg)
650 (if (not (eq (char-before) ?\())
651 (lambda (sym) ;why not just nil ? -sm
652 (or (boundp sym) (fboundp sym)
653 (symbol-plist sym)))
654 ;; Looks like a funcall position. Let's double check.
655 (if (condition-case nil
656 (progn (up-list -2) (forward-char 1)
657 (eq (char-after) ?\())
658 (error nil))
659 ;; If the first element of the parent list is an open
660 ;; parenthesis we are probably not in a funcall position.
661 ;; Maybe a `let' varlist or something.
662 nil
663 ;; Else, we assume that a function name is expected.
664 'fboundp)))))
665 (completion (try-completion pattern obarray predicate)))
666 (cond ((eq completion t))
667 ((null completion)
668 (if (window-minibuffer-p (selected-window))
669 (minibuffer-message (format " [No completions of \"%s\"]" pattern))
670 (message "Can't find completion for \"%s\"" pattern))
671 (ding))
672 ((not (string= pattern completion))
673 (delete-region beg end)
674 (insert completion)
675 ;; Don't leave around a completions buffer that's out of date.
676 (let ((win (get-buffer-window "*Completions*" 0)))
677 (if win (with-selected-window win (bury-buffer)))))
678 (t
679 (let ((minibuf-is-in-use
680 (eq (minibuffer-window) (selected-window))))
681 (unless minibuf-is-in-use
682 (message "Making completion list..."))
683 (let ((list (all-completions pattern obarray predicate)))
684 (setq list (sort list 'string<))
685 (or (eq predicate 'fboundp)
686 (let (new)
687 (while list
688 (setq new (cons (if (fboundp (intern (car list)))
689 (list (car list) " <f>")
690 (car list))
691 new))
692 (setq list (cdr list)))
693 (setq list (nreverse new))))
694 (if (> (length list) 1)
695 (with-output-to-temp-buffer "*Completions*"
696 (display-completion-list list pattern))
697 ;; Don't leave around a completions buffer that's
698 ;; out of date.
699 (let ((win (get-buffer-window "*Completions*" 0)))
700 (if win (with-selected-window win (bury-buffer))))))
701 (unless minibuf-is-in-use
702 (message "Making completion list...%s" "done")))))))))
703
704 ;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
705 ;;; lisp.el ends here