1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 (defvar defun-prompt-regexp nil
23 "Non-nil => regexp to ignore, before the `(' that starts a defun.")
25 (defun forward-sexp (&optional arg
)
26 "Move forward across one balanced expression (sexp).
27 With argument, do it that many times. Negative arg -N means
28 move backward across N balanced expressions."
31 (goto-char (or (scan-sexps (point) arg
) (buffer-end arg
)))
32 (if (< arg
0) (backward-prefix-chars)))
34 (defun backward-sexp (&optional arg
)
35 "Move backward across one balanced expression (sexp).
36 With argument, do it that many times. Negative arg -N means
37 move forward across N balanced expressions."
40 (forward-sexp (- arg
)))
42 (defun mark-sexp (arg)
43 "Set mark ARG sexps from point.
44 The place mark goes is the same place \\[forward-sexp] would
45 move to with the same argument."
52 (defun forward-list (&optional arg
)
53 "Move forward across one balanced group of parentheses.
54 With argument, do it that many times.
55 Negative arg -N means move backward across N groups of parentheses."
58 (goto-char (or (scan-lists (point) arg
0) (buffer-end arg
))))
60 (defun backward-list (&optional arg
)
61 "Move backward across one balanced group of parentheses.
62 With argument, do it that many times.
63 Negative arg -N means move forward across N groups of parentheses."
66 (forward-list (- arg
)))
68 (defun down-list (arg)
69 "Move forward down one level of parentheses.
70 With argument, do this that many times.
71 A negative argument means move backward but still go down a level.
72 In Lisp programs, an argument is required."
74 (let ((inc (if (> arg
0) 1 -
1)))
76 (goto-char (or (scan-lists (point) inc -
1) (buffer-end arg
)))
77 (setq arg
(- arg inc
)))))
79 (defun backward-up-list (arg)
80 "Move backward out of one level of parentheses.
81 With argument, do this that many times.
82 A negative argument means move forward but still to a less deep spot.
83 In Lisp programs, an argument is required."
88 "Move forward out of one level of parentheses.
89 With argument, do this that many times.
90 A negative argument means move backward but still to a less deep spot.
91 In Lisp programs, an argument is required."
93 (let ((inc (if (> arg
0) 1 -
1)))
95 (goto-char (or (scan-lists (point) inc
1) (buffer-end arg
)))
96 (setq arg
(- arg inc
)))))
98 (defun kill-sexp (arg)
99 "Kill the sexp (balanced expression) following the cursor.
100 With argument, kill that many sexps after the cursor.
101 Negative arg -N means kill N sexps before the cursor."
103 (let ((opoint (point)))
105 (kill-region opoint
(point))))
107 (defun backward-kill-sexp (arg)
108 "Kill the sexp (balanced expression) preceding the cursor.
109 With argument, kill that many sexps before the cursor.
110 Negative arg -N means kill N sexps after the cursor."
114 (defun beginning-of-defun (&optional arg
)
115 "Move backward to the beginning of a defun.
116 With argument, do it that many times. Negative arg -N
117 means move forward to Nth following beginning of defun.
118 Returns t unless search stops due to beginning or end of buffer.
120 Normally a defun starts when there is an char with open-parenthesis
121 syntax at the beginning of a line. If `defun-prompt-regexp' is
122 non-nil, then a string which matches that regexp may precede the
125 (and arg
(< arg
0) (forward-char 1))
126 (and (re-search-backward (if defun-prompt-regexp
128 "\\(" defun-prompt-regexp
"\\)\\s(")
130 nil
'move
(or arg
1))
131 (progn (beginning-of-line) t
)))
133 (defun buffer-end (arg)
134 (if (> arg
0) (point-max) (point-min)))
136 (defun end-of-defun (&optional arg
)
137 "Move forward to next end of defun. With argument, do it that many times.
138 Negative argument -N means move back to Nth preceding end of defun.
140 An end of a defun occurs right after the close-parenthesis that matches
141 the open-parenthesis that starts a defun; see `beginning-of-defun'."
143 (if (or (null arg
) (= arg
0)) (setq arg
1))
145 (while (and (> arg
0) (< (point) (point-max)))
146 (let ((pos (point)) npos
)
151 (beginning-of-defun 1)))
153 (or (bobp) (forward-char -
1))
154 (beginning-of-defun -1))
157 (skip-chars-forward " \t")
158 (if (looking-at "\\s<\\|\n")
164 (beginning-of-defun 1)
168 (if (beginning-of-defun 2)
171 (skip-chars-forward " \t")
172 (if (looking-at "[;\n]")
174 (goto-char (point-min)))))
175 (setq arg
(1+ arg
)))))
178 "Put mark at end of this defun, point at beginning.
179 The defun marked is the one that contains point or follows point."
185 (re-search-backward "^\n" (- (point) 1) t
))
187 (defun insert-parentheses (arg)
188 "Put parentheses around next ARG sexps. Leave point after open-paren.
189 No argument is equivalent to zero: just insert () and leave point between."
191 (if arg
(setq arg
(prefix-numeric-value arg
))
193 (or (eq arg
0) (skip-chars-forward " \t"))
194 (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\
) ))
198 (or (eq arg
0) (forward-sexp arg
))
200 (and (memq (char-syntax (following-char)) '(?w ?_ ?\
( ))
203 (defun move-past-close-and-reindent ()
204 "Move past next `)', delete indentation before it, then indent after it."
208 (while (save-excursion ; this is my contribution
209 (let ((before-paren (point)))
210 (back-to-indentation)
211 (= (point) before-paren
)))
212 (delete-indentation))
214 (newline-and-indent))
216 (defun lisp-complete-symbol ()
217 "Perform completion on Lisp symbol preceding point. That symbol is
218 compared against the symbols that exist and any additional characters
219 determined by what is there are inserted.
220 If the symbol starts just after an open-parenthesis, only symbols
221 with function definitions are considered. Otherwise, all symbols with
222 function definitions, values or properties are considered."
225 (buffer-syntax (syntax-table))
228 (set-syntax-table emacs-lisp-mode-syntax-table
)
230 (while (= (char-syntax (following-char)) ?
\')
233 (set-syntax-table buffer-syntax
)))
234 (pattern (buffer-substring beg end
))
236 (if (eq (char-after (1- beg
)) ?\
()
238 (function (lambda (sym)
239 (or (boundp sym
) (fboundp sym
)
240 (symbol-plist sym
))))))
241 (completion (try-completion pattern obarray predicate
)))
242 (cond ((eq completion t
))
244 (message "Can't find completion for \"%s\"" pattern
)
246 ((not (string= pattern completion
))
247 (delete-region beg end
)
250 (message "Making completion list...")
251 (let ((list (all-completions pattern obarray predicate
)))
252 (or (eq predicate
'fboundp
)
255 (setq new
(cons (if (fboundp (intern (car list
)))
256 (list (car list
) " <f>")
259 (setq list
(cdr list
)))
260 (setq list
(nreverse new
))))
261 (with-output-to-temp-buffer " *Completions*"
262 (display-completion-list list
)))
263 (message "Making completion list...%s" "done")))))
265 ;;; lisp.el ends here