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