Commit | Line | Data |
---|---|---|
b73b9811 | 1 | ;; Lisp editing commands for Emacs |
2 | ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. | |
3 | ||
4 | ;; This file is part of GNU Emacs. | |
5 | ||
6 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
7 | ;; it under the terms of the GNU General Public License as published by | |
8 | ;; the Free Software Foundation; either version 1, or (at your option) | |
9 | ;; any later version. | |
10 | ||
11 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
12 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;; GNU General Public License for more details. | |
15 | ||
16 | ;; You should have received a copy of the GNU General Public License | |
17 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
18 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | ||
20 | ||
21 | (defvar defun-prompt-regexp nil | |
22 | "Non-nil => regexp to ignore, before the `(' that starts a defun.") | |
23 | ||
24 | (defun forward-sexp (&optional arg) | |
25 | "Move forward across one balanced expression (sexp). | |
e3f99b91 JB |
26 | With argument, do it that many times. Negative arg -N means |
27 | move backward across N balanced expressions." | |
b73b9811 | 28 | (interactive "p") |
29 | (or arg (setq arg 1)) | |
30 | (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) | |
31 | (if (< arg 0) (backward-prefix-chars))) | |
32 | ||
33 | (defun backward-sexp (&optional arg) | |
34 | "Move backward across one balanced expression (sexp). | |
e3f99b91 JB |
35 | With argument, do it that many times. Negative arg -N means |
36 | move forward across N balanced expressions." | |
b73b9811 | 37 | (interactive "p") |
38 | (or arg (setq arg 1)) | |
39 | (forward-sexp (- arg))) | |
40 | ||
41 | (defun mark-sexp (arg) | |
42 | "Set mark ARG sexps from point. | |
e3f99b91 JB |
43 | The place mark goes is the same place \\[forward-sexp] would |
44 | move to with the same argument." | |
b73b9811 | 45 | (interactive "p") |
46 | (push-mark | |
47 | (save-excursion | |
48 | (forward-sexp arg) | |
49 | (point)))) | |
50 | ||
51 | (defun forward-list (&optional arg) | |
52 | "Move forward across one balanced group of parentheses. | |
53 | With argument, do it that many times. | |
54 | Negative arg -N means move backward across N groups of parentheses." | |
55 | (interactive "p") | |
56 | (or arg (setq arg 1)) | |
57 | (goto-char (or (scan-lists (point) arg 0) (buffer-end arg)))) | |
58 | ||
59 | (defun backward-list (&optional arg) | |
60 | "Move backward across one balanced group of parentheses. | |
61 | With argument, do it that many times. | |
62 | Negative arg -N means move forward across N groups of parentheses." | |
63 | (interactive "p") | |
64 | (or arg (setq arg 1)) | |
65 | (forward-list (- arg))) | |
66 | ||
67 | (defun down-list (arg) | |
68 | "Move forward down one level of parentheses. | |
69 | With argument, do this that many times. | |
70 | A negative argument means move backward but still go down a level. | |
71 | In Lisp programs, an argument is required." | |
72 | (interactive "p") | |
73 | (let ((inc (if (> arg 0) 1 -1))) | |
74 | (while (/= arg 0) | |
75 | (goto-char (or (scan-lists (point) inc -1) (buffer-end arg))) | |
76 | (setq arg (- arg inc))))) | |
77 | ||
78 | (defun backward-up-list (arg) | |
79 | "Move backward out of one level of parentheses. | |
80 | With argument, do this that many times. | |
81 | A negative argument means move forward but still to a less deep spot. | |
82 | In Lisp programs, an argument is required." | |
83 | (interactive "p") | |
84 | (up-list (- arg))) | |
85 | ||
86 | (defun up-list (arg) | |
87 | "Move forward out of one level of parentheses. | |
88 | With argument, do this that many times. | |
89 | A negative argument means move backward but still to a less deep spot. | |
90 | In Lisp programs, an argument is required." | |
91 | (interactive "p") | |
92 | (let ((inc (if (> arg 0) 1 -1))) | |
93 | (while (/= arg 0) | |
94 | (goto-char (or (scan-lists (point) inc 1) (buffer-end arg))) | |
95 | (setq arg (- arg inc))))) | |
96 | ||
97 | (defun kill-sexp (arg) | |
98 | "Kill the sexp (balanced expression) following the cursor. | |
99 | With argument, kill that many sexps after the cursor. | |
100 | Negative arg -N means kill N sexps before the cursor." | |
101 | (interactive "p") | |
102 | (let ((opoint (point))) | |
103 | (forward-sexp arg) | |
104 | (kill-region opoint (point)))) | |
105 | ||
106 | (defun backward-kill-sexp (arg) | |
107 | "Kill the sexp (balanced expression) preceding the cursor. | |
108 | With argument, kill that many sexps before the cursor. | |
109 | Negative arg -N means kill N sexps after the cursor." | |
110 | (interactive "p") | |
111 | (kill-sexp (- arg))) | |
112 | \f | |
113 | (defun beginning-of-defun (&optional arg) | |
114 | "Move backward to the beginning of a defun. | |
115 | With argument, do it that many times. Negative arg -N | |
116 | means move forward to Nth following beginning of defun. | |
117 | Returns t unless search stops due to beginning or end of buffer. | |
118 | ||
119 | Normally a defun starts when there is an char with open-parenthesis | |
120 | syntax at the beginning of a line. If `defun-prompt-regexp' is | |
121 | non-nil, then a string which matches that regexp may precede the | |
122 | open-parenthesis." | |
123 | (interactive "p") | |
124 | (and arg (< arg 0) (forward-char 1)) | |
125 | (and (re-search-backward (if defun-prompt-regexp | |
126 | (concat "^\\s(\\|" | |
127 | "\\(" defun-prompt-regexp "\\)\\s(") | |
128 | "^\\s(") | |
129 | nil 'move (or arg 1)) | |
130 | (progn (beginning-of-line) t))) | |
131 | ||
132 | (defun buffer-end (arg) | |
133 | (if (> arg 0) (point-max) (point-min))) | |
134 | ||
135 | (defun end-of-defun (&optional arg) | |
136 | "Move forward to next end of defun. With argument, do it that many times. | |
137 | Negative argument -N means move back to Nth preceding end of defun. | |
138 | ||
139 | An end of a defun occurs right after the close-parenthesis that matches | |
140 | the open-parenthesis that starts a defun; see `beginning-of-defun'." | |
141 | (interactive "p") | |
142 | (if (or (null arg) (= arg 0)) (setq arg 1)) | |
143 | (let ((first t)) | |
144 | (while (and (> arg 0) (< (point) (point-max))) | |
145 | (let ((pos (point)) npos) | |
146 | (while (progn | |
147 | (if (and first | |
148 | (progn | |
149 | (forward-char 1) | |
150 | (beginning-of-defun 1))) | |
151 | nil | |
152 | (or (bobp) (forward-char -1)) | |
153 | (beginning-of-defun -1)) | |
154 | (setq first nil) | |
155 | (forward-list 1) | |
156 | (skip-chars-forward " \t") | |
157 | (if (looking-at "\\s<\\|\n") | |
158 | (forward-line 1)) | |
159 | (<= (point) pos)))) | |
160 | (setq arg (1- arg))) | |
161 | (while (< arg 0) | |
162 | (let ((pos (point))) | |
163 | (beginning-of-defun 1) | |
164 | (forward-sexp 1) | |
165 | (forward-line 1) | |
166 | (if (>= (point) pos) | |
167 | (if (beginning-of-defun 2) | |
168 | (progn | |
169 | (forward-list 1) | |
170 | (skip-chars-forward " \t") | |
171 | (if (looking-at "[;\n]") | |
172 | (forward-line 1))) | |
173 | (goto-char (point-min))))) | |
174 | (setq arg (1+ arg))))) | |
175 | ||
176 | (defun mark-defun () | |
177 | "Put mark at end of this defun, point at beginning. | |
178 | The defun marked is the one that contains point or follows point." | |
179 | (interactive) | |
180 | (push-mark (point)) | |
181 | (end-of-defun) | |
182 | (push-mark (point)) | |
183 | (beginning-of-defun) | |
184 | (re-search-backward "^\n" (- (point) 1) t)) | |
185 | ||
186 | (defun insert-parentheses (arg) | |
187 | "Put parentheses around next ARG sexps. Leave point after open-paren. | |
188 | No argument is equivalent to zero: just insert () and leave point between." | |
189 | (interactive "P") | |
a17f9e55 RS |
190 | (if arg (setq arg (prefix-numeric-value arg)) |
191 | (setq arg 0)) | |
192 | (or (eq arg 0) (skip-chars-forward " \t")) | |
b73b9811 | 193 | (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\) )) |
194 | (insert " ")) | |
b73b9811 | 195 | (insert ?\() |
196 | (save-excursion | |
a17f9e55 | 197 | (or (eq arg 0) (forward-sexp arg)) |
b73b9811 | 198 | (insert ?\)) |
a17f9e55 RS |
199 | (and (memq (char-syntax (following-char)) '(?w ?_ ?\( )) |
200 | (insert " ")))) | |
b73b9811 | 201 | |
202 | (defun move-past-close-and-reindent () | |
203 | "Move past next `)', delete indentation before it, then indent after it." | |
204 | (interactive) | |
205 | (up-list 1) | |
206 | (forward-char -1) | |
207 | (while (save-excursion ; this is my contribution | |
208 | (let ((before-paren (point))) | |
209 | (back-to-indentation) | |
210 | (= (point) before-paren))) | |
211 | (delete-indentation)) | |
212 | (forward-char 1) | |
213 | (newline-and-indent)) | |
214 | \f | |
215 | (defun lisp-complete-symbol () | |
e3f99b91 JB |
216 | "Perform completion on Lisp symbol preceding point. That symbol is |
217 | compared against the symbols that exist and any additional characters | |
218 | determined by what is there are inserted. | |
219 | If the symbol starts just after an open-parenthesis, only symbols | |
220 | with function definitions are considered. Otherwise, all symbols with | |
221 | function definitions, values or properties are considered." | |
b73b9811 | 222 | (interactive) |
223 | (let* ((end (point)) | |
224 | (buffer-syntax (syntax-table)) | |
ecb75942 RS |
225 | (beg (unwind-protect |
226 | (save-excursion | |
227 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
228 | (backward-sexp 1) | |
229 | (while (= (char-syntax (following-char)) ?\') | |
230 | (forward-char 1)) | |
231 | (point)) | |
b73b9811 | 232 | (set-syntax-table buffer-syntax))) |
233 | (pattern (buffer-substring beg end)) | |
234 | (predicate | |
235 | (if (eq (char-after (1- beg)) ?\() | |
236 | 'fboundp | |
237 | (function (lambda (sym) | |
238 | (or (boundp sym) (fboundp sym) | |
239 | (symbol-plist sym)))))) | |
240 | (completion (try-completion pattern obarray predicate))) | |
241 | (cond ((eq completion t)) | |
242 | ((null completion) | |
243 | (message "Can't find completion for \"%s\"" pattern) | |
244 | (ding)) | |
245 | ((not (string= pattern completion)) | |
246 | (delete-region beg end) | |
247 | (insert completion)) | |
248 | (t | |
249 | (message "Making completion list...") | |
250 | (let ((list (all-completions pattern obarray predicate))) | |
251 | (or (eq predicate 'fboundp) | |
252 | (let (new) | |
253 | (while list | |
254 | (setq new (cons (if (fboundp (intern (car list))) | |
255 | (list (car list) " <f>") | |
256 | (car list)) | |
257 | new)) | |
258 | (setq list (cdr list))) | |
259 | (setq list (nreverse new)))) | |
260 | (with-output-to-temp-buffer " *Completions*" | |
261 | (display-completion-list list))) | |
262 | (message "Making completion list...%s" "done"))))) |