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