]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lisp.el
Add a provide statement.
[gnu-emacs] / lisp / emacs-lisp / lisp.el
1 ;;; lisp.el --- Lisp editing commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 1994, 2000, 2004 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: lisp, languages
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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 the character that starts 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
38 Setting this variable automatically makes it local to the current buffer."
39 :type '(choice (const nil)
40 regexp)
41 :group 'lisp)
42 (make-variable-buffer-local 'defun-prompt-regexp)
43
44 (defcustom parens-require-spaces t
45 "Non-nil means `insert-parentheses' should insert whitespace as needed."
46 :type 'boolean
47 :group 'lisp)
48
49 (defvar forward-sexp-function nil
50 "If non-nil, `forward-sexp' delegates to this function.
51 Should take the same arguments and behave similarly to `forward-sexp'.")
52
53 (defun forward-sexp (&optional arg)
54 "Move forward across one balanced expression (sexp).
55 With ARG, do it that many times. Negative arg -N means
56 move backward across N balanced expressions."
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))))
63
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 (interactive "p")
69 (or arg (setq arg 1))
70 (forward-sexp (- arg)))
71
72 (defun mark-sexp (&optional arg)
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 If this command is repeated, it marks the next ARG sexps after the ones
77 already marked."
78 (interactive "P")
79 (cond ((and (eq last-command this-command) (mark t))
80 (setq arg (if arg (prefix-numeric-value arg)
81 (if (> (mark) (point)) 1 -1)))
82 (set-mark
83 (save-excursion
84 (goto-char (mark))
85 (forward-sexp arg)
86 (point))))
87 (t
88 (push-mark
89 (save-excursion
90 (forward-sexp (prefix-numeric-value arg))
91 (point))
92 nil t))))
93
94 (defun forward-list (&optional arg)
95 "Move forward across one balanced group of parentheses.
96 With ARG, do it that many times.
97 Negative arg -N means move backward across N groups of parentheses."
98 (interactive "p")
99 (or arg (setq arg 1))
100 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
101
102 (defun backward-list (&optional arg)
103 "Move backward across one balanced group of parentheses.
104 With ARG, do it that many times.
105 Negative arg -N means move forward across N groups of parentheses."
106 (interactive "p")
107 (or arg (setq arg 1))
108 (forward-list (- arg)))
109
110 (defun down-list (&optional arg)
111 "Move forward down one level of parentheses.
112 With ARG, do this that many times.
113 A negative argument means move backward but still go down a level."
114 (interactive "p")
115 (or arg (setq arg 1))
116 (let ((inc (if (> arg 0) 1 -1)))
117 (while (/= arg 0)
118 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
119 (setq arg (- arg inc)))))
120
121 (defun backward-up-list (&optional arg)
122 "Move backward out of one level of parentheses.
123 With ARG, do this that many times.
124 A negative argument means move forward but still to a less deep spot."
125 (interactive "p")
126 (up-list (- (or arg 1))))
127
128 (defun up-list (&optional arg)
129 "Move forward out of one level of parentheses.
130 With ARG, do this that many times.
131 A negative argument means move backward but still to a less deep spot."
132 (interactive "p")
133 (or arg (setq arg 1))
134 (let ((inc (if (> arg 0) 1 -1)))
135 (while (/= arg 0)
136 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
137 (setq arg (- arg inc)))))
138
139 (defun kill-sexp (&optional arg)
140 "Kill the sexp (balanced expression) following the cursor.
141 With ARG, kill that many sexps after the cursor.
142 Negative arg -N means kill N sexps before the cursor."
143 (interactive "p")
144 (let ((opoint (point)))
145 (forward-sexp (or arg 1))
146 (kill-region opoint (point))))
147
148 (defun backward-kill-sexp (&optional arg)
149 "Kill the sexp (balanced expression) preceding the cursor.
150 With ARG, kill that many sexps before the cursor.
151 Negative arg -N means kill N sexps after the cursor."
152 (interactive "p")
153 (kill-sexp (- (or arg 1))))
154
155 ;; After Zmacs:
156 (defun kill-backward-up-list (&optional arg)
157 "Kill the form containing the current sexp, leaving the sexp itself.
158 A prefix argument ARG causes the relevant number of surrounding
159 forms to be removed."
160 (interactive "*p")
161 (let ((current-sexp (thing-at-point 'sexp)))
162 (if current-sexp
163 (save-excursion
164 (backward-up-list arg)
165 (kill-sexp)
166 (insert current-sexp))
167 (error "Not at a sexp"))))
168 \f
169 (defvar beginning-of-defun-function nil
170 "If non-nil, function for `beginning-of-defun-raw' to call.
171 This is used to find the beginning of the defun instead of using the
172 normal recipe (see `beginning-of-defun'). Major modes can define this
173 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
174 needs.
175
176 The function (of no args) should go to the line on which the current
177 defun starts, and return non-nil, or should return nil if it can't
178 find the beginning.")
179
180 (defun beginning-of-defun (&optional arg)
181 "Move backward to the beginning of a defun.
182 With ARG, do it that many times. Negative arg -N
183 means move forward to Nth following beginning of defun.
184 Returns t unless search stops due to beginning or end of buffer.
185
186 Normally a defun starts when there is a char with open-parenthesis
187 syntax at the beginning of a line. If `defun-prompt-regexp' is
188 non-nil, then a string which matches that regexp may precede the
189 open-parenthesis, and point ends up at the beginning of the line.
190
191 If variable `beginning-of-defun-function' is non-nil, its value
192 is called as a function to find the defun's beginning."
193 (interactive "p")
194 (and (eq this-command 'beginning-of-defun)
195 (or inhibit-mark-movement (eq last-command 'beginning-of-defun)
196 (push-mark)))
197 (and (beginning-of-defun-raw arg)
198 (progn (beginning-of-line) t)))
199
200 (defun beginning-of-defun-raw (&optional arg)
201 "Move point to the character that starts a defun.
202 This is identical to function `beginning-of-defun', except that point
203 does not move to the beginning of the line when `defun-prompt-regexp'
204 is non-nil.
205
206 If variable `beginning-of-defun-function' is non-nil, its value
207 is called as a function to find the defun's beginning."
208 (interactive "p")
209 (if beginning-of-defun-function
210 (if (> (setq arg (or arg 1)) 0)
211 (dotimes (i arg)
212 (funcall beginning-of-defun-function))
213 ;; Better not call end-of-defun-function directly, in case
214 ;; it's not defined.
215 (end-of-defun (- arg)))
216 (and arg (< arg 0) (not (eobp)) (forward-char 1))
217 (and (re-search-backward (if defun-prompt-regexp
218 (concat (if open-paren-in-column-0-is-defun-start
219 "^\\s(\\|" "")
220 "\\(?:" defun-prompt-regexp "\\)\\s(")
221 "^\\s(")
222 nil 'move (or arg 1))
223 (progn (goto-char (1- (match-end 0)))) t)))
224
225 (defvar end-of-defun-function nil
226 "If non-nil, function for function `end-of-defun' to call.
227 This is used to find the end of the defun instead of using the normal
228 recipe (see `end-of-defun'). Major modes can define this if the
229 normal method is not appropriate.")
230
231 (defun buffer-end (arg)
232 (if (> arg 0) (point-max) (point-min)))
233
234 (defun end-of-defun (&optional arg)
235 "Move forward to next end of defun. With argument, do it that many times.
236 Negative argument -N means move back to Nth preceding end of defun.
237
238 An end of a defun occurs right after the close-parenthesis that
239 matches the open-parenthesis that starts a defun; see function
240 `beginning-of-defun'.
241
242 If variable `end-of-defun-function' is non-nil, its value
243 is called as a function to find the defun's end."
244 (interactive "p")
245 (and (eq this-command 'end-of-defun)
246 (or inhibit-mark-movement (eq last-command 'end-of-defun)
247 (push-mark)))
248 (if (or (null arg) (= arg 0)) (setq arg 1))
249 (if end-of-defun-function
250 (if (> arg 0)
251 (dotimes (i arg)
252 (funcall end-of-defun-function))
253 ;; Better not call beginning-of-defun-function
254 ;; directly, in case it's not defined.
255 (beginning-of-defun (- arg)))
256 (let ((first t))
257 (while (and (> arg 0) (< (point) (point-max)))
258 (let ((pos (point)))
259 (while (progn
260 (if (and first
261 (progn
262 (end-of-line 1)
263 (beginning-of-defun-raw 1)))
264 nil
265 (or (bobp) (forward-char -1))
266 (beginning-of-defun-raw -1))
267 (setq first nil)
268 (forward-list 1)
269 (skip-chars-forward " \t")
270 (if (looking-at "\\s<\\|\n")
271 (forward-line 1))
272 (<= (point) pos))))
273 (setq arg (1- arg)))
274 (while (< arg 0)
275 (let ((pos (point)))
276 (beginning-of-defun-raw 1)
277 (forward-sexp 1)
278 (forward-line 1)
279 (if (>= (point) pos)
280 (if (beginning-of-defun-raw 2)
281 (progn
282 (forward-list 1)
283 (skip-chars-forward " \t")
284 (if (looking-at "\\s<\\|\n")
285 (forward-line 1)))
286 (goto-char (point-min)))))
287 (setq arg (1+ arg))))))
288
289 (defun mark-defun ()
290 "Put mark at end of this defun, point at beginning.
291 The defun marked is the one that contains point or follows point.
292 If this command is repeated, marks more defuns after the ones
293 already marked."
294 (interactive)
295 (cond ((and (eq last-command this-command) (mark t))
296 (set-mark
297 (save-excursion
298 (goto-char (mark))
299 (end-of-defun)
300 (point))))
301 (t
302 (let ((opoint (point))
303 beg end)
304 (push-mark opoint)
305 ;; Try first in this order for the sake of languages with nested
306 ;; functions where several can end at the same place as with
307 ;; the offside rule, e.g. Python.
308 (beginning-of-defun)
309 (setq beg (point))
310 (end-of-defun)
311 (setq end (point))
312 (while (looking-at "^\n")
313 (forward-line 1))
314 (if (> (point) opoint)
315 (progn
316 ;; We got the right defun.
317 (push-mark beg nil t)
318 (goto-char end)
319 (exchange-point-and-mark))
320 ;; beginning-of-defun moved back one defun
321 ;; so we got the wrong one.
322 (goto-char opoint)
323 (end-of-defun)
324 (push-mark (point) nil t)
325 (beginning-of-defun))
326 (re-search-backward "^\n" (- (point) 1) t)))))
327
328 (defun narrow-to-defun (&optional arg)
329 "Make text outside current defun invisible.
330 The defun visible is the one that contains point or follows point.
331 Optional ARG is ignored."
332 (interactive)
333 (save-excursion
334 (widen)
335 (let ((opoint (point))
336 beg end)
337 ;; Try first in this order for the sake of languages with nested
338 ;; functions where several can end at the same place as with
339 ;; the offside rule, e.g. Python.
340 (beginning-of-defun)
341 (setq beg (point))
342 (end-of-defun)
343 (setq end (point))
344 (while (looking-at "^\n")
345 (forward-line 1))
346 (unless (> (point) opoint)
347 ;; beginning-of-defun moved back one defun
348 ;; so we got the wrong one.
349 (goto-char opoint)
350 (end-of-defun)
351 (setq end (point))
352 (beginning-of-defun)
353 (setq beg (point)))
354 (goto-char end)
355 (re-search-backward "^\n" (- (point) 1) t)
356 (narrow-to-region beg end))))
357
358 (defvar insert-pair-alist
359 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
360 "Alist of paired characters inserted by `insert-pair'.
361 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
362 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
363 of the pair whose key is equal to the last input character with
364 or without modifiers, are inserted by `insert-pair'.")
365
366 (defun insert-pair (&optional arg open close)
367 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
368 Leave point after the first character.
369 A negative ARG encloses the preceding ARG sexps instead.
370 No argument is equivalent to zero: just insert characters
371 and leave point between.
372 If `parens-require-spaces' is non-nil, this command also inserts a space
373 before and after, depending on the surrounding characters.
374 If region is active, insert enclosing characters at region boundaries.
375
376 If arguments OPEN and CLOSE are nil, the character pair is found
377 from the variable `insert-pair-alist' according to the last input
378 character with or without modifiers. If no character pair is
379 found in the variable `insert-pair-alist', then the last input
380 character is inserted ARG times."
381 (interactive "P")
382 (if (not (and open close))
383 (let ((pair (or (assq last-command-char insert-pair-alist)
384 (assq (event-basic-type last-command-event)
385 insert-pair-alist))))
386 (if pair
387 (if (nth 2 pair)
388 (setq open (nth 1 pair) close (nth 2 pair))
389 (setq open (nth 0 pair) close (nth 1 pair))))))
390 (if (and open close)
391 (if (and transient-mark-mode mark-active)
392 (progn
393 (save-excursion (goto-char (region-end)) (insert close))
394 (save-excursion (goto-char (region-beginning)) (insert open)))
395 (if arg (setq arg (prefix-numeric-value arg))
396 (setq arg 0))
397 (cond ((> arg 0) (skip-chars-forward " \t"))
398 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
399 (and parens-require-spaces
400 (not (bobp))
401 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
402 (insert " "))
403 (insert open)
404 (save-excursion
405 (or (eq arg 0) (forward-sexp arg))
406 (insert close)
407 (and parens-require-spaces
408 (not (eobp))
409 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
410 (insert " "))))
411 (insert-char (event-basic-type last-command-event)
412 (prefix-numeric-value arg))))
413
414 (defun insert-parentheses (&optional arg)
415 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
416 A negative ARG encloses the preceding ARG sexps instead.
417 No argument is equivalent to zero: just insert `()' and leave point between.
418 If `parens-require-spaces' is non-nil, this command also inserts a space
419 before and after, depending on the surrounding characters.
420 If region is active, insert enclosing characters at region boundaries."
421 (interactive "P")
422 (insert-pair arg ?\( ?\)))
423
424 (defun delete-pair ()
425 "Delete a pair of characters enclosing the sexp that follows point."
426 (interactive)
427 (save-excursion (forward-sexp 1) (delete-char -1))
428 (delete-char 1))
429
430 (defun raise-sexp (&optional arg)
431 "Raise ARG sexps higher up the tree."
432 (interactive "p")
433 (let ((s (if (and transient-mark-mode mark-active)
434 (buffer-substring (region-beginning) (region-end))
435 (buffer-substring
436 (point)
437 (save-excursion (forward-sexp arg) (point))))))
438 (backward-up-list 1)
439 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
440 (save-excursion (insert s))))
441
442 (defun move-past-close-and-reindent ()
443 "Move past next `)', delete indentation before it, then indent after it."
444 (interactive)
445 (up-list 1)
446 (forward-char -1)
447 (while (save-excursion ; this is my contribution
448 (let ((before-paren (point)))
449 (back-to-indentation)
450 (and (= (point) before-paren)
451 (progn
452 ;; Move to end of previous line.
453 (beginning-of-line)
454 (forward-char -1)
455 ;; Verify it doesn't end within a string or comment.
456 (let ((end (point))
457 state)
458 (beginning-of-line)
459 ;; Get state at start of line.
460 (setq state (list 0 nil nil
461 (null (calculate-lisp-indent))
462 nil nil nil nil
463 nil))
464 ;; Parse state across the line to get state at end.
465 (setq state (parse-partial-sexp (point) end nil nil
466 state))
467 ;; Check not in string or comment.
468 (and (not (elt state 3)) (not (elt state 4))))))))
469 (delete-indentation))
470 (forward-char 1)
471 (newline-and-indent))
472
473 (defun check-parens () ; lame name?
474 "Check for unbalanced parentheses in the current buffer.
475 More accurately, check the narrowed part of the buffer for unbalanced
476 expressions (\"sexps\") in general. This is done according to the
477 current syntax table and will find unbalanced brackets or quotes as
478 appropriate. (See Info node `(emacs)Lists and Sexps'.) If imbalance
479 is found, an error is signalled and point is left at the first
480 unbalanced character."
481 (interactive)
482 (condition-case data
483 ;; Buffer can't have more than (point-max) sexps.
484 (scan-sexps (point-min) (point-max))
485 (scan-error (goto-char (nth 2 data))
486 ;; Could print (nth 1 data), which is either
487 ;; "Containing expression ends prematurely" or
488 ;; "Unbalanced parentheses", but those may not be so
489 ;; accurate/helpful, e.g. quotes may actually be
490 ;; mismatched.
491 (error "Unmatched bracket or quote"))
492 (error (cond ((eq 'scan-error (car data))
493 (goto-char (nth 2 data))
494 (error "Unmatched bracket or quote"))
495 (t (signal (car data) (cdr data)))))))
496 \f
497 (defun lisp-complete-symbol (&optional predicate)
498 "Perform completion on Lisp symbol preceding point.
499 Compare that symbol against the known Lisp symbols.
500 If no characters can be completed, display a list of possible completions.
501 Repeating the command at that point scrolls the list.
502
503 When called from a program, optional arg PREDICATE is a predicate
504 determining which symbols are considered, e.g. `commandp'.
505 If PREDICATE is nil, the context determines which symbols are
506 considered. If the symbol starts just after an open-parenthesis, only
507 symbols with function definitions are considered. Otherwise, all
508 symbols with function definitions, values or properties are
509 considered."
510 (interactive)
511
512 (let ((window (get-buffer-window "*Completions*")))
513 (if (and (eq last-command this-command)
514 window (window-live-p window) (window-buffer window)
515 (buffer-name (window-buffer window)))
516 ;; If this command was repeated, and
517 ;; there's a fresh completion window with a live buffer,
518 ;; and this command is repeated, scroll that window.
519 (with-current-buffer (window-buffer window)
520 (if (pos-visible-in-window-p (point-max) window)
521 (set-window-start window (point-min))
522 (save-selected-window
523 (select-window window)
524 (scroll-up))))
525
526 ;; Do completion.
527 (let* ((end (point))
528 (beg (with-syntax-table emacs-lisp-mode-syntax-table
529 (save-excursion
530 (backward-sexp 1)
531 (while (= (char-syntax (following-char)) ?\')
532 (forward-char 1))
533 (point))))
534 (pattern (buffer-substring-no-properties beg end))
535 (predicate
536 (or predicate
537 (save-excursion
538 (goto-char beg)
539 (if (not (eq (char-before) ?\())
540 (lambda (sym) ;why not just nil ? -sm
541 (or (boundp sym) (fboundp sym)
542 (symbol-plist sym)))
543 ;; Looks like a funcall position. Let's double check.
544 (if (condition-case nil
545 (progn (up-list -2) (forward-char 1)
546 (eq (char-after) ?\())
547 (error nil))
548 ;; If the first element of the parent list is an open
549 ;; parenthesis we are probably not in a funcall position.
550 ;; Maybe a `let' varlist or something.
551 nil
552 ;; Else, we assume that a function name is expected.
553 'fboundp)))))
554 (completion (try-completion pattern obarray predicate)))
555 (cond ((eq completion t))
556 ((null completion)
557 (message "Can't find completion for \"%s\"" pattern)
558 (ding))
559 ((not (string= pattern completion))
560 (delete-region beg end)
561 (insert completion))
562 (t
563 (message "Making completion list...")
564 (let ((list (all-completions pattern obarray predicate)))
565 (setq list (sort list 'string<))
566 (or (eq predicate 'fboundp)
567 (let (new)
568 (while list
569 (setq new (cons (if (fboundp (intern (car list)))
570 (list (car list) " <f>")
571 (car list))
572 new))
573 (setq list (cdr list)))
574 (setq list (nreverse new))))
575 (with-output-to-temp-buffer "*Completions*"
576 (display-completion-list list)))
577 (message "Making completion list...%s" "done")))))))
578
579 ;;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
580 ;;; lisp.el ends here