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