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