Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; lisp.el --- Lisp editing commands for Emacs |
2 | ||
9750e079 ER |
3 | ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
4 | ||
e5167999 | 5 | ;; Maintainer: FSF |
e9571d2a | 6 | ;; Keywords: lisp, languages |
e5167999 | 7 | |
b73b9811 | 8 | ;; This file is part of GNU Emacs. |
9 | ||
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 | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
b73b9811 | 13 | ;; any later version. |
14 | ||
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. | |
19 | ||
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. | |
23 | ||
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; Lisp ediing commands to go with Lisp major mode. | |
27 | ||
e5167999 | 28 | ;;; Code: |
b73b9811 | 29 | |
30 | (defvar defun-prompt-regexp nil | |
31 | "Non-nil => regexp to ignore, before the `(' that starts a defun.") | |
32 | ||
33 | (defun forward-sexp (&optional arg) | |
34 | "Move forward across one balanced expression (sexp). | |
e3f99b91 JB |
35 | With argument, do it that many times. Negative arg -N means |
36 | move backward across N balanced expressions." | |
b73b9811 | 37 | (interactive "p") |
38 | (or arg (setq arg 1)) | |
39 | (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) | |
40 | (if (< arg 0) (backward-prefix-chars))) | |
41 | ||
42 | (defun backward-sexp (&optional arg) | |
43 | "Move backward across one balanced expression (sexp). | |
e3f99b91 JB |
44 | With argument, do it that many times. Negative arg -N means |
45 | move forward across N balanced expressions." | |
b73b9811 | 46 | (interactive "p") |
47 | (or arg (setq arg 1)) | |
48 | (forward-sexp (- arg))) | |
49 | ||
50 | (defun mark-sexp (arg) | |
51 | "Set mark ARG sexps from point. | |
e3f99b91 JB |
52 | The place mark goes is the same place \\[forward-sexp] would |
53 | move to with the same argument." | |
b73b9811 | 54 | (interactive "p") |
55 | (push-mark | |
56 | (save-excursion | |
57 | (forward-sexp arg) | |
58 | (point)))) | |
59 | ||
60 | (defun forward-list (&optional arg) | |
61 | "Move forward across one balanced group of parentheses. | |
62 | With argument, do it that many times. | |
63 | Negative arg -N means move backward across N groups of parentheses." | |
64 | (interactive "p") | |
65 | (or arg (setq arg 1)) | |
66 | (goto-char (or (scan-lists (point) arg 0) (buffer-end arg)))) | |
67 | ||
68 | (defun backward-list (&optional arg) | |
69 | "Move backward across one balanced group of parentheses. | |
70 | With argument, do it that many times. | |
71 | Negative arg -N means move forward across N groups of parentheses." | |
72 | (interactive "p") | |
73 | (or arg (setq arg 1)) | |
74 | (forward-list (- arg))) | |
75 | ||
76 | (defun down-list (arg) | |
77 | "Move forward down one level of parentheses. | |
78 | With argument, do this that many times. | |
79 | A negative argument means move backward but still go down a level. | |
80 | In Lisp programs, an argument is required." | |
81 | (interactive "p") | |
82 | (let ((inc (if (> arg 0) 1 -1))) | |
83 | (while (/= arg 0) | |
84 | (goto-char (or (scan-lists (point) inc -1) (buffer-end arg))) | |
85 | (setq arg (- arg inc))))) | |
86 | ||
87 | (defun backward-up-list (arg) | |
88 | "Move backward out of one level of parentheses. | |
89 | With argument, do this that many times. | |
90 | A negative argument means move forward but still to a less deep spot. | |
91 | In Lisp programs, an argument is required." | |
92 | (interactive "p") | |
93 | (up-list (- arg))) | |
94 | ||
95 | (defun up-list (arg) | |
96 | "Move forward out of one level of parentheses. | |
97 | With argument, do this that many times. | |
98 | A negative argument means move backward but still to a less deep spot. | |
99 | In Lisp programs, an argument is required." | |
100 | (interactive "p") | |
101 | (let ((inc (if (> arg 0) 1 -1))) | |
102 | (while (/= arg 0) | |
103 | (goto-char (or (scan-lists (point) inc 1) (buffer-end arg))) | |
104 | (setq arg (- arg inc))))) | |
105 | ||
106 | (defun kill-sexp (arg) | |
107 | "Kill the sexp (balanced expression) following the cursor. | |
108 | With argument, kill that many sexps after the cursor. | |
109 | Negative arg -N means kill N sexps before the cursor." | |
110 | (interactive "p") | |
111 | (let ((opoint (point))) | |
112 | (forward-sexp arg) | |
113 | (kill-region opoint (point)))) | |
114 | ||
115 | (defun backward-kill-sexp (arg) | |
116 | "Kill the sexp (balanced expression) preceding the cursor. | |
117 | With argument, kill that many sexps before the cursor. | |
118 | Negative arg -N means kill N sexps after the cursor." | |
119 | (interactive "p") | |
120 | (kill-sexp (- arg))) | |
121 | \f | |
122 | (defun beginning-of-defun (&optional arg) | |
123 | "Move backward to the beginning of a defun. | |
124 | With argument, do it that many times. Negative arg -N | |
125 | means move forward to Nth following beginning of defun. | |
126 | Returns t unless search stops due to beginning or end of buffer. | |
127 | ||
128 | Normally a defun starts when there is an char with open-parenthesis | |
129 | syntax at the beginning of a line. If `defun-prompt-regexp' is | |
130 | non-nil, then a string which matches that regexp may precede the | |
131 | open-parenthesis." | |
132 | (interactive "p") | |
133 | (and arg (< arg 0) (forward-char 1)) | |
134 | (and (re-search-backward (if defun-prompt-regexp | |
135 | (concat "^\\s(\\|" | |
136 | "\\(" defun-prompt-regexp "\\)\\s(") | |
137 | "^\\s(") | |
138 | nil 'move (or arg 1)) | |
139 | (progn (beginning-of-line) t))) | |
140 | ||
141 | (defun buffer-end (arg) | |
142 | (if (> arg 0) (point-max) (point-min))) | |
143 | ||
144 | (defun end-of-defun (&optional arg) | |
145 | "Move forward to next end of defun. With argument, do it that many times. | |
146 | Negative argument -N means move back to Nth preceding end of defun. | |
147 | ||
148 | An end of a defun occurs right after the close-parenthesis that matches | |
149 | the open-parenthesis that starts a defun; see `beginning-of-defun'." | |
150 | (interactive "p") | |
151 | (if (or (null arg) (= arg 0)) (setq arg 1)) | |
152 | (let ((first t)) | |
153 | (while (and (> arg 0) (< (point) (point-max))) | |
154 | (let ((pos (point)) npos) | |
155 | (while (progn | |
156 | (if (and first | |
157 | (progn | |
158 | (forward-char 1) | |
159 | (beginning-of-defun 1))) | |
160 | nil | |
161 | (or (bobp) (forward-char -1)) | |
162 | (beginning-of-defun -1)) | |
163 | (setq first nil) | |
164 | (forward-list 1) | |
165 | (skip-chars-forward " \t") | |
166 | (if (looking-at "\\s<\\|\n") | |
167 | (forward-line 1)) | |
168 | (<= (point) pos)))) | |
169 | (setq arg (1- arg))) | |
170 | (while (< arg 0) | |
171 | (let ((pos (point))) | |
172 | (beginning-of-defun 1) | |
173 | (forward-sexp 1) | |
174 | (forward-line 1) | |
175 | (if (>= (point) pos) | |
176 | (if (beginning-of-defun 2) | |
177 | (progn | |
178 | (forward-list 1) | |
179 | (skip-chars-forward " \t") | |
180 | (if (looking-at "[;\n]") | |
181 | (forward-line 1))) | |
182 | (goto-char (point-min))))) | |
183 | (setq arg (1+ arg))))) | |
184 | ||
185 | (defun mark-defun () | |
186 | "Put mark at end of this defun, point at beginning. | |
187 | The defun marked is the one that contains point or follows point." | |
188 | (interactive) | |
189 | (push-mark (point)) | |
190 | (end-of-defun) | |
191 | (push-mark (point)) | |
192 | (beginning-of-defun) | |
193 | (re-search-backward "^\n" (- (point) 1) t)) | |
194 | ||
195 | (defun insert-parentheses (arg) | |
196 | "Put parentheses around next ARG sexps. Leave point after open-paren. | |
197 | No argument is equivalent to zero: just insert () and leave point between." | |
198 | (interactive "P") | |
a17f9e55 RS |
199 | (if arg (setq arg (prefix-numeric-value arg)) |
200 | (setq arg 0)) | |
201 | (or (eq arg 0) (skip-chars-forward " \t")) | |
b73b9811 | 202 | (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\) )) |
203 | (insert " ")) | |
b73b9811 | 204 | (insert ?\() |
205 | (save-excursion | |
a17f9e55 | 206 | (or (eq arg 0) (forward-sexp arg)) |
b73b9811 | 207 | (insert ?\)) |
a17f9e55 RS |
208 | (and (memq (char-syntax (following-char)) '(?w ?_ ?\( )) |
209 | (insert " ")))) | |
b73b9811 | 210 | |
211 | (defun move-past-close-and-reindent () | |
212 | "Move past next `)', delete indentation before it, then indent after it." | |
213 | (interactive) | |
214 | (up-list 1) | |
215 | (forward-char -1) | |
216 | (while (save-excursion ; this is my contribution | |
217 | (let ((before-paren (point))) | |
218 | (back-to-indentation) | |
219 | (= (point) before-paren))) | |
220 | (delete-indentation)) | |
221 | (forward-char 1) | |
222 | (newline-and-indent)) | |
223 | \f | |
224 | (defun lisp-complete-symbol () | |
e3f99b91 JB |
225 | "Perform completion on Lisp symbol preceding point. That symbol is |
226 | compared against the symbols that exist and any additional characters | |
227 | determined by what is there are inserted. | |
228 | If the symbol starts just after an open-parenthesis, only symbols | |
229 | with function definitions are considered. Otherwise, all symbols with | |
230 | function definitions, values or properties are considered." | |
b73b9811 | 231 | (interactive) |
232 | (let* ((end (point)) | |
233 | (buffer-syntax (syntax-table)) | |
ecb75942 RS |
234 | (beg (unwind-protect |
235 | (save-excursion | |
236 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
237 | (backward-sexp 1) | |
238 | (while (= (char-syntax (following-char)) ?\') | |
239 | (forward-char 1)) | |
240 | (point)) | |
b73b9811 | 241 | (set-syntax-table buffer-syntax))) |
242 | (pattern (buffer-substring beg end)) | |
243 | (predicate | |
244 | (if (eq (char-after (1- beg)) ?\() | |
245 | 'fboundp | |
246 | (function (lambda (sym) | |
247 | (or (boundp sym) (fboundp sym) | |
248 | (symbol-plist sym)))))) | |
249 | (completion (try-completion pattern obarray predicate))) | |
250 | (cond ((eq completion t)) | |
251 | ((null completion) | |
252 | (message "Can't find completion for \"%s\"" pattern) | |
253 | (ding)) | |
254 | ((not (string= pattern completion)) | |
255 | (delete-region beg end) | |
256 | (insert completion)) | |
257 | (t | |
258 | (message "Making completion list...") | |
259 | (let ((list (all-completions pattern obarray predicate))) | |
260 | (or (eq predicate 'fboundp) | |
261 | (let (new) | |
262 | (while list | |
263 | (setq new (cons (if (fboundp (intern (car list))) | |
264 | (list (car list) " <f>") | |
265 | (car list)) | |
266 | new)) | |
267 | (setq list (cdr list))) | |
268 | (setq list (nreverse new)))) | |
269 | (with-output-to-temp-buffer " *Completions*" | |
270 | (display-completion-list list))) | |
271 | (message "Making completion list...%s" "done"))))) | |
6594deb0 ER |
272 | |
273 | ;;; lisp.el ends here |