New syntax-propertize functionality.
[emacs.git] / lisp / emacs-lisp / lisp.el
blobe799dcd77c154ad8212d5a85c306da5c2f4264b2
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 Free Software Foundation, 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 (while (/= arg 0)
146 (if forward-sexp-function
147 (condition-case err
148 (while (let ((pos (point)))
149 (forward-sexp inc)
150 (/= (point) pos)))
151 (scan-error (goto-char (nth 2 err))))
152 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg))))
153 (setq arg (- arg inc)))))
155 (defun kill-sexp (&optional arg)
156 "Kill the sexp (balanced expression) following point.
157 With ARG, kill that many sexps after point.
158 Negative arg -N means kill N sexps before point.
159 This command assumes point is not in a string or comment."
160 (interactive "p")
161 (let ((opoint (point)))
162 (forward-sexp (or arg 1))
163 (kill-region opoint (point))))
165 (defun backward-kill-sexp (&optional arg)
166 "Kill the sexp (balanced expression) preceding point.
167 With ARG, kill that many sexps before point.
168 Negative arg -N means kill N sexps after point.
169 This command assumes point is not in a string or comment."
170 (interactive "p")
171 (kill-sexp (- (or arg 1))))
173 ;; After Zmacs:
174 (defun kill-backward-up-list (&optional arg)
175 "Kill the form containing the current sexp, leaving the sexp itself.
176 A prefix argument ARG causes the relevant number of surrounding
177 forms to be removed.
178 This command assumes point is not in a string or comment."
179 (interactive "*p")
180 (let ((current-sexp (thing-at-point 'sexp)))
181 (if current-sexp
182 (save-excursion
183 (backward-up-list arg)
184 (kill-sexp)
185 (insert current-sexp))
186 (error "Not at a sexp"))))
188 (defvar beginning-of-defun-function nil
189 "If non-nil, function for `beginning-of-defun-raw' to call.
190 This is used to find the beginning of the defun instead of using the
191 normal recipe (see `beginning-of-defun'). Major modes can define this
192 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
193 needs.
195 The function takes the same argument as `beginning-of-defun' and should
196 behave similarly, returning non-nil if it found the beginning of a defun.
197 Ideally it should move to a point right before an open-paren which encloses
198 the body of the defun.")
200 (defun beginning-of-defun (&optional arg)
201 "Move backward to the beginning of a defun.
202 With ARG, do it that many times. Negative ARG means move forward
203 to the ARGth following beginning of defun.
205 If search is successful, return t; point ends up at the beginning
206 of the line where the search succeeded. Otherwise, return nil.
208 When `open-paren-in-column-0-is-defun-start' is non-nil, a defun
209 is assumed to start where there is a char with open-parenthesis
210 syntax at the beginning of a line. If `defun-prompt-regexp' is
211 non-nil, then a string which matches that regexp may also precede
212 the open-parenthesis. If `defun-prompt-regexp' and
213 `open-paren-in-column-0-is-defun-start' are both nil, this
214 function instead finds an open-paren at the outermost level.
216 If the variable `beginning-of-defun-function' is non-nil, its
217 value is called as a function, with argument ARG, to find the
218 defun's beginning.
220 Regardless of the values of `defun-prompt-regexp' and
221 `beginning-of-defun-function', point always moves to the
222 beginning of the line whenever the search is successful."
223 (interactive "^p")
224 (or (not (eq this-command 'beginning-of-defun))
225 (eq last-command 'beginning-of-defun)
226 (and transient-mark-mode mark-active)
227 (push-mark))
228 (and (beginning-of-defun-raw arg)
229 (progn (beginning-of-line) t)))
231 (defun beginning-of-defun-raw (&optional arg)
232 "Move point to the character that starts a defun.
233 This is identical to function `beginning-of-defun', except that point
234 does not move to the beginning of the line when `defun-prompt-regexp'
235 is non-nil.
237 If variable `beginning-of-defun-function' is non-nil, its value
238 is called as a function to find the defun's beginning."
239 (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG
240 ; to beginning-of-defun-function.
241 (unless arg (setq arg 1))
242 (cond
243 (beginning-of-defun-function
244 (condition-case nil
245 (funcall beginning-of-defun-function arg)
246 ;; We used to define beginning-of-defun-function as taking no argument
247 ;; but that makes it impossible to implement correct forward motion:
248 ;; we used to use end-of-defun for that, but it's not supposed to do
249 ;; the same thing (it moves to the end of a defun not to the beginning
250 ;; of the next).
251 ;; In case the beginning-of-defun-function uses the old calling
252 ;; convention, fallback on the old implementation.
253 (wrong-number-of-arguments
254 (if (> arg 0)
255 (dotimes (i arg)
256 (funcall beginning-of-defun-function))
257 ;; Better not call end-of-defun-function directly, in case
258 ;; it's not defined.
259 (end-of-defun (- arg))))))
261 ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start)
262 (and (< arg 0) (not (eobp)) (forward-char 1))
263 (and (re-search-backward (if defun-prompt-regexp
264 (concat (if open-paren-in-column-0-is-defun-start
265 "^\\s(\\|" "")
266 "\\(?:" defun-prompt-regexp "\\)\\s(")
267 "^\\s(")
268 nil 'move arg)
269 (progn (goto-char (1- (match-end 0)))
270 t)))
272 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
273 ;; are both nil, column 0 has no significance - so scan forward
274 ;; from BOB to see how nested point is, then carry on from there.
276 ;; It is generally not a good idea to land up here, because the
277 ;; call to scan-lists below can be extremely slow. This is because
278 ;; back_comment in syntax.c may have to scan from bob to find the
279 ;; beginning of each comment. Fixing this is not trivial -- cyd.
281 ((eq arg 0))
283 (let ((floor (point-min))
284 (ceiling (point-max))
285 (arg-+ve (> arg 0)))
286 (save-restriction
287 (widen)
288 (let ((ppss (let (syntax-begin-function
289 font-lock-beginning-of-syntax-function)
290 (syntax-ppss)))
291 ;; position of least enclosing paren, or nil.
292 encl-pos)
293 ;; Back out of any comment/string, so that encl-pos will always
294 ;; become nil if we're at top-level.
295 (when (nth 8 ppss)
296 (goto-char (nth 8 ppss))
297 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
298 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
299 (if encl-pos (goto-char encl-pos))
301 (and encl-pos arg-+ve (setq arg (1- arg)))
302 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
303 (setq arg (1+ arg)))
305 (condition-case nil ; to catch crazy parens.
306 (progn
307 (goto-char (scan-lists (point) (- arg) 0))
308 (if arg-+ve
309 (if (>= (point) floor)
311 (goto-char floor)
312 nil)
313 ;; forward to next (, or trigger the c-c
314 (goto-char (1- (scan-lists (point) 1 -1)))
315 (if (<= (point) ceiling)
317 (goto-char ceiling)
318 nil)))
319 (error
320 (goto-char (if arg-+ve floor ceiling))
321 nil))))))))
323 (defvar end-of-defun-function
324 (lambda () (forward-sexp 1))
325 "Function for `end-of-defun' to call.
326 This is used to find the end of the defun at point.
327 It is called with no argument, right after calling `beginning-of-defun-raw'.
328 So the function can assume that point is at the beginning of the defun body.
329 It should move point to the first position after the defun.")
331 (defun buffer-end (arg)
332 "Return the \"far end\" position of the buffer, in direction ARG.
333 If ARG is positive, that's the end of the buffer.
334 Otherwise, that's the beginning of the buffer."
335 (if (> arg 0) (point-max) (point-min)))
337 (defun end-of-defun (&optional arg)
338 "Move forward to next end of defun.
339 With argument, do it that many times.
340 Negative argument -N means move back to Nth preceding end of defun.
342 An end of a defun occurs right after the close-parenthesis that
343 matches the open-parenthesis that starts a defun; see function
344 `beginning-of-defun'.
346 If variable `end-of-defun-function' is non-nil, its value
347 is called as a function to find the defun's end."
348 (interactive "^p")
349 (or (not (eq this-command 'end-of-defun))
350 (eq last-command 'end-of-defun)
351 (and transient-mark-mode mark-active)
352 (push-mark))
353 (if (or (null arg) (= arg 0)) (setq arg 1))
354 (let ((pos (point))
355 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
356 (funcall end-of-defun-function)
357 ;; When comparing point against pos, we want to consider that if
358 ;; point was right after the end of the function, it's still
359 ;; considered as "in that function".
360 ;; E.g. `eval-defun' from right after the last close-paren.
361 (unless (bolp)
362 (skip-chars-forward " \t")
363 (if (looking-at "\\s<\\|\n")
364 (forward-line 1)))
365 (cond
366 ((> arg 0)
367 ;; Moving forward.
368 (if (> (point) pos)
369 ;; We already moved forward by one because we started from
370 ;; within a function.
371 (setq arg (1- arg))
372 ;; We started from after the end of the previous function.
373 (goto-char pos))
374 (unless (zerop arg)
375 (beginning-of-defun-raw (- arg))
376 (funcall end-of-defun-function)))
377 ((< arg 0)
378 ;; Moving backward.
379 (if (< (point) pos)
380 ;; We already moved backward because we started from between
381 ;; two functions.
382 (setq arg (1+ arg))
383 ;; We started from inside a function.
384 (goto-char beg))
385 (unless (zerop arg)
386 (beginning-of-defun-raw (- arg))
387 (funcall end-of-defun-function))))
388 (unless (bolp)
389 (skip-chars-forward " \t")
390 (if (looking-at "\\s<\\|\n")
391 (forward-line 1)))))
393 (defun mark-defun (&optional allow-extend)
394 "Put mark at end of this defun, point at beginning.
395 The defun marked is the one that contains point or follows point.
397 Interactively, if this command is repeated
398 or (in Transient Mark mode) if the mark is active,
399 it marks the next defun after the ones already marked."
400 (interactive "p")
401 (cond ((and allow-extend
402 (or (and (eq last-command this-command) (mark t))
403 (and transient-mark-mode mark-active)))
404 (set-mark
405 (save-excursion
406 (goto-char (mark))
407 (end-of-defun)
408 (point))))
410 (let ((opoint (point))
411 beg end)
412 (push-mark opoint)
413 ;; Try first in this order for the sake of languages with nested
414 ;; functions where several can end at the same place as with
415 ;; the offside rule, e.g. Python.
416 (beginning-of-defun)
417 (setq beg (point))
418 (end-of-defun)
419 (setq end (point))
420 (while (looking-at "^\n")
421 (forward-line 1))
422 (if (> (point) opoint)
423 (progn
424 ;; We got the right defun.
425 (push-mark beg nil t)
426 (goto-char end)
427 (exchange-point-and-mark))
428 ;; beginning-of-defun moved back one defun
429 ;; so we got the wrong one.
430 (goto-char opoint)
431 (end-of-defun)
432 (push-mark (point) nil t)
433 (beginning-of-defun))
434 (re-search-backward "^\n" (- (point) 1) t)))))
436 (defun narrow-to-defun (&optional arg)
437 "Make text outside current defun invisible.
438 The defun visible is the one that contains point or follows point.
439 Optional ARG is ignored."
440 (interactive)
441 (save-excursion
442 (widen)
443 (let ((opoint (point))
444 beg end)
445 ;; Try first in this order for the sake of languages with nested
446 ;; functions where several can end at the same place as with
447 ;; the offside rule, e.g. Python.
448 (beginning-of-defun)
449 (setq beg (point))
450 (end-of-defun)
451 (setq end (point))
452 (while (looking-at "^\n")
453 (forward-line 1))
454 (unless (> (point) opoint)
455 ;; beginning-of-defun moved back one defun
456 ;; so we got the wrong one.
457 (goto-char opoint)
458 (end-of-defun)
459 (setq end (point))
460 (beginning-of-defun)
461 (setq beg (point)))
462 (goto-char end)
463 (re-search-backward "^\n" (- (point) 1) t)
464 (narrow-to-region beg end))))
466 (defvar insert-pair-alist
467 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
468 "Alist of paired characters inserted by `insert-pair'.
469 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
470 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
471 of the pair whose key is equal to the last input character with
472 or without modifiers, are inserted by `insert-pair'.")
474 (defun insert-pair (&optional arg open close)
475 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
476 Leave point after the first character.
477 A negative ARG encloses the preceding ARG sexps instead.
478 No argument is equivalent to zero: just insert characters
479 and leave point between.
480 If `parens-require-spaces' is non-nil, this command also inserts a space
481 before and after, depending on the surrounding characters.
482 If region is active, insert enclosing characters at region boundaries.
484 If arguments OPEN and CLOSE are nil, the character pair is found
485 from the variable `insert-pair-alist' according to the last input
486 character with or without modifiers. If no character pair is
487 found in the variable `insert-pair-alist', then the last input
488 character is inserted ARG times.
490 This command assumes point is not in a string or comment."
491 (interactive "P")
492 (if (not (and open close))
493 (let ((pair (or (assq last-command-event insert-pair-alist)
494 (assq (event-basic-type last-command-event)
495 insert-pair-alist))))
496 (if pair
497 (if (nth 2 pair)
498 (setq open (nth 1 pair) close (nth 2 pair))
499 (setq open (nth 0 pair) close (nth 1 pair))))))
500 (if (and open close)
501 (if (and transient-mark-mode mark-active)
502 (progn
503 (save-excursion (goto-char (region-end)) (insert close))
504 (save-excursion (goto-char (region-beginning)) (insert open)))
505 (if arg (setq arg (prefix-numeric-value arg))
506 (setq arg 0))
507 (cond ((> arg 0) (skip-chars-forward " \t"))
508 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
509 (and parens-require-spaces
510 (not (bobp))
511 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
512 (insert " "))
513 (insert open)
514 (save-excursion
515 (or (eq arg 0) (forward-sexp arg))
516 (insert close)
517 (and parens-require-spaces
518 (not (eobp))
519 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
520 (insert " "))))
521 (insert-char (event-basic-type last-command-event)
522 (prefix-numeric-value arg))))
524 (defun insert-parentheses (&optional arg)
525 "Enclose following ARG sexps in parentheses.
526 Leave point after open-paren.
527 A negative ARG encloses the preceding ARG sexps instead.
528 No argument is equivalent to zero: just insert `()' and leave point between.
529 If `parens-require-spaces' is non-nil, this command also inserts a space
530 before and after, depending on the surrounding characters.
531 If region is active, insert enclosing characters at region boundaries.
533 This command assumes point is not in a string or comment."
534 (interactive "P")
535 (insert-pair arg ?\( ?\)))
537 (defun delete-pair ()
538 "Delete a pair of characters enclosing the sexp that follows point."
539 (interactive)
540 (save-excursion (forward-sexp 1) (delete-char -1))
541 (delete-char 1))
543 (defun raise-sexp (&optional arg)
544 "Raise ARG sexps higher up the tree."
545 (interactive "p")
546 (let ((s (if (and transient-mark-mode mark-active)
547 (buffer-substring (region-beginning) (region-end))
548 (buffer-substring
549 (point)
550 (save-excursion (forward-sexp arg) (point))))))
551 (backward-up-list 1)
552 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
553 (save-excursion (insert s))))
555 (defun move-past-close-and-reindent ()
556 "Move past next `)', delete indentation before it, then indent after it."
557 (interactive)
558 (up-list 1)
559 (forward-char -1)
560 (while (save-excursion ; this is my contribution
561 (let ((before-paren (point)))
562 (back-to-indentation)
563 (and (= (point) before-paren)
564 (progn
565 ;; Move to end of previous line.
566 (beginning-of-line)
567 (forward-char -1)
568 ;; Verify it doesn't end within a string or comment.
569 (let ((end (point))
570 state)
571 (beginning-of-line)
572 ;; Get state at start of line.
573 (setq state (list 0 nil nil
574 (null (calculate-lisp-indent))
575 nil nil nil nil
576 nil))
577 ;; Parse state across the line to get state at end.
578 (setq state (parse-partial-sexp (point) end nil nil
579 state))
580 ;; Check not in string or comment.
581 (and (not (elt state 3)) (not (elt state 4))))))))
582 (delete-indentation))
583 (forward-char 1)
584 (newline-and-indent))
586 (defun check-parens () ; lame name?
587 "Check for unbalanced parentheses in the current buffer.
588 More accurately, check the narrowed part of the buffer for unbalanced
589 expressions (\"sexps\") in general. This is done according to the
590 current syntax table and will find unbalanced brackets or quotes as
591 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
592 found, an error is signaled and point is left at the first unbalanced
593 character."
594 (interactive)
595 (condition-case data
596 ;; Buffer can't have more than (point-max) sexps.
597 (scan-sexps (point-min) (point-max))
598 (scan-error (goto-char (nth 2 data))
599 ;; Could print (nth 1 data), which is either
600 ;; "Containing expression ends prematurely" or
601 ;; "Unbalanced parentheses", but those may not be so
602 ;; accurate/helpful, e.g. quotes may actually be
603 ;; mismatched.
604 (error "Unmatched bracket or quote"))))
606 (defun field-complete (table &optional predicate)
607 (let ((minibuffer-completion-table table)
608 (minibuffer-completion-predicate predicate)
609 ;; This made sense for lisp-complete-symbol, but for
610 ;; field-complete, this is out of place. --Stef
611 ;; (completion-annotate-function
612 ;; (unless (eq predicate 'fboundp)
613 ;; (lambda (str)
614 ;; (if (fboundp (intern-soft str)) " <f>"))))
616 (call-interactively 'minibuffer-complete)))
618 (defun lisp-complete-symbol (&optional predicate)
619 "Perform completion on Lisp symbol preceding point.
620 Compare that symbol against the known Lisp symbols.
621 If no characters can be completed, display a list of possible completions.
622 Repeating the command at that point scrolls the list.
624 When called from a program, optional arg PREDICATE is a predicate
625 determining which symbols are considered, e.g. `commandp'.
626 If PREDICATE is nil, the context determines which symbols are
627 considered. If the symbol starts just after an open-parenthesis, only
628 symbols with function definitions are considered. Otherwise, all
629 symbols with function definitions, values or properties are
630 considered."
631 (interactive)
632 (let* ((data (lisp-completion-at-point predicate))
633 (plist (nthcdr 3 data)))
634 (if (null data)
635 (minibuffer-message "Nothing to complete")
636 (let ((completion-annotate-function
637 (plist-get plist :annotate-function)))
638 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
639 (plist-get plist :predicate))))))
642 (defun lisp-completion-at-point (&optional predicate)
643 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
644 ;; FIXME: the `end' could be after point?
645 (let* ((pos (point))
646 (beg (with-syntax-table emacs-lisp-mode-syntax-table
647 (condition-case nil
648 (save-excursion
649 (backward-sexp 1)
650 (skip-syntax-forward "'")
651 (point))
652 (scan-error pos))))
653 (predicate
654 (or predicate
655 (save-excursion
656 (goto-char beg)
657 (if (not (eq (char-before) ?\())
658 (lambda (sym) ;why not just nil ? -sm
659 (or (boundp sym) (fboundp sym)
660 (symbol-plist sym)))
661 ;; Looks like a funcall position. Let's double check.
662 (if (condition-case nil
663 (progn (up-list -2) (forward-char 1)
664 (eq (char-after) ?\())
665 (error nil))
666 ;; If the first element of the parent list is an open
667 ;; parenthesis we are probably not in a funcall position.
668 ;; Maybe a `let' varlist or something.
670 ;; Else, we assume that a function name is expected.
671 'fboundp)))))
672 (end
673 (unless (or (eq beg (point-max))
674 (member (char-syntax (char-after beg)) '(?\" ?\( ?\))))
675 (condition-case nil
676 (save-excursion
677 (goto-char beg)
678 (forward-sexp 1)
679 (when (>= (point) pos)
680 (point)))
681 (scan-error pos)))))
682 (when end
683 (list beg end obarray
684 :predicate predicate
685 :annotate-function
686 (unless (eq predicate 'fboundp)
687 (lambda (str) (if (fboundp (intern-soft str)) " <f>")))))))
689 ;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
690 ;;; lisp.el ends here