1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 1986 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Lisp ediing commands to go with Lisp major mode.
30 (defvar defun-prompt-regexp nil
31 "Non-nil => regexp to ignore, before the `(' that starts a defun.")
33 (defun forward-sexp (&optional arg)
34 "Move forward across one balanced expression (sexp).
35 With argument, do it that many times. Negative arg -N means
36 move backward across N balanced expressions."
39 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
40 (if (< arg 0) (backward-prefix-chars)))
42 (defun backward-sexp (&optional arg)
43 "Move backward across one balanced expression (sexp).
44 With argument, do it that many times. Negative arg -N means
45 move forward across N balanced expressions."
48 (forward-sexp (- arg)))
50 (defun mark-sexp (arg)
51 "Set mark ARG sexps from point.
52 The place mark goes is the same place \\[forward-sexp] would
53 move to with the same argument."
61 (defun forward-list (&optional arg)
62 "Move forward across one balanced group of parentheses.
63 With argument, do it that many times.
64 Negative arg -N means move backward across N groups of parentheses."
67 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
69 (defun backward-list (&optional arg)
70 "Move backward across one balanced group of parentheses.
71 With argument, do it that many times.
72 Negative arg -N means move forward across N groups of parentheses."
75 (forward-list (- arg)))
77 (defun down-list (arg)
78 "Move forward down one level of parentheses.
79 With argument, do this that many times.
80 A negative argument means move backward but still go down a level.
81 In Lisp programs, an argument is required."
83 (let ((inc (if (> arg 0) 1 -1)))
85 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
86 (setq arg (- arg inc)))))
88 (defun backward-up-list (arg)
89 "Move backward out of one level of parentheses.
90 With argument, do this that many times.
91 A negative argument means move forward but still to a less deep spot.
92 In Lisp programs, an argument is required."
97 "Move forward out of one level of parentheses.
98 With argument, do this that many times.
99 A negative argument means move backward but still to a less deep spot.
100 In Lisp programs, an argument is required."
102 (let ((inc (if (> arg 0) 1 -1)))
104 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
105 (setq arg (- arg inc)))))
107 (defun kill-sexp (arg)
108 "Kill the sexp (balanced expression) following the cursor.
109 With argument, kill that many sexps after the cursor.
110 Negative arg -N means kill N sexps before the cursor."
112 (let ((opoint (point)))
114 (kill-region opoint (point))))
116 (defun backward-kill-sexp (arg)
117 "Kill the sexp (balanced expression) preceding the cursor.
118 With argument, kill that many sexps before the cursor.
119 Negative arg -N means kill N sexps after the cursor."
123 (defun beginning-of-defun (&optional arg)
124 "Move backward to the beginning of a defun.
125 With argument, do it that many times. Negative arg -N
126 means move forward to Nth following beginning of defun.
127 Returns t unless search stops due to beginning or end of buffer.
129 Normally a defun starts when there is an char with open-parenthesis
130 syntax at the beginning of a line. If `defun-prompt-regexp' is
131 non-nil, then a string which matches that regexp may precede the
134 (and arg (< arg 0) (forward-char 1))
135 (and (re-search-backward (if defun-prompt-regexp
137 "\\(" defun-prompt-regexp "\\)\\s(")
139 nil 'move (or arg 1))
140 (progn (beginning-of-line) t)))
142 (defun buffer-end (arg)
143 (if (> arg 0) (point-max) (point-min)))
145 (defun end-of-defun (&optional arg)
146 "Move forward to next end of defun. With argument, do it that many times.
147 Negative argument -N means move back to Nth preceding end of defun.
149 An end of a defun occurs right after the close-parenthesis that matches
150 the open-parenthesis that starts a defun; see `beginning-of-defun'."
152 (if (or (null arg) (= arg 0)) (setq arg 1))
154 (while (and (> arg 0) (< (point) (point-max)))
155 (let ((pos (point)) npos)
160 (beginning-of-defun 1)))
162 (or (bobp) (forward-char -1))
163 (beginning-of-defun -1))
166 (skip-chars-forward " \t")
167 (if (looking-at "\\s<\\|\n")
173 (beginning-of-defun 1)
177 (if (beginning-of-defun 2)
180 (skip-chars-forward " \t")
181 (if (looking-at "[;\n]")
183 (goto-char (point-min)))))
184 (setq arg (1+ arg)))))
187 "Put mark at end of this defun, point at beginning.
188 The defun marked is the one that contains point or follows point."
192 (push-mark (point) nil t)
194 (re-search-backward "^\n" (- (point) 1) t))
196 (defun insert-parentheses (arg)
197 "Put parentheses around next ARG sexps. Leave point after open-paren.
198 No argument is equivalent to zero: just insert () and leave point between."
200 (if arg (setq arg (prefix-numeric-value arg))
202 (or (eq arg 0) (skip-chars-forward " \t"))
203 (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
207 (or (eq arg 0) (forward-sexp arg))
209 (and (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
212 (defun move-past-close-and-reindent ()
213 "Move past next `)', delete indentation before it, then indent after it."
217 (while (save-excursion ; this is my contribution
218 (let ((before-paren (point)))
219 (back-to-indentation)
220 (= (point) before-paren)))
221 (delete-indentation))
223 (newline-and-indent))
225 (defun lisp-complete-symbol ()
226 "Perform completion on Lisp symbol preceding point. That symbol is
227 compared against the symbols that exist and any additional characters
228 determined by what is there are inserted.
229 If the symbol starts just after an open-parenthesis, only symbols
230 with function definitions are considered. Otherwise, all symbols with
231 function definitions, values or properties are considered."
234 (buffer-syntax (syntax-table))
237 (set-syntax-table emacs-lisp-mode-syntax-table)
239 (while (= (char-syntax (following-char)) ?\')
242 (set-syntax-table buffer-syntax)))
243 (pattern (buffer-substring beg end))
245 (if (eq (char-after (1- beg)) ?\()
247 (function (lambda (sym)
248 (or (boundp sym) (fboundp sym)
249 (symbol-plist sym))))))
250 (completion (try-completion pattern obarray predicate)))
251 (cond ((eq completion t))
253 (message "Can't find completion for \"%s\"" pattern)
255 ((not (string= pattern completion))
256 (delete-region beg end)
259 (message "Making completion list...")
260 (let ((list (all-completions pattern obarray predicate)))
261 (or (eq predicate 'fboundp)
264 (setq new (cons (if (fboundp (intern (car list)))
265 (list (car list) " <f>")
268 (setq list (cdr list)))
269 (setq list (nreverse new))))
270 (with-output-to-temp-buffer " *Completions*"
271 (display-completion-list list)))
272 (message "Making completion list...%s" "done")))))
274 ;;; lisp.el ends here