cc-engine.el (c-beginning-of-statement-1): Enhance to parse case clauses
[emacs.git] / lisp / emacs-lisp / lisp.el
blobf11c99100a13b4df2123accc6729064b84f412f1
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)))
263 t)))
265 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
266 ;; are both nil, column 0 has no significance - so scan forward
267 ;; from BOB to see how nested point is, then carry on from there.
269 ;; It is generally not a good idea to land up here, because the
270 ;; call to scan-lists below can be extremely slow. This is because
271 ;; back_comment in syntax.c may have to scan from bob to find the
272 ;; beginning of each comment. Fixing this is not trivial -- cyd.
274 ((eq arg 0))
276 (let ((floor (point-min))
277 (ceiling (point-max))
278 (arg-+ve (> arg 0)))
279 (save-restriction
280 (widen)
281 (let ((ppss (let (syntax-begin-function
282 font-lock-beginning-of-syntax-function)
283 (syntax-ppss)))
284 ;; position of least enclosing paren, or nil.
285 encl-pos)
286 ;; Back out of any comment/string, so that encl-pos will always
287 ;; become nil if we're at top-level.
288 (when (nth 8 ppss)
289 (goto-char (nth 8 ppss))
290 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
291 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
292 (if encl-pos (goto-char encl-pos))
294 (and encl-pos arg-+ve (setq arg (1- arg)))
295 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
296 (setq arg (1+ arg)))
298 (condition-case nil ; to catch crazy parens.
299 (progn
300 (goto-char (scan-lists (point) (- arg) 0))
301 (if arg-+ve
302 (if (>= (point) floor)
304 (goto-char floor)
305 nil)
306 ;; forward to next (, or trigger the c-c
307 (goto-char (1- (scan-lists (point) 1 -1)))
308 (if (<= (point) ceiling)
310 (goto-char ceiling)
311 nil)))
312 (error
313 (goto-char (if arg-+ve floor ceiling))
314 nil))))))))
316 (defvar end-of-defun-function #'forward-sexp
317 "Function for `end-of-defun' to call.
318 This is used to find the end of the defun at point.
319 It is called with no argument, right after calling `beginning-of-defun-raw'.
320 So the function can assume that point is at the beginning of the defun body.
321 It should move point to the first position after the defun.")
323 (defun buffer-end (arg)
324 "Return the \"far end\" position of the buffer, in direction ARG.
325 If ARG is positive, that's the end of the buffer.
326 Otherwise, that's the beginning of the buffer."
327 (if (> arg 0) (point-max) (point-min)))
329 (defun end-of-defun (&optional arg)
330 "Move forward to next end of defun.
331 With argument, do it that many times.
332 Negative argument -N means move back to Nth preceding end of defun.
334 An end of a defun occurs right after the close-parenthesis that
335 matches the open-parenthesis that starts a defun; see function
336 `beginning-of-defun'.
338 If variable `end-of-defun-function' is non-nil, its value
339 is called as a function to find the defun's end."
340 (interactive "^p")
341 (or (not (eq this-command 'end-of-defun))
342 (eq last-command 'end-of-defun)
343 (and transient-mark-mode mark-active)
344 (push-mark))
345 (if (or (null arg) (= arg 0)) (setq arg 1))
346 (let ((pos (point))
347 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
348 (funcall end-of-defun-function)
349 ;; When comparing point against pos, we want to consider that if
350 ;; point was right after the end of the function, it's still
351 ;; considered as "in that function".
352 ;; E.g. `eval-defun' from right after the last close-paren.
353 (unless (bolp)
354 (skip-chars-forward " \t")
355 (if (looking-at "\\s<\\|\n")
356 (forward-line 1)))
357 (cond
358 ((> arg 0)
359 ;; Moving forward.
360 (if (> (point) pos)
361 ;; We already moved forward by one because we started from
362 ;; within a function.
363 (setq arg (1- arg))
364 ;; We started from after the end of the previous function.
365 (goto-char pos))
366 (unless (zerop arg)
367 (beginning-of-defun-raw (- arg))
368 (funcall end-of-defun-function)))
369 ((< arg 0)
370 ;; Moving backward.
371 (if (< (point) pos)
372 ;; We already moved backward because we started from between
373 ;; two functions.
374 (setq arg (1+ arg))
375 ;; We started from inside a function.
376 (goto-char beg))
377 (unless (zerop arg)
378 (beginning-of-defun-raw (- arg))
379 (funcall end-of-defun-function))))
380 (unless (bolp)
381 (skip-chars-forward " \t")
382 (if (looking-at "\\s<\\|\n")
383 (forward-line 1)))))
385 (defun mark-defun (&optional allow-extend)
386 "Put mark at end of this defun, point at beginning.
387 The defun marked is the one that contains point or follows point.
389 Interactively, if this command is repeated
390 or (in Transient Mark mode) if the mark is active,
391 it marks the next defun after the ones already marked."
392 (interactive "p")
393 (cond ((and allow-extend
394 (or (and (eq last-command this-command) (mark t))
395 (and transient-mark-mode mark-active)))
396 (set-mark
397 (save-excursion
398 (goto-char (mark))
399 (end-of-defun)
400 (point))))
402 (let ((opoint (point))
403 beg end)
404 (push-mark opoint)
405 ;; Try first in this order for the sake of languages with nested
406 ;; functions where several can end at the same place as with
407 ;; the offside rule, e.g. Python.
408 (beginning-of-defun)
409 (setq beg (point))
410 (end-of-defun)
411 (setq end (point))
412 (while (looking-at "^\n")
413 (forward-line 1))
414 (if (> (point) opoint)
415 (progn
416 ;; We got the right defun.
417 (push-mark beg nil t)
418 (goto-char end)
419 (exchange-point-and-mark))
420 ;; beginning-of-defun moved back one defun
421 ;; so we got the wrong one.
422 (goto-char opoint)
423 (end-of-defun)
424 (push-mark (point) nil t)
425 (beginning-of-defun))
426 (re-search-backward "^\n" (- (point) 1) t)))))
428 (defun narrow-to-defun (&optional arg)
429 "Make text outside current defun invisible.
430 The defun visible is the one that contains point or follows point.
431 Optional ARG is ignored."
432 (interactive)
433 (save-excursion
434 (widen)
435 (let ((opoint (point))
436 beg end)
437 ;; Try first in this order for the sake of languages with nested
438 ;; functions where several can end at the same place as with
439 ;; the offside rule, e.g. Python.
440 (beginning-of-defun)
441 (setq beg (point))
442 (end-of-defun)
443 (setq end (point))
444 (while (looking-at "^\n")
445 (forward-line 1))
446 (unless (> (point) opoint)
447 ;; beginning-of-defun moved back one defun
448 ;; so we got the wrong one.
449 (goto-char opoint)
450 (end-of-defun)
451 (setq end (point))
452 (beginning-of-defun)
453 (setq beg (point)))
454 (goto-char end)
455 (re-search-backward "^\n" (- (point) 1) t)
456 (narrow-to-region beg end))))
458 (defvar insert-pair-alist
459 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
460 "Alist of paired characters inserted by `insert-pair'.
461 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
462 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
463 of the pair whose key is equal to the last input character with
464 or without modifiers, are inserted by `insert-pair'.")
466 (defun insert-pair (&optional arg open close)
467 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
468 Leave point after the first character.
469 A negative ARG encloses the preceding ARG sexps instead.
470 No argument is equivalent to zero: just insert characters
471 and leave point between.
472 If `parens-require-spaces' is non-nil, this command also inserts a space
473 before and after, depending on the surrounding characters.
474 If region is active, insert enclosing characters at region boundaries.
476 If arguments OPEN and CLOSE are nil, the character pair is found
477 from the variable `insert-pair-alist' according to the last input
478 character with or without modifiers. If no character pair is
479 found in the variable `insert-pair-alist', then the last input
480 character is inserted ARG times.
482 This command assumes point is not in a string or comment."
483 (interactive "P")
484 (if (not (and open close))
485 (let ((pair (or (assq last-command-event insert-pair-alist)
486 (assq (event-basic-type last-command-event)
487 insert-pair-alist))))
488 (if pair
489 (if (nth 2 pair)
490 (setq open (nth 1 pair) close (nth 2 pair))
491 (setq open (nth 0 pair) close (nth 1 pair))))))
492 (if (and open close)
493 (if (and transient-mark-mode mark-active)
494 (progn
495 (save-excursion (goto-char (region-end)) (insert close))
496 (save-excursion (goto-char (region-beginning)) (insert open)))
497 (if arg (setq arg (prefix-numeric-value arg))
498 (setq arg 0))
499 (cond ((> arg 0) (skip-chars-forward " \t"))
500 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
501 (and parens-require-spaces
502 (not (bobp))
503 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
504 (insert " "))
505 (insert open)
506 (save-excursion
507 (or (eq arg 0) (forward-sexp arg))
508 (insert close)
509 (and parens-require-spaces
510 (not (eobp))
511 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
512 (insert " "))))
513 (insert-char (event-basic-type last-command-event)
514 (prefix-numeric-value arg))))
516 (defun insert-parentheses (&optional arg)
517 "Enclose following ARG sexps in parentheses.
518 Leave point after open-paren.
519 A negative ARG encloses the preceding ARG sexps instead.
520 No argument is equivalent to zero: just insert `()' and leave point between.
521 If `parens-require-spaces' is non-nil, this command also inserts a space
522 before and after, depending on the surrounding characters.
523 If region is active, insert enclosing characters at region boundaries.
525 This command assumes point is not in a string or comment."
526 (interactive "P")
527 (insert-pair arg ?\( ?\)))
529 (defun delete-pair ()
530 "Delete a pair of characters enclosing the sexp that follows point."
531 (interactive)
532 (save-excursion (forward-sexp 1) (delete-char -1))
533 (delete-char 1))
535 (defun raise-sexp (&optional arg)
536 "Raise ARG sexps higher up the tree."
537 (interactive "p")
538 (let ((s (if (and transient-mark-mode mark-active)
539 (buffer-substring (region-beginning) (region-end))
540 (buffer-substring
541 (point)
542 (save-excursion (forward-sexp arg) (point))))))
543 (backward-up-list 1)
544 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
545 (save-excursion (insert s))))
547 (defun move-past-close-and-reindent ()
548 "Move past next `)', delete indentation before it, then indent after it."
549 (interactive)
550 (up-list 1)
551 (forward-char -1)
552 (while (save-excursion ; this is my contribution
553 (let ((before-paren (point)))
554 (back-to-indentation)
555 (and (= (point) before-paren)
556 (progn
557 ;; Move to end of previous line.
558 (beginning-of-line)
559 (forward-char -1)
560 ;; Verify it doesn't end within a string or comment.
561 (let ((end (point))
562 state)
563 (beginning-of-line)
564 ;; Get state at start of line.
565 (setq state (list 0 nil nil
566 (null (calculate-lisp-indent))
567 nil nil nil nil
568 nil))
569 ;; Parse state across the line to get state at end.
570 (setq state (parse-partial-sexp (point) end nil nil
571 state))
572 ;; Check not in string or comment.
573 (and (not (elt state 3)) (not (elt state 4))))))))
574 (delete-indentation))
575 (forward-char 1)
576 (newline-and-indent))
578 (defun check-parens () ; lame name?
579 "Check for unbalanced parentheses in the current buffer.
580 More accurately, check the narrowed part of the buffer for unbalanced
581 expressions (\"sexps\") in general. This is done according to the
582 current syntax table and will find unbalanced brackets or quotes as
583 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
584 found, an error is signaled and point is left at the first unbalanced
585 character."
586 (interactive)
587 (condition-case data
588 ;; Buffer can't have more than (point-max) sexps.
589 (scan-sexps (point-min) (point-max))
590 (scan-error (goto-char (nth 2 data))
591 ;; Could print (nth 1 data), which is either
592 ;; "Containing expression ends prematurely" or
593 ;; "Unbalanced parentheses", but those may not be so
594 ;; accurate/helpful, e.g. quotes may actually be
595 ;; mismatched.
596 (error "Unmatched bracket or quote"))))
598 (defun field-complete (table &optional predicate)
599 (let* ((pattern (field-string-no-properties))
600 (completion (try-completion pattern table predicate)))
601 (cond ((eq completion t))
602 ((null completion)
603 (message "Can't find completion for \"%s\"" pattern)
604 (ding))
605 ((not (string= pattern completion))
606 (delete-region (field-beginning) (field-end))
607 (insert completion)
608 ;; Don't leave around a completions buffer that's out of date.
609 (let ((win (get-buffer-window "*Completions*" 0)))
610 (if win (with-selected-window win (bury-buffer)))))
612 (let ((minibuf-is-in-use
613 (eq (minibuffer-window) (selected-window))))
614 (unless minibuf-is-in-use
615 (message "Making completion list..."))
616 (let ((list (all-completions pattern table predicate)))
617 (setq list (sort list 'string<))
618 (or (eq predicate 'fboundp)
619 (let (new)
620 (while list
621 (setq new (cons (if (fboundp (intern (car list)))
622 (list (car list) " <f>")
623 (car list))
624 new))
625 (setq list (cdr list)))
626 (setq list (nreverse new))))
627 (if (> (length list) 1)
628 (with-output-to-temp-buffer "*Completions*"
629 (display-completion-list list pattern))
630 ;; Don't leave around a completions buffer that's
631 ;; out of date.
632 (let ((win (get-buffer-window "*Completions*" 0)))
633 (if win (with-selected-window win (bury-buffer))))))
634 (unless minibuf-is-in-use
635 (message "Making completion list...%s" "done")))))))
637 (defun lisp-complete-symbol (&optional predicate)
638 "Perform completion on Lisp symbol preceding point.
639 Compare that symbol against the known Lisp symbols.
640 If no characters can be completed, display a list of possible completions.
641 Repeating the command at that point scrolls the list.
643 When called from a program, optional arg PREDICATE is a predicate
644 determining which symbols are considered, e.g. `commandp'.
645 If PREDICATE is nil, the context determines which symbols are
646 considered. If the symbol starts just after an open-parenthesis, only
647 symbols with function definitions are considered. Otherwise, all
648 symbols with function definitions, values or properties are
649 considered."
650 (interactive)
651 (let ((window (get-buffer-window "*Completions*" 0)))
652 (if (and (eq last-command this-command)
653 window (window-live-p window) (window-buffer window)
654 (buffer-name (window-buffer window)))
655 ;; If this command was repeated, and
656 ;; there's a fresh completion window with a live buffer,
657 ;; and this command is repeated, scroll that window.
658 (with-current-buffer (window-buffer window)
659 (if (pos-visible-in-window-p (point-max) window)
660 (set-window-start window (point-min))
661 (save-selected-window
662 (select-window window)
663 (scroll-up))))
665 ;; Do completion.
666 (let* ((end (point))
667 (beg (with-syntax-table emacs-lisp-mode-syntax-table
668 (save-excursion
669 (backward-sexp 1)
670 (while (= (char-syntax (following-char)) ?\')
671 (forward-char 1))
672 (point))))
673 (pattern (buffer-substring-no-properties beg end))
674 (predicate
675 (or predicate
676 (save-excursion
677 (goto-char beg)
678 (if (not (eq (char-before) ?\())
679 (lambda (sym) ;why not just nil ? -sm
680 (or (boundp sym) (fboundp sym)
681 (symbol-plist sym)))
682 ;; Looks like a funcall position. Let's double check.
683 (if (condition-case nil
684 (progn (up-list -2) (forward-char 1)
685 (eq (char-after) ?\())
686 (error nil))
687 ;; If the first element of the parent list is an open
688 ;; parenthesis we are probably not in a funcall position.
689 ;; Maybe a `let' varlist or something.
691 ;; Else, we assume that a function name is expected.
692 'fboundp)))))
693 (completion (try-completion pattern obarray predicate)))
694 (cond ((eq completion t))
695 ((null completion)
696 (if (window-minibuffer-p (selected-window))
697 (minibuffer-message (format " [No completions of \"%s\"]" pattern))
698 (message "Can't find completion for \"%s\"" pattern))
699 (ding))
700 ((not (string= pattern completion))
701 (delete-region beg end)
702 (insert completion)
703 ;; Don't leave around a completions buffer that's out of date.
704 (let ((win (get-buffer-window "*Completions*" 0)))
705 (if win (with-selected-window win (bury-buffer)))))
707 (let ((minibuf-is-in-use
708 (eq (minibuffer-window) (selected-window))))
709 (unless minibuf-is-in-use
710 (message "Making completion list..."))
711 (let ((list (all-completions pattern obarray predicate)))
712 (setq list (sort list 'string<))
713 (unless (eq predicate 'fboundp)
714 (let (new)
715 (dolist (compl list)
716 (push (if (fboundp (intern compl))
717 (list compl " <f>")
718 compl)
719 new))
720 (setq list (nreverse new))))
721 (if (> (length list) 1)
722 (with-output-to-temp-buffer "*Completions*"
723 (display-completion-list list pattern))
724 ;; Don't leave around a completions buffer that's
725 ;; out of date.
726 (let ((win (get-buffer-window "*Completions*" 0)))
727 (if win (with-selected-window win (bury-buffer))))))
728 (unless minibuf-is-in-use
729 (message "Making completion list...%s" "done")))))))))
731 ;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
732 ;;; lisp.el ends here