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