* emacs.texi (Acknowledgments): Add a couple more names.
[emacs.git] / lisp / emacs-lisp / lisp.el
blobcee2b828b6c41d0387894a12b79ff6d0024ec12f
1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; Lisp editing commands to go with Lisp major mode. More-or-less
27 ;; applicable in other modes too.
29 ;;; Code:
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)
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)
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'.")
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))))
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)))
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))))
89 (push-mark
90 (save-excursion
91 (forward-sexp (prefix-numeric-value arg))
92 (point))
93 nil t))))
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))))
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)))
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)))))
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))))
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)))))
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))))
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))))
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"))))
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.
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.")
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 means move forward
185 to the ARGth following beginning of defun.
187 If search is successful, return t; point ends up at the beginning
188 of the line where the search succeeded. Otherwise, return nil.
190 When `open-paren-in-column-0-is-defun-start' is non-nil, a defun
191 is assumed to start where there is a char with open-parenthesis
192 syntax at the beginning of a line. If `defun-prompt-regexp' is
193 non-nil, then a string which matches that regexp may also precede
194 the open-parenthesis. If `defun-prompt-regexp' and
195 `open-paren-in-column-0-is-defun-start' are both nil, this
196 function instead finds an open-paren at the outermost level.
198 If the variable `beginning-of-defun-function' is non-nil, its
199 value is called as a function, with argument ARG, to find the
200 defun's beginning.
202 Regardless of the values of `defun-prompt-regexp' and
203 `beginning-of-defun-function', point always moves to the
204 beginning of the line whenever the search is successful."
205 (interactive "p")
206 (or (not (eq this-command 'beginning-of-defun))
207 (eq last-command 'beginning-of-defun)
208 (and transient-mark-mode mark-active)
209 (push-mark))
210 (and (beginning-of-defun-raw arg)
211 (progn (beginning-of-line) t)))
213 (defun beginning-of-defun-raw (&optional arg)
214 "Move point to the character that starts a defun.
215 This is identical to function `beginning-of-defun', except that point
216 does not move to the beginning of the line when `defun-prompt-regexp'
217 is non-nil.
219 If variable `beginning-of-defun-function' is non-nil, its value
220 is called as a function to find the defun's beginning."
221 (interactive "p") ; change this to "P", maybe, if we ever come to pass ARG
222 ; to beginning-of-defun-function.
223 (unless arg (setq arg 1))
224 (cond
225 (beginning-of-defun-function
226 (condition-case nil
227 (funcall beginning-of-defun-function arg)
228 ;; We used to define beginning-of-defun-function as taking no argument
229 ;; but that makes it impossible to implement correct forward motion:
230 ;; we used to use end-of-defun for that, but it's not supposed to do
231 ;; the same thing (it moves to the end of a defun not to the beginning
232 ;; of the next).
233 ;; In case the beginning-of-defun-function uses the old calling
234 ;; convention, fallback on the old implementation.
235 (wrong-number-of-arguments
236 (if (> arg 0)
237 (dotimes (i arg)
238 (funcall beginning-of-defun-function))
239 ;; Better not call end-of-defun-function directly, in case
240 ;; it's not defined.
241 (end-of-defun (- arg))))))
243 ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start)
244 (and (< arg 0) (not (eobp)) (forward-char 1))
245 (and (re-search-backward (if defun-prompt-regexp
246 (concat (if open-paren-in-column-0-is-defun-start
247 "^\\s(\\|" "")
248 "\\(?:" defun-prompt-regexp "\\)\\s(")
249 "^\\s(")
250 nil 'move arg)
251 (progn (goto-char (1- (match-end 0)))) t))
253 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
254 ;; are both nil, column 0 has no significance - so scan forward
255 ;; from BOB to see how nested point is, then carry on from there.
257 ;; It is generally not a good idea to land up here, because the
258 ;; call to scan-lists below can be extremely slow. This is because
259 ;; back_comment in syntax.c may have to scan from bob to find the
260 ;; beginning of each comment. Fixing this is not trivial -- cyd.
262 ((eq arg 0))
264 (let ((floor (point-min))
265 (ceiling (point-max))
266 (arg-+ve (> arg 0)))
267 (save-restriction
268 (widen)
269 (let ((ppss (let (syntax-begin-function
270 font-lock-beginning-of-syntax-function)
271 (syntax-ppss)))
272 ;; position of least enclosing paren, or nil.
273 encl-pos)
274 ;; Back out of any comment/string, so that encl-pos will always
275 ;; become nil if we're at top-level.
276 (when (nth 8 ppss)
277 (goto-char (nth 8 ppss))
278 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
279 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
280 (if encl-pos (goto-char encl-pos))
282 (and encl-pos arg-+ve (setq arg (1- arg)))
283 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
284 (setq arg (1+ arg)))
286 (condition-case nil ; to catch crazy parens.
287 (progn
288 (goto-char (scan-lists (point) (- arg) 0))
289 (if arg-+ve
290 (if (>= (point) floor)
292 (goto-char floor)
293 nil)
294 ;; forward to next (, or trigger the c-c
295 (goto-char (1- (scan-lists (point) 1 -1)))
296 (if (<= (point) ceiling)
298 (goto-char ceiling)
299 nil)))
300 (error
301 (goto-char (if arg-+ve floor ceiling))
302 nil))))))))
304 (defvar end-of-defun-function #'forward-sexp
305 "Function for `end-of-defun' to call.
306 This is used to find the end of the defun.
307 It is called with no argument, right after calling `beginning-of-defun-raw'.
308 So the function can assume that point is at the beginning of the defun body.")
310 (defun buffer-end (arg)
311 "Return the \"far end\" position of the buffer, in direction ARG.
312 If ARG is positive, that's the end of the buffer.
313 Otherwise, that's the beginning of the buffer."
314 (if (> arg 0) (point-max) (point-min)))
316 (defun end-of-defun (&optional arg)
317 "Move forward to next end of defun.
318 With argument, do it that many times.
319 Negative argument -N means move back to Nth preceding end of defun.
321 An end of a defun occurs right after the close-parenthesis that
322 matches the open-parenthesis that starts a defun; see function
323 `beginning-of-defun'.
325 If variable `end-of-defun-function' is non-nil, its value
326 is called as a function to find the defun's end."
327 (interactive "p")
328 (or (not (eq this-command 'end-of-defun))
329 (eq last-command 'end-of-defun)
330 (and transient-mark-mode mark-active)
331 (push-mark))
332 (if (or (null arg) (= arg 0)) (setq arg 1))
333 (while (> arg 0)
334 (let ((pos (point)))
335 (end-of-line 1)
336 (beginning-of-defun-raw 1)
337 (while (unless (eobp)
338 (funcall end-of-defun-function)
339 (skip-chars-forward " \t")
340 (if (looking-at "\\s<\\|\n")
341 (forward-line 1))
342 ;; If we started after the end of the previous function, then
343 ;; try again with the next one.
344 (when (<= (point) pos)
345 (or (bobp) (forward-char -1))
346 (beginning-of-defun-raw -1)
347 'try-again))))
348 (setq arg (1- arg)))
349 (while (< arg 0)
350 (let ((pos (point)))
351 (while (unless (bobp)
352 (beginning-of-line 1)
353 (beginning-of-defun-raw 1)
354 (let ((beg (point)))
355 (funcall end-of-defun-function)
356 (skip-chars-forward " \t")
357 (if (looking-at "\\s<\\|\n")
358 (forward-line 1))
359 ;; If we started from within the function just found, then
360 ;; try again with the previous one.
361 (when (>= (point) pos)
362 (goto-char beg)
363 'try-again)))))
364 (setq arg (1+ arg))))
366 (defun mark-defun (&optional allow-extend)
367 "Put mark at end of this defun, point at beginning.
368 The defun marked is the one that contains point or follows point.
370 Interactively, if this command is repeated
371 or (in Transient Mark mode) if the mark is active,
372 it marks the next defun after the ones already marked."
373 (interactive "p")
374 (cond ((and allow-extend
375 (or (and (eq last-command this-command) (mark t))
376 (and transient-mark-mode mark-active)))
377 (set-mark
378 (save-excursion
379 (goto-char (mark))
380 (end-of-defun)
381 (point))))
383 (let ((opoint (point))
384 beg end)
385 (push-mark opoint)
386 ;; Try first in this order for the sake of languages with nested
387 ;; functions where several can end at the same place as with
388 ;; the offside rule, e.g. Python.
389 (beginning-of-defun)
390 (setq beg (point))
391 (end-of-defun)
392 (setq end (point))
393 (while (looking-at "^\n")
394 (forward-line 1))
395 (if (> (point) opoint)
396 (progn
397 ;; We got the right defun.
398 (push-mark beg nil t)
399 (goto-char end)
400 (exchange-point-and-mark))
401 ;; beginning-of-defun moved back one defun
402 ;; so we got the wrong one.
403 (goto-char opoint)
404 (end-of-defun)
405 (push-mark (point) nil t)
406 (beginning-of-defun))
407 (re-search-backward "^\n" (- (point) 1) t)))))
409 (defun narrow-to-defun (&optional arg)
410 "Make text outside current defun invisible.
411 The defun visible is the one that contains point or follows point.
412 Optional ARG is ignored."
413 (interactive)
414 (save-excursion
415 (widen)
416 (let ((opoint (point))
417 beg end)
418 ;; Try first in this order for the sake of languages with nested
419 ;; functions where several can end at the same place as with
420 ;; the offside rule, e.g. Python.
421 (beginning-of-defun)
422 (setq beg (point))
423 (end-of-defun)
424 (setq end (point))
425 (while (looking-at "^\n")
426 (forward-line 1))
427 (unless (> (point) opoint)
428 ;; beginning-of-defun moved back one defun
429 ;; so we got the wrong one.
430 (goto-char opoint)
431 (end-of-defun)
432 (setq end (point))
433 (beginning-of-defun)
434 (setq beg (point)))
435 (goto-char end)
436 (re-search-backward "^\n" (- (point) 1) t)
437 (narrow-to-region beg end))))
439 (defvar insert-pair-alist
440 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
441 "Alist of paired characters inserted by `insert-pair'.
442 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
443 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
444 of the pair whose key is equal to the last input character with
445 or without modifiers, are inserted by `insert-pair'.")
447 (defun insert-pair (&optional arg open close)
448 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
449 Leave point after the first character.
450 A negative ARG encloses the preceding ARG sexps instead.
451 No argument is equivalent to zero: just insert characters
452 and leave point between.
453 If `parens-require-spaces' is non-nil, this command also inserts a space
454 before and after, depending on the surrounding characters.
455 If region is active, insert enclosing characters at region boundaries.
457 If arguments OPEN and CLOSE are nil, the character pair is found
458 from the variable `insert-pair-alist' according to the last input
459 character with or without modifiers. If no character pair is
460 found in the variable `insert-pair-alist', then the last input
461 character is inserted ARG times."
462 (interactive "P")
463 (if (not (and open close))
464 (let ((pair (or (assq last-command-char insert-pair-alist)
465 (assq (event-basic-type last-command-event)
466 insert-pair-alist))))
467 (if pair
468 (if (nth 2 pair)
469 (setq open (nth 1 pair) close (nth 2 pair))
470 (setq open (nth 0 pair) close (nth 1 pair))))))
471 (if (and open close)
472 (if (and transient-mark-mode mark-active)
473 (progn
474 (save-excursion (goto-char (region-end)) (insert close))
475 (save-excursion (goto-char (region-beginning)) (insert open)))
476 (if arg (setq arg (prefix-numeric-value arg))
477 (setq arg 0))
478 (cond ((> arg 0) (skip-chars-forward " \t"))
479 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
480 (and parens-require-spaces
481 (not (bobp))
482 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
483 (insert " "))
484 (insert open)
485 (save-excursion
486 (or (eq arg 0) (forward-sexp arg))
487 (insert close)
488 (and parens-require-spaces
489 (not (eobp))
490 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
491 (insert " "))))
492 (insert-char (event-basic-type last-command-event)
493 (prefix-numeric-value arg))))
495 (defun insert-parentheses (&optional arg)
496 "Enclose following ARG sexps in parentheses.
497 Leave point after open-paren.
498 A negative ARG encloses the preceding ARG sexps instead.
499 No argument is equivalent to zero: just insert `()' and leave point between.
500 If `parens-require-spaces' is non-nil, this command also inserts a space
501 before and after, depending on the surrounding characters.
502 If region is active, insert enclosing characters at region boundaries."
503 (interactive "P")
504 (insert-pair arg ?\( ?\)))
506 (defun delete-pair ()
507 "Delete a pair of characters enclosing the sexp that follows point."
508 (interactive)
509 (save-excursion (forward-sexp 1) (delete-char -1))
510 (delete-char 1))
512 (defun raise-sexp (&optional arg)
513 "Raise ARG sexps higher up the tree."
514 (interactive "p")
515 (let ((s (if (and transient-mark-mode mark-active)
516 (buffer-substring (region-beginning) (region-end))
517 (buffer-substring
518 (point)
519 (save-excursion (forward-sexp arg) (point))))))
520 (backward-up-list 1)
521 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
522 (save-excursion (insert s))))
524 (defun move-past-close-and-reindent ()
525 "Move past next `)', delete indentation before it, then indent after it."
526 (interactive)
527 (up-list 1)
528 (forward-char -1)
529 (while (save-excursion ; this is my contribution
530 (let ((before-paren (point)))
531 (back-to-indentation)
532 (and (= (point) before-paren)
533 (progn
534 ;; Move to end of previous line.
535 (beginning-of-line)
536 (forward-char -1)
537 ;; Verify it doesn't end within a string or comment.
538 (let ((end (point))
539 state)
540 (beginning-of-line)
541 ;; Get state at start of line.
542 (setq state (list 0 nil nil
543 (null (calculate-lisp-indent))
544 nil nil nil nil
545 nil))
546 ;; Parse state across the line to get state at end.
547 (setq state (parse-partial-sexp (point) end nil nil
548 state))
549 ;; Check not in string or comment.
550 (and (not (elt state 3)) (not (elt state 4))))))))
551 (delete-indentation))
552 (forward-char 1)
553 (newline-and-indent))
555 (defun check-parens () ; lame name?
556 "Check for unbalanced parentheses in the current buffer.
557 More accurately, check the narrowed part of the buffer for unbalanced
558 expressions (\"sexps\") in general. This is done according to the
559 current syntax table and will find unbalanced brackets or quotes as
560 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
561 found, an error is signaled and point is left at the first unbalanced
562 character."
563 (interactive)
564 (condition-case data
565 ;; Buffer can't have more than (point-max) sexps.
566 (scan-sexps (point-min) (point-max))
567 (scan-error (goto-char (nth 2 data))
568 ;; Could print (nth 1 data), which is either
569 ;; "Containing expression ends prematurely" or
570 ;; "Unbalanced parentheses", but those may not be so
571 ;; accurate/helpful, e.g. quotes may actually be
572 ;; mismatched.
573 (error "Unmatched bracket or quote"))))
575 (defun field-complete (table &optional predicate)
576 (let* ((pattern (field-string-no-properties))
577 (completion (try-completion pattern table predicate)))
578 (cond ((eq completion t))
579 ((null completion)
580 (message "Can't find completion for \"%s\"" pattern)
581 (ding))
582 ((not (string= pattern completion))
583 (delete-region (field-beginning) (field-end))
584 (insert completion)
585 ;; Don't leave around a completions buffer that's out of date.
586 (let ((win (get-buffer-window "*Completions*" 0)))
587 (if win (with-selected-window win (bury-buffer)))))
589 (let ((minibuf-is-in-use
590 (eq (minibuffer-window) (selected-window))))
591 (unless minibuf-is-in-use
592 (message "Making completion list..."))
593 (let ((list (all-completions pattern table predicate)))
594 (setq list (sort list 'string<))
595 (or (eq predicate 'fboundp)
596 (let (new)
597 (while list
598 (setq new (cons (if (fboundp (intern (car list)))
599 (list (car list) " <f>")
600 (car list))
601 new))
602 (setq list (cdr list)))
603 (setq list (nreverse new))))
604 (if (> (length list) 1)
605 (with-output-to-temp-buffer "*Completions*"
606 (display-completion-list list pattern))
607 ;; Don't leave around a completions buffer that's
608 ;; out of date.
609 (let ((win (get-buffer-window "*Completions*" 0)))
610 (if win (with-selected-window win (bury-buffer))))))
611 (unless minibuf-is-in-use
612 (message "Making completion list...%s" "done")))))))
614 (defun lisp-complete-symbol (&optional predicate)
615 "Perform completion on Lisp symbol preceding point.
616 Compare that symbol against the known Lisp symbols.
617 If no characters can be completed, display a list of possible completions.
618 Repeating the command at that point scrolls the list.
620 When called from a program, optional arg PREDICATE is a predicate
621 determining which symbols are considered, e.g. `commandp'.
622 If PREDICATE is nil, the context determines which symbols are
623 considered. If the symbol starts just after an open-parenthesis, only
624 symbols with function definitions are considered. Otherwise, all
625 symbols with function definitions, values or properties are
626 considered."
627 (interactive)
628 (let ((window (get-buffer-window "*Completions*" 0)))
629 (if (and (eq last-command this-command)
630 window (window-live-p window) (window-buffer window)
631 (buffer-name (window-buffer window)))
632 ;; If this command was repeated, and
633 ;; there's a fresh completion window with a live buffer,
634 ;; and this command is repeated, scroll that window.
635 (with-current-buffer (window-buffer window)
636 (if (pos-visible-in-window-p (point-max) window)
637 (set-window-start window (point-min))
638 (save-selected-window
639 (select-window window)
640 (scroll-up))))
642 ;; Do completion.
643 (let* ((end (point))
644 (beg (with-syntax-table emacs-lisp-mode-syntax-table
645 (save-excursion
646 (backward-sexp 1)
647 (while (= (char-syntax (following-char)) ?\')
648 (forward-char 1))
649 (point))))
650 (pattern (buffer-substring-no-properties beg end))
651 (predicate
652 (or predicate
653 (save-excursion
654 (goto-char beg)
655 (if (not (eq (char-before) ?\())
656 (lambda (sym) ;why not just nil ? -sm
657 (or (boundp sym) (fboundp sym)
658 (symbol-plist sym)))
659 ;; Looks like a funcall position. Let's double check.
660 (if (condition-case nil
661 (progn (up-list -2) (forward-char 1)
662 (eq (char-after) ?\())
663 (error nil))
664 ;; If the first element of the parent list is an open
665 ;; parenthesis we are probably not in a funcall position.
666 ;; Maybe a `let' varlist or something.
668 ;; Else, we assume that a function name is expected.
669 'fboundp)))))
670 (completion (try-completion pattern obarray predicate)))
671 (cond ((eq completion t))
672 ((null completion)
673 (if (window-minibuffer-p (selected-window))
674 (minibuffer-message (format " [No completions of \"%s\"]" pattern))
675 (message "Can't find completion for \"%s\"" pattern))
676 (ding))
677 ((not (string= pattern completion))
678 (delete-region beg end)
679 (insert completion)
680 ;; Don't leave around a completions buffer that's out of date.
681 (let ((win (get-buffer-window "*Completions*" 0)))
682 (if win (with-selected-window win (bury-buffer)))))
684 (let ((minibuf-is-in-use
685 (eq (minibuffer-window) (selected-window))))
686 (unless minibuf-is-in-use
687 (message "Making completion list..."))
688 (let ((list (all-completions pattern obarray predicate)))
689 (setq list (sort list 'string<))
690 (unless (eq predicate 'fboundp)
691 (let (new)
692 (dolist (compl list)
693 (push (if (fboundp (intern compl))
694 (list compl " <f>")
695 compl)
696 new))
697 (setq list (nreverse new))))
698 (if (> (length list) 1)
699 (with-output-to-temp-buffer "*Completions*"
700 (display-completion-list list pattern))
701 ;; Don't leave around a completions buffer that's
702 ;; out of date.
703 (let ((win (get-buffer-window "*Completions*" 0)))
704 (if win (with-selected-window win (bury-buffer))))))
705 (unless minibuf-is-in-use
706 (message "Making completion list...%s" "done")))))))))
708 ;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
709 ;;; lisp.el ends here