0
|
1 ;;; lisp.el --- Lisp editing commands for XEmacs
|
|
2
|
155
|
3 ;; Copyright (C) 1985, 1986, 1994, 1997 Free Software Foundation, Inc.
|
0
|
4
|
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: lisp, languages
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
0
|
24
|
155
|
25 ;;; Synched up with: Emacs/Mule zeta.
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; Lisp editing commands to go with Lisp major mode.
|
|
30
|
159
|
31 ;; 06/11/1997 - Use char-(after|before) instead of
|
|
32 ;; (following|preceding)-char. -slb
|
|
33
|
0
|
34 ;;; Code:
|
|
35
|
|
36 ;; Note that this variable is used by non-lisp modes too.
|
|
37 (defvar defun-prompt-regexp nil
|
|
38 "*Non-nil => regexp to ignore, before the character that starts a defun.
|
|
39 This is only necessary if the opening paren or brace is not in column 0.
|
|
40 See `beginning-of-defun'.")
|
|
41 (make-variable-buffer-local 'defun-prompt-regexp)
|
|
42
|
|
43 (defvar parens-require-spaces t
|
|
44 "Non-nil => `insert-parentheses' should insert whitespace as needed.")
|
|
45
|
|
46 (defun forward-sexp (&optional arg)
|
|
47 "Move forward across one balanced expression (sexp).
|
2
|
48 With argument, do it that many times. Negative arg -N means
|
|
49 move backward across N balanced expressions."
|
|
50 ;; XEmacs change (for zmacs regions)
|
0
|
51 (interactive "_p")
|
|
52 (or arg (setq arg 1))
|
2
|
53 ;; XEmacs: evil hack! The other half of the evil hack below.
|
0
|
54 (if (and (> arg 0) (looking-at "#s("))
|
|
55 (goto-char (+ (point) 2)))
|
126
|
56 ;; XEmacs change -- don't bomb out if unbalanced sexp
|
0
|
57 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
|
|
58 (if (< arg 0) (backward-prefix-chars))
|
2
|
59 ;; XEmacs: evil hack! Skip back over #s so that structures are read
|
|
60 ;; properly. the current cheesified syntax tables just aren't up to
|
|
61 ;; this.
|
0
|
62 (if (and (< arg 0)
|
|
63 (eq (char-after (point)) ?\()
|
|
64 (>= (- (point) (point-min)) 2)
|
|
65 (eq (char-after (- (point) 1)) ?s)
|
|
66 (eq (char-after (- (point) 2)) ?#))
|
|
67 (goto-char (- (point) 2))))
|
|
68
|
|
69 (defun backward-sexp (&optional arg)
|
|
70 "Move backward across one balanced expression (sexp).
|
2
|
71 With argument, do it that many times. Negative arg -N means
|
|
72 move forward across N balanced expressions."
|
|
73 ;; XEmacs change (for zmacs regions)
|
0
|
74 (interactive "_p")
|
|
75 (or arg (setq arg 1))
|
|
76 (forward-sexp (- arg)))
|
|
77
|
|
78 (defun mark-sexp (arg)
|
|
79 "Set mark ARG sexps from point.
|
2
|
80 The place mark goes is the same place \\[forward-sexp] would
|
|
81 move to with the same argument.
|
0
|
82 Repeat this command to mark more sexps in the same direction."
|
|
83 (interactive "p")
|
2
|
84 ;; XEmacs change
|
0
|
85 (mark-something 'mark-sexp 'forward-sexp arg))
|
|
86
|
|
87 (defun forward-list (&optional arg)
|
|
88 "Move forward across one balanced group of parentheses.
|
|
89 With argument, do it that many times.
|
|
90 Negative arg -N means move backward across N groups of parentheses."
|
2
|
91 ;; XEmacs change
|
0
|
92 (interactive "_p")
|
|
93 (or arg (setq arg 1))
|
|
94 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
|
|
95
|
|
96 (defun backward-list (&optional arg)
|
|
97 "Move backward across one balanced group of parentheses.
|
|
98 With argument, do it that many times.
|
|
99 Negative arg -N means move forward across N groups of parentheses."
|
2
|
100 ;; XEmacs change (for zmacs regions)
|
0
|
101 (interactive "_p")
|
|
102 (or arg (setq arg 1))
|
|
103 (forward-list (- arg)))
|
|
104
|
|
105 (defun down-list (arg)
|
|
106 "Move forward down one level of parentheses.
|
|
107 With argument, do this that many times.
|
|
108 A negative argument means move backward but still go down a level.
|
|
109 In Lisp programs, an argument is required."
|
2
|
110 ;; XEmacs change (for zmacs regions)
|
0
|
111 (interactive "_p")
|
|
112 (let ((inc (if (> arg 0) 1 -1)))
|
|
113 (while (/= arg 0)
|
|
114 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
|
|
115 (setq arg (- arg inc)))))
|
|
116
|
|
117 (defun backward-up-list (arg)
|
|
118 "Move backward out of one level of parentheses.
|
|
119 With argument, do this that many times.
|
|
120 A negative argument means move forward but still to a less deep spot.
|
|
121 In Lisp programs, an argument is required."
|
74
|
122 (interactive "_p")
|
0
|
123 (up-list (- arg)))
|
|
124
|
|
125 (defun up-list (arg)
|
|
126 "Move forward out of one level of parentheses.
|
|
127 With argument, do this that many times.
|
|
128 A negative argument means move backward but still to a less deep spot.
|
|
129 In Lisp programs, an argument is required."
|
2
|
130 ;; XEmacs change (for zmacs regions)
|
0
|
131 (interactive "_p")
|
|
132 (let ((inc (if (> arg 0) 1 -1)))
|
|
133 (while (/= arg 0)
|
|
134 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
|
|
135 (setq arg (- arg inc)))))
|
|
136
|
|
137 (defun kill-sexp (arg)
|
|
138 "Kill the sexp (balanced expression) following the cursor.
|
|
139 With argument, kill that many sexps after the cursor.
|
|
140 Negative arg -N means kill N sexps before the cursor."
|
|
141 (interactive "p")
|
|
142 (let ((opoint (point)))
|
|
143 (forward-sexp arg)
|
|
144 (kill-region opoint (point))))
|
|
145
|
|
146 (defun backward-kill-sexp (arg)
|
|
147 "Kill the sexp (balanced expression) preceding the cursor.
|
|
148 With argument, kill that many sexps before the cursor.
|
|
149 Negative arg -N means kill N sexps after the cursor."
|
|
150 (interactive "p")
|
|
151 (kill-sexp (- arg)))
|
|
152
|
|
153 (defun beginning-of-defun (&optional arg)
|
|
154 "Move backward to the beginning of a defun.
|
|
155 With argument, do it that many times. Negative arg -N
|
|
156 means move forward to Nth following beginning of defun.
|
|
157 Returns t unless search stops due to beginning or end of buffer.
|
|
158
|
|
159 Normally a defun starts when there is an char with open-parenthesis
|
|
160 syntax at the beginning of a line. If `defun-prompt-regexp' is
|
|
161 non-nil, then a string which matches that regexp may precede the
|
|
162 open-parenthesis, and point ends up at the beginning of the line."
|
2
|
163 ;; XEmacs change (for zmacs regions)
|
0
|
164 (interactive "_p")
|
|
165 (and (beginning-of-defun-raw arg)
|
|
166 (progn (beginning-of-line) t)))
|
|
167
|
|
168 (defun beginning-of-defun-raw (&optional arg)
|
|
169 "Move point to the character that starts a defun.
|
|
170 This is identical to beginning-of-defun, except that point does not move
|
|
171 to the beginning of the line when `defun-prompt-regexp' is non-nil."
|
|
172 (interactive "p")
|
|
173 (and arg (< arg 0) (not (eobp)) (forward-char 1))
|
|
174 (and (re-search-backward (if defun-prompt-regexp
|
|
175 (concat "^\\s(\\|"
|
|
176 "\\(" defun-prompt-regexp "\\)\\s(")
|
|
177 "^\\s(")
|
|
178 nil 'move (or arg 1))
|
|
179 (progn (goto-char (1- (match-end 0)))) t))
|
|
180
|
2
|
181 ;; XEmacs change (optional buffer parameter)
|
0
|
182 (defun buffer-end (arg &optional buffer)
|
|
183 "Return `point-max' of BUFFER if ARG is > 0; return `point-min' otherwise.
|
|
184 BUFFER defaults to the current buffer if omitted."
|
|
185 (if (> arg 0) (point-max buffer) (point-min buffer)))
|
|
186
|
|
187 (defun end-of-defun (&optional arg)
|
|
188 "Move forward to next end of defun. With argument, do it that many times.
|
|
189 Negative argument -N means move back to Nth preceding end of defun.
|
|
190
|
|
191 An end of a defun occurs right after the close-parenthesis that matches
|
|
192 the open-parenthesis that starts a defun; see `beginning-of-defun'."
|
2
|
193 ;; XEmacs change (for zmacs regions)
|
0
|
194 (interactive "_p")
|
|
195 (if (or (null arg) (= arg 0)) (setq arg 1))
|
|
196 (let ((first t))
|
|
197 (while (and (> arg 0) (< (point) (point-max)))
|
155
|
198 (let ((pos (point))) ; XEmacs -- remove unused npos.
|
0
|
199 (while (progn
|
|
200 (if (and first
|
|
201 (progn
|
|
202 (end-of-line 1)
|
|
203 (beginning-of-defun-raw 1)))
|
|
204 nil
|
|
205 (or (bobp) (forward-char -1))
|
|
206 (beginning-of-defun-raw -1))
|
|
207 (setq first nil)
|
|
208 (forward-list 1)
|
|
209 (skip-chars-forward " \t")
|
|
210 (if (looking-at "\\s<\\|\n")
|
|
211 (forward-line 1))
|
|
212 (<= (point) pos))))
|
|
213 (setq arg (1- arg)))
|
|
214 (while (< arg 0)
|
|
215 (let ((pos (point)))
|
|
216 (beginning-of-defun-raw 1)
|
|
217 (forward-sexp 1)
|
|
218 (forward-line 1)
|
|
219 (if (>= (point) pos)
|
|
220 (if (beginning-of-defun-raw 2)
|
|
221 (progn
|
|
222 (forward-list 1)
|
|
223 (skip-chars-forward " \t")
|
|
224 (if (looking-at "\\s<\\|\n")
|
|
225 (forward-line 1)))
|
|
226 (goto-char (point-min)))))
|
|
227 (setq arg (1+ arg)))))
|
|
228
|
|
229 (defun mark-defun ()
|
|
230 "Put mark at end of this defun, point at beginning.
|
|
231 The defun marked is the one that contains point or follows point."
|
|
232 (interactive)
|
|
233 (push-mark (point))
|
|
234 (end-of-defun)
|
|
235 (push-mark (point) nil t)
|
|
236 (beginning-of-defun)
|
|
237 (re-search-backward "^\n" (- (point) 1) t))
|
|
238
|
155
|
239 (defun narrow-to-defun (&optional arg)
|
|
240 "Make text outside current defun invisible.
|
|
241 The defun visible is the one that contains point or follows point."
|
|
242 (interactive)
|
|
243 (save-excursion
|
|
244 (widen)
|
|
245 (end-of-defun)
|
|
246 (let ((end (point)))
|
|
247 (beginning-of-defun)
|
|
248 (narrow-to-region (point) end))))
|
|
249
|
0
|
250 (defun insert-parentheses (arg)
|
155
|
251 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
|
|
252 A negative ARG encloses the preceding ARG sexps instead.
|
0
|
253 No argument is equivalent to zero: just insert `()' and leave point between.
|
|
254 If `parens-require-spaces' is non-nil, this command also inserts a space
|
|
255 before and after, depending on the surrounding characters."
|
|
256 (interactive "P")
|
|
257 (if arg (setq arg (prefix-numeric-value arg))
|
|
258 (setq arg 0))
|
155
|
259 (cond ((> arg 0) (skip-chars-forward " \t"))
|
|
260 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
|
0
|
261 (and parens-require-spaces
|
|
262 (not (bobp))
|
159
|
263 (memq (char-syntax (char-before (point))) '(?w ?_ ?\) ))
|
0
|
264 (insert " "))
|
|
265 (insert ?\()
|
|
266 (save-excursion
|
|
267 (or (eq arg 0) (forward-sexp arg))
|
|
268 (insert ?\))
|
|
269 (and parens-require-spaces
|
|
270 (not (eobp))
|
159
|
271 (memq (char-syntax (char-after (point))) '(?w ?_ ?\( ))
|
0
|
272 (insert " "))))
|
|
273
|
|
274 (defun move-past-close-and-reindent ()
|
|
275 "Move past next `)', delete indentation before it, then indent after it."
|
|
276 (interactive)
|
|
277 (up-list 1)
|
|
278 (forward-char -1)
|
|
279 (while (save-excursion ; this is my contribution
|
|
280 (let ((before-paren (point)))
|
|
281 (back-to-indentation)
|
|
282 (= (point) before-paren)))
|
|
283 (delete-indentation))
|
|
284 (forward-char 1)
|
|
285 (newline-and-indent))
|
|
286
|
|
287 (defun lisp-complete-symbol ()
|
|
288 "Perform completion on Lisp symbol preceding point.
|
2
|
289 Compare that symbol against the known Lisp symbols.
|
|
290
|
|
291 The context determines which symbols are considered.
|
|
292 If the symbol starts just after an open-parenthesis, only symbols
|
|
293 with function definitions are considered. Otherwise, all symbols with
|
|
294 function definitions, values or properties are considered."
|
0
|
295 (interactive)
|
|
296 (let* ((end (point))
|
|
297 (buffer-syntax (syntax-table))
|
|
298 (beg (unwind-protect
|
|
299 (save-excursion
|
2
|
300 ;; XEmacs change
|
0
|
301 (if emacs-lisp-mode-syntax-table
|
|
302 (set-syntax-table emacs-lisp-mode-syntax-table))
|
|
303 (backward-sexp 1)
|
159
|
304 (while (eq (char-syntax (char-after (point))) ?\')
|
0
|
305 (forward-char 1))
|
|
306 (point))
|
|
307 (set-syntax-table buffer-syntax)))
|
|
308 (pattern (buffer-substring beg end))
|
|
309 (predicate
|
|
310 (if (eq (char-after (1- beg)) ?\()
|
|
311 'fboundp
|
2
|
312 ;; XEmacs change
|
0
|
313 #'(lambda (sym)
|
|
314 (or (boundp sym) (fboundp sym)
|
|
315 (symbol-plist sym)))))
|
|
316 (completion (try-completion pattern obarray predicate)))
|
|
317 (cond ((eq completion t))
|
|
318 ((null completion)
|
|
319 (message "Can't find completion for \"%s\"" pattern)
|
|
320 (ding))
|
|
321 ((not (string= pattern completion))
|
|
322 (delete-region beg end)
|
|
323 (insert completion))
|
|
324 (t
|
|
325 (message "Making completion list...")
|
|
326 (let ((list (all-completions pattern obarray predicate))
|
155
|
327 ;FSFmacs crock unnecessary in XEmacs
|
0
|
328 ;see minibuf.el
|
|
329 ;(completion-fixup-function
|
2
|
330 ; (function (lambda () (if (save-excursion
|
0
|
331 ; (goto-char (max (point-min)
|
|
332 ; (- (point) 4)))
|
|
333 ; (looking-at " <f>"))
|
|
334 ; (forward-char -4))))
|
|
335 )
|
|
336 (or (eq predicate 'fboundp)
|
|
337 (let (new)
|
|
338 (while list
|
|
339 (setq new (cons (if (fboundp (intern (car list)))
|
|
340 (list (car list) " <f>")
|
|
341 (car list))
|
|
342 new))
|
|
343 (setq list (cdr list)))
|
|
344 (setq list (nreverse new))))
|
|
345 (with-output-to-temp-buffer "*Completions*"
|
116
|
346 (display-completion-list list)))
|
2
|
347 (message "Making completion list...%s" "done")))))
|
0
|
348
|
|
349 ;;; lisp.el ends here
|