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