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