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