* lisp/emacs-lisp/lisp.el (lisp-complete-symbol, lisp-completion-at-point):
[emacs.git] / lisp / emacs-lisp / lisp.el
blob1f1fa16108f1f9bcc260dc7ba83bde922fcd11c5
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, 2010, 2011 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
317 (lambda () (forward-sexp 1))
318 "Function for `end-of-defun' to call.
319 This is used to find the end of the defun at point.
320 It is called with no argument, right after calling `beginning-of-defun-raw'.
321 So the function can assume that point is at the beginning of the defun body.
322 It should move point to the first position after the defun.")
324 (defun buffer-end (arg)
325 "Return the \"far end\" position of the buffer, in direction ARG.
326 If ARG is positive, that's the end of the buffer.
327 Otherwise, that's the beginning of the buffer."
328 (if (> arg 0) (point-max) (point-min)))
330 (defun end-of-defun (&optional arg)
331 "Move forward to next end of defun.
332 With argument, do it that many times.
333 Negative argument -N means move back to Nth preceding end of defun.
335 An end of a defun occurs right after the close-parenthesis that
336 matches the open-parenthesis that starts a defun; see function
337 `beginning-of-defun'.
339 If variable `end-of-defun-function' is non-nil, its value
340 is called as a function to find the defun's end."
341 (interactive "^p")
342 (or (not (eq this-command 'end-of-defun))
343 (eq last-command 'end-of-defun)
344 (and transient-mark-mode mark-active)
345 (push-mark))
346 (if (or (null arg) (= arg 0)) (setq arg 1))
347 (let ((pos (point))
348 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
349 (funcall end-of-defun-function)
350 ;; When comparing point against pos, we want to consider that if
351 ;; point was right after the end of the function, it's still
352 ;; considered as "in that function".
353 ;; E.g. `eval-defun' from right after the last close-paren.
354 (unless (bolp)
355 (skip-chars-forward " \t")
356 (if (looking-at "\\s<\\|\n")
357 (forward-line 1)))
358 (cond
359 ((> arg 0)
360 ;; Moving forward.
361 (if (> (point) pos)
362 ;; We already moved forward by one because we started from
363 ;; within a function.
364 (setq arg (1- arg))
365 ;; We started from after the end of the previous function.
366 (goto-char pos))
367 (unless (zerop arg)
368 (beginning-of-defun-raw (- arg))
369 (funcall end-of-defun-function)))
370 ((< arg 0)
371 ;; Moving backward.
372 (if (< (point) pos)
373 ;; We already moved backward because we started from between
374 ;; two functions.
375 (setq arg (1+ arg))
376 ;; We started from inside a function.
377 (goto-char beg))
378 (unless (zerop arg)
379 (beginning-of-defun-raw (- arg))
380 (funcall end-of-defun-function))))
381 (unless (bolp)
382 (skip-chars-forward " \t")
383 (if (looking-at "\\s<\\|\n")
384 (forward-line 1)))))
386 (defun mark-defun (&optional allow-extend)
387 "Put mark at end of this defun, point at beginning.
388 The defun marked is the one that contains point or follows point.
390 Interactively, if this command is repeated
391 or (in Transient Mark mode) if the mark is active,
392 it marks the next defun after the ones already marked."
393 (interactive "p")
394 (cond ((and allow-extend
395 (or (and (eq last-command this-command) (mark t))
396 (and transient-mark-mode mark-active)))
397 (set-mark
398 (save-excursion
399 (goto-char (mark))
400 (end-of-defun)
401 (point))))
403 (let ((opoint (point))
404 beg end)
405 (push-mark opoint)
406 ;; Try first in this order for the sake of languages with nested
407 ;; functions where several can end at the same place as with
408 ;; the offside rule, e.g. Python.
409 (beginning-of-defun)
410 (setq beg (point))
411 (end-of-defun)
412 (setq end (point))
413 (while (looking-at "^\n")
414 (forward-line 1))
415 (if (> (point) opoint)
416 (progn
417 ;; We got the right defun.
418 (push-mark beg nil t)
419 (goto-char end)
420 (exchange-point-and-mark))
421 ;; beginning-of-defun moved back one defun
422 ;; so we got the wrong one.
423 (goto-char opoint)
424 (end-of-defun)
425 (push-mark (point) nil t)
426 (beginning-of-defun))
427 (re-search-backward "^\n" (- (point) 1) t)))))
429 (defun narrow-to-defun (&optional arg)
430 "Make text outside current defun invisible.
431 The defun visible is the one that contains point or follows point.
432 Optional ARG is ignored."
433 (interactive)
434 (save-excursion
435 (widen)
436 (let ((opoint (point))
437 beg end)
438 ;; Try first in this order for the sake of languages with nested
439 ;; functions where several can end at the same place as with
440 ;; the offside rule, e.g. Python.
441 (beginning-of-defun)
442 (setq beg (point))
443 (end-of-defun)
444 (setq end (point))
445 (while (looking-at "^\n")
446 (forward-line 1))
447 (unless (> (point) opoint)
448 ;; beginning-of-defun moved back one defun
449 ;; so we got the wrong one.
450 (goto-char opoint)
451 (end-of-defun)
452 (setq end (point))
453 (beginning-of-defun)
454 (setq beg (point)))
455 (goto-char end)
456 (re-search-backward "^\n" (- (point) 1) t)
457 (narrow-to-region beg end))))
459 (defvar insert-pair-alist
460 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
461 "Alist of paired characters inserted by `insert-pair'.
462 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
463 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
464 of the pair whose key is equal to the last input character with
465 or without modifiers, are inserted by `insert-pair'.")
467 (defun insert-pair (&optional arg open close)
468 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
469 Leave point after the first character.
470 A negative ARG encloses the preceding ARG sexps instead.
471 No argument is equivalent to zero: just insert characters
472 and leave point between.
473 If `parens-require-spaces' is non-nil, this command also inserts a space
474 before and after, depending on the surrounding characters.
475 If region is active, insert enclosing characters at region boundaries.
477 If arguments OPEN and CLOSE are nil, the character pair is found
478 from the variable `insert-pair-alist' according to the last input
479 character with or without modifiers. If no character pair is
480 found in the variable `insert-pair-alist', then the last input
481 character is inserted ARG times.
483 This command assumes point is not in a string or comment."
484 (interactive "P")
485 (if (not (and open close))
486 (let ((pair (or (assq last-command-event insert-pair-alist)
487 (assq (event-basic-type last-command-event)
488 insert-pair-alist))))
489 (if pair
490 (if (nth 2 pair)
491 (setq open (nth 1 pair) close (nth 2 pair))
492 (setq open (nth 0 pair) close (nth 1 pair))))))
493 (if (and open close)
494 (if (and transient-mark-mode mark-active)
495 (progn
496 (save-excursion (goto-char (region-end)) (insert close))
497 (save-excursion (goto-char (region-beginning)) (insert open)))
498 (if arg (setq arg (prefix-numeric-value arg))
499 (setq arg 0))
500 (cond ((> arg 0) (skip-chars-forward " \t"))
501 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
502 (and parens-require-spaces
503 (not (bobp))
504 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
505 (insert " "))
506 (insert open)
507 (save-excursion
508 (or (eq arg 0) (forward-sexp arg))
509 (insert close)
510 (and parens-require-spaces
511 (not (eobp))
512 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
513 (insert " "))))
514 (insert-char (event-basic-type last-command-event)
515 (prefix-numeric-value arg))))
517 (defun insert-parentheses (&optional arg)
518 "Enclose following ARG sexps in parentheses.
519 Leave point after open-paren.
520 A negative ARG encloses the preceding ARG sexps instead.
521 No argument is equivalent to zero: just insert `()' and leave point between.
522 If `parens-require-spaces' is non-nil, this command also inserts a space
523 before and after, depending on the surrounding characters.
524 If region is active, insert enclosing characters at region boundaries.
526 This command assumes point is not in a string or comment."
527 (interactive "P")
528 (insert-pair arg ?\( ?\)))
530 (defun delete-pair ()
531 "Delete a pair of characters enclosing the sexp that follows point."
532 (interactive)
533 (save-excursion (forward-sexp 1) (delete-char -1))
534 (delete-char 1))
536 (defun raise-sexp (&optional arg)
537 "Raise ARG sexps higher up the tree."
538 (interactive "p")
539 (let ((s (if (and transient-mark-mode mark-active)
540 (buffer-substring (region-beginning) (region-end))
541 (buffer-substring
542 (point)
543 (save-excursion (forward-sexp arg) (point))))))
544 (backward-up-list 1)
545 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
546 (save-excursion (insert s))))
548 (defun move-past-close-and-reindent ()
549 "Move past next `)', delete indentation before it, then indent after it."
550 (interactive)
551 (up-list 1)
552 (forward-char -1)
553 (while (save-excursion ; this is my contribution
554 (let ((before-paren (point)))
555 (back-to-indentation)
556 (and (= (point) before-paren)
557 (progn
558 ;; Move to end of previous line.
559 (beginning-of-line)
560 (forward-char -1)
561 ;; Verify it doesn't end within a string or comment.
562 (let ((end (point))
563 state)
564 (beginning-of-line)
565 ;; Get state at start of line.
566 (setq state (list 0 nil nil
567 (null (calculate-lisp-indent))
568 nil nil nil nil
569 nil))
570 ;; Parse state across the line to get state at end.
571 (setq state (parse-partial-sexp (point) end nil nil
572 state))
573 ;; Check not in string or comment.
574 (and (not (elt state 3)) (not (elt state 4))))))))
575 (delete-indentation))
576 (forward-char 1)
577 (newline-and-indent))
579 (defun check-parens () ; lame name?
580 "Check for unbalanced parentheses in the current buffer.
581 More accurately, check the narrowed part of the buffer for unbalanced
582 expressions (\"sexps\") in general. This is done according to the
583 current syntax table and will find unbalanced brackets or quotes as
584 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
585 found, an error is signaled and point is left at the first unbalanced
586 character."
587 (interactive)
588 (condition-case data
589 ;; Buffer can't have more than (point-max) sexps.
590 (scan-sexps (point-min) (point-max))
591 (scan-error (goto-char (nth 2 data))
592 ;; Could print (nth 1 data), which is either
593 ;; "Containing expression ends prematurely" or
594 ;; "Unbalanced parentheses", but those may not be so
595 ;; accurate/helpful, e.g. quotes may actually be
596 ;; mismatched.
597 (error "Unmatched bracket or quote"))))
599 (defun field-complete (table &optional predicate)
600 (let ((minibuffer-completion-table table)
601 (minibuffer-completion-predicate predicate)
602 ;; This made sense for lisp-complete-symbol, but for
603 ;; field-complete, this is out of place. --Stef
604 ;; (completion-annotate-function
605 ;; (unless (eq predicate 'fboundp)
606 ;; (lambda (str)
607 ;; (if (fboundp (intern-soft str)) " <f>"))))
609 (call-interactively 'minibuffer-complete)))
611 (defun lisp-complete-symbol (&optional predicate)
612 "Perform completion on Lisp symbol preceding point.
613 Compare that symbol against the known Lisp symbols.
614 If no characters can be completed, display a list of possible completions.
615 Repeating the command at that point scrolls the list.
617 When called from a program, optional arg PREDICATE is a predicate
618 determining which symbols are considered, e.g. `commandp'.
619 If PREDICATE is nil, the context determines which symbols are
620 considered. If the symbol starts just after an open-parenthesis, only
621 symbols with function definitions are considered. Otherwise, all
622 symbols with function definitions, values or properties are
623 considered."
624 (interactive)
625 (let* ((data (lisp-completion-at-point predicate))
626 (plist (nthcdr 3 data)))
627 (let ((completion-annotate-function
628 (plist-get plist :annotation-function)))
629 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
630 (plist-get plist :predicate)))))
633 (defun lisp-completion-at-point (&optional predicate)
634 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
635 ;; FIXME: the `end' could be after point?
636 (with-syntax-table emacs-lisp-mode-syntax-table
637 (let* ((end (point))
638 (beg (save-excursion
639 (backward-sexp 1)
640 (while (= (char-syntax (following-char)) ?\')
641 (forward-char 1))
642 (point)))
643 (predicate
644 (or predicate
645 (save-excursion
646 (goto-char beg)
647 (if (not (eq (char-before) ?\())
648 (lambda (sym) ;why not just nil ? -sm
649 (or (boundp sym) (fboundp sym)
650 (symbol-plist sym)))
651 ;; Looks like a funcall position. Let's double check.
652 (if (condition-case nil
653 (progn (up-list -2) (forward-char 1)
654 (eq (char-after) ?\())
655 (error nil))
656 ;; If the first element of the parent list is an open
657 ;; paren we are probably not in a funcall position.
658 ;; Maybe a `let' varlist or something.
660 ;; Else, we assume that a function name is expected.
661 'fboundp))))))
662 (list beg end obarray
663 :predicate predicate
664 :annotation-function
665 (unless (eq predicate 'fboundp)
666 (lambda (str) (if (fboundp (intern-soft str)) " <f>")))))))
668 ;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
669 ;;; lisp.el ends here