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