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