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