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