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