(Fget_buffer_create): Disallow empty string.
[emacs.git] / lisp / emacs-lisp / lisp.el
blobf5904be736481b1a27c3dfe4c16eecde9404d291
1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: lisp, languages
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 ;;; Commentary:
26 ;; Lisp editing commands to go with Lisp major mode.
28 ;;; Code:
30 ;; Note that this variable is used by non-lisp modes too.
31 (defvar defun-prompt-regexp nil
32 "*Non-nil => regexp to ignore, before the character that starts a defun.
33 This is only necessary if the opening paren or brace is not in column 0.
34 See `beginning-of-defun'.")
36 (defvar parens-require-spaces t
37 "Non-nil => `insert-parentheses' should insert whitespace as needed.")
39 (defun forward-sexp (&optional arg)
40 "Move forward across one balanced expression (sexp).
41 With argument, do it that many times. Negative arg -N means
42 move backward across N balanced expressions."
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)))
48 (defun backward-sexp (&optional arg)
49 "Move backward across one balanced expression (sexp).
50 With argument, do it that many times. Negative arg -N means
51 move forward across N balanced expressions."
52 (interactive "p")
53 (or arg (setq arg 1))
54 (forward-sexp (- arg)))
56 (defun mark-sexp (arg)
57 "Set mark ARG sexps from point.
58 The place mark goes is the same place \\[forward-sexp] would
59 move to with the same argument."
60 (interactive "p")
61 (push-mark
62 (save-excursion
63 (forward-sexp arg)
64 (point))
65 nil t))
67 (defun forward-list (&optional arg)
68 "Move forward across one balanced group of parentheses.
69 With argument, do it that many times.
70 Negative 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))))
75 (defun backward-list (&optional arg)
76 "Move backward across one balanced group of parentheses.
77 With argument, do it that many times.
78 Negative arg -N means move forward across N groups of parentheses."
79 (interactive "p")
80 (or arg (setq arg 1))
81 (forward-list (- arg)))
83 (defun down-list (arg)
84 "Move forward down one level of parentheses.
85 With argument, do this that many times.
86 A negative argument means move backward but still go down a level.
87 In 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)))))
94 (defun backward-up-list (arg)
95 "Move backward out of one level of parentheses.
96 With argument, do this that many times.
97 A negative argument means move forward but still to a less deep spot.
98 In Lisp programs, an argument is required."
99 (interactive "p")
100 (up-list (- arg)))
102 (defun up-list (arg)
103 "Move forward out of one level of parentheses.
104 With argument, do this that many times.
105 A negative argument means move backward but still to a less deep spot.
106 In 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)))))
113 (defun kill-sexp (arg)
114 "Kill the sexp (balanced expression) following the cursor.
115 With argument, kill that many sexps after the cursor.
116 Negative 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))))
122 (defun backward-kill-sexp (arg)
123 "Kill the sexp (balanced expression) preceding the cursor.
124 With argument, kill that many sexps before the cursor.
125 Negative arg -N means kill N sexps after the cursor."
126 (interactive "p")
127 (kill-sexp (- arg)))
129 (defun beginning-of-defun (&optional arg)
130 "Move backward to the beginning of a defun.
131 With argument, do it that many times. Negative arg -N
132 means move forward to Nth following beginning of defun.
133 Returns t unless search stops due to beginning or end of buffer.
135 Normally a defun starts when there is an char with open-parenthesis
136 syntax at the beginning of a line. If `defun-prompt-regexp' is
137 non-nil, then a string which matches that regexp may precede the
138 open-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)))
143 (defun beginning-of-defun-raw (&optional arg)
144 "Move point to the character that starts a defun.
145 This is identical to beginning-of-defun, except that point does not move
146 to the beginning of the line when `defun-prompt-regexp' is non-nil."
147 (interactive "p")
148 (and arg (< arg 0) (not (eobp)) (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))
154 (progn (goto-char (1- (match-end 0)))) t))
156 (defun buffer-end (arg)
157 (if (> arg 0) (point-max) (point-min)))
159 (defun end-of-defun (&optional arg)
160 "Move forward to next end of defun. With argument, do it that many times.
161 Negative argument -N means move back to Nth preceding end of defun.
163 An end of a defun occurs right after the close-parenthesis that matches
164 the 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
173 (end-of-line 1)
174 (beginning-of-defun-raw 1)))
176 (or (bobp) (forward-char -1))
177 (beginning-of-defun-raw -1))
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)))
187 (beginning-of-defun-raw 1)
188 (forward-sexp 1)
189 (forward-line 1)
190 (if (>= (point) pos)
191 (if (beginning-of-defun-raw 2)
192 (progn
193 (forward-list 1)
194 (skip-chars-forward " \t")
195 (if (looking-at "\\s<\\|\n")
196 (forward-line 1)))
197 (goto-char (point-min)))))
198 (setq arg (1+ arg)))))
200 (defun mark-defun ()
201 "Put mark at end of this defun, point at beginning.
202 The defun marked is the one that contains point or follows point."
203 (interactive)
204 (push-mark (point))
205 (end-of-defun)
206 (push-mark (point) nil t)
207 (beginning-of-defun)
208 (re-search-backward "^\n" (- (point) 1) t))
210 (defun insert-parentheses (arg)
211 "Put parentheses around next ARG sexps. Leave point after open-paren.
212 No argument is equivalent to zero: just insert `()' and leave point between.
213 If `parens-require-spaces' is non-nil, this command also inserts a space
214 before and after, depending on the surrounding characters."
215 (interactive "P")
216 (if arg (setq arg (prefix-numeric-value arg))
217 (setq arg 0))
218 (or (eq arg 0) (skip-chars-forward " \t"))
219 (and parens-require-spaces
220 (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
221 (insert " "))
222 (insert ?\()
223 (save-excursion
224 (or (eq arg 0) (forward-sexp arg))
225 (insert ?\))
226 (and parens-require-spaces
227 (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
228 (insert " "))))
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))
243 (defun lisp-complete-symbol ()
244 "Perform completion on Lisp symbol preceding point.
245 Compare that symbol against the known Lisp symbols.
247 The context determines which symbols are considered.
248 If the symbol starts just after an open-parenthesis, only symbols
249 with function definitions are considered. Otherwise, all symbols with
250 function definitions, values or properties are considered."
251 (interactive)
252 (let* ((end (point))
253 (buffer-syntax (syntax-table))
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))
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))
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")))))
293 ;;; lisp.el ends here