Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; simple.el --- basic editing commands for Emacs |
2 | ||
5a8dd30e | 3 | ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 1998 |
69c1dd37 | 4 | ;; Free Software Foundation, Inc. |
2076c87c JB |
5 | |
6 | ;; This file is part of GNU Emacs. | |
7 | ||
8 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 | ;; it under the terms of the GNU General Public License as published by | |
3a801d0c | 10 | ;; the Free Software Foundation; either version 2, or (at your option) |
2076c87c JB |
11 | ;; any later version. |
12 | ||
13 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;; GNU General Public License for more details. | |
17 | ||
18 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
19 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
20 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | ;; Boston, MA 02111-1307, USA. | |
2076c87c | 22 | |
d9ecc911 ER |
23 | ;;; Commentary: |
24 | ||
25 | ;; A grab-bag of basic Emacs commands not specifically related to some | |
26 | ;; major mode or to file-handling. | |
27 | ||
3a801d0c | 28 | ;;; Code: |
2076c87c | 29 | |
69c1dd37 RS |
30 | (defgroup killing nil |
31 | "Killing and yanking commands" | |
32 | :group 'editing) | |
33 | ||
34 | (defgroup fill-comments nil | |
35 | "Indenting and filling of comments." | |
36 | :prefix "comment-" | |
37 | :group 'fill) | |
38 | ||
39 | (defgroup paren-matching nil | |
40 | "Highlight (un)matching of parens and expressions." | |
69c1dd37 RS |
41 | :group 'matching) |
42 | ||
43 | ||
30bb9754 | 44 | (defun newline (&optional arg) |
d133d835 | 45 | "Insert a newline, and move to left margin of the new line if it's blank. |
30bb9754 BG |
46 | The newline is marked with the text-property `hard'. |
47 | With arg, insert that many newlines. | |
48 | In Auto Fill mode, if no numeric arg, break the preceding line if it's long." | |
49 | (interactive "*P") | |
4c4cbf11 | 50 | (barf-if-buffer-read-only) |
30bb9754 BG |
51 | ;; Inserting a newline at the end of a line produces better redisplay in |
52 | ;; try_window_id than inserting at the beginning of a line, and the textual | |
53 | ;; result is the same. So, if we're at beginning of line, pretend to be at | |
54 | ;; the end of the previous line. | |
55 | (let ((flag (and (not (bobp)) | |
56 | (bolp) | |
1cd24721 RS |
57 | ;; Make sure no functions want to be told about |
58 | ;; the range of the changes. | |
59 | (not after-change-function) | |
60 | (not before-change-function) | |
61 | (not after-change-functions) | |
62 | (not before-change-functions) | |
fd977703 RS |
63 | ;; Make sure there are no markers here. |
64 | (not (buffer-has-markers-at (1- (point)))) | |
2f047f6c | 65 | (not (buffer-has-markers-at (point))) |
1cd24721 RS |
66 | ;; Make sure no text properties want to know |
67 | ;; where the change was. | |
68 | (not (get-char-property (1- (point)) 'modification-hooks)) | |
69 | (not (get-char-property (1- (point)) 'insert-behind-hooks)) | |
70 | (or (eobp) | |
71 | (not (get-char-property (point) 'insert-in-front-hooks))) | |
31a5333f MB |
72 | ;; Make sure the newline before point isn't intangible. |
73 | (not (get-char-property (1- (point)) 'intangible)) | |
74 | ;; Make sure the newline before point isn't read-only. | |
75 | (not (get-char-property (1- (point)) 'read-only)) | |
76 | ;; Make sure the newline before point isn't invisible. | |
77 | (not (get-char-property (1- (point)) 'invisible)) | |
78 | ;; Make sure the newline before point has the same | |
79 | ;; properties as the char before it (if any). | |
30bb9754 | 80 | (< (or (previous-property-change (point)) -2) |
d133d835 RS |
81 | (- (point) 2)))) |
82 | (was-page-start (and (bolp) | |
83 | (looking-at page-delimiter))) | |
84 | (beforepos (point))) | |
30bb9754 BG |
85 | (if flag (backward-char 1)) |
86 | ;; Call self-insert so that auto-fill, abbrev expansion etc. happens. | |
87 | ;; Set last-command-char to tell self-insert what to insert. | |
88 | (let ((last-command-char ?\n) | |
89 | ;; Don't auto-fill if we have a numeric argument. | |
3954fff9 RS |
90 | ;; Also not if flag is true (it would fill wrong line); |
91 | ;; there is no need to since we're at BOL. | |
92 | (auto-fill-function (if (or arg flag) nil auto-fill-function))) | |
4cc9d0dc RS |
93 | (unwind-protect |
94 | (self-insert-command (prefix-numeric-value arg)) | |
95 | ;; If we get an error in self-insert-command, put point at right place. | |
96 | (if flag (forward-char 1)))) | |
2f047f6c KH |
97 | ;; Even if we did *not* get an error, keep that forward-char; |
98 | ;; all further processing should apply to the newline that the user | |
99 | ;; thinks he inserted. | |
100 | ||
30bb9754 BG |
101 | ;; Mark the newline(s) `hard'. |
102 | (if use-hard-newlines | |
2f047f6c | 103 | (set-hard-newline-properties |
55741b46 | 104 | (- (point) (if arg (prefix-numeric-value arg) 1)) (point))) |
d133d835 RS |
105 | ;; If the newline leaves the previous line blank, |
106 | ;; and we have a left margin, delete that from the blank line. | |
107 | (or flag | |
108 | (save-excursion | |
109 | (goto-char beforepos) | |
110 | (beginning-of-line) | |
111 | (and (looking-at "[ \t]$") | |
112 | (> (current-left-margin) 0) | |
113 | (delete-region (point) (progn (end-of-line) (point)))))) | |
d133d835 RS |
114 | ;; Indent the line after the newline, except in one case: |
115 | ;; when we added the newline at the beginning of a line | |
116 | ;; which starts a page. | |
117 | (or was-page-start | |
118 | (move-to-left-margin nil t))) | |
30bb9754 BG |
119 | nil) |
120 | ||
55741b46 RS |
121 | (defun set-hard-newline-properties (from to) |
122 | (let ((sticky (get-text-property from 'rear-nonsticky))) | |
123 | (put-text-property from to 'hard 't) | |
124 | ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list | |
125 | (if (and (listp sticky) (not (memq 'hard sticky))) | |
126 | (put-text-property from (point) 'rear-nonsticky | |
127 | (cons 'hard sticky))))) | |
128 | ||
2076c87c | 129 | (defun open-line (arg) |
ff1fbe3e | 130 | "Insert a newline and leave point before it. |
3db1e3b5 | 131 | If there is a fill prefix and/or a left-margin, insert them on the new line |
d133d835 | 132 | if the line would have been blank. |
616ed245 | 133 | With arg N, insert N newlines." |
2076c87c | 134 | (interactive "*p") |
616ed245 | 135 | (let* ((do-fill-prefix (and fill-prefix (bolp))) |
3db1e3b5 | 136 | (do-left-margin (and (bolp) (> (current-left-margin) 0))) |
28191e20 | 137 | (loc (point))) |
d133d835 RS |
138 | (newline arg) |
139 | (goto-char loc) | |
28191e20 | 140 | (while (> arg 0) |
d133d835 RS |
141 | (cond ((bolp) |
142 | (if do-left-margin (indent-to (current-left-margin))) | |
143 | (if do-fill-prefix (insert-and-inherit fill-prefix)))) | |
144 | (forward-line 1) | |
28191e20 | 145 | (setq arg (1- arg))) |
d133d835 RS |
146 | (goto-char loc) |
147 | (end-of-line))) | |
2076c87c JB |
148 | |
149 | (defun split-line () | |
150 | "Split current line, moving portion beyond point vertically down." | |
151 | (interactive "*") | |
152 | (skip-chars-forward " \t") | |
153 | (let ((col (current-column)) | |
154 | (pos (point))) | |
28191e20 | 155 | (newline 1) |
2076c87c JB |
156 | (indent-to col 0) |
157 | (goto-char pos))) | |
158 | ||
159 | (defun quoted-insert (arg) | |
160 | "Read next input character and insert it. | |
ff1fbe3e | 161 | This is useful for inserting control characters. |
37fb0200 RS |
162 | |
163 | If the first character you type after this command is an octal digit, | |
164 | you should type a sequence of octal digits which specify a character code. | |
037a2961 | 165 | Any nondigit terminates the sequence. If the terminator is a RET, |
37fb0200 | 166 | it is discarded; any other terminator is used itself as input. |
037a2961 RS |
167 | The variable `read-quoted-char-radix' specifies the radix for this feature; |
168 | set it to 10 or 16 to use decimal or hex instead of octal. | |
b6a22db0 JB |
169 | |
170 | In overwrite mode, this function inserts the character anyway, and | |
171 | does not handle octal digits specially. This means that if you use | |
172 | overwrite as your normal editing mode, you can use this function to | |
173 | insert characters when necessary. | |
174 | ||
175 | In binary overwrite mode, this function does overwrite, and octal | |
37fb0200 RS |
176 | digits are interpreted as a character code. This is intended to be |
177 | useful for editing binary files." | |
2076c87c | 178 | (interactive "*p") |
b6a22db0 JB |
179 | (let ((char (if (or (not overwrite-mode) |
180 | (eq overwrite-mode 'overwrite-mode-binary)) | |
181 | (read-quoted-char) | |
182 | (read-char)))) | |
5280b595 KH |
183 | ;; Assume character codes 0240 - 0377 stand for characters in some |
184 | ;; single-byte character set, and convert them to Emacs | |
185 | ;; characters. | |
186 | (if (and enable-multibyte-characters | |
187 | (>= char ?\240) | |
188 | (<= char ?\377)) | |
189 | (setq char (unibyte-char-to-multibyte char))) | |
ec321cad RS |
190 | (if (> arg 0) |
191 | (if (eq overwrite-mode 'overwrite-mode-binary) | |
192 | (delete-char arg))) | |
193 | (while (> arg 0) | |
194 | (insert-and-inherit char) | |
195 | (setq arg (1- arg))))) | |
2076c87c JB |
196 | |
197 | (defun delete-indentation (&optional arg) | |
198 | "Join this line to previous and fix up whitespace at join. | |
ccc58657 | 199 | If there is a fill prefix, delete it from the beginning of this line. |
2076c87c JB |
200 | With argument, join this line to following line." |
201 | (interactive "*P") | |
202 | (beginning-of-line) | |
203 | (if arg (forward-line 1)) | |
204 | (if (eq (preceding-char) ?\n) | |
205 | (progn | |
206 | (delete-region (point) (1- (point))) | |
ccc58657 RS |
207 | ;; If the second line started with the fill prefix, |
208 | ;; delete the prefix. | |
209 | (if (and fill-prefix | |
01b8e020 | 210 | (<= (+ (point) (length fill-prefix)) (point-max)) |
ccc58657 RS |
211 | (string= fill-prefix |
212 | (buffer-substring (point) | |
213 | (+ (point) (length fill-prefix))))) | |
214 | (delete-region (point) (+ (point) (length fill-prefix)))) | |
2076c87c JB |
215 | (fixup-whitespace)))) |
216 | ||
217 | (defun fixup-whitespace () | |
218 | "Fixup white space between objects around point. | |
219 | Leave one space or none, according to the context." | |
220 | (interactive "*") | |
221 | (save-excursion | |
222 | (delete-horizontal-space) | |
223 | (if (or (looking-at "^\\|\\s)") | |
224 | (save-excursion (forward-char -1) | |
225 | (looking-at "$\\|\\s(\\|\\s'"))) | |
226 | nil | |
227 | (insert ?\ )))) | |
228 | ||
229 | (defun delete-horizontal-space () | |
230 | "Delete all spaces and tabs around point." | |
231 | (interactive "*") | |
232 | (skip-chars-backward " \t") | |
233 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
234 | ||
235 | (defun just-one-space () | |
236 | "Delete all spaces and tabs around point, leaving one space." | |
237 | (interactive "*") | |
238 | (skip-chars-backward " \t") | |
239 | (if (= (following-char) ? ) | |
240 | (forward-char 1) | |
241 | (insert ? )) | |
242 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
243 | ||
244 | (defun delete-blank-lines () | |
245 | "On blank line, delete all surrounding blank lines, leaving just one. | |
246 | On isolated blank line, delete that one. | |
6d30d416 | 247 | On nonblank line, delete any immediately following blank lines." |
2076c87c JB |
248 | (interactive "*") |
249 | (let (thisblank singleblank) | |
250 | (save-excursion | |
251 | (beginning-of-line) | |
252 | (setq thisblank (looking-at "[ \t]*$")) | |
70e14c01 | 253 | ;; Set singleblank if there is just one blank line here. |
2076c87c JB |
254 | (setq singleblank |
255 | (and thisblank | |
256 | (not (looking-at "[ \t]*\n[ \t]*$")) | |
257 | (or (bobp) | |
258 | (progn (forward-line -1) | |
259 | (not (looking-at "[ \t]*$"))))))) | |
70e14c01 | 260 | ;; Delete preceding blank lines, and this one too if it's the only one. |
2076c87c JB |
261 | (if thisblank |
262 | (progn | |
263 | (beginning-of-line) | |
264 | (if singleblank (forward-line 1)) | |
265 | (delete-region (point) | |
266 | (if (re-search-backward "[^ \t\n]" nil t) | |
267 | (progn (forward-line 1) (point)) | |
268 | (point-min))))) | |
70e14c01 JB |
269 | ;; Delete following blank lines, unless the current line is blank |
270 | ;; and there are no following blank lines. | |
2076c87c JB |
271 | (if (not (and thisblank singleblank)) |
272 | (save-excursion | |
273 | (end-of-line) | |
274 | (forward-line 1) | |
275 | (delete-region (point) | |
276 | (if (re-search-forward "[^ \t\n]" nil t) | |
277 | (progn (beginning-of-line) (point)) | |
70e14c01 JB |
278 | (point-max))))) |
279 | ;; Handle the special case where point is followed by newline and eob. | |
280 | ;; Delete the line, leaving point at eob. | |
281 | (if (looking-at "^[ \t]*\n\\'") | |
282 | (delete-region (point) (point-max))))) | |
2076c87c JB |
283 | |
284 | (defun back-to-indentation () | |
285 | "Move point to the first non-whitespace character on this line." | |
286 | (interactive) | |
287 | (beginning-of-line 1) | |
288 | (skip-chars-forward " \t")) | |
289 | ||
290 | (defun newline-and-indent () | |
291 | "Insert a newline, then indent according to major mode. | |
ff1fbe3e | 292 | Indentation is done using the value of `indent-line-function'. |
2076c87c | 293 | In programming language modes, this is the same as TAB. |
ff1fbe3e | 294 | In some text modes, where TAB inserts a tab, this command indents to the |
eed5698b | 295 | column specified by the function `current-left-margin'." |
2076c87c JB |
296 | (interactive "*") |
297 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
46947372 | 298 | (newline) |
2076c87c JB |
299 | (indent-according-to-mode)) |
300 | ||
301 | (defun reindent-then-newline-and-indent () | |
302 | "Reindent current line, insert newline, then indent the new line. | |
303 | Indentation of both lines is done according to the current major mode, | |
ff1fbe3e | 304 | which means calling the current value of `indent-line-function'. |
2076c87c JB |
305 | In programming language modes, this is the same as TAB. |
306 | In some text modes, where TAB inserts a tab, this indents to the | |
eed5698b | 307 | column specified by the function `current-left-margin'." |
2076c87c JB |
308 | (interactive "*") |
309 | (save-excursion | |
310 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
311 | (indent-according-to-mode)) | |
46947372 | 312 | (newline) |
2076c87c JB |
313 | (indent-according-to-mode)) |
314 | ||
dff7d67f RS |
315 | ;; Internal subroutine of delete-char |
316 | (defun kill-forward-chars (arg) | |
317 | (if (listp arg) (setq arg (car arg))) | |
318 | (if (eq arg '-) (setq arg -1)) | |
d5d99b80 | 319 | (kill-region (point) (forward-point arg))) |
dff7d67f RS |
320 | |
321 | ;; Internal subroutine of backward-delete-char | |
322 | (defun kill-backward-chars (arg) | |
323 | (if (listp arg) (setq arg (car arg))) | |
324 | (if (eq arg '-) (setq arg -1)) | |
d5d99b80 | 325 | (kill-region (point) (forward-point (- arg)))) |
dff7d67f | 326 | |
cc2b2b6c RS |
327 | (defcustom backward-delete-char-untabify-method 'untabify |
328 | "*The method for untabifying when deleting backward. | |
329 | Can be `untabify' -- turn a tab to many spaces, then delete one space. | |
330 | `hungry' -- delete all whitespace, both tabs and spaces. | |
331 | nil -- just delete one character." | |
332 | :type '(choice (const untabify) (const hungry) (const nil)) | |
333 | :group 'killing) | |
334 | ||
2076c87c JB |
335 | (defun backward-delete-char-untabify (arg &optional killp) |
336 | "Delete characters backward, changing tabs into spaces. | |
cc2b2b6c | 337 | The exact behavior depends on `backward-delete-char-untabify-method'. |
2076c87c JB |
338 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. |
339 | Interactively, ARG is the prefix arg (default 1) | |
aba05ae4 | 340 | and KILLP is t if a prefix arg was specified." |
2076c87c | 341 | (interactive "*p\nP") |
cc2b2b6c | 342 | (when (eq backward-delete-char-untabify-method 'untabify) |
dbe524b6 RS |
343 | (let ((count arg)) |
344 | (save-excursion | |
345 | (while (and (> count 0) (not (bobp))) | |
346 | (if (= (preceding-char) ?\t) | |
347 | (let ((col (current-column))) | |
348 | (forward-char -1) | |
349 | (setq col (- col (current-column))) | |
350 | (insert-char ?\ col) | |
351 | (delete-char 1))) | |
352 | (forward-char -1) | |
353 | (setq count (1- count)))))) | |
cc2b2b6c RS |
354 | (delete-backward-char |
355 | (if (eq backward-delete-char-untabify-method 'hungry) | |
356 | (let ((wh (- (point) (save-excursion (skip-chars-backward " \t") | |
357 | (point))))) | |
358 | (+ arg (if (zerop wh) 0 (1- wh)))) | |
359 | arg) | |
360 | killp)) | |
2076c87c JB |
361 | |
362 | (defun zap-to-char (arg char) | |
363 | "Kill up to and including ARG'th occurrence of CHAR. | |
364 | Goes backward if ARG is negative; error if CHAR not found." | |
365 | (interactive "p\ncZap to char: ") | |
366 | (kill-region (point) (progn | |
367 | (search-forward (char-to-string char) nil nil arg) | |
368 | ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) | |
369 | (point)))) | |
370 | ||
371 | (defun beginning-of-buffer (&optional arg) | |
372 | "Move point to the beginning of the buffer; leave mark at previous position. | |
c66587fe RS |
373 | With arg N, put point N/10 of the way from the beginning. |
374 | ||
375 | If the buffer is narrowed, this command uses the beginning and size | |
376 | of the accessible part of the buffer. | |
ff1fbe3e RS |
377 | |
378 | Don't use this command in Lisp programs! | |
2076c87c JB |
379 | \(goto-char (point-min)) is faster and avoids clobbering the mark." |
380 | (interactive "P") | |
381 | (push-mark) | |
c66587fe RS |
382 | (let ((size (- (point-max) (point-min)))) |
383 | (goto-char (if arg | |
384 | (+ (point-min) | |
385 | (if (> size 10000) | |
386 | ;; Avoid overflow for large buffer sizes! | |
387 | (* (prefix-numeric-value arg) | |
388 | (/ size 10)) | |
389 | (/ (+ 10 (* size (prefix-numeric-value arg))) 10))) | |
390 | (point-min)))) | |
2076c87c JB |
391 | (if arg (forward-line 1))) |
392 | ||
393 | (defun end-of-buffer (&optional arg) | |
394 | "Move point to the end of the buffer; leave mark at previous position. | |
c66587fe RS |
395 | With arg N, put point N/10 of the way from the end. |
396 | ||
397 | If the buffer is narrowed, this command uses the beginning and size | |
398 | of the accessible part of the buffer. | |
ff1fbe3e RS |
399 | |
400 | Don't use this command in Lisp programs! | |
2076c87c JB |
401 | \(goto-char (point-max)) is faster and avoids clobbering the mark." |
402 | (interactive "P") | |
403 | (push-mark) | |
c66587fe RS |
404 | (let ((size (- (point-max) (point-min)))) |
405 | (goto-char (if arg | |
406 | (- (point-max) | |
407 | (if (> size 10000) | |
408 | ;; Avoid overflow for large buffer sizes! | |
409 | (* (prefix-numeric-value arg) | |
410 | (/ size 10)) | |
411 | (/ (* size (prefix-numeric-value arg)) 10))) | |
412 | (point-max)))) | |
3a801d0c ER |
413 | ;; If we went to a place in the middle of the buffer, |
414 | ;; adjust it to the beginning of a line. | |
2076c87c | 415 | (if arg (forward-line 1) |
3a801d0c ER |
416 | ;; If the end of the buffer is not already on the screen, |
417 | ;; then scroll specially to put it near, but not at, the bottom. | |
418 | (if (let ((old-point (point))) | |
419 | (save-excursion | |
420 | (goto-char (window-start)) | |
421 | (vertical-motion (window-height)) | |
422 | (< (point) old-point))) | |
97dfc68c RS |
423 | (progn |
424 | (overlay-recenter (point)) | |
425 | (recenter -3))))) | |
2076c87c JB |
426 | |
427 | (defun mark-whole-buffer () | |
70e14c01 JB |
428 | "Put point at beginning and mark at end of buffer. |
429 | You probably should not use this function in Lisp programs; | |
430 | it is usually a mistake for a Lisp function to use any subroutine | |
431 | that uses or sets the mark." | |
2076c87c JB |
432 | (interactive) |
433 | (push-mark (point)) | |
fd0f4056 | 434 | (push-mark (point-max) nil t) |
2076c87c JB |
435 | (goto-char (point-min))) |
436 | ||
437 | (defun count-lines-region (start end) | |
eb8c3be9 | 438 | "Print number of lines and characters in the region." |
2076c87c JB |
439 | (interactive "r") |
440 | (message "Region has %d lines, %d characters" | |
441 | (count-lines start end) (- end start))) | |
442 | ||
443 | (defun what-line () | |
2578be76 | 444 | "Print the current buffer line number and narrowed line number of point." |
2076c87c | 445 | (interactive) |
2578be76 | 446 | (let ((opoint (point)) start) |
2076c87c | 447 | (save-excursion |
2578be76 RS |
448 | (save-restriction |
449 | (goto-char (point-min)) | |
450 | (widen) | |
451 | (beginning-of-line) | |
452 | (setq start (point)) | |
453 | (goto-char opoint) | |
454 | (beginning-of-line) | |
455 | (if (/= start 1) | |
456 | (message "line %d (narrowed line %d)" | |
457 | (1+ (count-lines 1 (point))) | |
458 | (1+ (count-lines start (point)))) | |
459 | (message "Line %d" (1+ (count-lines 1 (point))))))))) | |
460 | ||
2076c87c JB |
461 | |
462 | (defun count-lines (start end) | |
463 | "Return number of lines between START and END. | |
464 | This is usually the number of newlines between them, | |
ff1fbe3e | 465 | but can be one more if START is not equal to END |
2076c87c | 466 | and the greater of them is not at the start of a line." |
e406700d RS |
467 | (save-excursion |
468 | (save-restriction | |
469 | (narrow-to-region start end) | |
470 | (goto-char (point-min)) | |
471 | (if (eq selective-display t) | |
472 | (save-match-data | |
dde92ca6 RS |
473 | (let ((done 0)) |
474 | (while (re-search-forward "[\n\C-m]" nil t 40) | |
475 | (setq done (+ 40 done))) | |
476 | (while (re-search-forward "[\n\C-m]" nil t 1) | |
477 | (setq done (+ 1 done))) | |
043efc41 RS |
478 | (goto-char (point-max)) |
479 | (if (and (/= start end) | |
480 | (not (bolp))) | |
481 | (1+ done) | |
e406700d RS |
482 | done))) |
483 | (- (buffer-size) (forward-line (buffer-size))))))) | |
2076c87c | 484 | |
d5d99b80 KH |
485 | (defun what-cursor-position (&optional detail) |
486 | "Print info on cursor position (on screen and within buffer). | |
e5a902cf KH |
487 | With prefix argument, print detailed info of a character on cursor position. |
488 | ||
489 | For the detailed information, Emacs internal character code, Emacs | |
490 | internal character components (the character set name and position | |
491 | code(s)), and the corresponding external character components (the | |
492 | external character set name and external character code(s)) are shown | |
493 | in this order. | |
494 | ||
495 | Each language environment may show different external character components." | |
d5d99b80 | 496 | (interactive "P") |
2076c87c JB |
497 | (let* ((char (following-char)) |
498 | (beg (point-min)) | |
499 | (end (point-max)) | |
500 | (pos (point)) | |
501 | (total (buffer-size)) | |
502 | (percent (if (> total 50000) | |
503 | ;; Avoid overflow from multiplying by 100! | |
504 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
505 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
506 | (hscroll (if (= (window-hscroll) 0) | |
507 | "" | |
508 | (format " Hscroll=%d" (window-hscroll)))) | |
509 | (col (current-column))) | |
510 | (if (= pos end) | |
511 | (if (or (/= beg 1) (/= end (1+ total))) | |
512 | (message "point=%d of %d(%d%%) <%d - %d> column %d %s" | |
513 | pos total percent beg end col hscroll) | |
514 | (message "point=%d of %d(%d%%) column %d %s" | |
515 | pos total percent col hscroll)) | |
e5a902cf KH |
516 | (if detail |
517 | (let* ((internal (split-char char)) | |
518 | (charset (char-charset char)) | |
519 | (slot (assq charset charset-origin-alist)) | |
520 | external) | |
521 | (if slot | |
522 | (setq external (list (nth 1 slot) (funcall (nth 2 slot) char))) | |
523 | (if (eq charset 'composition) | |
524 | (setq internal '("composite-character")) | |
525 | (setq external (cons (charset-short-name charset) | |
526 | (copy-sequence (cdr internal)))) | |
527 | (if (= (charset-iso-graphic-plane charset) 1) | |
528 | (progn | |
529 | (setcar (cdr external) (+ (nth 1 external) 128)) | |
530 | (if (nth 2 external) | |
531 | (setcar (nthcdr 2 external) | |
532 | (+ (nth 2 external) 128))))))) | |
533 | (message "Char: %s (0%o, %d, 0x%x) %s %s" | |
534 | (if (< char 256) | |
535 | (single-key-description char) | |
536 | (char-to-string char)) | |
537 | char char char (or internal "") (or external ""))) | |
d5d99b80 | 538 | (if (or (/= beg 1) (/= end (1+ total))) |
e5a902cf | 539 | (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) <%d - %d> column %d %s" |
d5d99b80 KH |
540 | (if (< char 256) |
541 | (single-key-description char) | |
542 | (char-to-string char)) | |
e5a902cf KH |
543 | char char char pos total percent beg end col hscroll) |
544 | (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) column %d %s" | |
d5d99b80 KH |
545 | (if (< char 256) |
546 | (single-key-description char) | |
547 | (char-to-string char)) | |
e5a902cf | 548 | char char char pos total percent col hscroll)))))) |
2076c87c JB |
549 | |
550 | (defun fundamental-mode () | |
551 | "Major mode not specialized for anything in particular. | |
552 | Other major modes are defined by comparison with this one." | |
553 | (interactive) | |
554 | (kill-all-local-variables)) | |
555 | ||
4578d35d | 556 | (defvar read-expression-map (cons 'keymap minibuffer-local-map) |
854c16c5 RS |
557 | "Minibuffer keymap used for reading Lisp expressions.") |
558 | (define-key read-expression-map "\M-\t" 'lisp-complete-symbol) | |
559 | ||
8570b0ca RM |
560 | (defvar read-expression-history nil) |
561 | ||
562 | ;; We define this, rather than making `eval' interactive, | |
2076c87c | 563 | ;; for the sake of completion of names like eval-region, eval-current-buffer. |
ecb7ad00 RS |
564 | (defun eval-expression (eval-expression-arg |
565 | &optional eval-expression-insert-value) | |
2076c87c | 566 | "Evaluate EXPRESSION and print value in minibuffer. |
eb57c304 | 567 | Value is also consed on to front of the variable `values'." |
adca5fa6 | 568 | (interactive |
b387ef9a RS |
569 | (list (read-from-minibuffer "Eval: " |
570 | nil read-expression-map t | |
ecb7ad00 RS |
571 | 'read-expression-history) |
572 | current-prefix-arg)) | |
bc43b8e8 | 573 | (setq values (cons (eval eval-expression-arg) values)) |
ecb7ad00 RS |
574 | (prin1 (car values) |
575 | (if eval-expression-insert-value (current-buffer) t))) | |
2076c87c JB |
576 | |
577 | (defun edit-and-eval-command (prompt command) | |
578 | "Prompting with PROMPT, let user edit COMMAND and eval result. | |
579 | COMMAND is a Lisp expression. Let user edit that expression in | |
580 | the minibuffer, then read and evaluate the result." | |
b387ef9a RS |
581 | (let ((command (read-from-minibuffer prompt |
582 | (prin1-to-string command) | |
583 | read-expression-map t | |
584 | '(command-history . 1)))) | |
5d6c83ae | 585 | ;; If command was added to command-history as a string, |
1f238ac2 | 586 | ;; get rid of that. We want only evaluable expressions there. |
5d6c83ae KH |
587 | (if (stringp (car command-history)) |
588 | (setq command-history (cdr command-history))) | |
589 | ||
590 | ;; If command to be redone does not match front of history, | |
591 | ;; add it to the history. | |
592 | (or (equal command (car command-history)) | |
593 | (setq command-history (cons command command-history))) | |
2076c87c JB |
594 | (eval command))) |
595 | ||
ebb61177 | 596 | (defun repeat-complex-command (arg) |
2076c87c JB |
597 | "Edit and re-evaluate last complex command, or ARGth from last. |
598 | A complex command is one which used the minibuffer. | |
599 | The command is placed in the minibuffer as a Lisp form for editing. | |
600 | The result is executed, repeating the command as changed. | |
601 | If the command has been changed or is not the most recent previous command | |
602 | it is added to the front of the command history. | |
eb6e9899 RS |
603 | You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] |
604 | to get different commands to edit and resubmit." | |
2076c87c | 605 | (interactive "p") |
ba343182 | 606 | (let ((elt (nth (1- arg) command-history)) |
2076c87c JB |
607 | newcmd) |
608 | (if elt | |
854c16c5 | 609 | (progn |
eab22e27 | 610 | (setq newcmd |
74ae5fab RS |
611 | (let ((print-level nil) |
612 | (minibuffer-history-position arg) | |
99ea24de | 613 | (minibuffer-history-sexp-flag (1+ (minibuffer-depth)))) |
7908d27c RS |
614 | (read-from-minibuffer |
615 | "Redo: " (prin1-to-string elt) read-expression-map t | |
616 | (cons 'command-history arg)))) | |
eab22e27 | 617 | |
db16f109 | 618 | ;; If command was added to command-history as a string, |
1f238ac2 | 619 | ;; get rid of that. We want only evaluable expressions there. |
db16f109 RS |
620 | (if (stringp (car command-history)) |
621 | (setq command-history (cdr command-history))) | |
622 | ||
623 | ;; If command to be redone does not match front of history, | |
624 | ;; add it to the history. | |
625 | (or (equal newcmd (car command-history)) | |
626 | (setq command-history (cons newcmd command-history))) | |
2076c87c JB |
627 | (eval newcmd)) |
628 | (ding)))) | |
e91f80c4 | 629 | \f |
854c16c5 RS |
630 | (defvar minibuffer-history nil |
631 | "Default minibuffer history list. | |
632 | This is used for all minibuffer input | |
633 | except when an alternate history list is specified.") | |
634 | (defvar minibuffer-history-sexp-flag nil | |
c2d4b6d9 | 635 | "Non-nil when doing history operations on `command-history'. |
854c16c5 | 636 | More generally, indicates that the history list being acted on |
99ea24de KH |
637 | contains expressions rather than strings. |
638 | It is only valid if its value equals the current minibuffer depth, | |
639 | to handle recursive uses of the minibuffer.") | |
e91f80c4 RS |
640 | (setq minibuffer-history-variable 'minibuffer-history) |
641 | (setq minibuffer-history-position nil) | |
854c16c5 | 642 | (defvar minibuffer-history-search-history nil) |
e91f80c4 | 643 | |
29929437 | 644 | (mapcar |
d0678801 RM |
645 | (lambda (key-and-command) |
646 | (mapcar | |
647 | (lambda (keymap-and-completionp) | |
648 | ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P). | |
649 | ;; If the cdr of KEY-AND-COMMAND (the command) is a cons, | |
650 | ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t. | |
651 | (define-key (symbol-value (car keymap-and-completionp)) | |
652 | (car key-and-command) | |
653 | (let ((command (cdr key-and-command))) | |
654 | (if (consp command) | |
b5e6f936 RM |
655 | ;; (and ... nil) => ... turns back on the completion-oriented |
656 | ;; history commands which rms turned off since they seem to | |
657 | ;; do things he doesn't like. | |
658 | (if (and (cdr keymap-and-completionp) nil) ;XXX turned off | |
d81362b0 | 659 | (progn (error "EMACS BUG!") (cdr command)) |
d0678801 RM |
660 | (car command)) |
661 | command)))) | |
662 | '((minibuffer-local-map . nil) | |
663 | (minibuffer-local-ns-map . nil) | |
664 | (minibuffer-local-completion-map . t) | |
665 | (minibuffer-local-must-match-map . t) | |
666 | (read-expression-map . nil)))) | |
d81362b0 RM |
667 | '(("\en" . (next-history-element . next-complete-history-element)) |
668 | ([next] . (next-history-element . next-complete-history-element)) | |
669 | ("\ep" . (previous-history-element . previous-complete-history-element)) | |
670 | ([prior] . (previous-history-element . previous-complete-history-element)) | |
29929437 JB |
671 | ("\er" . previous-matching-history-element) |
672 | ("\es" . next-matching-history-element))) | |
e91f80c4 | 673 | |
93cee14b RS |
674 | (defvar minibuffer-text-before-history nil |
675 | "Text that was in this minibuffer before any history commands. | |
676 | This is nil if there have not yet been any history commands | |
677 | in this use of the minibuffer.") | |
678 | ||
679 | (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize) | |
680 | ||
681 | (defun minibuffer-history-initialize () | |
682 | (setq minibuffer-text-before-history nil)) | |
683 | ||
6e30a99a RS |
684 | (defcustom minibuffer-history-case-insensitive-variables nil |
685 | "*Minibuffer history variables for which matching should ignore case. | |
686 | If a history variable is a member of this list, then the | |
687 | \\[previous-matching-history-element] and \\[next-matching-history-element]\ | |
688 | commands ignore case when searching it, regardless of `case-fold-search'." | |
689 | :type '(repeat variable) | |
690 | :group 'minibuffer) | |
691 | ||
e91f80c4 | 692 | (defun previous-matching-history-element (regexp n) |
854c16c5 RS |
693 | "Find the previous history element that matches REGEXP. |
694 | \(Previous history elements refer to earlier actions.) | |
695 | With prefix argument N, search for Nth previous match. | |
5c2010f0 | 696 | If N is negative, find the next or Nth next match. |
6e30a99a RS |
697 | An uppercase letter in REGEXP makes the search case-sensitive. |
698 | See also `minibuffer-history-case-insensitive-variables'." | |
854c16c5 | 699 | (interactive |
c1172a19 | 700 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
701 | (regexp (read-from-minibuffer "Previous element matching (regexp): " |
702 | nil | |
703 | minibuffer-local-map | |
704 | nil | |
705 | 'minibuffer-history-search-history))) | |
706 | ;; Use the last regexp specified, by default, if input is empty. | |
707 | (list (if (string= regexp "") | |
a8e96cea KH |
708 | (if minibuffer-history-search-history |
709 | (car minibuffer-history-search-history) | |
710 | (error "No previous history search regexp")) | |
c1172a19 | 711 | regexp) |
854c16c5 | 712 | (prefix-numeric-value current-prefix-arg)))) |
93cee14b RS |
713 | (if (and (zerop minibuffer-history-position) |
714 | (null minibuffer-text-before-history)) | |
715 | (setq minibuffer-text-before-history (buffer-string))) | |
e91f80c4 | 716 | (let ((history (symbol-value minibuffer-history-variable)) |
5c2010f0 RS |
717 | (case-fold-search |
718 | (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped | |
6e30a99a RS |
719 | ;; On some systems, ignore case for file names. |
720 | (if (memq minibuffer-history-variable | |
721 | minibuffer-history-case-insensitive-variables) | |
722 | t | |
723 | ;; Respect the user's setting for case-fold-search: | |
724 | case-fold-search) | |
5c2010f0 | 725 | nil)) |
ccc58657 | 726 | prevpos |
e91f80c4 RS |
727 | (pos minibuffer-history-position)) |
728 | (while (/= n 0) | |
729 | (setq prevpos pos) | |
730 | (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) | |
731 | (if (= pos prevpos) | |
732 | (error (if (= pos 1) | |
ccc58657 RS |
733 | "No later matching history item" |
734 | "No earlier matching history item"))) | |
e91f80c4 | 735 | (if (string-match regexp |
99ea24de KH |
736 | (if (eq minibuffer-history-sexp-flag |
737 | (minibuffer-depth)) | |
7908d27c RS |
738 | (let ((print-level nil)) |
739 | (prin1-to-string (nth (1- pos) history))) | |
e91f80c4 | 740 | (nth (1- pos) history))) |
854c16c5 | 741 | (setq n (+ n (if (< n 0) 1 -1))))) |
e91f80c4 RS |
742 | (setq minibuffer-history-position pos) |
743 | (erase-buffer) | |
744 | (let ((elt (nth (1- pos) history))) | |
99ea24de | 745 | (insert (if (eq minibuffer-history-sexp-flag (minibuffer-depth)) |
7908d27c RS |
746 | (let ((print-level nil)) |
747 | (prin1-to-string elt)) | |
e91f80c4 | 748 | elt))) |
854c16c5 RS |
749 | (goto-char (point-min))) |
750 | (if (or (eq (car (car command-history)) 'previous-matching-history-element) | |
751 | (eq (car (car command-history)) 'next-matching-history-element)) | |
752 | (setq command-history (cdr command-history)))) | |
e91f80c4 | 753 | |
e91f80c4 | 754 | (defun next-matching-history-element (regexp n) |
854c16c5 RS |
755 | "Find the next history element that matches REGEXP. |
756 | \(The next history element refers to a more recent action.) | |
757 | With prefix argument N, search for Nth next match. | |
5c2010f0 RS |
758 | If N is negative, find the previous or Nth previous match. |
759 | An uppercase letter in REGEXP makes the search case-sensitive." | |
854c16c5 | 760 | (interactive |
c1172a19 | 761 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
762 | (regexp (read-from-minibuffer "Next element matching (regexp): " |
763 | nil | |
764 | minibuffer-local-map | |
765 | nil | |
766 | 'minibuffer-history-search-history))) | |
767 | ;; Use the last regexp specified, by default, if input is empty. | |
768 | (list (if (string= regexp "") | |
769 | (setcar minibuffer-history-search-history | |
770 | (nth 1 minibuffer-history-search-history)) | |
771 | regexp) | |
854c16c5 | 772 | (prefix-numeric-value current-prefix-arg)))) |
e91f80c4 | 773 | (previous-matching-history-element regexp (- n))) |
2076c87c | 774 | |
ebb61177 RS |
775 | (defun next-history-element (n) |
776 | "Insert the next element of the minibuffer history into the minibuffer." | |
2076c87c | 777 | (interactive "p") |
0818b15e | 778 | (or (zerop n) |
93cee14b RS |
779 | (let ((narg (- minibuffer-history-position n)) |
780 | (minimum (if minibuffer-default -1 0)) | |
781 | elt) | |
782 | (if (and (zerop minibuffer-history-position) | |
783 | (null minibuffer-text-before-history)) | |
784 | (setq minibuffer-text-before-history (buffer-string))) | |
785 | (if (< narg minimum) | |
81091543 RS |
786 | (if minibuffer-default |
787 | (error "End of history; no next item") | |
788 | (error "End of history; no default available"))) | |
93cee14b RS |
789 | (if (> narg (length (symbol-value minibuffer-history-variable))) |
790 | (error "Beginning of history; no preceding item")) | |
791 | (erase-buffer) | |
792 | (setq minibuffer-history-position narg) | |
793 | (cond ((= narg -1) | |
794 | (setq elt minibuffer-default)) | |
795 | ((= narg 0) | |
54c548db | 796 | (setq elt (or minibuffer-text-before-history "")) |
93cee14b RS |
797 | (setq minibuffer-text-before-history nil)) |
798 | (t (setq elt (nth (1- minibuffer-history-position) | |
799 | (symbol-value minibuffer-history-variable))))) | |
800 | (insert | |
99ea24de | 801 | (if (eq minibuffer-history-sexp-flag (minibuffer-depth)) |
93cee14b RS |
802 | (let ((print-level nil)) |
803 | (prin1-to-string elt)) | |
804 | elt)) | |
805 | (goto-char (point-min))))) | |
2076c87c | 806 | |
ebb61177 | 807 | (defun previous-history-element (n) |
3ee3a076 | 808 | "Inserts the previous element of the minibuffer history into the minibuffer." |
2076c87c | 809 | (interactive "p") |
2c5e21c1 | 810 | (next-history-element (- n))) |
d0678801 RM |
811 | |
812 | (defun next-complete-history-element (n) | |
1f6fcec3 | 813 | "Get next element of history which is a completion of minibuffer contents." |
d0678801 | 814 | (interactive "p") |
b5e6f936 RM |
815 | (let ((point-at-start (point))) |
816 | (next-matching-history-element | |
817 | (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n) | |
818 | ;; next-matching-history-element always puts us at (point-min). | |
819 | ;; Move to the position we were at before changing the buffer contents. | |
820 | ;; This is still sensical, because the text before point has not changed. | |
821 | (goto-char point-at-start))) | |
d0678801 RM |
822 | |
823 | (defun previous-complete-history-element (n) | |
1f6fcec3 RS |
824 | "\ |
825 | Get previous element of history which is a completion of minibuffer contents." | |
d0678801 RM |
826 | (interactive "p") |
827 | (next-complete-history-element (- n))) | |
e91f80c4 | 828 | \f |
2076c87c JB |
829 | (defun goto-line (arg) |
830 | "Goto line ARG, counting from line 1 at beginning of buffer." | |
831 | (interactive "NGoto line: ") | |
5f1a943c | 832 | (setq arg (prefix-numeric-value arg)) |
2076c87c JB |
833 | (save-restriction |
834 | (widen) | |
835 | (goto-char 1) | |
836 | (if (eq selective-display t) | |
837 | (re-search-forward "[\n\C-m]" nil 'end (1- arg)) | |
838 | (forward-line (1- arg))))) | |
839 | ||
840 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg | |
e462e42f | 841 | (defalias 'advertised-undo 'undo) |
2076c87c JB |
842 | |
843 | (defun undo (&optional arg) | |
844 | "Undo some previous changes. | |
845 | Repeat this command to undo more changes. | |
65627aad RS |
846 | A numeric argument serves as a repeat count. |
847 | ||
848 | Just C-u as argument requests selective undo, | |
849 | limited to changes within the current region. | |
850 | Likewise in Transient Mark mode when the mark is active." | |
851 | (interactive "*P") | |
456c617c RS |
852 | ;; If we don't get all the way thru, make last-command indicate that |
853 | ;; for the following command. | |
854 | (setq this-command t) | |
b553cffa RS |
855 | (let ((modified (buffer-modified-p)) |
856 | (recent-save (recent-auto-save-p))) | |
71e40adf JB |
857 | (or (eq (selected-window) (minibuffer-window)) |
858 | (message "Undo!")) | |
2076c87c | 859 | (or (eq last-command 'undo) |
65627aad RS |
860 | (progn (if (or arg (and transient-mark-mode mark-active)) |
861 | (undo-start (region-beginning) (region-end)) | |
862 | (undo-start)) | |
2076c87c | 863 | (undo-more 1))) |
65627aad | 864 | (undo-more (if arg (prefix-numeric-value arg) 1)) |
2512c9f0 RS |
865 | ;; Don't specify a position in the undo record for the undo command. |
866 | ;; Instead, undoing this should move point to where the change is. | |
867 | (let ((tail buffer-undo-list) | |
868 | done) | |
869 | (while (and tail (not done) (not (null (car tail)))) | |
870 | (if (integerp (car tail)) | |
871 | (progn | |
872 | (setq done t) | |
873 | (setq buffer-undo-list (delq (car tail) buffer-undo-list)))) | |
874 | (setq tail (cdr tail)))) | |
2076c87c | 875 | (and modified (not (buffer-modified-p)) |
456c617c RS |
876 | (delete-auto-save-file-if-necessary recent-save))) |
877 | ;; If we do get all the way thru, make this-command indicate that. | |
878 | (setq this-command 'undo)) | |
2076c87c | 879 | |
278b0a58 RS |
880 | (defvar pending-undo-list nil |
881 | "Within a run of consecutive undo commands, list remaining to be undone.") | |
882 | ||
52d1110d RS |
883 | (defvar undo-in-progress nil |
884 | "Non-nil while performing an undo. | |
885 | Some change-hooks test this variable to do something different.") | |
886 | ||
2076c87c JB |
887 | (defun undo-more (count) |
888 | "Undo back N undo-boundaries beyond what was already undone recently. | |
ff1fbe3e RS |
889 | Call `undo-start' to get ready to undo recent changes, |
890 | then call `undo-more' one or more times to undo them." | |
2076c87c JB |
891 | (or pending-undo-list |
892 | (error "No further undo information")) | |
52d1110d RS |
893 | (let ((undo-in-progress t)) |
894 | (setq pending-undo-list (primitive-undo count pending-undo-list)))) | |
2076c87c | 895 | |
65627aad RS |
896 | ;; Deep copy of a list |
897 | (defun undo-copy-list (list) | |
898 | "Make a copy of undo list LIST." | |
899 | (mapcar 'undo-copy-list-1 list)) | |
900 | ||
901 | (defun undo-copy-list-1 (elt) | |
902 | (if (consp elt) | |
903 | (cons (car elt) (undo-copy-list-1 (cdr elt))) | |
904 | elt)) | |
905 | ||
906 | (defun undo-start (&optional beg end) | |
907 | "Set `pending-undo-list' to the front of the undo list. | |
908 | The next call to `undo-more' will undo the most recently made change. | |
909 | If BEG and END are specified, then only undo elements | |
910 | that apply to text between BEG and END are used; other undo elements | |
911 | are ignored. If BEG and END are nil, all undo elements are used." | |
912 | (if (eq buffer-undo-list t) | |
913 | (error "No undo information in this buffer")) | |
914 | (setq pending-undo-list | |
915 | (if (and beg end (not (= beg end))) | |
916 | (undo-make-selective-list (min beg end) (max beg end)) | |
917 | buffer-undo-list))) | |
918 | ||
919 | (defvar undo-adjusted-markers) | |
920 | ||
921 | (defun undo-make-selective-list (start end) | |
922 | "Return a list of undo elements for the region START to END. | |
923 | The elements come from `buffer-undo-list', but we keep only | |
924 | the elements inside this region, and discard those outside this region. | |
925 | If we find an element that crosses an edge of this region, | |
926 | we stop and ignore all further elements." | |
927 | (let ((undo-list-copy (undo-copy-list buffer-undo-list)) | |
928 | (undo-list (list nil)) | |
929 | undo-adjusted-markers | |
930 | some-rejected | |
931 | undo-elt undo-elt temp-undo-list delta) | |
932 | (while undo-list-copy | |
933 | (setq undo-elt (car undo-list-copy)) | |
934 | (let ((keep-this | |
935 | (cond ((and (consp undo-elt) (eq (car undo-elt) t)) | |
936 | ;; This is a "was unmodified" element. | |
937 | ;; Keep it if we have kept everything thus far. | |
938 | (not some-rejected)) | |
939 | (t | |
940 | (undo-elt-in-region undo-elt start end))))) | |
941 | (if keep-this | |
942 | (progn | |
943 | (setq end (+ end (cdr (undo-delta undo-elt)))) | |
944 | ;; Don't put two nils together in the list | |
945 | (if (not (and (eq (car undo-list) nil) | |
946 | (eq undo-elt nil))) | |
947 | (setq undo-list (cons undo-elt undo-list)))) | |
948 | (if (undo-elt-crosses-region undo-elt start end) | |
949 | (setq undo-list-copy nil) | |
950 | (setq some-rejected t) | |
951 | (setq temp-undo-list (cdr undo-list-copy)) | |
952 | (setq delta (undo-delta undo-elt)) | |
953 | ||
954 | (when (/= (cdr delta) 0) | |
955 | (let ((position (car delta)) | |
956 | (offset (cdr delta))) | |
957 | ||
958 | ;; Loop down the earlier events adjusting their buffer positions | |
959 | ;; to reflect the fact that a change to the buffer isn't being | |
960 | ;; undone. We only need to process those element types which | |
961 | ;; undo-elt-in-region will return as being in the region since | |
962 | ;; only those types can ever get into the output | |
963 | ||
964 | (while temp-undo-list | |
965 | (setq undo-elt (car temp-undo-list)) | |
966 | (cond ((integerp undo-elt) | |
967 | (if (>= undo-elt position) | |
968 | (setcar temp-undo-list (- undo-elt offset)))) | |
969 | ((atom undo-elt) nil) | |
970 | ((stringp (car undo-elt)) | |
971 | ;; (TEXT . POSITION) | |
972 | (let ((text-pos (abs (cdr undo-elt))) | |
973 | (point-at-end (< (cdr undo-elt) 0 ))) | |
974 | (if (>= text-pos position) | |
975 | (setcdr undo-elt (* (if point-at-end -1 1) | |
976 | (- text-pos offset)))))) | |
977 | ((integerp (car undo-elt)) | |
978 | ;; (BEGIN . END) | |
979 | (when (>= (car undo-elt) position) | |
980 | (setcar undo-elt (- (car undo-elt) offset)) | |
981 | (setcdr undo-elt (- (cdr undo-elt) offset)))) | |
982 | ((null (car undo-elt)) | |
983 | ;; (nil PROPERTY VALUE BEG . END) | |
984 | (let ((tail (nthcdr 3 undo-elt))) | |
985 | (when (>= (car tail) position) | |
986 | (setcar tail (- (car tail) offset)) | |
987 | (setcdr tail (- (cdr tail) offset)))))) | |
988 | (setq temp-undo-list (cdr temp-undo-list)))))))) | |
989 | (setq undo-list-copy (cdr undo-list-copy))) | |
990 | (nreverse undo-list))) | |
991 | ||
992 | (defun undo-elt-in-region (undo-elt start end) | |
993 | "Determine whether UNDO-ELT falls inside the region START ... END. | |
994 | If it crosses the edge, we return nil." | |
995 | (cond ((integerp undo-elt) | |
996 | (and (>= undo-elt start) | |
997 | (< undo-elt end))) | |
998 | ((eq undo-elt nil) | |
999 | t) | |
1000 | ((atom undo-elt) | |
1001 | nil) | |
1002 | ((stringp (car undo-elt)) | |
1003 | ;; (TEXT . POSITION) | |
1004 | (and (>= (abs (cdr undo-elt)) start) | |
1005 | (< (abs (cdr undo-elt)) end))) | |
1006 | ((and (consp undo-elt) (markerp (car undo-elt))) | |
1007 | ;; This is a marker-adjustment element (MARKER . ADJUSTMENT). | |
1008 | ;; See if MARKER is inside the region. | |
1009 | (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers))) | |
1010 | (unless alist-elt | |
1011 | (setq alist-elt (cons (car undo-elt) | |
1012 | (marker-position (car undo-elt)))) | |
1013 | (setq undo-adjusted-markers | |
1014 | (cons alist-elt undo-adjusted-markers))) | |
1015 | (and (cdr alist-elt) | |
1016 | (>= (cdr alist-elt) start) | |
1017 | (< (cdr alist-elt) end)))) | |
1018 | ((null (car undo-elt)) | |
1019 | ;; (nil PROPERTY VALUE BEG . END) | |
1020 | (let ((tail (nthcdr 3 undo-elt))) | |
1021 | (and (>= (car tail) start) | |
1022 | (< (cdr tail) end)))) | |
1023 | ((integerp (car undo-elt)) | |
1024 | ;; (BEGIN . END) | |
1025 | (and (>= (car undo-elt) start) | |
1026 | (< (cdr undo-elt) end))))) | |
1027 | ||
1028 | (defun undo-elt-crosses-region (undo-elt start end) | |
1029 | "Test whether UNDO-ELT crosses one edge of that region START ... END. | |
1030 | This assumes we have already decided that UNDO-ELT | |
1031 | is not *inside* the region START...END." | |
1032 | (cond ((atom undo-elt) nil) | |
1033 | ((null (car undo-elt)) | |
1034 | ;; (nil PROPERTY VALUE BEG . END) | |
1035 | (let ((tail (nthcdr 3 undo-elt))) | |
1036 | (not (or (< (car tail) end) | |
1037 | (> (cdr tail) start))))) | |
1038 | ((integerp (car undo-elt)) | |
1039 | ;; (BEGIN . END) | |
1040 | (not (or (< (car undo-elt) end) | |
1041 | (> (cdr undo-elt) start)))))) | |
1042 | ||
1043 | ;; Return the first affected buffer position and the delta for an undo element | |
1044 | ;; delta is defined as the change in subsequent buffer positions if we *did* | |
1045 | ;; the undo. | |
1046 | (defun undo-delta (undo-elt) | |
1047 | (if (consp undo-elt) | |
1048 | (cond ((stringp (car undo-elt)) | |
1049 | ;; (TEXT . POSITION) | |
1050 | (cons (abs (cdr undo-elt)) (length (car undo-elt)))) | |
1051 | ((integerp (car undo-elt)) | |
1052 | ;; (BEGIN . END) | |
1053 | (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt)))) | |
1054 | (t | |
1055 | '(0 . 0))) | |
1056 | '(0 . 0))) | |
1057 | \f | |
009ef402 RS |
1058 | (defvar shell-command-history nil |
1059 | "History list for some commands that read shell commands.") | |
1060 | ||
59fc41e5 RS |
1061 | (defvar shell-command-switch "-c" |
1062 | "Switch used to have the shell execute its command line argument.") | |
1063 | ||
d0d74413 | 1064 | (defun shell-command (command &optional output-buffer) |
2076c87c | 1065 | "Execute string COMMAND in inferior shell; display output, if any. |
d382f610 | 1066 | |
2076c87c | 1067 | If COMMAND ends in ampersand, execute it asynchronously. |
d382f610 | 1068 | The output appears in the buffer `*Async Shell Command*'. |
bcad4985 | 1069 | That buffer is in shell mode. |
d382f610 | 1070 | |
bcad4985 KH |
1071 | Otherwise, COMMAND is executed synchronously. The output appears in the |
1072 | buffer `*Shell Command Output*'. | |
d382f610 RS |
1073 | If the output is one line, it is displayed in the echo area *as well*, |
1074 | but it is nonetheless available in buffer `*Shell Command Output*', | |
1075 | even though that buffer is not automatically displayed. | |
1076 | If there is no output, or if output is inserted in the current buffer, | |
1077 | then `*Shell Command Output*' is deleted. | |
d0d74413 | 1078 | |
07f458c1 RS |
1079 | To specify a coding system for converting non-ASCII characters |
1080 | in the shell command output, use \\[universal-coding-system-argument] | |
1081 | before this command. | |
1082 | ||
1083 | Noninteractive callers can specify coding systems by binding | |
1084 | `coding-system-for-read' and `coding-system-for-write'. | |
1085 | ||
d0d74413 RS |
1086 | The optional second argument OUTPUT-BUFFER, if non-nil, |
1087 | says to put the output in some other buffer. | |
1088 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
1089 | If OUTPUT-BUFFER is not a buffer and not nil, | |
1090 | insert output in current buffer. (This cannot be done asynchronously.) | |
1091 | In either case, the output is inserted after point (leaving mark after it)." | |
aa00b92d RS |
1092 | (interactive (list (read-from-minibuffer "Shell command: " |
1093 | nil nil nil 'shell-command-history) | |
1094 | current-prefix-arg)) | |
c7edd03c KH |
1095 | ;; Look for a handler in case default-directory is a remote file name. |
1096 | (let ((handler | |
1097 | (find-file-name-handler (directory-file-name default-directory) | |
1098 | 'shell-command))) | |
1099 | (if handler | |
1100 | (funcall handler 'shell-command command output-buffer) | |
1101 | (if (and output-buffer | |
1102 | (not (or (bufferp output-buffer) (stringp output-buffer)))) | |
1103 | (progn (barf-if-buffer-read-only) | |
1104 | (push-mark) | |
1105 | ;; We do not use -f for csh; we will not support broken use of | |
1106 | ;; .cshrcs. Even the BSD csh manual says to use | |
1107 | ;; "if ($?prompt) exit" before things which are not useful | |
1108 | ;; non-interactively. Besides, if someone wants their other | |
1109 | ;; aliases for shell commands then they can still have them. | |
1110 | (call-process shell-file-name nil t nil | |
1111 | shell-command-switch command) | |
1112 | ;; This is like exchange-point-and-mark, but doesn't | |
1113 | ;; activate the mark. It is cleaner to avoid activation, | |
1114 | ;; even though the command loop would deactivate the mark | |
1115 | ;; because we inserted text. | |
1116 | (goto-char (prog1 (mark t) | |
1117 | (set-marker (mark-marker) (point) | |
1118 | (current-buffer))))) | |
1119 | ;; Preserve the match data in case called from a program. | |
1120 | (save-match-data | |
1121 | (if (string-match "[ \t]*&[ \t]*$" command) | |
1122 | ;; Command ending with ampersand means asynchronous. | |
1123 | (let ((buffer (get-buffer-create | |
1124 | (or output-buffer "*Async Shell Command*"))) | |
1125 | (directory default-directory) | |
1126 | proc) | |
1127 | ;; Remove the ampersand. | |
1128 | (setq command (substring command 0 (match-beginning 0))) | |
1129 | ;; If will kill a process, query first. | |
1130 | (setq proc (get-buffer-process buffer)) | |
1131 | (if proc | |
1132 | (if (yes-or-no-p "A command is running. Kill it? ") | |
1133 | (kill-process proc) | |
1134 | (error "Shell command in progress"))) | |
1135 | (save-excursion | |
1136 | (set-buffer buffer) | |
1137 | (setq buffer-read-only nil) | |
1138 | (erase-buffer) | |
1139 | (display-buffer buffer) | |
1140 | (setq default-directory directory) | |
1141 | (setq proc (start-process "Shell" buffer shell-file-name | |
1142 | shell-command-switch command)) | |
1143 | (setq mode-line-process '(":%s")) | |
1144 | (require 'shell) (shell-mode) | |
1145 | (set-process-sentinel proc 'shell-command-sentinel) | |
1146 | )) | |
f2b3cdc3 | 1147 | (shell-command-on-region (point) (point) command output-buffer) |
c7edd03c | 1148 | )))))) |
65627aad | 1149 | \f |
2076c87c JB |
1150 | ;; We have a sentinel to prevent insertion of a termination message |
1151 | ;; in the buffer itself. | |
1152 | (defun shell-command-sentinel (process signal) | |
bcad4985 KH |
1153 | (if (memq (process-status process) '(exit signal)) |
1154 | (message "%s: %s." | |
1155 | (car (cdr (cdr (process-command process)))) | |
1156 | (substring signal 0 -1)))) | |
2076c87c | 1157 | |
d0d74413 | 1158 | (defun shell-command-on-region (start end command |
cce1c318 RS |
1159 | &optional output-buffer replace |
1160 | error-buffer) | |
2076c87c JB |
1161 | "Execute string COMMAND in inferior shell with region as input. |
1162 | Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
1163 | Prefix arg means replace the region with it. | |
56c0450e | 1164 | |
07f458c1 RS |
1165 | To specify a coding system for converting non-ASCII characters |
1166 | in the input and output to the shell command, use \\[universal-coding-system-argument] | |
1167 | before this command. By default, the input (from the current buffer) | |
1168 | is encoded in the same coding system that will be used to save the file, | |
1169 | `buffer-file-coding-system'. If the output is going to replace the region, | |
1170 | then it is decoded from that same coding system. | |
1171 | ||
8923a211 RS |
1172 | The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, |
1173 | REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding | |
1174 | systems by binding `coding-system-for-read' and | |
1175 | `coding-system-for-write'. | |
2076c87c JB |
1176 | |
1177 | If the output is one line, it is displayed in the echo area, | |
1178 | but it is nonetheless available in buffer `*Shell Command Output*' | |
56c0450e | 1179 | even though that buffer is not automatically displayed. |
c42f586d | 1180 | If there is no output, or if output is inserted in the current buffer, |
56c0450e | 1181 | then `*Shell Command Output*' is deleted. |
d0d74413 | 1182 | |
56c0450e RS |
1183 | If the optional fourth argument OUTPUT-BUFFER is non-nil, |
1184 | that says to put the output in some other buffer. | |
d0d74413 RS |
1185 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. |
1186 | If OUTPUT-BUFFER is not a buffer and not nil, | |
1187 | insert output in the current buffer. | |
cce1c318 RS |
1188 | In either case, the output is inserted after point (leaving mark after it). |
1189 | ||
8923a211 RS |
1190 | If REPLACE, the optional fifth argument, is non-nil, that means insert |
1191 | the output in place of text from START to END, putting point and mark | |
1192 | around it. | |
1193 | ||
1194 | If optional fifth sixth ERROR-BUFFER is non-nil, it is a buffer | |
cce1c318 RS |
1195 | or buffer name to which to direct the command's standard error output. |
1196 | If it is nil, error output is mingled with regular output." | |
cae49185 RS |
1197 | (interactive (let ((string |
1198 | ;; Do this before calling region-beginning | |
1199 | ;; and region-end, in case subprocess output | |
1200 | ;; relocates them while we are in the minibuffer. | |
1201 | (read-from-minibuffer "Shell command on region: " | |
1202 | nil nil nil | |
1203 | 'shell-command-history))) | |
2b03c506 RS |
1204 | ;; call-interactively recognizes region-beginning and |
1205 | ;; region-end specially, leaving them in the history. | |
1206 | (list (region-beginning) (region-end) | |
cae49185 RS |
1207 | string |
1208 | current-prefix-arg | |
4d9bd664 | 1209 | current-prefix-arg))) |
cce1c318 RS |
1210 | (let ((error-file |
1211 | (if error-buffer | |
1212 | (concat (file-name-directory temp-file-name-pattern) | |
1213 | (make-temp-name "scor")) | |
1214 | nil))) | |
4d9bd664 RS |
1215 | (if (or replace |
1216 | (and output-buffer | |
29fc44dd KH |
1217 | (not (or (bufferp output-buffer) (stringp output-buffer)))) |
1218 | (equal (buffer-name (current-buffer)) "*Shell Command Output*")) | |
2076c87c | 1219 | ;; Replace specified region with output from command. |
6db3a963 | 1220 | (let ((swap (and replace (< start end)))) |
4d9bd664 RS |
1221 | ;; Don't muck with mark unless REPLACE says we should. |
1222 | (goto-char start) | |
56c0450e | 1223 | (and replace (push-mark)) |
cce1c318 RS |
1224 | (call-process-region start end shell-file-name t |
1225 | (if error-file | |
1226 | (list t error-file) | |
1227 | t) | |
1228 | nil shell-command-switch command) | |
b5f7c943 KH |
1229 | (let ((shell-buffer (get-buffer "*Shell Command Output*"))) |
1230 | (and shell-buffer (not (eq shell-buffer (current-buffer))) | |
1231 | (kill-buffer shell-buffer))) | |
4d9bd664 | 1232 | ;; Don't muck with mark unless REPLACE says we should. |
56c0450e | 1233 | (and replace swap (exchange-point-and-mark))) |
2076c87c JB |
1234 | ;; No prefix argument: put the output in a temp buffer, |
1235 | ;; replacing its entire contents. | |
d0d74413 RS |
1236 | (let ((buffer (get-buffer-create |
1237 | (or output-buffer "*Shell Command Output*"))) | |
5a8dd30e DL |
1238 | (success nil) |
1239 | (exit-status nil)) | |
34ee0963 RS |
1240 | (unwind-protect |
1241 | (if (eq buffer (current-buffer)) | |
1242 | ;; If the input is the same buffer as the output, | |
1243 | ;; delete everything but the specified region, | |
1244 | ;; then replace that region with the output. | |
a9594ce3 | 1245 | (progn (setq buffer-read-only nil) |
6db3a963 | 1246 | (delete-region (max start end) (point-max)) |
aa247686 | 1247 | (delete-region (point-min) (min start end)) |
5a8dd30e DL |
1248 | (setq exit-status |
1249 | (call-process-region (point-min) (point-max) | |
1250 | shell-file-name t | |
1251 | (if error-file | |
1252 | (list t error-file) | |
1253 | t) | |
1254 | nil shell-command-switch command)) | |
34ee0963 RS |
1255 | (setq success t)) |
1256 | ;; Clear the output buffer, then run the command with output there. | |
1257 | (save-excursion | |
1258 | (set-buffer buffer) | |
a9594ce3 | 1259 | (setq buffer-read-only nil) |
34ee0963 | 1260 | (erase-buffer)) |
5a8dd30e DL |
1261 | (setq exit-status |
1262 | (call-process-region start end shell-file-name nil | |
1263 | (if error-file | |
1264 | (list buffer error-file) | |
1265 | buffer) | |
1266 | nil shell-command-switch command)) | |
34ee0963 RS |
1267 | (setq success t)) |
1268 | ;; Report the amount of output. | |
1269 | (let ((lines (save-excursion | |
1270 | (set-buffer buffer) | |
1271 | (if (= (buffer-size) 0) | |
1272 | 0 | |
1273 | (count-lines (point-min) (point-max)))))) | |
1274 | (cond ((= lines 0) | |
1275 | (if success | |
5a8dd30e DL |
1276 | (message "(Shell command %sed with no output)" |
1277 | (if (equal 0 exit-status) | |
1278 | "succeed" | |
1279 | "fail"))) | |
34ee0963 RS |
1280 | (kill-buffer buffer)) |
1281 | ((and success (= lines 1)) | |
1282 | (message "%s" | |
1283 | (save-excursion | |
1284 | (set-buffer buffer) | |
1285 | (goto-char (point-min)) | |
1286 | (buffer-substring (point) | |
4ec982c5 | 1287 | (progn (end-of-line) (point)))))) |
34ee0963 | 1288 | (t |
1277e7a2 KH |
1289 | (save-excursion |
1290 | (set-buffer buffer) | |
1291 | (goto-char (point-min))) | |
cce1c318 RS |
1292 | (display-buffer buffer))))))) |
1293 | (if (and error-file (file-exists-p error-file)) | |
1294 | (save-excursion | |
1295 | (set-buffer (get-buffer-create error-buffer)) | |
65627aad | 1296 | ;; Do no formatting while reading error file, for fear of looping. |
cce1c318 RS |
1297 | (format-insert-file error-file nil) |
1298 | (delete-file error-file))))) | |
d589bd99 RS |
1299 | |
1300 | (defun shell-command-to-string (command) | |
1301 | "Execute shell command COMMAND and return its output as a string." | |
1302 | (with-output-to-string | |
17cc9013 RS |
1303 | (with-current-buffer |
1304 | standard-output | |
1305 | (call-process shell-file-name nil t nil shell-command-switch command)))) | |
2076c87c | 1306 | \f |
1b43f83f | 1307 | (defvar universal-argument-map |
69d4c3c4 KH |
1308 | (let ((map (make-sparse-keymap))) |
1309 | (define-key map [t] 'universal-argument-other-key) | |
b9ff190d | 1310 | (define-key map (vector meta-prefix-char t) 'universal-argument-other-key) |
69d4c3c4 KH |
1311 | (define-key map [switch-frame] nil) |
1312 | (define-key map [?\C-u] 'universal-argument-more) | |
1313 | (define-key map [?-] 'universal-argument-minus) | |
1314 | (define-key map [?0] 'digit-argument) | |
1315 | (define-key map [?1] 'digit-argument) | |
1316 | (define-key map [?2] 'digit-argument) | |
1317 | (define-key map [?3] 'digit-argument) | |
1318 | (define-key map [?4] 'digit-argument) | |
1319 | (define-key map [?5] 'digit-argument) | |
1320 | (define-key map [?6] 'digit-argument) | |
1321 | (define-key map [?7] 'digit-argument) | |
1322 | (define-key map [?8] 'digit-argument) | |
1323 | (define-key map [?9] 'digit-argument) | |
1324 | map) | |
1325 | "Keymap used while processing \\[universal-argument].") | |
1326 | ||
0de84e16 RS |
1327 | (defvar universal-argument-num-events nil |
1328 | "Number of argument-specifying events read by `universal-argument'. | |
1329 | `universal-argument-other-key' uses this to discard those events | |
1330 | from (this-command-keys), and reread only the final command.") | |
1331 | ||
e8d1a377 KH |
1332 | (defun universal-argument () |
1333 | "Begin a numeric argument for the following command. | |
1334 | Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
1335 | \\[universal-argument] following the digits or minus sign ends the argument. | |
1336 | \\[universal-argument] without digits or minus sign provides 4 as argument. | |
1337 | Repeating \\[universal-argument] without digits or minus sign | |
0565d307 RS |
1338 | multiplies the argument by 4 each time. |
1339 | For some commands, just \\[universal-argument] by itself serves as a flag | |
a697fc62 RS |
1340 | which is different in effect from any particular numeric argument. |
1341 | These commands include \\[set-mark-command] and \\[start-kbd-macro]." | |
69d4c3c4 KH |
1342 | (interactive) |
1343 | (setq prefix-arg (list 4)) | |
0de84e16 | 1344 | (setq universal-argument-num-events (length (this-command-keys))) |
69d4c3c4 | 1345 | (setq overriding-terminal-local-map universal-argument-map)) |
e8d1a377 | 1346 | |
69d4c3c4 KH |
1347 | ;; A subsequent C-u means to multiply the factor by 4 if we've typed |
1348 | ;; nothing but C-u's; otherwise it means to terminate the prefix arg. | |
1349 | (defun universal-argument-more (arg) | |
e8d1a377 | 1350 | (interactive "P") |
69d4c3c4 KH |
1351 | (if (consp arg) |
1352 | (setq prefix-arg (list (* 4 (car arg)))) | |
1cd24721 RS |
1353 | (if (eq arg '-) |
1354 | (setq prefix-arg (list -4)) | |
1355 | (setq prefix-arg arg) | |
1356 | (setq overriding-terminal-local-map nil))) | |
0de84e16 | 1357 | (setq universal-argument-num-events (length (this-command-keys)))) |
e8d1a377 KH |
1358 | |
1359 | (defun negative-argument (arg) | |
1360 | "Begin a negative numeric argument for the next command. | |
1361 | \\[universal-argument] following digits or minus sign ends the argument." | |
1362 | (interactive "P") | |
69d4c3c4 KH |
1363 | (cond ((integerp arg) |
1364 | (setq prefix-arg (- arg))) | |
1365 | ((eq arg '-) | |
1366 | (setq prefix-arg nil)) | |
1367 | (t | |
b9ff190d | 1368 | (setq prefix-arg '-))) |
0de84e16 | 1369 | (setq universal-argument-num-events (length (this-command-keys))) |
b9ff190d | 1370 | (setq overriding-terminal-local-map universal-argument-map)) |
69d4c3c4 KH |
1371 | |
1372 | (defun digit-argument (arg) | |
1373 | "Part of the numeric argument for the next command. | |
1374 | \\[universal-argument] following digits or minus sign ends the argument." | |
1375 | (interactive "P") | |
1376 | (let ((digit (- (logand last-command-char ?\177) ?0))) | |
1377 | (cond ((integerp arg) | |
1378 | (setq prefix-arg (+ (* arg 10) | |
1379 | (if (< arg 0) (- digit) digit)))) | |
1380 | ((eq arg '-) | |
1381 | ;; Treat -0 as just -, so that -01 will work. | |
1382 | (setq prefix-arg (if (zerop digit) '- (- digit)))) | |
1383 | (t | |
b9ff190d | 1384 | (setq prefix-arg digit)))) |
0de84e16 | 1385 | (setq universal-argument-num-events (length (this-command-keys))) |
b9ff190d | 1386 | (setq overriding-terminal-local-map universal-argument-map)) |
69d4c3c4 KH |
1387 | |
1388 | ;; For backward compatibility, minus with no modifiers is an ordinary | |
1389 | ;; command if digits have already been entered. | |
1390 | (defun universal-argument-minus (arg) | |
1391 | (interactive "P") | |
1392 | (if (integerp arg) | |
1393 | (universal-argument-other-key arg) | |
1394 | (negative-argument arg))) | |
1395 | ||
1396 | ;; Anything else terminates the argument and is left in the queue to be | |
1397 | ;; executed as a command. | |
1398 | (defun universal-argument-other-key (arg) | |
1399 | (interactive "P") | |
1400 | (setq prefix-arg arg) | |
0de84e16 RS |
1401 | (let* ((key (this-command-keys)) |
1402 | (keylist (listify-key-sequence key))) | |
1403 | (setq unread-command-events | |
06697cdb RS |
1404 | (append (nthcdr universal-argument-num-events keylist) |
1405 | unread-command-events))) | |
f0ef2555 | 1406 | (reset-this-command-lengths) |
69d4c3c4 | 1407 | (setq overriding-terminal-local-map nil)) |
e8d1a377 | 1408 | \f |
2076c87c JB |
1409 | (defun forward-to-indentation (arg) |
1410 | "Move forward ARG lines and position at first nonblank character." | |
1411 | (interactive "p") | |
1412 | (forward-line arg) | |
1413 | (skip-chars-forward " \t")) | |
1414 | ||
1415 | (defun backward-to-indentation (arg) | |
1416 | "Move backward ARG lines and position at first nonblank character." | |
1417 | (interactive "p") | |
1418 | (forward-line (- arg)) | |
1419 | (skip-chars-forward " \t")) | |
1420 | ||
69c1dd37 RS |
1421 | (defcustom kill-whole-line nil |
1422 | "*If non-nil, `kill-line' with no arg at beg of line kills the whole line." | |
1423 | :type 'boolean | |
1424 | :group 'killing) | |
38ebcf29 | 1425 | |
2076c87c | 1426 | (defun kill-line (&optional arg) |
dff7d67f | 1427 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. |
2076c87c JB |
1428 | With prefix argument, kill that many lines from point. |
1429 | Negative arguments kill lines backward. | |
1430 | ||
1431 | When calling from a program, nil means \"no arg\", | |
dff7d67f RS |
1432 | a number counts as a prefix arg. |
1433 | ||
85969cb1 RS |
1434 | To kill a whole line, when point is not at the beginning, type \ |
1435 | \\[beginning-of-line] \\[kill-line] \\[kill-line]. | |
1436 | ||
1437 | If `kill-whole-line' is non-nil, then this command kills the whole line | |
1438 | including its terminating newline, when used at the beginning of a line | |
1439 | with no argument. As a consequence, you can always kill a whole line | |
1440 | by typing \\[beginning-of-line] \\[kill-line]." | |
2076c87c JB |
1441 | (interactive "P") |
1442 | (kill-region (point) | |
e6291fe1 RS |
1443 | ;; It is better to move point to the other end of the kill |
1444 | ;; before killing. That way, in a read-only buffer, point | |
1445 | ;; moves across the text that is copied to the kill ring. | |
1446 | ;; The choice has no effect on undo now that undo records | |
1447 | ;; the value of point from before the command was run. | |
1448 | (progn | |
2076c87c | 1449 | (if arg |
53efb707 | 1450 | (forward-visible-line (prefix-numeric-value arg)) |
2076c87c JB |
1451 | (if (eobp) |
1452 | (signal 'end-of-buffer nil)) | |
38ebcf29 | 1453 | (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp))) |
53efb707 RS |
1454 | (forward-visible-line 1) |
1455 | (end-of-visible-line))) | |
2076c87c | 1456 | (point)))) |
53efb707 RS |
1457 | |
1458 | (defun forward-visible-line (arg) | |
9e7a6b30 RS |
1459 | "Move forward by ARG lines, ignoring currently invisible newlines only. |
1460 | If ARG is negative, move backward -ARG lines. | |
1461 | If ARG is zero, move to the beginning of the current line." | |
53efb707 | 1462 | (condition-case nil |
29df77ba RS |
1463 | (if (> arg 0) |
1464 | (while (> arg 0) | |
1465 | (or (zerop (forward-line 1)) | |
1466 | (signal 'end-of-buffer nil)) | |
9e7a6b30 RS |
1467 | ;; If the following character is currently invisible, |
1468 | ;; skip all characters with that same `invisible' property value, | |
1469 | ;; then find the next newline. | |
1470 | (while (and (not (eobp)) | |
1471 | (let ((prop | |
1472 | (get-char-property (point) 'invisible))) | |
1473 | (if (eq buffer-invisibility-spec t) | |
1474 | prop | |
1475 | (or (memq prop buffer-invisibility-spec) | |
1476 | (assq prop buffer-invisibility-spec))))) | |
d91462ce RS |
1477 | (goto-char |
1478 | (if (get-text-property (point) 'invisible) | |
1479 | (or (next-single-property-change (point) 'invisible) | |
1480 | (point-max)) | |
1481 | (next-overlay-change (point)))) | |
9e7a6b30 RS |
1482 | (or (zerop (forward-line 1)) |
1483 | (signal 'end-of-buffer nil))) | |
1484 | (setq arg (1- arg))) | |
29df77ba RS |
1485 | (let ((first t)) |
1486 | (while (or first (< arg 0)) | |
1487 | (if (zerop arg) | |
1488 | (beginning-of-line) | |
1489 | (or (zerop (forward-line -1)) | |
1490 | (signal 'beginning-of-buffer nil))) | |
1491 | (while (and (not (bobp)) | |
1492 | (let ((prop | |
1493 | (get-char-property (1- (point)) 'invisible))) | |
1494 | (if (eq buffer-invisibility-spec t) | |
1495 | prop | |
1496 | (or (memq prop buffer-invisibility-spec) | |
1497 | (assq prop buffer-invisibility-spec))))) | |
d91462ce RS |
1498 | (goto-char |
1499 | (if (get-text-property (1- (point)) 'invisible) | |
1500 | (or (previous-single-property-change (point) 'invisible) | |
1501 | (point-min)) | |
1502 | (previous-overlay-change (point)))) | |
29df77ba RS |
1503 | (or (zerop (forward-line -1)) |
1504 | (signal 'beginning-of-buffer nil))) | |
1505 | (setq first nil) | |
1506 | (setq arg (1+ arg))))) | |
53efb707 RS |
1507 | ((beginning-of-buffer end-of-buffer) |
1508 | nil))) | |
1509 | ||
1510 | (defun end-of-visible-line () | |
1511 | "Move to end of current visible line." | |
1512 | (end-of-line) | |
1513 | ;; If the following character is currently invisible, | |
1514 | ;; skip all characters with that same `invisible' property value, | |
1515 | ;; then find the next newline. | |
1516 | (while (and (not (eobp)) | |
1517 | (let ((prop | |
1518 | (get-char-property (point) 'invisible))) | |
1519 | (if (eq buffer-invisibility-spec t) | |
1520 | prop | |
1521 | (or (memq prop buffer-invisibility-spec) | |
1522 | (assq prop buffer-invisibility-spec))))) | |
1523 | (if (get-text-property (point) 'invisible) | |
1524 | (goto-char (next-single-property-change (point) 'invisible)) | |
1525 | (goto-char (next-overlay-change (point)))) | |
53efb707 | 1526 | (end-of-line))) |
2076c87c | 1527 | \f |
70e14c01 JB |
1528 | ;;;; Window system cut and paste hooks. |
1529 | ||
1530 | (defvar interprogram-cut-function nil | |
1531 | "Function to call to make a killed region available to other programs. | |
1532 | ||
1533 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
1534 | pasting text between the windows of different programs. |
1535 | This variable holds a function that Emacs calls whenever text | |
1536 | is put in the kill ring, to make the new kill available to other | |
70e14c01 JB |
1537 | programs. |
1538 | ||
9f112a3d RS |
1539 | The function takes one or two arguments. |
1540 | The first argument, TEXT, is a string containing | |
1541 | the text which should be made available. | |
1542 | The second, PUSH, if non-nil means this is a \"new\" kill; | |
1543 | nil means appending to an \"old\" kill.") | |
70e14c01 JB |
1544 | |
1545 | (defvar interprogram-paste-function nil | |
1546 | "Function to call to get text cut from other programs. | |
1547 | ||
1548 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
1549 | pasting text between the windows of different programs. |
1550 | This variable holds a function that Emacs calls to obtain | |
70e14c01 JB |
1551 | text that other programs have provided for pasting. |
1552 | ||
1553 | The function should be called with no arguments. If the function | |
1554 | returns nil, then no other program has provided such text, and the top | |
1555 | of the Emacs kill ring should be used. If the function returns a | |
daa37602 JB |
1556 | string, that string should be put in the kill ring as the latest kill. |
1557 | ||
1558 | Note that the function should return a string only if a program other | |
1559 | than Emacs has provided a string for pasting; if Emacs provided the | |
1560 | most recent string, the function should return nil. If it is | |
1561 | difficult to tell whether Emacs or some other program provided the | |
1562 | current string, it is probably good enough to return nil if the string | |
1563 | is equal (according to `string=') to the last text Emacs provided.") | |
70e14c01 JB |
1564 | |
1565 | ||
1566 | \f | |
1567 | ;;;; The kill ring data structure. | |
2076c87c JB |
1568 | |
1569 | (defvar kill-ring nil | |
70e14c01 JB |
1570 | "List of killed text sequences. |
1571 | Since the kill ring is supposed to interact nicely with cut-and-paste | |
1572 | facilities offered by window systems, use of this variable should | |
1573 | interact nicely with `interprogram-cut-function' and | |
1574 | `interprogram-paste-function'. The functions `kill-new', | |
1575 | `kill-append', and `current-kill' are supposed to implement this | |
1576 | interaction; you may want to use them instead of manipulating the kill | |
1577 | ring directly.") | |
2076c87c | 1578 | |
69c1dd37 RS |
1579 | (defcustom kill-ring-max 30 |
1580 | "*Maximum length of kill ring before oldest elements are thrown away." | |
1581 | :type 'integer | |
1582 | :group 'killing) | |
2076c87c JB |
1583 | |
1584 | (defvar kill-ring-yank-pointer nil | |
1585 | "The tail of the kill ring whose car is the last thing yanked.") | |
1586 | ||
f914dc91 | 1587 | (defun kill-new (string &optional replace) |
70e14c01 JB |
1588 | "Make STRING the latest kill in the kill ring. |
1589 | Set the kill-ring-yank pointer to point to it. | |
f914dc91 KH |
1590 | If `interprogram-cut-function' is non-nil, apply it to STRING. |
1591 | Optional second argument REPLACE non-nil means that STRING will replace | |
1592 | the front of the kill ring, rather than being added to the list." | |
f1d01ba2 KH |
1593 | (and (fboundp 'menu-bar-update-yank-menu) |
1594 | (menu-bar-update-yank-menu string (and replace (car kill-ring)))) | |
f914dc91 KH |
1595 | (if replace |
1596 | (setcar kill-ring string) | |
1597 | (setq kill-ring (cons string kill-ring)) | |
1598 | (if (> (length kill-ring) kill-ring-max) | |
1599 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) | |
70e14c01 JB |
1600 | (setq kill-ring-yank-pointer kill-ring) |
1601 | (if interprogram-cut-function | |
657a33ab | 1602 | (funcall interprogram-cut-function string (not replace)))) |
70e14c01 | 1603 | |
2076c87c | 1604 | (defun kill-append (string before-p) |
70e14c01 JB |
1605 | "Append STRING to the end of the latest kill in the kill ring. |
1606 | If BEFORE-P is non-nil, prepend STRING to the kill. | |
88c1aa79 | 1607 | If `interprogram-cut-function' is set, pass the resulting kill to |
70e14c01 | 1608 | it." |
f914dc91 KH |
1609 | (kill-new (if before-p |
1610 | (concat string (car kill-ring)) | |
1611 | (concat (car kill-ring) string)) t)) | |
70e14c01 JB |
1612 | |
1613 | (defun current-kill (n &optional do-not-move) | |
1614 | "Rotate the yanking point by N places, and then return that kill. | |
1615 | If N is zero, `interprogram-paste-function' is set, and calling it | |
1616 | returns a string, then that string is added to the front of the | |
1617 | kill ring and returned as the latest kill. | |
1618 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the | |
1619 | yanking point; just return the Nth kill forward." | |
1620 | (let ((interprogram-paste (and (= n 0) | |
1621 | interprogram-paste-function | |
1622 | (funcall interprogram-paste-function)))) | |
1623 | (if interprogram-paste | |
1624 | (progn | |
1625 | ;; Disable the interprogram cut function when we add the new | |
1626 | ;; text to the kill ring, so Emacs doesn't try to own the | |
1627 | ;; selection, with identical text. | |
1628 | (let ((interprogram-cut-function nil)) | |
1629 | (kill-new interprogram-paste)) | |
1630 | interprogram-paste) | |
1631 | (or kill-ring (error "Kill ring is empty")) | |
47096a67 PE |
1632 | (let ((ARGth-kill-element |
1633 | (nthcdr (mod (- n (length kill-ring-yank-pointer)) | |
1634 | (length kill-ring)) | |
1635 | kill-ring))) | |
70e14c01 JB |
1636 | (or do-not-move |
1637 | (setq kill-ring-yank-pointer ARGth-kill-element)) | |
1638 | (car ARGth-kill-element))))) | |
c88ab9ce | 1639 | |
c88ab9ce | 1640 | |
70e14c01 JB |
1641 | \f |
1642 | ;;;; Commands for manipulating the kill ring. | |
c88ab9ce | 1643 | |
69c1dd37 RS |
1644 | (defcustom kill-read-only-ok nil |
1645 | "*Non-nil means don't signal an error for killing read-only text." | |
1646 | :type 'boolean | |
1647 | :group 'killing) | |
e6291fe1 | 1648 | |
3a5da8a8 RS |
1649 | (put 'text-read-only 'error-conditions |
1650 | '(text-read-only buffer-read-only error)) | |
1651 | (put 'text-read-only 'error-message "Text is read-only") | |
1652 | ||
2076c87c JB |
1653 | (defun kill-region (beg end) |
1654 | "Kill between point and mark. | |
1655 | The text is deleted but saved in the kill ring. | |
1656 | The command \\[yank] can retrieve it from there. | |
1657 | \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].) | |
2aa7a8bf JB |
1658 | If the buffer is read-only, Emacs will beep and refrain from deleting |
1659 | the text, but put the text in the kill ring anyway. This means that | |
1660 | you can use the killing commands to copy text from a read-only buffer. | |
2076c87c JB |
1661 | |
1662 | This is the primitive for programs to kill text (as opposed to deleting it). | |
1663 | Supply two arguments, character numbers indicating the stretch of text | |
1664 | to be killed. | |
1665 | Any command that calls this function is a \"kill command\". | |
1666 | If the previous command was also a kill command, | |
1667 | the text killed this time appends to the text killed last time | |
1668 | to make one entry in the kill ring." | |
2aa7a8bf | 1669 | (interactive "r") |
ccd19b9f KH |
1670 | (condition-case nil |
1671 | ;; Don't let the undo list be truncated before we can even access it. | |
1672 | (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100)) | |
1673 | (old-list buffer-undo-list) | |
1674 | tail | |
1675 | ;; If we can't rely on finding the killed text | |
1676 | ;; in the undo list, save it now as a string. | |
1677 | (string (if (or (eq buffer-undo-list t) | |
1678 | (= beg end)) | |
1679 | (buffer-substring beg end)))) | |
1680 | (delete-region beg end) | |
1681 | ;; Search back in buffer-undo-list for this string, | |
1682 | ;; in case a change hook made property changes. | |
1683 | (setq tail buffer-undo-list) | |
1684 | (unless string | |
1685 | (while (not (stringp (car (car tail)))) | |
1686 | (setq tail (cdr tail))) | |
1687 | ;; If we did not already make the string to use, | |
1688 | ;; use the same one that undo made for us. | |
1689 | (setq string (car (car tail)))) | |
1690 | ;; Add that string to the kill ring, one way or another. | |
1691 | (if (eq last-command 'kill-region) | |
1692 | (kill-append string (< end beg)) | |
1693 | (kill-new string)) | |
1694 | (setq this-command 'kill-region)) | |
1695 | ((buffer-read-only text-read-only) | |
1696 | ;; The code above failed because the buffer, or some of the characters | |
1697 | ;; in the region, are read-only. | |
1698 | ;; We should beep, in case the user just isn't aware of this. | |
1699 | ;; However, there's no harm in putting | |
1700 | ;; the region's text in the kill ring, anyway. | |
1701 | (copy-region-as-kill beg end) | |
cb3e1b4c RS |
1702 | ;; Set this-command now, so it will be set even if we get an error. |
1703 | (setq this-command 'kill-region) | |
1704 | ;; This should barf, if appropriate, and give us the correct error. | |
ccd19b9f KH |
1705 | (if kill-read-only-ok |
1706 | (message "Read only text copied to kill ring") | |
ccd19b9f KH |
1707 | ;; Signal an error if the buffer is read-only. |
1708 | (barf-if-buffer-read-only) | |
1709 | ;; If the buffer isn't read-only, the text is. | |
1710 | (signal 'text-read-only (list (current-buffer))))))) | |
2076c87c | 1711 | |
a382890a KH |
1712 | ;; copy-region-as-kill no longer sets this-command, because it's confusing |
1713 | ;; to get two copies of the text when the user accidentally types M-w and | |
1714 | ;; then corrects it with the intended C-w. | |
2076c87c JB |
1715 | (defun copy-region-as-kill (beg end) |
1716 | "Save the region as if killed, but don't kill it. | |
0e264847 | 1717 | In Transient Mark mode, deactivate the mark. |
46947372 JB |
1718 | If `interprogram-cut-function' is non-nil, also save the text for a window |
1719 | system cut and paste." | |
2076c87c JB |
1720 | (interactive "r") |
1721 | (if (eq last-command 'kill-region) | |
1722 | (kill-append (buffer-substring beg end) (< end beg)) | |
70e14c01 | 1723 | (kill-new (buffer-substring beg end))) |
0e264847 | 1724 | (if transient-mark-mode |
5c7319b6 | 1725 | (setq deactivate-mark t)) |
2076c87c JB |
1726 | nil) |
1727 | ||
1728 | (defun kill-ring-save (beg end) | |
0964e562 | 1729 | "Save the region as if killed, but don't kill it. |
0e264847 | 1730 | In Transient Mark mode, deactivate the mark. |
0964e562 | 1731 | If `interprogram-cut-function' is non-nil, also save the text for a window |
0e264847 RS |
1732 | system cut and paste. |
1733 | ||
1734 | This command is similar to `copy-region-as-kill', except that it gives | |
1735 | visual feedback indicating the extent of the region being copied." | |
2076c87c JB |
1736 | (interactive "r") |
1737 | (copy-region-as-kill beg end) | |
3a801d0c | 1738 | (if (interactive-p) |
66050f10 RS |
1739 | (let ((other-end (if (= (point) beg) end beg)) |
1740 | (opoint (point)) | |
1741 | ;; Inhibit quitting so we can make a quit here | |
1742 | ;; look like a C-g typed as a command. | |
1743 | (inhibit-quit t)) | |
1744 | (if (pos-visible-in-window-p other-end (selected-window)) | |
1745 | (progn | |
1746 | ;; Swap point and mark. | |
1747 | (set-marker (mark-marker) (point) (current-buffer)) | |
1748 | (goto-char other-end) | |
1749 | (sit-for 1) | |
1750 | ;; Swap back. | |
1751 | (set-marker (mark-marker) other-end (current-buffer)) | |
1752 | (goto-char opoint) | |
1753 | ;; If user quit, deactivate the mark | |
1754 | ;; as C-g would as a command. | |
e4e593ae | 1755 | (and quit-flag mark-active |
fcadf1c7 | 1756 | (deactivate-mark))) |
66050f10 RS |
1757 | (let* ((killed-text (current-kill 0)) |
1758 | (message-len (min (length killed-text) 40))) | |
1759 | (if (= (point) beg) | |
1760 | ;; Don't say "killed"; that is misleading. | |
1761 | (message "Saved text until \"%s\"" | |
1762 | (substring killed-text (- message-len))) | |
1763 | (message "Saved text from \"%s\"" | |
1764 | (substring killed-text 0 message-len)))))))) | |
2076c87c JB |
1765 | |
1766 | (defun append-next-kill () | |
ff1fbe3e | 1767 | "Cause following command, if it kills, to append to previous kill." |
2076c87c JB |
1768 | (interactive) |
1769 | (if (interactive-p) | |
1770 | (progn | |
1771 | (setq this-command 'kill-region) | |
1772 | (message "If the next command is a kill, it will append")) | |
1773 | (setq last-command 'kill-region))) | |
1774 | ||
2076c87c | 1775 | (defun yank-pop (arg) |
ff1fbe3e RS |
1776 | "Replace just-yanked stretch of killed text with a different stretch. |
1777 | This command is allowed only immediately after a `yank' or a `yank-pop'. | |
2076c87c | 1778 | At such a time, the region contains a stretch of reinserted |
ff1fbe3e | 1779 | previously-killed text. `yank-pop' deletes that text and inserts in its |
2076c87c JB |
1780 | place a different stretch of killed text. |
1781 | ||
1782 | With no argument, the previous kill is inserted. | |
ff1fbe3e RS |
1783 | With argument N, insert the Nth previous kill. |
1784 | If N is negative, this is a more recent kill. | |
2076c87c JB |
1785 | |
1786 | The sequence of kills wraps around, so that after the oldest one | |
1787 | comes the newest one." | |
1788 | (interactive "*p") | |
1789 | (if (not (eq last-command 'yank)) | |
1790 | (error "Previous command was not a yank")) | |
1791 | (setq this-command 'yank) | |
3a5da8a8 RS |
1792 | (let ((inhibit-read-only t) |
1793 | (before (< (point) (mark t)))) | |
9a1277dd | 1794 | (delete-region (point) (mark t)) |
fd0f4056 | 1795 | (set-marker (mark-marker) (point) (current-buffer)) |
6cd829a8 | 1796 | (let ((opoint (point))) |
7ae13091 | 1797 | (insert (current-kill arg)) |
6cd829a8 RS |
1798 | (let ((inhibit-read-only t)) |
1799 | (remove-text-properties opoint (point) '(read-only nil)))) | |
fd0f4056 RS |
1800 | (if before |
1801 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. | |
1802 | ;; It is cleaner to avoid activation, even though the command | |
1803 | ;; loop would deactivate the mark because we inserted text. | |
1804 | (goto-char (prog1 (mark t) | |
1805 | (set-marker (mark-marker) (point) (current-buffer)))))) | |
0964e562 | 1806 | nil) |
2076c87c JB |
1807 | |
1808 | (defun yank (&optional arg) | |
1809 | "Reinsert the last stretch of killed text. | |
1810 | More precisely, reinsert the stretch of killed text most recently | |
ff1fbe3e RS |
1811 | killed OR yanked. Put point at end, and set mark at beginning. |
1812 | With just C-u as argument, same but put point at beginning (and mark at end). | |
1813 | With argument N, reinsert the Nth most recently killed stretch of killed | |
2076c87c JB |
1814 | text. |
1815 | See also the command \\[yank-pop]." | |
1816 | (interactive "*P") | |
456c617c RS |
1817 | ;; If we don't get all the way thru, make last-command indicate that |
1818 | ;; for the following command. | |
1819 | (setq this-command t) | |
2076c87c | 1820 | (push-mark (point)) |
6cd829a8 | 1821 | (let ((opoint (point))) |
7ae13091 RS |
1822 | (insert (current-kill (cond |
1823 | ((listp arg) 0) | |
1824 | ((eq arg '-) -1) | |
1825 | (t (1- arg))))) | |
6cd829a8 RS |
1826 | (let ((inhibit-read-only t)) |
1827 | (remove-text-properties opoint (point) '(read-only nil)))) | |
2076c87c | 1828 | (if (consp arg) |
fd0f4056 RS |
1829 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. |
1830 | ;; It is cleaner to avoid activation, even though the command | |
1831 | ;; loop would deactivate the mark because we inserted text. | |
1832 | (goto-char (prog1 (mark t) | |
1833 | (set-marker (mark-marker) (point) (current-buffer))))) | |
456c617c RS |
1834 | ;; If we do get all the way thru, make this-command indicate that. |
1835 | (setq this-command 'yank) | |
0964e562 | 1836 | nil) |
70e14c01 JB |
1837 | |
1838 | (defun rotate-yank-pointer (arg) | |
1839 | "Rotate the yanking point in the kill ring. | |
1840 | With argument, rotate that many kills forward (or backward, if negative)." | |
1841 | (interactive "p") | |
1842 | (current-kill arg)) | |
1843 | ||
2076c87c JB |
1844 | \f |
1845 | (defun insert-buffer (buffer) | |
1846 | "Insert after point the contents of BUFFER. | |
1847 | Puts mark after the inserted text. | |
1848 | BUFFER may be a buffer or a buffer name." | |
c3d4f949 | 1849 | (interactive |
a3e7c391 FP |
1850 | (list |
1851 | (progn | |
1852 | (barf-if-buffer-read-only) | |
1853 | (read-buffer "Insert buffer: " | |
1854 | (if (eq (selected-window) (next-window (selected-window))) | |
1855 | (other-buffer (current-buffer)) | |
1856 | (window-buffer (next-window (selected-window)))) | |
1857 | t)))) | |
2076c87c JB |
1858 | (or (bufferp buffer) |
1859 | (setq buffer (get-buffer buffer))) | |
1860 | (let (start end newmark) | |
1861 | (save-excursion | |
1862 | (save-excursion | |
1863 | (set-buffer buffer) | |
1864 | (setq start (point-min) end (point-max))) | |
1865 | (insert-buffer-substring buffer start end) | |
1866 | (setq newmark (point))) | |
1537a263 JB |
1867 | (push-mark newmark)) |
1868 | nil) | |
2076c87c JB |
1869 | |
1870 | (defun append-to-buffer (buffer start end) | |
1871 | "Append to specified buffer the text of the region. | |
1872 | It is inserted into that buffer before its point. | |
1873 | ||
1874 | When calling from a program, give three arguments: | |
1875 | BUFFER (or buffer name), START and END. | |
1876 | START and END specify the portion of the current buffer to be copied." | |
70e14c01 | 1877 | (interactive |
5d771766 | 1878 | (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t)) |
23efee2c | 1879 | (region-beginning) (region-end))) |
2076c87c JB |
1880 | (let ((oldbuf (current-buffer))) |
1881 | (save-excursion | |
1882 | (set-buffer (get-buffer-create buffer)) | |
1883 | (insert-buffer-substring oldbuf start end)))) | |
1884 | ||
1885 | (defun prepend-to-buffer (buffer start end) | |
1886 | "Prepend to specified buffer the text of the region. | |
1887 | It is inserted into that buffer after its point. | |
1888 | ||
1889 | When calling from a program, give three arguments: | |
1890 | BUFFER (or buffer name), START and END. | |
1891 | START and END specify the portion of the current buffer to be copied." | |
1892 | (interactive "BPrepend to buffer: \nr") | |
1893 | (let ((oldbuf (current-buffer))) | |
1894 | (save-excursion | |
1895 | (set-buffer (get-buffer-create buffer)) | |
1896 | (save-excursion | |
1897 | (insert-buffer-substring oldbuf start end))))) | |
1898 | ||
1899 | (defun copy-to-buffer (buffer start end) | |
1900 | "Copy to specified buffer the text of the region. | |
1901 | It is inserted into that buffer, replacing existing text there. | |
1902 | ||
1903 | When calling from a program, give three arguments: | |
1904 | BUFFER (or buffer name), START and END. | |
1905 | START and END specify the portion of the current buffer to be copied." | |
1906 | (interactive "BCopy to buffer: \nr") | |
1907 | (let ((oldbuf (current-buffer))) | |
1908 | (save-excursion | |
1909 | (set-buffer (get-buffer-create buffer)) | |
1910 | (erase-buffer) | |
1911 | (save-excursion | |
1912 | (insert-buffer-substring oldbuf start end))))) | |
1913 | \f | |
62d1c1fc RM |
1914 | (put 'mark-inactive 'error-conditions '(mark-inactive error)) |
1915 | (put 'mark-inactive 'error-message "The mark is not active now") | |
1916 | ||
af39530e | 1917 | (defun mark (&optional force) |
c7c8b31e | 1918 | "Return this buffer's mark value as integer; error if mark inactive. |
af39530e | 1919 | If optional argument FORCE is non-nil, access the mark value |
c7c8b31e RS |
1920 | even if the mark is not currently active, and return nil |
1921 | if there is no mark at all. | |
af39530e | 1922 | |
2076c87c JB |
1923 | If you are using this in an editing command, you are most likely making |
1924 | a mistake; see the documentation of `set-mark'." | |
0e3a7b14 | 1925 | (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive) |
af39530e | 1926 | (marker-position (mark-marker)) |
62d1c1fc | 1927 | (signal 'mark-inactive nil))) |
2076c87c | 1928 | |
19d35374 RM |
1929 | ;; Many places set mark-active directly, and several of them failed to also |
1930 | ;; run deactivate-mark-hook. This shorthand should simplify. | |
1931 | (defsubst deactivate-mark () | |
1932 | "Deactivate the mark by setting `mark-active' to nil. | |
fcadf1c7 | 1933 | \(That makes a difference only in Transient Mark mode.) |
19d35374 | 1934 | Also runs the hook `deactivate-mark-hook'." |
a4b9d3da RS |
1935 | (if transient-mark-mode |
1936 | (progn | |
1937 | (setq mark-active nil) | |
1938 | (run-hooks 'deactivate-mark-hook)))) | |
19d35374 | 1939 | |
2076c87c JB |
1940 | (defun set-mark (pos) |
1941 | "Set this buffer's mark to POS. Don't use this function! | |
1942 | That is to say, don't use this function unless you want | |
1943 | the user to see that the mark has moved, and you want the previous | |
1944 | mark position to be lost. | |
1945 | ||
1946 | Normally, when a new mark is set, the old one should go on the stack. | |
1947 | This is why most applications should use push-mark, not set-mark. | |
1948 | ||
ff1fbe3e | 1949 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1950 | purposes. The mark saves a location for the user's convenience. |
1951 | Most editing commands should not alter the mark. | |
1952 | To remember a location for internal use in the Lisp program, | |
1953 | store it in a Lisp variable. Example: | |
1954 | ||
1955 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
1956 | ||
fcadf1c7 RS |
1957 | (if pos |
1958 | (progn | |
1959 | (setq mark-active t) | |
1960 | (run-hooks 'activate-mark-hook) | |
1961 | (set-marker (mark-marker) pos (current-buffer))) | |
24c22852 RS |
1962 | ;; Normally we never clear mark-active except in Transient Mark mode. |
1963 | ;; But when we actually clear out the mark value too, | |
1964 | ;; we must clear mark-active in any mode. | |
1965 | (setq mark-active nil) | |
1966 | (run-hooks 'deactivate-mark-hook) | |
1967 | (set-marker (mark-marker) nil))) | |
2076c87c JB |
1968 | |
1969 | (defvar mark-ring nil | |
e55e2267 | 1970 | "The list of former marks of the current buffer, most recent first.") |
2076c87c | 1971 | (make-variable-buffer-local 'mark-ring) |
e55e2267 | 1972 | (put 'mark-ring 'permanent-local t) |
2076c87c | 1973 | |
69c1dd37 RS |
1974 | (defcustom mark-ring-max 16 |
1975 | "*Maximum size of mark ring. Start discarding off end if gets this big." | |
1976 | :type 'integer | |
1977 | :group 'editing-basics) | |
2076c87c | 1978 | |
dc029f0b RM |
1979 | (defvar global-mark-ring nil |
1980 | "The list of saved global marks, most recent first.") | |
1981 | ||
69c1dd37 | 1982 | (defcustom global-mark-ring-max 16 |
dc029f0b | 1983 | "*Maximum size of global mark ring. \ |
69c1dd37 RS |
1984 | Start discarding off end if gets this big." |
1985 | :type 'integer | |
1986 | :group 'editing-basics) | |
dc029f0b | 1987 | |
2076c87c JB |
1988 | (defun set-mark-command (arg) |
1989 | "Set mark at where point is, or jump to mark. | |
dc029f0b RM |
1990 | With no prefix argument, set mark, push old mark position on local mark |
1991 | ring, and push mark on global mark ring. | |
1992 | With argument, jump to mark, and pop a new position for mark off the ring | |
1993 | \(does not affect global mark ring\). | |
2076c87c | 1994 | |
ff1fbe3e | 1995 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
1996 | purposes. See the documentation of `set-mark' for more information." |
1997 | (interactive "P") | |
1998 | (if (null arg) | |
9a1277dd | 1999 | (progn |
fd0f4056 | 2000 | (push-mark nil nil t)) |
af39530e | 2001 | (if (null (mark t)) |
2076c87c | 2002 | (error "No mark set in this buffer") |
9a1277dd | 2003 | (goto-char (mark t)) |
2076c87c JB |
2004 | (pop-mark)))) |
2005 | ||
fd0f4056 | 2006 | (defun push-mark (&optional location nomsg activate) |
2076c87c | 2007 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. |
f1382a3d RM |
2008 | If the last global mark pushed was not in the current buffer, |
2009 | also push LOCATION on the global mark ring. | |
fd0f4056 | 2010 | Display `Mark set' unless the optional second arg NOMSG is non-nil. |
8cdc660f | 2011 | In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil. |
2076c87c | 2012 | |
ff1fbe3e | 2013 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
9a1277dd RS |
2014 | purposes. See the documentation of `set-mark' for more information. |
2015 | ||
2016 | In Transient Mark mode, this does not activate the mark." | |
af39530e | 2017 | (if (null (mark t)) |
2076c87c JB |
2018 | nil |
2019 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) | |
2020 | (if (> (length mark-ring) mark-ring-max) | |
2021 | (progn | |
2022 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
2023 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) | |
9a1277dd | 2024 | (set-marker (mark-marker) (or location (point)) (current-buffer)) |
dc029f0b | 2025 | ;; Now push the mark on the global mark ring. |
f1382a3d | 2026 | (if (and global-mark-ring |
e08d3f7c | 2027 | (eq (marker-buffer (car global-mark-ring)) (current-buffer))) |
f1382a3d RM |
2028 | ;; The last global mark pushed was in this same buffer. |
2029 | ;; Don't push another one. | |
2030 | nil | |
2031 | (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring)) | |
dc029f0b RM |
2032 | (if (> (length global-mark-ring) global-mark-ring-max) |
2033 | (progn | |
2034 | (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) | |
2035 | nil) | |
f1382a3d | 2036 | (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))) |
efcf38c7 | 2037 | (or nomsg executing-kbd-macro (> (minibuffer-depth) 0) |
2076c87c | 2038 | (message "Mark set")) |
8cdc660f RS |
2039 | (if (or activate (not transient-mark-mode)) |
2040 | (set-mark (mark t))) | |
2076c87c JB |
2041 | nil) |
2042 | ||
2043 | (defun pop-mark () | |
2044 | "Pop off mark ring into the buffer's actual mark. | |
2045 | Does not set point. Does nothing if mark ring is empty." | |
2046 | (if mark-ring | |
2047 | (progn | |
2048 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
9a1277dd | 2049 | (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer)) |
19d35374 | 2050 | (deactivate-mark) |
2076c87c | 2051 | (move-marker (car mark-ring) nil) |
9a1277dd | 2052 | (if (null (mark t)) (ding)) |
2076c87c JB |
2053 | (setq mark-ring (cdr mark-ring))))) |
2054 | ||
e462e42f | 2055 | (defalias 'exchange-dot-and-mark 'exchange-point-and-mark) |
2076c87c | 2056 | (defun exchange-point-and-mark () |
af39530e RS |
2057 | "Put the mark where point is now, and point where the mark is now. |
2058 | This command works even when the mark is not active, | |
2059 | and it reactivates the mark." | |
2076c87c | 2060 | (interactive nil) |
af39530e | 2061 | (let ((omark (mark t))) |
2076c87c JB |
2062 | (if (null omark) |
2063 | (error "No mark set in this buffer")) | |
2064 | (set-mark (point)) | |
2065 | (goto-char omark) | |
2066 | nil)) | |
e23c2c21 | 2067 | |
22c3935a | 2068 | (defun transient-mark-mode (arg) |
e23c2c21 | 2069 | "Toggle Transient Mark mode. |
b411b5fa | 2070 | With arg, turn Transient Mark mode on if arg is positive, off otherwise. |
e23c2c21 | 2071 | |
5dd1220d RS |
2072 | In Transient Mark mode, when the mark is active, the region is highlighted. |
2073 | Changing the buffer \"deactivates\" the mark. | |
2074 | So do certain other operations that set the mark | |
2075 | but whose main purpose is something else--for example, | |
2076 | incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]." | |
22c3935a | 2077 | (interactive "P") |
e23c2c21 RS |
2078 | (setq transient-mark-mode |
2079 | (if (null arg) | |
2080 | (not transient-mark-mode) | |
2c5d387c | 2081 | (> (prefix-numeric-value arg) 0))) |
22c3935a RS |
2082 | (if (interactive-p) |
2083 | (if transient-mark-mode | |
2084 | (message "Transient Mark mode enabled") | |
2085 | (message "Transient Mark mode disabled")))) | |
dc029f0b RM |
2086 | |
2087 | (defun pop-global-mark () | |
2088 | "Pop off global mark ring and jump to the top location." | |
2089 | (interactive) | |
52b6d445 RS |
2090 | ;; Pop entries which refer to non-existent buffers. |
2091 | (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) | |
2092 | (setq global-mark-ring (cdr global-mark-ring))) | |
dc029f0b RM |
2093 | (or global-mark-ring |
2094 | (error "No global mark set")) | |
2095 | (let* ((marker (car global-mark-ring)) | |
2096 | (buffer (marker-buffer marker)) | |
2097 | (position (marker-position marker))) | |
34c31301 RS |
2098 | (setq global-mark-ring (nconc (cdr global-mark-ring) |
2099 | (list (car global-mark-ring)))) | |
dc029f0b RM |
2100 | (set-buffer buffer) |
2101 | (or (and (>= position (point-min)) | |
2102 | (<= position (point-max))) | |
2103 | (widen)) | |
2104 | (goto-char position) | |
2105 | (switch-to-buffer buffer))) | |
2076c87c | 2106 | \f |
69c1dd37 RS |
2107 | (defcustom next-line-add-newlines t |
2108 | "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error." | |
2109 | :type 'boolean | |
2110 | :group 'editing-basics) | |
38ebcf29 | 2111 | |
2076c87c JB |
2112 | (defun next-line (arg) |
2113 | "Move cursor vertically down ARG lines. | |
2114 | If there is no character in the target line exactly under the current column, | |
2115 | the cursor is positioned after the character in that line which spans this | |
2116 | column, or at the end of the line if it is not long enough. | |
38ebcf29 | 2117 | If there is no line in the buffer after this one, behavior depends on the |
1a2c3941 RS |
2118 | value of `next-line-add-newlines'. If non-nil, it inserts a newline character |
2119 | to create a line, and moves the cursor to that line. Otherwise it moves the | |
e47d38f6 | 2120 | cursor to the end of the buffer. |
2076c87c JB |
2121 | |
2122 | The command \\[set-goal-column] can be used to create | |
85969cb1 RS |
2123 | a semipermanent goal column for this command. |
2124 | Then instead of trying to move exactly vertically (or as close as possible), | |
2125 | this command moves to the specified goal column (or as close as possible). | |
2126 | The goal column is stored in the variable `goal-column', which is nil | |
2127 | when there is no goal column. | |
2076c87c JB |
2128 | |
2129 | If you are thinking of using this in a Lisp program, consider | |
2130 | using `forward-line' instead. It is usually easier to use | |
2131 | and more reliable (no dependence on goal column, etc.)." | |
2132 | (interactive "p") | |
028922cf RS |
2133 | (if (and next-line-add-newlines (= arg 1)) |
2134 | (let ((opoint (point))) | |
3534a809 RS |
2135 | (end-of-line) |
2136 | (if (eobp) | |
28191e20 | 2137 | (newline 1) |
028922cf RS |
2138 | (goto-char opoint) |
2139 | (line-move arg))) | |
1a2c3941 RS |
2140 | (if (interactive-p) |
2141 | (condition-case nil | |
2142 | (line-move arg) | |
2143 | ((beginning-of-buffer end-of-buffer) (ding))) | |
2144 | (line-move arg))) | |
2076c87c JB |
2145 | nil) |
2146 | ||
2147 | (defun previous-line (arg) | |
2148 | "Move cursor vertically up ARG lines. | |
2149 | If there is no character in the target line exactly over the current column, | |
2150 | the cursor is positioned after the character in that line which spans this | |
2151 | column, or at the end of the line if it is not long enough. | |
2152 | ||
2153 | The command \\[set-goal-column] can be used to create | |
85969cb1 RS |
2154 | a semipermanent goal column for this command. |
2155 | Then instead of trying to move exactly vertically (or as close as possible), | |
2156 | this command moves to the specified goal column (or as close as possible). | |
2157 | The goal column is stored in the variable `goal-column', which is nil | |
2158 | when there is no goal column. | |
2076c87c JB |
2159 | |
2160 | If you are thinking of using this in a Lisp program, consider using | |
c2e8a012 | 2161 | `forward-line' with a negative argument instead. It is usually easier |
2076c87c JB |
2162 | to use and more reliable (no dependence on goal column, etc.)." |
2163 | (interactive "p") | |
1a2c3941 RS |
2164 | (if (interactive-p) |
2165 | (condition-case nil | |
2166 | (line-move (- arg)) | |
2167 | ((beginning-of-buffer end-of-buffer) (ding))) | |
2168 | (line-move (- arg))) | |
2076c87c JB |
2169 | nil) |
2170 | ||
69c1dd37 | 2171 | (defcustom track-eol nil |
2076c87c JB |
2172 | "*Non-nil means vertical motion starting at end of line keeps to ends of lines. |
2173 | This means moving to the end of each line moved onto. | |
69c1dd37 RS |
2174 | The beginning of a blank line does not count as the end of a line." |
2175 | :type 'boolean | |
2176 | :group 'editing-basics) | |
2177 | ||
2178 | (defcustom goal-column nil | |
2179 | "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil." | |
2180 | :type '(choice integer | |
2181 | (const :tag "None" nil)) | |
2182 | :group 'editing-basics) | |
912c6728 | 2183 | (make-variable-buffer-local 'goal-column) |
2076c87c JB |
2184 | |
2185 | (defvar temporary-goal-column 0 | |
2186 | "Current goal column for vertical motion. | |
2187 | It is the column where point was | |
2188 | at the start of current run of vertical motion commands. | |
c637ae6f | 2189 | When the `track-eol' feature is doing its job, the value is 9999.") |
2076c87c | 2190 | |
69c1dd37 | 2191 | (defcustom line-move-ignore-invisible nil |
098fc1fb | 2192 | "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines. |
69c1dd37 RS |
2193 | Outline mode sets this." |
2194 | :type 'boolean | |
2195 | :group 'editing-basics) | |
098fc1fb | 2196 | |
8c745744 RS |
2197 | ;; This is the guts of next-line and previous-line. |
2198 | ;; Arg says how many lines to move. | |
2076c87c | 2199 | (defun line-move (arg) |
2596511d RS |
2200 | ;; Don't run any point-motion hooks, and disregard intangibility, |
2201 | ;; for intermediate positions. | |
2202 | (let ((inhibit-point-motion-hooks t) | |
2203 | (opoint (point)) | |
6c8499b9 | 2204 | new line-end line-beg) |
2596511d RS |
2205 | (unwind-protect |
2206 | (progn | |
2207 | (if (not (or (eq last-command 'next-line) | |
2208 | (eq last-command 'previous-line))) | |
2209 | (setq temporary-goal-column | |
2210 | (if (and track-eol (eolp) | |
2211 | ;; Don't count beg of empty line as end of line | |
2212 | ;; unless we just did explicit end-of-line. | |
2213 | (or (not (bolp)) (eq last-command 'end-of-line))) | |
2214 | 9999 | |
2215 | (current-column)))) | |
2216 | (if (and (not (integerp selective-display)) | |
2217 | (not line-move-ignore-invisible)) | |
2218 | ;; Use just newline characters. | |
2219 | (or (if (> arg 0) | |
2220 | (progn (if (> arg 1) (forward-line (1- arg))) | |
2221 | ;; This way of moving forward ARG lines | |
2222 | ;; verifies that we have a newline after the last one. | |
2223 | ;; It doesn't get confused by intangible text. | |
2224 | (end-of-line) | |
2225 | (zerop (forward-line 1))) | |
2226 | (and (zerop (forward-line arg)) | |
2227 | (bolp))) | |
2228 | (signal (if (< arg 0) | |
2229 | 'beginning-of-buffer | |
2230 | 'end-of-buffer) | |
2231 | nil)) | |
2232 | ;; Move by arg lines, but ignore invisible ones. | |
2233 | (while (> arg 0) | |
2234 | (end-of-line) | |
2235 | (and (zerop (vertical-motion 1)) | |
2236 | (signal 'end-of-buffer nil)) | |
2237 | ;; If the following character is currently invisible, | |
2238 | ;; skip all characters with that same `invisible' property value. | |
2239 | (while (and (not (eobp)) | |
2240 | (let ((prop | |
2241 | (get-char-property (point) 'invisible))) | |
2242 | (if (eq buffer-invisibility-spec t) | |
2243 | prop | |
2244 | (or (memq prop buffer-invisibility-spec) | |
2245 | (assq prop buffer-invisibility-spec))))) | |
2246 | (if (get-text-property (point) 'invisible) | |
2247 | (goto-char (next-single-property-change (point) 'invisible)) | |
2248 | (goto-char (next-overlay-change (point))))) | |
2249 | (setq arg (1- arg))) | |
2250 | (while (< arg 0) | |
2251 | (beginning-of-line) | |
2252 | (and (zerop (vertical-motion -1)) | |
2253 | (signal 'beginning-of-buffer nil)) | |
2254 | (while (and (not (bobp)) | |
2255 | (let ((prop | |
2256 | (get-char-property (1- (point)) 'invisible))) | |
2257 | (if (eq buffer-invisibility-spec t) | |
2258 | prop | |
2259 | (or (memq prop buffer-invisibility-spec) | |
2260 | (assq prop buffer-invisibility-spec))))) | |
2261 | (if (get-text-property (1- (point)) 'invisible) | |
2262 | (goto-char (previous-single-property-change (point) 'invisible)) | |
2263 | (goto-char (previous-overlay-change (point))))) | |
2264 | (setq arg (1+ arg)))) | |
0565d307 RS |
2265 | (let ((buffer-invisibility-spec nil)) |
2266 | (move-to-column (or goal-column temporary-goal-column)))) | |
50be475d RS |
2267 | (setq new (point)) |
2268 | ;; If we are moving into some intangible text, | |
2269 | ;; look for following text on the same line which isn't intangible | |
2270 | ;; and move there. | |
6c8499b9 RS |
2271 | (setq line-end (save-excursion (end-of-line) (point))) |
2272 | (setq line-beg (save-excursion (beginning-of-line) (point))) | |
50be475d RS |
2273 | (let ((after (and (< new (point-max)) |
2274 | (get-char-property new 'intangible))) | |
2275 | (before (and (> new (point-min)) | |
6c8499b9 RS |
2276 | (get-char-property (1- new) 'intangible)))) |
2277 | (when (and before (eq before after) | |
2278 | (not (bolp))) | |
50be475d RS |
2279 | (goto-char (point-min)) |
2280 | (let ((inhibit-point-motion-hooks nil)) | |
2281 | (goto-char new)) | |
2282 | (if (<= new line-end) | |
2283 | (setq new (point))))) | |
6c8499b9 RS |
2284 | ;; NEW is where we want to move to. |
2285 | ;; LINE-BEG and LINE-END are the beginning and end of the line. | |
2286 | ;; Move there in just one step, from our starting position, | |
2287 | ;; with intangibility and point-motion hooks enabled this time. | |
2596511d RS |
2288 | (goto-char opoint) |
2289 | (setq inhibit-point-motion-hooks nil) | |
6c8499b9 RS |
2290 | (goto-char new) |
2291 | ;; If intangibility processing moved us to a different line, | |
2292 | ;; readjust the horizontal position within the line we ended up at. | |
2293 | (when (or (< (point) line-beg) (> (point) line-end)) | |
2294 | (setq new (point)) | |
2295 | (setq inhibit-point-motion-hooks t) | |
2296 | (setq line-end (save-excursion (end-of-line) (point))) | |
2297 | (beginning-of-line) | |
2298 | (setq line-beg (point)) | |
2299 | (let ((buffer-invisibility-spec nil)) | |
2300 | (move-to-column (or goal-column temporary-goal-column))) | |
2301 | (if (<= (point) line-end) | |
2302 | (setq new (point))) | |
2303 | (goto-char (point-min)) | |
2304 | (setq inhibit-point-motion-hooks nil) | |
2305 | (goto-char new) | |
2306 | ))) | |
2596511d | 2307 | nil) |
2076c87c | 2308 | |
d5ab2033 JB |
2309 | ;;; Many people have said they rarely use this feature, and often type |
2310 | ;;; it by accident. Maybe it shouldn't even be on a key. | |
2311 | (put 'set-goal-column 'disabled t) | |
2076c87c JB |
2312 | |
2313 | (defun set-goal-column (arg) | |
2314 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
2315 | Those commands will move to this position in the line moved to | |
2316 | rather than trying to keep the same horizontal position. | |
2317 | With a non-nil argument, clears out the goal column | |
912c6728 RS |
2318 | so that \\[next-line] and \\[previous-line] resume vertical motion. |
2319 | The goal column is stored in the variable `goal-column'." | |
2076c87c JB |
2320 | (interactive "P") |
2321 | (if arg | |
2322 | (progn | |
2323 | (setq goal-column nil) | |
2324 | (message "No goal column")) | |
2325 | (setq goal-column (current-column)) | |
2326 | (message (substitute-command-keys | |
2327 | "Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
2328 | goal-column)) | |
2329 | nil) | |
2330 | \f | |
0d5bbbf7 ER |
2331 | ;;; Partial support for horizontal autoscrolling. Someday, this feature |
2332 | ;;; will be built into the C level and all the (hscroll-point-visible) calls | |
2333 | ;;; will go away. | |
2334 | ||
69c1dd37 | 2335 | (defcustom hscroll-step 0 |
0d5bbbf7 ER |
2336 | "*The number of columns to try scrolling a window by when point moves out. |
2337 | If that fails to bring point back on frame, point is centered instead. | |
69c1dd37 RS |
2338 | If this is zero, point is always centered after it moves off frame." |
2339 | :type '(choice (const :tag "Alway Center" 0) | |
2340 | (integer :format "%v" 1)) | |
2341 | :group 'editing-basics) | |
0d5bbbf7 ER |
2342 | |
2343 | (defun hscroll-point-visible () | |
26c5bf8e KH |
2344 | "Scrolls the selected window horizontally to make point visible." |
2345 | (save-excursion | |
2346 | (set-buffer (window-buffer)) | |
2347 | (if (not (or truncate-lines | |
2348 | (> (window-hscroll) 0) | |
2349 | (and truncate-partial-width-windows | |
2350 | (< (window-width) (frame-width))))) | |
2351 | ;; Point is always visible when lines are wrapped. | |
2352 | () | |
2353 | ;; If point is on the invisible part of the line before window-start, | |
2354 | ;; then hscrolling can't bring it back, so reset window-start first. | |
2355 | (and (< (point) (window-start)) | |
2356 | (let ((ws-bol (save-excursion | |
2357 | (goto-char (window-start)) | |
2358 | (beginning-of-line) | |
2359 | (point)))) | |
2360 | (and (>= (point) ws-bol) | |
2361 | (set-window-start nil ws-bol)))) | |
2362 | (let* ((here (hscroll-window-column)) | |
2363 | (left (min (window-hscroll) 1)) | |
2364 | (right (1- (window-width)))) | |
2365 | ;; Allow for the truncation glyph, if we're not exactly at eol. | |
2366 | (if (not (and (= here right) | |
2367 | (= (following-char) ?\n))) | |
2368 | (setq right (1- right))) | |
2369 | (cond | |
2370 | ;; If too far away, just recenter. But don't show too much | |
2371 | ;; white space off the end of the line. | |
2372 | ((or (< here (- left hscroll-step)) | |
2373 | (> here (+ right hscroll-step))) | |
2374 | (let ((eol (save-excursion (end-of-line) (hscroll-window-column)))) | |
2375 | (scroll-left (min (- here (/ (window-width) 2)) | |
2376 | (- eol (window-width) -5))))) | |
2377 | ;; Within range. Scroll by one step (or maybe not at all). | |
2378 | ((< here left) | |
2379 | (scroll-right hscroll-step)) | |
2380 | ((> here right) | |
2381 | (scroll-left hscroll-step))))))) | |
2382 | ||
2383 | ;; This function returns the window's idea of the display column of point, | |
2384 | ;; assuming that the window is already known to be truncated rather than | |
2385 | ;; wrapped, and that we've already handled the case where point is on the | |
2386 | ;; part of the line before window-start. We ignore window-width; if point | |
2387 | ;; is beyond the right margin, we want to know how far. The return value | |
2388 | ;; includes the effects of window-hscroll, window-start, and the prompt | |
2389 | ;; string in the minibuffer. It may be negative due to hscroll. | |
2390 | (defun hscroll-window-column () | |
2391 | (let* ((hscroll (window-hscroll)) | |
2392 | (startpos (save-excursion | |
2393 | (beginning-of-line) | |
2394 | (if (= (point) (save-excursion | |
2395 | (goto-char (window-start)) | |
2396 | (beginning-of-line) | |
2397 | (point))) | |
2398 | (goto-char (window-start))) | |
2399 | (point))) | |
2400 | (hpos (+ (if (and (eq (selected-window) (minibuffer-window)) | |
2401 | (= 1 (window-start)) | |
2402 | (= startpos (point-min))) | |
2403 | (minibuffer-prompt-width) | |
2404 | 0) | |
2405 | (min 0 (- 1 hscroll)))) | |
2406 | val) | |
2407 | (car (cdr (compute-motion startpos (cons hpos 0) | |
2408 | (point) (cons 0 1) | |
2409 | 1000000 (cons hscroll 0) nil))))) | |
2410 | ||
0d5bbbf7 | 2411 | |
dff7d67f RS |
2412 | ;; rms: (1) The definitions of arrow keys should not simply restate |
2413 | ;; what keys they are. The arrow keys should run the ordinary commands. | |
2414 | ;; (2) The arrow keys are just one of many common ways of moving point | |
2415 | ;; within a line. Real horizontal autoscrolling would be a good feature, | |
2416 | ;; but supporting it only for arrow keys is too incomplete to be desirable. | |
2417 | ||
2418 | ;;;;; Make arrow keys do the right thing for improved terminal support | |
2419 | ;;;;; When we implement true horizontal autoscrolling, right-arrow and | |
2420 | ;;;;; left-arrow can lose the (if truncate-lines ...) clause and become | |
2421 | ;;;;; aliases. These functions are bound to the corresponding keyboard | |
2422 | ;;;;; events in loaddefs.el. | |
2423 | ||
2424 | ;;(defun right-arrow (arg) | |
2425 | ;; "Move right one character on the screen (with prefix ARG, that many chars). | |
2426 | ;;Scroll right if needed to keep point horizontally onscreen." | |
2427 | ;; (interactive "P") | |
2428 | ;; (forward-char arg) | |
2429 | ;; (hscroll-point-visible)) | |
2430 | ||
2431 | ;;(defun left-arrow (arg) | |
2432 | ;; "Move left one character on the screen (with prefix ARG, that many chars). | |
2433 | ;;Scroll left if needed to keep point horizontally onscreen." | |
2434 | ;; (interactive "P") | |
2435 | ;; (backward-char arg) | |
2436 | ;; (hscroll-point-visible)) | |
7492f5a6 RS |
2437 | |
2438 | (defun scroll-other-window-down (lines) | |
e47d38f6 RS |
2439 | "Scroll the \"other window\" down. |
2440 | For more details, see the documentation for `scroll-other-window'." | |
7492f5a6 RS |
2441 | (interactive "P") |
2442 | (scroll-other-window | |
2443 | ;; Just invert the argument's meaning. | |
2444 | ;; We can do that without knowing which window it will be. | |
2445 | (if (eq lines '-) nil | |
2446 | (if (null lines) '- | |
2447 | (- (prefix-numeric-value lines)))))) | |
e47d38f6 | 2448 | (define-key esc-map [?\C-\S-v] 'scroll-other-window-down) |
3aef9604 RS |
2449 | |
2450 | (defun beginning-of-buffer-other-window (arg) | |
2451 | "Move point to the beginning of the buffer in the other window. | |
2452 | Leave mark at previous position. | |
2453 | With arg N, put point N/10 of the way from the true beginning." | |
2454 | (interactive "P") | |
2455 | (let ((orig-window (selected-window)) | |
2456 | (window (other-window-for-scrolling))) | |
2457 | ;; We use unwind-protect rather than save-window-excursion | |
2458 | ;; because the latter would preserve the things we want to change. | |
2459 | (unwind-protect | |
2460 | (progn | |
2461 | (select-window window) | |
2462 | ;; Set point and mark in that window's buffer. | |
2463 | (beginning-of-buffer arg) | |
2464 | ;; Set point accordingly. | |
2465 | (recenter '(t))) | |
2466 | (select-window orig-window)))) | |
2467 | ||
2468 | (defun end-of-buffer-other-window (arg) | |
2469 | "Move point to the end of the buffer in the other window. | |
2470 | Leave mark at previous position. | |
2471 | With arg N, put point N/10 of the way from the true end." | |
2472 | (interactive "P") | |
2473 | ;; See beginning-of-buffer-other-window for comments. | |
2474 | (let ((orig-window (selected-window)) | |
2475 | (window (other-window-for-scrolling))) | |
2476 | (unwind-protect | |
2477 | (progn | |
2478 | (select-window window) | |
4500ff36 | 2479 | (end-of-buffer arg) |
3aef9604 RS |
2480 | (recenter '(t))) |
2481 | (select-window orig-window)))) | |
38ebcf29 | 2482 | \f |
2076c87c JB |
2483 | (defun transpose-chars (arg) |
2484 | "Interchange characters around point, moving forward one character. | |
2485 | With prefix arg ARG, effect is to take character before point | |
2486 | and drag it forward past ARG other characters (backward if ARG negative). | |
2487 | If no argument and at end of line, the previous two chars are exchanged." | |
2488 | (interactive "*P") | |
2489 | (and (null arg) (eolp) (forward-char -1)) | |
2490 | (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
2491 | ||
2492 | (defun transpose-words (arg) | |
2493 | "Interchange words around point, leaving point at end of them. | |
2494 | With prefix arg ARG, effect is to take word before or around point | |
2495 | and drag it forward past ARG other words (backward if ARG negative). | |
2496 | If ARG is zero, the words around or after point and around or after mark | |
2497 | are interchanged." | |
2498 | (interactive "*p") | |
2499 | (transpose-subr 'forward-word arg)) | |
2500 | ||
2501 | (defun transpose-sexps (arg) | |
2502 | "Like \\[transpose-words] but applies to sexps. | |
2503 | Does not work on a sexp that point is in the middle of | |
2504 | if it is a list or string." | |
2505 | (interactive "*p") | |
2506 | (transpose-subr 'forward-sexp arg)) | |
2507 | ||
2508 | (defun transpose-lines (arg) | |
2509 | "Exchange current line and previous line, leaving point after both. | |
2510 | With argument ARG, takes previous line and moves it past ARG lines. | |
2511 | With argument 0, interchanges line point is in with line mark is in." | |
2512 | (interactive "*p") | |
2513 | (transpose-subr (function | |
2514 | (lambda (arg) | |
d3f4ef3f | 2515 | (if (> arg 0) |
2076c87c | 2516 | (progn |
d3f4ef3f AS |
2517 | ;; Move forward over ARG lines, |
2518 | ;; but create newlines if necessary. | |
2519 | (setq arg (forward-line arg)) | |
2520 | (if (/= (preceding-char) ?\n) | |
2521 | (setq arg (1+ arg))) | |
2522 | (if (> arg 0) | |
2523 | (newline arg))) | |
2076c87c JB |
2524 | (forward-line arg)))) |
2525 | arg)) | |
2526 | ||
2527 | (defun transpose-subr (mover arg) | |
2528 | (let (start1 end1 start2 end2) | |
2529 | (if (= arg 0) | |
2530 | (progn | |
2531 | (save-excursion | |
2532 | (funcall mover 1) | |
2533 | (setq end2 (point)) | |
2534 | (funcall mover -1) | |
2535 | (setq start2 (point)) | |
2536 | (goto-char (mark)) | |
2537 | (funcall mover 1) | |
2538 | (setq end1 (point)) | |
2539 | (funcall mover -1) | |
2540 | (setq start1 (point)) | |
2541 | (transpose-subr-1)) | |
d3f4ef3f AS |
2542 | (exchange-point-and-mark)) |
2543 | (if (> arg 0) | |
2544 | (progn | |
2545 | (funcall mover -1) | |
2546 | (setq start1 (point)) | |
2547 | (funcall mover 1) | |
2548 | (setq end1 (point)) | |
2549 | (funcall mover arg) | |
2550 | (setq end2 (point)) | |
2551 | (funcall mover (- arg)) | |
2552 | (setq start2 (point)) | |
2553 | (transpose-subr-1) | |
2554 | (goto-char end2)) | |
2555 | (funcall mover -1) | |
2556 | (setq start2 (point)) | |
2557 | (funcall mover 1) | |
2558 | (setq end2 (point)) | |
2559 | (funcall mover (1- arg)) | |
2560 | (setq start1 (point)) | |
2561 | (funcall mover (- arg)) | |
2562 | (setq end1 (point)) | |
2563 | (transpose-subr-1))))) | |
2076c87c JB |
2564 | |
2565 | (defun transpose-subr-1 () | |
2566 | (if (> (min end1 end2) (max start1 start2)) | |
2567 | (error "Don't have two things to transpose")) | |
d5d99b80 KH |
2568 | (let* ((word1 (buffer-substring start1 end1)) |
2569 | (len1 (length word1)) | |
2570 | (word2 (buffer-substring start2 end2)) | |
2571 | (len2 (length word2))) | |
2076c87c JB |
2572 | (delete-region start2 end2) |
2573 | (goto-char start2) | |
2574 | (insert word1) | |
2575 | (goto-char (if (< start1 start2) start1 | |
d5d99b80 KH |
2576 | (+ start1 (- len1 len2)))) |
2577 | (delete-region (point) (+ (point) len1)) | |
2076c87c JB |
2578 | (insert word2))) |
2579 | \f | |
69c1dd37 | 2580 | (defcustom comment-column 32 |
2076c87c | 2581 | "*Column to indent right-margin comments to. |
8a8fa723 JB |
2582 | Setting this variable automatically makes it local to the current buffer. |
2583 | Each mode establishes a different default value for this variable; you | |
69c1dd37 RS |
2584 | can set the value for a particular mode using that mode's hook." |
2585 | :type 'integer | |
2586 | :group 'fill-comments) | |
2076c87c JB |
2587 | (make-variable-buffer-local 'comment-column) |
2588 | ||
69c1dd37 RS |
2589 | (defcustom comment-start nil |
2590 | "*String to insert to start a new comment, or nil if no comment syntax." | |
2591 | :type '(choice (const :tag "None" nil) | |
2592 | string) | |
2593 | :group 'fill-comments) | |
2076c87c | 2594 | |
69c1dd37 | 2595 | (defcustom comment-start-skip nil |
2076c87c JB |
2596 | "*Regexp to match the start of a comment plus everything up to its body. |
2597 | If there are any \\(...\\) pairs, the comment delimiter text is held to begin | |
69c1dd37 RS |
2598 | at the place matched by the close of the first pair." |
2599 | :type '(choice (const :tag "None" nil) | |
2600 | regexp) | |
2601 | :group 'fill-comments) | |
2076c87c | 2602 | |
69c1dd37 | 2603 | (defcustom comment-end "" |
2076c87c | 2604 | "*String to insert to end a new comment. |
69c1dd37 RS |
2605 | Should be an empty string if comments are terminated by end-of-line." |
2606 | :type 'string | |
2607 | :group 'fill-comments) | |
2076c87c | 2608 | |
1b43f83f | 2609 | (defvar comment-indent-hook nil |
ec9a76e3 JB |
2610 | "Obsolete variable for function to compute desired indentation for a comment. |
2611 | This function is called with no args with point at the beginning of | |
2612 | the comment's starting delimiter.") | |
2613 | ||
1b43f83f | 2614 | (defvar comment-indent-function |
2076c87c JB |
2615 | '(lambda () comment-column) |
2616 | "Function to compute desired indentation for a comment. | |
2617 | This function is called with no args with point at the beginning of | |
2618 | the comment's starting delimiter.") | |
2619 | ||
69c1dd37 | 2620 | (defcustom block-comment-start nil |
534a0de5 RS |
2621 | "*String to insert to start a new comment on a line by itself. |
2622 | If nil, use `comment-start' instead. | |
2623 | Note that the regular expression `comment-start-skip' should skip this string | |
69c1dd37 RS |
2624 | as well as the `comment-start' string." |
2625 | :type '(choice (const :tag "Use comment-start" nil) | |
2626 | string) | |
2627 | :group 'fill-comments) | |
534a0de5 | 2628 | |
69c1dd37 | 2629 | (defcustom block-comment-end nil |
534a0de5 RS |
2630 | "*String to insert to end a new comment on a line by itself. |
2631 | Should be an empty string if comments are terminated by end-of-line. | |
69c1dd37 RS |
2632 | If nil, use `comment-end' instead." |
2633 | :type '(choice (const :tag "Use comment-end" nil) | |
2634 | string) | |
2635 | :group 'fill-comments) | |
534a0de5 | 2636 | |
2076c87c JB |
2637 | (defun indent-for-comment () |
2638 | "Indent this line's comment to comment column, or insert an empty comment." | |
2639 | (interactive "*") | |
534a0de5 RS |
2640 | (let* ((empty (save-excursion (beginning-of-line) |
2641 | (looking-at "[ \t]*$"))) | |
2642 | (starter (or (and empty block-comment-start) comment-start)) | |
2643 | (ender (or (and empty block-comment-end) comment-end))) | |
d4a753f9 DL |
2644 | (cond |
2645 | ((null starter) | |
2646 | (error "No comment syntax defined")) | |
2647 | ((null comment-start-skip) | |
2648 | (error "This mode doesn't define `comment-start-skip'")) | |
2649 | (t (let* ((eolpos (save-excursion (end-of-line) (point))) | |
2650 | cpos indent begpos) | |
2651 | (beginning-of-line) | |
2652 | (if (re-search-forward comment-start-skip eolpos 'move) | |
2653 | (progn (setq cpos (point-marker)) | |
2654 | ;; Find the start of the comment delimiter. | |
2655 | ;; If there were paren-pairs in comment-start-skip, | |
2656 | ;; position at the end of the first pair. | |
2657 | (if (match-end 1) | |
2658 | (goto-char (match-end 1)) | |
2659 | ;; If comment-start-skip matched a string with | |
2660 | ;; internal whitespace (not final whitespace) then | |
2661 | ;; the delimiter start at the end of that | |
2662 | ;; whitespace. Otherwise, it starts at the | |
2663 | ;; beginning of what was matched. | |
2664 | (skip-syntax-backward " " (match-beginning 0)) | |
2665 | (skip-syntax-backward "^ " (match-beginning 0))))) | |
2666 | (setq begpos (point)) | |
2667 | ;; Compute desired indent. | |
2668 | (if (= (current-column) | |
2669 | (setq indent (if comment-indent-hook | |
2670 | (funcall comment-indent-hook) | |
2671 | (funcall comment-indent-function)))) | |
2672 | (goto-char begpos) | |
2673 | ;; If that's different from current, change it. | |
2674 | (skip-chars-backward " \t") | |
2675 | (delete-region (point) begpos) | |
2676 | (indent-to indent)) | |
2677 | ;; An existing comment? | |
2678 | (if cpos | |
2679 | (progn (goto-char cpos) | |
2680 | (set-marker cpos nil)) | |
2681 | ;; No, insert one. | |
2682 | (insert starter) | |
2683 | (save-excursion | |
2684 | (insert ender)))))))) | |
2076c87c JB |
2685 | |
2686 | (defun set-comment-column (arg) | |
2687 | "Set the comment column based on point. | |
2688 | With no arg, set the comment column to the current column. | |
2689 | With just minus as arg, kill any comment on this line. | |
2690 | With any other arg, set comment column to indentation of the previous comment | |
2691 | and then align or create a comment on this line at that column." | |
2692 | (interactive "P") | |
2693 | (if (eq arg '-) | |
2694 | (kill-comment nil) | |
2695 | (if arg | |
2696 | (progn | |
2697 | (save-excursion | |
2698 | (beginning-of-line) | |
2699 | (re-search-backward comment-start-skip) | |
2700 | (beginning-of-line) | |
2701 | (re-search-forward comment-start-skip) | |
2702 | (goto-char (match-beginning 0)) | |
2703 | (setq comment-column (current-column)) | |
2704 | (message "Comment column set to %d" comment-column)) | |
2705 | (indent-for-comment)) | |
2706 | (setq comment-column (current-column)) | |
2707 | (message "Comment column set to %d" comment-column)))) | |
2708 | ||
2709 | (defun kill-comment (arg) | |
2710 | "Kill the comment on this line, if any. | |
2711 | With argument, kill comments on that many lines starting with this one." | |
2712 | ;; this function loses in a lot of situations. it incorrectly recognises | |
2713 | ;; comment delimiters sometimes (ergo, inside a string), doesn't work | |
2714 | ;; with multi-line comments, can kill extra whitespace if comment wasn't | |
2715 | ;; through end-of-line, et cetera. | |
2716 | (interactive "P") | |
2717 | (or comment-start-skip (error "No comment syntax defined")) | |
2718 | (let ((count (prefix-numeric-value arg)) endc) | |
2719 | (while (> count 0) | |
2720 | (save-excursion | |
2721 | (end-of-line) | |
2722 | (setq endc (point)) | |
2723 | (beginning-of-line) | |
2724 | (and (string< "" comment-end) | |
2725 | (setq endc | |
2726 | (progn | |
2727 | (re-search-forward (regexp-quote comment-end) endc 'move) | |
2728 | (skip-chars-forward " \t") | |
2729 | (point)))) | |
2730 | (beginning-of-line) | |
2731 | (if (re-search-forward comment-start-skip endc t) | |
2732 | (progn | |
2733 | (goto-char (match-beginning 0)) | |
2734 | (skip-chars-backward " \t") | |
2735 | (kill-region (point) endc) | |
2736 | ;; to catch comments a line beginnings | |
2737 | (indent-according-to-mode)))) | |
2738 | (if arg (forward-line 1)) | |
2739 | (setq count (1- count))))) | |
2740 | ||
3fb7b588 KH |
2741 | (defvar comment-padding 1 |
2742 | "Number of spaces `comment-region' puts between comment chars and text. | |
2743 | ||
2744 | Extra spacing between the comment characters and the comment text | |
2745 | makes the comment easier to read. Default is 1. Nil means 0 and is | |
2746 | more efficient.") | |
2747 | ||
2076c87c | 2748 | (defun comment-region (beg end &optional arg) |
f28039bb RS |
2749 | "Comment or uncomment each line in the region. |
2750 | With just C-u prefix arg, uncomment each line in region. | |
2751 | Numeric prefix arg ARG means use ARG comment characters. | |
2076c87c JB |
2752 | If ARG is negative, delete that many comment characters instead. |
2753 | Comments are terminated on each line, even for syntax in which newline does | |
2754 | not end the comment. Blank lines do not get comments." | |
2755 | ;; if someone wants it to only put a comment-start at the beginning and | |
2756 | ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x | |
2757 | ;; is easy enough. No option is made here for other than commenting | |
2758 | ;; every line. | |
f28039bb | 2759 | (interactive "r\nP") |
2076c87c JB |
2760 | (or comment-start (error "No comment syntax is defined")) |
2761 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | |
2762 | (save-excursion | |
2763 | (save-restriction | |
f28039bb RS |
2764 | (let ((cs comment-start) (ce comment-end) |
2765 | numarg) | |
2766 | (if (consp arg) (setq numarg t) | |
2767 | (setq numarg (prefix-numeric-value arg)) | |
2768 | ;; For positive arg > 1, replicate the comment delims now, | |
2769 | ;; then insert the replicated strings just once. | |
2770 | (while (> numarg 1) | |
2771 | (setq cs (concat cs comment-start) | |
2772 | ce (concat ce comment-end)) | |
2773 | (setq numarg (1- numarg)))) | |
3fb7b588 KH |
2774 | (when comment-padding |
2775 | (setq cs (concat cs (make-string comment-padding ? )))) | |
f28039bb | 2776 | ;; Loop over all lines from BEG to END. |
2076c87c JB |
2777 | (narrow-to-region beg end) |
2778 | (goto-char beg) | |
2779 | (while (not (eobp)) | |
f28039bb RS |
2780 | (if (or (eq numarg t) (< numarg 0)) |
2781 | (progn | |
2782 | ;; Delete comment start from beginning of line. | |
2783 | (if (eq numarg t) | |
2784 | (while (looking-at (regexp-quote cs)) | |
2785 | (delete-char (length cs))) | |
2786 | (let ((count numarg)) | |
2787 | (while (and (> 1 (setq count (1+ count))) | |
2788 | (looking-at (regexp-quote cs))) | |
2789 | (delete-char (length cs))))) | |
2790 | ;; Delete comment end from end of line. | |
2791 | (if (string= "" ce) | |
2792 | nil | |
2793 | (if (eq numarg t) | |
2794 | (progn | |
2795 | (end-of-line) | |
2796 | ;; This is questionable if comment-end ends in | |
2797 | ;; whitespace. That is pretty brain-damaged, | |
2798 | ;; though. | |
5c8ddcfb RS |
2799 | (while (progn (skip-chars-backward " \t") |
2800 | (and (>= (- (point) (point-min)) (length ce)) | |
2801 | (save-excursion | |
2802 | (backward-char (length ce)) | |
2803 | (looking-at (regexp-quote ce))))) | |
f28039bb | 2804 | (delete-char (- (length ce))))) |
ee095968 RS |
2805 | (let ((count numarg)) |
2806 | (while (> 1 (setq count (1+ count))) | |
2807 | (end-of-line) | |
2808 | ;; this is questionable if comment-end ends in whitespace | |
2809 | ;; that is pretty brain-damaged though | |
2810 | (skip-chars-backward " \t") | |
e1f0a6d2 RS |
2811 | (if (>= (- (point) (point-min)) (length ce)) |
2812 | (save-excursion | |
2813 | (backward-char (length ce)) | |
2814 | (if (looking-at (regexp-quote ce)) | |
2815 | (delete-char (length ce))))))))) | |
6e88ed49 | 2816 | (forward-line 1)) |
f28039bb | 2817 | ;; Insert at beginning and at end. |
2076c87c JB |
2818 | (if (looking-at "[ \t]*$") () |
2819 | (insert cs) | |
2820 | (if (string= "" ce) () | |
2821 | (end-of-line) | |
2822 | (insert ce))) | |
2823 | (search-forward "\n" nil 'move))))))) | |
2824 | \f | |
2825 | (defun backward-word (arg) | |
2826 | "Move backward until encountering the end of a word. | |
2827 | With argument, do this that many times. | |
ff1fbe3e | 2828 | In programs, it is faster to call `forward-word' with negative arg." |
9e50756b | 2829 | (interactive "p") |
2076c87c JB |
2830 | (forward-word (- arg))) |
2831 | ||
2832 | (defun mark-word (arg) | |
2833 | "Set mark arg words away from point." | |
2834 | (interactive "p") | |
2835 | (push-mark | |
2836 | (save-excursion | |
2837 | (forward-word arg) | |
fd0f4056 RS |
2838 | (point)) |
2839 | nil t)) | |
2076c87c JB |
2840 | |
2841 | (defun kill-word (arg) | |
2842 | "Kill characters forward until encountering the end of a word. | |
2843 | With argument, do this that many times." | |
2844 | (interactive "p") | |
e6291fe1 | 2845 | (kill-region (point) (progn (forward-word arg) (point)))) |
2076c87c JB |
2846 | |
2847 | (defun backward-kill-word (arg) | |
2848 | "Kill characters backward until encountering the end of a word. | |
2849 | With argument, do this that many times." | |
2850 | (interactive "p") | |
2851 | (kill-word (- arg))) | |
d7c64071 | 2852 | |
1e8c5ac4 RS |
2853 | (defun current-word (&optional strict) |
2854 | "Return the word point is on (or a nearby word) as a string. | |
2855 | If optional arg STRICT is non-nil, return nil unless point is within | |
2856 | or adjacent to a word." | |
d7c64071 ER |
2857 | (save-excursion |
2858 | (let ((oldpoint (point)) (start (point)) (end (point))) | |
2859 | (skip-syntax-backward "w_") (setq start (point)) | |
2860 | (goto-char oldpoint) | |
2861 | (skip-syntax-forward "w_") (setq end (point)) | |
2862 | (if (and (eq start oldpoint) (eq end oldpoint)) | |
1e8c5ac4 RS |
2863 | ;; Point is neither within nor adjacent to a word. |
2864 | (and (not strict) | |
2865 | (progn | |
2866 | ;; Look for preceding word in same line. | |
2867 | (skip-syntax-backward "^w_" | |
2868 | (save-excursion (beginning-of-line) | |
2869 | (point))) | |
2870 | (if (bolp) | |
2871 | ;; No preceding word in same line. | |
2872 | ;; Look for following word in same line. | |
2873 | (progn | |
2874 | (skip-syntax-forward "^w_" | |
2875 | (save-excursion (end-of-line) | |
2876 | (point))) | |
2877 | (setq start (point)) | |
2878 | (skip-syntax-forward "w_") | |
2879 | (setq end (point))) | |
2880 | (setq end (point)) | |
2881 | (skip-syntax-backward "w_") | |
2882 | (setq start (point))) | |
020db25f RS |
2883 | (buffer-substring-no-properties start end))) |
2884 | (buffer-substring-no-properties start end))))) | |
2076c87c | 2885 | \f |
69c1dd37 | 2886 | (defcustom fill-prefix nil |
2076c87c | 2887 | "*String for filling to insert at front of new line, or nil for none. |
69c1dd37 RS |
2888 | Setting this variable automatically makes it local to the current buffer." |
2889 | :type '(choice (const :tag "None" nil) | |
2890 | string) | |
2891 | :group 'fill) | |
2076c87c JB |
2892 | (make-variable-buffer-local 'fill-prefix) |
2893 | ||
69c1dd37 RS |
2894 | (defcustom auto-fill-inhibit-regexp nil |
2895 | "*Regexp to match lines which should not be auto-filled." | |
2896 | :type '(choice (const :tag "None" nil) | |
2897 | regexp) | |
2898 | :group 'fill) | |
2076c87c | 2899 | |
b3ac9fa9 RS |
2900 | (defvar comment-line-break-function 'indent-new-comment-line |
2901 | "*Mode-specific function which line breaks and continues a comment. | |
2902 | ||
2903 | This function is only called during auto-filling of a comment section. | |
2904 | The function should take a single optional argument, which is a flag | |
2905 | indicating whether it should use soft newlines. | |
2906 | ||
2907 | Setting this variable automatically makes it local to the current buffer.") | |
2908 | ||
dbe524b6 | 2909 | ;; This function is used as the auto-fill-function of a buffer |
e2504204 KH |
2910 | ;; when Auto-Fill mode is enabled. |
2911 | ;; It returns t if it really did any work. | |
dbe524b6 RS |
2912 | ;; (Actually some major modes use a different auto-fill function, |
2913 | ;; but this one is the default one.) | |
2076c87c | 2914 | (defun do-auto-fill () |
a0170800 RS |
2915 | (let (fc justify bol give-up |
2916 | (fill-prefix fill-prefix)) | |
c18465c4 | 2917 | (if (or (not (setq justify (current-justification))) |
8f066a20 RS |
2918 | (null (setq fc (current-fill-column))) |
2919 | (and (eq justify 'left) | |
2920 | (<= (current-column) fc)) | |
eed5698b RS |
2921 | (save-excursion (beginning-of-line) |
2922 | (setq bol (point)) | |
2923 | (and auto-fill-inhibit-regexp | |
2924 | (looking-at auto-fill-inhibit-regexp)))) | |
2925 | nil ;; Auto-filling not required | |
3db1e3b5 BG |
2926 | (if (memq justify '(full center right)) |
2927 | (save-excursion (unjustify-current-line))) | |
a0170800 RS |
2928 | |
2929 | ;; Choose a fill-prefix automatically. | |
2930 | (if (and adaptive-fill-mode | |
2931 | (or (null fill-prefix) (string= fill-prefix ""))) | |
e4b62d7c RS |
2932 | (let ((prefix |
2933 | (fill-context-prefix | |
2934 | (save-excursion (backward-paragraph 1) (point)) | |
50be475d | 2935 | (save-excursion (forward-paragraph 1) (point))))) |
e4b62d7c RS |
2936 | (and prefix (not (equal prefix "")) |
2937 | (setq fill-prefix prefix)))) | |
a0170800 | 2938 | |
eed5698b | 2939 | (while (and (not give-up) (> (current-column) fc)) |
e47d38f6 | 2940 | ;; Determine where to split the line. |
db893d00 RS |
2941 | (let* (after-prefix |
2942 | (fill-point | |
2943 | (let ((opoint (point)) | |
2944 | bounce | |
2945 | (first t)) | |
2946 | (save-excursion | |
2947 | (beginning-of-line) | |
2948 | (setq after-prefix (point)) | |
2949 | (and fill-prefix | |
2950 | (looking-at (regexp-quote fill-prefix)) | |
2951 | (setq after-prefix (match-end 0))) | |
2952 | (move-to-column (1+ fc)) | |
dbe524b6 RS |
2953 | ;; Move back to the point where we can break the line. |
2954 | ;; We break the line between word or | |
db893d00 RS |
2955 | ;; after/before the character which has character |
2956 | ;; category `|'. We search space, \c| followed by | |
dbe524b6 | 2957 | ;; a character, or \c| following a character. If |
db893d00 RS |
2958 | ;; not found, place the point at beginning of line. |
2959 | (while (or first | |
2960 | ;; If this is after period and a single space, | |
2961 | ;; move back once more--we don't want to break | |
2962 | ;; the line there and make it look like a | |
2963 | ;; sentence end. | |
2964 | (and (not (bobp)) | |
2965 | (not bounce) | |
2966 | sentence-end-double-space | |
2967 | (save-excursion (forward-char -1) | |
2968 | (and (looking-at "\\. ") | |
dbe524b6 RS |
2969 | (not (looking-at "\\. "))))) |
2970 | (and (not (bobp)) | |
2971 | (not bounce) | |
2972 | fill-nobreak-predicate | |
2973 | (funcall fill-nobreak-predicate))) | |
db893d00 RS |
2974 | (setq first nil) |
2975 | (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^") | |
2976 | ;; If we find nowhere on the line to break it, | |
2977 | ;; break after one word. Set bounce to t | |
2978 | ;; so we will not keep going in this while loop. | |
2979 | (if (<= (point) after-prefix) | |
2980 | (progn | |
2981 | (goto-char after-prefix) | |
2982 | (re-search-forward "[ \t]" opoint t) | |
2983 | (setq bounce t)) | |
2984 | (if (looking-at "[ \t]") | |
2985 | ;; Break the line at word boundary. | |
2986 | (skip-chars-backward " \t") | |
2987 | ;; Break the line after/before \c|. | |
2988 | (forward-char 1)))) | |
22c47bc5 KH |
2989 | (if enable-multibyte-characters |
2990 | (let ((charset (charset-after (1- (point))))) | |
2991 | (if (eq charset 'ascii) | |
2992 | (setq charset (charset-after (point)))) | |
2993 | (if (not (eq charset 'ascii)) | |
2994 | (fill-find-break-point charset after-prefix)))) | |
db893d00 | 2995 | ;; Let fill-point be set to the place where we end up. |
4b9c8a06 RS |
2996 | ;; But move back before any whitespace here. |
2997 | (skip-chars-backward " \t") | |
db893d00 RS |
2998 | (point))))) |
2999 | ||
3000 | ;; See whether the place we found is any good. | |
e47d38f6 RS |
3001 | (if (save-excursion |
3002 | (goto-char fill-point) | |
07f458c1 | 3003 | (and (not (bolp)) |
db893d00 RS |
3004 | ;; There is no use breaking at end of line. |
3005 | (not (save-excursion (skip-chars-forward " ") (eolp))) | |
3006 | ;; It is futile to split at the end of the prefix | |
3007 | ;; since we would just insert the prefix again. | |
3008 | (not (and after-prefix (<= (point) after-prefix))) | |
07f458c1 RS |
3009 | ;; Don't split right after a comment starter |
3010 | ;; since we would just make another comment starter. | |
3011 | (not (and comment-start-skip | |
3012 | (let ((limit (point))) | |
3013 | (beginning-of-line) | |
3014 | (and (re-search-forward comment-start-skip | |
3015 | limit t) | |
3016 | (eq (point) limit))))))) | |
db893d00 | 3017 | ;; Ok, we have a useful place to break the line. Do it. |
e47d38f6 RS |
3018 | (let ((prev-column (current-column))) |
3019 | ;; If point is at the fill-point, do not `save-excursion'. | |
3020 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
3021 | ;; point will end up before it rather than after it. | |
3022 | (if (save-excursion | |
3023 | (skip-chars-backward " \t") | |
3024 | (= (point) fill-point)) | |
b3ac9fa9 | 3025 | (funcall comment-line-break-function t) |
e47d38f6 RS |
3026 | (save-excursion |
3027 | (goto-char fill-point) | |
b3ac9fa9 | 3028 | (funcall comment-line-break-function t))) |
e47d38f6 RS |
3029 | ;; Now do justification, if required |
3030 | (if (not (eq justify 'left)) | |
3031 | (save-excursion | |
3032 | (end-of-line 0) | |
3033 | (justify-current-line justify nil t))) | |
3034 | ;; If making the new line didn't reduce the hpos of | |
3035 | ;; the end of the line, then give up now; | |
3036 | ;; trying again will not help. | |
3037 | (if (>= (current-column) prev-column) | |
3038 | (setq give-up t))) | |
db893d00 | 3039 | ;; No good place to break => stop trying. |
e47d38f6 | 3040 | (setq give-up t)))) |
24ebf92e | 3041 | ;; Justify last line. |
e2504204 KH |
3042 | (justify-current-line justify t t) |
3043 | t))) | |
2076c87c | 3044 | |
24ebf92e RS |
3045 | (defvar normal-auto-fill-function 'do-auto-fill |
3046 | "The function to use for `auto-fill-function' if Auto Fill mode is turned on. | |
3047 | Some major modes set this.") | |
3048 | ||
d7465b15 | 3049 | (defun auto-fill-mode (&optional arg) |
24ebf92e RS |
3050 | "Toggle Auto Fill mode. |
3051 | With arg, turn Auto Fill mode on if and only if arg is positive. | |
3052 | In Auto Fill mode, inserting a space at a column beyond `current-fill-column' | |
3053 | automatically breaks the line at a previous space. | |
3054 | ||
3055 | The value of `normal-auto-fill-function' specifies the function to use | |
3056 | for `auto-fill-function' when turning Auto Fill mode on." | |
d7465b15 RS |
3057 | (interactive "P") |
3058 | (prog1 (setq auto-fill-function | |
3059 | (if (if (null arg) | |
3060 | (not auto-fill-function) | |
3061 | (> (prefix-numeric-value arg) 0)) | |
24ebf92e | 3062 | normal-auto-fill-function |
d7465b15 | 3063 | nil)) |
7911ecc8 | 3064 | (force-mode-line-update))) |
d7465b15 RS |
3065 | |
3066 | ;; This holds a document string used to document auto-fill-mode. | |
3067 | (defun auto-fill-function () | |
3068 | "Automatically break line at a previous space, in insertion of text." | |
3069 | nil) | |
3070 | ||
3071 | (defun turn-on-auto-fill () | |
3072 | "Unconditionally turn on Auto Fill mode." | |
3073 | (auto-fill-mode 1)) | |
3074 | ||
3075 | (defun set-fill-column (arg) | |
4cc0ea11 | 3076 | "Set `fill-column' to specified argument. |
923efb99 | 3077 | Use \\[universal-argument] followed by a number to specify a column. |
4cc0ea11 | 3078 | Just \\[universal-argument] as argument means to use the current column." |
d7465b15 | 3079 | (interactive "P") |
f4520363 RS |
3080 | (if (consp arg) |
3081 | (setq arg (current-column))) | |
3082 | (if (not (integerp arg)) | |
3083 | ;; Disallow missing argument; it's probably a typo for C-x C-f. | |
3084 | (error "set-fill-column requires an explicit argument") | |
3085 | (message "Fill column set to %d (was %d)" arg fill-column) | |
3086 | (setq fill-column arg))) | |
d7465b15 | 3087 | \f |
69c1dd37 | 3088 | (defcustom comment-multi-line nil |
2076c87c | 3089 | "*Non-nil means \\[indent-new-comment-line] should continue same comment |
c88ab9ce | 3090 | on new line, with no new terminator or starter. |
69c1dd37 RS |
3091 | This is obsolete because you might as well use \\[newline-and-indent]." |
3092 | :type 'boolean | |
3093 | :group 'fill-comments) | |
2076c87c | 3094 | |
28191e20 | 3095 | (defun indent-new-comment-line (&optional soft) |
d7465b15 RS |
3096 | "Break line at point and indent, continuing comment if within one. |
3097 | This indents the body of the continued comment | |
3098 | under the previous comment line. | |
c88ab9ce ER |
3099 | |
3100 | This command is intended for styles where you write a comment per line, | |
3101 | starting a new comment (and terminating it if necessary) on each line. | |
28191e20 RS |
3102 | If you want to continue one comment across several lines, use \\[newline-and-indent]. |
3103 | ||
3a0c4755 RS |
3104 | If a fill column is specified, it overrides the use of the comment column |
3105 | or comment indentation. | |
3106 | ||
28191e20 RS |
3107 | The inserted newline is marked hard if `use-hard-newlines' is true, |
3108 | unless optional argument SOFT is non-nil." | |
3109 | (interactive) | |
2076c87c JB |
3110 | (let (comcol comstart) |
3111 | (skip-chars-backward " \t") | |
3112 | (delete-region (point) | |
3113 | (progn (skip-chars-forward " \t") | |
3114 | (point))) | |
eed5698b | 3115 | (if soft (insert-and-inherit ?\n) (newline 1)) |
3a0c4755 RS |
3116 | (if fill-prefix |
3117 | (progn | |
3118 | (indent-to-left-margin) | |
3119 | (insert-and-inherit fill-prefix)) | |
3120 | (if (not comment-multi-line) | |
2076c87c | 3121 | (save-excursion |
3a0c4755 RS |
3122 | (if (and comment-start-skip |
3123 | (let ((opoint (point))) | |
3124 | (forward-line -1) | |
3125 | (re-search-forward comment-start-skip opoint t))) | |
3126 | ;; The old line is a comment. | |
3127 | ;; Set WIN to the pos of the comment-start. | |
3128 | ;; But if the comment is empty, look at preceding lines | |
3129 | ;; to find one that has a nonempty comment. | |
3130 | ||
3131 | ;; If comment-start-skip contains a \(...\) pair, | |
3132 | ;; the real comment delimiter starts at the end of that pair. | |
3133 | (let ((win (or (match-end 1) (match-beginning 0)))) | |
3134 | (while (and (eolp) (not (bobp)) | |
3135 | (let (opoint) | |
3136 | (beginning-of-line) | |
3137 | (setq opoint (point)) | |
3138 | (forward-line -1) | |
3139 | (re-search-forward comment-start-skip opoint t))) | |
3140 | (setq win (or (match-end 1) (match-beginning 0)))) | |
3141 | ;; Indent this line like what we found. | |
3142 | (goto-char win) | |
3143 | (setq comcol (current-column)) | |
3144 | (setq comstart | |
3145 | (buffer-substring (point) (match-end 0))))))) | |
3146 | (if comcol | |
3147 | (let ((comment-column comcol) | |
3148 | (comment-start comstart) | |
3149 | (comment-end comment-end)) | |
3150 | (and comment-end (not (equal comment-end "")) | |
3151 | ; (if (not comment-multi-line) | |
3152 | (progn | |
3153 | (forward-char -1) | |
3154 | (insert comment-end) | |
3155 | (forward-char 1)) | |
3156 | ; (setq comment-column (+ comment-column (length comment-start)) | |
3157 | ; comment-start "") | |
3158 | ; ) | |
3159 | ) | |
3160 | (if (not (eolp)) | |
3161 | (setq comment-end "")) | |
3162 | (insert-and-inherit ?\n) | |
3163 | (forward-char -1) | |
3164 | (indent-for-comment) | |
3165 | (save-excursion | |
3166 | ;; Make sure we delete the newline inserted above. | |
3167 | (end-of-line) | |
3168 | (delete-char 1))) | |
3169 | (indent-according-to-mode))))) | |
2076c87c JB |
3170 | \f |
3171 | (defun set-selective-display (arg) | |
ff1fbe3e RS |
3172 | "Set `selective-display' to ARG; clear it if no arg. |
3173 | When the value of `selective-display' is a number > 0, | |
3174 | lines whose indentation is >= that value are not displayed. | |
3175 | The variable `selective-display' has a separate value for each buffer." | |
2076c87c JB |
3176 | (interactive "P") |
3177 | (if (eq selective-display t) | |
3178 | (error "selective-display already in use for marked lines")) | |
c88ab9ce ER |
3179 | (let ((current-vpos |
3180 | (save-restriction | |
3181 | (narrow-to-region (point-min) (point)) | |
3182 | (goto-char (window-start)) | |
3183 | (vertical-motion (window-height))))) | |
3184 | (setq selective-display | |
3185 | (and arg (prefix-numeric-value arg))) | |
3186 | (recenter current-vpos)) | |
2076c87c JB |
3187 | (set-window-start (selected-window) (window-start (selected-window))) |
3188 | (princ "selective-display set to " t) | |
3189 | (prin1 selective-display t) | |
3190 | (princ "." t)) | |
3191 | ||
4f8f7f9f | 3192 | (defvar overwrite-mode-textual " Ovwrt" |
b6a22db0 | 3193 | "The string displayed in the mode line when in overwrite mode.") |
4f8f7f9f | 3194 | (defvar overwrite-mode-binary " Bin Ovwrt" |
b6a22db0 JB |
3195 | "The string displayed in the mode line when in binary overwrite mode.") |
3196 | ||
2076c87c JB |
3197 | (defun overwrite-mode (arg) |
3198 | "Toggle overwrite mode. | |
3199 | With arg, turn overwrite mode on iff arg is positive. | |
3200 | In overwrite mode, printing characters typed in replace existing text | |
b6a22db0 JB |
3201 | on a one-for-one basis, rather than pushing it to the right. At the |
3202 | end of a line, such characters extend the line. Before a tab, | |
3203 | such characters insert until the tab is filled in. | |
3204 | \\[quoted-insert] still inserts characters in overwrite mode; this | |
3205 | is supposed to make it easier to insert characters when necessary." | |
3206 | (interactive "P") | |
3207 | (setq overwrite-mode | |
3208 | (if (if (null arg) (not overwrite-mode) | |
3209 | (> (prefix-numeric-value arg) 0)) | |
3210 | 'overwrite-mode-textual)) | |
3211 | (force-mode-line-update)) | |
3212 | ||
3213 | (defun binary-overwrite-mode (arg) | |
3214 | "Toggle binary overwrite mode. | |
3215 | With arg, turn binary overwrite mode on iff arg is positive. | |
3216 | In binary overwrite mode, printing characters typed in replace | |
3217 | existing text. Newlines are not treated specially, so typing at the | |
3218 | end of a line joins the line to the next, with the typed character | |
3219 | between them. Typing before a tab character simply replaces the tab | |
3220 | with the character typed. | |
3221 | \\[quoted-insert] replaces the text at the cursor, just as ordinary | |
3222 | typing characters do. | |
3223 | ||
3224 | Note that binary overwrite mode is not its own minor mode; it is a | |
3225 | specialization of overwrite-mode, entered by setting the | |
3226 | `overwrite-mode' variable to `overwrite-mode-binary'." | |
2076c87c JB |
3227 | (interactive "P") |
3228 | (setq overwrite-mode | |
b6a22db0 | 3229 | (if (if (null arg) |
a61099dd | 3230 | (not (eq overwrite-mode 'overwrite-mode-binary)) |
b6a22db0 JB |
3231 | (> (prefix-numeric-value arg) 0)) |
3232 | 'overwrite-mode-binary)) | |
3233 | (force-mode-line-update)) | |
2076c87c | 3234 | \f |
69c1dd37 RS |
3235 | (defcustom line-number-mode t |
3236 | "*Non-nil means display line number in mode line." | |
3237 | :type 'boolean | |
3238 | :group 'editing-basics) | |
a61099dd RS |
3239 | |
3240 | (defun line-number-mode (arg) | |
3241 | "Toggle Line Number mode. | |
3242 | With arg, turn Line Number mode on iff arg is positive. | |
3243 | When Line Number mode is enabled, the line number appears | |
3244 | in the mode line." | |
3245 | (interactive "P") | |
3246 | (setq line-number-mode | |
3247 | (if (null arg) (not line-number-mode) | |
3248 | (> (prefix-numeric-value arg) 0))) | |
3249 | (force-mode-line-update)) | |
3250 | ||
69c1dd37 RS |
3251 | (defcustom column-number-mode nil |
3252 | "*Non-nil means display column number in mode line." | |
3253 | :type 'boolean | |
3254 | :group 'editing-basics) | |
bcad4985 KH |
3255 | |
3256 | (defun column-number-mode (arg) | |
3257 | "Toggle Column Number mode. | |
3258 | With arg, turn Column Number mode on iff arg is positive. | |
3259 | When Column Number mode is enabled, the column number appears | |
3260 | in the mode line." | |
3261 | (interactive "P") | |
3262 | (setq column-number-mode | |
3263 | (if (null arg) (not column-number-mode) | |
3264 | (> (prefix-numeric-value arg) 0))) | |
3265 | (force-mode-line-update)) | |
3266 | ||
4b384a8f | 3267 | (defgroup paren-blinking nil |
020db25f | 3268 | "Blinking matching of parens and expressions." |
4b384a8f SM |
3269 | :prefix "blink-matching-" |
3270 | :group 'paren-matching) | |
3271 | ||
69c1dd37 RS |
3272 | (defcustom blink-matching-paren t |
3273 | "*Non-nil means show matching open-paren when close-paren is inserted." | |
3274 | :type 'boolean | |
4b384a8f | 3275 | :group 'paren-blinking) |
2076c87c | 3276 | |
69c1dd37 | 3277 | (defcustom blink-matching-paren-on-screen t |
29fc44dd | 3278 | "*Non-nil means show matching open-paren when it is on screen. |
4b384a8f SM |
3279 | If nil, means don't show it (but the open-paren can still be shown |
3280 | when it is off screen)." | |
69c1dd37 | 3281 | :type 'boolean |
4b384a8f | 3282 | :group 'paren-blinking) |
29fc44dd | 3283 | |
4b384a8f | 3284 | (defcustom blink-matching-paren-distance (* 25 1024) |
69c1dd37 RS |
3285 | "*If non-nil, is maximum distance to search for matching open-paren." |
3286 | :type 'integer | |
4b384a8f | 3287 | :group 'paren-blinking) |
2076c87c | 3288 | |
69c1dd37 | 3289 | (defcustom blink-matching-delay 1 |
4b384a8f SM |
3290 | "*Time in seconds to delay after showing a matching paren." |
3291 | :type 'number | |
3292 | :group 'paren-blinking) | |
72dddf8b | 3293 | |
69c1dd37 | 3294 | (defcustom blink-matching-paren-dont-ignore-comments nil |
4b384a8f | 3295 | "*Non-nil means `blink-matching-paren' will not ignore comments." |
69c1dd37 | 3296 | :type 'boolean |
4b384a8f | 3297 | :group 'paren-blinking) |
903b7f65 | 3298 | |
2076c87c JB |
3299 | (defun blink-matching-open () |
3300 | "Move cursor momentarily to the beginning of the sexp before point." | |
3301 | (interactive) | |
3302 | (and (> (point) (1+ (point-min))) | |
2076c87c | 3303 | blink-matching-paren |
7e1ddd45 RS |
3304 | ;; Verify an even number of quoting characters precede the close. |
3305 | (= 1 (logand 1 (- (point) | |
3306 | (save-excursion | |
3307 | (forward-char -1) | |
3308 | (skip-syntax-backward "/\\") | |
3309 | (point))))) | |
2076c87c JB |
3310 | (let* ((oldpos (point)) |
3311 | (blinkpos) | |
3312 | (mismatch)) | |
3313 | (save-excursion | |
3314 | (save-restriction | |
3315 | (if blink-matching-paren-distance | |
3316 | (narrow-to-region (max (point-min) | |
3317 | (- (point) blink-matching-paren-distance)) | |
3318 | oldpos)) | |
3319 | (condition-case () | |
903b7f65 RS |
3320 | (let ((parse-sexp-ignore-comments |
3321 | (and parse-sexp-ignore-comments | |
3322 | (not blink-matching-paren-dont-ignore-comments)))) | |
3323 | (setq blinkpos (scan-sexps oldpos -1))) | |
2076c87c | 3324 | (error nil))) |
903b7f65 RS |
3325 | (and blinkpos |
3326 | (/= (char-syntax (char-after blinkpos)) | |
3327 | ?\$) | |
2076c87c | 3328 | (setq mismatch |
903b7f65 RS |
3329 | (or (null (matching-paren (char-after blinkpos))) |
3330 | (/= (char-after (1- oldpos)) | |
3331 | (matching-paren (char-after blinkpos)))))) | |
2076c87c JB |
3332 | (if mismatch (setq blinkpos nil)) |
3333 | (if blinkpos | |
a117eaee KH |
3334 | ;; Don't log messages about paren matching. |
3335 | (let (message-log-max) | |
2076c87c JB |
3336 | (goto-char blinkpos) |
3337 | (if (pos-visible-in-window-p) | |
29fc44dd KH |
3338 | (and blink-matching-paren-on-screen |
3339 | (sit-for blink-matching-delay)) | |
2076c87c JB |
3340 | (goto-char blinkpos) |
3341 | (message | |
3342 | "Matches %s" | |
e9f1d66d | 3343 | ;; Show what precedes the open in its line, if anything. |
2076c87c JB |
3344 | (if (save-excursion |
3345 | (skip-chars-backward " \t") | |
3346 | (not (bolp))) | |
3347 | (buffer-substring (progn (beginning-of-line) (point)) | |
3348 | (1+ blinkpos)) | |
e9f1d66d RS |
3349 | ;; Show what follows the open in its line, if anything. |
3350 | (if (save-excursion | |
3351 | (forward-char 1) | |
3352 | (skip-chars-forward " \t") | |
3353 | (not (eolp))) | |
3354 | (buffer-substring blinkpos | |
3355 | (progn (end-of-line) (point))) | |
267935b9 RS |
3356 | ;; Otherwise show the previous nonblank line, |
3357 | ;; if there is one. | |
3358 | (if (save-excursion | |
3359 | (skip-chars-backward "\n \t") | |
3360 | (not (bobp))) | |
3361 | (concat | |
3362 | (buffer-substring (progn | |
3363 | (skip-chars-backward "\n \t") | |
3364 | (beginning-of-line) | |
3365 | (point)) | |
3366 | (progn (end-of-line) | |
3367 | (skip-chars-backward " \t") | |
3368 | (point))) | |
3369 | ;; Replace the newline and other whitespace with `...'. | |
3370 | "..." | |
3371 | (buffer-substring blinkpos (1+ blinkpos))) | |
3372 | ;; There is nothing to show except the char itself. | |
3373 | (buffer-substring blinkpos (1+ blinkpos)))))))) | |
2076c87c JB |
3374 | (cond (mismatch |
3375 | (message "Mismatched parentheses")) | |
3376 | ((not blink-matching-paren-distance) | |
3377 | (message "Unmatched parenthesis")))))))) | |
3378 | ||
3379 | ;Turned off because it makes dbx bomb out. | |
3380 | (setq blink-paren-function 'blink-matching-open) | |
3381 | ||
9a1277dd RS |
3382 | ;; This executes C-g typed while Emacs is waiting for a command. |
3383 | ;; Quitting out of a program does not go through here; | |
3384 | ;; that happens in the QUIT macro at the C code level. | |
2076c87c | 3385 | (defun keyboard-quit () |
d5dae4e1 | 3386 | "Signal a `quit' condition. |
af39530e RS |
3387 | During execution of Lisp code, this character causes a quit directly. |
3388 | At top-level, as an editor command, this simply beeps." | |
2076c87c | 3389 | (interactive) |
19d35374 | 3390 | (deactivate-mark) |
2076c87c JB |
3391 | (signal 'quit nil)) |
3392 | ||
3393 | (define-key global-map "\C-g" 'keyboard-quit) | |
c66587fe | 3394 | |
1c6c6fde RS |
3395 | (defvar buffer-quit-function nil |
3396 | "Function to call to \"quit\" the current buffer, or nil if none. | |
3397 | \\[keyboard-escape-quit] calls this function when its more local actions | |
3398 | \(such as cancelling a prefix argument, minibuffer or region) do not apply.") | |
3399 | ||
c66587fe RS |
3400 | (defun keyboard-escape-quit () |
3401 | "Exit the current \"mode\" (in a generalized sense of the word). | |
3402 | This command can exit an interactive command such as `query-replace', | |
3403 | can clear out a prefix argument or a region, | |
3404 | can get out of the minibuffer or other recursive edit, | |
1c6c6fde RS |
3405 | cancel the use of the current buffer (for special-purpose buffers), |
3406 | or go back to just one window (by deleting all but the selected window)." | |
c66587fe RS |
3407 | (interactive) |
3408 | (cond ((eq last-command 'mode-exited) nil) | |
3409 | ((> (minibuffer-depth) 0) | |
3410 | (abort-recursive-edit)) | |
3411 | (current-prefix-arg | |
3412 | nil) | |
3413 | ((and transient-mark-mode | |
3414 | mark-active) | |
3415 | (deactivate-mark)) | |
1b657835 RS |
3416 | ((> (recursion-depth) 0) |
3417 | (exit-recursive-edit)) | |
1c6c6fde RS |
3418 | (buffer-quit-function |
3419 | (funcall buffer-quit-function)) | |
c66587fe | 3420 | ((not (one-window-p t)) |
1b657835 RS |
3421 | (delete-other-windows)) |
3422 | ((string-match "^ \\*" (buffer-name (current-buffer))) | |
3423 | (bury-buffer)))) | |
c66587fe | 3424 | |
1c6c6fde | 3425 | (define-key global-map "\e\e\e" 'keyboard-escape-quit) |
2076c87c | 3426 | \f |
69c1dd37 | 3427 | (defcustom mail-user-agent 'sendmail-user-agent |
a31ca314 RS |
3428 | "*Your preference for a mail composition package. |
3429 | Various Emacs Lisp packages (e.g. reporter) require you to compose an | |
3430 | outgoing email message. This variable lets you specify which | |
3431 | mail-sending package you prefer. | |
3432 | ||
3433 | Valid values include: | |
3434 | ||
3183b230 RS |
3435 | sendmail-user-agent -- use the default Emacs Mail package |
3436 | mh-e-user-agent -- use the Emacs interface to the MH mail system | |
3437 | message-user-agent -- use the GNUS mail sending package | |
a31ca314 RS |
3438 | |
3439 | Additional valid symbols may be available; check with the author of | |
69c1dd37 RS |
3440 | your package for details." |
3441 | :type '(radio (function-item :tag "Default Emacs mail" | |
3442 | :format "%t\n" | |
3443 | sendmail-user-agent) | |
3444 | (function-item :tag "Emacs interface to MH" | |
3445 | :format "%t\n" | |
3446 | mh-e-user-agent) | |
3447 | (function-item :tag "Gnus mail sending package" | |
3448 | :format "%t\n" | |
3449 | message-user-agent) | |
3450 | (function :tag "Other")) | |
3451 | :group 'mail) | |
a31ca314 RS |
3452 | |
3453 | (defun define-mail-user-agent (symbol composefunc sendfunc | |
3454 | &optional abortfunc hookvar) | |
3455 | "Define a symbol to identify a mail-sending package for `mail-user-agent'. | |
3456 | ||
3457 | SYMBOL can be any Lisp symbol. Its function definition and/or | |
3458 | value as a variable do not matter for this usage; we use only certain | |
3459 | properties on its property list, to encode the rest of the arguments. | |
3460 | ||
3461 | COMPOSEFUNC is program callable function that composes an outgoing | |
3462 | mail message buffer. This function should set up the basics of the | |
3463 | buffer without requiring user interaction. It should populate the | |
3183b230 RS |
3464 | standard mail headers, leaving the `to:' and `subject:' headers blank |
3465 | by default. | |
a31ca314 | 3466 | |
d0008a00 RS |
3467 | COMPOSEFUNC should accept several optional arguments--the same |
3468 | arguments that `compose-mail' takes. See that function's documentation. | |
a31ca314 | 3469 | |
3183b230 RS |
3470 | SENDFUNC is the command a user would run to send the message. |
3471 | ||
3472 | Optional ABORTFUNC is the command a user would run to abort the | |
a31ca314 RS |
3473 | message. For mail packages that don't have a separate abort function, |
3474 | this can be `kill-buffer' (the equivalent of omitting this argument). | |
3475 | ||
3476 | Optional HOOKVAR is a hook variable that gets run before the message | |
3183b230 RS |
3477 | is actually sent. Callers that use the `mail-user-agent' may |
3478 | install a hook function temporarily on this hook variable. | |
3479 | If HOOKVAR is nil, `mail-send-hook' is used. | |
a31ca314 RS |
3480 | |
3481 | The properties used on SYMBOL are `composefunc', `sendfunc', | |
3482 | `abortfunc', and `hookvar'." | |
3483 | (put symbol 'composefunc composefunc) | |
3484 | (put symbol 'sendfunc sendfunc) | |
3485 | (put symbol 'abortfunc (or abortfunc 'kill-buffer)) | |
3486 | (put symbol 'hookvar (or hookvar 'mail-send-hook))) | |
3487 | ||
d0008a00 | 3488 | (defun assoc-ignore-case (key alist) |
9f0f00d7 RS |
3489 | "Like `assoc', but ignores differences in case and text representation. |
3490 | KEY must be a string. Upper-case and lower-case letters are treated as equal. | |
3491 | Unibyte strings are converted to multibyte for comparison." | |
d0008a00 RS |
3492 | (let (element) |
3493 | (while (and alist (not element)) | |
9f0f00d7 RS |
3494 | (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t)) |
3495 | (setq element (car alist))) | |
3496 | (setq alist (cdr alist))) | |
3497 | element)) | |
3498 | ||
3499 | (defun assoc-ignore-representation (key alist) | |
3500 | "Like `assoc', but ignores differences in text representation. | |
3501 | KEY must be a string. | |
3502 | Unibyte strings are converted to multibyte for comparison." | |
3503 | (let (element) | |
3504 | (while (and alist (not element)) | |
3505 | (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil)) | |
d0008a00 RS |
3506 | (setq element (car alist))) |
3507 | (setq alist (cdr alist))) | |
3508 | element)) | |
3509 | ||
a31ca314 | 3510 | (define-mail-user-agent 'sendmail-user-agent |
34fbcdf3 | 3511 | 'sendmail-user-agent-compose |
a31ca314 RS |
3512 | 'mail-send-and-exit) |
3513 | ||
360b5483 RS |
3514 | (defun rfc822-goto-eoh () |
3515 | ;; Go to header delimiter line in a mail message, following RFC822 rules | |
3516 | (goto-char (point-min)) | |
3517 | (while (looking-at "^[^: \n]+:\\|^[ \t]") | |
3518 | (forward-line 1)) | |
3519 | (point)) | |
3520 | ||
34fbcdf3 RS |
3521 | (defun sendmail-user-agent-compose (&optional to subject other-headers continue |
3522 | switch-function yank-action | |
3523 | send-actions) | |
3524 | (if switch-function | |
3525 | (let ((special-display-buffer-names nil) | |
3526 | (special-display-regexps nil) | |
3527 | (same-window-buffer-names nil) | |
3528 | (same-window-regexps nil)) | |
3529 | (funcall switch-function "*mail*"))) | |
3530 | (let ((cc (cdr (assoc-ignore-case "cc" other-headers))) | |
3531 | (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers)))) | |
3532 | (or (mail continue to subject in-reply-to cc yank-action send-actions) | |
3533 | continue | |
3534 | (error "Message aborted")) | |
3535 | (save-excursion | |
360b5483 | 3536 | (rfc822-goto-eoh) |
34fbcdf3 RS |
3537 | (while other-headers |
3538 | (if (not (member (car (car other-headers)) '("in-reply-to" "cc"))) | |
3539 | (insert (car (car other-headers)) ": " | |
3540 | (cdr (car other-headers)) "\n")) | |
3541 | (setq other-headers (cdr other-headers))) | |
3542 | t))) | |
3543 | ||
a31ca314 RS |
3544 | (define-mail-user-agent 'mh-e-user-agent |
3545 | 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft | |
3546 | 'mh-before-send-letter-hook) | |
d0008a00 RS |
3547 | |
3548 | (defun compose-mail (&optional to subject other-headers continue | |
3549 | switch-function yank-action send-actions) | |
3550 | "Start composing a mail message to send. | |
3551 | This uses the user's chosen mail composition package | |
3552 | as selected with the variable `mail-user-agent'. | |
3553 | The optional arguments TO and SUBJECT specify recipients | |
3554 | and the initial Subject field, respectively. | |
3555 | ||
3556 | OTHER-HEADERS is an alist specifying additional | |
3557 | header fields. Elements look like (HEADER . VALUE) where both | |
3558 | HEADER and VALUE are strings. | |
3559 | ||
3560 | CONTINUE, if non-nil, says to continue editing a message already | |
3561 | being composed. | |
3562 | ||
3563 | SWITCH-FUNCTION, if non-nil, is a function to use to | |
3564 | switch to and display the buffer used for mail composition. | |
3565 | ||
3566 | YANK-ACTION, if non-nil, is an action to perform, if and when necessary, | |
06720de2 RS |
3567 | to insert the raw text of the message being replied to. |
3568 | It has the form (FUNCTION . ARGS). The user agent will apply | |
3569 | FUNCTION to ARGS, to insert the raw text of the original message. | |
3570 | \(The user agent will also run `mail-citation-hook', *after* the | |
3571 | original text has been inserted in this way.) | |
d0008a00 RS |
3572 | |
3573 | SEND-ACTIONS is a list of actions to call when the message is sent. | |
3574 | Each action has the form (FUNCTION . ARGS)." | |
b5f019be RS |
3575 | (interactive |
3576 | (list nil nil nil current-prefix-arg)) | |
d0008a00 RS |
3577 | (let ((function (get mail-user-agent 'composefunc))) |
3578 | (funcall function to subject other-headers continue | |
3579 | switch-function yank-action send-actions))) | |
b5f019be RS |
3580 | |
3581 | (defun compose-mail-other-window (&optional to subject other-headers continue | |
3582 | yank-action send-actions) | |
3583 | "Like \\[compose-mail], but edit the outgoing message in another window." | |
3584 | (interactive | |
3585 | (list nil nil nil current-prefix-arg)) | |
3586 | (compose-mail to subject other-headers continue | |
3587 | 'switch-to-buffer-other-window yank-action send-actions)) | |
3588 | ||
3589 | ||
3590 | (defun compose-mail-other-frame (&optional to subject other-headers continue | |
3591 | yank-action send-actions) | |
3592 | "Like \\[compose-mail], but edit the outgoing message in another frame." | |
3593 | (interactive | |
3594 | (list nil nil nil current-prefix-arg)) | |
3595 | (compose-mail to subject other-headers continue | |
3596 | 'switch-to-buffer-other-frame yank-action send-actions)) | |
a31ca314 | 3597 | \f |
610c1c68 RS |
3598 | (defvar set-variable-value-history nil |
3599 | "History of values entered with `set-variable'.") | |
3600 | ||
3601 | (defun set-variable (var val) | |
3602 | "Set VARIABLE to VALUE. VALUE is a Lisp object. | |
3603 | When using this interactively, enter a Lisp object for VALUE. | |
3604 | If you want VALUE to be a string, you must surround it with doublequotes. | |
3605 | VALUE is used literally, not evaluated. | |
3606 | ||
3607 | If VARIABLE has a `variable-interactive' property, that is used as if | |
3608 | it were the arg to `interactive' (which see) to interactively read VALUE. | |
3609 | ||
3610 | If VARIABLE has been defined with `defcustom', then the type information | |
3611 | in the definition is used to check that VALUE is valid." | |
e9dfb72e RS |
3612 | (interactive |
3613 | (let* ((default-var (variable-at-point)) | |
3614 | (var (if (symbolp default-var) | |
3615 | (read-variable (format "Set variable (default %s): " default-var) | |
3616 | default-var) | |
3617 | (read-variable "Set variable: "))) | |
610c1c68 RS |
3618 | (minibuffer-help-form '(describe-variable var)) |
3619 | (prop (get var 'variable-interactive)) | |
3620 | (prompt (format "Set %s to value: " var)) | |
3621 | (val (if prop | |
3622 | ;; Use VAR's `variable-interactive' property | |
3623 | ;; as an interactive spec for prompting. | |
3624 | (call-interactively `(lambda (arg) | |
3625 | (interactive ,prop) | |
3626 | arg)) | |
3627 | (read | |
3628 | (read-string prompt nil | |
3629 | 'set-variable-value-history))))) | |
3630 | (list var val))) | |
3631 | ||
f8496faa | 3632 | (let ((type (get var 'custom-type))) |
610c1c68 RS |
3633 | (when type |
3634 | ;; Match with custom type. | |
3635 | (require 'wid-edit) | |
610c1c68 RS |
3636 | (setq type (widget-convert type)) |
3637 | (unless (widget-apply type :match val) | |
3638 | (error "Value `%S' does not match type %S of %S" | |
3639 | val (car type) var)))) | |
3640 | (set var val)) | |
e8a700bf RS |
3641 | \f |
3642 | ;; Define the major mode for lists of completions. | |
3643 | ||
98b45886 RS |
3644 | (defvar completion-list-mode-map nil |
3645 | "Local map for completion list buffers.") | |
ac29eb79 | 3646 | (or completion-list-mode-map |
e8a700bf RS |
3647 | (let ((map (make-sparse-keymap))) |
3648 | (define-key map [mouse-2] 'mouse-choose-completion) | |
eaf76065 | 3649 | (define-key map [down-mouse-2] nil) |
80298193 | 3650 | (define-key map "\C-m" 'choose-completion) |
1c6c6fde | 3651 | (define-key map "\e\e\e" 'delete-completion-window) |
dde69dbe RS |
3652 | (define-key map [left] 'previous-completion) |
3653 | (define-key map [right] 'next-completion) | |
ac29eb79 | 3654 | (setq completion-list-mode-map map))) |
e8a700bf RS |
3655 | |
3656 | ;; Completion mode is suitable only for specially formatted data. | |
ac29eb79 | 3657 | (put 'completion-list-mode 'mode-class 'special) |
e8a700bf | 3658 | |
98b45886 RS |
3659 | (defvar completion-reference-buffer nil |
3660 | "Record the buffer that was current when the completion list was requested. | |
3661 | This is a local variable in the completion list buffer. | |
ec39964e | 3662 | Initial value is nil to avoid some compiler warnings.") |
3819736b | 3663 | |
83434bda RS |
3664 | (defvar completion-no-auto-exit nil |
3665 | "Non-nil means `choose-completion-string' should never exit the minibuffer. | |
3666 | This also applies to other functions such as `choose-completion' | |
3667 | and `mouse-choose-completion'.") | |
3668 | ||
98b45886 RS |
3669 | (defvar completion-base-size nil |
3670 | "Number of chars at beginning of minibuffer not involved in completion. | |
3671 | This is a local variable in the completion list buffer | |
3672 | but it talks about the buffer in `completion-reference-buffer'. | |
3673 | If this is nil, it means to compare text to determine which part | |
3674 | of the tail end of the buffer's text is involved in completion.") | |
f6b293e3 | 3675 | |
1c6c6fde RS |
3676 | (defun delete-completion-window () |
3677 | "Delete the completion list window. | |
3678 | Go to the window from which completion was requested." | |
3679 | (interactive) | |
3680 | (let ((buf completion-reference-buffer)) | |
ddb2b181 RS |
3681 | (if (one-window-p t) |
3682 | (if (window-dedicated-p (selected-window)) | |
3683 | (delete-frame (selected-frame))) | |
3684 | (delete-window (selected-window)) | |
3685 | (if (get-buffer-window buf) | |
3686 | (select-window (get-buffer-window buf)))))) | |
1c6c6fde | 3687 | |
dde69dbe RS |
3688 | (defun previous-completion (n) |
3689 | "Move to the previous item in the completion list." | |
3690 | (interactive "p") | |
3691 | (next-completion (- n))) | |
3692 | ||
3693 | (defun next-completion (n) | |
3694 | "Move to the next item in the completion list. | |
1f238ac2 | 3695 | With prefix argument N, move N items (negative N means move backward)." |
dde69dbe RS |
3696 | (interactive "p") |
3697 | (while (and (> n 0) (not (eobp))) | |
b61a81c2 RS |
3698 | (let ((prop (get-text-property (point) 'mouse-face)) |
3699 | (end (point-max))) | |
dde69dbe RS |
3700 | ;; If in a completion, move to the end of it. |
3701 | (if prop | |
b61a81c2 | 3702 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) |
dde69dbe | 3703 | ;; Move to start of next one. |
b61a81c2 | 3704 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) |
dde69dbe RS |
3705 | (setq n (1- n))) |
3706 | (while (and (< n 0) (not (bobp))) | |
b61a81c2 RS |
3707 | (let ((prop (get-text-property (1- (point)) 'mouse-face)) |
3708 | (end (point-min))) | |
dde69dbe RS |
3709 | ;; If in a completion, move to the start of it. |
3710 | (if prop | |
b61a81c2 RS |
3711 | (goto-char (previous-single-property-change |
3712 | (point) 'mouse-face nil end))) | |
dde69dbe | 3713 | ;; Move to end of the previous completion. |
b61a81c2 | 3714 | (goto-char (previous-single-property-change (point) 'mouse-face nil end)) |
dde69dbe | 3715 | ;; Move to the start of that one. |
b61a81c2 | 3716 | (goto-char (previous-single-property-change (point) 'mouse-face nil end))) |
dde69dbe RS |
3717 | (setq n (1+ n)))) |
3718 | ||
80298193 RS |
3719 | (defun choose-completion () |
3720 | "Choose the completion that point is in or next to." | |
3721 | (interactive) | |
f6b293e3 RS |
3722 | (let (beg end completion (buffer completion-reference-buffer) |
3723 | (base-size completion-base-size)) | |
6096f362 RS |
3724 | (if (and (not (eobp)) (get-text-property (point) 'mouse-face)) |
3725 | (setq end (point) beg (1+ (point)))) | |
3726 | (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
3f299281 | 3727 | (setq end (1- (point)) beg (point))) |
6096f362 RS |
3728 | (if (null beg) |
3729 | (error "No completion here")) | |
3730 | (setq beg (previous-single-property-change beg 'mouse-face)) | |
88dd3c24 | 3731 | (setq end (or (next-single-property-change end 'mouse-face) (point-max))) |
ab63960f RS |
3732 | (setq completion (buffer-substring beg end)) |
3733 | (let ((owindow (selected-window))) | |
3734 | (if (and (one-window-p t 'selected-frame) | |
3735 | (window-dedicated-p (selected-window))) | |
3736 | ;; This is a special buffer's frame | |
3737 | (iconify-frame (selected-frame)) | |
3738 | (or (window-dedicated-p (selected-window)) | |
3739 | (bury-buffer))) | |
3740 | (select-window owindow)) | |
f6b293e3 | 3741 | (choose-completion-string completion buffer base-size))) |
80298193 RS |
3742 | |
3743 | ;; Delete the longest partial match for STRING | |
3744 | ;; that can be found before POINT. | |
3745 | (defun choose-completion-delete-max-match (string) | |
3746 | (let ((opoint (point)) | |
3747 | (len (min (length string) | |
3748 | (- (point) (point-min))))) | |
3749 | (goto-char (- (point) (length string))) | |
61bbf6fe RS |
3750 | (if completion-ignore-case |
3751 | (setq string (downcase string))) | |
80298193 RS |
3752 | (while (and (> len 0) |
3753 | (let ((tail (buffer-substring (point) | |
3754 | (+ (point) len)))) | |
61bbf6fe RS |
3755 | (if completion-ignore-case |
3756 | (setq tail (downcase tail))) | |
80298193 RS |
3757 | (not (string= tail (substring string 0 len))))) |
3758 | (setq len (1- len)) | |
3759 | (forward-char 1)) | |
3760 | (delete-char len))) | |
3761 | ||
98b45886 RS |
3762 | ;; Switch to BUFFER and insert the completion choice CHOICE. |
3763 | ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text | |
3764 | ;; to keep. If it is nil, use choose-completion-delete-max-match instead. | |
74d0290b RS |
3765 | |
3766 | ;; If BUFFER is the minibuffer, exit the minibuffer | |
83434bda RS |
3767 | ;; unless it is reading a file name and CHOICE is a directory, |
3768 | ;; or completion-no-auto-exit is non-nil. | |
f6b293e3 | 3769 | (defun choose-completion-string (choice &optional buffer base-size) |
80298193 | 3770 | (let ((buffer (or buffer completion-reference-buffer))) |
cf52ad58 RS |
3771 | ;; If BUFFER is a minibuffer, barf unless it's the currently |
3772 | ;; active minibuffer. | |
3773 | (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer)) | |
45486731 RS |
3774 | (or (not (active-minibuffer-window)) |
3775 | (not (equal buffer | |
3776 | (window-buffer (active-minibuffer-window)))))) | |
cf52ad58 RS |
3777 | (error "Minibuffer is not active for completion") |
3778 | ;; Insert the completion into the buffer where completion was requested. | |
3779 | (set-buffer buffer) | |
f6b293e3 RS |
3780 | (if base-size |
3781 | (delete-region (+ base-size (point-min)) (point)) | |
3782 | (choose-completion-delete-max-match choice)) | |
cf52ad58 | 3783 | (insert choice) |
63240af1 RS |
3784 | (remove-text-properties (- (point) (length choice)) (point) |
3785 | '(mouse-face nil)) | |
cf52ad58 RS |
3786 | ;; Update point in the window that BUFFER is showing in. |
3787 | (let ((window (get-buffer-window buffer t))) | |
3788 | (set-window-point window (point))) | |
3789 | ;; If completing for the minibuffer, exit it with this choice. | |
83434bda RS |
3790 | (and (not completion-no-auto-exit) |
3791 | (equal buffer (window-buffer (minibuffer-window))) | |
8881ad9a | 3792 | minibuffer-completion-table |
74d0290b RS |
3793 | ;; If this is reading a file name, and the file name chosen |
3794 | ;; is a directory, don't exit the minibuffer. | |
3795 | (if (and (eq minibuffer-completion-table 'read-file-name-internal) | |
3796 | (file-directory-p (buffer-string))) | |
3797 | (select-window (active-minibuffer-window)) | |
3798 | (exit-minibuffer)))))) | |
80298193 | 3799 | |
ac29eb79 | 3800 | (defun completion-list-mode () |
e8a700bf | 3801 | "Major mode for buffers showing lists of possible completions. |
80298193 RS |
3802 | Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\ |
3803 | to select the completion near point. | |
3804 | Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\ | |
3805 | with the mouse." | |
e8a700bf RS |
3806 | (interactive) |
3807 | (kill-all-local-variables) | |
ac29eb79 RS |
3808 | (use-local-map completion-list-mode-map) |
3809 | (setq mode-name "Completion List") | |
3810 | (setq major-mode 'completion-list-mode) | |
f6b293e3 RS |
3811 | (make-local-variable 'completion-base-size) |
3812 | (setq completion-base-size nil) | |
ac29eb79 | 3813 | (run-hooks 'completion-list-mode-hook)) |
e8a700bf | 3814 | |
747a0e2f RS |
3815 | (defvar completion-setup-hook nil |
3816 | "Normal hook run at the end of setting up a completion list buffer. | |
3817 | When this hook is run, the current buffer is the one in which the | |
3818 | command to display the completion list buffer was run. | |
3819 | The completion list buffer is available as the value of `standard-output'.") | |
3820 | ||
98b45886 RS |
3821 | ;; This function goes in completion-setup-hook, so that it is called |
3822 | ;; after the text of the completion list buffer is written. | |
6096f362 | 3823 | |
e8a700bf RS |
3824 | (defun completion-setup-function () |
3825 | (save-excursion | |
98b45886 | 3826 | (let ((mainbuf (current-buffer))) |
3819736b RS |
3827 | (set-buffer standard-output) |
3828 | (completion-list-mode) | |
3829 | (make-local-variable 'completion-reference-buffer) | |
3830 | (setq completion-reference-buffer mainbuf) | |
50be475d RS |
3831 | (if (eq minibuffer-completion-table 'read-file-name-internal) |
3832 | ;; For file name completion, | |
3833 | ;; use the number of chars before the start of the | |
3834 | ;; last file name component. | |
3835 | (setq completion-base-size | |
3836 | (save-excursion | |
3837 | (set-buffer mainbuf) | |
3838 | (goto-char (point-max)) | |
3839 | (skip-chars-backward (format "^%c" directory-sep-char)) | |
3840 | (- (point) (point-min)))) | |
19183a29 RS |
3841 | ;; Otherwise, in minibuffer, the whole input is being completed. |
3842 | (save-match-data | |
3843 | (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" | |
3844 | (buffer-name mainbuf)) | |
3845 | (setq completion-base-size 0)))) | |
3819736b RS |
3846 | (goto-char (point-min)) |
3847 | (if window-system | |
3848 | (insert (substitute-command-keys | |
80298193 RS |
3849 | "Click \\[mouse-choose-completion] on a completion to select it.\n"))) |
3850 | (insert (substitute-command-keys | |
3851 | "In this buffer, type \\[choose-completion] to \ | |
7d22ed15 | 3852 | select the completion near point.\n\n"))))) |
c88ab9ce | 3853 | |
e8a700bf | 3854 | (add-hook 'completion-setup-hook 'completion-setup-function) |
dde69dbe RS |
3855 | |
3856 | (define-key minibuffer-local-completion-map [prior] | |
3857 | 'switch-to-completions) | |
3858 | (define-key minibuffer-local-must-match-map [prior] | |
3859 | 'switch-to-completions) | |
3860 | (define-key minibuffer-local-completion-map "\M-v" | |
3861 | 'switch-to-completions) | |
3862 | (define-key minibuffer-local-must-match-map "\M-v" | |
3863 | 'switch-to-completions) | |
3864 | ||
3865 | (defun switch-to-completions () | |
3866 | "Select the completion list window." | |
3867 | (interactive) | |
9595fbdb RS |
3868 | ;; Make sure we have a completions window. |
3869 | (or (get-buffer-window "*Completions*") | |
3870 | (minibuffer-completion-help)) | |
fdbd7c4d KH |
3871 | (let ((window (get-buffer-window "*Completions*"))) |
3872 | (when window | |
3873 | (select-window window) | |
3874 | (goto-char (point-min)) | |
3875 | (search-forward "\n\n") | |
3876 | (forward-line 1)))) | |
a3d1480b | 3877 | \f |
82072f33 RS |
3878 | ;; Support keyboard commands to turn on various modifiers. |
3879 | ||
3880 | ;; These functions -- which are not commands -- each add one modifier | |
3881 | ;; to the following event. | |
3882 | ||
3883 | (defun event-apply-alt-modifier (ignore-prompt) | |
70cf9f08 KH |
3884 | "Add the Alt modifier to the following event. |
3885 | For example, type \\[event-apply-alt-modifier] & to enter Alt-&." | |
82072f33 RS |
3886 | (vector (event-apply-modifier (read-event) 'alt 22 "A-"))) |
3887 | (defun event-apply-super-modifier (ignore-prompt) | |
70cf9f08 KH |
3888 | "Add the Super modifier to the following event. |
3889 | For example, type \\[event-apply-super-modifier] & to enter Super-&." | |
82072f33 RS |
3890 | (vector (event-apply-modifier (read-event) 'super 23 "s-"))) |
3891 | (defun event-apply-hyper-modifier (ignore-prompt) | |
70cf9f08 KH |
3892 | "Add the Hyper modifier to the following event. |
3893 | For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&." | |
82072f33 RS |
3894 | (vector (event-apply-modifier (read-event) 'hyper 24 "H-"))) |
3895 | (defun event-apply-shift-modifier (ignore-prompt) | |
70cf9f08 KH |
3896 | "Add the Shift modifier to the following event. |
3897 | For example, type \\[event-apply-shift-modifier] & to enter Shift-&." | |
82072f33 RS |
3898 | (vector (event-apply-modifier (read-event) 'shift 25 "S-"))) |
3899 | (defun event-apply-control-modifier (ignore-prompt) | |
70cf9f08 KH |
3900 | "Add the Ctrl modifier to the following event. |
3901 | For example, type \\[event-apply-control-modifier] & to enter Ctrl-&." | |
82072f33 RS |
3902 | (vector (event-apply-modifier (read-event) 'control 26 "C-"))) |
3903 | (defun event-apply-meta-modifier (ignore-prompt) | |
70cf9f08 KH |
3904 | "Add the Meta modifier to the following event. |
3905 | For example, type \\[event-apply-meta-modifier] & to enter Meta-&." | |
82072f33 RS |
3906 | (vector (event-apply-modifier (read-event) 'meta 27 "M-"))) |
3907 | ||
3908 | (defun event-apply-modifier (event symbol lshiftby prefix) | |
3909 | "Apply a modifier flag to event EVENT. | |
3910 | SYMBOL is the name of this modifier, as a symbol. | |
3911 | LSHIFTBY is the numeric value of this modifier, in keyboard events. | |
3912 | PREFIX is the string that represents this modifier in an event type symbol." | |
3913 | (if (numberp event) | |
3914 | (cond ((eq symbol 'control) | |
90bebcb0 KH |
3915 | (if (and (<= (downcase event) ?z) |
3916 | (>= (downcase event) ?a)) | |
82072f33 | 3917 | (- (downcase event) ?a -1) |
90bebcb0 KH |
3918 | (if (and (<= (downcase event) ?Z) |
3919 | (>= (downcase event) ?A)) | |
82072f33 RS |
3920 | (- (downcase event) ?A -1) |
3921 | (logior (lsh 1 lshiftby) event)))) | |
3922 | ((eq symbol 'shift) | |
3923 | (if (and (<= (downcase event) ?z) | |
3924 | (>= (downcase event) ?a)) | |
3925 | (upcase event) | |
3926 | (logior (lsh 1 lshiftby) event))) | |
3927 | (t | |
3928 | (logior (lsh 1 lshiftby) event))) | |
3929 | (if (memq symbol (event-modifiers event)) | |
3930 | event | |
3931 | (let ((event-type (if (symbolp event) event (car event)))) | |
3932 | (setq event-type (intern (concat prefix (symbol-name event-type)))) | |
3933 | (if (symbolp event) | |
3934 | event-type | |
3935 | (cons event-type (cdr event))))))) | |
3936 | ||
e5fff738 KH |
3937 | (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier) |
3938 | (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier) | |
3939 | (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier) | |
3940 | (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier) | |
3941 | (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier) | |
3942 | (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier) | |
82072f33 | 3943 | \f |
a3d1480b JB |
3944 | ;;;; Keypad support. |
3945 | ||
3946 | ;;; Make the keypad keys act like ordinary typing keys. If people add | |
3947 | ;;; bindings for the function key symbols, then those bindings will | |
3948 | ;;; override these, so this shouldn't interfere with any existing | |
3949 | ;;; bindings. | |
3950 | ||
0d173134 | 3951 | ;; Also tell read-char how to handle these keys. |
a3d1480b JB |
3952 | (mapcar |
3953 | (lambda (keypad-normal) | |
3954 | (let ((keypad (nth 0 keypad-normal)) | |
3955 | (normal (nth 1 keypad-normal))) | |
0d173134 | 3956 | (put keypad 'ascii-character normal) |
a3d1480b JB |
3957 | (define-key function-key-map (vector keypad) (vector normal)))) |
3958 | '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4) | |
3959 | (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9) | |
3960 | (kp-space ?\ ) | |
3961 | (kp-tab ?\t) | |
3962 | (kp-enter ?\r) | |
3963 | (kp-multiply ?*) | |
3964 | (kp-add ?+) | |
3965 | (kp-separator ?,) | |
3966 | (kp-subtract ?-) | |
3967 | (kp-decimal ?.) | |
3968 | (kp-divide ?/) | |
3969 | (kp-equal ?=))) | |
3970 | ||
c88ab9ce | 3971 | ;;; simple.el ends here |