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