Mercurial > emacs
annotate lisp/simple.el @ 1091:3302f6e93ce5
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 11 Sep 1992 09:04:24 +0000 |
parents | 25b929c06f83 |
children | e33f6475229a |
rev | line source |
---|---|
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1 ;;; simple.el --- basic editing commands for Emacs |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
2 |
582 | 3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
475 | 4 |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
9 ;; the Free Software Foundation; either version 2, or (at your option) |
475 | 10 ;; any later version. |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
18 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
21 ;;; Code: |
475 | 22 |
23 (defun open-line (arg) | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
24 "Insert a newline and leave point before it. |
1063
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
25 If there is a fill prefix, insert the fill prefix on the new line |
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
26 if the line would have been empty. |
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
27 With arg N, insert N newlines." |
475 | 28 (interactive "*p") |
1063
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
29 (let* ((do-fill-prefix (and fill-prefix (bolp))) |
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
30 (flag (and (null do-fill-prefix) (bolp) (not (bobp))))) |
475 | 31 (if flag (forward-char -1)) |
32 (while (> arg 0) | |
33 (save-excursion | |
1063
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
34 (insert ?\n)) |
25b929c06f83
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1039
diff
changeset
|
35 (if do-fill-prefix (insert fill-prefix)) |
475 | 36 (setq arg (1- arg))) |
37 (if flag (forward-char 1)))) | |
38 | |
39 (defun split-line () | |
40 "Split current line, moving portion beyond point vertically down." | |
41 (interactive "*") | |
42 (skip-chars-forward " \t") | |
43 (let ((col (current-column)) | |
44 (pos (point))) | |
45 (insert ?\n) | |
46 (indent-to col 0) | |
47 (goto-char pos))) | |
48 | |
49 (defun quoted-insert (arg) | |
50 "Read next input character and insert it. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
51 This is useful for inserting control characters. |
475 | 52 You may also type up to 3 octal digits, to insert a character with that code" |
53 (interactive "*p") | |
54 (let ((char (read-quoted-char))) | |
55 (while (> arg 0) | |
56 (insert char) | |
57 (setq arg (1- arg))))) | |
58 | |
59 (defun delete-indentation (&optional arg) | |
60 "Join this line to previous and fix up whitespace at join. | |
892
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
61 If there is a fill prefix, delete it from the beginning of this line. |
475 | 62 With argument, join this line to following line." |
63 (interactive "*P") | |
64 (beginning-of-line) | |
65 (if arg (forward-line 1)) | |
66 (if (eq (preceding-char) ?\n) | |
67 (progn | |
68 (delete-region (point) (1- (point))) | |
892
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
69 ;; If the second line started with the fill prefix, |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
70 ;; delete the prefix. |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
71 (if (and fill-prefix |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
72 (string= fill-prefix |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
73 (buffer-substring (point) |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
74 (+ (point) (length fill-prefix))))) |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
75 (delete-region (point) (+ (point) (length fill-prefix)))) |
475 | 76 (fixup-whitespace)))) |
77 | |
78 (defun fixup-whitespace () | |
79 "Fixup white space between objects around point. | |
80 Leave one space or none, according to the context." | |
81 (interactive "*") | |
82 (save-excursion | |
83 (delete-horizontal-space) | |
84 (if (or (looking-at "^\\|\\s)") | |
85 (save-excursion (forward-char -1) | |
86 (looking-at "$\\|\\s(\\|\\s'"))) | |
87 nil | |
88 (insert ?\ )))) | |
89 | |
90 (defun delete-horizontal-space () | |
91 "Delete all spaces and tabs around point." | |
92 (interactive "*") | |
93 (skip-chars-backward " \t") | |
94 (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
95 | |
96 (defun just-one-space () | |
97 "Delete all spaces and tabs around point, leaving one space." | |
98 (interactive "*") | |
99 (skip-chars-backward " \t") | |
100 (if (= (following-char) ? ) | |
101 (forward-char 1) | |
102 (insert ? )) | |
103 (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
104 | |
105 (defun delete-blank-lines () | |
106 "On blank line, delete all surrounding blank lines, leaving just one. | |
107 On isolated blank line, delete that one. | |
108 On nonblank line, delete all blank lines that follow it." | |
109 (interactive "*") | |
110 (let (thisblank singleblank) | |
111 (save-excursion | |
112 (beginning-of-line) | |
113 (setq thisblank (looking-at "[ \t]*$")) | |
715 | 114 ;; Set singleblank if there is just one blank line here. |
475 | 115 (setq singleblank |
116 (and thisblank | |
117 (not (looking-at "[ \t]*\n[ \t]*$")) | |
118 (or (bobp) | |
119 (progn (forward-line -1) | |
120 (not (looking-at "[ \t]*$"))))))) | |
715 | 121 ;; Delete preceding blank lines, and this one too if it's the only one. |
475 | 122 (if thisblank |
123 (progn | |
124 (beginning-of-line) | |
125 (if singleblank (forward-line 1)) | |
126 (delete-region (point) | |
127 (if (re-search-backward "[^ \t\n]" nil t) | |
128 (progn (forward-line 1) (point)) | |
129 (point-min))))) | |
715 | 130 ;; Delete following blank lines, unless the current line is blank |
131 ;; and there are no following blank lines. | |
475 | 132 (if (not (and thisblank singleblank)) |
133 (save-excursion | |
134 (end-of-line) | |
135 (forward-line 1) | |
136 (delete-region (point) | |
137 (if (re-search-forward "[^ \t\n]" nil t) | |
138 (progn (beginning-of-line) (point)) | |
715 | 139 (point-max))))) |
140 ;; Handle the special case where point is followed by newline and eob. | |
141 ;; Delete the line, leaving point at eob. | |
142 (if (looking-at "^[ \t]*\n\\'") | |
143 (delete-region (point) (point-max))))) | |
475 | 144 |
145 (defun back-to-indentation () | |
146 "Move point to the first non-whitespace character on this line." | |
147 (interactive) | |
148 (beginning-of-line 1) | |
149 (skip-chars-forward " \t")) | |
150 | |
151 (defun newline-and-indent () | |
152 "Insert a newline, then indent according to major mode. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
153 Indentation is done using the value of `indent-line-function'. |
475 | 154 In programming language modes, this is the same as TAB. |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
155 In some text modes, where TAB inserts a tab, this command indents to the |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
156 column specified by the variable `left-margin'." |
475 | 157 (interactive "*") |
158 (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
617 | 159 (newline) |
475 | 160 (indent-according-to-mode)) |
161 | |
162 (defun reindent-then-newline-and-indent () | |
163 "Reindent current line, insert newline, then indent the new line. | |
164 Indentation of both lines is done according to the current major mode, | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
165 which means calling the current value of `indent-line-function'. |
475 | 166 In programming language modes, this is the same as TAB. |
167 In some text modes, where TAB inserts a tab, this indents to the | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
168 column specified by the variable `left-margin'." |
475 | 169 (interactive "*") |
170 (save-excursion | |
171 (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
172 (indent-according-to-mode)) | |
617 | 173 (newline) |
475 | 174 (indent-according-to-mode)) |
175 | |
176 ;; Internal subroutine of delete-char | |
177 (defun kill-forward-chars (arg) | |
178 (if (listp arg) (setq arg (car arg))) | |
179 (if (eq arg '-) (setq arg -1)) | |
180 (kill-region (point) (+ (point) arg))) | |
181 | |
182 ;; Internal subroutine of backward-delete-char | |
183 (defun kill-backward-chars (arg) | |
184 (if (listp arg) (setq arg (car arg))) | |
185 (if (eq arg '-) (setq arg -1)) | |
186 (kill-region (point) (- (point) arg))) | |
187 | |
188 (defun backward-delete-char-untabify (arg &optional killp) | |
189 "Delete characters backward, changing tabs into spaces. | |
190 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. | |
191 Interactively, ARG is the prefix arg (default 1) | |
192 and KILLP is t if prefix arg is was specified." | |
193 (interactive "*p\nP") | |
194 (let ((count arg)) | |
195 (save-excursion | |
196 (while (and (> count 0) (not (bobp))) | |
197 (if (= (preceding-char) ?\t) | |
198 (let ((col (current-column))) | |
199 (forward-char -1) | |
200 (setq col (- col (current-column))) | |
201 (insert-char ?\ col) | |
202 (delete-char 1))) | |
203 (forward-char -1) | |
204 (setq count (1- count))))) | |
205 (delete-backward-char arg killp) | |
206 ;; In overwrite mode, back over columns while clearing them out, | |
207 ;; unless at end of line. | |
208 (and overwrite-mode (not (eolp)) | |
209 (save-excursion (insert-char ?\ arg)))) | |
210 | |
211 (defun zap-to-char (arg char) | |
212 "Kill up to and including ARG'th occurrence of CHAR. | |
213 Goes backward if ARG is negative; error if CHAR not found." | |
214 (interactive "p\ncZap to char: ") | |
215 (kill-region (point) (progn | |
216 (search-forward (char-to-string char) nil nil arg) | |
217 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) | |
218 (point)))) | |
219 | |
220 (defun beginning-of-buffer (&optional arg) | |
221 "Move point to the beginning of the buffer; leave mark at previous position. | |
222 With arg N, put point N/10 of the way from the true beginning. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
223 |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
224 Don't use this command in Lisp programs! |
475 | 225 \(goto-char (point-min)) is faster and avoids clobbering the mark." |
226 (interactive "P") | |
227 (push-mark) | |
228 (goto-char (if arg | |
229 (if (> (buffer-size) 10000) | |
230 ;; Avoid overflow for large buffer sizes! | |
231 (* (prefix-numeric-value arg) | |
232 (/ (buffer-size) 10)) | |
233 (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10)) | |
234 (point-min))) | |
235 (if arg (forward-line 1))) | |
236 | |
237 (defun end-of-buffer (&optional arg) | |
238 "Move point to the end of the buffer; leave mark at previous position. | |
239 With arg N, put point N/10 of the way from the true end. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
240 |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
241 Don't use this command in Lisp programs! |
475 | 242 \(goto-char (point-max)) is faster and avoids clobbering the mark." |
243 (interactive "P") | |
244 (push-mark) | |
245 (goto-char (if arg | |
246 (- (1+ (buffer-size)) | |
247 (if (> (buffer-size) 10000) | |
248 ;; Avoid overflow for large buffer sizes! | |
249 (* (prefix-numeric-value arg) | |
250 (/ (buffer-size) 10)) | |
251 (/ (* (buffer-size) (prefix-numeric-value arg)) 10))) | |
252 (point-max))) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
253 ;; If we went to a place in the middle of the buffer, |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
254 ;; adjust it to the beginning of a line. |
475 | 255 (if arg (forward-line 1) |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
256 ;; If the end of the buffer is not already on the screen, |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
257 ;; then scroll specially to put it near, but not at, the bottom. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
258 (if (let ((old-point (point))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
259 (save-excursion |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
260 (goto-char (window-start)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
261 (vertical-motion (window-height)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
262 (< (point) old-point))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
263 (recenter -3)))) |
475 | 264 |
265 (defun mark-whole-buffer () | |
715 | 266 "Put point at beginning and mark at end of buffer. |
267 You probably should not use this function in Lisp programs; | |
268 it is usually a mistake for a Lisp function to use any subroutine | |
269 that uses or sets the mark." | |
475 | 270 (interactive) |
271 (push-mark (point)) | |
272 (push-mark (point-max)) | |
273 (goto-char (point-min))) | |
274 | |
275 (defun count-lines-region (start end) | |
276 "Print number of lines and charcters in the region." | |
277 (interactive "r") | |
278 (message "Region has %d lines, %d characters" | |
279 (count-lines start end) (- end start))) | |
280 | |
281 (defun what-line () | |
282 "Print the current line number (in the buffer) of point." | |
283 (interactive) | |
284 (save-restriction | |
285 (widen) | |
286 (save-excursion | |
287 (beginning-of-line) | |
288 (message "Line %d" | |
289 (1+ (count-lines 1 (point))))))) | |
290 | |
291 (defun count-lines (start end) | |
292 "Return number of lines between START and END. | |
293 This is usually the number of newlines between them, | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
294 but can be one more if START is not equal to END |
475 | 295 and the greater of them is not at the start of a line." |
296 (save-excursion | |
297 (save-restriction | |
298 (narrow-to-region start end) | |
299 (goto-char (point-min)) | |
300 (if (eq selective-display t) | |
301 (let ((done 0)) | |
302 (while (re-search-forward "[\n\C-m]" nil t 40) | |
303 (setq done (+ 40 done))) | |
304 (while (re-search-forward "[\n\C-m]" nil t 1) | |
305 (setq done (+ 1 done))) | |
306 done) | |
307 (- (buffer-size) (forward-line (buffer-size))))))) | |
308 | |
309 (defun what-cursor-position () | |
310 "Print info on cursor position (on screen and within buffer)." | |
311 (interactive) | |
312 (let* ((char (following-char)) | |
313 (beg (point-min)) | |
314 (end (point-max)) | |
315 (pos (point)) | |
316 (total (buffer-size)) | |
317 (percent (if (> total 50000) | |
318 ;; Avoid overflow from multiplying by 100! | |
319 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
320 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
321 (hscroll (if (= (window-hscroll) 0) | |
322 "" | |
323 (format " Hscroll=%d" (window-hscroll)))) | |
324 (col (current-column))) | |
325 (if (= pos end) | |
326 (if (or (/= beg 1) (/= end (1+ total))) | |
327 (message "point=%d of %d(%d%%) <%d - %d> column %d %s" | |
328 pos total percent beg end col hscroll) | |
329 (message "point=%d of %d(%d%%) column %d %s" | |
330 pos total percent col hscroll)) | |
331 (if (or (/= beg 1) (/= end (1+ total))) | |
332 (message "Char: %s (0%o) point=%d of %d(%d%%) <%d - %d> column %d %s" | |
333 (single-key-description char) char pos total percent beg end col hscroll) | |
334 (message "Char: %s (0%o) point=%d of %d(%d%%) column %d %s" | |
335 (single-key-description char) char pos total percent col hscroll))))) | |
336 | |
337 (defun fundamental-mode () | |
338 "Major mode not specialized for anything in particular. | |
339 Other major modes are defined by comparison with this one." | |
340 (interactive) | |
341 (kill-all-local-variables)) | |
342 | |
343 (put 'eval-expression 'disabled t) | |
344 | |
345 ;; We define this, rather than making eval interactive, | |
346 ;; for the sake of completion of names like eval-region, eval-current-buffer. | |
347 (defun eval-expression (expression) | |
348 "Evaluate EXPRESSION and print value in minibuffer. | |
954
9e51bb887797
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
921
diff
changeset
|
349 Value is also consed on to front of the variable `values'." |
475 | 350 (interactive "xEval: ") |
351 (setq values (cons (eval expression) values)) | |
352 (prin1 (car values) t)) | |
353 | |
354 (defun edit-and-eval-command (prompt command) | |
355 "Prompting with PROMPT, let user edit COMMAND and eval result. | |
356 COMMAND is a Lisp expression. Let user edit that expression in | |
357 the minibuffer, then read and evaluate the result." | |
358 (let ((command (read-minibuffer prompt | |
359 (prin1-to-string command)))) | |
360 ;; Add edited command to command history, unless redundant. | |
361 (or (equal command (car command-history)) | |
362 (setq command-history (cons command command-history))) | |
363 (eval command))) | |
364 | |
874
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
365 (defun repeat-complex-command (arg) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
366 "Edit and re-evaluate last complex command, or ARGth from last. |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
367 A complex command is one which used the minibuffer. |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
368 The command is placed in the minibuffer as a Lisp form for editing. |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
369 The result is executed, repeating the command as changed. |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
370 If the command has been changed or is not the most recent previous command |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
371 it is added to the front of the command history. |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
372 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
373 to get different commands to edit and resubmit." |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
374 (interactive "p") |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
375 (let ((elt (nth (1- arg) command-history)) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
376 (minibuffer-history-position arg) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
377 (minibuffer-history-sexp-flag t) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
378 newcmd) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
379 (if elt |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
380 (let ((minibuffer-history-variable ' command-history)) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
381 (setq newcmd (read-from-minibuffer "Redo: " |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
382 (prin1-to-string elt) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
383 minibuffer-local-map |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
384 t |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
385 (cons 'command-history |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
386 arg))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
387 ;; If command to be redone does not match front of history, |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
388 ;; add it to the history. |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
389 (or (equal newcmd (car command-history)) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
390 (setq command-history (cons newcmd command-history))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
391 (eval newcmd)) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
392 (ding)))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
393 |
858
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
394 (defvar minibuffer-history nil) |
862
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
395 (defvar minibuffer-history-sexp-flag nil) |
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
396 (setq minibuffer-history-variable 'minibuffer-history) |
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
397 (setq minibuffer-history-position nil) |
858
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
398 |
921 | 399 (mapcar |
400 (function (lambda (key-and-command) | |
401 (mapcar | |
402 (function (lambda (keymap) | |
403 (define-key (symbol-value keymap) | |
404 (car key-and-command) | |
405 (cdr key-and-command)))) | |
406 '(minibuffer-local-map | |
407 minibuffer-local-ns-map | |
408 minibuffer-local-completion-map | |
409 minibuffer-local-must-match-map)))) | |
410 '(("\en" . next-history-element) ([next] . next-history-element) | |
411 ("\ep" . previous-history-element) ([prior] . previous-history-element) | |
412 ("\er" . previous-matching-history-element) | |
413 ("\es" . next-matching-history-element))) | |
874
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
414 |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
415 (put 'previous-matching-history-element 'enable-recursive-minibuffers t) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
416 (defun previous-matching-history-element (regexp n) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
417 (interactive "sPrevious element matching (regexp): \np") |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
418 (let ((history (symbol-value minibuffer-history-variable)) |
892
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
419 prevpos |
874
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
420 (pos minibuffer-history-position)) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
421 (while (/= n 0) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
422 (setq prevpos pos) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
423 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
424 (if (= pos prevpos) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
425 (error (if (= pos 1) |
892
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
426 "No later matching history item" |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
427 "No earlier matching history item"))) |
874
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
428 (if (string-match regexp |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
429 (if minibuffer-history-sexp-flag |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
430 (prin1-to-string (nth (1- pos) history)) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
431 (nth (1- pos) history))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
432 (setq n (+ n (if (< n 0) -1 1))))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
433 (setq minibuffer-history-position pos) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
434 (erase-buffer) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
435 (let ((elt (nth (1- pos) history))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
436 (insert (if minibuffer-history-sexp-flag |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
437 (prin1-to-string elt) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
438 elt))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
439 (goto-char (point-min)))) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
440 |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
441 (put 'next-matching-history-element 'enable-recursive-minibuffers t) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
442 (defun next-matching-history-element (regexp n) |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
443 (interactive "sNext element matching (regexp): \np") |
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
444 (previous-matching-history-element regexp (- n))) |
475 | 445 |
858
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
446 (defun next-history-element (n) |
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
447 "Insert the next element of the minibuffer history into the minibuffer." |
475 | 448 (interactive "p") |
858
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
449 (let ((narg (min (max 1 (- minibuffer-history-position n)) |
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
450 (length (symbol-value minibuffer-history-variable))))) |
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
451 (if (= minibuffer-history-position narg) |
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
452 (error (if (= minibuffer-history-position 1) |
892
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
453 "End of history; no next item" |
3a9943a4a440
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
874
diff
changeset
|
454 "Beginning of history; no preceding item")) |
475 | 455 (erase-buffer) |
858
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
456 (setq minibuffer-history-position narg) |
862
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
457 (let ((elt (nth (1- minibuffer-history-position) |
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
458 (symbol-value minibuffer-history-variable)))) |
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
459 (insert |
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
460 (if minibuffer-history-sexp-flag |
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
859
diff
changeset
|
461 (prin1-to-string elt) |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
862
diff
changeset
|
462 elt))) |
475 | 463 (goto-char (point-min))))) |
464 | |
858
b11800dc877d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
465 (defun previous-history-element (n) |
475 | 466 "Inserts the previous element of `command-history' into the minibuffer." |
467 (interactive "p") | |
859
5f325fbc093d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
858
diff
changeset
|
468 (next-history-element (- n))) |
874
b945f592b94d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
469 |
475 | 470 (defun goto-line (arg) |
471 "Goto line ARG, counting from line 1 at beginning of buffer." | |
472 (interactive "NGoto line: ") | |
473 (save-restriction | |
474 (widen) | |
475 (goto-char 1) | |
476 (if (eq selective-display t) | |
477 (re-search-forward "[\n\C-m]" nil 'end (1- arg)) | |
478 (forward-line (1- arg))))) | |
479 | |
480 ;Put this on C-x u, so we can force that rather than C-_ into startup msg | |
481 (fset 'advertised-undo 'undo) | |
482 | |
483 (defun undo (&optional arg) | |
484 "Undo some previous changes. | |
485 Repeat this command to undo more changes. | |
486 A numeric argument serves as a repeat count." | |
487 (interactive "*p") | |
488 (let ((modified (buffer-modified-p))) | |
582 | 489 (or (eq (selected-window) (minibuffer-window)) |
490 (message "Undo!")) | |
475 | 491 (or (eq last-command 'undo) |
492 (progn (undo-start) | |
493 (undo-more 1))) | |
494 (setq this-command 'undo) | |
495 (undo-more (or arg 1)) | |
496 (and modified (not (buffer-modified-p)) | |
497 (delete-auto-save-file-if-necessary)))) | |
498 | |
499 (defun undo-start () | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
500 "Set `pending-undo-list' to the front of the undo list. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
501 The next call to `undo-more' will undo the most recently made change." |
475 | 502 (if (eq buffer-undo-list t) |
503 (error "No undo information in this buffer")) | |
504 (setq pending-undo-list buffer-undo-list)) | |
505 | |
506 (defun undo-more (count) | |
507 "Undo back N undo-boundaries beyond what was already undone recently. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
508 Call `undo-start' to get ready to undo recent changes, |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
509 then call `undo-more' one or more times to undo them." |
475 | 510 (or pending-undo-list |
511 (error "No further undo information")) | |
512 (setq pending-undo-list (primitive-undo count pending-undo-list))) | |
513 | |
514 (defvar last-shell-command "") | |
515 (defvar last-shell-command-on-region "") | |
516 | |
517 (defun shell-command (command &optional flag) | |
518 "Execute string COMMAND in inferior shell; display output, if any. | |
519 If COMMAND ends in ampersand, execute it asynchronously. | |
520 | |
521 Optional second arg non-nil (prefix arg, if interactive) | |
522 means insert output in current buffer after point (leave mark after it). | |
523 This cannot be done asynchronously." | |
524 (interactive (list (read-string "Shell command: " last-shell-command) | |
525 current-prefix-arg)) | |
526 (if flag | |
527 (progn (barf-if-buffer-read-only) | |
528 (push-mark) | |
529 ;; We do not use -f for csh; we will not support broken use of | |
530 ;; .cshrcs. Even the BSD csh manual says to use | |
531 ;; "if ($?prompt) exit" before things which are not useful | |
532 ;; non-interactively. Besides, if someone wants their other | |
533 ;; aliases for shell commands then they can still have them. | |
534 (call-process shell-file-name nil t nil | |
535 "-c" command) | |
536 (exchange-point-and-mark)) | |
537 ;; Preserve the match data in case called from a program. | |
538 (let ((data (match-data))) | |
539 (unwind-protect | |
540 (if (string-match "[ \t]*&[ \t]*$" command) | |
541 ;; Command ending with ampersand means asynchronous. | |
542 (let ((buffer (get-buffer-create "*shell-command*")) | |
543 (directory default-directory) | |
544 proc) | |
545 ;; Remove the ampersand. | |
546 (setq command (substring command 0 (match-beginning 0))) | |
547 ;; If will kill a process, query first. | |
548 (setq proc (get-buffer-process buffer)) | |
549 (if proc | |
550 (if (yes-or-no-p "A command is running. Kill it? ") | |
551 (kill-process proc) | |
552 (error "Shell command in progress"))) | |
553 (save-excursion | |
554 (set-buffer buffer) | |
555 (erase-buffer) | |
556 (display-buffer buffer) | |
557 (setq default-directory directory) | |
558 (setq proc (start-process "Shell" buffer | |
559 shell-file-name "-c" command)) | |
560 (setq mode-line-process '(": %s")) | |
561 (set-process-sentinel proc 'shell-command-sentinel) | |
562 (set-process-filter proc 'shell-command-filter) | |
563 )) | |
564 (shell-command-on-region (point) (point) command nil)) | |
565 (store-match-data data))))) | |
566 | |
567 ;; We have a sentinel to prevent insertion of a termination message | |
568 ;; in the buffer itself. | |
569 (defun shell-command-sentinel (process signal) | |
570 (if (memq (process-status process) '(exit signal)) | |
571 (progn | |
572 (message "%s: %s." | |
573 (car (cdr (cdr (process-command process)))) | |
574 (substring signal 0 -1)) | |
575 (save-excursion | |
576 (set-buffer (process-buffer process)) | |
577 (setq mode-line-process nil)) | |
578 (delete-process process)))) | |
579 | |
580 (defun shell-command-filter (proc string) | |
581 ;; Do save-excursion by hand so that we can leave point numerically unchanged | |
582 ;; despite an insertion immediately after it. | |
583 (let* ((obuf (current-buffer)) | |
584 (buffer (process-buffer proc)) | |
585 opoint | |
586 (window (get-buffer-window buffer)) | |
587 (pos (window-start window))) | |
588 (unwind-protect | |
589 (progn | |
590 (set-buffer buffer) | |
591 (setq opoint (point)) | |
592 (goto-char (point-max)) | |
593 (insert-before-markers string)) | |
594 ;; insert-before-markers moved this marker: set it back. | |
595 (set-window-start window pos) | |
596 ;; Finish our save-excursion. | |
597 (goto-char opoint) | |
598 (set-buffer obuf)))) | |
599 | |
600 (defun shell-command-on-region (start end command &optional flag interactive) | |
601 "Execute string COMMAND in inferior shell with region as input. | |
602 Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
603 Prefix arg means replace the region with it. | |
604 Noninteractive args are START, END, COMMAND, FLAG. | |
605 Noninteractively FLAG means insert output in place of text from START to END, | |
606 and put point at the end, but don't alter the mark. | |
607 | |
608 If the output is one line, it is displayed in the echo area, | |
609 but it is nonetheless available in buffer `*Shell Command Output*' | |
610 even though that buffer is not automatically displayed. If there is no output | |
611 or output is inserted in the current buffer then `*Shell Command Output*' is | |
612 deleted." | |
613 (interactive (list (min (point) (mark)) (max (point) (mark)) | |
614 (read-string "Shell command on region: " | |
615 last-shell-command-on-region) | |
616 current-prefix-arg | |
617 (prefix-numeric-value current-prefix-arg))) | |
618 (if flag | |
619 ;; Replace specified region with output from command. | |
620 (let ((swap (and interactive (< (point) (mark))))) | |
621 ;; Don't muck with mark | |
622 ;; unless called interactively. | |
623 (and interactive (push-mark)) | |
624 (call-process-region start end shell-file-name t t nil | |
625 "-c" command) | |
626 (if (get-buffer "*Shell Command Output*") | |
627 (kill-buffer "*Shell Command Output*")) | |
628 (and interactive swap (exchange-point-and-mark))) | |
629 ;; No prefix argument: put the output in a temp buffer, | |
630 ;; replacing its entire contents. | |
631 (let ((buffer (get-buffer-create "*Shell Command Output*"))) | |
632 (if (eq buffer (current-buffer)) | |
633 ;; If the input is the same buffer as the output, | |
634 ;; delete everything but the specified region, | |
635 ;; then replace that region with the output. | |
636 (progn (delete-region end (point-max)) | |
637 (delete-region (point-min) start) | |
638 (call-process-region (point-min) (point-max) | |
639 shell-file-name t t nil | |
640 "-c" command)) | |
641 ;; Clear the output buffer, then run the command with output there. | |
642 (save-excursion | |
643 (set-buffer buffer) | |
644 (erase-buffer)) | |
645 (call-process-region start end shell-file-name | |
646 nil buffer nil | |
647 "-c" command)) | |
648 ;; Report the amount of output. | |
649 (let ((lines (save-excursion | |
650 (set-buffer buffer) | |
651 (if (= (buffer-size) 0) | |
652 0 | |
653 (count-lines (point-min) (point-max)))))) | |
654 (cond ((= lines 0) | |
655 (message "(Shell command completed with no output)") | |
656 (kill-buffer "*Shell Command Output*")) | |
657 ((= lines 1) | |
658 (message "%s" | |
659 (save-excursion | |
660 (set-buffer buffer) | |
661 (goto-char (point-min)) | |
662 (buffer-substring (point) | |
663 (progn (end-of-line) (point)))))) | |
664 (t | |
665 (set-window-start (display-buffer buffer) 1))))))) | |
666 | |
667 (defun universal-argument () | |
668 "Begin a numeric argument for the following command. | |
669 Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
670 \\[universal-argument] following the digits or minus sign ends the argument. | |
671 \\[universal-argument] without digits or minus sign provides 4 as argument. | |
672 Repeating \\[universal-argument] without digits or minus sign | |
673 multiplies the argument by 4 each time." | |
674 (interactive nil) | |
513 | 675 (let ((factor 4) |
676 key) | |
715 | 677 ;; (describe-arg (list factor) 1) |
678 (setq key (read-key-sequence nil t)) | |
513 | 679 (while (equal (key-binding key) 'universal-argument) |
680 (setq factor (* 4 factor)) | |
715 | 681 ;; (describe-arg (list factor) 1) |
682 (setq key (read-key-sequence nil t))) | |
513 | 683 (prefix-arg-internal key factor nil))) |
475 | 684 |
513 | 685 (defun prefix-arg-internal (key factor value) |
475 | 686 (let ((sign 1)) |
687 (if (and (numberp value) (< value 0)) | |
688 (setq sign -1 value (- value))) | |
689 (if (eq value '-) | |
690 (setq sign -1 value nil)) | |
715 | 691 ;; (describe-arg value sign) |
513 | 692 (while (equal key "-") |
693 (setq sign (- sign) factor nil) | |
715 | 694 ;; (describe-arg value sign) |
695 (setq key (read-key-sequence nil t))) | |
1039 | 696 (while (and (stringp key) |
697 (= (length key) 1) | |
513 | 698 (not (string< key "0")) |
699 (not (string< "9" key))) | |
700 (setq value (+ (* (if (numberp value) value 0) 10) | |
701 (- (aref key 0) ?0)) | |
702 factor nil) | |
715 | 703 ;; (describe-arg value sign) |
704 (setq key (read-key-sequence nil t))) | |
475 | 705 (setq prefix-arg |
513 | 706 (cond (factor (list factor)) |
475 | 707 ((numberp value) (* value sign)) |
708 ((= sign -1) '-))) | |
513 | 709 ;; Calling universal-argument after digits |
710 ;; terminates the argument but is ignored. | |
711 (if (eq (key-binding key) 'universal-argument) | |
712 (progn | |
713 (describe-arg value sign) | |
715 | 714 (setq key (read-key-sequence nil t)))) |
513 | 715 (if (= (length key) 1) |
716 ;; Make sure self-insert-command finds the proper character; | |
717 ;; unread the character and let the command loop process it. | |
718 (setq unread-command-char (string-to-char key)) | |
719 ;; We can't push back a longer string, so we'll emulate the | |
720 ;; command loop ourselves. | |
721 (command-execute (key-binding key))))) | |
475 | 722 |
513 | 723 (defun describe-arg (value sign) |
724 (cond ((numberp value) | |
725 (message "Arg: %d" (* value sign))) | |
726 ((consp value) | |
727 (message "Arg: [%d]" (car value))) | |
728 ((< sign 0) | |
729 (message "Arg: -")))) | |
475 | 730 |
731 (defun digit-argument (arg) | |
732 "Part of the numeric argument for the next command. | |
733 \\[universal-argument] following digits or minus sign ends the argument." | |
734 (interactive "P") | |
513 | 735 (prefix-arg-internal (char-to-string (logand last-command-char ?\177)) |
736 nil arg)) | |
475 | 737 |
738 (defun negative-argument (arg) | |
739 "Begin a negative numeric argument for the next command. | |
740 \\[universal-argument] following digits or minus sign ends the argument." | |
741 (interactive "P") | |
513 | 742 (prefix-arg-internal "-" nil arg)) |
475 | 743 |
744 (defun forward-to-indentation (arg) | |
745 "Move forward ARG lines and position at first nonblank character." | |
746 (interactive "p") | |
747 (forward-line arg) | |
748 (skip-chars-forward " \t")) | |
749 | |
750 (defun backward-to-indentation (arg) | |
751 "Move backward ARG lines and position at first nonblank character." | |
752 (interactive "p") | |
753 (forward-line (- arg)) | |
754 (skip-chars-forward " \t")) | |
755 | |
756 (defun kill-line (&optional arg) | |
757 "Kill the rest of the current line; if no nonblanks there, kill thru newline. | |
758 With prefix argument, kill that many lines from point. | |
759 Negative arguments kill lines backward. | |
760 | |
761 When calling from a program, nil means \"no arg\", | |
762 a number counts as a prefix arg." | |
763 (interactive "P") | |
764 (kill-region (point) | |
765 (progn | |
766 (if arg | |
767 (forward-line (prefix-numeric-value arg)) | |
768 (if (eobp) | |
769 (signal 'end-of-buffer nil)) | |
770 (if (looking-at "[ \t]*$") | |
771 (forward-line 1) | |
772 (end-of-line))) | |
773 (point)))) | |
774 | |
715 | 775 ;;;; Window system cut and paste hooks. |
776 | |
777 (defvar interprogram-cut-function nil | |
778 "Function to call to make a killed region available to other programs. | |
779 | |
780 Most window systems provide some sort of facility for cutting and | |
781 pasting text between the windows of different programs. On startup, | |
782 this variable is set to a function which emacs will call whenever text | |
783 is put in the kill ring to make the new kill available to other | |
784 programs. | |
785 | |
786 The function takes one argument, TEXT, which is a string containing | |
787 the text which should be made available.") | |
788 | |
789 (defvar interprogram-paste-function nil | |
790 "Function to call to get text cut from other programs. | |
791 | |
792 Most window systems provide some sort of facility for cutting and | |
793 pasting text between the windows of different programs. On startup, | |
794 this variable is set to a function which emacs will call to obtain | |
795 text that other programs have provided for pasting. | |
796 | |
797 The function should be called with no arguments. If the function | |
798 returns nil, then no other program has provided such text, and the top | |
799 of the Emacs kill ring should be used. If the function returns a | |
727 | 800 string, that string should be put in the kill ring as the latest kill. |
801 | |
802 Note that the function should return a string only if a program other | |
803 than Emacs has provided a string for pasting; if Emacs provided the | |
804 most recent string, the function should return nil. If it is | |
805 difficult to tell whether Emacs or some other program provided the | |
806 current string, it is probably good enough to return nil if the string | |
807 is equal (according to `string=') to the last text Emacs provided.") | |
715 | 808 |
809 | |
810 | |
811 ;;;; The kill ring data structure. | |
475 | 812 |
813 (defvar kill-ring nil | |
715 | 814 "List of killed text sequences. |
815 Since the kill ring is supposed to interact nicely with cut-and-paste | |
816 facilities offered by window systems, use of this variable should | |
817 interact nicely with `interprogram-cut-function' and | |
818 `interprogram-paste-function'. The functions `kill-new', | |
819 `kill-append', and `current-kill' are supposed to implement this | |
820 interaction; you may want to use them instead of manipulating the kill | |
821 ring directly.") | |
475 | 822 |
823 (defconst kill-ring-max 30 | |
824 "*Maximum length of kill ring before oldest elements are thrown away.") | |
825 | |
826 (defvar kill-ring-yank-pointer nil | |
827 "The tail of the kill ring whose car is the last thing yanked.") | |
828 | |
715 | 829 (defun kill-new (string) |
830 "Make STRING the latest kill in the kill ring. | |
831 Set the kill-ring-yank pointer to point to it. | |
832 If `interprogram-cut-function' is non-nil, apply it to STRING." | |
833 (setq kill-ring (cons string kill-ring)) | |
834 (if (> (length kill-ring) kill-ring-max) | |
835 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)) | |
836 (setq kill-ring-yank-pointer kill-ring) | |
837 (if interprogram-cut-function | |
838 (funcall interprogram-cut-function string))) | |
839 | |
475 | 840 (defun kill-append (string before-p) |
715 | 841 "Append STRING to the end of the latest kill in the kill ring. |
842 If BEFORE-P is non-nil, prepend STRING to the kill. | |
843 If 'interprogram-cut-function' is set, pass the resulting kill to | |
844 it." | |
475 | 845 (setcar kill-ring |
846 (if before-p | |
847 (concat string (car kill-ring)) | |
715 | 848 (concat (car kill-ring) string))) |
849 (if interprogram-cut-function | |
850 (funcall interprogram-cut-function (car kill-ring)))) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
851 |
715 | 852 (defun current-kill (n &optional do-not-move) |
853 "Rotate the yanking point by N places, and then return that kill. | |
854 If N is zero, `interprogram-paste-function' is set, and calling it | |
855 returns a string, then that string is added to the front of the | |
856 kill ring and returned as the latest kill. | |
857 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the | |
858 yanking point; just return the Nth kill forward." | |
859 (let ((interprogram-paste (and (= n 0) | |
860 interprogram-paste-function | |
861 (funcall interprogram-paste-function)))) | |
862 (if interprogram-paste | |
863 (progn | |
864 ;; Disable the interprogram cut function when we add the new | |
865 ;; text to the kill ring, so Emacs doesn't try to own the | |
866 ;; selection, with identical text. | |
867 (let ((interprogram-cut-function nil)) | |
868 (kill-new interprogram-paste)) | |
869 interprogram-paste) | |
870 (or kill-ring (error "Kill ring is empty")) | |
871 (let* ((length (length kill-ring)) | |
872 (ARGth-kill-element | |
873 (nthcdr (% (+ n (- length (length kill-ring-yank-pointer))) | |
874 length) | |
875 kill-ring))) | |
876 (or do-not-move | |
877 (setq kill-ring-yank-pointer ARGth-kill-element)) | |
878 (car ARGth-kill-element))))) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
879 |
715 | 880 |
881 | |
882 ;;;; Commands for manipulating the kill ring. | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
883 |
475 | 884 (defun kill-region (beg end) |
885 "Kill between point and mark. | |
886 The text is deleted but saved in the kill ring. | |
887 The command \\[yank] can retrieve it from there. | |
888 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].) | |
889 | |
890 This is the primitive for programs to kill text (as opposed to deleting it). | |
891 Supply two arguments, character numbers indicating the stretch of text | |
892 to be killed. | |
893 Any command that calls this function is a \"kill command\". | |
894 If the previous command was also a kill command, | |
895 the text killed this time appends to the text killed last time | |
896 to make one entry in the kill ring." | |
897 (interactive "r") | |
715 | 898 (cond |
899 (buffer-read-only | |
900 (copy-region-as-kill beg end)) | |
901 ((not (or (eq buffer-undo-list t) | |
902 (eq last-command 'kill-region) | |
903 (eq beg end))) | |
904 ;; Don't let the undo list be truncated before we can even access it. | |
762 | 905 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))) |
715 | 906 (delete-region beg end) |
907 ;; Take the same string recorded for undo | |
908 ;; and put it in the kill-ring. | |
909 (kill-new (car (car buffer-undo-list))) | |
910 (setq this-command 'kill-region))) | |
911 (t | |
475 | 912 (copy-region-as-kill beg end) |
715 | 913 (delete-region beg end)))) |
475 | 914 |
915 (defun copy-region-as-kill (beg end) | |
916 "Save the region as if killed, but don't kill it. | |
617 | 917 If `interprogram-cut-function' is non-nil, also save the text for a window |
918 system cut and paste." | |
475 | 919 (interactive "r") |
920 (if (eq last-command 'kill-region) | |
921 (kill-append (buffer-substring beg end) (< end beg)) | |
715 | 922 (kill-new (buffer-substring beg end))) |
923 (setq this-command 'kill-region) | |
475 | 924 nil) |
925 | |
926 (defun kill-ring-save (beg end) | |
927 "Save the region as if killed, but don't kill it." | |
928 (interactive "r") | |
929 (copy-region-as-kill beg end) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
930 (if (interactive-p) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
931 (save-excursion |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
932 (let ((other-end (if (= (point) beg) end beg))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
933 (if (pos-visible-in-window-p other-end (selected-window)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
934 (progn |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
935 (goto-char other-end) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
936 (sit-for 1)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
937 (let* ((killed-text (current-kill 0)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
938 (message-len (min (length killed-text) 40))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
939 (if (= (point) beg) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
940 ;; Don't say "killed"; that is misleading. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
941 (message "Saved text until \"%s\"" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
942 (substring killed-text (- message-len))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
943 (message "Saved text from \"%s\"" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
944 (substring killed-text 0 message-len))))))))) |
475 | 945 |
946 (defun append-next-kill () | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
947 "Cause following command, if it kills, to append to previous kill." |
475 | 948 (interactive) |
949 (if (interactive-p) | |
950 (progn | |
951 (setq this-command 'kill-region) | |
952 (message "If the next command is a kill, it will append")) | |
953 (setq last-command 'kill-region))) | |
954 | |
955 (defun yank-pop (arg) | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
956 "Replace just-yanked stretch of killed text with a different stretch. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
957 This command is allowed only immediately after a `yank' or a `yank-pop'. |
475 | 958 At such a time, the region contains a stretch of reinserted |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
959 previously-killed text. `yank-pop' deletes that text and inserts in its |
475 | 960 place a different stretch of killed text. |
961 | |
962 With no argument, the previous kill is inserted. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
963 With argument N, insert the Nth previous kill. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
964 If N is negative, this is a more recent kill. |
475 | 965 |
966 The sequence of kills wraps around, so that after the oldest one | |
967 comes the newest one." | |
968 (interactive "*p") | |
969 (if (not (eq last-command 'yank)) | |
970 (error "Previous command was not a yank")) | |
971 (setq this-command 'yank) | |
972 (let ((before (< (point) (mark)))) | |
973 (delete-region (point) (mark)) | |
974 (set-mark (point)) | |
715 | 975 (insert (current-kill arg)) |
475 | 976 (if before (exchange-point-and-mark)))) |
977 | |
978 (defun yank (&optional arg) | |
979 "Reinsert the last stretch of killed text. | |
980 More precisely, reinsert the stretch of killed text most recently | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
981 killed OR yanked. Put point at end, and set mark at beginning. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
982 With just C-u as argument, same but put point at beginning (and mark at end). |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
983 With argument N, reinsert the Nth most recently killed stretch of killed |
475 | 984 text. |
985 See also the command \\[yank-pop]." | |
986 (interactive "*P") | |
987 (push-mark (point)) | |
715 | 988 (insert (current-kill (cond |
989 ((listp arg) 0) | |
990 ((eq arg '-) -1) | |
991 (t (1- arg))))) | |
475 | 992 (if (consp arg) |
993 (exchange-point-and-mark))) | |
715 | 994 |
995 (defun rotate-yank-pointer (arg) | |
996 "Rotate the yanking point in the kill ring. | |
997 With argument, rotate that many kills forward (or backward, if negative)." | |
998 (interactive "p") | |
999 (current-kill arg)) | |
1000 | |
475 | 1001 |
1002 (defun insert-buffer (buffer) | |
1003 "Insert after point the contents of BUFFER. | |
1004 Puts mark after the inserted text. | |
1005 BUFFER may be a buffer or a buffer name." | |
1006 (interactive (list (read-buffer "Insert buffer: " (other-buffer) t))) | |
1007 (or (bufferp buffer) | |
1008 (setq buffer (get-buffer buffer))) | |
1009 (let (start end newmark) | |
1010 (save-excursion | |
1011 (save-excursion | |
1012 (set-buffer buffer) | |
1013 (setq start (point-min) end (point-max))) | |
1014 (insert-buffer-substring buffer start end) | |
1015 (setq newmark (point))) | |
1016 (push-mark newmark))) | |
1017 | |
1018 (defun append-to-buffer (buffer start end) | |
1019 "Append to specified buffer the text of the region. | |
1020 It is inserted into that buffer before its point. | |
1021 | |
1022 When calling from a program, give three arguments: | |
1023 BUFFER (or buffer name), START and END. | |
1024 START and END specify the portion of the current buffer to be copied." | |
715 | 1025 (interactive |
1026 (list (read-buffer "Append to buffer: " (other-buffer nil t) t))) | |
475 | 1027 (let ((oldbuf (current-buffer))) |
1028 (save-excursion | |
1029 (set-buffer (get-buffer-create buffer)) | |
1030 (insert-buffer-substring oldbuf start end)))) | |
1031 | |
1032 (defun prepend-to-buffer (buffer start end) | |
1033 "Prepend to specified buffer the text of the region. | |
1034 It is inserted into that buffer after its point. | |
1035 | |
1036 When calling from a program, give three arguments: | |
1037 BUFFER (or buffer name), START and END. | |
1038 START and END specify the portion of the current buffer to be copied." | |
1039 (interactive "BPrepend to buffer: \nr") | |
1040 (let ((oldbuf (current-buffer))) | |
1041 (save-excursion | |
1042 (set-buffer (get-buffer-create buffer)) | |
1043 (save-excursion | |
1044 (insert-buffer-substring oldbuf start end))))) | |
1045 | |
1046 (defun copy-to-buffer (buffer start end) | |
1047 "Copy to specified buffer the text of the region. | |
1048 It is inserted into that buffer, replacing existing text there. | |
1049 | |
1050 When calling from a program, give three arguments: | |
1051 BUFFER (or buffer name), START and END. | |
1052 START and END specify the portion of the current buffer to be copied." | |
1053 (interactive "BCopy to buffer: \nr") | |
1054 (let ((oldbuf (current-buffer))) | |
1055 (save-excursion | |
1056 (set-buffer (get-buffer-create buffer)) | |
1057 (erase-buffer) | |
1058 (save-excursion | |
1059 (insert-buffer-substring oldbuf start end))))) | |
1060 | |
1061 (defun mark () | |
1062 "Return this buffer's mark value as integer, or nil if no mark. | |
1063 If you are using this in an editing command, you are most likely making | |
1064 a mistake; see the documentation of `set-mark'." | |
1065 (marker-position (mark-marker))) | |
1066 | |
1067 (defun set-mark (pos) | |
1068 "Set this buffer's mark to POS. Don't use this function! | |
1069 That is to say, don't use this function unless you want | |
1070 the user to see that the mark has moved, and you want the previous | |
1071 mark position to be lost. | |
1072 | |
1073 Normally, when a new mark is set, the old one should go on the stack. | |
1074 This is why most applications should use push-mark, not set-mark. | |
1075 | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1076 Novice Emacs Lisp programmers often try to use the mark for the wrong |
475 | 1077 purposes. The mark saves a location for the user's convenience. |
1078 Most editing commands should not alter the mark. | |
1079 To remember a location for internal use in the Lisp program, | |
1080 store it in a Lisp variable. Example: | |
1081 | |
1082 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
1083 | |
1084 (set-marker (mark-marker) pos (current-buffer))) | |
1085 | |
1086 (defvar mark-ring nil | |
1087 "The list of saved former marks of the current buffer, | |
1088 most recent first.") | |
1089 (make-variable-buffer-local 'mark-ring) | |
1090 | |
1091 (defconst mark-ring-max 16 | |
1092 "*Maximum size of mark ring. Start discarding off end if gets this big.") | |
1093 | |
1094 (defun set-mark-command (arg) | |
1095 "Set mark at where point is, or jump to mark. | |
1096 With no prefix argument, set mark, and push previous mark on mark ring. | |
1097 With argument, jump to mark, and pop into mark off the mark ring. | |
1098 | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1099 Novice Emacs Lisp programmers often try to use the mark for the wrong |
475 | 1100 purposes. See the documentation of `set-mark' for more information." |
1101 (interactive "P") | |
1102 (if (null arg) | |
1103 (push-mark) | |
1104 (if (null (mark)) | |
1105 (error "No mark set in this buffer") | |
1106 (goto-char (mark)) | |
1107 (pop-mark)))) | |
1108 | |
1109 (defun push-mark (&optional location nomsg) | |
1110 "Set mark at LOCATION (point, by default) and push old mark on mark ring. | |
1111 Displays \"Mark set\" unless the optional second arg NOMSG is non-nil. | |
1112 | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1113 Novice Emacs Lisp programmers often try to use the mark for the wrong |
475 | 1114 purposes. See the documentation of `set-mark' for more information." |
1115 (if (null (mark)) | |
1116 nil | |
1117 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) | |
1118 (if (> (length mark-ring) mark-ring-max) | |
1119 (progn | |
1120 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
1121 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) | |
1122 (set-mark (or location (point))) | |
1123 (or nomsg executing-macro (> (minibuffer-depth) 0) | |
1124 (message "Mark set")) | |
1125 nil) | |
1126 | |
1127 (defun pop-mark () | |
1128 "Pop off mark ring into the buffer's actual mark. | |
1129 Does not set point. Does nothing if mark ring is empty." | |
1130 (if mark-ring | |
1131 (progn | |
1132 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
1133 (set-mark (+ 0 (car mark-ring))) | |
1134 (move-marker (car mark-ring) nil) | |
1135 (if (null (mark)) (ding)) | |
1136 (setq mark-ring (cdr mark-ring))))) | |
1137 | |
1138 (fset 'exchange-dot-and-mark 'exchange-point-and-mark) | |
1139 (defun exchange-point-and-mark () | |
1140 "Put the mark where point is now, and point where the mark is now." | |
1141 (interactive nil) | |
1142 (let ((omark (mark))) | |
1143 (if (null omark) | |
1144 (error "No mark set in this buffer")) | |
1145 (set-mark (point)) | |
1146 (goto-char omark) | |
1147 nil)) | |
1148 | |
1149 (defun next-line (arg) | |
1150 "Move cursor vertically down ARG lines. | |
1151 If there is no character in the target line exactly under the current column, | |
1152 the cursor is positioned after the character in that line which spans this | |
1153 column, or at the end of the line if it is not long enough. | |
1154 If there is no line in the buffer after this one, | |
1155 a newline character is inserted to create a line | |
1156 and the cursor moves to that line. | |
1157 | |
1158 The command \\[set-goal-column] can be used to create | |
1159 a semipermanent goal column to which this command always moves. | |
1160 Then it does not try to move vertically. This goal column is stored | |
1161 in `goal-column', which is nil when there is none. | |
1162 | |
1163 If you are thinking of using this in a Lisp program, consider | |
1164 using `forward-line' instead. It is usually easier to use | |
1165 and more reliable (no dependence on goal column, etc.)." | |
1166 (interactive "p") | |
1167 (if (= arg 1) | |
1168 (let ((opoint (point))) | |
1169 (forward-line 1) | |
1170 (if (or (= opoint (point)) | |
1171 (not (eq (preceding-char) ?\n))) | |
1172 (insert ?\n) | |
1173 (goto-char opoint) | |
1174 (line-move arg))) | |
1175 (line-move arg)) | |
1176 nil) | |
1177 | |
1178 (defun previous-line (arg) | |
1179 "Move cursor vertically up ARG lines. | |
1180 If there is no character in the target line exactly over the current column, | |
1181 the cursor is positioned after the character in that line which spans this | |
1182 column, or at the end of the line if it is not long enough. | |
1183 | |
1184 The command \\[set-goal-column] can be used to create | |
1185 a semipermanent goal column to which this command always moves. | |
1186 Then it does not try to move vertically. | |
1187 | |
1188 If you are thinking of using this in a Lisp program, consider using | |
1189 `forward-line' with negative argument instead.. It is usually easier | |
1190 to use and more reliable (no dependence on goal column, etc.)." | |
1191 (interactive "p") | |
1192 (line-move (- arg)) | |
1193 nil) | |
1194 | |
1195 (defconst track-eol nil | |
1196 "*Non-nil means vertical motion starting at end of line keeps to ends of lines. | |
1197 This means moving to the end of each line moved onto. | |
1198 The beginning of a blank line does not count as the end of a line.") | |
1199 | |
1200 (make-variable-buffer-local | |
1201 (defvar goal-column nil | |
1202 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.")) | |
1203 | |
1204 (defvar temporary-goal-column 0 | |
1205 "Current goal column for vertical motion. | |
1206 It is the column where point was | |
1207 at the start of current run of vertical motion commands. | |
513 | 1208 When the `track-eol' feature is doing its job, the value is 9999.") |
475 | 1209 |
1210 (defun line-move (arg) | |
1211 (if (not (or (eq last-command 'next-line) | |
1212 (eq last-command 'previous-line))) | |
1213 (setq temporary-goal-column | |
1214 (if (and track-eol (eolp) | |
1215 ;; Don't count beg of empty line as end of line | |
1216 ;; unless we just did explicit end-of-line. | |
1217 (or (not (bolp)) (eq last-command 'end-of-line))) | |
1218 9999 | |
1219 (current-column)))) | |
1220 (if (not (integerp selective-display)) | |
1221 (forward-line arg) | |
1222 ;; Move by arg lines, but ignore invisible ones. | |
1223 (while (> arg 0) | |
1224 (vertical-motion 1) | |
1225 (forward-char -1) | |
1226 (forward-line 1) | |
1227 (setq arg (1- arg))) | |
1228 (while (< arg 0) | |
1229 (vertical-motion -1) | |
1230 (beginning-of-line) | |
1231 (setq arg (1+ arg)))) | |
1232 (move-to-column (or goal-column temporary-goal-column)) | |
1233 nil) | |
1234 | |
1235 | |
1236 (defun set-goal-column (arg) | |
1237 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
1238 Those commands will move to this position in the line moved to | |
1239 rather than trying to keep the same horizontal position. | |
1240 With a non-nil argument, clears out the goal column | |
1241 so that \\[next-line] and \\[previous-line] resume vertical motion." | |
1242 (interactive "P") | |
1243 (if arg | |
1244 (progn | |
1245 (setq goal-column nil) | |
1246 (message "No goal column")) | |
1247 (setq goal-column (current-column)) | |
1248 (message (substitute-command-keys | |
1249 "Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
1250 goal-column)) | |
1251 nil) | |
1252 | |
1253 (defun transpose-chars (arg) | |
1254 "Interchange characters around point, moving forward one character. | |
1255 With prefix arg ARG, effect is to take character before point | |
1256 and drag it forward past ARG other characters (backward if ARG negative). | |
1257 If no argument and at end of line, the previous two chars are exchanged." | |
1258 (interactive "*P") | |
1259 (and (null arg) (eolp) (forward-char -1)) | |
1260 (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
1261 | |
1262 (defun transpose-words (arg) | |
1263 "Interchange words around point, leaving point at end of them. | |
1264 With prefix arg ARG, effect is to take word before or around point | |
1265 and drag it forward past ARG other words (backward if ARG negative). | |
1266 If ARG is zero, the words around or after point and around or after mark | |
1267 are interchanged." | |
1268 (interactive "*p") | |
1269 (transpose-subr 'forward-word arg)) | |
1270 | |
1271 (defun transpose-sexps (arg) | |
1272 "Like \\[transpose-words] but applies to sexps. | |
1273 Does not work on a sexp that point is in the middle of | |
1274 if it is a list or string." | |
1275 (interactive "*p") | |
1276 (transpose-subr 'forward-sexp arg)) | |
1277 | |
1278 (defun transpose-lines (arg) | |
1279 "Exchange current line and previous line, leaving point after both. | |
1280 With argument ARG, takes previous line and moves it past ARG lines. | |
1281 With argument 0, interchanges line point is in with line mark is in." | |
1282 (interactive "*p") | |
1283 (transpose-subr (function | |
1284 (lambda (arg) | |
1285 (if (= arg 1) | |
1286 (progn | |
1287 ;; Move forward over a line, | |
1288 ;; but create a newline if none exists yet. | |
1289 (end-of-line) | |
1290 (if (eobp) | |
1291 (newline) | |
1292 (forward-char 1))) | |
1293 (forward-line arg)))) | |
1294 arg)) | |
1295 | |
1296 (defun transpose-subr (mover arg) | |
1297 (let (start1 end1 start2 end2) | |
1298 (if (= arg 0) | |
1299 (progn | |
1300 (save-excursion | |
1301 (funcall mover 1) | |
1302 (setq end2 (point)) | |
1303 (funcall mover -1) | |
1304 (setq start2 (point)) | |
1305 (goto-char (mark)) | |
1306 (funcall mover 1) | |
1307 (setq end1 (point)) | |
1308 (funcall mover -1) | |
1309 (setq start1 (point)) | |
1310 (transpose-subr-1)) | |
1311 (exchange-point-and-mark))) | |
1312 (while (> arg 0) | |
1313 (funcall mover -1) | |
1314 (setq start1 (point)) | |
1315 (funcall mover 1) | |
1316 (setq end1 (point)) | |
1317 (funcall mover 1) | |
1318 (setq end2 (point)) | |
1319 (funcall mover -1) | |
1320 (setq start2 (point)) | |
1321 (transpose-subr-1) | |
1322 (goto-char end2) | |
1323 (setq arg (1- arg))) | |
1324 (while (< arg 0) | |
1325 (funcall mover -1) | |
1326 (setq start2 (point)) | |
1327 (funcall mover -1) | |
1328 (setq start1 (point)) | |
1329 (funcall mover 1) | |
1330 (setq end1 (point)) | |
1331 (funcall mover 1) | |
1332 (setq end2 (point)) | |
1333 (transpose-subr-1) | |
1334 (setq arg (1+ arg))))) | |
1335 | |
1336 (defun transpose-subr-1 () | |
1337 (if (> (min end1 end2) (max start1 start2)) | |
1338 (error "Don't have two things to transpose")) | |
1339 (let ((word1 (buffer-substring start1 end1)) | |
1340 (word2 (buffer-substring start2 end2))) | |
1341 (delete-region start2 end2) | |
1342 (goto-char start2) | |
1343 (insert word1) | |
1344 (goto-char (if (< start1 start2) start1 | |
1345 (+ start1 (- (length word1) (length word2))))) | |
1346 (delete-char (length word1)) | |
1347 (insert word2))) | |
1348 | |
1349 (defconst comment-column 32 | |
1350 "*Column to indent right-margin comments to. | |
1351 Setting this variable automatically makes it local to the current buffer.") | |
1352 (make-variable-buffer-local 'comment-column) | |
1353 | |
1354 (defconst comment-start nil | |
1355 "*String to insert to start a new comment, or nil if no comment syntax defined.") | |
1356 | |
1357 (defconst comment-start-skip nil | |
1358 "*Regexp to match the start of a comment plus everything up to its body. | |
1359 If there are any \\(...\\) pairs, the comment delimiter text is held to begin | |
1360 at the place matched by the close of the first pair.") | |
1361 | |
1362 (defconst comment-end "" | |
1363 "*String to insert to end a new comment. | |
1364 Should be an empty string if comments are terminated by end-of-line.") | |
1365 | |
1366 (defconst comment-indent-hook | |
1367 '(lambda () comment-column) | |
1368 "Function to compute desired indentation for a comment. | |
1369 This function is called with no args with point at the beginning of | |
1370 the comment's starting delimiter.") | |
1371 | |
1372 (defun indent-for-comment () | |
1373 "Indent this line's comment to comment column, or insert an empty comment." | |
1374 (interactive "*") | |
1375 (beginning-of-line 1) | |
1376 (if (null comment-start) | |
1377 (error "No comment syntax defined") | |
1378 (let* ((eolpos (save-excursion (end-of-line) (point))) | |
1379 cpos indent begpos) | |
1380 (if (re-search-forward comment-start-skip eolpos 'move) | |
1381 (progn (setq cpos (point-marker)) | |
1382 ;; Find the start of the comment delimiter. | |
1383 ;; If there were paren-pairs in comment-start-skip, | |
1384 ;; position at the end of the first pair. | |
1385 (if (match-end 1) | |
1386 (goto-char (match-end 1)) | |
1387 ;; If comment-start-skip matched a string with internal | |
1388 ;; whitespace (not final whitespace) then the delimiter | |
1389 ;; start at the end of that whitespace. | |
1390 ;; Otherwise, it starts at the beginning of what was matched. | |
1391 (skip-chars-backward " \t" (match-beginning 0)) | |
1392 (skip-chars-backward "^ \t" (match-beginning 0))))) | |
1393 (setq begpos (point)) | |
1394 ;; Compute desired indent. | |
1395 (if (= (current-column) | |
1396 (setq indent (funcall comment-indent-hook))) | |
1397 (goto-char begpos) | |
1398 ;; If that's different from current, change it. | |
1399 (skip-chars-backward " \t") | |
1400 (delete-region (point) begpos) | |
1401 (indent-to indent)) | |
1402 ;; An existing comment? | |
1403 (if cpos | |
1404 (progn (goto-char cpos) | |
1405 (set-marker cpos nil)) | |
1406 ;; No, insert one. | |
1407 (insert comment-start) | |
1408 (save-excursion | |
1409 (insert comment-end)))))) | |
1410 | |
1411 (defun set-comment-column (arg) | |
1412 "Set the comment column based on point. | |
1413 With no arg, set the comment column to the current column. | |
1414 With just minus as arg, kill any comment on this line. | |
1415 With any other arg, set comment column to indentation of the previous comment | |
1416 and then align or create a comment on this line at that column." | |
1417 (interactive "P") | |
1418 (if (eq arg '-) | |
1419 (kill-comment nil) | |
1420 (if arg | |
1421 (progn | |
1422 (save-excursion | |
1423 (beginning-of-line) | |
1424 (re-search-backward comment-start-skip) | |
1425 (beginning-of-line) | |
1426 (re-search-forward comment-start-skip) | |
1427 (goto-char (match-beginning 0)) | |
1428 (setq comment-column (current-column)) | |
1429 (message "Comment column set to %d" comment-column)) | |
1430 (indent-for-comment)) | |
1431 (setq comment-column (current-column)) | |
1432 (message "Comment column set to %d" comment-column)))) | |
1433 | |
1434 (defun kill-comment (arg) | |
1435 "Kill the comment on this line, if any. | |
1436 With argument, kill comments on that many lines starting with this one." | |
1437 ;; this function loses in a lot of situations. it incorrectly recognises | |
1438 ;; comment delimiters sometimes (ergo, inside a string), doesn't work | |
1439 ;; with multi-line comments, can kill extra whitespace if comment wasn't | |
1440 ;; through end-of-line, et cetera. | |
1441 (interactive "P") | |
1442 (or comment-start-skip (error "No comment syntax defined")) | |
1443 (let ((count (prefix-numeric-value arg)) endc) | |
1444 (while (> count 0) | |
1445 (save-excursion | |
1446 (end-of-line) | |
1447 (setq endc (point)) | |
1448 (beginning-of-line) | |
1449 (and (string< "" comment-end) | |
1450 (setq endc | |
1451 (progn | |
1452 (re-search-forward (regexp-quote comment-end) endc 'move) | |
1453 (skip-chars-forward " \t") | |
1454 (point)))) | |
1455 (beginning-of-line) | |
1456 (if (re-search-forward comment-start-skip endc t) | |
1457 (progn | |
1458 (goto-char (match-beginning 0)) | |
1459 (skip-chars-backward " \t") | |
1460 (kill-region (point) endc) | |
1461 ;; to catch comments a line beginnings | |
1462 (indent-according-to-mode)))) | |
1463 (if arg (forward-line 1)) | |
1464 (setq count (1- count))))) | |
1465 | |
1466 (defun comment-region (beg end &optional arg) | |
1467 "Comment the region; third arg numeric means use ARG comment characters. | |
1468 If ARG is negative, delete that many comment characters instead. | |
1469 Comments are terminated on each line, even for syntax in which newline does | |
1470 not end the comment. Blank lines do not get comments." | |
1471 ;; if someone wants it to only put a comment-start at the beginning and | |
1472 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x | |
1473 ;; is easy enough. No option is made here for other than commenting | |
1474 ;; every line. | |
1475 (interactive "r\np") | |
1476 (or comment-start (error "No comment syntax is defined")) | |
1477 (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | |
1478 (save-excursion | |
1479 (save-restriction | |
1480 (let ((cs comment-start) (ce comment-end)) | |
1481 (cond ((not arg) (setq arg 1)) | |
1482 ((> arg 1) | |
1483 (while (> (setq arg (1- arg)) 0) | |
1484 (setq cs (concat cs comment-start) | |
1485 ce (concat ce comment-end))))) | |
1486 (narrow-to-region beg end) | |
1487 (goto-char beg) | |
1488 (while (not (eobp)) | |
1489 (if (< arg 0) | |
1490 (let ((count arg)) | |
1491 (while (and (> 1 (setq count (1+ count))) | |
1492 (looking-at (regexp-quote cs))) | |
1493 (delete-char (length cs))) | |
1494 (if (string= "" ce) () | |
1495 (setq count arg) | |
1496 (while (> 1 (setq count (1+ count))) | |
1497 (end-of-line) | |
1498 ;; this is questionable if comment-end ends in whitespace | |
1499 ;; that is pretty brain-damaged though | |
1500 (skip-chars-backward " \t") | |
1501 (backward-char (length ce)) | |
1502 (if (looking-at (regexp-quote ce)) | |
1503 (delete-char (length ce)))))) | |
1504 (if (looking-at "[ \t]*$") () | |
1505 (insert cs) | |
1506 (if (string= "" ce) () | |
1507 (end-of-line) | |
1508 (insert ce))) | |
1509 (search-forward "\n" nil 'move))))))) | |
1510 | |
1511 (defun backward-word (arg) | |
1512 "Move backward until encountering the end of a word. | |
1513 With argument, do this that many times. | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1514 In programs, it is faster to call `forward-word' with negative arg." |
475 | 1515 (interactive "p") |
1516 (forward-word (- arg))) | |
1517 | |
1518 (defun mark-word (arg) | |
1519 "Set mark arg words away from point." | |
1520 (interactive "p") | |
1521 (push-mark | |
1522 (save-excursion | |
1523 (forward-word arg) | |
1524 (point)))) | |
1525 | |
1526 (defun kill-word (arg) | |
1527 "Kill characters forward until encountering the end of a word. | |
1528 With argument, do this that many times." | |
1529 (interactive "p") | |
1530 (kill-region (point) (progn (forward-word arg) (point)))) | |
1531 | |
1532 (defun backward-kill-word (arg) | |
1533 "Kill characters backward until encountering the end of a word. | |
1534 With argument, do this that many times." | |
1535 (interactive "p") | |
1536 (kill-word (- arg))) | |
1537 | |
1538 (defconst fill-prefix nil | |
1539 "*String for filling to insert at front of new line, or nil for none. | |
1540 Setting this variable automatically makes it local to the current buffer.") | |
1541 (make-variable-buffer-local 'fill-prefix) | |
1542 | |
1543 (defconst auto-fill-inhibit-regexp nil | |
1544 "*Regexp to match lines which should not be auto-filled.") | |
1545 | |
1546 (defun do-auto-fill () | |
1547 (let (give-up) | |
1548 (or (and auto-fill-inhibit-regexp | |
1549 (save-excursion (beginning-of-line) | |
1550 (looking-at auto-fill-inhibit-regexp))) | |
1551 (while (and (not give-up) (> (current-column) fill-column)) | |
1552 (let ((fill-point | |
1553 (let ((opoint (point))) | |
1554 (save-excursion | |
1555 (move-to-column (1+ fill-column)) | |
1556 (skip-chars-backward "^ \t\n") | |
1557 (if (bolp) | |
1558 (re-search-forward "[ \t]" opoint t)) | |
1559 (skip-chars-backward " \t") | |
1560 (point))))) | |
1561 ;; If there is a space on the line before fill-point, | |
1562 ;; and nonspaces precede it, break the line there. | |
1563 (if (save-excursion | |
1564 (goto-char fill-point) | |
1565 (not (bolp))) | |
1566 ;; If point is at the fill-point, do not `save-excursion'. | |
1567 ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
1568 ;; point will end up before it rather than after it. | |
1569 (if (save-excursion | |
1570 (skip-chars-backward " \t") | |
1571 (= (point) fill-point)) | |
1572 (indent-new-comment-line) | |
1573 (save-excursion | |
1574 (goto-char fill-point) | |
1575 (indent-new-comment-line))) | |
1576 ;; No place to break => stop trying. | |
1577 (setq give-up t))))))) | |
1578 | |
1579 (defconst comment-multi-line nil | |
1580 "*Non-nil means \\[indent-new-comment-line] should continue same comment | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1581 on new line, with no new terminator or starter. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1582 This is obsolete because you might as well use \\[newline-and-indent].") |
475 | 1583 |
1584 (defun indent-new-comment-line () | |
1585 "Break line at point and indent, continuing comment if presently within one. | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1586 The body of the continued comment is indented under the previous comment line. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1587 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1588 This command is intended for styles where you write a comment per line, |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1589 starting a new comment (and terminating it if necessary) on each line. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1590 If you want to continue one comment across several lines, use \\[newline-and-indent]." |
475 | 1591 (interactive "*") |
1592 (let (comcol comstart) | |
1593 (skip-chars-backward " \t") | |
1594 (delete-region (point) | |
1595 (progn (skip-chars-forward " \t") | |
1596 (point))) | |
1597 (insert ?\n) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1598 (if (not comment-multi-line) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1599 (save-excursion |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1600 (if (and comment-start-skip |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1601 (let ((opoint (point))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1602 (forward-line -1) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1603 (re-search-forward comment-start-skip opoint t))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1604 ;; The old line is a comment. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1605 ;; Set WIN to the pos of the comment-start. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1606 ;; But if the comment is empty, look at preceding lines |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1607 ;; to find one that has a nonempty comment. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1608 (let ((win (match-beginning 0))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1609 (while (and (eolp) (not (bobp)) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1610 (let (opoint) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1611 (beginning-of-line) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1612 (setq opoint (point)) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1613 (forward-line -1) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1614 (re-search-forward comment-start-skip opoint t))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1615 (setq win (match-beginning 0))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1616 ;; Indent this line like what we found. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1617 (goto-char win) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1618 (setq comcol (current-column)) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1619 (setq comstart (buffer-substring (point) (match-end 0))))))) |
475 | 1620 (if comcol |
1621 (let ((comment-column comcol) | |
1622 (comment-start comstart) | |
1623 (comment-end comment-end)) | |
1624 (and comment-end (not (equal comment-end "")) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1625 ; (if (not comment-multi-line) |
475 | 1626 (progn |
1627 (forward-char -1) | |
1628 (insert comment-end) | |
1629 (forward-char 1)) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1630 ; (setq comment-column (+ comment-column (length comment-start)) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1631 ; comment-start "") |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1632 ; ) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1633 ) |
475 | 1634 (if (not (eolp)) |
1635 (setq comment-end "")) | |
1636 (insert ?\n) | |
1637 (forward-char -1) | |
1638 (indent-for-comment) | |
1639 (save-excursion | |
1640 ;; Make sure we delete the newline inserted above. | |
1641 (end-of-line) | |
1642 (delete-char 1))) | |
1643 (if fill-prefix | |
1644 (insert fill-prefix) | |
1645 (indent-according-to-mode))))) | |
1646 | |
1647 (defun auto-fill-mode (&optional arg) | |
1648 "Toggle auto-fill mode. | |
1649 With arg, turn auto-fill mode on if and only if arg is positive. | |
1650 In auto-fill mode, inserting a space at a column beyond fill-column | |
1651 automatically breaks the line at a previous space." | |
1652 (interactive "P") | |
1653 (prog1 (setq auto-fill-function | |
1654 (if (if (null arg) | |
1655 (not auto-fill-function) | |
1656 (> (prefix-numeric-value arg) 0)) | |
1657 'do-auto-fill | |
1658 nil)) | |
1659 ;; update mode-line | |
1660 (set-buffer-modified-p (buffer-modified-p)))) | |
1661 | |
1662 (defun turn-on-auto-fill () | |
1663 "Unconditionally turn on Auto Fill mode." | |
1664 (auto-fill-mode 1)) | |
1665 | |
1666 (defun set-fill-column (arg) | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1667 "Set `fill-column' to current column, or to argument if given. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1668 The variable `fill-column' has a separate value for each buffer." |
475 | 1669 (interactive "P") |
1670 (setq fill-column (if (integerp arg) arg (current-column))) | |
1671 (message "fill-column set to %d" fill-column)) | |
1672 | |
1673 (defun set-selective-display (arg) | |
1027
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1674 "Set `selective-display' to ARG; clear it if no arg. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1675 When the value of `selective-display' is a number > 0, |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1676 lines whose indentation is >= that value are not displayed. |
f0000f6f7942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
954
diff
changeset
|
1677 The variable `selective-display' has a separate value for each buffer." |
475 | 1678 (interactive "P") |
1679 (if (eq selective-display t) | |
1680 (error "selective-display already in use for marked lines")) | |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1681 (let ((current-vpos |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1682 (save-restriction |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1683 (narrow-to-region (point-min) (point)) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1684 (goto-char (window-start)) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1685 (vertical-motion (window-height))))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1686 (setq selective-display |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1687 (and arg (prefix-numeric-value arg))) |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1688 (recenter current-vpos)) |
475 | 1689 (set-window-start (selected-window) (window-start (selected-window))) |
1690 (princ "selective-display set to " t) | |
1691 (prin1 selective-display t) | |
1692 (princ "." t)) | |
1693 | |
1694 (defun overwrite-mode (arg) | |
1695 "Toggle overwrite mode. | |
1696 With arg, turn overwrite mode on iff arg is positive. | |
1697 In overwrite mode, printing characters typed in replace existing text | |
1698 on a one-for-one basis, rather than pushing it to the right." | |
1699 (interactive "P") | |
1700 (setq overwrite-mode | |
1701 (if (null arg) (not overwrite-mode) | |
1702 (> (prefix-numeric-value arg) 0))) | |
1703 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line. | |
1704 | |
1705 (defvar blink-matching-paren t | |
1706 "*Non-nil means show matching open-paren when close-paren is inserted.") | |
1707 | |
1708 (defconst blink-matching-paren-distance 4000 | |
1709 "*If non-nil, is maximum distance to search for matching open-paren | |
1710 when close-paren is inserted.") | |
1711 | |
1712 (defun blink-matching-open () | |
1713 "Move cursor momentarily to the beginning of the sexp before point." | |
1714 (interactive) | |
1715 (and (> (point) (1+ (point-min))) | |
1716 (/= (char-syntax (char-after (- (point) 2))) ?\\ ) | |
1717 blink-matching-paren | |
1718 (let* ((oldpos (point)) | |
1719 (blinkpos) | |
1720 (mismatch)) | |
1721 (save-excursion | |
1722 (save-restriction | |
1723 (if blink-matching-paren-distance | |
1724 (narrow-to-region (max (point-min) | |
1725 (- (point) blink-matching-paren-distance)) | |
1726 oldpos)) | |
1727 (condition-case () | |
1728 (setq blinkpos (scan-sexps oldpos -1)) | |
1729 (error nil))) | |
1730 (and blinkpos (/= (char-syntax (char-after blinkpos)) | |
1731 ?\$) | |
1732 (setq mismatch | |
1733 (/= (char-after (1- oldpos)) | |
1734 (logand (lsh (aref (syntax-table) | |
1735 (char-after blinkpos)) | |
1736 -8) | |
1737 255)))) | |
1738 (if mismatch (setq blinkpos nil)) | |
1739 (if blinkpos | |
1740 (progn | |
1741 (goto-char blinkpos) | |
1742 (if (pos-visible-in-window-p) | |
1743 (sit-for 1) | |
1744 (goto-char blinkpos) | |
1745 (message | |
1746 "Matches %s" | |
1747 (if (save-excursion | |
1748 (skip-chars-backward " \t") | |
1749 (not (bolp))) | |
1750 (buffer-substring (progn (beginning-of-line) (point)) | |
1751 (1+ blinkpos)) | |
1752 (buffer-substring blinkpos | |
1753 (progn | |
1754 (forward-char 1) | |
1755 (skip-chars-forward "\n \t") | |
1756 (end-of-line) | |
1757 (point))))))) | |
1758 (cond (mismatch | |
1759 (message "Mismatched parentheses")) | |
1760 ((not blink-matching-paren-distance) | |
1761 (message "Unmatched parenthesis")))))))) | |
1762 | |
1763 ;Turned off because it makes dbx bomb out. | |
1764 (setq blink-paren-function 'blink-matching-open) | |
1765 | |
1766 ; this is just something for the luser to see in a keymap -- this is not | |
1767 ; how quitting works normally! | |
1768 (defun keyboard-quit () | |
1769 "Signal a quit condition." | |
1770 (interactive) | |
1771 (signal 'quit nil)) | |
1772 | |
1773 (define-key global-map "\C-g" 'keyboard-quit) | |
1774 | |
1775 (defun set-variable (var val) | |
1776 "Set VARIABLE to VALUE. VALUE is a Lisp object. | |
1777 When using this interactively, supply a Lisp expression for VALUE. | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1778 If you want VALUE to be a string, you must surround it with doublequotes. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1779 |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1780 If VARIABLE has a `variable-interactive' property, that is used as if |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1781 it were the arg to `interactive' (which see) to interactively read the value." |
475 | 1782 (interactive |
1783 (let* ((var (read-variable "Set variable: ")) | |
1784 (minibuffer-help-form | |
1785 '(funcall myhelp)) | |
1786 (myhelp | |
1787 (function | |
1788 (lambda () | |
1789 (with-output-to-temp-buffer "*Help*" | |
1790 (prin1 var) | |
1791 (princ "\nDocumentation:\n") | |
1792 (princ (substring (documentation-property var 'variable-documentation) | |
1793 1)) | |
1794 (if (boundp var) | |
1795 (let ((print-length 20)) | |
1796 (princ "\n\nCurrent value: ") | |
1797 (prin1 (symbol-value var)))) | |
1798 nil))))) | |
1799 (list var | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1800 (let ((prop (get var 'variable-interactive))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1801 (if prop |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1802 ;; Use VAR's `variable-interactive' property |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1803 ;; as an interactive spec for prompting. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1804 (call-interactively (list 'lambda '(arg) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1805 (list 'interactive prop) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1806 'arg)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
762
diff
changeset
|
1807 (eval-minibuffer (format "Set %s to value: " var))))))) |
475 | 1808 (set var val)) |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1809 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
617
diff
changeset
|
1810 ;;; simple.el ends here |