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