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