Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; simple.el --- basic editing commands for Emacs |
2 | ||
f8c25f1b | 3 | ;; Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc. |
2076c87c JB |
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 | |
3a801d0c | 9 | ;; the Free Software Foundation; either version 2, or (at your option) |
2076c87c JB |
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 | |
b578f267 EN |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
19 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | ;; Boston, MA 02111-1307, USA. | |
2076c87c | 21 | |
d9ecc911 ER |
22 | ;;; Commentary: |
23 | ||
24 | ;; A grab-bag of basic Emacs commands not specifically related to some | |
25 | ;; major mode or to file-handling. | |
26 | ||
3a801d0c | 27 | ;;; Code: |
2076c87c | 28 | |
30bb9754 | 29 | (defun newline (&optional arg) |
d133d835 | 30 | "Insert a newline, and move to left margin of the new line if it's blank. |
30bb9754 BG |
31 | The newline is marked with the text-property `hard'. |
32 | With arg, insert that many newlines. | |
33 | In Auto Fill mode, if no numeric arg, break the preceding line if it's long." | |
34 | (interactive "*P") | |
4c4cbf11 | 35 | (barf-if-buffer-read-only) |
30bb9754 BG |
36 | ;; Inserting a newline at the end of a line produces better redisplay in |
37 | ;; try_window_id than inserting at the beginning of a line, and the textual | |
38 | ;; result is the same. So, if we're at beginning of line, pretend to be at | |
39 | ;; the end of the previous line. | |
40 | (let ((flag (and (not (bobp)) | |
41 | (bolp) | |
42 | (< (or (previous-property-change (point)) -2) | |
d133d835 RS |
43 | (- (point) 2)))) |
44 | (was-page-start (and (bolp) | |
45 | (looking-at page-delimiter))) | |
46 | (beforepos (point))) | |
30bb9754 BG |
47 | (if flag (backward-char 1)) |
48 | ;; Call self-insert so that auto-fill, abbrev expansion etc. happens. | |
49 | ;; Set last-command-char to tell self-insert what to insert. | |
50 | (let ((last-command-char ?\n) | |
51 | ;; Don't auto-fill if we have a numeric argument. | |
3954fff9 RS |
52 | ;; Also not if flag is true (it would fill wrong line); |
53 | ;; there is no need to since we're at BOL. | |
54 | (auto-fill-function (if (or arg flag) nil auto-fill-function))) | |
4cc9d0dc RS |
55 | (unwind-protect |
56 | (self-insert-command (prefix-numeric-value arg)) | |
57 | ;; If we get an error in self-insert-command, put point at right place. | |
58 | (if flag (forward-char 1)))) | |
59 | ;; If we did *not* get an error, cancel that forward-char. | |
60 | (if flag (backward-char 1)) | |
30bb9754 BG |
61 | ;; Mark the newline(s) `hard'. |
62 | (if use-hard-newlines | |
63 | (let* ((from (- (point) (if arg (prefix-numeric-value arg) 1))) | |
64 | (sticky (get-text-property from 'rear-nonsticky))) | |
65 | (put-text-property from (point) 'hard 't) | |
66 | ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list | |
67 | (if (and (listp sticky) (not (memq 'hard sticky))) | |
68 | (put-text-property from (point) 'rear-nonsticky | |
69 | (cons 'hard sticky))))) | |
d133d835 RS |
70 | ;; If the newline leaves the previous line blank, |
71 | ;; and we have a left margin, delete that from the blank line. | |
72 | (or flag | |
73 | (save-excursion | |
74 | (goto-char beforepos) | |
75 | (beginning-of-line) | |
76 | (and (looking-at "[ \t]$") | |
77 | (> (current-left-margin) 0) | |
78 | (delete-region (point) (progn (end-of-line) (point)))))) | |
79 | (if flag (forward-char 1)) | |
80 | ;; Indent the line after the newline, except in one case: | |
81 | ;; when we added the newline at the beginning of a line | |
82 | ;; which starts a page. | |
83 | (or was-page-start | |
84 | (move-to-left-margin nil t))) | |
30bb9754 BG |
85 | nil) |
86 | ||
2076c87c | 87 | (defun open-line (arg) |
ff1fbe3e | 88 | "Insert a newline and leave point before it. |
3db1e3b5 | 89 | If there is a fill prefix and/or a left-margin, insert them on the new line |
d133d835 | 90 | if the line would have been blank. |
616ed245 | 91 | With arg N, insert N newlines." |
2076c87c | 92 | (interactive "*p") |
616ed245 | 93 | (let* ((do-fill-prefix (and fill-prefix (bolp))) |
3db1e3b5 | 94 | (do-left-margin (and (bolp) (> (current-left-margin) 0))) |
28191e20 | 95 | (loc (point))) |
d133d835 RS |
96 | (newline arg) |
97 | (goto-char loc) | |
28191e20 | 98 | (while (> arg 0) |
d133d835 RS |
99 | (cond ((bolp) |
100 | (if do-left-margin (indent-to (current-left-margin))) | |
101 | (if do-fill-prefix (insert-and-inherit fill-prefix)))) | |
102 | (forward-line 1) | |
28191e20 | 103 | (setq arg (1- arg))) |
d133d835 RS |
104 | (goto-char loc) |
105 | (end-of-line))) | |
2076c87c JB |
106 | |
107 | (defun split-line () | |
108 | "Split current line, moving portion beyond point vertically down." | |
109 | (interactive "*") | |
110 | (skip-chars-forward " \t") | |
111 | (let ((col (current-column)) | |
112 | (pos (point))) | |
28191e20 | 113 | (newline 1) |
2076c87c JB |
114 | (indent-to col 0) |
115 | (goto-char pos))) | |
116 | ||
117 | (defun quoted-insert (arg) | |
118 | "Read next input character and insert it. | |
ff1fbe3e | 119 | This is useful for inserting control characters. |
dbc4e1c1 | 120 | You may also type up to 3 octal digits, to insert a character with that code. |
b6a22db0 JB |
121 | |
122 | In overwrite mode, this function inserts the character anyway, and | |
123 | does not handle octal digits specially. This means that if you use | |
124 | overwrite as your normal editing mode, you can use this function to | |
125 | insert characters when necessary. | |
126 | ||
127 | In binary overwrite mode, this function does overwrite, and octal | |
128 | digits are interpreted as a character code. This is supposed to make | |
129 | this function useful in editing binary files." | |
2076c87c | 130 | (interactive "*p") |
b6a22db0 JB |
131 | (let ((char (if (or (not overwrite-mode) |
132 | (eq overwrite-mode 'overwrite-mode-binary)) | |
133 | (read-quoted-char) | |
134 | (read-char)))) | |
ec321cad RS |
135 | (if (> arg 0) |
136 | (if (eq overwrite-mode 'overwrite-mode-binary) | |
137 | (delete-char arg))) | |
138 | (while (> arg 0) | |
139 | (insert-and-inherit char) | |
140 | (setq arg (1- arg))))) | |
2076c87c JB |
141 | |
142 | (defun delete-indentation (&optional arg) | |
143 | "Join this line to previous and fix up whitespace at join. | |
ccc58657 | 144 | If there is a fill prefix, delete it from the beginning of this line. |
2076c87c JB |
145 | With argument, join this line to following line." |
146 | (interactive "*P") | |
147 | (beginning-of-line) | |
148 | (if arg (forward-line 1)) | |
149 | (if (eq (preceding-char) ?\n) | |
150 | (progn | |
151 | (delete-region (point) (1- (point))) | |
ccc58657 RS |
152 | ;; If the second line started with the fill prefix, |
153 | ;; delete the prefix. | |
154 | (if (and fill-prefix | |
01b8e020 | 155 | (<= (+ (point) (length fill-prefix)) (point-max)) |
ccc58657 RS |
156 | (string= fill-prefix |
157 | (buffer-substring (point) | |
158 | (+ (point) (length fill-prefix))))) | |
159 | (delete-region (point) (+ (point) (length fill-prefix)))) | |
2076c87c JB |
160 | (fixup-whitespace)))) |
161 | ||
162 | (defun fixup-whitespace () | |
163 | "Fixup white space between objects around point. | |
164 | Leave one space or none, according to the context." | |
165 | (interactive "*") | |
166 | (save-excursion | |
167 | (delete-horizontal-space) | |
168 | (if (or (looking-at "^\\|\\s)") | |
169 | (save-excursion (forward-char -1) | |
170 | (looking-at "$\\|\\s(\\|\\s'"))) | |
171 | nil | |
172 | (insert ?\ )))) | |
173 | ||
174 | (defun delete-horizontal-space () | |
175 | "Delete all spaces and tabs around point." | |
176 | (interactive "*") | |
177 | (skip-chars-backward " \t") | |
178 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
179 | ||
180 | (defun just-one-space () | |
181 | "Delete all spaces and tabs around point, leaving one space." | |
182 | (interactive "*") | |
183 | (skip-chars-backward " \t") | |
184 | (if (= (following-char) ? ) | |
185 | (forward-char 1) | |
186 | (insert ? )) | |
187 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
188 | ||
189 | (defun delete-blank-lines () | |
190 | "On blank line, delete all surrounding blank lines, leaving just one. | |
191 | On isolated blank line, delete that one. | |
6d30d416 | 192 | On nonblank line, delete any immediately following blank lines." |
2076c87c JB |
193 | (interactive "*") |
194 | (let (thisblank singleblank) | |
195 | (save-excursion | |
196 | (beginning-of-line) | |
197 | (setq thisblank (looking-at "[ \t]*$")) | |
70e14c01 | 198 | ;; Set singleblank if there is just one blank line here. |
2076c87c JB |
199 | (setq singleblank |
200 | (and thisblank | |
201 | (not (looking-at "[ \t]*\n[ \t]*$")) | |
202 | (or (bobp) | |
203 | (progn (forward-line -1) | |
204 | (not (looking-at "[ \t]*$"))))))) | |
70e14c01 | 205 | ;; Delete preceding blank lines, and this one too if it's the only one. |
2076c87c JB |
206 | (if thisblank |
207 | (progn | |
208 | (beginning-of-line) | |
209 | (if singleblank (forward-line 1)) | |
210 | (delete-region (point) | |
211 | (if (re-search-backward "[^ \t\n]" nil t) | |
212 | (progn (forward-line 1) (point)) | |
213 | (point-min))))) | |
70e14c01 JB |
214 | ;; Delete following blank lines, unless the current line is blank |
215 | ;; and there are no following blank lines. | |
2076c87c JB |
216 | (if (not (and thisblank singleblank)) |
217 | (save-excursion | |
218 | (end-of-line) | |
219 | (forward-line 1) | |
220 | (delete-region (point) | |
221 | (if (re-search-forward "[^ \t\n]" nil t) | |
222 | (progn (beginning-of-line) (point)) | |
70e14c01 JB |
223 | (point-max))))) |
224 | ;; Handle the special case where point is followed by newline and eob. | |
225 | ;; Delete the line, leaving point at eob. | |
226 | (if (looking-at "^[ \t]*\n\\'") | |
227 | (delete-region (point) (point-max))))) | |
2076c87c JB |
228 | |
229 | (defun back-to-indentation () | |
230 | "Move point to the first non-whitespace character on this line." | |
231 | (interactive) | |
232 | (beginning-of-line 1) | |
233 | (skip-chars-forward " \t")) | |
234 | ||
235 | (defun newline-and-indent () | |
236 | "Insert a newline, then indent according to major mode. | |
ff1fbe3e | 237 | Indentation is done using the value of `indent-line-function'. |
2076c87c | 238 | In programming language modes, this is the same as TAB. |
ff1fbe3e | 239 | In some text modes, where TAB inserts a tab, this command indents to the |
eed5698b | 240 | column specified by the function `current-left-margin'." |
2076c87c JB |
241 | (interactive "*") |
242 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
46947372 | 243 | (newline) |
2076c87c JB |
244 | (indent-according-to-mode)) |
245 | ||
246 | (defun reindent-then-newline-and-indent () | |
247 | "Reindent current line, insert newline, then indent the new line. | |
248 | Indentation of both lines is done according to the current major mode, | |
ff1fbe3e | 249 | which means calling the current value of `indent-line-function'. |
2076c87c JB |
250 | In programming language modes, this is the same as TAB. |
251 | In some text modes, where TAB inserts a tab, this indents to the | |
eed5698b | 252 | column specified by the function `current-left-margin'." |
2076c87c JB |
253 | (interactive "*") |
254 | (save-excursion | |
255 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
256 | (indent-according-to-mode)) | |
46947372 | 257 | (newline) |
2076c87c JB |
258 | (indent-according-to-mode)) |
259 | ||
dff7d67f RS |
260 | ;; Internal subroutine of delete-char |
261 | (defun kill-forward-chars (arg) | |
262 | (if (listp arg) (setq arg (car arg))) | |
263 | (if (eq arg '-) (setq arg -1)) | |
264 | (kill-region (point) (+ (point) arg))) | |
265 | ||
266 | ;; Internal subroutine of backward-delete-char | |
267 | (defun kill-backward-chars (arg) | |
268 | (if (listp arg) (setq arg (car arg))) | |
269 | (if (eq arg '-) (setq arg -1)) | |
270 | (kill-region (point) (- (point) arg))) | |
271 | ||
2076c87c JB |
272 | (defun backward-delete-char-untabify (arg &optional killp) |
273 | "Delete characters backward, changing tabs into spaces. | |
274 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. | |
275 | Interactively, ARG is the prefix arg (default 1) | |
aba05ae4 | 276 | and KILLP is t if a prefix arg was specified." |
2076c87c JB |
277 | (interactive "*p\nP") |
278 | (let ((count arg)) | |
279 | (save-excursion | |
280 | (while (and (> count 0) (not (bobp))) | |
281 | (if (= (preceding-char) ?\t) | |
282 | (let ((col (current-column))) | |
283 | (forward-char -1) | |
284 | (setq col (- col (current-column))) | |
285 | (insert-char ?\ col) | |
286 | (delete-char 1))) | |
287 | (forward-char -1) | |
288 | (setq count (1- count))))) | |
289 | (delete-backward-char arg killp) | |
290 | ;; In overwrite mode, back over columns while clearing them out, | |
291 | ;; unless at end of line. | |
292 | (and overwrite-mode (not (eolp)) | |
293 | (save-excursion (insert-char ?\ arg)))) | |
294 | ||
295 | (defun zap-to-char (arg char) | |
296 | "Kill up to and including ARG'th occurrence of CHAR. | |
297 | Goes backward if ARG is negative; error if CHAR not found." | |
298 | (interactive "p\ncZap to char: ") | |
299 | (kill-region (point) (progn | |
300 | (search-forward (char-to-string char) nil nil arg) | |
301 | ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) | |
302 | (point)))) | |
303 | ||
304 | (defun beginning-of-buffer (&optional arg) | |
305 | "Move point to the beginning of the buffer; leave mark at previous position. | |
c66587fe RS |
306 | With arg N, put point N/10 of the way from the beginning. |
307 | ||
308 | If the buffer is narrowed, this command uses the beginning and size | |
309 | of the accessible part of the buffer. | |
ff1fbe3e RS |
310 | |
311 | Don't use this command in Lisp programs! | |
2076c87c JB |
312 | \(goto-char (point-min)) is faster and avoids clobbering the mark." |
313 | (interactive "P") | |
314 | (push-mark) | |
c66587fe RS |
315 | (let ((size (- (point-max) (point-min)))) |
316 | (goto-char (if arg | |
317 | (+ (point-min) | |
318 | (if (> size 10000) | |
319 | ;; Avoid overflow for large buffer sizes! | |
320 | (* (prefix-numeric-value arg) | |
321 | (/ size 10)) | |
322 | (/ (+ 10 (* size (prefix-numeric-value arg))) 10))) | |
323 | (point-min)))) | |
2076c87c JB |
324 | (if arg (forward-line 1))) |
325 | ||
326 | (defun end-of-buffer (&optional arg) | |
327 | "Move point to the end of the buffer; leave mark at previous position. | |
c66587fe RS |
328 | With arg N, put point N/10 of the way from the end. |
329 | ||
330 | If the buffer is narrowed, this command uses the beginning and size | |
331 | of the accessible part of the buffer. | |
ff1fbe3e RS |
332 | |
333 | Don't use this command in Lisp programs! | |
2076c87c JB |
334 | \(goto-char (point-max)) is faster and avoids clobbering the mark." |
335 | (interactive "P") | |
336 | (push-mark) | |
c66587fe RS |
337 | (let ((size (- (point-max) (point-min)))) |
338 | (goto-char (if arg | |
339 | (- (point-max) | |
340 | (if (> size 10000) | |
341 | ;; Avoid overflow for large buffer sizes! | |
342 | (* (prefix-numeric-value arg) | |
343 | (/ size 10)) | |
344 | (/ (* size (prefix-numeric-value arg)) 10))) | |
345 | (point-max)))) | |
3a801d0c ER |
346 | ;; If we went to a place in the middle of the buffer, |
347 | ;; adjust it to the beginning of a line. | |
2076c87c | 348 | (if arg (forward-line 1) |
3a801d0c ER |
349 | ;; If the end of the buffer is not already on the screen, |
350 | ;; then scroll specially to put it near, but not at, the bottom. | |
351 | (if (let ((old-point (point))) | |
352 | (save-excursion | |
353 | (goto-char (window-start)) | |
354 | (vertical-motion (window-height)) | |
355 | (< (point) old-point))) | |
97dfc68c RS |
356 | (progn |
357 | (overlay-recenter (point)) | |
358 | (recenter -3))))) | |
2076c87c JB |
359 | |
360 | (defun mark-whole-buffer () | |
70e14c01 JB |
361 | "Put point at beginning and mark at end of buffer. |
362 | You probably should not use this function in Lisp programs; | |
363 | it is usually a mistake for a Lisp function to use any subroutine | |
364 | that uses or sets the mark." | |
2076c87c JB |
365 | (interactive) |
366 | (push-mark (point)) | |
fd0f4056 | 367 | (push-mark (point-max) nil t) |
2076c87c JB |
368 | (goto-char (point-min))) |
369 | ||
370 | (defun count-lines-region (start end) | |
eb8c3be9 | 371 | "Print number of lines and characters in the region." |
2076c87c JB |
372 | (interactive "r") |
373 | (message "Region has %d lines, %d characters" | |
374 | (count-lines start end) (- end start))) | |
375 | ||
376 | (defun what-line () | |
2578be76 | 377 | "Print the current buffer line number and narrowed line number of point." |
2076c87c | 378 | (interactive) |
2578be76 | 379 | (let ((opoint (point)) start) |
2076c87c | 380 | (save-excursion |
2578be76 RS |
381 | (save-restriction |
382 | (goto-char (point-min)) | |
383 | (widen) | |
384 | (beginning-of-line) | |
385 | (setq start (point)) | |
386 | (goto-char opoint) | |
387 | (beginning-of-line) | |
388 | (if (/= start 1) | |
389 | (message "line %d (narrowed line %d)" | |
390 | (1+ (count-lines 1 (point))) | |
391 | (1+ (count-lines start (point)))) | |
392 | (message "Line %d" (1+ (count-lines 1 (point))))))))) | |
393 | ||
2076c87c JB |
394 | |
395 | (defun count-lines (start end) | |
396 | "Return number of lines between START and END. | |
397 | This is usually the number of newlines between them, | |
ff1fbe3e | 398 | but can be one more if START is not equal to END |
2076c87c | 399 | and the greater of them is not at the start of a line." |
e406700d RS |
400 | (save-excursion |
401 | (save-restriction | |
402 | (narrow-to-region start end) | |
403 | (goto-char (point-min)) | |
404 | (if (eq selective-display t) | |
405 | (save-match-data | |
dde92ca6 RS |
406 | (let ((done 0)) |
407 | (while (re-search-forward "[\n\C-m]" nil t 40) | |
408 | (setq done (+ 40 done))) | |
409 | (while (re-search-forward "[\n\C-m]" nil t 1) | |
410 | (setq done (+ 1 done))) | |
043efc41 RS |
411 | (goto-char (point-max)) |
412 | (if (and (/= start end) | |
413 | (not (bolp))) | |
414 | (1+ done) | |
e406700d RS |
415 | done))) |
416 | (- (buffer-size) (forward-line (buffer-size))))))) | |
2076c87c JB |
417 | |
418 | (defun what-cursor-position () | |
419 | "Print info on cursor position (on screen and within buffer)." | |
420 | (interactive) | |
421 | (let* ((char (following-char)) | |
422 | (beg (point-min)) | |
423 | (end (point-max)) | |
424 | (pos (point)) | |
425 | (total (buffer-size)) | |
426 | (percent (if (> total 50000) | |
427 | ;; Avoid overflow from multiplying by 100! | |
428 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
429 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
430 | (hscroll (if (= (window-hscroll) 0) | |
431 | "" | |
432 | (format " Hscroll=%d" (window-hscroll)))) | |
433 | (col (current-column))) | |
434 | (if (= pos end) | |
435 | (if (or (/= beg 1) (/= end (1+ total))) | |
436 | (message "point=%d of %d(%d%%) <%d - %d> column %d %s" | |
437 | pos total percent beg end col hscroll) | |
438 | (message "point=%d of %d(%d%%) column %d %s" | |
439 | pos total percent col hscroll)) | |
440 | (if (or (/= beg 1) (/= end (1+ total))) | |
2a83421c RS |
441 | (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) <%d - %d> column %d %s" |
442 | (single-key-description char) char char char pos total percent beg end col hscroll) | |
443 | (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) column %d %s" | |
444 | (single-key-description char) char char char pos total percent col hscroll))))) | |
2076c87c JB |
445 | |
446 | (defun fundamental-mode () | |
447 | "Major mode not specialized for anything in particular. | |
448 | Other major modes are defined by comparison with this one." | |
449 | (interactive) | |
450 | (kill-all-local-variables)) | |
451 | ||
4578d35d | 452 | (defvar read-expression-map (cons 'keymap minibuffer-local-map) |
854c16c5 RS |
453 | "Minibuffer keymap used for reading Lisp expressions.") |
454 | (define-key read-expression-map "\M-\t" 'lisp-complete-symbol) | |
455 | ||
2076c87c JB |
456 | (put 'eval-expression 'disabled t) |
457 | ||
8570b0ca RM |
458 | (defvar read-expression-history nil) |
459 | ||
460 | ;; We define this, rather than making `eval' interactive, | |
2076c87c JB |
461 | ;; for the sake of completion of names like eval-region, eval-current-buffer. |
462 | (defun eval-expression (expression) | |
463 | "Evaluate EXPRESSION and print value in minibuffer. | |
eb57c304 | 464 | Value is also consed on to front of the variable `values'." |
adca5fa6 | 465 | (interactive |
b387ef9a RS |
466 | (list (read-from-minibuffer "Eval: " |
467 | nil read-expression-map t | |
468 | 'read-expression-history))) | |
2076c87c JB |
469 | (setq values (cons (eval expression) values)) |
470 | (prin1 (car values) t)) | |
471 | ||
472 | (defun edit-and-eval-command (prompt command) | |
473 | "Prompting with PROMPT, let user edit COMMAND and eval result. | |
474 | COMMAND is a Lisp expression. Let user edit that expression in | |
475 | the minibuffer, then read and evaluate the result." | |
b387ef9a RS |
476 | (let ((command (read-from-minibuffer prompt |
477 | (prin1-to-string command) | |
478 | read-expression-map t | |
479 | '(command-history . 1)))) | |
5d6c83ae | 480 | ;; If command was added to command-history as a string, |
1f238ac2 | 481 | ;; get rid of that. We want only evaluable expressions there. |
5d6c83ae KH |
482 | (if (stringp (car command-history)) |
483 | (setq command-history (cdr command-history))) | |
484 | ||
485 | ;; If command to be redone does not match front of history, | |
486 | ;; add it to the history. | |
487 | (or (equal command (car command-history)) | |
488 | (setq command-history (cons command command-history))) | |
2076c87c JB |
489 | (eval command))) |
490 | ||
ebb61177 | 491 | (defun repeat-complex-command (arg) |
2076c87c JB |
492 | "Edit and re-evaluate last complex command, or ARGth from last. |
493 | A complex command is one which used the minibuffer. | |
494 | The command is placed in the minibuffer as a Lisp form for editing. | |
495 | The result is executed, repeating the command as changed. | |
496 | If the command has been changed or is not the most recent previous command | |
497 | it is added to the front of the command history. | |
eb6e9899 RS |
498 | You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] |
499 | to get different commands to edit and resubmit." | |
2076c87c | 500 | (interactive "p") |
ba343182 | 501 | (let ((elt (nth (1- arg) command-history)) |
ebb61177 | 502 | (minibuffer-history-position arg) |
ba343182 | 503 | (minibuffer-history-sexp-flag t) |
2076c87c JB |
504 | newcmd) |
505 | (if elt | |
854c16c5 | 506 | (progn |
eab22e27 | 507 | (setq newcmd |
7908d27c RS |
508 | (let ((print-level nil)) |
509 | (read-from-minibuffer | |
510 | "Redo: " (prin1-to-string elt) read-expression-map t | |
511 | (cons 'command-history arg)))) | |
eab22e27 | 512 | |
db16f109 | 513 | ;; If command was added to command-history as a string, |
1f238ac2 | 514 | ;; get rid of that. We want only evaluable expressions there. |
db16f109 RS |
515 | (if (stringp (car command-history)) |
516 | (setq command-history (cdr command-history))) | |
517 | ||
518 | ;; If command to be redone does not match front of history, | |
519 | ;; add it to the history. | |
520 | (or (equal newcmd (car command-history)) | |
521 | (setq command-history (cons newcmd command-history))) | |
2076c87c JB |
522 | (eval newcmd)) |
523 | (ding)))) | |
e91f80c4 | 524 | \f |
854c16c5 RS |
525 | (defvar minibuffer-history nil |
526 | "Default minibuffer history list. | |
527 | This is used for all minibuffer input | |
528 | except when an alternate history list is specified.") | |
529 | (defvar minibuffer-history-sexp-flag nil | |
c2d4b6d9 | 530 | "Non-nil when doing history operations on `command-history'. |
854c16c5 RS |
531 | More generally, indicates that the history list being acted on |
532 | contains expressions rather than strings.") | |
e91f80c4 RS |
533 | (setq minibuffer-history-variable 'minibuffer-history) |
534 | (setq minibuffer-history-position nil) | |
854c16c5 | 535 | (defvar minibuffer-history-search-history nil) |
e91f80c4 | 536 | |
29929437 | 537 | (mapcar |
d0678801 RM |
538 | (lambda (key-and-command) |
539 | (mapcar | |
540 | (lambda (keymap-and-completionp) | |
541 | ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P). | |
542 | ;; If the cdr of KEY-AND-COMMAND (the command) is a cons, | |
543 | ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t. | |
544 | (define-key (symbol-value (car keymap-and-completionp)) | |
545 | (car key-and-command) | |
546 | (let ((command (cdr key-and-command))) | |
547 | (if (consp command) | |
b5e6f936 RM |
548 | ;; (and ... nil) => ... turns back on the completion-oriented |
549 | ;; history commands which rms turned off since they seem to | |
550 | ;; do things he doesn't like. | |
551 | (if (and (cdr keymap-and-completionp) nil) ;XXX turned off | |
d81362b0 | 552 | (progn (error "EMACS BUG!") (cdr command)) |
d0678801 RM |
553 | (car command)) |
554 | command)))) | |
555 | '((minibuffer-local-map . nil) | |
556 | (minibuffer-local-ns-map . nil) | |
557 | (minibuffer-local-completion-map . t) | |
558 | (minibuffer-local-must-match-map . t) | |
559 | (read-expression-map . nil)))) | |
d81362b0 RM |
560 | '(("\en" . (next-history-element . next-complete-history-element)) |
561 | ([next] . (next-history-element . next-complete-history-element)) | |
562 | ("\ep" . (previous-history-element . previous-complete-history-element)) | |
563 | ([prior] . (previous-history-element . previous-complete-history-element)) | |
29929437 JB |
564 | ("\er" . previous-matching-history-element) |
565 | ("\es" . next-matching-history-element))) | |
e91f80c4 | 566 | |
e91f80c4 | 567 | (defun previous-matching-history-element (regexp n) |
854c16c5 RS |
568 | "Find the previous history element that matches REGEXP. |
569 | \(Previous history elements refer to earlier actions.) | |
570 | With prefix argument N, search for Nth previous match. | |
571 | If N is negative, find the next or Nth next match." | |
572 | (interactive | |
c1172a19 RS |
573 | (let* ((enable-recursive-minibuffers t) |
574 | (minibuffer-history-sexp-flag nil) | |
575 | (regexp (read-from-minibuffer "Previous element matching (regexp): " | |
576 | nil | |
577 | minibuffer-local-map | |
578 | nil | |
579 | 'minibuffer-history-search-history))) | |
580 | ;; Use the last regexp specified, by default, if input is empty. | |
581 | (list (if (string= regexp "") | |
a8e96cea KH |
582 | (if minibuffer-history-search-history |
583 | (car minibuffer-history-search-history) | |
584 | (error "No previous history search regexp")) | |
c1172a19 | 585 | regexp) |
854c16c5 | 586 | (prefix-numeric-value current-prefix-arg)))) |
e91f80c4 | 587 | (let ((history (symbol-value minibuffer-history-variable)) |
ccc58657 | 588 | prevpos |
e91f80c4 RS |
589 | (pos minibuffer-history-position)) |
590 | (while (/= n 0) | |
591 | (setq prevpos pos) | |
592 | (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) | |
593 | (if (= pos prevpos) | |
594 | (error (if (= pos 1) | |
ccc58657 RS |
595 | "No later matching history item" |
596 | "No earlier matching history item"))) | |
e91f80c4 RS |
597 | (if (string-match regexp |
598 | (if minibuffer-history-sexp-flag | |
7908d27c RS |
599 | (let ((print-level nil)) |
600 | (prin1-to-string (nth (1- pos) history))) | |
e91f80c4 | 601 | (nth (1- pos) history))) |
854c16c5 | 602 | (setq n (+ n (if (< n 0) 1 -1))))) |
e91f80c4 RS |
603 | (setq minibuffer-history-position pos) |
604 | (erase-buffer) | |
605 | (let ((elt (nth (1- pos) history))) | |
606 | (insert (if minibuffer-history-sexp-flag | |
7908d27c RS |
607 | (let ((print-level nil)) |
608 | (prin1-to-string elt)) | |
e91f80c4 | 609 | elt))) |
854c16c5 RS |
610 | (goto-char (point-min))) |
611 | (if (or (eq (car (car command-history)) 'previous-matching-history-element) | |
612 | (eq (car (car command-history)) 'next-matching-history-element)) | |
613 | (setq command-history (cdr command-history)))) | |
e91f80c4 | 614 | |
e91f80c4 | 615 | (defun next-matching-history-element (regexp n) |
854c16c5 RS |
616 | "Find the next history element that matches REGEXP. |
617 | \(The next history element refers to a more recent action.) | |
618 | With prefix argument N, search for Nth next match. | |
619 | If N is negative, find the previous or Nth previous match." | |
620 | (interactive | |
c1172a19 RS |
621 | (let* ((enable-recursive-minibuffers t) |
622 | (minibuffer-history-sexp-flag nil) | |
623 | (regexp (read-from-minibuffer "Next element matching (regexp): " | |
624 | nil | |
625 | minibuffer-local-map | |
626 | nil | |
627 | 'minibuffer-history-search-history))) | |
628 | ;; Use the last regexp specified, by default, if input is empty. | |
629 | (list (if (string= regexp "") | |
630 | (setcar minibuffer-history-search-history | |
631 | (nth 1 minibuffer-history-search-history)) | |
632 | regexp) | |
854c16c5 | 633 | (prefix-numeric-value current-prefix-arg)))) |
e91f80c4 | 634 | (previous-matching-history-element regexp (- n))) |
2076c87c | 635 | |
ebb61177 RS |
636 | (defun next-history-element (n) |
637 | "Insert the next element of the minibuffer history into the minibuffer." | |
2076c87c | 638 | (interactive "p") |
0818b15e RS |
639 | (or (zerop n) |
640 | (let ((narg (min (max 1 (- minibuffer-history-position n)) | |
641 | (length (symbol-value minibuffer-history-variable))))) | |
642 | (if (or (zerop narg) | |
643 | (= minibuffer-history-position narg)) | |
644 | (error (if (if (zerop narg) | |
645 | (> n 0) | |
646 | (= minibuffer-history-position 1)) | |
647 | "End of history; no next item" | |
648 | "Beginning of history; no preceding item")) | |
649 | (erase-buffer) | |
650 | (setq minibuffer-history-position narg) | |
651 | (let ((elt (nth (1- minibuffer-history-position) | |
652 | (symbol-value minibuffer-history-variable)))) | |
653 | (insert | |
654 | (if minibuffer-history-sexp-flag | |
655 | (let ((print-level nil)) | |
656 | (prin1-to-string elt)) | |
657 | elt))) | |
658 | (goto-char (point-min)))))) | |
2076c87c | 659 | |
ebb61177 | 660 | (defun previous-history-element (n) |
3ee3a076 | 661 | "Inserts the previous element of the minibuffer history into the minibuffer." |
2076c87c | 662 | (interactive "p") |
2c5e21c1 | 663 | (next-history-element (- n))) |
d0678801 RM |
664 | |
665 | (defun next-complete-history-element (n) | |
1f6fcec3 | 666 | "Get next element of history which is a completion of minibuffer contents." |
d0678801 | 667 | (interactive "p") |
b5e6f936 RM |
668 | (let ((point-at-start (point))) |
669 | (next-matching-history-element | |
670 | (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n) | |
671 | ;; next-matching-history-element always puts us at (point-min). | |
672 | ;; Move to the position we were at before changing the buffer contents. | |
673 | ;; This is still sensical, because the text before point has not changed. | |
674 | (goto-char point-at-start))) | |
d0678801 RM |
675 | |
676 | (defun previous-complete-history-element (n) | |
1f6fcec3 RS |
677 | "\ |
678 | Get previous element of history which is a completion of minibuffer contents." | |
d0678801 RM |
679 | (interactive "p") |
680 | (next-complete-history-element (- n))) | |
e91f80c4 | 681 | \f |
2076c87c JB |
682 | (defun goto-line (arg) |
683 | "Goto line ARG, counting from line 1 at beginning of buffer." | |
684 | (interactive "NGoto line: ") | |
5f1a943c | 685 | (setq arg (prefix-numeric-value arg)) |
2076c87c JB |
686 | (save-restriction |
687 | (widen) | |
688 | (goto-char 1) | |
689 | (if (eq selective-display t) | |
690 | (re-search-forward "[\n\C-m]" nil 'end (1- arg)) | |
691 | (forward-line (1- arg))))) | |
692 | ||
693 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg | |
dff7d67f | 694 | (define-function 'advertised-undo 'undo) |
2076c87c JB |
695 | |
696 | (defun undo (&optional arg) | |
697 | "Undo some previous changes. | |
698 | Repeat this command to undo more changes. | |
699 | A numeric argument serves as a repeat count." | |
700 | (interactive "*p") | |
456c617c RS |
701 | ;; If we don't get all the way thru, make last-command indicate that |
702 | ;; for the following command. | |
703 | (setq this-command t) | |
b553cffa RS |
704 | (let ((modified (buffer-modified-p)) |
705 | (recent-save (recent-auto-save-p))) | |
71e40adf JB |
706 | (or (eq (selected-window) (minibuffer-window)) |
707 | (message "Undo!")) | |
2076c87c JB |
708 | (or (eq last-command 'undo) |
709 | (progn (undo-start) | |
710 | (undo-more 1))) | |
2076c87c | 711 | (undo-more (or arg 1)) |
2512c9f0 RS |
712 | ;; Don't specify a position in the undo record for the undo command. |
713 | ;; Instead, undoing this should move point to where the change is. | |
714 | (let ((tail buffer-undo-list) | |
715 | done) | |
716 | (while (and tail (not done) (not (null (car tail)))) | |
717 | (if (integerp (car tail)) | |
718 | (progn | |
719 | (setq done t) | |
720 | (setq buffer-undo-list (delq (car tail) buffer-undo-list)))) | |
721 | (setq tail (cdr tail)))) | |
2076c87c | 722 | (and modified (not (buffer-modified-p)) |
456c617c RS |
723 | (delete-auto-save-file-if-necessary recent-save))) |
724 | ;; If we do get all the way thru, make this-command indicate that. | |
725 | (setq this-command 'undo)) | |
2076c87c | 726 | |
278b0a58 RS |
727 | (defvar pending-undo-list nil |
728 | "Within a run of consecutive undo commands, list remaining to be undone.") | |
729 | ||
2076c87c | 730 | (defun undo-start () |
ff1fbe3e RS |
731 | "Set `pending-undo-list' to the front of the undo list. |
732 | The next call to `undo-more' will undo the most recently made change." | |
2076c87c JB |
733 | (if (eq buffer-undo-list t) |
734 | (error "No undo information in this buffer")) | |
735 | (setq pending-undo-list buffer-undo-list)) | |
736 | ||
737 | (defun undo-more (count) | |
738 | "Undo back N undo-boundaries beyond what was already undone recently. | |
ff1fbe3e RS |
739 | Call `undo-start' to get ready to undo recent changes, |
740 | then call `undo-more' one or more times to undo them." | |
2076c87c JB |
741 | (or pending-undo-list |
742 | (error "No further undo information")) | |
743 | (setq pending-undo-list (primitive-undo count pending-undo-list))) | |
744 | ||
009ef402 RS |
745 | (defvar shell-command-history nil |
746 | "History list for some commands that read shell commands.") | |
747 | ||
59fc41e5 RS |
748 | (defvar shell-command-switch "-c" |
749 | "Switch used to have the shell execute its command line argument.") | |
750 | ||
d0d74413 | 751 | (defun shell-command (command &optional output-buffer) |
2076c87c | 752 | "Execute string COMMAND in inferior shell; display output, if any. |
d382f610 | 753 | |
2076c87c | 754 | If COMMAND ends in ampersand, execute it asynchronously. |
d382f610 | 755 | The output appears in the buffer `*Async Shell Command*'. |
bcad4985 | 756 | That buffer is in shell mode. |
d382f610 | 757 | |
bcad4985 KH |
758 | Otherwise, COMMAND is executed synchronously. The output appears in the |
759 | buffer `*Shell Command Output*'. | |
d382f610 RS |
760 | If the output is one line, it is displayed in the echo area *as well*, |
761 | but it is nonetheless available in buffer `*Shell Command Output*', | |
762 | even though that buffer is not automatically displayed. | |
763 | If there is no output, or if output is inserted in the current buffer, | |
764 | then `*Shell Command Output*' is deleted. | |
d0d74413 RS |
765 | |
766 | The optional second argument OUTPUT-BUFFER, if non-nil, | |
767 | says to put the output in some other buffer. | |
768 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
769 | If OUTPUT-BUFFER is not a buffer and not nil, | |
770 | insert output in current buffer. (This cannot be done asynchronously.) | |
771 | In either case, the output is inserted after point (leaving mark after it)." | |
aa00b92d RS |
772 | (interactive (list (read-from-minibuffer "Shell command: " |
773 | nil nil nil 'shell-command-history) | |
774 | current-prefix-arg)) | |
d0d74413 RS |
775 | (if (and output-buffer |
776 | (not (or (bufferp output-buffer) (stringp output-buffer)))) | |
2076c87c JB |
777 | (progn (barf-if-buffer-read-only) |
778 | (push-mark) | |
779 | ;; We do not use -f for csh; we will not support broken use of | |
780 | ;; .cshrcs. Even the BSD csh manual says to use | |
781 | ;; "if ($?prompt) exit" before things which are not useful | |
782 | ;; non-interactively. Besides, if someone wants their other | |
783 | ;; aliases for shell commands then they can still have them. | |
784 | (call-process shell-file-name nil t nil | |
59fc41e5 | 785 | shell-command-switch command) |
bcad4985 KH |
786 | ;; This is like exchange-point-and-mark, but doesn't |
787 | ;; activate the mark. It is cleaner to avoid activation, | |
788 | ;; even though the command loop would deactivate the mark | |
789 | ;; because we inserted text. | |
c3e46f0c RS |
790 | (goto-char (prog1 (mark t) |
791 | (set-marker (mark-marker) (point) | |
792 | (current-buffer))))) | |
2076c87c | 793 | ;; Preserve the match data in case called from a program. |
bcad4985 KH |
794 | (save-match-data |
795 | (if (string-match "[ \t]*&[ \t]*$" command) | |
796 | ;; Command ending with ampersand means asynchronous. | |
797 | (let ((buffer (get-buffer-create | |
1f238ac2 | 798 | (or output-buffer "*Async Shell Command*"))) |
bcad4985 KH |
799 | (directory default-directory) |
800 | proc) | |
801 | ;; Remove the ampersand. | |
802 | (setq command (substring command 0 (match-beginning 0))) | |
803 | ;; If will kill a process, query first. | |
804 | (setq proc (get-buffer-process buffer)) | |
805 | (if proc | |
806 | (if (yes-or-no-p "A command is running. Kill it? ") | |
807 | (kill-process proc) | |
808 | (error "Shell command in progress"))) | |
809 | (save-excursion | |
810 | (set-buffer buffer) | |
811 | (setq buffer-read-only nil) | |
812 | (erase-buffer) | |
813 | (display-buffer buffer) | |
814 | (setq default-directory directory) | |
815 | (setq proc (start-process "Shell" buffer shell-file-name | |
816 | shell-command-switch command)) | |
817 | (setq mode-line-process '(":%s")) | |
818 | (require 'shell) (shell-mode) | |
819 | (set-process-sentinel proc 'shell-command-sentinel) | |
820 | )) | |
821 | (shell-command-on-region (point) (point) command nil) | |
822 | )))) | |
2076c87c JB |
823 | |
824 | ;; We have a sentinel to prevent insertion of a termination message | |
825 | ;; in the buffer itself. | |
826 | (defun shell-command-sentinel (process signal) | |
bcad4985 KH |
827 | (if (memq (process-status process) '(exit signal)) |
828 | (message "%s: %s." | |
829 | (car (cdr (cdr (process-command process)))) | |
830 | (substring signal 0 -1)))) | |
2076c87c | 831 | |
d0d74413 | 832 | (defun shell-command-on-region (start end command |
56c0450e | 833 | &optional output-buffer replace) |
2076c87c JB |
834 | "Execute string COMMAND in inferior shell with region as input. |
835 | Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
836 | Prefix arg means replace the region with it. | |
56c0450e RS |
837 | |
838 | The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE. | |
839 | If REPLACE is non-nil, that means insert the output | |
4d9bd664 | 840 | in place of text from START to END, putting point and mark around it. |
2076c87c JB |
841 | |
842 | If the output is one line, it is displayed in the echo area, | |
843 | but it is nonetheless available in buffer `*Shell Command Output*' | |
56c0450e | 844 | even though that buffer is not automatically displayed. |
c42f586d | 845 | If there is no output, or if output is inserted in the current buffer, |
56c0450e | 846 | then `*Shell Command Output*' is deleted. |
d0d74413 | 847 | |
56c0450e RS |
848 | If the optional fourth argument OUTPUT-BUFFER is non-nil, |
849 | that says to put the output in some other buffer. | |
d0d74413 RS |
850 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. |
851 | If OUTPUT-BUFFER is not a buffer and not nil, | |
852 | insert output in the current buffer. | |
853 | In either case, the output is inserted after point (leaving mark after it)." | |
cae49185 RS |
854 | (interactive (let ((string |
855 | ;; Do this before calling region-beginning | |
856 | ;; and region-end, in case subprocess output | |
857 | ;; relocates them while we are in the minibuffer. | |
858 | (read-from-minibuffer "Shell command on region: " | |
859 | nil nil nil | |
860 | 'shell-command-history))) | |
2b03c506 RS |
861 | ;; call-interactively recognizes region-beginning and |
862 | ;; region-end specially, leaving them in the history. | |
863 | (list (region-beginning) (region-end) | |
cae49185 RS |
864 | string |
865 | current-prefix-arg | |
4d9bd664 RS |
866 | current-prefix-arg))) |
867 | (if (or replace | |
868 | (and output-buffer | |
29fc44dd KH |
869 | (not (or (bufferp output-buffer) (stringp output-buffer)))) |
870 | (equal (buffer-name (current-buffer)) "*Shell Command Output*")) | |
2076c87c | 871 | ;; Replace specified region with output from command. |
6db3a963 | 872 | (let ((swap (and replace (< start end)))) |
4d9bd664 RS |
873 | ;; Don't muck with mark unless REPLACE says we should. |
874 | (goto-char start) | |
56c0450e | 875 | (and replace (push-mark)) |
2076c87c | 876 | (call-process-region start end shell-file-name t t nil |
59fc41e5 | 877 | shell-command-switch command) |
b5f7c943 KH |
878 | (let ((shell-buffer (get-buffer "*Shell Command Output*"))) |
879 | (and shell-buffer (not (eq shell-buffer (current-buffer))) | |
880 | (kill-buffer shell-buffer))) | |
4d9bd664 | 881 | ;; Don't muck with mark unless REPLACE says we should. |
56c0450e | 882 | (and replace swap (exchange-point-and-mark))) |
2076c87c JB |
883 | ;; No prefix argument: put the output in a temp buffer, |
884 | ;; replacing its entire contents. | |
d0d74413 RS |
885 | (let ((buffer (get-buffer-create |
886 | (or output-buffer "*Shell Command Output*"))) | |
34ee0963 RS |
887 | (success nil)) |
888 | (unwind-protect | |
889 | (if (eq buffer (current-buffer)) | |
890 | ;; If the input is the same buffer as the output, | |
891 | ;; delete everything but the specified region, | |
892 | ;; then replace that region with the output. | |
a9594ce3 | 893 | (progn (setq buffer-read-only nil) |
6db3a963 RS |
894 | (delete-region (max start end) (point-max)) |
895 | (delete-region (point-min) (max start end)) | |
34ee0963 RS |
896 | (call-process-region (point-min) (point-max) |
897 | shell-file-name t t nil | |
59fc41e5 | 898 | shell-command-switch command) |
34ee0963 RS |
899 | (setq success t)) |
900 | ;; Clear the output buffer, then run the command with output there. | |
901 | (save-excursion | |
902 | (set-buffer buffer) | |
a9594ce3 | 903 | (setq buffer-read-only nil) |
34ee0963 RS |
904 | (erase-buffer)) |
905 | (call-process-region start end shell-file-name | |
906 | nil buffer nil | |
59fc41e5 | 907 | shell-command-switch command) |
34ee0963 RS |
908 | (setq success t)) |
909 | ;; Report the amount of output. | |
910 | (let ((lines (save-excursion | |
911 | (set-buffer buffer) | |
912 | (if (= (buffer-size) 0) | |
913 | 0 | |
914 | (count-lines (point-min) (point-max)))))) | |
915 | (cond ((= lines 0) | |
916 | (if success | |
917 | (message "(Shell command completed with no output)")) | |
918 | (kill-buffer buffer)) | |
919 | ((and success (= lines 1)) | |
920 | (message "%s" | |
921 | (save-excursion | |
922 | (set-buffer buffer) | |
923 | (goto-char (point-min)) | |
924 | (buffer-substring (point) | |
4ec982c5 | 925 | (progn (end-of-line) (point)))))) |
34ee0963 RS |
926 | (t |
927 | (set-window-start (display-buffer buffer) 1)))))))) | |
2076c87c | 928 | \f |
69d4c3c4 KH |
929 | (defconst universal-argument-map |
930 | (let ((map (make-sparse-keymap))) | |
931 | (define-key map [t] 'universal-argument-other-key) | |
b9ff190d | 932 | (define-key map (vector meta-prefix-char t) 'universal-argument-other-key) |
69d4c3c4 KH |
933 | (define-key map [switch-frame] nil) |
934 | (define-key map [?\C-u] 'universal-argument-more) | |
935 | (define-key map [?-] 'universal-argument-minus) | |
936 | (define-key map [?0] 'digit-argument) | |
937 | (define-key map [?1] 'digit-argument) | |
938 | (define-key map [?2] 'digit-argument) | |
939 | (define-key map [?3] 'digit-argument) | |
940 | (define-key map [?4] 'digit-argument) | |
941 | (define-key map [?5] 'digit-argument) | |
942 | (define-key map [?6] 'digit-argument) | |
943 | (define-key map [?7] 'digit-argument) | |
944 | (define-key map [?8] 'digit-argument) | |
945 | (define-key map [?9] 'digit-argument) | |
946 | map) | |
947 | "Keymap used while processing \\[universal-argument].") | |
948 | ||
0de84e16 RS |
949 | (defvar universal-argument-num-events nil |
950 | "Number of argument-specifying events read by `universal-argument'. | |
951 | `universal-argument-other-key' uses this to discard those events | |
952 | from (this-command-keys), and reread only the final command.") | |
953 | ||
e8d1a377 KH |
954 | (defun universal-argument () |
955 | "Begin a numeric argument for the following command. | |
956 | Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
957 | \\[universal-argument] following the digits or minus sign ends the argument. | |
958 | \\[universal-argument] without digits or minus sign provides 4 as argument. | |
959 | Repeating \\[universal-argument] without digits or minus sign | |
960 | multiplies the argument by 4 each time." | |
69d4c3c4 KH |
961 | (interactive) |
962 | (setq prefix-arg (list 4)) | |
0de84e16 | 963 | (setq universal-argument-num-events (length (this-command-keys))) |
69d4c3c4 | 964 | (setq overriding-terminal-local-map universal-argument-map)) |
e8d1a377 | 965 | |
69d4c3c4 KH |
966 | ;; A subsequent C-u means to multiply the factor by 4 if we've typed |
967 | ;; nothing but C-u's; otherwise it means to terminate the prefix arg. | |
968 | (defun universal-argument-more (arg) | |
e8d1a377 | 969 | (interactive "P") |
69d4c3c4 KH |
970 | (if (consp arg) |
971 | (setq prefix-arg (list (* 4 (car arg)))) | |
972 | (setq prefix-arg arg) | |
0de84e16 RS |
973 | (setq overriding-terminal-local-map nil)) |
974 | (setq universal-argument-num-events (length (this-command-keys)))) | |
e8d1a377 KH |
975 | |
976 | (defun negative-argument (arg) | |
977 | "Begin a negative numeric argument for the next command. | |
978 | \\[universal-argument] following digits or minus sign ends the argument." | |
979 | (interactive "P") | |
69d4c3c4 KH |
980 | (cond ((integerp arg) |
981 | (setq prefix-arg (- arg))) | |
982 | ((eq arg '-) | |
983 | (setq prefix-arg nil)) | |
984 | (t | |
b9ff190d | 985 | (setq prefix-arg '-))) |
0de84e16 | 986 | (setq universal-argument-num-events (length (this-command-keys))) |
b9ff190d | 987 | (setq overriding-terminal-local-map universal-argument-map)) |
69d4c3c4 KH |
988 | |
989 | (defun digit-argument (arg) | |
990 | "Part of the numeric argument for the next command. | |
991 | \\[universal-argument] following digits or minus sign ends the argument." | |
992 | (interactive "P") | |
993 | (let ((digit (- (logand last-command-char ?\177) ?0))) | |
994 | (cond ((integerp arg) | |
995 | (setq prefix-arg (+ (* arg 10) | |
996 | (if (< arg 0) (- digit) digit)))) | |
997 | ((eq arg '-) | |
998 | ;; Treat -0 as just -, so that -01 will work. | |
999 | (setq prefix-arg (if (zerop digit) '- (- digit)))) | |
1000 | (t | |
b9ff190d | 1001 | (setq prefix-arg digit)))) |
0de84e16 | 1002 | (setq universal-argument-num-events (length (this-command-keys))) |
b9ff190d | 1003 | (setq overriding-terminal-local-map universal-argument-map)) |
69d4c3c4 KH |
1004 | |
1005 | ;; For backward compatibility, minus with no modifiers is an ordinary | |
1006 | ;; command if digits have already been entered. | |
1007 | (defun universal-argument-minus (arg) | |
1008 | (interactive "P") | |
1009 | (if (integerp arg) | |
1010 | (universal-argument-other-key arg) | |
1011 | (negative-argument arg))) | |
1012 | ||
1013 | ;; Anything else terminates the argument and is left in the queue to be | |
1014 | ;; executed as a command. | |
1015 | (defun universal-argument-other-key (arg) | |
1016 | (interactive "P") | |
1017 | (setq prefix-arg arg) | |
0de84e16 RS |
1018 | (let* ((key (this-command-keys)) |
1019 | (keylist (listify-key-sequence key))) | |
1020 | (setq unread-command-events | |
06697cdb RS |
1021 | (append (nthcdr universal-argument-num-events keylist) |
1022 | unread-command-events))) | |
f0ef2555 | 1023 | (reset-this-command-lengths) |
69d4c3c4 | 1024 | (setq overriding-terminal-local-map nil)) |
e8d1a377 | 1025 | \f |
2076c87c JB |
1026 | (defun forward-to-indentation (arg) |
1027 | "Move forward ARG lines and position at first nonblank character." | |
1028 | (interactive "p") | |
1029 | (forward-line arg) | |
1030 | (skip-chars-forward " \t")) | |
1031 | ||
1032 | (defun backward-to-indentation (arg) | |
1033 | "Move backward ARG lines and position at first nonblank character." | |
1034 | (interactive "p") | |
1035 | (forward-line (- arg)) | |
1036 | (skip-chars-forward " \t")) | |
1037 | ||
38ebcf29 | 1038 | (defvar kill-whole-line nil |
dff7d67f | 1039 | "*If non-nil, `kill-line' with no arg at beg of line kills the whole line.") |
38ebcf29 | 1040 | |
2076c87c | 1041 | (defun kill-line (&optional arg) |
dff7d67f | 1042 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. |
2076c87c JB |
1043 | With prefix argument, kill that many lines from point. |
1044 | Negative arguments kill lines backward. | |
1045 | ||
1046 | When calling from a program, nil means \"no arg\", | |
dff7d67f RS |
1047 | a number counts as a prefix arg. |
1048 | ||
1049 | If `kill-whole-line' is non-nil, then kill the whole line | |
1050 | when given no argument at the beginning of a line." | |
2076c87c JB |
1051 | (interactive "P") |
1052 | (kill-region (point) | |
e6291fe1 RS |
1053 | ;; It is better to move point to the other end of the kill |
1054 | ;; before killing. That way, in a read-only buffer, point | |
1055 | ;; moves across the text that is copied to the kill ring. | |
1056 | ;; The choice has no effect on undo now that undo records | |
1057 | ;; the value of point from before the command was run. | |
1058 | (progn | |
2076c87c JB |
1059 | (if arg |
1060 | (forward-line (prefix-numeric-value arg)) | |
1061 | (if (eobp) | |
1062 | (signal 'end-of-buffer nil)) | |
38ebcf29 | 1063 | (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp))) |
2076c87c JB |
1064 | (forward-line 1) |
1065 | (end-of-line))) | |
1066 | (point)))) | |
1067 | \f | |
70e14c01 JB |
1068 | ;;;; Window system cut and paste hooks. |
1069 | ||
1070 | (defvar interprogram-cut-function nil | |
1071 | "Function to call to make a killed region available to other programs. | |
1072 | ||
1073 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
1074 | pasting text between the windows of different programs. |
1075 | This variable holds a function that Emacs calls whenever text | |
1076 | is put in the kill ring, to make the new kill available to other | |
70e14c01 JB |
1077 | programs. |
1078 | ||
9f112a3d RS |
1079 | The function takes one or two arguments. |
1080 | The first argument, TEXT, is a string containing | |
1081 | the text which should be made available. | |
1082 | The second, PUSH, if non-nil means this is a \"new\" kill; | |
1083 | nil means appending to an \"old\" kill.") | |
70e14c01 JB |
1084 | |
1085 | (defvar interprogram-paste-function nil | |
1086 | "Function to call to get text cut from other programs. | |
1087 | ||
1088 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
1089 | pasting text between the windows of different programs. |
1090 | This variable holds a function that Emacs calls to obtain | |
70e14c01 JB |
1091 | text that other programs have provided for pasting. |
1092 | ||
1093 | The function should be called with no arguments. If the function | |
1094 | returns nil, then no other program has provided such text, and the top | |
1095 | of the Emacs kill ring should be used. If the function returns a | |
daa37602 JB |
1096 | string, that string should be put in the kill ring as the latest kill. |
1097 | ||
1098 | Note that the function should return a string only if a program other | |
1099 | than Emacs has provided a string for pasting; if Emacs provided the | |
1100 | most recent string, the function should return nil. If it is | |
1101 | difficult to tell whether Emacs or some other program provided the | |
1102 | current string, it is probably good enough to return nil if the string | |
1103 | is equal (according to `string=') to the last text Emacs provided.") | |
70e14c01 JB |
1104 | |
1105 | ||
1106 | \f | |
1107 | ;;;; The kill ring data structure. | |
2076c87c JB |
1108 | |
1109 | (defvar kill-ring nil | |
70e14c01 JB |
1110 | "List of killed text sequences. |
1111 | Since the kill ring is supposed to interact nicely with cut-and-paste | |
1112 | facilities offered by window systems, use of this variable should | |
1113 | interact nicely with `interprogram-cut-function' and | |
1114 | `interprogram-paste-function'. The functions `kill-new', | |
1115 | `kill-append', and `current-kill' are supposed to implement this | |
1116 | interaction; you may want to use them instead of manipulating the kill | |
1117 | ring directly.") | |
2076c87c JB |
1118 | |
1119 | (defconst kill-ring-max 30 | |
1120 | "*Maximum length of kill ring before oldest elements are thrown away.") | |
1121 | ||
1122 | (defvar kill-ring-yank-pointer nil | |
1123 | "The tail of the kill ring whose car is the last thing yanked.") | |
1124 | ||
f914dc91 | 1125 | (defun kill-new (string &optional replace) |
70e14c01 JB |
1126 | "Make STRING the latest kill in the kill ring. |
1127 | Set the kill-ring-yank pointer to point to it. | |
f914dc91 KH |
1128 | If `interprogram-cut-function' is non-nil, apply it to STRING. |
1129 | Optional second argument REPLACE non-nil means that STRING will replace | |
1130 | the front of the kill ring, rather than being added to the list." | |
f1d01ba2 KH |
1131 | (and (fboundp 'menu-bar-update-yank-menu) |
1132 | (menu-bar-update-yank-menu string (and replace (car kill-ring)))) | |
f914dc91 KH |
1133 | (if replace |
1134 | (setcar kill-ring string) | |
1135 | (setq kill-ring (cons string kill-ring)) | |
1136 | (if (> (length kill-ring) kill-ring-max) | |
1137 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) | |
70e14c01 JB |
1138 | (setq kill-ring-yank-pointer kill-ring) |
1139 | (if interprogram-cut-function | |
657a33ab | 1140 | (funcall interprogram-cut-function string (not replace)))) |
70e14c01 | 1141 | |
2076c87c | 1142 | (defun kill-append (string before-p) |
70e14c01 JB |
1143 | "Append STRING to the end of the latest kill in the kill ring. |
1144 | If BEFORE-P is non-nil, prepend STRING to the kill. | |
88c1aa79 | 1145 | If `interprogram-cut-function' is set, pass the resulting kill to |
70e14c01 | 1146 | it." |
f914dc91 KH |
1147 | (kill-new (if before-p |
1148 | (concat string (car kill-ring)) | |
1149 | (concat (car kill-ring) string)) t)) | |
70e14c01 JB |
1150 | |
1151 | (defun current-kill (n &optional do-not-move) | |
1152 | "Rotate the yanking point by N places, and then return that kill. | |
1153 | If N is zero, `interprogram-paste-function' is set, and calling it | |
1154 | returns a string, then that string is added to the front of the | |
1155 | kill ring and returned as the latest kill. | |
1156 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the | |
1157 | yanking point; just return the Nth kill forward." | |
1158 | (let ((interprogram-paste (and (= n 0) | |
1159 | interprogram-paste-function | |
1160 | (funcall interprogram-paste-function)))) | |
1161 | (if interprogram-paste | |
1162 | (progn | |
1163 | ;; Disable the interprogram cut function when we add the new | |
1164 | ;; text to the kill ring, so Emacs doesn't try to own the | |
1165 | ;; selection, with identical text. | |
1166 | (let ((interprogram-cut-function nil)) | |
1167 | (kill-new interprogram-paste)) | |
1168 | interprogram-paste) | |
1169 | (or kill-ring (error "Kill ring is empty")) | |
47096a67 PE |
1170 | (let ((ARGth-kill-element |
1171 | (nthcdr (mod (- n (length kill-ring-yank-pointer)) | |
1172 | (length kill-ring)) | |
1173 | kill-ring))) | |
70e14c01 JB |
1174 | (or do-not-move |
1175 | (setq kill-ring-yank-pointer ARGth-kill-element)) | |
1176 | (car ARGth-kill-element))))) | |
c88ab9ce | 1177 | |
c88ab9ce | 1178 | |
70e14c01 JB |
1179 | \f |
1180 | ;;;; Commands for manipulating the kill ring. | |
c88ab9ce | 1181 | |
e6291fe1 RS |
1182 | (defvar kill-read-only-ok nil |
1183 | "*Non-nil means don't signal an error for killing read-only text.") | |
1184 | ||
2076c87c JB |
1185 | (defun kill-region (beg end) |
1186 | "Kill between point and mark. | |
1187 | The text is deleted but saved in the kill ring. | |
1188 | The command \\[yank] can retrieve it from there. | |
1189 | \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].) | |
2aa7a8bf JB |
1190 | If the buffer is read-only, Emacs will beep and refrain from deleting |
1191 | the text, but put the text in the kill ring anyway. This means that | |
1192 | you can use the killing commands to copy text from a read-only buffer. | |
2076c87c JB |
1193 | |
1194 | This is the primitive for programs to kill text (as opposed to deleting it). | |
1195 | Supply two arguments, character numbers indicating the stretch of text | |
1196 | to be killed. | |
1197 | Any command that calls this function is a \"kill command\". | |
1198 | If the previous command was also a kill command, | |
1199 | the text killed this time appends to the text killed last time | |
1200 | to make one entry in the kill ring." | |
2aa7a8bf | 1201 | (interactive "r") |
70e14c01 | 1202 | (cond |
2aa7a8bf JB |
1203 | |
1204 | ;; If the buffer is read-only, we should beep, in case the person | |
1205 | ;; just isn't aware of this. However, there's no harm in putting | |
1206 | ;; the region's text in the kill ring, anyway. | |
e6291fe1 RS |
1207 | ((or (and buffer-read-only (not inhibit-read-only)) |
1208 | (text-property-not-all beg end 'read-only nil)) | |
2aa7a8bf | 1209 | (copy-region-as-kill beg end) |
1537a263 | 1210 | ;; This should always barf, and give us the correct error. |
e6291fe1 RS |
1211 | (if kill-read-only-ok |
1212 | (message "Read only text copied to kill ring") | |
626a097c | 1213 | (setq this-command 'kill-region) |
e6291fe1 | 1214 | (barf-if-buffer-read-only))) |
2aa7a8bf JB |
1215 | |
1216 | ;; In certain cases, we can arrange for the undo list and the kill | |
1217 | ;; ring to share the same string object. This code does that. | |
70e14c01 JB |
1218 | ((not (or (eq buffer-undo-list t) |
1219 | (eq last-command 'kill-region) | |
713dca1c RS |
1220 | ;; Use = since positions may be numbers or markers. |
1221 | (= beg end))) | |
70e14c01 | 1222 | ;; Don't let the undo list be truncated before we can even access it. |
12bcd3b6 RS |
1223 | (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100)) |
1224 | (old-list buffer-undo-list) | |
1225 | tail) | |
70e14c01 | 1226 | (delete-region beg end) |
12bcd3b6 RS |
1227 | ;; Search back in buffer-undo-list for this string, |
1228 | ;; in case a change hook made property changes. | |
1229 | (setq tail buffer-undo-list) | |
1230 | (while (not (stringp (car (car tail)))) | |
1231 | (setq tail (cdr tail))) | |
70e14c01 JB |
1232 | ;; Take the same string recorded for undo |
1233 | ;; and put it in the kill-ring. | |
626a097c | 1234 | (kill-new (car (car tail))))) |
2aa7a8bf | 1235 | |
70e14c01 | 1236 | (t |
2076c87c | 1237 | (copy-region-as-kill beg end) |
626a097c KH |
1238 | (delete-region beg end))) |
1239 | (setq this-command 'kill-region)) | |
2076c87c | 1240 | |
a382890a KH |
1241 | ;; copy-region-as-kill no longer sets this-command, because it's confusing |
1242 | ;; to get two copies of the text when the user accidentally types M-w and | |
1243 | ;; then corrects it with the intended C-w. | |
2076c87c JB |
1244 | (defun copy-region-as-kill (beg end) |
1245 | "Save the region as if killed, but don't kill it. | |
46947372 JB |
1246 | If `interprogram-cut-function' is non-nil, also save the text for a window |
1247 | system cut and paste." | |
2076c87c JB |
1248 | (interactive "r") |
1249 | (if (eq last-command 'kill-region) | |
1250 | (kill-append (buffer-substring beg end) (< end beg)) | |
70e14c01 | 1251 | (kill-new (buffer-substring beg end))) |
2076c87c JB |
1252 | nil) |
1253 | ||
1254 | (defun kill-ring-save (beg end) | |
0964e562 | 1255 | "Save the region as if killed, but don't kill it. |
230c6b36 | 1256 | This command is similar to `copy-region-as-kill', except that it gives |
0964e562 JB |
1257 | visual feedback indicating the extent of the region being copied. |
1258 | If `interprogram-cut-function' is non-nil, also save the text for a window | |
1259 | system cut and paste." | |
2076c87c JB |
1260 | (interactive "r") |
1261 | (copy-region-as-kill beg end) | |
3a801d0c | 1262 | (if (interactive-p) |
66050f10 RS |
1263 | (let ((other-end (if (= (point) beg) end beg)) |
1264 | (opoint (point)) | |
1265 | ;; Inhibit quitting so we can make a quit here | |
1266 | ;; look like a C-g typed as a command. | |
1267 | (inhibit-quit t)) | |
1268 | (if (pos-visible-in-window-p other-end (selected-window)) | |
1269 | (progn | |
1270 | ;; Swap point and mark. | |
1271 | (set-marker (mark-marker) (point) (current-buffer)) | |
1272 | (goto-char other-end) | |
1273 | (sit-for 1) | |
1274 | ;; Swap back. | |
1275 | (set-marker (mark-marker) other-end (current-buffer)) | |
1276 | (goto-char opoint) | |
1277 | ;; If user quit, deactivate the mark | |
1278 | ;; as C-g would as a command. | |
e4e593ae | 1279 | (and quit-flag mark-active |
fcadf1c7 | 1280 | (deactivate-mark))) |
66050f10 RS |
1281 | (let* ((killed-text (current-kill 0)) |
1282 | (message-len (min (length killed-text) 40))) | |
1283 | (if (= (point) beg) | |
1284 | ;; Don't say "killed"; that is misleading. | |
1285 | (message "Saved text until \"%s\"" | |
1286 | (substring killed-text (- message-len))) | |
1287 | (message "Saved text from \"%s\"" | |
1288 | (substring killed-text 0 message-len)))))))) | |
2076c87c JB |
1289 | |
1290 | (defun append-next-kill () | |
ff1fbe3e | 1291 | "Cause following command, if it kills, to append to previous kill." |
2076c87c JB |
1292 | (interactive) |
1293 | (if (interactive-p) | |
1294 | (progn | |
1295 | (setq this-command 'kill-region) | |
1296 | (message "If the next command is a kill, it will append")) | |
1297 | (setq last-command 'kill-region))) | |
1298 | ||
2076c87c | 1299 | (defun yank-pop (arg) |
ff1fbe3e RS |
1300 | "Replace just-yanked stretch of killed text with a different stretch. |
1301 | This command is allowed only immediately after a `yank' or a `yank-pop'. | |
2076c87c | 1302 | At such a time, the region contains a stretch of reinserted |
ff1fbe3e | 1303 | previously-killed text. `yank-pop' deletes that text and inserts in its |
2076c87c JB |
1304 | place a different stretch of killed text. |
1305 | ||
1306 | With no argument, the previous kill is inserted. | |
ff1fbe3e RS |
1307 | With argument N, insert the Nth previous kill. |
1308 | If N is negative, this is a more recent kill. | |
2076c87c JB |
1309 | |
1310 | The sequence of kills wraps around, so that after the oldest one | |
1311 | comes the newest one." | |
1312 | (interactive "*p") | |
1313 | (if (not (eq last-command 'yank)) | |
1314 | (error "Previous command was not a yank")) | |
1315 | (setq this-command 'yank) | |
9a1277dd RS |
1316 | (let ((before (< (point) (mark t)))) |
1317 | (delete-region (point) (mark t)) | |
fd0f4056 | 1318 | (set-marker (mark-marker) (point) (current-buffer)) |
70e14c01 | 1319 | (insert (current-kill arg)) |
fd0f4056 RS |
1320 | (if before |
1321 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. | |
1322 | ;; It is cleaner to avoid activation, even though the command | |
1323 | ;; loop would deactivate the mark because we inserted text. | |
1324 | (goto-char (prog1 (mark t) | |
1325 | (set-marker (mark-marker) (point) (current-buffer)))))) | |
0964e562 | 1326 | nil) |
2076c87c JB |
1327 | |
1328 | (defun yank (&optional arg) | |
1329 | "Reinsert the last stretch of killed text. | |
1330 | More precisely, reinsert the stretch of killed text most recently | |
ff1fbe3e RS |
1331 | killed OR yanked. Put point at end, and set mark at beginning. |
1332 | With just C-u as argument, same but put point at beginning (and mark at end). | |
1333 | With argument N, reinsert the Nth most recently killed stretch of killed | |
2076c87c JB |
1334 | text. |
1335 | See also the command \\[yank-pop]." | |
1336 | (interactive "*P") | |
456c617c RS |
1337 | ;; If we don't get all the way thru, make last-command indicate that |
1338 | ;; for the following command. | |
1339 | (setq this-command t) | |
2076c87c | 1340 | (push-mark (point)) |
70e14c01 JB |
1341 | (insert (current-kill (cond |
1342 | ((listp arg) 0) | |
1343 | ((eq arg '-) -1) | |
1344 | (t (1- arg))))) | |
2076c87c | 1345 | (if (consp arg) |
fd0f4056 RS |
1346 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. |
1347 | ;; It is cleaner to avoid activation, even though the command | |
1348 | ;; loop would deactivate the mark because we inserted text. | |
1349 | (goto-char (prog1 (mark t) | |
1350 | (set-marker (mark-marker) (point) (current-buffer))))) | |
456c617c RS |
1351 | ;; If we do get all the way thru, make this-command indicate that. |
1352 | (setq this-command 'yank) | |
0964e562 | 1353 | nil) |
70e14c01 JB |
1354 | |
1355 | (defun rotate-yank-pointer (arg) | |
1356 | "Rotate the yanking point in the kill ring. | |
1357 | With argument, rotate that many kills forward (or backward, if negative)." | |
1358 | (interactive "p") | |
1359 | (current-kill arg)) | |
1360 | ||
2076c87c JB |
1361 | \f |
1362 | (defun insert-buffer (buffer) | |
1363 | "Insert after point the contents of BUFFER. | |
1364 | Puts mark after the inserted text. | |
1365 | BUFFER may be a buffer or a buffer name." | |
58ff020d | 1366 | (interactive (list (progn (barf-if-buffer-read-only) |
8be055fd RS |
1367 | (read-buffer "Insert buffer: " |
1368 | (other-buffer (current-buffer) t) | |
1369 | t)))) | |
2076c87c JB |
1370 | (or (bufferp buffer) |
1371 | (setq buffer (get-buffer buffer))) | |
1372 | (let (start end newmark) | |
1373 | (save-excursion | |
1374 | (save-excursion | |
1375 | (set-buffer buffer) | |
1376 | (setq start (point-min) end (point-max))) | |
1377 | (insert-buffer-substring buffer start end) | |
1378 | (setq newmark (point))) | |
1537a263 JB |
1379 | (push-mark newmark)) |
1380 | nil) | |
2076c87c JB |
1381 | |
1382 | (defun append-to-buffer (buffer start end) | |
1383 | "Append to specified buffer the text of the region. | |
1384 | It is inserted into that buffer before its point. | |
1385 | ||
1386 | When calling from a program, give three arguments: | |
1387 | BUFFER (or buffer name), START and END. | |
1388 | START and END specify the portion of the current buffer to be copied." | |
70e14c01 | 1389 | (interactive |
5d771766 | 1390 | (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t)) |
23efee2c | 1391 | (region-beginning) (region-end))) |
2076c87c JB |
1392 | (let ((oldbuf (current-buffer))) |
1393 | (save-excursion | |
1394 | (set-buffer (get-buffer-create buffer)) | |
1395 | (insert-buffer-substring oldbuf start end)))) | |
1396 | ||
1397 | (defun prepend-to-buffer (buffer start end) | |
1398 | "Prepend to specified buffer the text of the region. | |
1399 | It is inserted into that buffer after its point. | |
1400 | ||
1401 | When calling from a program, give three arguments: | |
1402 | BUFFER (or buffer name), START and END. | |
1403 | START and END specify the portion of the current buffer to be copied." | |
1404 | (interactive "BPrepend to buffer: \nr") | |
1405 | (let ((oldbuf (current-buffer))) | |
1406 | (save-excursion | |
1407 | (set-buffer (get-buffer-create buffer)) | |
1408 | (save-excursion | |
1409 | (insert-buffer-substring oldbuf start end))))) | |
1410 | ||
1411 | (defun copy-to-buffer (buffer start end) | |
1412 | "Copy to specified buffer the text of the region. | |
1413 | It is inserted into that buffer, replacing existing text there. | |
1414 | ||
1415 | When calling from a program, give three arguments: | |
1416 | BUFFER (or buffer name), START and END. | |
1417 | START and END specify the portion of the current buffer to be copied." | |
1418 | (interactive "BCopy to buffer: \nr") | |
1419 | (let ((oldbuf (current-buffer))) | |
1420 | (save-excursion | |
1421 | (set-buffer (get-buffer-create buffer)) | |
1422 | (erase-buffer) | |
1423 | (save-excursion | |
1424 | (insert-buffer-substring oldbuf start end))))) | |
1425 | \f | |
62d1c1fc RM |
1426 | (put 'mark-inactive 'error-conditions '(mark-inactive error)) |
1427 | (put 'mark-inactive 'error-message "The mark is not active now") | |
1428 | ||
af39530e | 1429 | (defun mark (&optional force) |
c7c8b31e | 1430 | "Return this buffer's mark value as integer; error if mark inactive. |
af39530e | 1431 | If optional argument FORCE is non-nil, access the mark value |
c7c8b31e RS |
1432 | even if the mark is not currently active, and return nil |
1433 | if there is no mark at all. | |
af39530e | 1434 | |
2076c87c JB |
1435 | If you are using this in an editing command, you are most likely making |
1436 | a mistake; see the documentation of `set-mark'." | |
0e3a7b14 | 1437 | (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive) |
af39530e | 1438 | (marker-position (mark-marker)) |
62d1c1fc | 1439 | (signal 'mark-inactive nil))) |
2076c87c | 1440 | |
19d35374 RM |
1441 | ;; Many places set mark-active directly, and several of them failed to also |
1442 | ;; run deactivate-mark-hook. This shorthand should simplify. | |
1443 | (defsubst deactivate-mark () | |
1444 | "Deactivate the mark by setting `mark-active' to nil. | |
fcadf1c7 | 1445 | \(That makes a difference only in Transient Mark mode.) |
19d35374 | 1446 | Also runs the hook `deactivate-mark-hook'." |
a4b9d3da RS |
1447 | (if transient-mark-mode |
1448 | (progn | |
1449 | (setq mark-active nil) | |
1450 | (run-hooks 'deactivate-mark-hook)))) | |
19d35374 | 1451 | |
2076c87c JB |
1452 | (defun set-mark (pos) |
1453 | "Set this buffer's mark to POS. Don't use this function! | |
1454 | That is to say, don't use this function unless you want | |
1455 | the user to see that the mark has moved, and you want the previous | |
1456 | mark position to be lost. | |
1457 | ||
1458 | Normally, when a new mark is set, the old one should go on the stack. | |
1459 | This is why most applications should use push-mark, not set-mark. | |
1460 | ||
ff1fbe3e | 1461 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1462 | purposes. The mark saves a location for the user's convenience. |
1463 | Most editing commands should not alter the mark. | |
1464 | To remember a location for internal use in the Lisp program, | |
1465 | store it in a Lisp variable. Example: | |
1466 | ||
1467 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
1468 | ||
fcadf1c7 RS |
1469 | (if pos |
1470 | (progn | |
1471 | (setq mark-active t) | |
1472 | (run-hooks 'activate-mark-hook) | |
1473 | (set-marker (mark-marker) pos (current-buffer))) | |
24c22852 RS |
1474 | ;; Normally we never clear mark-active except in Transient Mark mode. |
1475 | ;; But when we actually clear out the mark value too, | |
1476 | ;; we must clear mark-active in any mode. | |
1477 | (setq mark-active nil) | |
1478 | (run-hooks 'deactivate-mark-hook) | |
1479 | (set-marker (mark-marker) nil))) | |
2076c87c JB |
1480 | |
1481 | (defvar mark-ring nil | |
e55e2267 | 1482 | "The list of former marks of the current buffer, most recent first.") |
2076c87c | 1483 | (make-variable-buffer-local 'mark-ring) |
e55e2267 | 1484 | (put 'mark-ring 'permanent-local t) |
2076c87c JB |
1485 | |
1486 | (defconst mark-ring-max 16 | |
1487 | "*Maximum size of mark ring. Start discarding off end if gets this big.") | |
1488 | ||
dc029f0b RM |
1489 | (defvar global-mark-ring nil |
1490 | "The list of saved global marks, most recent first.") | |
1491 | ||
1492 | (defconst global-mark-ring-max 16 | |
1493 | "*Maximum size of global mark ring. \ | |
1494 | Start discarding off end if gets this big.") | |
1495 | ||
2076c87c JB |
1496 | (defun set-mark-command (arg) |
1497 | "Set mark at where point is, or jump to mark. | |
dc029f0b RM |
1498 | With no prefix argument, set mark, push old mark position on local mark |
1499 | ring, and push mark on global mark ring. | |
1500 | With argument, jump to mark, and pop a new position for mark off the ring | |
1501 | \(does not affect global mark ring\). | |
2076c87c | 1502 | |
ff1fbe3e | 1503 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1504 | purposes. See the documentation of `set-mark' for more information." |
1505 | (interactive "P") | |
1506 | (if (null arg) | |
9a1277dd | 1507 | (progn |
fd0f4056 | 1508 | (push-mark nil nil t)) |
af39530e | 1509 | (if (null (mark t)) |
2076c87c | 1510 | (error "No mark set in this buffer") |
9a1277dd | 1511 | (goto-char (mark t)) |
2076c87c JB |
1512 | (pop-mark)))) |
1513 | ||
fd0f4056 | 1514 | (defun push-mark (&optional location nomsg activate) |
2076c87c | 1515 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. |
f1382a3d RM |
1516 | If the last global mark pushed was not in the current buffer, |
1517 | also push LOCATION on the global mark ring. | |
fd0f4056 | 1518 | Display `Mark set' unless the optional second arg NOMSG is non-nil. |
8cdc660f | 1519 | In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil. |
2076c87c | 1520 | |
ff1fbe3e | 1521 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
9a1277dd RS |
1522 | purposes. See the documentation of `set-mark' for more information. |
1523 | ||
1524 | In Transient Mark mode, this does not activate the mark." | |
af39530e | 1525 | (if (null (mark t)) |
2076c87c JB |
1526 | nil |
1527 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) | |
1528 | (if (> (length mark-ring) mark-ring-max) | |
1529 | (progn | |
1530 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
1531 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) | |
9a1277dd | 1532 | (set-marker (mark-marker) (or location (point)) (current-buffer)) |
dc029f0b | 1533 | ;; Now push the mark on the global mark ring. |
f1382a3d | 1534 | (if (and global-mark-ring |
e08d3f7c | 1535 | (eq (marker-buffer (car global-mark-ring)) (current-buffer))) |
f1382a3d RM |
1536 | ;; The last global mark pushed was in this same buffer. |
1537 | ;; Don't push another one. | |
1538 | nil | |
1539 | (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring)) | |
dc029f0b RM |
1540 | (if (> (length global-mark-ring) global-mark-ring-max) |
1541 | (progn | |
1542 | (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) | |
1543 | nil) | |
f1382a3d | 1544 | (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))) |
2076c87c JB |
1545 | (or nomsg executing-macro (> (minibuffer-depth) 0) |
1546 | (message "Mark set")) | |
8cdc660f RS |
1547 | (if (or activate (not transient-mark-mode)) |
1548 | (set-mark (mark t))) | |
2076c87c JB |
1549 | nil) |
1550 | ||
1551 | (defun pop-mark () | |
1552 | "Pop off mark ring into the buffer's actual mark. | |
1553 | Does not set point. Does nothing if mark ring is empty." | |
1554 | (if mark-ring | |
1555 | (progn | |
1556 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
9a1277dd | 1557 | (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer)) |
19d35374 | 1558 | (deactivate-mark) |
2076c87c | 1559 | (move-marker (car mark-ring) nil) |
9a1277dd | 1560 | (if (null (mark t)) (ding)) |
2076c87c JB |
1561 | (setq mark-ring (cdr mark-ring))))) |
1562 | ||
dff7d67f | 1563 | (define-function 'exchange-dot-and-mark 'exchange-point-and-mark) |
2076c87c | 1564 | (defun exchange-point-and-mark () |
af39530e RS |
1565 | "Put the mark where point is now, and point where the mark is now. |
1566 | This command works even when the mark is not active, | |
1567 | and it reactivates the mark." | |
2076c87c | 1568 | (interactive nil) |
af39530e | 1569 | (let ((omark (mark t))) |
2076c87c JB |
1570 | (if (null omark) |
1571 | (error "No mark set in this buffer")) | |
1572 | (set-mark (point)) | |
1573 | (goto-char omark) | |
1574 | nil)) | |
e23c2c21 RS |
1575 | |
1576 | (defun transient-mark-mode (arg) | |
1577 | "Toggle Transient Mark mode. | |
b411b5fa | 1578 | With arg, turn Transient Mark mode on if arg is positive, off otherwise. |
e23c2c21 | 1579 | |
5dd1220d RS |
1580 | In Transient Mark mode, when the mark is active, the region is highlighted. |
1581 | Changing the buffer \"deactivates\" the mark. | |
1582 | So do certain other operations that set the mark | |
1583 | but whose main purpose is something else--for example, | |
1584 | incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]." | |
e23c2c21 RS |
1585 | (interactive "P") |
1586 | (setq transient-mark-mode | |
1587 | (if (null arg) | |
1588 | (not transient-mark-mode) | |
1589 | (> (prefix-numeric-value arg) 0)))) | |
dc029f0b RM |
1590 | |
1591 | (defun pop-global-mark () | |
1592 | "Pop off global mark ring and jump to the top location." | |
1593 | (interactive) | |
52b6d445 RS |
1594 | ;; Pop entries which refer to non-existent buffers. |
1595 | (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) | |
1596 | (setq global-mark-ring (cdr global-mark-ring))) | |
dc029f0b RM |
1597 | (or global-mark-ring |
1598 | (error "No global mark set")) | |
1599 | (let* ((marker (car global-mark-ring)) | |
1600 | (buffer (marker-buffer marker)) | |
1601 | (position (marker-position marker))) | |
34c31301 RS |
1602 | (setq global-mark-ring (nconc (cdr global-mark-ring) |
1603 | (list (car global-mark-ring)))) | |
dc029f0b RM |
1604 | (set-buffer buffer) |
1605 | (or (and (>= position (point-min)) | |
1606 | (<= position (point-max))) | |
1607 | (widen)) | |
1608 | (goto-char position) | |
1609 | (switch-to-buffer buffer))) | |
2076c87c | 1610 | \f |
38ebcf29 | 1611 | (defvar next-line-add-newlines t |
dff7d67f | 1612 | "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error.") |
38ebcf29 | 1613 | |
2076c87c JB |
1614 | (defun next-line (arg) |
1615 | "Move cursor vertically down ARG lines. | |
1616 | If there is no character in the target line exactly under the current column, | |
1617 | the cursor is positioned after the character in that line which spans this | |
1618 | column, or at the end of the line if it is not long enough. | |
38ebcf29 | 1619 | If there is no line in the buffer after this one, behavior depends on the |
1a2c3941 RS |
1620 | value of `next-line-add-newlines'. If non-nil, it inserts a newline character |
1621 | to create a line, and moves the cursor to that line. Otherwise it moves the | |
e47d38f6 | 1622 | cursor to the end of the buffer. |
2076c87c JB |
1623 | |
1624 | The command \\[set-goal-column] can be used to create | |
1625 | a semipermanent goal column to which this command always moves. | |
1626 | Then it does not try to move vertically. This goal column is stored | |
1627 | in `goal-column', which is nil when there is none. | |
1628 | ||
1629 | If you are thinking of using this in a Lisp program, consider | |
1630 | using `forward-line' instead. It is usually easier to use | |
1631 | and more reliable (no dependence on goal column, etc.)." | |
1632 | (interactive "p") | |
028922cf RS |
1633 | (if (and next-line-add-newlines (= arg 1)) |
1634 | (let ((opoint (point))) | |
3534a809 RS |
1635 | (end-of-line) |
1636 | (if (eobp) | |
28191e20 | 1637 | (newline 1) |
028922cf RS |
1638 | (goto-char opoint) |
1639 | (line-move arg))) | |
1a2c3941 RS |
1640 | (if (interactive-p) |
1641 | (condition-case nil | |
1642 | (line-move arg) | |
1643 | ((beginning-of-buffer end-of-buffer) (ding))) | |
1644 | (line-move arg))) | |
2076c87c JB |
1645 | nil) |
1646 | ||
1647 | (defun previous-line (arg) | |
1648 | "Move cursor vertically up ARG lines. | |
1649 | If there is no character in the target line exactly over the current column, | |
1650 | the cursor is positioned after the character in that line which spans this | |
1651 | column, or at the end of the line if it is not long enough. | |
1652 | ||
1653 | The command \\[set-goal-column] can be used to create | |
1654 | a semipermanent goal column to which this command always moves. | |
1655 | Then it does not try to move vertically. | |
1656 | ||
1657 | If you are thinking of using this in a Lisp program, consider using | |
c2e8a012 | 1658 | `forward-line' with a negative argument instead. It is usually easier |
2076c87c JB |
1659 | to use and more reliable (no dependence on goal column, etc.)." |
1660 | (interactive "p") | |
1a2c3941 RS |
1661 | (if (interactive-p) |
1662 | (condition-case nil | |
1663 | (line-move (- arg)) | |
1664 | ((beginning-of-buffer end-of-buffer) (ding))) | |
1665 | (line-move (- arg))) | |
2076c87c JB |
1666 | nil) |
1667 | ||
1668 | (defconst track-eol nil | |
1669 | "*Non-nil means vertical motion starting at end of line keeps to ends of lines. | |
1670 | This means moving to the end of each line moved onto. | |
1671 | The beginning of a blank line does not count as the end of a line.") | |
1672 | ||
912c6728 RS |
1673 | (defvar goal-column nil |
1674 | "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.") | |
1675 | (make-variable-buffer-local 'goal-column) | |
2076c87c JB |
1676 | |
1677 | (defvar temporary-goal-column 0 | |
1678 | "Current goal column for vertical motion. | |
1679 | It is the column where point was | |
1680 | at the start of current run of vertical motion commands. | |
c637ae6f | 1681 | When the `track-eol' feature is doing its job, the value is 9999.") |
2076c87c | 1682 | |
098fc1fb RS |
1683 | (defvar line-move-ignore-invisible nil |
1684 | "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines. | |
1685 | Outline mode sets this.") | |
1686 | ||
8c745744 RS |
1687 | ;; This is the guts of next-line and previous-line. |
1688 | ;; Arg says how many lines to move. | |
2076c87c | 1689 | (defun line-move (arg) |
2596511d RS |
1690 | ;; Don't run any point-motion hooks, and disregard intangibility, |
1691 | ;; for intermediate positions. | |
1692 | (let ((inhibit-point-motion-hooks t) | |
1693 | (opoint (point)) | |
1694 | new) | |
1695 | (unwind-protect | |
1696 | (progn | |
1697 | (if (not (or (eq last-command 'next-line) | |
1698 | (eq last-command 'previous-line))) | |
1699 | (setq temporary-goal-column | |
1700 | (if (and track-eol (eolp) | |
1701 | ;; Don't count beg of empty line as end of line | |
1702 | ;; unless we just did explicit end-of-line. | |
1703 | (or (not (bolp)) (eq last-command 'end-of-line))) | |
1704 | 9999 | |
1705 | (current-column)))) | |
1706 | (if (and (not (integerp selective-display)) | |
1707 | (not line-move-ignore-invisible)) | |
1708 | ;; Use just newline characters. | |
1709 | (or (if (> arg 0) | |
1710 | (progn (if (> arg 1) (forward-line (1- arg))) | |
1711 | ;; This way of moving forward ARG lines | |
1712 | ;; verifies that we have a newline after the last one. | |
1713 | ;; It doesn't get confused by intangible text. | |
1714 | (end-of-line) | |
1715 | (zerop (forward-line 1))) | |
1716 | (and (zerop (forward-line arg)) | |
1717 | (bolp))) | |
1718 | (signal (if (< arg 0) | |
1719 | 'beginning-of-buffer | |
1720 | 'end-of-buffer) | |
1721 | nil)) | |
1722 | ;; Move by arg lines, but ignore invisible ones. | |
1723 | (while (> arg 0) | |
1724 | (end-of-line) | |
1725 | (and (zerop (vertical-motion 1)) | |
1726 | (signal 'end-of-buffer nil)) | |
1727 | ;; If the following character is currently invisible, | |
1728 | ;; skip all characters with that same `invisible' property value. | |
1729 | (while (and (not (eobp)) | |
1730 | (let ((prop | |
1731 | (get-char-property (point) 'invisible))) | |
1732 | (if (eq buffer-invisibility-spec t) | |
1733 | prop | |
1734 | (or (memq prop buffer-invisibility-spec) | |
1735 | (assq prop buffer-invisibility-spec))))) | |
1736 | (if (get-text-property (point) 'invisible) | |
1737 | (goto-char (next-single-property-change (point) 'invisible)) | |
1738 | (goto-char (next-overlay-change (point))))) | |
1739 | (setq arg (1- arg))) | |
1740 | (while (< arg 0) | |
1741 | (beginning-of-line) | |
1742 | (and (zerop (vertical-motion -1)) | |
1743 | (signal 'beginning-of-buffer nil)) | |
1744 | (while (and (not (bobp)) | |
1745 | (let ((prop | |
1746 | (get-char-property (1- (point)) 'invisible))) | |
1747 | (if (eq buffer-invisibility-spec t) | |
1748 | prop | |
1749 | (or (memq prop buffer-invisibility-spec) | |
1750 | (assq prop buffer-invisibility-spec))))) | |
1751 | (if (get-text-property (1- (point)) 'invisible) | |
1752 | (goto-char (previous-single-property-change (point) 'invisible)) | |
1753 | (goto-char (previous-overlay-change (point))))) | |
1754 | (setq arg (1+ arg)))) | |
1755 | (move-to-column (or goal-column temporary-goal-column))) | |
1756 | ;; Remember where we moved to, go back home, | |
1757 | ;; then do the motion over again | |
1758 | ;; in just one step, with intangibility and point-motion hooks | |
1759 | ;; enabled this time. | |
1760 | (setq new (point)) | |
1761 | (goto-char opoint) | |
1762 | (setq inhibit-point-motion-hooks nil) | |
1763 | (goto-char new))) | |
1764 | nil) | |
2076c87c | 1765 | |
d5ab2033 JB |
1766 | ;;; Many people have said they rarely use this feature, and often type |
1767 | ;;; it by accident. Maybe it shouldn't even be on a key. | |
1768 | (put 'set-goal-column 'disabled t) | |
2076c87c JB |
1769 | |
1770 | (defun set-goal-column (arg) | |
1771 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
1772 | Those commands will move to this position in the line moved to | |
1773 | rather than trying to keep the same horizontal position. | |
1774 | With a non-nil argument, clears out the goal column | |
912c6728 RS |
1775 | so that \\[next-line] and \\[previous-line] resume vertical motion. |
1776 | The goal column is stored in the variable `goal-column'." | |
2076c87c JB |
1777 | (interactive "P") |
1778 | (if arg | |
1779 | (progn | |
1780 | (setq goal-column nil) | |
1781 | (message "No goal column")) | |
1782 | (setq goal-column (current-column)) | |
1783 | (message (substitute-command-keys | |
1784 | "Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
1785 | goal-column)) | |
1786 | nil) | |
1787 | \f | |
0d5bbbf7 ER |
1788 | ;;; Partial support for horizontal autoscrolling. Someday, this feature |
1789 | ;;; will be built into the C level and all the (hscroll-point-visible) calls | |
1790 | ;;; will go away. | |
1791 | ||
1792 | (defvar hscroll-step 0 | |
1793 | "*The number of columns to try scrolling a window by when point moves out. | |
1794 | If that fails to bring point back on frame, point is centered instead. | |
1795 | If this is zero, point is always centered after it moves off frame.") | |
1796 | ||
1797 | (defun hscroll-point-visible () | |
26c5bf8e KH |
1798 | "Scrolls the selected window horizontally to make point visible." |
1799 | (save-excursion | |
1800 | (set-buffer (window-buffer)) | |
1801 | (if (not (or truncate-lines | |
1802 | (> (window-hscroll) 0) | |
1803 | (and truncate-partial-width-windows | |
1804 | (< (window-width) (frame-width))))) | |
1805 | ;; Point is always visible when lines are wrapped. | |
1806 | () | |
1807 | ;; If point is on the invisible part of the line before window-start, | |
1808 | ;; then hscrolling can't bring it back, so reset window-start first. | |
1809 | (and (< (point) (window-start)) | |
1810 | (let ((ws-bol (save-excursion | |
1811 | (goto-char (window-start)) | |
1812 | (beginning-of-line) | |
1813 | (point)))) | |
1814 | (and (>= (point) ws-bol) | |
1815 | (set-window-start nil ws-bol)))) | |
1816 | (let* ((here (hscroll-window-column)) | |
1817 | (left (min (window-hscroll) 1)) | |
1818 | (right (1- (window-width)))) | |
1819 | ;; Allow for the truncation glyph, if we're not exactly at eol. | |
1820 | (if (not (and (= here right) | |
1821 | (= (following-char) ?\n))) | |
1822 | (setq right (1- right))) | |
1823 | (cond | |
1824 | ;; If too far away, just recenter. But don't show too much | |
1825 | ;; white space off the end of the line. | |
1826 | ((or (< here (- left hscroll-step)) | |
1827 | (> here (+ right hscroll-step))) | |
1828 | (let ((eol (save-excursion (end-of-line) (hscroll-window-column)))) | |
1829 | (scroll-left (min (- here (/ (window-width) 2)) | |
1830 | (- eol (window-width) -5))))) | |
1831 | ;; Within range. Scroll by one step (or maybe not at all). | |
1832 | ((< here left) | |
1833 | (scroll-right hscroll-step)) | |
1834 | ((> here right) | |
1835 | (scroll-left hscroll-step))))))) | |
1836 | ||
1837 | ;; This function returns the window's idea of the display column of point, | |
1838 | ;; assuming that the window is already known to be truncated rather than | |
1839 | ;; wrapped, and that we've already handled the case where point is on the | |
1840 | ;; part of the line before window-start. We ignore window-width; if point | |
1841 | ;; is beyond the right margin, we want to know how far. The return value | |
1842 | ;; includes the effects of window-hscroll, window-start, and the prompt | |
1843 | ;; string in the minibuffer. It may be negative due to hscroll. | |
1844 | (defun hscroll-window-column () | |
1845 | (let* ((hscroll (window-hscroll)) | |
1846 | (startpos (save-excursion | |
1847 | (beginning-of-line) | |
1848 | (if (= (point) (save-excursion | |
1849 | (goto-char (window-start)) | |
1850 | (beginning-of-line) | |
1851 | (point))) | |
1852 | (goto-char (window-start))) | |
1853 | (point))) | |
1854 | (hpos (+ (if (and (eq (selected-window) (minibuffer-window)) | |
1855 | (= 1 (window-start)) | |
1856 | (= startpos (point-min))) | |
1857 | (minibuffer-prompt-width) | |
1858 | 0) | |
1859 | (min 0 (- 1 hscroll)))) | |
1860 | val) | |
1861 | (car (cdr (compute-motion startpos (cons hpos 0) | |
1862 | (point) (cons 0 1) | |
1863 | 1000000 (cons hscroll 0) nil))))) | |
1864 | ||
0d5bbbf7 | 1865 | |
dff7d67f RS |
1866 | ;; rms: (1) The definitions of arrow keys should not simply restate |
1867 | ;; what keys they are. The arrow keys should run the ordinary commands. | |
1868 | ;; (2) The arrow keys are just one of many common ways of moving point | |
1869 | ;; within a line. Real horizontal autoscrolling would be a good feature, | |
1870 | ;; but supporting it only for arrow keys is too incomplete to be desirable. | |
1871 | ||
1872 | ;;;;; Make arrow keys do the right thing for improved terminal support | |
1873 | ;;;;; When we implement true horizontal autoscrolling, right-arrow and | |
1874 | ;;;;; left-arrow can lose the (if truncate-lines ...) clause and become | |
1875 | ;;;;; aliases. These functions are bound to the corresponding keyboard | |
1876 | ;;;;; events in loaddefs.el. | |
1877 | ||
1878 | ;;(defun right-arrow (arg) | |
1879 | ;; "Move right one character on the screen (with prefix ARG, that many chars). | |
1880 | ;;Scroll right if needed to keep point horizontally onscreen." | |
1881 | ;; (interactive "P") | |
1882 | ;; (forward-char arg) | |
1883 | ;; (hscroll-point-visible)) | |
1884 | ||
1885 | ;;(defun left-arrow (arg) | |
1886 | ;; "Move left one character on the screen (with prefix ARG, that many chars). | |
1887 | ;;Scroll left if needed to keep point horizontally onscreen." | |
1888 | ;; (interactive "P") | |
1889 | ;; (backward-char arg) | |
1890 | ;; (hscroll-point-visible)) | |
7492f5a6 RS |
1891 | |
1892 | (defun scroll-other-window-down (lines) | |
e47d38f6 RS |
1893 | "Scroll the \"other window\" down. |
1894 | For more details, see the documentation for `scroll-other-window'." | |
7492f5a6 RS |
1895 | (interactive "P") |
1896 | (scroll-other-window | |
1897 | ;; Just invert the argument's meaning. | |
1898 | ;; We can do that without knowing which window it will be. | |
1899 | (if (eq lines '-) nil | |
1900 | (if (null lines) '- | |
1901 | (- (prefix-numeric-value lines)))))) | |
e47d38f6 | 1902 | (define-key esc-map [?\C-\S-v] 'scroll-other-window-down) |
3aef9604 RS |
1903 | |
1904 | (defun beginning-of-buffer-other-window (arg) | |
1905 | "Move point to the beginning of the buffer in the other window. | |
1906 | Leave mark at previous position. | |
1907 | With arg N, put point N/10 of the way from the true beginning." | |
1908 | (interactive "P") | |
1909 | (let ((orig-window (selected-window)) | |
1910 | (window (other-window-for-scrolling))) | |
1911 | ;; We use unwind-protect rather than save-window-excursion | |
1912 | ;; because the latter would preserve the things we want to change. | |
1913 | (unwind-protect | |
1914 | (progn | |
1915 | (select-window window) | |
1916 | ;; Set point and mark in that window's buffer. | |
1917 | (beginning-of-buffer arg) | |
1918 | ;; Set point accordingly. | |
1919 | (recenter '(t))) | |
1920 | (select-window orig-window)))) | |
1921 | ||
1922 | (defun end-of-buffer-other-window (arg) | |
1923 | "Move point to the end of the buffer in the other window. | |
1924 | Leave mark at previous position. | |
1925 | With arg N, put point N/10 of the way from the true end." | |
1926 | (interactive "P") | |
1927 | ;; See beginning-of-buffer-other-window for comments. | |
1928 | (let ((orig-window (selected-window)) | |
1929 | (window (other-window-for-scrolling))) | |
1930 | (unwind-protect | |
1931 | (progn | |
1932 | (select-window window) | |
4500ff36 | 1933 | (end-of-buffer arg) |
3aef9604 RS |
1934 | (recenter '(t))) |
1935 | (select-window orig-window)))) | |
38ebcf29 | 1936 | \f |
2076c87c JB |
1937 | (defun transpose-chars (arg) |
1938 | "Interchange characters around point, moving forward one character. | |
1939 | With prefix arg ARG, effect is to take character before point | |
1940 | and drag it forward past ARG other characters (backward if ARG negative). | |
1941 | If no argument and at end of line, the previous two chars are exchanged." | |
1942 | (interactive "*P") | |
1943 | (and (null arg) (eolp) (forward-char -1)) | |
1944 | (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
1945 | ||
1946 | (defun transpose-words (arg) | |
1947 | "Interchange words around point, leaving point at end of them. | |
1948 | With prefix arg ARG, effect is to take word before or around point | |
1949 | and drag it forward past ARG other words (backward if ARG negative). | |
1950 | If ARG is zero, the words around or after point and around or after mark | |
1951 | are interchanged." | |
1952 | (interactive "*p") | |
1953 | (transpose-subr 'forward-word arg)) | |
1954 | ||
1955 | (defun transpose-sexps (arg) | |
1956 | "Like \\[transpose-words] but applies to sexps. | |
1957 | Does not work on a sexp that point is in the middle of | |
1958 | if it is a list or string." | |
1959 | (interactive "*p") | |
1960 | (transpose-subr 'forward-sexp arg)) | |
1961 | ||
1962 | (defun transpose-lines (arg) | |
1963 | "Exchange current line and previous line, leaving point after both. | |
1964 | With argument ARG, takes previous line and moves it past ARG lines. | |
1965 | With argument 0, interchanges line point is in with line mark is in." | |
1966 | (interactive "*p") | |
1967 | (transpose-subr (function | |
1968 | (lambda (arg) | |
1969 | (if (= arg 1) | |
1970 | (progn | |
1971 | ;; Move forward over a line, | |
1972 | ;; but create a newline if none exists yet. | |
1973 | (end-of-line) | |
1974 | (if (eobp) | |
1975 | (newline) | |
1976 | (forward-char 1))) | |
1977 | (forward-line arg)))) | |
1978 | arg)) | |
1979 | ||
1980 | (defun transpose-subr (mover arg) | |
1981 | (let (start1 end1 start2 end2) | |
1982 | (if (= arg 0) | |
1983 | (progn | |
1984 | (save-excursion | |
1985 | (funcall mover 1) | |
1986 | (setq end2 (point)) | |
1987 | (funcall mover -1) | |
1988 | (setq start2 (point)) | |
1989 | (goto-char (mark)) | |
1990 | (funcall mover 1) | |
1991 | (setq end1 (point)) | |
1992 | (funcall mover -1) | |
1993 | (setq start1 (point)) | |
1994 | (transpose-subr-1)) | |
1995 | (exchange-point-and-mark))) | |
1996 | (while (> arg 0) | |
1997 | (funcall mover -1) | |
1998 | (setq start1 (point)) | |
1999 | (funcall mover 1) | |
2000 | (setq end1 (point)) | |
2001 | (funcall mover 1) | |
2002 | (setq end2 (point)) | |
2003 | (funcall mover -1) | |
2004 | (setq start2 (point)) | |
2005 | (transpose-subr-1) | |
2006 | (goto-char end2) | |
2007 | (setq arg (1- arg))) | |
2008 | (while (< arg 0) | |
2009 | (funcall mover -1) | |
2010 | (setq start2 (point)) | |
2011 | (funcall mover -1) | |
2012 | (setq start1 (point)) | |
2013 | (funcall mover 1) | |
2014 | (setq end1 (point)) | |
2015 | (funcall mover 1) | |
2016 | (setq end2 (point)) | |
2017 | (transpose-subr-1) | |
2018 | (setq arg (1+ arg))))) | |
2019 | ||
2020 | (defun transpose-subr-1 () | |
2021 | (if (> (min end1 end2) (max start1 start2)) | |
2022 | (error "Don't have two things to transpose")) | |
2023 | (let ((word1 (buffer-substring start1 end1)) | |
2024 | (word2 (buffer-substring start2 end2))) | |
2025 | (delete-region start2 end2) | |
2026 | (goto-char start2) | |
2027 | (insert word1) | |
2028 | (goto-char (if (< start1 start2) start1 | |
2029 | (+ start1 (- (length word1) (length word2))))) | |
2030 | (delete-char (length word1)) | |
2031 | (insert word2))) | |
2032 | \f | |
2033 | (defconst comment-column 32 | |
2034 | "*Column to indent right-margin comments to. | |
8a8fa723 JB |
2035 | Setting this variable automatically makes it local to the current buffer. |
2036 | Each mode establishes a different default value for this variable; you | |
b492f73b | 2037 | can set the value for a particular mode using that mode's hook.") |
2076c87c JB |
2038 | (make-variable-buffer-local 'comment-column) |
2039 | ||
2040 | (defconst comment-start nil | |
534a0de5 | 2041 | "*String to insert to start a new comment, or nil if no comment syntax.") |
2076c87c JB |
2042 | |
2043 | (defconst comment-start-skip nil | |
2044 | "*Regexp to match the start of a comment plus everything up to its body. | |
2045 | If there are any \\(...\\) pairs, the comment delimiter text is held to begin | |
2046 | at the place matched by the close of the first pair.") | |
2047 | ||
2048 | (defconst comment-end "" | |
2049 | "*String to insert to end a new comment. | |
2050 | Should be an empty string if comments are terminated by end-of-line.") | |
2051 | ||
ec9a76e3 JB |
2052 | (defconst comment-indent-hook nil |
2053 | "Obsolete variable for function to compute desired indentation for a comment. | |
2054 | This function is called with no args with point at the beginning of | |
2055 | the comment's starting delimiter.") | |
2056 | ||
2057 | (defconst comment-indent-function | |
2076c87c JB |
2058 | '(lambda () comment-column) |
2059 | "Function to compute desired indentation for a comment. | |
2060 | This function is called with no args with point at the beginning of | |
2061 | the comment's starting delimiter.") | |
2062 | ||
534a0de5 RS |
2063 | (defconst block-comment-start nil |
2064 | "*String to insert to start a new comment on a line by itself. | |
2065 | If nil, use `comment-start' instead. | |
2066 | Note that the regular expression `comment-start-skip' should skip this string | |
2067 | as well as the `comment-start' string.") | |
2068 | ||
2069 | (defconst block-comment-end nil | |
2070 | "*String to insert to end a new comment on a line by itself. | |
2071 | Should be an empty string if comments are terminated by end-of-line. | |
2072 | If nil, use `comment-end' instead.") | |
2073 | ||
2076c87c JB |
2074 | (defun indent-for-comment () |
2075 | "Indent this line's comment to comment column, or insert an empty comment." | |
2076 | (interactive "*") | |
534a0de5 RS |
2077 | (let* ((empty (save-excursion (beginning-of-line) |
2078 | (looking-at "[ \t]*$"))) | |
2079 | (starter (or (and empty block-comment-start) comment-start)) | |
2080 | (ender (or (and empty block-comment-end) comment-end))) | |
2081 | (if (null starter) | |
2082 | (error "No comment syntax defined") | |
2083 | (let* ((eolpos (save-excursion (end-of-line) (point))) | |
2084 | cpos indent begpos) | |
6389928d | 2085 | (beginning-of-line) |
534a0de5 RS |
2086 | (if (re-search-forward comment-start-skip eolpos 'move) |
2087 | (progn (setq cpos (point-marker)) | |
2088 | ;; Find the start of the comment delimiter. | |
2089 | ;; If there were paren-pairs in comment-start-skip, | |
2090 | ;; position at the end of the first pair. | |
2091 | (if (match-end 1) | |
2092 | (goto-char (match-end 1)) | |
2093 | ;; If comment-start-skip matched a string with | |
2094 | ;; internal whitespace (not final whitespace) then | |
2095 | ;; the delimiter start at the end of that | |
2096 | ;; whitespace. Otherwise, it starts at the | |
2097 | ;; beginning of what was matched. | |
2098 | (skip-syntax-backward " " (match-beginning 0)) | |
2099 | (skip-syntax-backward "^ " (match-beginning 0))))) | |
2100 | (setq begpos (point)) | |
2101 | ;; Compute desired indent. | |
2102 | (if (= (current-column) | |
2103 | (setq indent (if comment-indent-hook | |
2104 | (funcall comment-indent-hook) | |
2105 | (funcall comment-indent-function)))) | |
2106 | (goto-char begpos) | |
2107 | ;; If that's different from current, change it. | |
2108 | (skip-chars-backward " \t") | |
2109 | (delete-region (point) begpos) | |
2110 | (indent-to indent)) | |
2111 | ;; An existing comment? | |
2112 | (if cpos | |
2113 | (progn (goto-char cpos) | |
2114 | (set-marker cpos nil)) | |
2115 | ;; No, insert one. | |
2116 | (insert starter) | |
2117 | (save-excursion | |
2118 | (insert ender))))))) | |
2076c87c JB |
2119 | |
2120 | (defun set-comment-column (arg) | |
2121 | "Set the comment column based on point. | |
2122 | With no arg, set the comment column to the current column. | |
2123 | With just minus as arg, kill any comment on this line. | |
2124 | With any other arg, set comment column to indentation of the previous comment | |
2125 | and then align or create a comment on this line at that column." | |
2126 | (interactive "P") | |
2127 | (if (eq arg '-) | |
2128 | (kill-comment nil) | |
2129 | (if arg | |
2130 | (progn | |
2131 | (save-excursion | |
2132 | (beginning-of-line) | |
2133 | (re-search-backward comment-start-skip) | |
2134 | (beginning-of-line) | |
2135 | (re-search-forward comment-start-skip) | |
2136 | (goto-char (match-beginning 0)) | |
2137 | (setq comment-column (current-column)) | |
2138 | (message "Comment column set to %d" comment-column)) | |
2139 | (indent-for-comment)) | |
2140 | (setq comment-column (current-column)) | |
2141 | (message "Comment column set to %d" comment-column)))) | |
2142 | ||
2143 | (defun kill-comment (arg) | |
2144 | "Kill the comment on this line, if any. | |
2145 | With argument, kill comments on that many lines starting with this one." | |
2146 | ;; this function loses in a lot of situations. it incorrectly recognises | |
2147 | ;; comment delimiters sometimes (ergo, inside a string), doesn't work | |
2148 | ;; with multi-line comments, can kill extra whitespace if comment wasn't | |
2149 | ;; through end-of-line, et cetera. | |
2150 | (interactive "P") | |
2151 | (or comment-start-skip (error "No comment syntax defined")) | |
2152 | (let ((count (prefix-numeric-value arg)) endc) | |
2153 | (while (> count 0) | |
2154 | (save-excursion | |
2155 | (end-of-line) | |
2156 | (setq endc (point)) | |
2157 | (beginning-of-line) | |
2158 | (and (string< "" comment-end) | |
2159 | (setq endc | |
2160 | (progn | |
2161 | (re-search-forward (regexp-quote comment-end) endc 'move) | |
2162 | (skip-chars-forward " \t") | |
2163 | (point)))) | |
2164 | (beginning-of-line) | |
2165 | (if (re-search-forward comment-start-skip endc t) | |
2166 | (progn | |
2167 | (goto-char (match-beginning 0)) | |
2168 | (skip-chars-backward " \t") | |
2169 | (kill-region (point) endc) | |
2170 | ;; to catch comments a line beginnings | |
2171 | (indent-according-to-mode)))) | |
2172 | (if arg (forward-line 1)) | |
2173 | (setq count (1- count))))) | |
2174 | ||
2175 | (defun comment-region (beg end &optional arg) | |
f28039bb RS |
2176 | "Comment or uncomment each line in the region. |
2177 | With just C-u prefix arg, uncomment each line in region. | |
2178 | Numeric prefix arg ARG means use ARG comment characters. | |
2076c87c JB |
2179 | If ARG is negative, delete that many comment characters instead. |
2180 | Comments are terminated on each line, even for syntax in which newline does | |
2181 | not end the comment. Blank lines do not get comments." | |
2182 | ;; if someone wants it to only put a comment-start at the beginning and | |
2183 | ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x | |
2184 | ;; is easy enough. No option is made here for other than commenting | |
2185 | ;; every line. | |
f28039bb | 2186 | (interactive "r\nP") |
2076c87c JB |
2187 | (or comment-start (error "No comment syntax is defined")) |
2188 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | |
2189 | (save-excursion | |
2190 | (save-restriction | |
f28039bb RS |
2191 | (let ((cs comment-start) (ce comment-end) |
2192 | numarg) | |
2193 | (if (consp arg) (setq numarg t) | |
2194 | (setq numarg (prefix-numeric-value arg)) | |
2195 | ;; For positive arg > 1, replicate the comment delims now, | |
2196 | ;; then insert the replicated strings just once. | |
2197 | (while (> numarg 1) | |
2198 | (setq cs (concat cs comment-start) | |
2199 | ce (concat ce comment-end)) | |
2200 | (setq numarg (1- numarg)))) | |
2201 | ;; Loop over all lines from BEG to END. | |
2076c87c JB |
2202 | (narrow-to-region beg end) |
2203 | (goto-char beg) | |
2204 | (while (not (eobp)) | |
f28039bb RS |
2205 | (if (or (eq numarg t) (< numarg 0)) |
2206 | (progn | |
2207 | ;; Delete comment start from beginning of line. | |
2208 | (if (eq numarg t) | |
2209 | (while (looking-at (regexp-quote cs)) | |
2210 | (delete-char (length cs))) | |
2211 | (let ((count numarg)) | |
2212 | (while (and (> 1 (setq count (1+ count))) | |
2213 | (looking-at (regexp-quote cs))) | |
2214 | (delete-char (length cs))))) | |
2215 | ;; Delete comment end from end of line. | |
2216 | (if (string= "" ce) | |
2217 | nil | |
2218 | (if (eq numarg t) | |
2219 | (progn | |
2220 | (end-of-line) | |
2221 | ;; This is questionable if comment-end ends in | |
2222 | ;; whitespace. That is pretty brain-damaged, | |
2223 | ;; though. | |
2224 | (skip-chars-backward " \t") | |
2225 | (if (and (>= (- (point) (point-min)) (length ce)) | |
2226 | (save-excursion | |
2227 | (backward-char (length ce)) | |
2228 | (looking-at (regexp-quote ce)))) | |
2229 | (delete-char (- (length ce))))) | |
ee095968 RS |
2230 | (let ((count numarg)) |
2231 | (while (> 1 (setq count (1+ count))) | |
2232 | (end-of-line) | |
2233 | ;; this is questionable if comment-end ends in whitespace | |
2234 | ;; that is pretty brain-damaged though | |
2235 | (skip-chars-backward " \t") | |
2236 | (save-excursion | |
2237 | (backward-char (length ce)) | |
2238 | (if (looking-at (regexp-quote ce)) | |
2239 | (delete-char (length ce)))))))) | |
6e88ed49 | 2240 | (forward-line 1)) |
f28039bb | 2241 | ;; Insert at beginning and at end. |
2076c87c JB |
2242 | (if (looking-at "[ \t]*$") () |
2243 | (insert cs) | |
2244 | (if (string= "" ce) () | |
2245 | (end-of-line) | |
2246 | (insert ce))) | |
2247 | (search-forward "\n" nil 'move))))))) | |
2248 | \f | |
2249 | (defun backward-word (arg) | |
2250 | "Move backward until encountering the end of a word. | |
2251 | With argument, do this that many times. | |
ff1fbe3e | 2252 | In programs, it is faster to call `forward-word' with negative arg." |
9e50756b | 2253 | (interactive "p") |
2076c87c JB |
2254 | (forward-word (- arg))) |
2255 | ||
2256 | (defun mark-word (arg) | |
2257 | "Set mark arg words away from point." | |
2258 | (interactive "p") | |
2259 | (push-mark | |
2260 | (save-excursion | |
2261 | (forward-word arg) | |
fd0f4056 RS |
2262 | (point)) |
2263 | nil t)) | |
2076c87c JB |
2264 | |
2265 | (defun kill-word (arg) | |
2266 | "Kill characters forward until encountering the end of a word. | |
2267 | With argument, do this that many times." | |
2268 | (interactive "p") | |
e6291fe1 | 2269 | (kill-region (point) (progn (forward-word arg) (point)))) |
2076c87c JB |
2270 | |
2271 | (defun backward-kill-word (arg) | |
2272 | "Kill characters backward until encountering the end of a word. | |
2273 | With argument, do this that many times." | |
2274 | (interactive "p") | |
2275 | (kill-word (- arg))) | |
d7c64071 | 2276 | |
1e8c5ac4 RS |
2277 | (defun current-word (&optional strict) |
2278 | "Return the word point is on (or a nearby word) as a string. | |
2279 | If optional arg STRICT is non-nil, return nil unless point is within | |
2280 | or adjacent to a word." | |
d7c64071 ER |
2281 | (save-excursion |
2282 | (let ((oldpoint (point)) (start (point)) (end (point))) | |
2283 | (skip-syntax-backward "w_") (setq start (point)) | |
2284 | (goto-char oldpoint) | |
2285 | (skip-syntax-forward "w_") (setq end (point)) | |
2286 | (if (and (eq start oldpoint) (eq end oldpoint)) | |
1e8c5ac4 RS |
2287 | ;; Point is neither within nor adjacent to a word. |
2288 | (and (not strict) | |
2289 | (progn | |
2290 | ;; Look for preceding word in same line. | |
2291 | (skip-syntax-backward "^w_" | |
2292 | (save-excursion (beginning-of-line) | |
2293 | (point))) | |
2294 | (if (bolp) | |
2295 | ;; No preceding word in same line. | |
2296 | ;; Look for following word in same line. | |
2297 | (progn | |
2298 | (skip-syntax-forward "^w_" | |
2299 | (save-excursion (end-of-line) | |
2300 | (point))) | |
2301 | (setq start (point)) | |
2302 | (skip-syntax-forward "w_") | |
2303 | (setq end (point))) | |
2304 | (setq end (point)) | |
2305 | (skip-syntax-backward "w_") | |
2306 | (setq start (point))) | |
2307 | (buffer-substring start end))) | |
2308 | (buffer-substring start end))))) | |
2076c87c JB |
2309 | \f |
2310 | (defconst fill-prefix nil | |
2311 | "*String for filling to insert at front of new line, or nil for none. | |
2312 | Setting this variable automatically makes it local to the current buffer.") | |
2313 | (make-variable-buffer-local 'fill-prefix) | |
2314 | ||
2315 | (defconst auto-fill-inhibit-regexp nil | |
2316 | "*Regexp to match lines which should not be auto-filled.") | |
2317 | ||
2318 | (defun do-auto-fill () | |
a0170800 RS |
2319 | (let (fc justify bol give-up |
2320 | (fill-prefix fill-prefix)) | |
c18465c4 | 2321 | (if (or (not (setq justify (current-justification))) |
8f066a20 RS |
2322 | (null (setq fc (current-fill-column))) |
2323 | (and (eq justify 'left) | |
2324 | (<= (current-column) fc)) | |
eed5698b RS |
2325 | (save-excursion (beginning-of-line) |
2326 | (setq bol (point)) | |
2327 | (and auto-fill-inhibit-regexp | |
2328 | (looking-at auto-fill-inhibit-regexp)))) | |
2329 | nil ;; Auto-filling not required | |
3db1e3b5 BG |
2330 | (if (memq justify '(full center right)) |
2331 | (save-excursion (unjustify-current-line))) | |
a0170800 RS |
2332 | |
2333 | ;; Choose a fill-prefix automatically. | |
2334 | (if (and adaptive-fill-mode | |
2335 | (or (null fill-prefix) (string= fill-prefix ""))) | |
e4b62d7c RS |
2336 | (let ((prefix |
2337 | (fill-context-prefix | |
2338 | (save-excursion (backward-paragraph 1) (point)) | |
2339 | (save-excursion (forward-paragraph 1) (point)) | |
2340 | ;; Don't accept a non-whitespace fill prefix | |
2341 | ;; from the first line of a paragraph. | |
2342 | "^[ \t]*$"))) | |
2343 | (and prefix (not (equal prefix "")) | |
2344 | (setq fill-prefix prefix)))) | |
a0170800 | 2345 | |
eed5698b | 2346 | (while (and (not give-up) (> (current-column) fc)) |
e47d38f6 RS |
2347 | ;; Determine where to split the line. |
2348 | (let ((fill-point | |
2349 | (let ((opoint (point)) | |
2350 | bounce | |
2351 | (first t)) | |
2352 | (save-excursion | |
2353 | (move-to-column (1+ fc)) | |
2354 | ;; Move back to a word boundary. | |
2355 | (while (or first | |
2356 | ;; If this is after period and a single space, | |
2357 | ;; move back once more--we don't want to break | |
2358 | ;; the line there and make it look like a | |
2359 | ;; sentence end. | |
2360 | (and (not (bobp)) | |
2361 | (not bounce) | |
2362 | sentence-end-double-space | |
2363 | (save-excursion (forward-char -1) | |
2364 | (and (looking-at "\\. ") | |
2365 | (not (looking-at "\\. ")))))) | |
2366 | (setq first nil) | |
2367 | (skip-chars-backward "^ \t\n") | |
2368 | ;; If we find nowhere on the line to break it, | |
2369 | ;; break after one word. Set bounce to t | |
2370 | ;; so we will not keep going in this while loop. | |
2371 | (if (bolp) | |
2372 | (progn | |
2373 | (re-search-forward "[ \t]" opoint t) | |
2374 | (setq bounce t))) | |
2375 | (skip-chars-backward " \t")) | |
2376 | ;; Let fill-point be set to the place where we end up. | |
2377 | (point))))) | |
2378 | ;; If that place is not the beginning of the line, | |
2379 | ;; break the line there. | |
2380 | (if (save-excursion | |
2381 | (goto-char fill-point) | |
2382 | (not (bolp))) | |
2383 | (let ((prev-column (current-column))) | |
2384 | ;; If point is at the fill-point, do not `save-excursion'. | |
2385 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
2386 | ;; point will end up before it rather than after it. | |
2387 | (if (save-excursion | |
2388 | (skip-chars-backward " \t") | |
2389 | (= (point) fill-point)) | |
2390 | (indent-new-comment-line t) | |
2391 | (save-excursion | |
2392 | (goto-char fill-point) | |
2393 | (indent-new-comment-line t))) | |
2394 | ;; Now do justification, if required | |
2395 | (if (not (eq justify 'left)) | |
2396 | (save-excursion | |
2397 | (end-of-line 0) | |
2398 | (justify-current-line justify nil t))) | |
2399 | ;; If making the new line didn't reduce the hpos of | |
2400 | ;; the end of the line, then give up now; | |
2401 | ;; trying again will not help. | |
2402 | (if (>= (current-column) prev-column) | |
2403 | (setq give-up t))) | |
2404 | ;; No place to break => stop trying. | |
2405 | (setq give-up t)))) | |
eed5698b RS |
2406 | ;; justify last line |
2407 | (justify-current-line justify t t)))) | |
2076c87c | 2408 | |
d7465b15 RS |
2409 | (defun auto-fill-mode (&optional arg) |
2410 | "Toggle auto-fill mode. | |
2411 | With arg, turn Auto-Fill mode on if and only if arg is positive. | |
eed5698b | 2412 | In Auto-Fill mode, inserting a space at a column beyond `current-fill-column' |
d7465b15 RS |
2413 | automatically breaks the line at a previous space." |
2414 | (interactive "P") | |
2415 | (prog1 (setq auto-fill-function | |
2416 | (if (if (null arg) | |
2417 | (not auto-fill-function) | |
2418 | (> (prefix-numeric-value arg) 0)) | |
2419 | 'do-auto-fill | |
2420 | nil)) | |
7911ecc8 | 2421 | (force-mode-line-update))) |
d7465b15 RS |
2422 | |
2423 | ;; This holds a document string used to document auto-fill-mode. | |
2424 | (defun auto-fill-function () | |
2425 | "Automatically break line at a previous space, in insertion of text." | |
2426 | nil) | |
2427 | ||
2428 | (defun turn-on-auto-fill () | |
2429 | "Unconditionally turn on Auto Fill mode." | |
2430 | (auto-fill-mode 1)) | |
2431 | ||
2432 | (defun set-fill-column (arg) | |
2433 | "Set `fill-column' to current column, or to argument if given. | |
2434 | The variable `fill-column' has a separate value for each buffer." | |
2435 | (interactive "P") | |
2436 | (setq fill-column (if (integerp arg) arg (current-column))) | |
2437 | (message "fill-column set to %d" fill-column)) | |
2438 | \f | |
2076c87c JB |
2439 | (defconst comment-multi-line nil |
2440 | "*Non-nil means \\[indent-new-comment-line] should continue same comment | |
c88ab9ce ER |
2441 | on new line, with no new terminator or starter. |
2442 | This is obsolete because you might as well use \\[newline-and-indent].") | |
2076c87c | 2443 | |
28191e20 | 2444 | (defun indent-new-comment-line (&optional soft) |
d7465b15 RS |
2445 | "Break line at point and indent, continuing comment if within one. |
2446 | This indents the body of the continued comment | |
2447 | under the previous comment line. | |
c88ab9ce ER |
2448 | |
2449 | This command is intended for styles where you write a comment per line, | |
2450 | starting a new comment (and terminating it if necessary) on each line. | |
28191e20 RS |
2451 | If you want to continue one comment across several lines, use \\[newline-and-indent]. |
2452 | ||
3a0c4755 RS |
2453 | If a fill column is specified, it overrides the use of the comment column |
2454 | or comment indentation. | |
2455 | ||
28191e20 RS |
2456 | The inserted newline is marked hard if `use-hard-newlines' is true, |
2457 | unless optional argument SOFT is non-nil." | |
2458 | (interactive) | |
2076c87c JB |
2459 | (let (comcol comstart) |
2460 | (skip-chars-backward " \t") | |
2461 | (delete-region (point) | |
2462 | (progn (skip-chars-forward " \t") | |
2463 | (point))) | |
eed5698b | 2464 | (if soft (insert-and-inherit ?\n) (newline 1)) |
3a0c4755 RS |
2465 | (if fill-prefix |
2466 | (progn | |
2467 | (indent-to-left-margin) | |
2468 | (insert-and-inherit fill-prefix)) | |
2469 | (if (not comment-multi-line) | |
2076c87c | 2470 | (save-excursion |
3a0c4755 RS |
2471 | (if (and comment-start-skip |
2472 | (let ((opoint (point))) | |
2473 | (forward-line -1) | |
2474 | (re-search-forward comment-start-skip opoint t))) | |
2475 | ;; The old line is a comment. | |
2476 | ;; Set WIN to the pos of the comment-start. | |
2477 | ;; But if the comment is empty, look at preceding lines | |
2478 | ;; to find one that has a nonempty comment. | |
2479 | ||
2480 | ;; If comment-start-skip contains a \(...\) pair, | |
2481 | ;; the real comment delimiter starts at the end of that pair. | |
2482 | (let ((win (or (match-end 1) (match-beginning 0)))) | |
2483 | (while (and (eolp) (not (bobp)) | |
2484 | (let (opoint) | |
2485 | (beginning-of-line) | |
2486 | (setq opoint (point)) | |
2487 | (forward-line -1) | |
2488 | (re-search-forward comment-start-skip opoint t))) | |
2489 | (setq win (or (match-end 1) (match-beginning 0)))) | |
2490 | ;; Indent this line like what we found. | |
2491 | (goto-char win) | |
2492 | (setq comcol (current-column)) | |
2493 | (setq comstart | |
2494 | (buffer-substring (point) (match-end 0))))))) | |
2495 | (if comcol | |
2496 | (let ((comment-column comcol) | |
2497 | (comment-start comstart) | |
2498 | (comment-end comment-end)) | |
2499 | (and comment-end (not (equal comment-end "")) | |
2500 | ; (if (not comment-multi-line) | |
2501 | (progn | |
2502 | (forward-char -1) | |
2503 | (insert comment-end) | |
2504 | (forward-char 1)) | |
2505 | ; (setq comment-column (+ comment-column (length comment-start)) | |
2506 | ; comment-start "") | |
2507 | ; ) | |
2508 | ) | |
2509 | (if (not (eolp)) | |
2510 | (setq comment-end "")) | |
2511 | (insert-and-inherit ?\n) | |
2512 | (forward-char -1) | |
2513 | (indent-for-comment) | |
2514 | (save-excursion | |
2515 | ;; Make sure we delete the newline inserted above. | |
2516 | (end-of-line) | |
2517 | (delete-char 1))) | |
2518 | (indent-according-to-mode))))) | |
2076c87c JB |
2519 | \f |
2520 | (defun set-selective-display (arg) | |
ff1fbe3e RS |
2521 | "Set `selective-display' to ARG; clear it if no arg. |
2522 | When the value of `selective-display' is a number > 0, | |
2523 | lines whose indentation is >= that value are not displayed. | |
2524 | The variable `selective-display' has a separate value for each buffer." | |
2076c87c JB |
2525 | (interactive "P") |
2526 | (if (eq selective-display t) | |
2527 | (error "selective-display already in use for marked lines")) | |
c88ab9ce ER |
2528 | (let ((current-vpos |
2529 | (save-restriction | |
2530 | (narrow-to-region (point-min) (point)) | |
2531 | (goto-char (window-start)) | |
2532 | (vertical-motion (window-height))))) | |
2533 | (setq selective-display | |
2534 | (and arg (prefix-numeric-value arg))) | |
2535 | (recenter current-vpos)) | |
2076c87c JB |
2536 | (set-window-start (selected-window) (window-start (selected-window))) |
2537 | (princ "selective-display set to " t) | |
2538 | (prin1 selective-display t) | |
2539 | (princ "." t)) | |
2540 | ||
b6a22db0 JB |
2541 | (defconst overwrite-mode-textual " Ovwrt" |
2542 | "The string displayed in the mode line when in overwrite mode.") | |
2543 | (defconst overwrite-mode-binary " Bin Ovwrt" | |
2544 | "The string displayed in the mode line when in binary overwrite mode.") | |
2545 | ||
2076c87c JB |
2546 | (defun overwrite-mode (arg) |
2547 | "Toggle overwrite mode. | |
2548 | With arg, turn overwrite mode on iff arg is positive. | |
2549 | In overwrite mode, printing characters typed in replace existing text | |
b6a22db0 JB |
2550 | on a one-for-one basis, rather than pushing it to the right. At the |
2551 | end of a line, such characters extend the line. Before a tab, | |
2552 | such characters insert until the tab is filled in. | |
2553 | \\[quoted-insert] still inserts characters in overwrite mode; this | |
2554 | is supposed to make it easier to insert characters when necessary." | |
2555 | (interactive "P") | |
2556 | (setq overwrite-mode | |
2557 | (if (if (null arg) (not overwrite-mode) | |
2558 | (> (prefix-numeric-value arg) 0)) | |
2559 | 'overwrite-mode-textual)) | |
2560 | (force-mode-line-update)) | |
2561 | ||
2562 | (defun binary-overwrite-mode (arg) | |
2563 | "Toggle binary overwrite mode. | |
2564 | With arg, turn binary overwrite mode on iff arg is positive. | |
2565 | In binary overwrite mode, printing characters typed in replace | |
2566 | existing text. Newlines are not treated specially, so typing at the | |
2567 | end of a line joins the line to the next, with the typed character | |
2568 | between them. Typing before a tab character simply replaces the tab | |
2569 | with the character typed. | |
2570 | \\[quoted-insert] replaces the text at the cursor, just as ordinary | |
2571 | typing characters do. | |
2572 | ||
2573 | Note that binary overwrite mode is not its own minor mode; it is a | |
2574 | specialization of overwrite-mode, entered by setting the | |
2575 | `overwrite-mode' variable to `overwrite-mode-binary'." | |
2076c87c JB |
2576 | (interactive "P") |
2577 | (setq overwrite-mode | |
b6a22db0 | 2578 | (if (if (null arg) |
a61099dd | 2579 | (not (eq overwrite-mode 'overwrite-mode-binary)) |
b6a22db0 JB |
2580 | (> (prefix-numeric-value arg) 0)) |
2581 | 'overwrite-mode-binary)) | |
2582 | (force-mode-line-update)) | |
2076c87c | 2583 | \f |
81e98f1a | 2584 | (defvar line-number-mode t |
a61099dd RS |
2585 | "*Non-nil means display line number in mode line.") |
2586 | ||
2587 | (defun line-number-mode (arg) | |
2588 | "Toggle Line Number mode. | |
2589 | With arg, turn Line Number mode on iff arg is positive. | |
2590 | When Line Number mode is enabled, the line number appears | |
2591 | in the mode line." | |
2592 | (interactive "P") | |
2593 | (setq line-number-mode | |
2594 | (if (null arg) (not line-number-mode) | |
2595 | (> (prefix-numeric-value arg) 0))) | |
2596 | (force-mode-line-update)) | |
2597 | ||
71601101 | 2598 | (defvar column-number-mode nil |
bcad4985 KH |
2599 | "*Non-nil means display column number in mode line.") |
2600 | ||
2601 | (defun column-number-mode (arg) | |
2602 | "Toggle Column Number mode. | |
2603 | With arg, turn Column Number mode on iff arg is positive. | |
2604 | When Column Number mode is enabled, the column number appears | |
2605 | in the mode line." | |
2606 | (interactive "P") | |
2607 | (setq column-number-mode | |
2608 | (if (null arg) (not column-number-mode) | |
2609 | (> (prefix-numeric-value arg) 0))) | |
2610 | (force-mode-line-update)) | |
2611 | ||
2076c87c JB |
2612 | (defvar blink-matching-paren t |
2613 | "*Non-nil means show matching open-paren when close-paren is inserted.") | |
2614 | ||
29fc44dd KH |
2615 | (defvar blink-matching-paren-on-screen t |
2616 | "*Non-nil means show matching open-paren when it is on screen. | |
2617 | nil means don't show it (but the open-paren can still be shown | |
2618 | when it is off screen.") | |
2619 | ||
37315725 RS |
2620 | (defconst blink-matching-paren-distance 12000 |
2621 | "*If non-nil, is maximum distance to search for matching open-paren.") | |
2076c87c | 2622 | |
72dddf8b RS |
2623 | (defconst blink-matching-delay 1 |
2624 | "*The number of seconds that `blink-matching-open' will delay at a match.") | |
2625 | ||
903b7f65 RS |
2626 | (defconst blink-matching-paren-dont-ignore-comments nil |
2627 | "*Non-nil means `blink-matching-paren' should not ignore comments.") | |
2628 | ||
2076c87c JB |
2629 | (defun blink-matching-open () |
2630 | "Move cursor momentarily to the beginning of the sexp before point." | |
2631 | (interactive) | |
2632 | (and (> (point) (1+ (point-min))) | |
2076c87c | 2633 | blink-matching-paren |
7e1ddd45 RS |
2634 | ;; Verify an even number of quoting characters precede the close. |
2635 | (= 1 (logand 1 (- (point) | |
2636 | (save-excursion | |
2637 | (forward-char -1) | |
2638 | (skip-syntax-backward "/\\") | |
2639 | (point))))) | |
2076c87c JB |
2640 | (let* ((oldpos (point)) |
2641 | (blinkpos) | |
2642 | (mismatch)) | |
2643 | (save-excursion | |
2644 | (save-restriction | |
2645 | (if blink-matching-paren-distance | |
2646 | (narrow-to-region (max (point-min) | |
2647 | (- (point) blink-matching-paren-distance)) | |
2648 | oldpos)) | |
2649 | (condition-case () | |
903b7f65 RS |
2650 | (let ((parse-sexp-ignore-comments |
2651 | (and parse-sexp-ignore-comments | |
2652 | (not blink-matching-paren-dont-ignore-comments)))) | |
2653 | (setq blinkpos (scan-sexps oldpos -1))) | |
2076c87c | 2654 | (error nil))) |
903b7f65 RS |
2655 | (and blinkpos |
2656 | (/= (char-syntax (char-after blinkpos)) | |
2657 | ?\$) | |
2076c87c | 2658 | (setq mismatch |
903b7f65 RS |
2659 | (or (null (matching-paren (char-after blinkpos))) |
2660 | (/= (char-after (1- oldpos)) | |
2661 | (matching-paren (char-after blinkpos)))))) | |
2076c87c JB |
2662 | (if mismatch (setq blinkpos nil)) |
2663 | (if blinkpos | |
2664 | (progn | |
2665 | (goto-char blinkpos) | |
2666 | (if (pos-visible-in-window-p) | |
29fc44dd KH |
2667 | (and blink-matching-paren-on-screen |
2668 | (sit-for blink-matching-delay)) | |
2076c87c JB |
2669 | (goto-char blinkpos) |
2670 | (message | |
2671 | "Matches %s" | |
e9f1d66d | 2672 | ;; Show what precedes the open in its line, if anything. |
2076c87c JB |
2673 | (if (save-excursion |
2674 | (skip-chars-backward " \t") | |
2675 | (not (bolp))) | |
2676 | (buffer-substring (progn (beginning-of-line) (point)) | |
2677 | (1+ blinkpos)) | |
e9f1d66d RS |
2678 | ;; Show what follows the open in its line, if anything. |
2679 | (if (save-excursion | |
2680 | (forward-char 1) | |
2681 | (skip-chars-forward " \t") | |
2682 | (not (eolp))) | |
2683 | (buffer-substring blinkpos | |
2684 | (progn (end-of-line) (point))) | |
267935b9 RS |
2685 | ;; Otherwise show the previous nonblank line, |
2686 | ;; if there is one. | |
2687 | (if (save-excursion | |
2688 | (skip-chars-backward "\n \t") | |
2689 | (not (bobp))) | |
2690 | (concat | |
2691 | (buffer-substring (progn | |
2692 | (skip-chars-backward "\n \t") | |
2693 | (beginning-of-line) | |
2694 | (point)) | |
2695 | (progn (end-of-line) | |
2696 | (skip-chars-backward " \t") | |
2697 | (point))) | |
2698 | ;; Replace the newline and other whitespace with `...'. | |
2699 | "..." | |
2700 | (buffer-substring blinkpos (1+ blinkpos))) | |
2701 | ;; There is nothing to show except the char itself. | |
2702 | (buffer-substring blinkpos (1+ blinkpos)))))))) | |
2076c87c JB |
2703 | (cond (mismatch |
2704 | (message "Mismatched parentheses")) | |
2705 | ((not blink-matching-paren-distance) | |
2706 | (message "Unmatched parenthesis")))))))) | |
2707 | ||
2708 | ;Turned off because it makes dbx bomb out. | |
2709 | (setq blink-paren-function 'blink-matching-open) | |
2710 | ||
9a1277dd RS |
2711 | ;; This executes C-g typed while Emacs is waiting for a command. |
2712 | ;; Quitting out of a program does not go through here; | |
2713 | ;; that happens in the QUIT macro at the C code level. | |
2076c87c | 2714 | (defun keyboard-quit () |
af39530e RS |
2715 | "Signal a quit condition. |
2716 | During execution of Lisp code, this character causes a quit directly. | |
2717 | At top-level, as an editor command, this simply beeps." | |
2076c87c | 2718 | (interactive) |
19d35374 | 2719 | (deactivate-mark) |
2076c87c JB |
2720 | (signal 'quit nil)) |
2721 | ||
2722 | (define-key global-map "\C-g" 'keyboard-quit) | |
c66587fe | 2723 | |
1c6c6fde RS |
2724 | (defvar buffer-quit-function nil |
2725 | "Function to call to \"quit\" the current buffer, or nil if none. | |
2726 | \\[keyboard-escape-quit] calls this function when its more local actions | |
2727 | \(such as cancelling a prefix argument, minibuffer or region) do not apply.") | |
2728 | ||
c66587fe RS |
2729 | (defun keyboard-escape-quit () |
2730 | "Exit the current \"mode\" (in a generalized sense of the word). | |
2731 | This command can exit an interactive command such as `query-replace', | |
2732 | can clear out a prefix argument or a region, | |
2733 | can get out of the minibuffer or other recursive edit, | |
1c6c6fde RS |
2734 | cancel the use of the current buffer (for special-purpose buffers), |
2735 | or go back to just one window (by deleting all but the selected window)." | |
c66587fe RS |
2736 | (interactive) |
2737 | (cond ((eq last-command 'mode-exited) nil) | |
2738 | ((> (minibuffer-depth) 0) | |
2739 | (abort-recursive-edit)) | |
2740 | (current-prefix-arg | |
2741 | nil) | |
2742 | ((and transient-mark-mode | |
2743 | mark-active) | |
2744 | (deactivate-mark)) | |
1c6c6fde RS |
2745 | (buffer-quit-function |
2746 | (funcall buffer-quit-function)) | |
c66587fe RS |
2747 | ((not (one-window-p t)) |
2748 | (delete-other-windows)))) | |
2749 | ||
1c6c6fde | 2750 | (define-key global-map "\e\e\e" 'keyboard-escape-quit) |
2076c87c JB |
2751 | \f |
2752 | (defun set-variable (var val) | |
2753 | "Set VARIABLE to VALUE. VALUE is a Lisp object. | |
2754 | When using this interactively, supply a Lisp expression for VALUE. | |
3a801d0c ER |
2755 | If you want VALUE to be a string, you must surround it with doublequotes. |
2756 | ||
2757 | If VARIABLE has a `variable-interactive' property, that is used as if | |
2758 | it were the arg to `interactive' (which see) to interactively read the value." | |
2076c87c JB |
2759 | (interactive |
2760 | (let* ((var (read-variable "Set variable: ")) | |
2761 | (minibuffer-help-form | |
2762 | '(funcall myhelp)) | |
2763 | (myhelp | |
2764 | (function | |
2765 | (lambda () | |
2766 | (with-output-to-temp-buffer "*Help*" | |
2767 | (prin1 var) | |
2768 | (princ "\nDocumentation:\n") | |
2769 | (princ (substring (documentation-property var 'variable-documentation) | |
2770 | 1)) | |
2771 | (if (boundp var) | |
2772 | (let ((print-length 20)) | |
2773 | (princ "\n\nCurrent value: ") | |
2774 | (prin1 (symbol-value var)))) | |
e6bcd155 KH |
2775 | (save-excursion |
2776 | (set-buffer standard-output) | |
2777 | (help-mode)) | |
2076c87c JB |
2778 | nil))))) |
2779 | (list var | |
3a801d0c ER |
2780 | (let ((prop (get var 'variable-interactive))) |
2781 | (if prop | |
2782 | ;; Use VAR's `variable-interactive' property | |
2783 | ;; as an interactive spec for prompting. | |
2784 | (call-interactively (list 'lambda '(arg) | |
2785 | (list 'interactive prop) | |
2786 | 'arg)) | |
2787 | (eval-minibuffer (format "Set %s to value: " var))))))) | |
2076c87c | 2788 | (set var val)) |
e8a700bf RS |
2789 | \f |
2790 | ;; Define the major mode for lists of completions. | |
2791 | ||
98b45886 RS |
2792 | (defvar completion-list-mode-map nil |
2793 | "Local map for completion list buffers.") | |
ac29eb79 | 2794 | (or completion-list-mode-map |
e8a700bf RS |
2795 | (let ((map (make-sparse-keymap))) |
2796 | (define-key map [mouse-2] 'mouse-choose-completion) | |
eaf76065 | 2797 | (define-key map [down-mouse-2] nil) |
80298193 | 2798 | (define-key map "\C-m" 'choose-completion) |
1c6c6fde | 2799 | (define-key map "\e\e\e" 'delete-completion-window) |
dde69dbe RS |
2800 | (define-key map [left] 'previous-completion) |
2801 | (define-key map [right] 'next-completion) | |
ac29eb79 | 2802 | (setq completion-list-mode-map map))) |
e8a700bf RS |
2803 | |
2804 | ;; Completion mode is suitable only for specially formatted data. | |
ac29eb79 | 2805 | (put 'completion-list-mode 'mode-class 'special) |
e8a700bf | 2806 | |
98b45886 RS |
2807 | (defvar completion-reference-buffer nil |
2808 | "Record the buffer that was current when the completion list was requested. | |
2809 | This is a local variable in the completion list buffer. | |
ec39964e | 2810 | Initial value is nil to avoid some compiler warnings.") |
3819736b | 2811 | |
98b45886 RS |
2812 | (defvar completion-base-size nil |
2813 | "Number of chars at beginning of minibuffer not involved in completion. | |
2814 | This is a local variable in the completion list buffer | |
2815 | but it talks about the buffer in `completion-reference-buffer'. | |
2816 | If this is nil, it means to compare text to determine which part | |
2817 | of the tail end of the buffer's text is involved in completion.") | |
f6b293e3 | 2818 | |
1c6c6fde RS |
2819 | (defun delete-completion-window () |
2820 | "Delete the completion list window. | |
2821 | Go to the window from which completion was requested." | |
2822 | (interactive) | |
2823 | (let ((buf completion-reference-buffer)) | |
2824 | (delete-window (selected-window)) | |
2825 | (if (get-buffer-window buf) | |
2826 | (select-window (get-buffer-window buf))))) | |
2827 | ||
dde69dbe RS |
2828 | (defun previous-completion (n) |
2829 | "Move to the previous item in the completion list." | |
2830 | (interactive "p") | |
2831 | (next-completion (- n))) | |
2832 | ||
2833 | (defun next-completion (n) | |
2834 | "Move to the next item in the completion list. | |
1f238ac2 | 2835 | With prefix argument N, move N items (negative N means move backward)." |
dde69dbe RS |
2836 | (interactive "p") |
2837 | (while (and (> n 0) (not (eobp))) | |
b61a81c2 RS |
2838 | (let ((prop (get-text-property (point) 'mouse-face)) |
2839 | (end (point-max))) | |
dde69dbe RS |
2840 | ;; If in a completion, move to the end of it. |
2841 | (if prop | |
b61a81c2 | 2842 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) |
dde69dbe | 2843 | ;; Move to start of next one. |
b61a81c2 | 2844 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) |
dde69dbe RS |
2845 | (setq n (1- n))) |
2846 | (while (and (< n 0) (not (bobp))) | |
b61a81c2 RS |
2847 | (let ((prop (get-text-property (1- (point)) 'mouse-face)) |
2848 | (end (point-min))) | |
dde69dbe RS |
2849 | ;; If in a completion, move to the start of it. |
2850 | (if prop | |
b61a81c2 RS |
2851 | (goto-char (previous-single-property-change |
2852 | (point) 'mouse-face nil end))) | |
dde69dbe | 2853 | ;; Move to end of the previous completion. |
b61a81c2 | 2854 | (goto-char (previous-single-property-change (point) 'mouse-face nil end)) |
dde69dbe | 2855 | ;; Move to the start of that one. |
b61a81c2 | 2856 | (goto-char (previous-single-property-change (point) 'mouse-face nil end))) |
dde69dbe RS |
2857 | (setq n (1+ n)))) |
2858 | ||
80298193 RS |
2859 | (defun choose-completion () |
2860 | "Choose the completion that point is in or next to." | |
2861 | (interactive) | |
f6b293e3 RS |
2862 | (let (beg end completion (buffer completion-reference-buffer) |
2863 | (base-size completion-base-size)) | |
6096f362 RS |
2864 | (if (and (not (eobp)) (get-text-property (point) 'mouse-face)) |
2865 | (setq end (point) beg (1+ (point)))) | |
2866 | (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
3f299281 | 2867 | (setq end (1- (point)) beg (point))) |
6096f362 RS |
2868 | (if (null beg) |
2869 | (error "No completion here")) | |
2870 | (setq beg (previous-single-property-change beg 'mouse-face)) | |
88dd3c24 | 2871 | (setq end (or (next-single-property-change end 'mouse-face) (point-max))) |
ab63960f RS |
2872 | (setq completion (buffer-substring beg end)) |
2873 | (let ((owindow (selected-window))) | |
2874 | (if (and (one-window-p t 'selected-frame) | |
2875 | (window-dedicated-p (selected-window))) | |
2876 | ;; This is a special buffer's frame | |
2877 | (iconify-frame (selected-frame)) | |
2878 | (or (window-dedicated-p (selected-window)) | |
2879 | (bury-buffer))) | |
2880 | (select-window owindow)) | |
f6b293e3 | 2881 | (choose-completion-string completion buffer base-size))) |
80298193 RS |
2882 | |
2883 | ;; Delete the longest partial match for STRING | |
2884 | ;; that can be found before POINT. | |
2885 | (defun choose-completion-delete-max-match (string) | |
2886 | (let ((opoint (point)) | |
2887 | (len (min (length string) | |
2888 | (- (point) (point-min))))) | |
2889 | (goto-char (- (point) (length string))) | |
61bbf6fe RS |
2890 | (if completion-ignore-case |
2891 | (setq string (downcase string))) | |
80298193 RS |
2892 | (while (and (> len 0) |
2893 | (let ((tail (buffer-substring (point) | |
2894 | (+ (point) len)))) | |
61bbf6fe RS |
2895 | (if completion-ignore-case |
2896 | (setq tail (downcase tail))) | |
80298193 RS |
2897 | (not (string= tail (substring string 0 len))))) |
2898 | (setq len (1- len)) | |
2899 | (forward-char 1)) | |
2900 | (delete-char len))) | |
2901 | ||
98b45886 RS |
2902 | ;; Switch to BUFFER and insert the completion choice CHOICE. |
2903 | ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text | |
2904 | ;; to keep. If it is nil, use choose-completion-delete-max-match instead. | |
f6b293e3 | 2905 | (defun choose-completion-string (choice &optional buffer base-size) |
80298193 | 2906 | (let ((buffer (or buffer completion-reference-buffer))) |
cf52ad58 RS |
2907 | ;; If BUFFER is a minibuffer, barf unless it's the currently |
2908 | ;; active minibuffer. | |
2909 | (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer)) | |
45486731 RS |
2910 | (or (not (active-minibuffer-window)) |
2911 | (not (equal buffer | |
2912 | (window-buffer (active-minibuffer-window)))))) | |
cf52ad58 RS |
2913 | (error "Minibuffer is not active for completion") |
2914 | ;; Insert the completion into the buffer where completion was requested. | |
2915 | (set-buffer buffer) | |
f6b293e3 RS |
2916 | (if base-size |
2917 | (delete-region (+ base-size (point-min)) (point)) | |
2918 | (choose-completion-delete-max-match choice)) | |
cf52ad58 | 2919 | (insert choice) |
63240af1 RS |
2920 | (remove-text-properties (- (point) (length choice)) (point) |
2921 | '(mouse-face nil)) | |
cf52ad58 RS |
2922 | ;; Update point in the window that BUFFER is showing in. |
2923 | (let ((window (get-buffer-window buffer t))) | |
2924 | (set-window-point window (point))) | |
2925 | ;; If completing for the minibuffer, exit it with this choice. | |
2926 | (and (equal buffer (window-buffer (minibuffer-window))) | |
8881ad9a RS |
2927 | minibuffer-completion-table |
2928 | (exit-minibuffer))))) | |
80298193 | 2929 | |
ac29eb79 | 2930 | (defun completion-list-mode () |
e8a700bf | 2931 | "Major mode for buffers showing lists of possible completions. |
80298193 RS |
2932 | Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\ |
2933 | to select the completion near point. | |
2934 | Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\ | |
2935 | with the mouse." | |
e8a700bf RS |
2936 | (interactive) |
2937 | (kill-all-local-variables) | |
ac29eb79 RS |
2938 | (use-local-map completion-list-mode-map) |
2939 | (setq mode-name "Completion List") | |
2940 | (setq major-mode 'completion-list-mode) | |
f6b293e3 RS |
2941 | (make-local-variable 'completion-base-size) |
2942 | (setq completion-base-size nil) | |
ac29eb79 | 2943 | (run-hooks 'completion-list-mode-hook)) |
e8a700bf | 2944 | |
98b45886 RS |
2945 | (defvar completion-fixup-function nil |
2946 | "A function to customize how completions are identified in completion lists. | |
2947 | `completion-setup-function' calls this function with no arguments | |
2948 | each time it has found what it thinks is one completion. | |
2949 | Point is at the end of the completion in the completion list buffer. | |
2950 | If this function moves point, it can alter the end of that completion.") | |
2951 | ||
2952 | ;; This function goes in completion-setup-hook, so that it is called | |
2953 | ;; after the text of the completion list buffer is written. | |
6096f362 | 2954 | |
e8a700bf RS |
2955 | (defun completion-setup-function () |
2956 | (save-excursion | |
98b45886 | 2957 | (let ((mainbuf (current-buffer))) |
3819736b RS |
2958 | (set-buffer standard-output) |
2959 | (completion-list-mode) | |
2960 | (make-local-variable 'completion-reference-buffer) | |
2961 | (setq completion-reference-buffer mainbuf) | |
98b45886 RS |
2962 | ;;; The value 0 is right in most cases, but not for file name completion. |
2963 | ;;; so this has to be turned off. | |
2964 | ;;; (setq completion-base-size 0) | |
3819736b RS |
2965 | (goto-char (point-min)) |
2966 | (if window-system | |
2967 | (insert (substitute-command-keys | |
80298193 RS |
2968 | "Click \\[mouse-choose-completion] on a completion to select it.\n"))) |
2969 | (insert (substitute-command-keys | |
2970 | "In this buffer, type \\[choose-completion] to \ | |
c26bb96e KH |
2971 | select the completion near point.\n\n")) |
2972 | (forward-line 1) | |
6096f362 RS |
2973 | (while (re-search-forward "[^ \t\n]+\\( [^ \t\n]+\\)*" nil t) |
2974 | (let ((beg (match-beginning 0)) | |
2975 | (end (point))) | |
2976 | (if completion-fixup-function | |
2977 | (funcall completion-fixup-function)) | |
2978 | (put-text-property beg (point) 'mouse-face 'highlight) | |
2979 | (goto-char end)))))) | |
c88ab9ce | 2980 | |
e8a700bf | 2981 | (add-hook 'completion-setup-hook 'completion-setup-function) |
dde69dbe RS |
2982 | |
2983 | (define-key minibuffer-local-completion-map [prior] | |
2984 | 'switch-to-completions) | |
2985 | (define-key minibuffer-local-must-match-map [prior] | |
2986 | 'switch-to-completions) | |
2987 | (define-key minibuffer-local-completion-map "\M-v" | |
2988 | 'switch-to-completions) | |
2989 | (define-key minibuffer-local-must-match-map "\M-v" | |
2990 | 'switch-to-completions) | |
2991 | ||
2992 | (defun switch-to-completions () | |
2993 | "Select the completion list window." | |
2994 | (interactive) | |
9595fbdb RS |
2995 | ;; Make sure we have a completions window. |
2996 | (or (get-buffer-window "*Completions*") | |
2997 | (minibuffer-completion-help)) | |
dde69dbe RS |
2998 | (select-window (get-buffer-window "*Completions*")) |
2999 | (goto-char (point-min)) | |
3000 | (search-forward "\n\n") | |
3001 | (forward-line 1)) | |
a3d1480b | 3002 | \f |
82072f33 RS |
3003 | ;; Support keyboard commands to turn on various modifiers. |
3004 | ||
3005 | ;; These functions -- which are not commands -- each add one modifier | |
3006 | ;; to the following event. | |
3007 | ||
3008 | (defun event-apply-alt-modifier (ignore-prompt) | |
3009 | (vector (event-apply-modifier (read-event) 'alt 22 "A-"))) | |
3010 | (defun event-apply-super-modifier (ignore-prompt) | |
3011 | (vector (event-apply-modifier (read-event) 'super 23 "s-"))) | |
3012 | (defun event-apply-hyper-modifier (ignore-prompt) | |
3013 | (vector (event-apply-modifier (read-event) 'hyper 24 "H-"))) | |
3014 | (defun event-apply-shift-modifier (ignore-prompt) | |
3015 | (vector (event-apply-modifier (read-event) 'shift 25 "S-"))) | |
3016 | (defun event-apply-control-modifier (ignore-prompt) | |
3017 | (vector (event-apply-modifier (read-event) 'control 26 "C-"))) | |
3018 | (defun event-apply-meta-modifier (ignore-prompt) | |
3019 | (vector (event-apply-modifier (read-event) 'meta 27 "M-"))) | |
3020 | ||
3021 | (defun event-apply-modifier (event symbol lshiftby prefix) | |
3022 | "Apply a modifier flag to event EVENT. | |
3023 | SYMBOL is the name of this modifier, as a symbol. | |
3024 | LSHIFTBY is the numeric value of this modifier, in keyboard events. | |
3025 | PREFIX is the string that represents this modifier in an event type symbol." | |
3026 | (if (numberp event) | |
3027 | (cond ((eq symbol 'control) | |
90bebcb0 KH |
3028 | (if (and (<= (downcase event) ?z) |
3029 | (>= (downcase event) ?a)) | |
82072f33 | 3030 | (- (downcase event) ?a -1) |
90bebcb0 KH |
3031 | (if (and (<= (downcase event) ?Z) |
3032 | (>= (downcase event) ?A)) | |
82072f33 RS |
3033 | (- (downcase event) ?A -1) |
3034 | (logior (lsh 1 lshiftby) event)))) | |
3035 | ((eq symbol 'shift) | |
3036 | (if (and (<= (downcase event) ?z) | |
3037 | (>= (downcase event) ?a)) | |
3038 | (upcase event) | |
3039 | (logior (lsh 1 lshiftby) event))) | |
3040 | (t | |
3041 | (logior (lsh 1 lshiftby) event))) | |
3042 | (if (memq symbol (event-modifiers event)) | |
3043 | event | |
3044 | (let ((event-type (if (symbolp event) event (car event)))) | |
3045 | (setq event-type (intern (concat prefix (symbol-name event-type)))) | |
3046 | (if (symbolp event) | |
3047 | event-type | |
3048 | (cons event-type (cdr event))))))) | |
3049 | ||
e5fff738 KH |
3050 | (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier) |
3051 | (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier) | |
3052 | (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier) | |
3053 | (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier) | |
3054 | (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier) | |
3055 | (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier) | |
82072f33 | 3056 | \f |
a3d1480b JB |
3057 | ;;;; Keypad support. |
3058 | ||
3059 | ;;; Make the keypad keys act like ordinary typing keys. If people add | |
3060 | ;;; bindings for the function key symbols, then those bindings will | |
3061 | ;;; override these, so this shouldn't interfere with any existing | |
3062 | ;;; bindings. | |
3063 | ||
0d173134 | 3064 | ;; Also tell read-char how to handle these keys. |
a3d1480b JB |
3065 | (mapcar |
3066 | (lambda (keypad-normal) | |
3067 | (let ((keypad (nth 0 keypad-normal)) | |
3068 | (normal (nth 1 keypad-normal))) | |
0d173134 | 3069 | (put keypad 'ascii-character normal) |
a3d1480b JB |
3070 | (define-key function-key-map (vector keypad) (vector normal)))) |
3071 | '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4) | |
3072 | (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9) | |
3073 | (kp-space ?\ ) | |
3074 | (kp-tab ?\t) | |
3075 | (kp-enter ?\r) | |
3076 | (kp-multiply ?*) | |
3077 | (kp-add ?+) | |
3078 | (kp-separator ?,) | |
3079 | (kp-subtract ?-) | |
3080 | (kp-decimal ?.) | |
3081 | (kp-divide ?/) | |
3082 | (kp-equal ?=))) | |
3083 | ||
c88ab9ce | 3084 | ;;; simple.el ends here |