Mercurial > emacs
annotate lisp/emacs-lisp/lisp.el @ 6397:70bf65b6aae9
(beginning-of-defun-raw): New function.
(end-of-defun): Handle defun-prompt-regexp correctly.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Fri, 18 Mar 1994 02:21:52 +0000 |
parents | 1740ec160ff8 |
children | 09fadfc6384e |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
215
diff
changeset
|
1 ;;; lisp.el --- Lisp editing commands for Emacs |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
215
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
5 ;; Maintainer: FSF |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
6 ;; Keywords: lisp, languages |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
7 |
84 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
84 | 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 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
24 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
25 |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2823
diff
changeset
|
26 ;; Lisp editing commands to go with Lisp major mode. |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
27 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
28 ;;; Code: |
84 | 29 |
6369 | 30 ;; Note that this variable is used by non-lisp modes too. |
84 | 31 (defvar defun-prompt-regexp nil |
6369 | 32 "*Non-nil => regexp to ignore, before the character that starts a defun. |
33 This is only necessary if the opening paren or brace is not in column 0. | |
34 See `beginning-of-defun'.") | |
84 | 35 |
3758
e212a0863773
(parens-require-spaces): Var renamed and sense changed.
Richard M. Stallman <rms@gnu.org>
parents:
3733
diff
changeset
|
36 (defvar parens-require-spaces t |
e212a0863773
(parens-require-spaces): Var renamed and sense changed.
Richard M. Stallman <rms@gnu.org>
parents:
3733
diff
changeset
|
37 "Non-nil => `insert-parentheses' should insert whitespace as needed.") |
3733
c1c105ffdd0c
(parens-dont-require-spaces): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
38 |
84 | 39 (defun forward-sexp (&optional arg) |
40 "Move forward across one balanced expression (sexp). | |
215 | 41 With argument, do it that many times. Negative arg -N means |
42 move backward across N balanced expressions." | |
84 | 43 (interactive "p") |
44 (or arg (setq arg 1)) | |
45 (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) | |
46 (if (< arg 0) (backward-prefix-chars))) | |
47 | |
48 (defun backward-sexp (&optional arg) | |
49 "Move backward across one balanced expression (sexp). | |
215 | 50 With argument, do it that many times. Negative arg -N means |
51 move forward across N balanced expressions." | |
84 | 52 (interactive "p") |
53 (or arg (setq arg 1)) | |
54 (forward-sexp (- arg))) | |
55 | |
56 (defun mark-sexp (arg) | |
57 "Set mark ARG sexps from point. | |
215 | 58 The place mark goes is the same place \\[forward-sexp] would |
59 move to with the same argument." | |
84 | 60 (interactive "p") |
61 (push-mark | |
62 (save-excursion | |
63 (forward-sexp arg) | |
2823
8ab0e280fbf0
(mark-sexp, mark-defun): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
64 (point)) |
8ab0e280fbf0
(mark-sexp, mark-defun): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
65 nil t)) |
84 | 66 |
67 (defun forward-list (&optional arg) | |
68 "Move forward across one balanced group of parentheses. | |
69 With argument, do it that many times. | |
70 Negative arg -N means move backward across N groups of parentheses." | |
71 (interactive "p") | |
72 (or arg (setq arg 1)) | |
73 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg)))) | |
74 | |
75 (defun backward-list (&optional arg) | |
76 "Move backward across one balanced group of parentheses. | |
77 With argument, do it that many times. | |
78 Negative arg -N means move forward across N groups of parentheses." | |
79 (interactive "p") | |
80 (or arg (setq arg 1)) | |
81 (forward-list (- arg))) | |
82 | |
83 (defun down-list (arg) | |
84 "Move forward down one level of parentheses. | |
85 With argument, do this that many times. | |
86 A negative argument means move backward but still go down a level. | |
87 In Lisp programs, an argument is required." | |
88 (interactive "p") | |
89 (let ((inc (if (> arg 0) 1 -1))) | |
90 (while (/= arg 0) | |
91 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg))) | |
92 (setq arg (- arg inc))))) | |
93 | |
94 (defun backward-up-list (arg) | |
95 "Move backward out of one level of parentheses. | |
96 With argument, do this that many times. | |
97 A negative argument means move forward but still to a less deep spot. | |
98 In Lisp programs, an argument is required." | |
99 (interactive "p") | |
100 (up-list (- arg))) | |
101 | |
102 (defun up-list (arg) | |
103 "Move forward out of one level of parentheses. | |
104 With argument, do this that many times. | |
105 A negative argument means move backward but still to a less deep spot. | |
106 In Lisp programs, an argument is required." | |
107 (interactive "p") | |
108 (let ((inc (if (> arg 0) 1 -1))) | |
109 (while (/= arg 0) | |
110 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg))) | |
111 (setq arg (- arg inc))))) | |
112 | |
113 (defun kill-sexp (arg) | |
114 "Kill the sexp (balanced expression) following the cursor. | |
115 With argument, kill that many sexps after the cursor. | |
116 Negative arg -N means kill N sexps before the cursor." | |
117 (interactive "p") | |
118 (let ((opoint (point))) | |
119 (forward-sexp arg) | |
120 (kill-region opoint (point)))) | |
121 | |
122 (defun backward-kill-sexp (arg) | |
123 "Kill the sexp (balanced expression) preceding the cursor. | |
124 With argument, kill that many sexps before the cursor. | |
125 Negative arg -N means kill N sexps after the cursor." | |
126 (interactive "p") | |
127 (kill-sexp (- arg))) | |
128 | |
129 (defun beginning-of-defun (&optional arg) | |
130 "Move backward to the beginning of a defun. | |
131 With argument, do it that many times. Negative arg -N | |
132 means move forward to Nth following beginning of defun. | |
133 Returns t unless search stops due to beginning or end of buffer. | |
134 | |
135 Normally a defun starts when there is an char with open-parenthesis | |
136 syntax at the beginning of a line. If `defun-prompt-regexp' is | |
137 non-nil, then a string which matches that regexp may precede the | |
6397
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
138 open-parenthesis, and point ends up at the beginning of the line." |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
139 (interactive "p") |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
140 (and (beginning-of-defun-raw arg) |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
141 (progn (beginning-of-line) t))) |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
142 |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
143 (defun beginning-of-defun-raw (&optional arg) |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
144 "Move point to the character that starts a defun. |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
145 This is identical to beginning-of-defun, except that point does not move |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
146 to the beginning of the line when `defun-prompt-regexp' is non-nil." |
84 | 147 (interactive "p") |
148 (and arg (< arg 0) (forward-char 1)) | |
149 (and (re-search-backward (if defun-prompt-regexp | |
150 (concat "^\\s(\\|" | |
151 "\\(" defun-prompt-regexp "\\)\\s(") | |
152 "^\\s(") | |
153 nil 'move (or arg 1)) | |
6397
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
154 (progn (goto-char (1- (match-end 0)))) t)) |
84 | 155 |
156 (defun buffer-end (arg) | |
157 (if (> arg 0) (point-max) (point-min))) | |
158 | |
159 (defun end-of-defun (&optional arg) | |
160 "Move forward to next end of defun. With argument, do it that many times. | |
161 Negative argument -N means move back to Nth preceding end of defun. | |
162 | |
163 An end of a defun occurs right after the close-parenthesis that matches | |
164 the open-parenthesis that starts a defun; see `beginning-of-defun'." | |
165 (interactive "p") | |
166 (if (or (null arg) (= arg 0)) (setq arg 1)) | |
167 (let ((first t)) | |
168 (while (and (> arg 0) (< (point) (point-max))) | |
169 (let ((pos (point)) npos) | |
170 (while (progn | |
171 (if (and first | |
172 (progn | |
6397
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
173 (end-of-line 1) |
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
174 (beginning-of-defun-raw 1))) |
84 | 175 nil |
176 (or (bobp) (forward-char -1)) | |
6397
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
177 (beginning-of-defun-raw -1)) |
84 | 178 (setq first nil) |
179 (forward-list 1) | |
180 (skip-chars-forward " \t") | |
181 (if (looking-at "\\s<\\|\n") | |
182 (forward-line 1)) | |
183 (<= (point) pos)))) | |
184 (setq arg (1- arg))) | |
185 (while (< arg 0) | |
186 (let ((pos (point))) | |
6397
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
187 (beginning-of-defun-raw 1) |
84 | 188 (forward-sexp 1) |
189 (forward-line 1) | |
190 (if (>= (point) pos) | |
6397
70bf65b6aae9
(beginning-of-defun-raw): New function.
Karl Heuer <kwzh@gnu.org>
parents:
6396
diff
changeset
|
191 (if (beginning-of-defun-raw 2) |
84 | 192 (progn |
193 (forward-list 1) | |
194 (skip-chars-forward " \t") | |
6396
1740ec160ff8
(end-of-defun): Fix check for trailing comment.
Karl Heuer <kwzh@gnu.org>
parents:
6369
diff
changeset
|
195 (if (looking-at "\\s<\\|\n") |
84 | 196 (forward-line 1))) |
197 (goto-char (point-min))))) | |
198 (setq arg (1+ arg))))) | |
199 | |
200 (defun mark-defun () | |
201 "Put mark at end of this defun, point at beginning. | |
202 The defun marked is the one that contains point or follows point." | |
203 (interactive) | |
204 (push-mark (point)) | |
205 (end-of-defun) | |
2823
8ab0e280fbf0
(mark-sexp, mark-defun): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
206 (push-mark (point) nil t) |
84 | 207 (beginning-of-defun) |
208 (re-search-backward "^\n" (- (point) 1) t)) | |
209 | |
210 (defun insert-parentheses (arg) | |
211 "Put parentheses around next ARG sexps. Leave point after open-paren. | |
3733
c1c105ffdd0c
(parens-dont-require-spaces): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
212 No argument is equivalent to zero: just insert `()' and leave point between. |
5838 | 213 If `parens-require-spaces' is non-nil, this command also inserts a space |
214 before and after, depending on the surrounding characters." | |
84 | 215 (interactive "P") |
133
2f5b3f50773d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
119
diff
changeset
|
216 (if arg (setq arg (prefix-numeric-value arg)) |
2f5b3f50773d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
119
diff
changeset
|
217 (setq arg 0)) |
2f5b3f50773d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
119
diff
changeset
|
218 (or (eq arg 0) (skip-chars-forward " \t")) |
3758
e212a0863773
(parens-require-spaces): Var renamed and sense changed.
Richard M. Stallman <rms@gnu.org>
parents:
3733
diff
changeset
|
219 (and parens-require-spaces |
3733
c1c105ffdd0c
(parens-dont-require-spaces): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
220 (memq (char-syntax (preceding-char)) '(?w ?_ ?\) )) |
84 | 221 (insert " ")) |
222 (insert ?\() | |
223 (save-excursion | |
133
2f5b3f50773d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
119
diff
changeset
|
224 (or (eq arg 0) (forward-sexp arg)) |
84 | 225 (insert ?\)) |
3758
e212a0863773
(parens-require-spaces): Var renamed and sense changed.
Richard M. Stallman <rms@gnu.org>
parents:
3733
diff
changeset
|
226 (and parens-require-spaces |
3733
c1c105ffdd0c
(parens-dont-require-spaces): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
227 (memq (char-syntax (following-char)) '(?w ?_ ?\( )) |
133
2f5b3f50773d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
119
diff
changeset
|
228 (insert " ")))) |
84 | 229 |
230 (defun move-past-close-and-reindent () | |
231 "Move past next `)', delete indentation before it, then indent after it." | |
232 (interactive) | |
233 (up-list 1) | |
234 (forward-char -1) | |
235 (while (save-excursion ; this is my contribution | |
236 (let ((before-paren (point))) | |
237 (back-to-indentation) | |
238 (= (point) before-paren))) | |
239 (delete-indentation)) | |
240 (forward-char 1) | |
241 (newline-and-indent)) | |
242 | |
243 (defun lisp-complete-symbol () | |
6004
4fa3d631dae8
(lisp-complete-symbol): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
5981
diff
changeset
|
244 "Perform completion on Lisp symbol preceding point. |
4fa3d631dae8
(lisp-complete-symbol): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
5981
diff
changeset
|
245 Compare that symbol against the known Lisp symbols. |
4fa3d631dae8
(lisp-complete-symbol): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
5981
diff
changeset
|
246 |
4fa3d631dae8
(lisp-complete-symbol): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
5981
diff
changeset
|
247 The context determines which symbols are considered. |
4fa3d631dae8
(lisp-complete-symbol): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
5981
diff
changeset
|
248 If the symbol starts just after an open-parenthesis, only symbols |
215 | 249 with function definitions are considered. Otherwise, all symbols with |
250 function definitions, values or properties are considered." | |
84 | 251 (interactive) |
252 (let* ((end (point)) | |
253 (buffer-syntax (syntax-table)) | |
119 | 254 (beg (unwind-protect |
255 (save-excursion | |
256 (set-syntax-table emacs-lisp-mode-syntax-table) | |
257 (backward-sexp 1) | |
258 (while (= (char-syntax (following-char)) ?\') | |
259 (forward-char 1)) | |
260 (point)) | |
84 | 261 (set-syntax-table buffer-syntax))) |
262 (pattern (buffer-substring beg end)) | |
263 (predicate | |
264 (if (eq (char-after (1- beg)) ?\() | |
265 'fboundp | |
266 (function (lambda (sym) | |
267 (or (boundp sym) (fboundp sym) | |
268 (symbol-plist sym)))))) | |
269 (completion (try-completion pattern obarray predicate))) | |
270 (cond ((eq completion t)) | |
271 ((null completion) | |
272 (message "Can't find completion for \"%s\"" pattern) | |
273 (ding)) | |
274 ((not (string= pattern completion)) | |
275 (delete-region beg end) | |
276 (insert completion)) | |
277 (t | |
278 (message "Making completion list...") | |
279 (let ((list (all-completions pattern obarray predicate))) | |
280 (or (eq predicate 'fboundp) | |
281 (let (new) | |
282 (while list | |
283 (setq new (cons (if (fboundp (intern (car list))) | |
284 (list (car list) " <f>") | |
285 (car list)) | |
286 new)) | |
287 (setq list (cdr list))) | |
288 (setq list (nreverse new)))) | |
289 (with-output-to-temp-buffer " *Completions*" | |
290 (display-completion-list list))) | |
291 (message "Making completion list...%s" "done"))))) | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
215
diff
changeset
|
292 |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
215
diff
changeset
|
293 ;;; lisp.el ends here |