aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Love2000-01-05 15:08:36 +0000
committerDave Love2000-01-05 15:08:36 +0000
commitc6eeec655084068ac8b1d5e5751ee8993abd6e2f (patch)
tree9baac949ee35dac96213d67aa81035eb8a4d3ee6
parentfd897522d43b64650abd089ecb1b3a5569fbbd49 (diff)
downloademacs-c6eeec655084068ac8b1d5e5751ee8993abd6e2f.tar.gz
emacs-c6eeec655084068ac8b1d5e5751ee8993abd6e2f.zip
(beginning-of-defun): New variable.
(beginning-of-defun-raw): Use it. (end-of-defun): New variable. (end-of-defun): Use it. (check-parens): New command.
-rw-r--r--lisp/emacs-lisp/lisp.el180
1 files changed, 120 insertions, 60 deletions
diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index ceedda40fcb..5cf4128d38d 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -1,6 +1,6 @@
1;;; lisp.el --- Lisp editing commands for Emacs 1;;; lisp.el --- Lisp editing commands for Emacs
2 2
3;; Copyright (C) 1985, 1986, 1994 Free Software Foundation, Inc. 3;; Copyright (C) 1985, 1986, 1994, 2000 Free Software Foundation, Inc.
4 4
5;; Maintainer: FSF 5;; Maintainer: FSF
6;; Keywords: lisp, languages 6;; Keywords: lisp, languages
@@ -32,7 +32,7 @@
32(defcustom defun-prompt-regexp nil 32(defcustom defun-prompt-regexp nil
33 "*Non-nil => regexp to ignore, before the character that starts a defun. 33 "*Non-nil => regexp to ignore, before the character that starts a defun.
34This is only necessary if the opening paren or brace is not in column 0. 34This is only necessary if the opening paren or brace is not in column 0.
35See `beginning-of-defun'." 35See function `beginning-of-defun'."
36 :type '(choice (const nil) 36 :type '(choice (const nil)
37 regexp) 37 regexp)
38 :group 'lisp) 38 :group 'lisp)
@@ -45,7 +45,7 @@ See `beginning-of-defun'."
45 45
46(defun forward-sexp (&optional arg) 46(defun forward-sexp (&optional arg)
47 "Move forward across one balanced expression (sexp). 47 "Move forward across one balanced expression (sexp).
48With argument, do it that many times. Negative arg -N means 48With ARG, do it that many times. Negative arg -N means
49move backward across N balanced expressions." 49move backward across N balanced expressions."
50 (interactive "p") 50 (interactive "p")
51 (or arg (setq arg 1)) 51 (or arg (setq arg 1))
@@ -54,7 +54,7 @@ move backward across N balanced expressions."
54 54
55(defun backward-sexp (&optional arg) 55(defun backward-sexp (&optional arg)
56 "Move backward across one balanced expression (sexp). 56 "Move backward across one balanced expression (sexp).
57With argument, do it that many times. Negative arg -N means 57With ARG, do it that many times. Negative arg -N means
58move forward across N balanced expressions." 58move forward across N balanced expressions."
59 (interactive "p") 59 (interactive "p")
60 (or arg (setq arg 1)) 60 (or arg (setq arg 1))
@@ -73,7 +73,7 @@ move to with the same argument."
73 73
74(defun forward-list (&optional arg) 74(defun forward-list (&optional arg)
75 "Move forward across one balanced group of parentheses. 75 "Move forward across one balanced group of parentheses.
76With argument, do it that many times. 76With ARG, do it that many times.
77Negative arg -N means move backward across N groups of parentheses." 77Negative arg -N means move backward across N groups of parentheses."
78 (interactive "p") 78 (interactive "p")
79 (or arg (setq arg 1)) 79 (or arg (setq arg 1))
@@ -81,7 +81,7 @@ Negative arg -N means move backward across N groups of parentheses."
81 81
82(defun backward-list (&optional arg) 82(defun backward-list (&optional arg)
83 "Move backward across one balanced group of parentheses. 83 "Move backward across one balanced group of parentheses.
84With argument, do it that many times. 84With ARG, do it that many times.
85Negative arg -N means move forward across N groups of parentheses." 85Negative arg -N means move forward across N groups of parentheses."
86 (interactive "p") 86 (interactive "p")
87 (or arg (setq arg 1)) 87 (or arg (setq arg 1))
@@ -89,7 +89,7 @@ Negative arg -N means move forward across N groups of parentheses."
89 89
90(defun down-list (arg) 90(defun down-list (arg)
91 "Move forward down one level of parentheses. 91 "Move forward down one level of parentheses.
92With argument, do this that many times. 92With ARG, do this that many times.
93A negative argument means move backward but still go down a level. 93A negative argument means move backward but still go down a level.
94In Lisp programs, an argument is required." 94In Lisp programs, an argument is required."
95 (interactive "p") 95 (interactive "p")
@@ -100,15 +100,15 @@ In Lisp programs, an argument is required."
100 100
101(defun backward-up-list (arg) 101(defun backward-up-list (arg)
102 "Move backward out of one level of parentheses. 102 "Move backward out of one level of parentheses.
103With argument, do this that many times. 103With ARG, do this that many times.
104A negative argument means move forward but still to a less deep spot. 104A negative argument means move forward but still to a less deep spot.
105In Lisp programs, an argument is required." 105In Lisp programs, an argument is required."
106 (interactive "p") 106 (interactive "p")
107 (up-list (- arg))) 107 (up-list (- arg)))
108 108
109(defun up-list (arg) 109(defun up-list (arg)
110 "Move forward out of one level of parentheses. 110 "Move forward out of one level of parentheses.
111With argument, do this that many times. 111With ARG, do this that many times.
112A negative argument means move backward but still to a less deep spot. 112A negative argument means move backward but still to a less deep spot.
113In Lisp programs, an argument is required." 113In Lisp programs, an argument is required."
114 (interactive "p") 114 (interactive "p")
@@ -119,7 +119,7 @@ In Lisp programs, an argument is required."
119 119
120(defun kill-sexp (arg) 120(defun kill-sexp (arg)
121 "Kill the sexp (balanced expression) following the cursor. 121 "Kill the sexp (balanced expression) following the cursor.
122With argument, kill that many sexps after the cursor. 122With ARG, kill that many sexps after the cursor.
123Negative arg -N means kill N sexps before the cursor." 123Negative arg -N means kill N sexps before the cursor."
124 (interactive "p") 124 (interactive "p")
125 (let ((opoint (point))) 125 (let ((opoint (point)))
@@ -128,37 +128,69 @@ Negative arg -N means kill N sexps before the cursor."
128 128
129(defun backward-kill-sexp (arg) 129(defun backward-kill-sexp (arg)
130 "Kill the sexp (balanced expression) preceding the cursor. 130 "Kill the sexp (balanced expression) preceding the cursor.
131With argument, kill that many sexps before the cursor. 131With ARG, kill that many sexps before the cursor.
132Negative arg -N means kill N sexps after the cursor." 132Negative arg -N means kill N sexps after the cursor."
133 (interactive "p") 133 (interactive "p")
134 (kill-sexp (- arg))) 134 (kill-sexp (- arg)))
135 135
136(defvar beginning-of-defun nil
137 "If non-nil, function for `beginning-of-defun-raw' to call.
138This is used to find the beginning of the defun instead of using the
139normal recipe described in the doc of function `beginning-of-defun'.
140Major modes can define this if defining `defun-prompt-regexp' is not
141sufficient to use the normal recipe.
142
143The function should go to the line on which the current \"defun\"
144starts and return non-nil or should return nil if it can't find the
145beginning.
146
147Buffer-local.")
148(make-variable-buffer-local 'beginning-of-defun)
149
136(defun beginning-of-defun (&optional arg) 150(defun beginning-of-defun (&optional arg)
137 "Move backward to the beginning of a defun. 151 "Move backward to the beginning of a defun.
138With argument, do it that many times. Negative arg -N 152With ARG, do it that many times. Negative arg -N
139means move forward to Nth following beginning of defun. 153means move forward to Nth following beginning of defun.
140Returns t unless search stops due to beginning or end of buffer. 154Returns t unless search stops due to beginning or end of buffer.
141 155
142Normally a defun starts when there is an char with open-parenthesis 156Normally a defun starts when there is an char with open-parenthesis
143syntax at the beginning of a line. If `defun-prompt-regexp' is 157syntax at the beginning of a line. If `defun-prompt-regexp' is
144non-nil, then a string which matches that regexp may precede the 158non-nil, then a string which matches that regexp may precede the
145open-parenthesis, and point ends up at the beginning of the line." 159open-parenthesis, and point ends up at the beginning of the line.
160
161If variable `beginning-of-defun' is non-nil, its value is called as a
162function to find the defun's beginning."
146 (interactive "p") 163 (interactive "p")
147 (and (beginning-of-defun-raw arg) 164 (and (beginning-of-defun-raw arg)
148 (progn (beginning-of-line) t))) 165 (progn (beginning-of-line) t)))
149 166
150(defun beginning-of-defun-raw (&optional arg) 167(defun beginning-of-defun-raw (&optional arg)
151 "Move point to the character that starts a defun. 168 "Move point to the character that starts a defun.
152This is identical to beginning-of-defun, except that point does not move 169This is identical to function `beginning-of-defun', except that point
153to the beginning of the line when `defun-prompt-regexp' is non-nil." 170does not move to the beginning of the line when `defun-prompt-regexp'
171is non-nil.
172
173If variable `beginning-of-defun' is non-nil, its value is called as a
174function to find the defun's beginning."
154 (interactive "p") 175 (interactive "p")
155 (and arg (< arg 0) (not (eobp)) (forward-char 1)) 176 (if beginning-of-defun
156 (and (re-search-backward (if defun-prompt-regexp 177 (funcall beginning-of-defun)
157 (concat "^\\s(\\|" 178 (and arg (< arg 0) (not (eobp)) (forward-char 1))
158 "\\(" defun-prompt-regexp "\\)\\s(") 179 (and (re-search-backward (if defun-prompt-regexp
159 "^\\s(") 180 (concat "^\\s(\\|"
160 nil 'move (or arg 1)) 181 "\\(" defun-prompt-regexp "\\)\\s(")
161 (progn (goto-char (1- (match-end 0)))) t)) 182 "^\\s(")
183 nil 'move (or arg 1))
184 (progn (goto-char (1- (match-end 0)))) t)))
185
186(defvar end-of-defun nil
187 "If non-nil, function for function `end-of-defun' to call.
188This is used to find the end of the defun instead of using the normal
189recipe described in the doc of function `end-of-defun'. Major modes
190can define this if the normal recipe is not appropriate.
191
192Buffer-local.")
193(make-variable-buffer-local 'end-of-defun)
162 194
163(defun buffer-end (arg) 195(defun buffer-end (arg)
164 (if (> arg 0) (point-max) (point-min))) 196 (if (> arg 0) (point-max) (point-min)))
@@ -167,42 +199,45 @@ to the beginning of the line when `defun-prompt-regexp' is non-nil."
167 "Move forward to next end of defun. With argument, do it that many times. 199 "Move forward to next end of defun. With argument, do it that many times.
168Negative argument -N means move back to Nth preceding end of defun. 200Negative argument -N means move back to Nth preceding end of defun.
169 201
170An end of a defun occurs right after the close-parenthesis that matches 202An end of a defun occurs right after the close-parenthesis that
171the open-parenthesis that starts a defun; see `beginning-of-defun'." 203matches the open-parenthesis that starts a defun; see function
204`beginning-of-defun'."
172 (interactive "p") 205 (interactive "p")
173 (if (or (null arg) (= arg 0)) (setq arg 1)) 206 (if end-of-defun
174 (let ((first t)) 207 (funcall end-of-defun)
175 (while (and (> arg 0) (< (point) (point-max))) 208 (if (or (null arg) (= arg 0)) (setq arg 1))
176 (let ((pos (point)) npos) 209 (let ((first t))
177 (while (progn 210 (while (and (> arg 0) (< (point) (point-max)))
178 (if (and first 211 (let ((pos (point)) npos)
179 (progn 212 (while (progn
180 (end-of-line 1) 213 (if (and first
181 (beginning-of-defun-raw 1))) 214 (progn
182 nil 215 (end-of-line 1)
183 (or (bobp) (forward-char -1)) 216 (beginning-of-defun-raw 1)))
184 (beginning-of-defun-raw -1)) 217 nil
185 (setq first nil) 218 (or (bobp) (forward-char -1))
186 (forward-list 1) 219 (beginning-of-defun-raw -1))
187 (skip-chars-forward " \t") 220 (setq first nil)
188 (if (looking-at "\\s<\\|\n") 221 (forward-list 1)
189 (forward-line 1)) 222 (skip-chars-forward " \t")
190 (<= (point) pos)))) 223 (if (looking-at "\\s<\\|\n")
191 (setq arg (1- arg))) 224 (forward-line 1))
192 (while (< arg 0) 225 (<= (point) pos))))
193 (let ((pos (point))) 226 (setq arg (1- arg)))
194 (beginning-of-defun-raw 1) 227 (while (< arg 0)
195 (forward-sexp 1) 228 (let ((pos (point)))
196 (forward-line 1) 229 (beginning-of-defun-raw 1)
197 (if (>= (point) pos) 230 (forward-sexp 1)
198 (if (beginning-of-defun-raw 2) 231 (forward-line 1)
199 (progn 232 (if (>= (point) pos)
200 (forward-list 1) 233 (if (beginning-of-defun-raw 2)
201 (skip-chars-forward " \t") 234 (progn
202 (if (looking-at "\\s<\\|\n") 235 (forward-list 1)
203 (forward-line 1))) 236 (skip-chars-forward " \t")
204 (goto-char (point-min))))) 237 (if (looking-at "\\s<\\|\n")
205 (setq arg (1+ arg))))) 238 (forward-line 1)))
239 (goto-char (point-min)))))
240 (setq arg (1+ arg))))))
206 241
207(defun mark-defun () 242(defun mark-defun ()
208 "Put mark at end of this defun, point at beginning. 243 "Put mark at end of this defun, point at beginning.
@@ -216,7 +251,8 @@ The defun marked is the one that contains point or follows point."
216 251
217(defun narrow-to-defun (&optional arg) 252(defun narrow-to-defun (&optional arg)
218 "Make text outside current defun invisible. 253 "Make text outside current defun invisible.
219The defun visible is the one that contains point or follows point." 254The defun visible is the one that contains point or follows point.
255Optional ARG is ignored."
220 (interactive) 256 (interactive)
221 (save-excursion 257 (save-excursion
222 (widen) 258 (widen)
@@ -267,7 +303,7 @@ before and after, depending on the surrounding characters."
267 state) 303 state)
268 (beginning-of-line) 304 (beginning-of-line)
269 ;; Get state at start of line. 305 ;; Get state at start of line.
270 (setq state (list 0 nil nil 306 (setq state (list 0 nil nil
271 (null (calculate-lisp-indent)) 307 (null (calculate-lisp-indent))
272 nil nil nil nil 308 nil nil nil nil
273 nil)) 309 nil))
@@ -279,6 +315,30 @@ before and after, depending on the surrounding characters."
279 (delete-indentation)) 315 (delete-indentation))
280 (forward-char 1) 316 (forward-char 1)
281 (newline-and-indent)) 317 (newline-and-indent))
318
319(defun check-parens () ; lame name?
320 "Check for unbalanced parentheses in the current buffer.
321More accurately, check the narrowed part of the buffer for unbalanced
322expressions (\"sexps\") in general. This is done according to the
323current syntax table and will find unbalanced brackets or quotes as
324appropriate. (See Info node `(emacs)Lists and Sexps'.) If imbalance
325is found, an error is signalled and point is left at the first
326unbalanced character."
327 (interactive)
328 (condition-case data
329 ;; Buffer can't have more than (point-max) sexps.
330 (scan-sexps (point-min) (point-max))
331 (scan-error (goto-char (nth 2 data))
332 ;; Could print (nth 1 data), which is either
333 ;; "Containing expression ends prematurely" or
334 ;; "Unbalanced parentheses", but those may not be so
335 ;; accurate/helpful, e.g. quotes may actually be
336 ;; mismatched.
337 (error "Unmatched bracket or quote"))
338 (error (cond ((eq 'scan-error (car data))
339 (goto-char (nth 2 data))
340 (error "Unmatched bracket or quote"))
341 (t (signal (car data) (cdr data)))))))
282 342
283(defun lisp-complete-symbol () 343(defun lisp-complete-symbol ()
284 "Perform completion on Lisp symbol preceding point. 344 "Perform completion on Lisp symbol preceding point.