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