1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1994, 2000 Free Software Foundation, Inc.
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)
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Lisp editing commands to go with Lisp major mode.
31 ;; Note that this variable is used by non-lisp modes too.
32 (defcustom defun-prompt-regexp nil
33 "*Non-nil => regexp to ignore, before the character that starts a defun.
34 This is only necessary if the opening paren or brace is not in column 0.
35 See function `beginning-of-defun'."
36 :type
'(choice (const nil
)
39 (make-variable-buffer-local 'defun-prompt-regexp
)
41 (defcustom parens-require-spaces t
42 "Non-nil => `insert-parentheses' should insert whitespace as needed."
46 (defun forward-sexp (&optional arg
)
47 "Move forward across one balanced expression (sexp).
48 With ARG, do it that many times. Negative arg -N means
49 move backward across N balanced expressions."
52 (goto-char (or (scan-sexps (point) arg
) (buffer-end arg
)))
53 (if (< arg
0) (backward-prefix-chars)))
55 (defun backward-sexp (&optional arg
)
56 "Move backward across one balanced expression (sexp).
57 With ARG, do it that many times. Negative arg -N means
58 move forward across N balanced expressions."
61 (forward-sexp (- arg
)))
63 (defun mark-sexp (arg)
64 "Set mark ARG sexps from point.
65 The place mark goes is the same place \\[forward-sexp] would
66 move to with the same argument."
74 (defun forward-list (&optional arg
)
75 "Move forward across one balanced group of parentheses.
76 With ARG, do it that many times.
77 Negative arg -N means move backward across N groups of parentheses."
80 (goto-char (or (scan-lists (point) arg
0) (buffer-end arg
))))
82 (defun backward-list (&optional arg
)
83 "Move backward across one balanced group of parentheses.
84 With ARG, do it that many times.
85 Negative arg -N means move forward across N groups of parentheses."
88 (forward-list (- arg
)))
90 (defun down-list (arg)
91 "Move forward down one level of parentheses.
92 With ARG, do this that many times.
93 A negative argument means move backward but still go down a level.
94 In Lisp programs, an argument is required."
96 (let ((inc (if (> arg
0) 1 -
1)))
98 (goto-char (or (scan-lists (point) inc -
1) (buffer-end arg
)))
99 (setq arg
(- arg inc
)))))
101 (defun backward-up-list (arg)
102 "Move backward out of one level of parentheses.
103 With ARG, do this that many times.
104 A negative argument means move forward but still to a less deep spot.
105 In Lisp programs, an argument is required."
110 "Move forward out of one level of parentheses.
111 With ARG, do this that many times.
112 A negative argument means move backward but still to a less deep spot.
113 In Lisp programs, an argument is required."
115 (let ((inc (if (> arg
0) 1 -
1)))
117 (goto-char (or (scan-lists (point) inc
1) (buffer-end arg
)))
118 (setq arg
(- arg inc
)))))
120 (defun kill-sexp (arg)
121 "Kill the sexp (balanced expression) following the cursor.
122 With ARG, kill that many sexps after the cursor.
123 Negative arg -N means kill N sexps before the cursor."
125 (let ((opoint (point)))
127 (kill-region opoint
(point))))
129 (defun backward-kill-sexp (arg)
130 "Kill the sexp (balanced expression) preceding the cursor.
131 With ARG, kill that many sexps before the cursor.
132 Negative arg -N means kill N sexps after the cursor."
136 (defvar beginning-of-defun-function nil
137 "If non-nil, function for `beginning-of-defun-raw' to call.
138 This is used to find the beginning of the defun instead of using the
139 normal recipe (see `beginning-of-defun'). Major modes can define this
140 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
143 The function should go to the line on which the current defun starts,
144 and return non-nil, or should return nil if it can't find the beginning.")
146 (defun beginning-of-defun (&optional arg
)
147 "Move backward to the beginning of a defun.
148 With ARG, do it that many times. Negative arg -N
149 means move forward to Nth following beginning of defun.
150 Returns t unless search stops due to beginning or end of buffer.
152 Normally a defun starts when there is an char with open-parenthesis
153 syntax at the beginning of a line. If `defun-prompt-regexp' is
154 non-nil, then a string which matches that regexp may precede the
155 open-parenthesis, and point ends up at the beginning of the line.
157 If variable `beginning-of-defun-function' is non-nil, its value
158 is called as a function to find the defun's beginning."
160 (and (beginning-of-defun-raw arg
)
161 (progn (beginning-of-line) t
)))
163 (defun beginning-of-defun-raw (&optional arg
)
164 "Move point to the character that starts a defun.
165 This is identical to function `beginning-of-defun', except that point
166 does not move to the beginning of the line when `defun-prompt-regexp'
169 If variable `beginning-of-defun-function' is non-nil, its value
170 is called as a function to find the defun's beginning."
172 (if beginning-of-defun-function
173 (funcall beginning-of-defun-function
)
174 (and arg
(< arg
0) (not (eobp)) (forward-char 1))
175 (and (re-search-backward (if defun-prompt-regexp
176 (concat (if open-paren-in-column-0-is-defun-start
178 "\\(" defun-prompt-regexp
"\\)\\s(")
180 nil
'move
(or arg
1))
181 (progn (goto-char (1- (match-end 0)))) t
)))
183 (defvar end-of-defun-function nil
184 "If non-nil, function for function `end-of-defun' to call.
185 This is used to find the end of the defun instead of using the normal
186 recipe (see `end-of-defun'). Major modes can define this if the
187 normal method is not appropriate.")
189 (defun buffer-end (arg)
190 (if (> arg
0) (point-max) (point-min)))
192 (defun end-of-defun (&optional arg
)
193 "Move forward to next end of defun. With argument, do it that many times.
194 Negative argument -N means move back to Nth preceding end of defun.
196 An end of a defun occurs right after the close-parenthesis that
197 matches the open-parenthesis that starts a defun; see function
198 `beginning-of-defun'.
200 If variable `end-of-defun-function' is non-nil, its value
201 is called as a function to find the defun's end."
203 (if end-of-defun-function
204 (funcall end-of-defun-function
)
205 (if (or (null arg
) (= arg
0)) (setq arg
1))
207 (while (and (> arg
0) (< (point) (point-max)))
208 (let ((pos (point)) npos
)
213 (beginning-of-defun-raw 1)))
215 (or (bobp) (forward-char -
1))
216 (beginning-of-defun-raw -
1))
219 (skip-chars-forward " \t")
220 (if (looking-at "\\s<\\|\n")
226 (beginning-of-defun-raw 1)
230 (if (beginning-of-defun-raw 2)
233 (skip-chars-forward " \t")
234 (if (looking-at "\\s<\\|\n")
236 (goto-char (point-min)))))
237 (setq arg
(1+ arg
))))))
240 "Put mark at end of this defun, point at beginning.
241 The defun marked is the one that contains point or follows point."
245 (push-mark (point) nil t
)
247 (re-search-backward "^\n" (- (point) 1) t
))
249 (defun narrow-to-defun (&optional arg
)
250 "Make text outside current defun invisible.
251 The defun visible is the one that contains point or follows point.
252 Optional ARG is ignored."
259 (narrow-to-region (point) end
))))
261 (defun insert-parentheses (arg)
262 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
263 A negative ARG encloses the preceding ARG sexps instead.
264 No argument is equivalent to zero: just insert `()' and leave point between.
265 If `parens-require-spaces' is non-nil, this command also inserts a space
266 before and after, depending on the surrounding characters."
268 (if arg
(setq arg
(prefix-numeric-value arg
))
270 (cond ((> arg
0) (skip-chars-forward " \t"))
271 ((< arg
0) (forward-sexp arg
) (setq arg
(- arg
))))
272 (and parens-require-spaces
274 (memq (char-syntax (preceding-char)) '(?w ?_ ?\
) ))
278 (or (eq arg
0) (forward-sexp arg
))
280 (and parens-require-spaces
282 (memq (char-syntax (following-char)) '(?w ?_ ?\
( ))
285 (defun move-past-close-and-reindent ()
286 "Move past next `)', delete indentation before it, then indent after it."
290 (while (save-excursion ; this is my contribution
291 (let ((before-paren (point)))
292 (back-to-indentation)
293 (and (= (point) before-paren
)
295 ;; Move to end of previous line.
298 ;; Verify it doesn't end within a string or comment.
302 ;; Get state at start of line.
303 (setq state
(list 0 nil nil
304 (null (calculate-lisp-indent))
307 ;; Parse state across the line to get state at end.
308 (setq state
(parse-partial-sexp (point) end nil nil
310 ;; Check not in string or comment.
311 (and (not (elt state
3)) (not (elt state
4))))))))
312 (delete-indentation))
314 (newline-and-indent))
316 (defun check-parens () ; lame name?
317 "Check for unbalanced parentheses in the current buffer.
318 More accurately, check the narrowed part of the buffer for unbalanced
319 expressions (\"sexps\") in general. This is done according to the
320 current syntax table and will find unbalanced brackets or quotes as
321 appropriate. (See Info node `(emacs)Lists and Sexps'.) If imbalance
322 is found, an error is signalled and point is left at the first
323 unbalanced character."
326 ;; Buffer can't have more than (point-max) sexps.
327 (scan-sexps (point-min) (point-max))
328 (scan-error (goto-char (nth 2 data
))
329 ;; Could print (nth 1 data), which is either
330 ;; "Containing expression ends prematurely" or
331 ;; "Unbalanced parentheses", but those may not be so
332 ;; accurate/helpful, e.g. quotes may actually be
334 (error "Unmatched bracket or quote"))
335 (error (cond ((eq 'scan-error
(car data
))
336 (goto-char (nth 2 data
))
337 (error "Unmatched bracket or quote"))
338 (t (signal (car data
) (cdr data
)))))))
340 (defun lisp-complete-symbol ()
341 "Perform completion on Lisp symbol preceding point.
342 Compare that symbol against the known Lisp symbols.
344 The context determines which symbols are considered.
345 If the symbol starts just after an open-parenthesis, only symbols
346 with function definitions are considered. Otherwise, all symbols with
347 function definitions, values or properties are considered."
350 (buffer-syntax (syntax-table))
353 (set-syntax-table emacs-lisp-mode-syntax-table
)
355 (while (= (char-syntax (following-char)) ?
\')
358 (set-syntax-table buffer-syntax
)))
359 (pattern (buffer-substring beg end
))
361 (if (eq (char-after (1- beg
)) ?\
()
363 (function (lambda (sym)
364 (or (boundp sym
) (fboundp sym
)
365 (symbol-plist sym
))))))
366 (completion (try-completion pattern obarray predicate
)))
367 (cond ((eq completion t
))
369 (message "Can't find completion for \"%s\"" pattern
)
371 ((not (string= pattern completion
))
372 (delete-region beg end
)
375 (message "Making completion list...")
376 (let ((list (all-completions pattern obarray predicate
)))
377 (setq list
(sort list
'string
<))
378 (or (eq predicate
'fboundp
)
381 (setq new
(cons (if (fboundp (intern (car list
)))
382 (list (car list
) " <f>")
385 (setq list
(cdr list
)))
386 (setq list
(nreverse new
))))
387 (with-output-to-temp-buffer "*Completions*"
388 (display-completion-list list
)))
389 (message "Making completion list...%s" "done")))))
391 ;;; lisp.el ends here