1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
6 ;; Keyword: 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 (defvar defun-prompt-regexp nil
27 "Non-nil => regexp to ignore, before the `(' that starts a defun.")
29 (defun forward-sexp (&optional arg)
30 "Move forward across one balanced expression (sexp).
31 With argument, do it that many times. Negative arg -N means
32 move backward across N balanced expressions."
35 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
36 (if (< arg 0) (backward-prefix-chars)))
38 (defun backward-sexp (&optional arg)
39 "Move backward across one balanced expression (sexp).
40 With argument, do it that many times. Negative arg -N means
41 move forward across N balanced expressions."
44 (forward-sexp (- arg)))
46 (defun mark-sexp (arg)
47 "Set mark ARG sexps from point.
48 The place mark goes is the same place \\[forward-sexp] would
49 move to with the same argument."
56 (defun forward-list (&optional arg)
57 "Move forward across one balanced group of parentheses.
58 With argument, do it that many times.
59 Negative arg -N means move backward across N groups of parentheses."
62 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
64 (defun backward-list (&optional arg)
65 "Move backward across one balanced group of parentheses.
66 With argument, do it that many times.
67 Negative arg -N means move forward across N groups of parentheses."
70 (forward-list (- arg)))
72 (defun down-list (arg)
73 "Move forward down one level of parentheses.
74 With argument, do this that many times.
75 A negative argument means move backward but still go down a level.
76 In Lisp programs, an argument is required."
78 (let ((inc (if (> arg 0) 1 -1)))
80 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
81 (setq arg (- arg inc)))))
83 (defun backward-up-list (arg)
84 "Move backward out of one level of parentheses.
85 With argument, do this that many times.
86 A negative argument means move forward but still to a less deep spot.
87 In Lisp programs, an argument is required."
92 "Move forward out of one level of parentheses.
93 With argument, do this that many times.
94 A negative argument means move backward but still to a less deep spot.
95 In Lisp programs, an argument is required."
97 (let ((inc (if (> arg 0) 1 -1)))
99 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
100 (setq arg (- arg inc)))))
102 (defun kill-sexp (arg)
103 "Kill the sexp (balanced expression) following the cursor.
104 With argument, kill that many sexps after the cursor.
105 Negative arg -N means kill N sexps before the cursor."
107 (let ((opoint (point)))
109 (kill-region opoint (point))))
111 (defun backward-kill-sexp (arg)
112 "Kill the sexp (balanced expression) preceding the cursor.
113 With argument, kill that many sexps before the cursor.
114 Negative arg -N means kill N sexps after the cursor."
118 (defun beginning-of-defun (&optional arg)
119 "Move backward to the beginning of a defun.
120 With argument, do it that many times. Negative arg -N
121 means move forward to Nth following beginning of defun.
122 Returns t unless search stops due to beginning or end of buffer.
124 Normally a defun starts when there is an char with open-parenthesis
125 syntax at the beginning of a line. If `defun-prompt-regexp' is
126 non-nil, then a string which matches that regexp may precede the
129 (and arg (< arg 0) (forward-char 1))
130 (and (re-search-backward (if defun-prompt-regexp
132 "\\(" defun-prompt-regexp "\\)\\s(")
134 nil 'move (or arg 1))
135 (progn (beginning-of-line) t)))
137 (defun buffer-end (arg)
138 (if (> arg 0) (point-max) (point-min)))
140 (defun end-of-defun (&optional arg)
141 "Move forward to next end of defun. With argument, do it that many times.
142 Negative argument -N means move back to Nth preceding end of defun.
144 An end of a defun occurs right after the close-parenthesis that matches
145 the open-parenthesis that starts a defun; see `beginning-of-defun'."
147 (if (or (null arg) (= arg 0)) (setq arg 1))
149 (while (and (> arg 0) (< (point) (point-max)))
150 (let ((pos (point)) npos)
155 (beginning-of-defun 1)))
157 (or (bobp) (forward-char -1))
158 (beginning-of-defun -1))
161 (skip-chars-forward " \t")
162 (if (looking-at "\\s<\\|\n")
168 (beginning-of-defun 1)
172 (if (beginning-of-defun 2)
175 (skip-chars-forward " \t")
176 (if (looking-at "[;\n]")
178 (goto-char (point-min)))))
179 (setq arg (1+ arg)))))
182 "Put mark at end of this defun, point at beginning.
183 The defun marked is the one that contains point or follows point."
189 (re-search-backward "^\n" (- (point) 1) t))
191 (defun insert-parentheses (arg)
192 "Put parentheses around next ARG sexps. Leave point after open-paren.
193 No argument is equivalent to zero: just insert () and leave point between."
195 (if arg (setq arg (prefix-numeric-value arg))
197 (or (eq arg 0) (skip-chars-forward " \t"))
198 (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
202 (or (eq arg 0) (forward-sexp arg))
204 (and (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
207 (defun move-past-close-and-reindent ()
208 "Move past next `)', delete indentation before it, then indent after it."
212 (while (save-excursion ; this is my contribution
213 (let ((before-paren (point)))
214 (back-to-indentation)
215 (= (point) before-paren)))
216 (delete-indentation))
218 (newline-and-indent))
220 (defun lisp-complete-symbol ()
221 "Perform completion on Lisp symbol preceding point. That symbol is
222 compared against the symbols that exist and any additional characters
223 determined by what is there are inserted.
224 If the symbol starts just after an open-parenthesis, only symbols
225 with function definitions are considered. Otherwise, all symbols with
226 function definitions, values or properties are considered."
229 (buffer-syntax (syntax-table))
232 (set-syntax-table emacs-lisp-mode-syntax-table)
234 (while (= (char-syntax (following-char)) ?\')
237 (set-syntax-table buffer-syntax)))
238 (pattern (buffer-substring beg end))
240 (if (eq (char-after (1- beg)) ?\()
242 (function (lambda (sym)
243 (or (boundp sym) (fboundp sym)
244 (symbol-plist sym))))))
245 (completion (try-completion pattern obarray predicate)))
246 (cond ((eq completion t))
248 (message "Can't find completion for \"%s\"" pattern)
250 ((not (string= pattern completion))
251 (delete-region beg end)
254 (message "Making completion list...")
255 (let ((list (all-completions pattern obarray predicate)))
256 (or (eq predicate 'fboundp)
259 (setq new (cons (if (fboundp (intern (car list)))
260 (list (car list) " <f>")
263 (setq list (cdr list)))
264 (setq list (nreverse new))))
265 (with-output-to-temp-buffer " *Completions*"
266 (display-completion-list list)))
267 (message "Making completion list...%s" "done")))))
269 ;;; lisp.el ends here