(help-with-tutorial): Avoid unexplained large blank area.
[bpt/emacs.git] / lisp / emacs-lisp / lisp.el
CommitLineData
6594deb0
ER
1;;; lisp.el --- Lisp editing commands for Emacs
2
9750e079
ER
3;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4
e5167999 5;; Maintainer: FSF
e9571d2a 6;; Keywords: lisp, languages
e5167999 7
b73b9811 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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
b73b9811 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
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
e41b2db1
ER
24;;; Commentary:
25
eb8c3be9 26;; Lisp editing commands to go with Lisp major mode.
e41b2db1 27
e5167999 28;;; Code:
b73b9811 29
7fe78b07 30;; Note that this variable is used by non-lisp modes too.
b73b9811 31(defvar defun-prompt-regexp nil
7fe78b07
KH
32 "*Non-nil => regexp to ignore, before the character that starts a defun.
33This is only necessary if the opening paren or brace is not in column 0.
34See `beginning-of-defun'.")
b73b9811 35
78e367e9
RS
36(defvar parens-require-spaces t
37 "Non-nil => `insert-parentheses' should insert whitespace as needed.")
44a53673 38
b73b9811 39(defun forward-sexp (&optional arg)
40 "Move forward across one balanced expression (sexp).
e3f99b91
JB
41With argument, do it that many times. Negative arg -N means
42move backward across N balanced expressions."
b73b9811 43 (interactive "p")
44 (or arg (setq arg 1))
45 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
46 (if (< arg 0) (backward-prefix-chars)))
47
48(defun backward-sexp (&optional arg)
49 "Move backward across one balanced expression (sexp).
e3f99b91
JB
50With argument, do it that many times. Negative arg -N means
51move forward across N balanced expressions."
b73b9811 52 (interactive "p")
53 (or arg (setq arg 1))
54 (forward-sexp (- arg)))
55
56(defun mark-sexp (arg)
57 "Set mark ARG sexps from point.
e3f99b91
JB
58The place mark goes is the same place \\[forward-sexp] would
59move to with the same argument."
b73b9811 60 (interactive "p")
61 (push-mark
62 (save-excursion
63 (forward-sexp arg)
0ba91115
RS
64 (point))
65 nil t))
b73b9811 66
67(defun forward-list (&optional arg)
68 "Move forward across one balanced group of parentheses.
69With argument, do it that many times.
70Negative arg -N means move backward across N groups of parentheses."
71 (interactive "p")
72 (or arg (setq arg 1))
73 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
74
75(defun backward-list (&optional arg)
76 "Move backward across one balanced group of parentheses.
77With argument, do it that many times.
78Negative arg -N means move forward across N groups of parentheses."
79 (interactive "p")
80 (or arg (setq arg 1))
81 (forward-list (- arg)))
82
83(defun down-list (arg)
84 "Move forward down one level of parentheses.
85With argument, do this that many times.
86A negative argument means move backward but still go down a level.
87In Lisp programs, an argument is required."
88 (interactive "p")
89 (let ((inc (if (> arg 0) 1 -1)))
90 (while (/= arg 0)
91 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
92 (setq arg (- arg inc)))))
93
94(defun backward-up-list (arg)
95 "Move backward out of one level of parentheses.
96With argument, do this that many times.
97A negative argument means move forward but still to a less deep spot.
98In Lisp programs, an argument is required."
99 (interactive "p")
100 (up-list (- arg)))
101
102(defun up-list (arg)
103 "Move forward out of one level of parentheses.
104With argument, do this that many times.
105A negative argument means move backward but still to a less deep spot.
106In Lisp programs, an argument is required."
107 (interactive "p")
108 (let ((inc (if (> arg 0) 1 -1)))
109 (while (/= arg 0)
110 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
111 (setq arg (- arg inc)))))
112
113(defun kill-sexp (arg)
114 "Kill the sexp (balanced expression) following the cursor.
115With argument, kill that many sexps after the cursor.
116Negative arg -N means kill N sexps before the cursor."
117 (interactive "p")
118 (let ((opoint (point)))
119 (forward-sexp arg)
120 (kill-region opoint (point))))
121
122(defun backward-kill-sexp (arg)
123 "Kill the sexp (balanced expression) preceding the cursor.
124With argument, kill that many sexps before the cursor.
125Negative arg -N means kill N sexps after the cursor."
126 (interactive "p")
127 (kill-sexp (- arg)))
128\f
129(defun beginning-of-defun (&optional arg)
130 "Move backward to the beginning of a defun.
131With argument, do it that many times. Negative arg -N
132means move forward to Nth following beginning of defun.
133Returns t unless search stops due to beginning or end of buffer.
134
135Normally a defun starts when there is an char with open-parenthesis
136syntax at the beginning of a line. If `defun-prompt-regexp' is
137non-nil, then a string which matches that regexp may precede the
afa995e1
KH
138open-parenthesis, and point ends up at the beginning of the line."
139 (interactive "p")
140 (and (beginning-of-defun-raw arg)
141 (progn (beginning-of-line) t)))
142
143(defun beginning-of-defun-raw (&optional arg)
144 "Move point to the character that starts a defun.
145This is identical to beginning-of-defun, except that point does not move
146to the beginning of the line when `defun-prompt-regexp' is non-nil."
b73b9811 147 (interactive "p")
148 (and arg (< arg 0) (forward-char 1))
149 (and (re-search-backward (if defun-prompt-regexp
150 (concat "^\\s(\\|"
151 "\\(" defun-prompt-regexp "\\)\\s(")
152 "^\\s(")
153 nil 'move (or arg 1))
afa995e1 154 (progn (goto-char (1- (match-end 0)))) t))
b73b9811 155
156(defun buffer-end (arg)
157 (if (> arg 0) (point-max) (point-min)))
158
159(defun end-of-defun (&optional arg)
160 "Move forward to next end of defun. With argument, do it that many times.
161Negative argument -N means move back to Nth preceding end of defun.
162
163An end of a defun occurs right after the close-parenthesis that matches
164the open-parenthesis that starts a defun; see `beginning-of-defun'."
165 (interactive "p")
166 (if (or (null arg) (= arg 0)) (setq arg 1))
167 (let ((first t))
168 (while (and (> arg 0) (< (point) (point-max)))
169 (let ((pos (point)) npos)
170 (while (progn
171 (if (and first
172 (progn
afa995e1
KH
173 (end-of-line 1)
174 (beginning-of-defun-raw 1)))
b73b9811 175 nil
176 (or (bobp) (forward-char -1))
afa995e1 177 (beginning-of-defun-raw -1))
b73b9811 178 (setq first nil)
179 (forward-list 1)
180 (skip-chars-forward " \t")
181 (if (looking-at "\\s<\\|\n")
182 (forward-line 1))
183 (<= (point) pos))))
184 (setq arg (1- arg)))
185 (while (< arg 0)
186 (let ((pos (point)))
afa995e1 187 (beginning-of-defun-raw 1)
b73b9811 188 (forward-sexp 1)
189 (forward-line 1)
190 (if (>= (point) pos)
afa995e1 191 (if (beginning-of-defun-raw 2)
b73b9811 192 (progn
193 (forward-list 1)
194 (skip-chars-forward " \t")
37f31acf 195 (if (looking-at "\\s<\\|\n")
b73b9811 196 (forward-line 1)))
197 (goto-char (point-min)))))
198 (setq arg (1+ arg)))))
199
200(defun mark-defun ()
201 "Put mark at end of this defun, point at beginning.
202The defun marked is the one that contains point or follows point."
203 (interactive)
204 (push-mark (point))
205 (end-of-defun)
0ba91115 206 (push-mark (point) nil t)
b73b9811 207 (beginning-of-defun)
208 (re-search-backward "^\n" (- (point) 1) t))
209
210(defun insert-parentheses (arg)
211 "Put parentheses around next ARG sexps. Leave point after open-paren.
44a53673 212No argument is equivalent to zero: just insert `()' and leave point between.
d5bfe076
KH
213If `parens-require-spaces' is non-nil, this command also inserts a space
214before and after, depending on the surrounding characters."
b73b9811 215 (interactive "P")
a17f9e55
RS
216 (if arg (setq arg (prefix-numeric-value arg))
217 (setq arg 0))
218 (or (eq arg 0) (skip-chars-forward " \t"))
78e367e9 219 (and parens-require-spaces
44a53673 220 (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
b73b9811 221 (insert " "))
b73b9811 222 (insert ?\()
223 (save-excursion
a17f9e55 224 (or (eq arg 0) (forward-sexp arg))
b73b9811 225 (insert ?\))
78e367e9 226 (and parens-require-spaces
44a53673 227 (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
a17f9e55 228 (insert " "))))
b73b9811 229
230(defun move-past-close-and-reindent ()
231 "Move past next `)', delete indentation before it, then indent after it."
232 (interactive)
233 (up-list 1)
234 (forward-char -1)
235 (while (save-excursion ; this is my contribution
236 (let ((before-paren (point)))
237 (back-to-indentation)
238 (= (point) before-paren)))
239 (delete-indentation))
240 (forward-char 1)
241 (newline-and-indent))
242\f
243(defun lisp-complete-symbol ()
2eb9adab
RS
244 "Perform completion on Lisp symbol preceding point.
245Compare that symbol against the known Lisp symbols.
246
247The context determines which symbols are considered.
248If the symbol starts just after an open-parenthesis, only symbols
e3f99b91
JB
249with function definitions are considered. Otherwise, all symbols with
250function definitions, values or properties are considered."
b73b9811 251 (interactive)
252 (let* ((end (point))
253 (buffer-syntax (syntax-table))
ecb75942
RS
254 (beg (unwind-protect
255 (save-excursion
256 (set-syntax-table emacs-lisp-mode-syntax-table)
257 (backward-sexp 1)
258 (while (= (char-syntax (following-char)) ?\')
259 (forward-char 1))
260 (point))
b73b9811 261 (set-syntax-table buffer-syntax)))
262 (pattern (buffer-substring beg end))
263 (predicate
264 (if (eq (char-after (1- beg)) ?\()
265 'fboundp
266 (function (lambda (sym)
267 (or (boundp sym) (fboundp sym)
268 (symbol-plist sym))))))
269 (completion (try-completion pattern obarray predicate)))
270 (cond ((eq completion t))
271 ((null completion)
272 (message "Can't find completion for \"%s\"" pattern)
273 (ding))
274 ((not (string= pattern completion))
275 (delete-region beg end)
276 (insert completion))
277 (t
278 (message "Making completion list...")
279 (let ((list (all-completions pattern obarray predicate)))
280 (or (eq predicate 'fboundp)
281 (let (new)
282 (while list
283 (setq new (cons (if (fboundp (intern (car list)))
284 (list (car list) " <f>")
285 (car list))
286 new))
287 (setq list (cdr list)))
288 (setq list (nreverse new))))
289 (with-output-to-temp-buffer " *Completions*"
290 (display-completion-list list)))
291 (message "Making completion list...%s" "done")))))
6594deb0
ER
292
293;;; lisp.el ends here