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