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