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