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