]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lisp.el
*** empty log message ***
[gnu-emacs] / lisp / emacs-lisp / lisp.el
1 ;;; lisp.el --- Lisp editing commands for Emacs
2
3 ;; Maintainer: FSF
4 ;; Last-Modified: 12 Mar 1992
5 ;; Keyword: lisp, languages
6
7 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Code:
26
27 (defvar defun-prompt-regexp nil
28 "Non-nil => regexp to ignore, before the `(' that starts a defun.")
29
30 (defun forward-sexp (&optional arg)
31 "Move forward across one balanced expression (sexp).
32 With argument, do it that many times. Negative arg -N means
33 move backward across N balanced expressions."
34 (interactive "p")
35 (or arg (setq arg 1))
36 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
37 (if (< arg 0) (backward-prefix-chars)))
38
39 (defun backward-sexp (&optional arg)
40 "Move backward across one balanced expression (sexp).
41 With argument, do it that many times. Negative arg -N means
42 move forward across N balanced expressions."
43 (interactive "p")
44 (or arg (setq arg 1))
45 (forward-sexp (- arg)))
46
47 (defun mark-sexp (arg)
48 "Set mark ARG sexps from point.
49 The place mark goes is the same place \\[forward-sexp] would
50 move to with the same argument."
51 (interactive "p")
52 (push-mark
53 (save-excursion
54 (forward-sexp arg)
55 (point))))
56
57 (defun forward-list (&optional arg)
58 "Move forward across one balanced group of parentheses.
59 With argument, do it that many times.
60 Negative arg -N means move backward across N groups of parentheses."
61 (interactive "p")
62 (or arg (setq arg 1))
63 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
64
65 (defun backward-list (&optional arg)
66 "Move backward across one balanced group of parentheses.
67 With argument, do it that many times.
68 Negative arg -N means move forward across N groups of parentheses."
69 (interactive "p")
70 (or arg (setq arg 1))
71 (forward-list (- arg)))
72
73 (defun down-list (arg)
74 "Move forward down one level of parentheses.
75 With argument, do this that many times.
76 A negative argument means move backward but still go down a level.
77 In Lisp programs, an argument is required."
78 (interactive "p")
79 (let ((inc (if (> arg 0) 1 -1)))
80 (while (/= arg 0)
81 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
82 (setq arg (- arg inc)))))
83
84 (defun backward-up-list (arg)
85 "Move backward out of one level of parentheses.
86 With argument, do this that many times.
87 A negative argument means move forward but still to a less deep spot.
88 In Lisp programs, an argument is required."
89 (interactive "p")
90 (up-list (- arg)))
91
92 (defun up-list (arg)
93 "Move forward out of one level of parentheses.
94 With argument, do this that many times.
95 A negative argument means move backward but still to a less deep spot.
96 In Lisp programs, an argument is required."
97 (interactive "p")
98 (let ((inc (if (> arg 0) 1 -1)))
99 (while (/= arg 0)
100 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
101 (setq arg (- arg inc)))))
102
103 (defun kill-sexp (arg)
104 "Kill the sexp (balanced expression) following the cursor.
105 With argument, kill that many sexps after the cursor.
106 Negative arg -N means kill N sexps before the cursor."
107 (interactive "p")
108 (let ((opoint (point)))
109 (forward-sexp arg)
110 (kill-region opoint (point))))
111
112 (defun backward-kill-sexp (arg)
113 "Kill the sexp (balanced expression) preceding the cursor.
114 With argument, kill that many sexps before the cursor.
115 Negative arg -N means kill N sexps after the cursor."
116 (interactive "p")
117 (kill-sexp (- arg)))
118 \f
119 (defun beginning-of-defun (&optional arg)
120 "Move backward to the beginning of a defun.
121 With argument, do it that many times. Negative arg -N
122 means move forward to Nth following beginning of defun.
123 Returns t unless search stops due to beginning or end of buffer.
124
125 Normally a defun starts when there is an char with open-parenthesis
126 syntax at the beginning of a line. If `defun-prompt-regexp' is
127 non-nil, then a string which matches that regexp may precede the
128 open-parenthesis."
129 (interactive "p")
130 (and arg (< arg 0) (forward-char 1))
131 (and (re-search-backward (if defun-prompt-regexp
132 (concat "^\\s(\\|"
133 "\\(" defun-prompt-regexp "\\)\\s(")
134 "^\\s(")
135 nil 'move (or arg 1))
136 (progn (beginning-of-line) t)))
137
138 (defun buffer-end (arg)
139 (if (> arg 0) (point-max) (point-min)))
140
141 (defun end-of-defun (&optional arg)
142 "Move forward to next end of defun. With argument, do it that many times.
143 Negative argument -N means move back to Nth preceding end of defun.
144
145 An end of a defun occurs right after the close-parenthesis that matches
146 the open-parenthesis that starts a defun; see `beginning-of-defun'."
147 (interactive "p")
148 (if (or (null arg) (= arg 0)) (setq arg 1))
149 (let ((first t))
150 (while (and (> arg 0) (< (point) (point-max)))
151 (let ((pos (point)) npos)
152 (while (progn
153 (if (and first
154 (progn
155 (forward-char 1)
156 (beginning-of-defun 1)))
157 nil
158 (or (bobp) (forward-char -1))
159 (beginning-of-defun -1))
160 (setq first nil)
161 (forward-list 1)
162 (skip-chars-forward " \t")
163 (if (looking-at "\\s<\\|\n")
164 (forward-line 1))
165 (<= (point) pos))))
166 (setq arg (1- arg)))
167 (while (< arg 0)
168 (let ((pos (point)))
169 (beginning-of-defun 1)
170 (forward-sexp 1)
171 (forward-line 1)
172 (if (>= (point) pos)
173 (if (beginning-of-defun 2)
174 (progn
175 (forward-list 1)
176 (skip-chars-forward " \t")
177 (if (looking-at "[;\n]")
178 (forward-line 1)))
179 (goto-char (point-min)))))
180 (setq arg (1+ arg)))))
181
182 (defun mark-defun ()
183 "Put mark at end of this defun, point at beginning.
184 The defun marked is the one that contains point or follows point."
185 (interactive)
186 (push-mark (point))
187 (end-of-defun)
188 (push-mark (point))
189 (beginning-of-defun)
190 (re-search-backward "^\n" (- (point) 1) t))
191
192 (defun insert-parentheses (arg)
193 "Put parentheses around next ARG sexps. Leave point after open-paren.
194 No argument is equivalent to zero: just insert () and leave point between."
195 (interactive "P")
196 (if arg (setq arg (prefix-numeric-value arg))
197 (setq arg 0))
198 (or (eq arg 0) (skip-chars-forward " \t"))
199 (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
200 (insert " "))
201 (insert ?\()
202 (save-excursion
203 (or (eq arg 0) (forward-sexp arg))
204 (insert ?\))
205 (and (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
206 (insert " "))))
207
208 (defun move-past-close-and-reindent ()
209 "Move past next `)', delete indentation before it, then indent after it."
210 (interactive)
211 (up-list 1)
212 (forward-char -1)
213 (while (save-excursion ; this is my contribution
214 (let ((before-paren (point)))
215 (back-to-indentation)
216 (= (point) before-paren)))
217 (delete-indentation))
218 (forward-char 1)
219 (newline-and-indent))
220 \f
221 (defun lisp-complete-symbol ()
222 "Perform completion on Lisp symbol preceding point. That symbol is
223 compared against the symbols that exist and any additional characters
224 determined by what is there are inserted.
225 If the symbol starts just after an open-parenthesis, only symbols
226 with function definitions are considered. Otherwise, all symbols with
227 function definitions, values or properties are considered."
228 (interactive)
229 (let* ((end (point))
230 (buffer-syntax (syntax-table))
231 (beg (unwind-protect
232 (save-excursion
233 (set-syntax-table emacs-lisp-mode-syntax-table)
234 (backward-sexp 1)
235 (while (= (char-syntax (following-char)) ?\')
236 (forward-char 1))
237 (point))
238 (set-syntax-table buffer-syntax)))
239 (pattern (buffer-substring beg end))
240 (predicate
241 (if (eq (char-after (1- beg)) ?\()
242 'fboundp
243 (function (lambda (sym)
244 (or (boundp sym) (fboundp sym)
245 (symbol-plist sym))))))
246 (completion (try-completion pattern obarray predicate)))
247 (cond ((eq completion t))
248 ((null completion)
249 (message "Can't find completion for \"%s\"" pattern)
250 (ding))
251 ((not (string= pattern completion))
252 (delete-region beg end)
253 (insert completion))
254 (t
255 (message "Making completion list...")
256 (let ((list (all-completions pattern obarray predicate)))
257 (or (eq predicate 'fboundp)
258 (let (new)
259 (while list
260 (setq new (cons (if (fboundp (intern (car list)))
261 (list (car list) " <f>")
262 (car list))
263 new))
264 (setq list (cdr list)))
265 (setq list (nreverse new))))
266 (with-output-to-temp-buffer " *Completions*"
267 (display-completion-list list)))
268 (message "Making completion list...%s" "done")))))
269
270 ;;; lisp.el ends here