(tar-parse-octal-long-integer): Return list, not vector.
[bpt/emacs.git] / lisp / simple.el
CommitLineData
c88ab9ce
ER
1;;; simple.el --- basic editing commands for Emacs
2
9e50756b 3;; Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
2076c87c
JB
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
3a801d0c 9;; the Free Software Foundation; either version 2, or (at your option)
2076c87c
JB
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
d9ecc911
ER
21;;; Commentary:
22
23;; A grab-bag of basic Emacs commands not specifically related to some
24;; major mode or to file-handling.
25
3a801d0c 26;;; Code:
2076c87c 27
30bb9754
BG
28(defun newline (&optional arg)
29 "Insert a newline and move to left margin of the new line.
30The newline is marked with the text-property `hard'.
31With arg, insert that many newlines.
32In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
33 (interactive "*P")
34 ;; Inserting a newline at the end of a line produces better redisplay in
35 ;; try_window_id than inserting at the beginning of a line, and the textual
36 ;; result is the same. So, if we're at beginning of line, pretend to be at
37 ;; the end of the previous line.
38 (let ((flag (and (not (bobp))
39 (bolp)
40 (< (or (previous-property-change (point)) -2)
41 (- (point) 2)))))
42 (if flag (backward-char 1))
43 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
44 ;; Set last-command-char to tell self-insert what to insert.
45 (let ((last-command-char ?\n)
46 ;; Don't auto-fill if we have a numeric argument.
47 (auto-fill-function (if arg nil auto-fill-function)))
48 (self-insert-command (prefix-numeric-value arg)))
49 ;; Mark the newline(s) `hard'.
50 (if use-hard-newlines
51 (let* ((from (- (point) (if arg (prefix-numeric-value arg) 1)))
52 (sticky (get-text-property from 'rear-nonsticky)))
53 (put-text-property from (point) 'hard 't)
54 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
55 (if (and (listp sticky) (not (memq 'hard sticky)))
56 (put-text-property from (point) 'rear-nonsticky
57 (cons 'hard sticky)))))
58 (if flag (forward-char 1)))
59 (move-to-left-margin nil t)
60 nil)
61
2076c87c 62(defun open-line (arg)
ff1fbe3e 63 "Insert a newline and leave point before it.
3db1e3b5 64If there is a fill prefix and/or a left-margin, insert them on the new line
616ed245
RS
65if the line would have been empty.
66With arg N, insert N newlines."
2076c87c 67 (interactive "*p")
616ed245 68 (let* ((do-fill-prefix (and fill-prefix (bolp)))
3db1e3b5 69 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
28191e20
RS
70 (loc (point)))
71 (while (> arg 0)
3db1e3b5 72 (if do-left-margin (indent-to (current-left-margin)))
eed5698b 73 (if do-fill-prefix (insert-and-inherit fill-prefix))
28191e20
RS
74 (newline 1)
75 (setq arg (1- arg)))
76 (goto-char loc))
77 (end-of-line))
2076c87c
JB
78
79(defun split-line ()
80 "Split current line, moving portion beyond point vertically down."
81 (interactive "*")
82 (skip-chars-forward " \t")
83 (let ((col (current-column))
84 (pos (point)))
28191e20 85 (newline 1)
2076c87c
JB
86 (indent-to col 0)
87 (goto-char pos)))
88
89(defun quoted-insert (arg)
90 "Read next input character and insert it.
ff1fbe3e 91This is useful for inserting control characters.
dbc4e1c1 92You may also type up to 3 octal digits, to insert a character with that code.
b6a22db0
JB
93
94In overwrite mode, this function inserts the character anyway, and
95does not handle octal digits specially. This means that if you use
96overwrite as your normal editing mode, you can use this function to
97insert characters when necessary.
98
99In binary overwrite mode, this function does overwrite, and octal
100digits are interpreted as a character code. This is supposed to make
101this function useful in editing binary files."
2076c87c 102 (interactive "*p")
b6a22db0
JB
103 (let ((char (if (or (not overwrite-mode)
104 (eq overwrite-mode 'overwrite-mode-binary))
105 (read-quoted-char)
106 (read-char))))
ec321cad
RS
107 (if (> arg 0)
108 (if (eq overwrite-mode 'overwrite-mode-binary)
109 (delete-char arg)))
110 (while (> arg 0)
111 (insert-and-inherit char)
112 (setq arg (1- arg)))))
2076c87c
JB
113
114(defun delete-indentation (&optional arg)
115 "Join this line to previous and fix up whitespace at join.
ccc58657 116If there is a fill prefix, delete it from the beginning of this line.
2076c87c
JB
117With argument, join this line to following line."
118 (interactive "*P")
119 (beginning-of-line)
120 (if arg (forward-line 1))
121 (if (eq (preceding-char) ?\n)
122 (progn
123 (delete-region (point) (1- (point)))
ccc58657
RS
124 ;; If the second line started with the fill prefix,
125 ;; delete the prefix.
126 (if (and fill-prefix
01b8e020 127 (<= (+ (point) (length fill-prefix)) (point-max))
ccc58657
RS
128 (string= fill-prefix
129 (buffer-substring (point)
130 (+ (point) (length fill-prefix)))))
131 (delete-region (point) (+ (point) (length fill-prefix))))
2076c87c
JB
132 (fixup-whitespace))))
133
134(defun fixup-whitespace ()
135 "Fixup white space between objects around point.
136Leave one space or none, according to the context."
137 (interactive "*")
138 (save-excursion
139 (delete-horizontal-space)
140 (if (or (looking-at "^\\|\\s)")
141 (save-excursion (forward-char -1)
142 (looking-at "$\\|\\s(\\|\\s'")))
143 nil
144 (insert ?\ ))))
145
146(defun delete-horizontal-space ()
147 "Delete all spaces and tabs around point."
148 (interactive "*")
149 (skip-chars-backward " \t")
150 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
151
152(defun just-one-space ()
153 "Delete all spaces and tabs around point, leaving one space."
154 (interactive "*")
155 (skip-chars-backward " \t")
156 (if (= (following-char) ? )
157 (forward-char 1)
158 (insert ? ))
159 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
160
161(defun delete-blank-lines ()
162 "On blank line, delete all surrounding blank lines, leaving just one.
163On isolated blank line, delete that one.
6d30d416 164On nonblank line, delete any immediately following blank lines."
2076c87c
JB
165 (interactive "*")
166 (let (thisblank singleblank)
167 (save-excursion
168 (beginning-of-line)
169 (setq thisblank (looking-at "[ \t]*$"))
70e14c01 170 ;; Set singleblank if there is just one blank line here.
2076c87c
JB
171 (setq singleblank
172 (and thisblank
173 (not (looking-at "[ \t]*\n[ \t]*$"))
174 (or (bobp)
175 (progn (forward-line -1)
176 (not (looking-at "[ \t]*$")))))))
70e14c01 177 ;; Delete preceding blank lines, and this one too if it's the only one.
2076c87c
JB
178 (if thisblank
179 (progn
180 (beginning-of-line)
181 (if singleblank (forward-line 1))
182 (delete-region (point)
183 (if (re-search-backward "[^ \t\n]" nil t)
184 (progn (forward-line 1) (point))
185 (point-min)))))
70e14c01
JB
186 ;; Delete following blank lines, unless the current line is blank
187 ;; and there are no following blank lines.
2076c87c
JB
188 (if (not (and thisblank singleblank))
189 (save-excursion
190 (end-of-line)
191 (forward-line 1)
192 (delete-region (point)
193 (if (re-search-forward "[^ \t\n]" nil t)
194 (progn (beginning-of-line) (point))
70e14c01
JB
195 (point-max)))))
196 ;; Handle the special case where point is followed by newline and eob.
197 ;; Delete the line, leaving point at eob.
198 (if (looking-at "^[ \t]*\n\\'")
199 (delete-region (point) (point-max)))))
2076c87c
JB
200
201(defun back-to-indentation ()
202 "Move point to the first non-whitespace character on this line."
203 (interactive)
204 (beginning-of-line 1)
205 (skip-chars-forward " \t"))
206
207(defun newline-and-indent ()
208 "Insert a newline, then indent according to major mode.
ff1fbe3e 209Indentation is done using the value of `indent-line-function'.
2076c87c 210In programming language modes, this is the same as TAB.
ff1fbe3e 211In some text modes, where TAB inserts a tab, this command indents to the
eed5698b 212column specified by the function `current-left-margin'."
2076c87c
JB
213 (interactive "*")
214 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
46947372 215 (newline)
2076c87c
JB
216 (indent-according-to-mode))
217
218(defun reindent-then-newline-and-indent ()
219 "Reindent current line, insert newline, then indent the new line.
220Indentation of both lines is done according to the current major mode,
ff1fbe3e 221which means calling the current value of `indent-line-function'.
2076c87c
JB
222In programming language modes, this is the same as TAB.
223In some text modes, where TAB inserts a tab, this indents to the
eed5698b 224column specified by the function `current-left-margin'."
2076c87c
JB
225 (interactive "*")
226 (save-excursion
227 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
228 (indent-according-to-mode))
46947372 229 (newline)
2076c87c
JB
230 (indent-according-to-mode))
231
dff7d67f
RS
232;; Internal subroutine of delete-char
233(defun kill-forward-chars (arg)
234 (if (listp arg) (setq arg (car arg)))
235 (if (eq arg '-) (setq arg -1))
236 (kill-region (point) (+ (point) arg)))
237
238;; Internal subroutine of backward-delete-char
239(defun kill-backward-chars (arg)
240 (if (listp arg) (setq arg (car arg)))
241 (if (eq arg '-) (setq arg -1))
242 (kill-region (point) (- (point) arg)))
243
2076c87c
JB
244(defun backward-delete-char-untabify (arg &optional killp)
245 "Delete characters backward, changing tabs into spaces.
246Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
247Interactively, ARG is the prefix arg (default 1)
aba05ae4 248and KILLP is t if a prefix arg was specified."
2076c87c
JB
249 (interactive "*p\nP")
250 (let ((count arg))
251 (save-excursion
252 (while (and (> count 0) (not (bobp)))
253 (if (= (preceding-char) ?\t)
254 (let ((col (current-column)))
255 (forward-char -1)
256 (setq col (- col (current-column)))
257 (insert-char ?\ col)
258 (delete-char 1)))
259 (forward-char -1)
260 (setq count (1- count)))))
261 (delete-backward-char arg killp)
262 ;; In overwrite mode, back over columns while clearing them out,
263 ;; unless at end of line.
264 (and overwrite-mode (not (eolp))
265 (save-excursion (insert-char ?\ arg))))
266
267(defun zap-to-char (arg char)
268 "Kill up to and including ARG'th occurrence of CHAR.
269Goes backward if ARG is negative; error if CHAR not found."
270 (interactive "p\ncZap to char: ")
271 (kill-region (point) (progn
272 (search-forward (char-to-string char) nil nil arg)
273; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
274 (point))))
275
276(defun beginning-of-buffer (&optional arg)
277 "Move point to the beginning of the buffer; leave mark at previous position.
c66587fe
RS
278With arg N, put point N/10 of the way from the beginning.
279
280If the buffer is narrowed, this command uses the beginning and size
281of the accessible part of the buffer.
ff1fbe3e
RS
282
283Don't use this command in Lisp programs!
2076c87c
JB
284\(goto-char (point-min)) is faster and avoids clobbering the mark."
285 (interactive "P")
286 (push-mark)
c66587fe
RS
287 (let ((size (- (point-max) (point-min))))
288 (goto-char (if arg
289 (+ (point-min)
290 (if (> size 10000)
291 ;; Avoid overflow for large buffer sizes!
292 (* (prefix-numeric-value arg)
293 (/ size 10))
294 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
295 (point-min))))
2076c87c
JB
296 (if arg (forward-line 1)))
297
298(defun end-of-buffer (&optional arg)
299 "Move point to the end of the buffer; leave mark at previous position.
c66587fe
RS
300With arg N, put point N/10 of the way from the end.
301
302If the buffer is narrowed, this command uses the beginning and size
303of the accessible part of the buffer.
ff1fbe3e
RS
304
305Don't use this command in Lisp programs!
2076c87c
JB
306\(goto-char (point-max)) is faster and avoids clobbering the mark."
307 (interactive "P")
308 (push-mark)
c66587fe
RS
309 (let ((size (- (point-max) (point-min))))
310 (goto-char (if arg
311 (- (point-max)
312 (if (> size 10000)
313 ;; Avoid overflow for large buffer sizes!
314 (* (prefix-numeric-value arg)
315 (/ size 10))
316 (/ (* size (prefix-numeric-value arg)) 10)))
317 (point-max))))
3a801d0c
ER
318 ;; If we went to a place in the middle of the buffer,
319 ;; adjust it to the beginning of a line.
2076c87c 320 (if arg (forward-line 1)
3a801d0c
ER
321 ;; If the end of the buffer is not already on the screen,
322 ;; then scroll specially to put it near, but not at, the bottom.
323 (if (let ((old-point (point)))
324 (save-excursion
325 (goto-char (window-start))
326 (vertical-motion (window-height))
327 (< (point) old-point)))
97dfc68c
RS
328 (progn
329 (overlay-recenter (point))
330 (recenter -3)))))
2076c87c
JB
331
332(defun mark-whole-buffer ()
70e14c01
JB
333 "Put point at beginning and mark at end of buffer.
334You probably should not use this function in Lisp programs;
335it is usually a mistake for a Lisp function to use any subroutine
336that uses or sets the mark."
2076c87c
JB
337 (interactive)
338 (push-mark (point))
fd0f4056 339 (push-mark (point-max) nil t)
2076c87c
JB
340 (goto-char (point-min)))
341
342(defun count-lines-region (start end)
eb8c3be9 343 "Print number of lines and characters in the region."
2076c87c
JB
344 (interactive "r")
345 (message "Region has %d lines, %d characters"
346 (count-lines start end) (- end start)))
347
348(defun what-line ()
349 "Print the current line number (in the buffer) of point."
350 (interactive)
351 (save-restriction
352 (widen)
353 (save-excursion
354 (beginning-of-line)
355 (message "Line %d"
356 (1+ (count-lines 1 (point)))))))
357
358(defun count-lines (start end)
359 "Return number of lines between START and END.
360This is usually the number of newlines between them,
ff1fbe3e 361but can be one more if START is not equal to END
2076c87c 362and the greater of them is not at the start of a line."
e406700d
RS
363 (save-excursion
364 (save-restriction
365 (narrow-to-region start end)
366 (goto-char (point-min))
367 (if (eq selective-display t)
368 (save-match-data
dde92ca6
RS
369 (let ((done 0))
370 (while (re-search-forward "[\n\C-m]" nil t 40)
371 (setq done (+ 40 done)))
372 (while (re-search-forward "[\n\C-m]" nil t 1)
373 (setq done (+ 1 done)))
043efc41
RS
374 (goto-char (point-max))
375 (if (and (/= start end)
376 (not (bolp)))
377 (1+ done)
e406700d
RS
378 done)))
379 (- (buffer-size) (forward-line (buffer-size)))))))
2076c87c
JB
380
381(defun what-cursor-position ()
382 "Print info on cursor position (on screen and within buffer)."
383 (interactive)
384 (let* ((char (following-char))
385 (beg (point-min))
386 (end (point-max))
387 (pos (point))
388 (total (buffer-size))
389 (percent (if (> total 50000)
390 ;; Avoid overflow from multiplying by 100!
391 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
392 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
393 (hscroll (if (= (window-hscroll) 0)
394 ""
395 (format " Hscroll=%d" (window-hscroll))))
396 (col (current-column)))
397 (if (= pos end)
398 (if (or (/= beg 1) (/= end (1+ total)))
399 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
400 pos total percent beg end col hscroll)
401 (message "point=%d of %d(%d%%) column %d %s"
402 pos total percent col hscroll))
403 (if (or (/= beg 1) (/= end (1+ total)))
2a83421c
RS
404 (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) <%d - %d> column %d %s"
405 (single-key-description char) char char char pos total percent beg end col hscroll)
406 (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) column %d %s"
407 (single-key-description char) char char char pos total percent col hscroll)))))
2076c87c
JB
408
409(defun fundamental-mode ()
410 "Major mode not specialized for anything in particular.
411Other major modes are defined by comparison with this one."
412 (interactive)
413 (kill-all-local-variables))
414
4578d35d 415(defvar read-expression-map (cons 'keymap minibuffer-local-map)
854c16c5
RS
416 "Minibuffer keymap used for reading Lisp expressions.")
417(define-key read-expression-map "\M-\t" 'lisp-complete-symbol)
418
2076c87c
JB
419(put 'eval-expression 'disabled t)
420
8570b0ca
RM
421(defvar read-expression-history nil)
422
423;; We define this, rather than making `eval' interactive,
2076c87c
JB
424;; for the sake of completion of names like eval-region, eval-current-buffer.
425(defun eval-expression (expression)
426 "Evaluate EXPRESSION and print value in minibuffer.
eb57c304 427Value is also consed on to front of the variable `values'."
adca5fa6 428 (interactive
b387ef9a
RS
429 (list (read-from-minibuffer "Eval: "
430 nil read-expression-map t
431 'read-expression-history)))
2076c87c
JB
432 (setq values (cons (eval expression) values))
433 (prin1 (car values) t))
434
435(defun edit-and-eval-command (prompt command)
436 "Prompting with PROMPT, let user edit COMMAND and eval result.
437COMMAND is a Lisp expression. Let user edit that expression in
438the minibuffer, then read and evaluate the result."
b387ef9a
RS
439 (let ((command (read-from-minibuffer prompt
440 (prin1-to-string command)
441 read-expression-map t
442 '(command-history . 1))))
5d6c83ae
KH
443 ;; If command was added to command-history as a string,
444 ;; get rid of that. We want only evallable expressions there.
445 (if (stringp (car command-history))
446 (setq command-history (cdr command-history)))
447
448 ;; If command to be redone does not match front of history,
449 ;; add it to the history.
450 (or (equal command (car command-history))
451 (setq command-history (cons command command-history)))
2076c87c
JB
452 (eval command)))
453
ebb61177 454(defun repeat-complex-command (arg)
2076c87c
JB
455 "Edit and re-evaluate last complex command, or ARGth from last.
456A complex command is one which used the minibuffer.
457The command is placed in the minibuffer as a Lisp form for editing.
458The result is executed, repeating the command as changed.
459If the command has been changed or is not the most recent previous command
460it is added to the front of the command history.
eb6e9899
RS
461You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
462to get different commands to edit and resubmit."
2076c87c 463 (interactive "p")
ba343182 464 (let ((elt (nth (1- arg) command-history))
ebb61177 465 (minibuffer-history-position arg)
ba343182 466 (minibuffer-history-sexp-flag t)
2076c87c
JB
467 newcmd)
468 (if elt
854c16c5 469 (progn
eab22e27 470 (setq newcmd
7908d27c
RS
471 (let ((print-level nil))
472 (read-from-minibuffer
473 "Redo: " (prin1-to-string elt) read-expression-map t
474 (cons 'command-history arg))))
eab22e27 475
db16f109
RS
476 ;; If command was added to command-history as a string,
477 ;; get rid of that. We want only evallable expressions there.
478 (if (stringp (car command-history))
479 (setq command-history (cdr command-history)))
480
481 ;; If command to be redone does not match front of history,
482 ;; add it to the history.
483 (or (equal newcmd (car command-history))
484 (setq command-history (cons newcmd command-history)))
2076c87c
JB
485 (eval newcmd))
486 (ding))))
e91f80c4 487\f
854c16c5
RS
488(defvar minibuffer-history nil
489 "Default minibuffer history list.
490This is used for all minibuffer input
491except when an alternate history list is specified.")
492(defvar minibuffer-history-sexp-flag nil
c2d4b6d9 493 "Non-nil when doing history operations on `command-history'.
854c16c5
RS
494More generally, indicates that the history list being acted on
495contains expressions rather than strings.")
e91f80c4
RS
496(setq minibuffer-history-variable 'minibuffer-history)
497(setq minibuffer-history-position nil)
854c16c5 498(defvar minibuffer-history-search-history nil)
e91f80c4 499
29929437 500(mapcar
d0678801
RM
501 (lambda (key-and-command)
502 (mapcar
503 (lambda (keymap-and-completionp)
504 ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
505 ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
506 ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
507 (define-key (symbol-value (car keymap-and-completionp))
508 (car key-and-command)
509 (let ((command (cdr key-and-command)))
510 (if (consp command)
b5e6f936
RM
511 ;; (and ... nil) => ... turns back on the completion-oriented
512 ;; history commands which rms turned off since they seem to
513 ;; do things he doesn't like.
514 (if (and (cdr keymap-and-completionp) nil) ;XXX turned off
d81362b0 515 (progn (error "EMACS BUG!") (cdr command))
d0678801
RM
516 (car command))
517 command))))
518 '((minibuffer-local-map . nil)
519 (minibuffer-local-ns-map . nil)
520 (minibuffer-local-completion-map . t)
521 (minibuffer-local-must-match-map . t)
522 (read-expression-map . nil))))
d81362b0
RM
523 '(("\en" . (next-history-element . next-complete-history-element))
524 ([next] . (next-history-element . next-complete-history-element))
525 ("\ep" . (previous-history-element . previous-complete-history-element))
526 ([prior] . (previous-history-element . previous-complete-history-element))
29929437
JB
527 ("\er" . previous-matching-history-element)
528 ("\es" . next-matching-history-element)))
e91f80c4 529
e91f80c4 530(defun previous-matching-history-element (regexp n)
854c16c5
RS
531 "Find the previous history element that matches REGEXP.
532\(Previous history elements refer to earlier actions.)
533With prefix argument N, search for Nth previous match.
534If N is negative, find the next or Nth next match."
535 (interactive
c1172a19
RS
536 (let* ((enable-recursive-minibuffers t)
537 (minibuffer-history-sexp-flag nil)
538 (regexp (read-from-minibuffer "Previous element matching (regexp): "
539 nil
540 minibuffer-local-map
541 nil
542 'minibuffer-history-search-history)))
543 ;; Use the last regexp specified, by default, if input is empty.
544 (list (if (string= regexp "")
545 (setcar minibuffer-history-search-history
546 (nth 1 minibuffer-history-search-history))
547 regexp)
854c16c5 548 (prefix-numeric-value current-prefix-arg))))
e91f80c4 549 (let ((history (symbol-value minibuffer-history-variable))
ccc58657 550 prevpos
e91f80c4
RS
551 (pos minibuffer-history-position))
552 (while (/= n 0)
553 (setq prevpos pos)
554 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
555 (if (= pos prevpos)
556 (error (if (= pos 1)
ccc58657
RS
557 "No later matching history item"
558 "No earlier matching history item")))
e91f80c4
RS
559 (if (string-match regexp
560 (if minibuffer-history-sexp-flag
7908d27c
RS
561 (let ((print-level nil))
562 (prin1-to-string (nth (1- pos) history)))
e91f80c4 563 (nth (1- pos) history)))
854c16c5 564 (setq n (+ n (if (< n 0) 1 -1)))))
e91f80c4
RS
565 (setq minibuffer-history-position pos)
566 (erase-buffer)
567 (let ((elt (nth (1- pos) history)))
568 (insert (if minibuffer-history-sexp-flag
7908d27c
RS
569 (let ((print-level nil))
570 (prin1-to-string elt))
e91f80c4 571 elt)))
854c16c5
RS
572 (goto-char (point-min)))
573 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
574 (eq (car (car command-history)) 'next-matching-history-element))
575 (setq command-history (cdr command-history))))
e91f80c4 576
e91f80c4 577(defun next-matching-history-element (regexp n)
854c16c5
RS
578 "Find the next history element that matches REGEXP.
579\(The next history element refers to a more recent action.)
580With prefix argument N, search for Nth next match.
581If N is negative, find the previous or Nth previous match."
582 (interactive
c1172a19
RS
583 (let* ((enable-recursive-minibuffers t)
584 (minibuffer-history-sexp-flag nil)
585 (regexp (read-from-minibuffer "Next element matching (regexp): "
586 nil
587 minibuffer-local-map
588 nil
589 'minibuffer-history-search-history)))
590 ;; Use the last regexp specified, by default, if input is empty.
591 (list (if (string= regexp "")
592 (setcar minibuffer-history-search-history
593 (nth 1 minibuffer-history-search-history))
594 regexp)
854c16c5 595 (prefix-numeric-value current-prefix-arg))))
e91f80c4 596 (previous-matching-history-element regexp (- n)))
2076c87c 597
ebb61177
RS
598(defun next-history-element (n)
599 "Insert the next element of the minibuffer history into the minibuffer."
2076c87c 600 (interactive "p")
0818b15e
RS
601 (or (zerop n)
602 (let ((narg (min (max 1 (- minibuffer-history-position n))
603 (length (symbol-value minibuffer-history-variable)))))
604 (if (or (zerop narg)
605 (= minibuffer-history-position narg))
606 (error (if (if (zerop narg)
607 (> n 0)
608 (= minibuffer-history-position 1))
609 "End of history; no next item"
610 "Beginning of history; no preceding item"))
611 (erase-buffer)
612 (setq minibuffer-history-position narg)
613 (let ((elt (nth (1- minibuffer-history-position)
614 (symbol-value minibuffer-history-variable))))
615 (insert
616 (if minibuffer-history-sexp-flag
617 (let ((print-level nil))
618 (prin1-to-string elt))
619 elt)))
620 (goto-char (point-min))))))
2076c87c 621
ebb61177 622(defun previous-history-element (n)
3ee3a076 623 "Inserts the previous element of the minibuffer history into the minibuffer."
2076c87c 624 (interactive "p")
2c5e21c1 625 (next-history-element (- n)))
d0678801
RM
626
627(defun next-complete-history-element (n)
1f6fcec3 628 "Get next element of history which is a completion of minibuffer contents."
d0678801 629 (interactive "p")
b5e6f936
RM
630 (let ((point-at-start (point)))
631 (next-matching-history-element
632 (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
633 ;; next-matching-history-element always puts us at (point-min).
634 ;; Move to the position we were at before changing the buffer contents.
635 ;; This is still sensical, because the text before point has not changed.
636 (goto-char point-at-start)))
d0678801
RM
637
638(defun previous-complete-history-element (n)
1f6fcec3
RS
639 "\
640Get previous element of history which is a completion of minibuffer contents."
d0678801
RM
641 (interactive "p")
642 (next-complete-history-element (- n)))
e91f80c4 643\f
2076c87c
JB
644(defun goto-line (arg)
645 "Goto line ARG, counting from line 1 at beginning of buffer."
646 (interactive "NGoto line: ")
5f1a943c 647 (setq arg (prefix-numeric-value arg))
2076c87c
JB
648 (save-restriction
649 (widen)
650 (goto-char 1)
651 (if (eq selective-display t)
652 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
653 (forward-line (1- arg)))))
654
655;Put this on C-x u, so we can force that rather than C-_ into startup msg
dff7d67f 656(define-function 'advertised-undo 'undo)
2076c87c
JB
657
658(defun undo (&optional arg)
659 "Undo some previous changes.
660Repeat this command to undo more changes.
661A numeric argument serves as a repeat count."
662 (interactive "*p")
456c617c
RS
663 ;; If we don't get all the way thru, make last-command indicate that
664 ;; for the following command.
665 (setq this-command t)
b553cffa
RS
666 (let ((modified (buffer-modified-p))
667 (recent-save (recent-auto-save-p)))
71e40adf
JB
668 (or (eq (selected-window) (minibuffer-window))
669 (message "Undo!"))
2076c87c
JB
670 (or (eq last-command 'undo)
671 (progn (undo-start)
672 (undo-more 1)))
2076c87c 673 (undo-more (or arg 1))
2512c9f0
RS
674 ;; Don't specify a position in the undo record for the undo command.
675 ;; Instead, undoing this should move point to where the change is.
676 (let ((tail buffer-undo-list)
677 done)
678 (while (and tail (not done) (not (null (car tail))))
679 (if (integerp (car tail))
680 (progn
681 (setq done t)
682 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
683 (setq tail (cdr tail))))
2076c87c 684 (and modified (not (buffer-modified-p))
456c617c
RS
685 (delete-auto-save-file-if-necessary recent-save)))
686 ;; If we do get all the way thru, make this-command indicate that.
687 (setq this-command 'undo))
2076c87c 688
278b0a58
RS
689(defvar pending-undo-list nil
690 "Within a run of consecutive undo commands, list remaining to be undone.")
691
2076c87c 692(defun undo-start ()
ff1fbe3e
RS
693 "Set `pending-undo-list' to the front of the undo list.
694The next call to `undo-more' will undo the most recently made change."
2076c87c
JB
695 (if (eq buffer-undo-list t)
696 (error "No undo information in this buffer"))
697 (setq pending-undo-list buffer-undo-list))
698
699(defun undo-more (count)
700 "Undo back N undo-boundaries beyond what was already undone recently.
ff1fbe3e
RS
701Call `undo-start' to get ready to undo recent changes,
702then call `undo-more' one or more times to undo them."
2076c87c
JB
703 (or pending-undo-list
704 (error "No further undo information"))
705 (setq pending-undo-list (primitive-undo count pending-undo-list)))
706
009ef402
RS
707(defvar shell-command-history nil
708 "History list for some commands that read shell commands.")
709
59fc41e5
RS
710(defvar shell-command-switch "-c"
711 "Switch used to have the shell execute its command line argument.")
712
d0d74413 713(defun shell-command (command &optional output-buffer)
2076c87c
JB
714 "Execute string COMMAND in inferior shell; display output, if any.
715If COMMAND ends in ampersand, execute it asynchronously.
d0d74413
RS
716The output appears in the buffer `*Shell Command*'.
717
718The optional second argument OUTPUT-BUFFER, if non-nil,
719says to put the output in some other buffer.
720If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
721If OUTPUT-BUFFER is not a buffer and not nil,
722insert output in current buffer. (This cannot be done asynchronously.)
723In either case, the output is inserted after point (leaving mark after it)."
aa00b92d
RS
724 (interactive (list (read-from-minibuffer "Shell command: "
725 nil nil nil 'shell-command-history)
726 current-prefix-arg))
d0d74413
RS
727 (if (and output-buffer
728 (not (or (bufferp output-buffer) (stringp output-buffer))))
2076c87c
JB
729 (progn (barf-if-buffer-read-only)
730 (push-mark)
731 ;; We do not use -f for csh; we will not support broken use of
732 ;; .cshrcs. Even the BSD csh manual says to use
733 ;; "if ($?prompt) exit" before things which are not useful
734 ;; non-interactively. Besides, if someone wants their other
735 ;; aliases for shell commands then they can still have them.
736 (call-process shell-file-name nil t nil
59fc41e5 737 shell-command-switch command)
c3e46f0c
RS
738 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
739 ;; It is cleaner to avoid activation, even though the command
740 ;; loop would deactivate the mark because we inserted text.
741 (goto-char (prog1 (mark t)
742 (set-marker (mark-marker) (point)
743 (current-buffer)))))
2076c87c
JB
744 ;; Preserve the match data in case called from a program.
745 (let ((data (match-data)))
746 (unwind-protect
747 (if (string-match "[ \t]*&[ \t]*$" command)
748 ;; Command ending with ampersand means asynchronous.
d0d74413
RS
749 (let ((buffer (get-buffer-create
750 (or output-buffer "*Shell-Command*")))
2076c87c
JB
751 (directory default-directory)
752 proc)
753 ;; Remove the ampersand.
754 (setq command (substring command 0 (match-beginning 0)))
755 ;; If will kill a process, query first.
756 (setq proc (get-buffer-process buffer))
757 (if proc
758 (if (yes-or-no-p "A command is running. Kill it? ")
759 (kill-process proc)
760 (error "Shell command in progress")))
761 (save-excursion
762 (set-buffer buffer)
a9594ce3 763 (setq buffer-read-only nil)
2076c87c
JB
764 (erase-buffer)
765 (display-buffer buffer)
766 (setq default-directory directory)
767 (setq proc (start-process "Shell" buffer
59fc41e5
RS
768 shell-file-name
769 shell-command-switch command))
3f655c8a 770 (setq mode-line-process '(":%s"))
2076c87c
JB
771 (set-process-sentinel proc 'shell-command-sentinel)
772 (set-process-filter proc 'shell-command-filter)
773 ))
774 (shell-command-on-region (point) (point) command nil))
775 (store-match-data data)))))
776
777;; We have a sentinel to prevent insertion of a termination message
778;; in the buffer itself.
779(defun shell-command-sentinel (process signal)
d7e84efb
RS
780 (if (and (memq (process-status process) '(exit signal))
781 (buffer-name (process-buffer process)))
2076c87c
JB
782 (progn
783 (message "%s: %s."
784 (car (cdr (cdr (process-command process))))
785 (substring signal 0 -1))
786 (save-excursion
787 (set-buffer (process-buffer process))
788 (setq mode-line-process nil))
789 (delete-process process))))
790
791(defun shell-command-filter (proc string)
792 ;; Do save-excursion by hand so that we can leave point numerically unchanged
793 ;; despite an insertion immediately after it.
794 (let* ((obuf (current-buffer))
795 (buffer (process-buffer proc))
796 opoint
797 (window (get-buffer-window buffer))
798 (pos (window-start window)))
799 (unwind-protect
800 (progn
801 (set-buffer buffer)
3886f8e2
RS
802 (or (= (point) (point-max))
803 (setq opoint (point)))
2076c87c
JB
804 (goto-char (point-max))
805 (insert-before-markers string))
806 ;; insert-before-markers moved this marker: set it back.
807 (set-window-start window pos)
808 ;; Finish our save-excursion.
3886f8e2
RS
809 (if opoint
810 (goto-char opoint))
2076c87c
JB
811 (set-buffer obuf))))
812
d0d74413 813(defun shell-command-on-region (start end command
56c0450e 814 &optional output-buffer replace)
2076c87c
JB
815 "Execute string COMMAND in inferior shell with region as input.
816Normally display output (if any) in temp buffer `*Shell Command Output*';
817Prefix arg means replace the region with it.
56c0450e
RS
818
819The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
820If REPLACE is non-nil, that means insert the output
4d9bd664 821in place of text from START to END, putting point and mark around it.
2076c87c
JB
822
823If the output is one line, it is displayed in the echo area,
824but it is nonetheless available in buffer `*Shell Command Output*'
56c0450e 825even though that buffer is not automatically displayed.
c42f586d 826If there is no output, or if output is inserted in the current buffer,
56c0450e 827then `*Shell Command Output*' is deleted.
d0d74413 828
56c0450e
RS
829If the optional fourth argument OUTPUT-BUFFER is non-nil,
830that says to put the output in some other buffer.
d0d74413
RS
831If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
832If OUTPUT-BUFFER is not a buffer and not nil,
833insert output in the current buffer.
834In either case, the output is inserted after point (leaving mark after it)."
cae49185
RS
835 (interactive (let ((string
836 ;; Do this before calling region-beginning
837 ;; and region-end, in case subprocess output
838 ;; relocates them while we are in the minibuffer.
839 (read-from-minibuffer "Shell command on region: "
840 nil nil nil
841 'shell-command-history)))
842 (list (region-beginning) (region-end)
843 string
844 current-prefix-arg
4d9bd664
RS
845 current-prefix-arg)))
846 (if (or replace
847 (and output-buffer
848 (not (or (bufferp output-buffer) (stringp output-buffer)))))
2076c87c 849 ;; Replace specified region with output from command.
56c0450e 850 (let ((swap (and replace (< (point) (mark)))))
4d9bd664
RS
851 ;; Don't muck with mark unless REPLACE says we should.
852 (goto-char start)
56c0450e 853 (and replace (push-mark))
2076c87c 854 (call-process-region start end shell-file-name t t nil
59fc41e5 855 shell-command-switch command)
b5f7c943
KH
856 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
857 (and shell-buffer (not (eq shell-buffer (current-buffer)))
858 (kill-buffer shell-buffer)))
4d9bd664 859 ;; Don't muck with mark unless REPLACE says we should.
56c0450e 860 (and replace swap (exchange-point-and-mark)))
2076c87c
JB
861 ;; No prefix argument: put the output in a temp buffer,
862 ;; replacing its entire contents.
d0d74413
RS
863 (let ((buffer (get-buffer-create
864 (or output-buffer "*Shell Command Output*")))
34ee0963
RS
865 (success nil))
866 (unwind-protect
867 (if (eq buffer (current-buffer))
868 ;; If the input is the same buffer as the output,
869 ;; delete everything but the specified region,
870 ;; then replace that region with the output.
a9594ce3
RS
871 (progn (setq buffer-read-only nil)
872 (delete-region end (point-max))
34ee0963
RS
873 (delete-region (point-min) start)
874 (call-process-region (point-min) (point-max)
875 shell-file-name t t nil
59fc41e5 876 shell-command-switch command)
34ee0963
RS
877 (setq success t))
878 ;; Clear the output buffer, then run the command with output there.
879 (save-excursion
880 (set-buffer buffer)
a9594ce3 881 (setq buffer-read-only nil)
34ee0963
RS
882 (erase-buffer))
883 (call-process-region start end shell-file-name
884 nil buffer nil
59fc41e5 885 shell-command-switch command)
34ee0963
RS
886 (setq success t))
887 ;; Report the amount of output.
888 (let ((lines (save-excursion
889 (set-buffer buffer)
890 (if (= (buffer-size) 0)
891 0
892 (count-lines (point-min) (point-max))))))
893 (cond ((= lines 0)
894 (if success
895 (message "(Shell command completed with no output)"))
896 (kill-buffer buffer))
897 ((and success (= lines 1))
898 (message "%s"
899 (save-excursion
900 (set-buffer buffer)
901 (goto-char (point-min))
902 (buffer-substring (point)
4ec982c5 903 (progn (end-of-line) (point))))))
34ee0963
RS
904 (t
905 (set-window-start (display-buffer buffer) 1))))))))
2076c87c 906\f
2076c87c
JB
907(defun forward-to-indentation (arg)
908 "Move forward ARG lines and position at first nonblank character."
909 (interactive "p")
910 (forward-line arg)
911 (skip-chars-forward " \t"))
912
913(defun backward-to-indentation (arg)
914 "Move backward ARG lines and position at first nonblank character."
915 (interactive "p")
916 (forward-line (- arg))
917 (skip-chars-forward " \t"))
918
38ebcf29 919(defvar kill-whole-line nil
dff7d67f 920 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line.")
38ebcf29 921
2076c87c 922(defun kill-line (&optional arg)
dff7d67f 923 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2076c87c
JB
924With prefix argument, kill that many lines from point.
925Negative arguments kill lines backward.
926
927When calling from a program, nil means \"no arg\",
dff7d67f
RS
928a number counts as a prefix arg.
929
930If `kill-whole-line' is non-nil, then kill the whole line
931when given no argument at the beginning of a line."
2076c87c
JB
932 (interactive "P")
933 (kill-region (point)
e6291fe1
RS
934 ;; It is better to move point to the other end of the kill
935 ;; before killing. That way, in a read-only buffer, point
936 ;; moves across the text that is copied to the kill ring.
937 ;; The choice has no effect on undo now that undo records
938 ;; the value of point from before the command was run.
939 (progn
2076c87c
JB
940 (if arg
941 (forward-line (prefix-numeric-value arg))
942 (if (eobp)
943 (signal 'end-of-buffer nil))
38ebcf29 944 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
2076c87c
JB
945 (forward-line 1)
946 (end-of-line)))
947 (point))))
948\f
70e14c01
JB
949;;;; Window system cut and paste hooks.
950
951(defvar interprogram-cut-function nil
952 "Function to call to make a killed region available to other programs.
953
954Most window systems provide some sort of facility for cutting and
9f112a3d
RS
955pasting text between the windows of different programs.
956This variable holds a function that Emacs calls whenever text
957is put in the kill ring, to make the new kill available to other
70e14c01
JB
958programs.
959
9f112a3d
RS
960The function takes one or two arguments.
961The first argument, TEXT, is a string containing
962the text which should be made available.
963The second, PUSH, if non-nil means this is a \"new\" kill;
964nil means appending to an \"old\" kill.")
70e14c01
JB
965
966(defvar interprogram-paste-function nil
967 "Function to call to get text cut from other programs.
968
969Most window systems provide some sort of facility for cutting and
9f112a3d
RS
970pasting text between the windows of different programs.
971This variable holds a function that Emacs calls to obtain
70e14c01
JB
972text that other programs have provided for pasting.
973
974The function should be called with no arguments. If the function
975returns nil, then no other program has provided such text, and the top
976of the Emacs kill ring should be used. If the function returns a
daa37602
JB
977string, that string should be put in the kill ring as the latest kill.
978
979Note that the function should return a string only if a program other
980than Emacs has provided a string for pasting; if Emacs provided the
981most recent string, the function should return nil. If it is
982difficult to tell whether Emacs or some other program provided the
983current string, it is probably good enough to return nil if the string
984is equal (according to `string=') to the last text Emacs provided.")
70e14c01
JB
985
986
987\f
988;;;; The kill ring data structure.
2076c87c
JB
989
990(defvar kill-ring nil
70e14c01
JB
991 "List of killed text sequences.
992Since the kill ring is supposed to interact nicely with cut-and-paste
993facilities offered by window systems, use of this variable should
994interact nicely with `interprogram-cut-function' and
995`interprogram-paste-function'. The functions `kill-new',
996`kill-append', and `current-kill' are supposed to implement this
997interaction; you may want to use them instead of manipulating the kill
998ring directly.")
2076c87c
JB
999
1000(defconst kill-ring-max 30
1001 "*Maximum length of kill ring before oldest elements are thrown away.")
1002
1003(defvar kill-ring-yank-pointer nil
1004 "The tail of the kill ring whose car is the last thing yanked.")
1005
f914dc91 1006(defun kill-new (string &optional replace)
70e14c01
JB
1007 "Make STRING the latest kill in the kill ring.
1008Set the kill-ring-yank pointer to point to it.
f914dc91
KH
1009If `interprogram-cut-function' is non-nil, apply it to STRING.
1010Optional second argument REPLACE non-nil means that STRING will replace
1011the front of the kill ring, rather than being added to the list."
f1d01ba2
KH
1012 (and (fboundp 'menu-bar-update-yank-menu)
1013 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
f914dc91
KH
1014 (if replace
1015 (setcar kill-ring string)
1016 (setq kill-ring (cons string kill-ring))
1017 (if (> (length kill-ring) kill-ring-max)
1018 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
70e14c01
JB
1019 (setq kill-ring-yank-pointer kill-ring)
1020 (if interprogram-cut-function
9f112a3d 1021 (funcall interprogram-cut-function string t)))
70e14c01 1022
2076c87c 1023(defun kill-append (string before-p)
70e14c01
JB
1024 "Append STRING to the end of the latest kill in the kill ring.
1025If BEFORE-P is non-nil, prepend STRING to the kill.
88c1aa79 1026If `interprogram-cut-function' is set, pass the resulting kill to
70e14c01 1027it."
f914dc91
KH
1028 (kill-new (if before-p
1029 (concat string (car kill-ring))
1030 (concat (car kill-ring) string)) t))
70e14c01
JB
1031
1032(defun current-kill (n &optional do-not-move)
1033 "Rotate the yanking point by N places, and then return that kill.
1034If N is zero, `interprogram-paste-function' is set, and calling it
1035returns a string, then that string is added to the front of the
1036kill ring and returned as the latest kill.
1037If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1038yanking point; just return the Nth kill forward."
1039 (let ((interprogram-paste (and (= n 0)
1040 interprogram-paste-function
1041 (funcall interprogram-paste-function))))
1042 (if interprogram-paste
1043 (progn
1044 ;; Disable the interprogram cut function when we add the new
1045 ;; text to the kill ring, so Emacs doesn't try to own the
1046 ;; selection, with identical text.
1047 (let ((interprogram-cut-function nil))
1048 (kill-new interprogram-paste))
1049 interprogram-paste)
1050 (or kill-ring (error "Kill ring is empty"))
47096a67
PE
1051 (let ((ARGth-kill-element
1052 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1053 (length kill-ring))
1054 kill-ring)))
70e14c01
JB
1055 (or do-not-move
1056 (setq kill-ring-yank-pointer ARGth-kill-element))
1057 (car ARGth-kill-element)))))
c88ab9ce 1058
c88ab9ce 1059
70e14c01
JB
1060\f
1061;;;; Commands for manipulating the kill ring.
c88ab9ce 1062
e6291fe1
RS
1063(defvar kill-read-only-ok nil
1064 "*Non-nil means don't signal an error for killing read-only text.")
1065
2076c87c
JB
1066(defun kill-region (beg end)
1067 "Kill between point and mark.
1068The text is deleted but saved in the kill ring.
1069The command \\[yank] can retrieve it from there.
1070\(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
2aa7a8bf
JB
1071If the buffer is read-only, Emacs will beep and refrain from deleting
1072the text, but put the text in the kill ring anyway. This means that
1073you can use the killing commands to copy text from a read-only buffer.
2076c87c
JB
1074
1075This is the primitive for programs to kill text (as opposed to deleting it).
1076Supply two arguments, character numbers indicating the stretch of text
1077 to be killed.
1078Any command that calls this function is a \"kill command\".
1079If the previous command was also a kill command,
1080the text killed this time appends to the text killed last time
1081to make one entry in the kill ring."
2aa7a8bf 1082 (interactive "r")
70e14c01 1083 (cond
2aa7a8bf
JB
1084
1085 ;; If the buffer is read-only, we should beep, in case the person
1086 ;; just isn't aware of this. However, there's no harm in putting
1087 ;; the region's text in the kill ring, anyway.
e6291fe1
RS
1088 ((or (and buffer-read-only (not inhibit-read-only))
1089 (text-property-not-all beg end 'read-only nil))
2aa7a8bf 1090 (copy-region-as-kill beg end)
1537a263 1091 ;; This should always barf, and give us the correct error.
e6291fe1
RS
1092 (if kill-read-only-ok
1093 (message "Read only text copied to kill ring")
626a097c 1094 (setq this-command 'kill-region)
e6291fe1 1095 (barf-if-buffer-read-only)))
2aa7a8bf
JB
1096
1097 ;; In certain cases, we can arrange for the undo list and the kill
1098 ;; ring to share the same string object. This code does that.
70e14c01
JB
1099 ((not (or (eq buffer-undo-list t)
1100 (eq last-command 'kill-region)
713dca1c
RS
1101 ;; Use = since positions may be numbers or markers.
1102 (= beg end)))
70e14c01 1103 ;; Don't let the undo list be truncated before we can even access it.
12bcd3b6
RS
1104 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))
1105 (old-list buffer-undo-list)
1106 tail)
70e14c01 1107 (delete-region beg end)
12bcd3b6
RS
1108 ;; Search back in buffer-undo-list for this string,
1109 ;; in case a change hook made property changes.
1110 (setq tail buffer-undo-list)
1111 (while (not (stringp (car (car tail))))
1112 (setq tail (cdr tail)))
70e14c01
JB
1113 ;; Take the same string recorded for undo
1114 ;; and put it in the kill-ring.
626a097c 1115 (kill-new (car (car tail)))))
2aa7a8bf 1116
70e14c01 1117 (t
2076c87c 1118 (copy-region-as-kill beg end)
626a097c
KH
1119 (delete-region beg end)))
1120 (setq this-command 'kill-region))
2076c87c 1121
a382890a
KH
1122;; copy-region-as-kill no longer sets this-command, because it's confusing
1123;; to get two copies of the text when the user accidentally types M-w and
1124;; then corrects it with the intended C-w.
2076c87c
JB
1125(defun copy-region-as-kill (beg end)
1126 "Save the region as if killed, but don't kill it.
46947372
JB
1127If `interprogram-cut-function' is non-nil, also save the text for a window
1128system cut and paste."
2076c87c
JB
1129 (interactive "r")
1130 (if (eq last-command 'kill-region)
1131 (kill-append (buffer-substring beg end) (< end beg))
70e14c01 1132 (kill-new (buffer-substring beg end)))
2076c87c
JB
1133 nil)
1134
1135(defun kill-ring-save (beg end)
0964e562 1136 "Save the region as if killed, but don't kill it.
230c6b36 1137This command is similar to `copy-region-as-kill', except that it gives
0964e562
JB
1138visual feedback indicating the extent of the region being copied.
1139If `interprogram-cut-function' is non-nil, also save the text for a window
1140system cut and paste."
2076c87c
JB
1141 (interactive "r")
1142 (copy-region-as-kill beg end)
3a801d0c 1143 (if (interactive-p)
66050f10
RS
1144 (let ((other-end (if (= (point) beg) end beg))
1145 (opoint (point))
1146 ;; Inhibit quitting so we can make a quit here
1147 ;; look like a C-g typed as a command.
1148 (inhibit-quit t))
1149 (if (pos-visible-in-window-p other-end (selected-window))
1150 (progn
1151 ;; Swap point and mark.
1152 (set-marker (mark-marker) (point) (current-buffer))
1153 (goto-char other-end)
1154 (sit-for 1)
1155 ;; Swap back.
1156 (set-marker (mark-marker) other-end (current-buffer))
1157 (goto-char opoint)
1158 ;; If user quit, deactivate the mark
1159 ;; as C-g would as a command.
e4e593ae 1160 (and quit-flag mark-active
fcadf1c7 1161 (deactivate-mark)))
66050f10
RS
1162 (let* ((killed-text (current-kill 0))
1163 (message-len (min (length killed-text) 40)))
1164 (if (= (point) beg)
1165 ;; Don't say "killed"; that is misleading.
1166 (message "Saved text until \"%s\""
1167 (substring killed-text (- message-len)))
1168 (message "Saved text from \"%s\""
1169 (substring killed-text 0 message-len))))))))
2076c87c
JB
1170
1171(defun append-next-kill ()
ff1fbe3e 1172 "Cause following command, if it kills, to append to previous kill."
2076c87c
JB
1173 (interactive)
1174 (if (interactive-p)
1175 (progn
1176 (setq this-command 'kill-region)
1177 (message "If the next command is a kill, it will append"))
1178 (setq last-command 'kill-region)))
1179
2076c87c 1180(defun yank-pop (arg)
ff1fbe3e
RS
1181 "Replace just-yanked stretch of killed text with a different stretch.
1182This command is allowed only immediately after a `yank' or a `yank-pop'.
2076c87c 1183At such a time, the region contains a stretch of reinserted
ff1fbe3e 1184previously-killed text. `yank-pop' deletes that text and inserts in its
2076c87c
JB
1185place a different stretch of killed text.
1186
1187With no argument, the previous kill is inserted.
ff1fbe3e
RS
1188With argument N, insert the Nth previous kill.
1189If N is negative, this is a more recent kill.
2076c87c
JB
1190
1191The sequence of kills wraps around, so that after the oldest one
1192comes the newest one."
1193 (interactive "*p")
1194 (if (not (eq last-command 'yank))
1195 (error "Previous command was not a yank"))
1196 (setq this-command 'yank)
9a1277dd
RS
1197 (let ((before (< (point) (mark t))))
1198 (delete-region (point) (mark t))
fd0f4056 1199 (set-marker (mark-marker) (point) (current-buffer))
70e14c01 1200 (insert (current-kill arg))
fd0f4056
RS
1201 (if before
1202 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1203 ;; It is cleaner to avoid activation, even though the command
1204 ;; loop would deactivate the mark because we inserted text.
1205 (goto-char (prog1 (mark t)
1206 (set-marker (mark-marker) (point) (current-buffer))))))
0964e562 1207 nil)
2076c87c
JB
1208
1209(defun yank (&optional arg)
1210 "Reinsert the last stretch of killed text.
1211More precisely, reinsert the stretch of killed text most recently
ff1fbe3e
RS
1212killed OR yanked. Put point at end, and set mark at beginning.
1213With just C-u as argument, same but put point at beginning (and mark at end).
1214With argument N, reinsert the Nth most recently killed stretch of killed
2076c87c
JB
1215text.
1216See also the command \\[yank-pop]."
1217 (interactive "*P")
456c617c
RS
1218 ;; If we don't get all the way thru, make last-command indicate that
1219 ;; for the following command.
1220 (setq this-command t)
2076c87c 1221 (push-mark (point))
70e14c01
JB
1222 (insert (current-kill (cond
1223 ((listp arg) 0)
1224 ((eq arg '-) -1)
1225 (t (1- arg)))))
2076c87c 1226 (if (consp arg)
fd0f4056
RS
1227 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1228 ;; It is cleaner to avoid activation, even though the command
1229 ;; loop would deactivate the mark because we inserted text.
1230 (goto-char (prog1 (mark t)
1231 (set-marker (mark-marker) (point) (current-buffer)))))
456c617c
RS
1232 ;; If we do get all the way thru, make this-command indicate that.
1233 (setq this-command 'yank)
0964e562 1234 nil)
70e14c01
JB
1235
1236(defun rotate-yank-pointer (arg)
1237 "Rotate the yanking point in the kill ring.
1238With argument, rotate that many kills forward (or backward, if negative)."
1239 (interactive "p")
1240 (current-kill arg))
1241
2076c87c
JB
1242\f
1243(defun insert-buffer (buffer)
1244 "Insert after point the contents of BUFFER.
1245Puts mark after the inserted text.
1246BUFFER may be a buffer or a buffer name."
58ff020d 1247 (interactive (list (progn (barf-if-buffer-read-only)
8be055fd
RS
1248 (read-buffer "Insert buffer: "
1249 (other-buffer (current-buffer) t)
1250 t))))
2076c87c
JB
1251 (or (bufferp buffer)
1252 (setq buffer (get-buffer buffer)))
1253 (let (start end newmark)
1254 (save-excursion
1255 (save-excursion
1256 (set-buffer buffer)
1257 (setq start (point-min) end (point-max)))
1258 (insert-buffer-substring buffer start end)
1259 (setq newmark (point)))
1537a263
JB
1260 (push-mark newmark))
1261 nil)
2076c87c
JB
1262
1263(defun append-to-buffer (buffer start end)
1264 "Append to specified buffer the text of the region.
1265It is inserted into that buffer before its point.
1266
1267When calling from a program, give three arguments:
1268BUFFER (or buffer name), START and END.
1269START and END specify the portion of the current buffer to be copied."
70e14c01 1270 (interactive
5d771766 1271 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
23efee2c 1272 (region-beginning) (region-end)))
2076c87c
JB
1273 (let ((oldbuf (current-buffer)))
1274 (save-excursion
1275 (set-buffer (get-buffer-create buffer))
1276 (insert-buffer-substring oldbuf start end))))
1277
1278(defun prepend-to-buffer (buffer start end)
1279 "Prepend to specified buffer the text of the region.
1280It is inserted into that buffer after its point.
1281
1282When calling from a program, give three arguments:
1283BUFFER (or buffer name), START and END.
1284START and END specify the portion of the current buffer to be copied."
1285 (interactive "BPrepend to buffer: \nr")
1286 (let ((oldbuf (current-buffer)))
1287 (save-excursion
1288 (set-buffer (get-buffer-create buffer))
1289 (save-excursion
1290 (insert-buffer-substring oldbuf start end)))))
1291
1292(defun copy-to-buffer (buffer start end)
1293 "Copy to specified buffer the text of the region.
1294It is inserted into that buffer, replacing existing text there.
1295
1296When calling from a program, give three arguments:
1297BUFFER (or buffer name), START and END.
1298START and END specify the portion of the current buffer to be copied."
1299 (interactive "BCopy to buffer: \nr")
1300 (let ((oldbuf (current-buffer)))
1301 (save-excursion
1302 (set-buffer (get-buffer-create buffer))
1303 (erase-buffer)
1304 (save-excursion
1305 (insert-buffer-substring oldbuf start end)))))
1306\f
0bf0c097
RS
1307(defvar mark-even-if-inactive nil
1308 "*Non-nil means you can use the mark even when inactive.
1309This option makes a difference in Transient Mark mode.
1310When the option is non-nil, deactivation of the mark
1311turns off region highlighting, but commands that use the mark
1312behave as if the mark were still active.")
1313
62d1c1fc
RM
1314(put 'mark-inactive 'error-conditions '(mark-inactive error))
1315(put 'mark-inactive 'error-message "The mark is not active now")
1316
af39530e 1317(defun mark (&optional force)
c7c8b31e 1318 "Return this buffer's mark value as integer; error if mark inactive.
af39530e 1319If optional argument FORCE is non-nil, access the mark value
c7c8b31e
RS
1320even if the mark is not currently active, and return nil
1321if there is no mark at all.
af39530e 1322
2076c87c
JB
1323If you are using this in an editing command, you are most likely making
1324a mistake; see the documentation of `set-mark'."
0e3a7b14 1325 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
af39530e 1326 (marker-position (mark-marker))
62d1c1fc 1327 (signal 'mark-inactive nil)))
2076c87c 1328
19d35374
RM
1329;; Many places set mark-active directly, and several of them failed to also
1330;; run deactivate-mark-hook. This shorthand should simplify.
1331(defsubst deactivate-mark ()
1332 "Deactivate the mark by setting `mark-active' to nil.
fcadf1c7 1333\(That makes a difference only in Transient Mark mode.)
19d35374 1334Also runs the hook `deactivate-mark-hook'."
a4b9d3da
RS
1335 (if transient-mark-mode
1336 (progn
1337 (setq mark-active nil)
1338 (run-hooks 'deactivate-mark-hook))))
19d35374 1339
2076c87c
JB
1340(defun set-mark (pos)
1341 "Set this buffer's mark to POS. Don't use this function!
1342That is to say, don't use this function unless you want
1343the user to see that the mark has moved, and you want the previous
1344mark position to be lost.
1345
1346Normally, when a new mark is set, the old one should go on the stack.
1347This is why most applications should use push-mark, not set-mark.
1348
ff1fbe3e 1349Novice Emacs Lisp programmers often try to use the mark for the wrong
2076c87c
JB
1350purposes. The mark saves a location for the user's convenience.
1351Most editing commands should not alter the mark.
1352To remember a location for internal use in the Lisp program,
1353store it in a Lisp variable. Example:
1354
1355 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1356
fcadf1c7
RS
1357 (if pos
1358 (progn
1359 (setq mark-active t)
1360 (run-hooks 'activate-mark-hook)
1361 (set-marker (mark-marker) pos (current-buffer)))
24c22852
RS
1362 ;; Normally we never clear mark-active except in Transient Mark mode.
1363 ;; But when we actually clear out the mark value too,
1364 ;; we must clear mark-active in any mode.
1365 (setq mark-active nil)
1366 (run-hooks 'deactivate-mark-hook)
1367 (set-marker (mark-marker) nil)))
2076c87c
JB
1368
1369(defvar mark-ring nil
e55e2267 1370 "The list of former marks of the current buffer, most recent first.")
2076c87c 1371(make-variable-buffer-local 'mark-ring)
e55e2267 1372(put 'mark-ring 'permanent-local t)
2076c87c
JB
1373
1374(defconst mark-ring-max 16
1375 "*Maximum size of mark ring. Start discarding off end if gets this big.")
1376
dc029f0b
RM
1377(defvar global-mark-ring nil
1378 "The list of saved global marks, most recent first.")
1379
1380(defconst global-mark-ring-max 16
1381 "*Maximum size of global mark ring. \
1382Start discarding off end if gets this big.")
1383
2076c87c
JB
1384(defun set-mark-command (arg)
1385 "Set mark at where point is, or jump to mark.
dc029f0b
RM
1386With no prefix argument, set mark, push old mark position on local mark
1387ring, and push mark on global mark ring.
1388With argument, jump to mark, and pop a new position for mark off the ring
1389\(does not affect global mark ring\).
2076c87c 1390
ff1fbe3e 1391Novice Emacs Lisp programmers often try to use the mark for the wrong
2076c87c
JB
1392purposes. See the documentation of `set-mark' for more information."
1393 (interactive "P")
1394 (if (null arg)
9a1277dd 1395 (progn
fd0f4056 1396 (push-mark nil nil t))
af39530e 1397 (if (null (mark t))
2076c87c 1398 (error "No mark set in this buffer")
9a1277dd 1399 (goto-char (mark t))
2076c87c
JB
1400 (pop-mark))))
1401
fd0f4056 1402(defun push-mark (&optional location nomsg activate)
2076c87c 1403 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
f1382a3d
RM
1404If the last global mark pushed was not in the current buffer,
1405also push LOCATION on the global mark ring.
fd0f4056 1406Display `Mark set' unless the optional second arg NOMSG is non-nil.
8cdc660f 1407In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2076c87c 1408
ff1fbe3e 1409Novice Emacs Lisp programmers often try to use the mark for the wrong
9a1277dd
RS
1410purposes. See the documentation of `set-mark' for more information.
1411
1412In Transient Mark mode, this does not activate the mark."
af39530e 1413 (if (null (mark t))
2076c87c
JB
1414 nil
1415 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1416 (if (> (length mark-ring) mark-ring-max)
1417 (progn
1418 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1419 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
9a1277dd 1420 (set-marker (mark-marker) (or location (point)) (current-buffer))
dc029f0b 1421 ;; Now push the mark on the global mark ring.
f1382a3d 1422 (if (and global-mark-ring
e08d3f7c 1423 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
f1382a3d
RM
1424 ;; The last global mark pushed was in this same buffer.
1425 ;; Don't push another one.
1426 nil
1427 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
dc029f0b
RM
1428 (if (> (length global-mark-ring) global-mark-ring-max)
1429 (progn
1430 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
1431 nil)
f1382a3d 1432 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2076c87c
JB
1433 (or nomsg executing-macro (> (minibuffer-depth) 0)
1434 (message "Mark set"))
8cdc660f
RS
1435 (if (or activate (not transient-mark-mode))
1436 (set-mark (mark t)))
2076c87c
JB
1437 nil)
1438
1439(defun pop-mark ()
1440 "Pop off mark ring into the buffer's actual mark.
1441Does not set point. Does nothing if mark ring is empty."
1442 (if mark-ring
1443 (progn
1444 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
9a1277dd 1445 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
19d35374 1446 (deactivate-mark)
2076c87c 1447 (move-marker (car mark-ring) nil)
9a1277dd 1448 (if (null (mark t)) (ding))
2076c87c
JB
1449 (setq mark-ring (cdr mark-ring)))))
1450
dff7d67f 1451(define-function 'exchange-dot-and-mark 'exchange-point-and-mark)
2076c87c 1452(defun exchange-point-and-mark ()
af39530e
RS
1453 "Put the mark where point is now, and point where the mark is now.
1454This command works even when the mark is not active,
1455and it reactivates the mark."
2076c87c 1456 (interactive nil)
af39530e 1457 (let ((omark (mark t)))
2076c87c
JB
1458 (if (null omark)
1459 (error "No mark set in this buffer"))
1460 (set-mark (point))
1461 (goto-char omark)
1462 nil))
e23c2c21
RS
1463
1464(defun transient-mark-mode (arg)
1465 "Toggle Transient Mark mode.
b411b5fa 1466With arg, turn Transient Mark mode on if arg is positive, off otherwise.
e23c2c21 1467
5dd1220d
RS
1468In Transient Mark mode, when the mark is active, the region is highlighted.
1469Changing the buffer \"deactivates\" the mark.
1470So do certain other operations that set the mark
1471but whose main purpose is something else--for example,
1472incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
e23c2c21
RS
1473 (interactive "P")
1474 (setq transient-mark-mode
1475 (if (null arg)
1476 (not transient-mark-mode)
1477 (> (prefix-numeric-value arg) 0))))
dc029f0b
RM
1478
1479(defun pop-global-mark ()
1480 "Pop off global mark ring and jump to the top location."
1481 (interactive)
52b6d445
RS
1482 ;; Pop entries which refer to non-existent buffers.
1483 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
1484 (setq global-mark-ring (cdr global-mark-ring)))
dc029f0b
RM
1485 (or global-mark-ring
1486 (error "No global mark set"))
1487 (let* ((marker (car global-mark-ring))
1488 (buffer (marker-buffer marker))
1489 (position (marker-position marker)))
34c31301
RS
1490 (setq global-mark-ring (nconc (cdr global-mark-ring)
1491 (list (car global-mark-ring))))
dc029f0b
RM
1492 (set-buffer buffer)
1493 (or (and (>= position (point-min))
1494 (<= position (point-max)))
1495 (widen))
1496 (goto-char position)
1497 (switch-to-buffer buffer)))
2076c87c 1498\f
38ebcf29 1499(defvar next-line-add-newlines t
dff7d67f 1500 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error.")
38ebcf29 1501
2076c87c
JB
1502(defun next-line (arg)
1503 "Move cursor vertically down ARG lines.
1504If there is no character in the target line exactly under the current column,
1505the cursor is positioned after the character in that line which spans this
1506column, or at the end of the line if it is not long enough.
38ebcf29 1507If there is no line in the buffer after this one, behavior depends on the
1a2c3941
RS
1508value of `next-line-add-newlines'. If non-nil, it inserts a newline character
1509to create a line, and moves the cursor to that line. Otherwise it moves the
1510cursor to the end of the buffer (if already at the end of the buffer, an error
38ebcf29 1511is signaled).
2076c87c
JB
1512
1513The command \\[set-goal-column] can be used to create
1514a semipermanent goal column to which this command always moves.
1515Then it does not try to move vertically. This goal column is stored
1516in `goal-column', which is nil when there is none.
1517
1518If you are thinking of using this in a Lisp program, consider
1519using `forward-line' instead. It is usually easier to use
1520and more reliable (no dependence on goal column, etc.)."
1521 (interactive "p")
028922cf
RS
1522 (if (and next-line-add-newlines (= arg 1))
1523 (let ((opoint (point)))
3534a809
RS
1524 (end-of-line)
1525 (if (eobp)
28191e20 1526 (newline 1)
028922cf
RS
1527 (goto-char opoint)
1528 (line-move arg)))
1a2c3941
RS
1529 (if (interactive-p)
1530 (condition-case nil
1531 (line-move arg)
1532 ((beginning-of-buffer end-of-buffer) (ding)))
1533 (line-move arg)))
2076c87c
JB
1534 nil)
1535
1536(defun previous-line (arg)
1537 "Move cursor vertically up ARG lines.
1538If there is no character in the target line exactly over the current column,
1539the cursor is positioned after the character in that line which spans this
1540column, or at the end of the line if it is not long enough.
1541
1542The command \\[set-goal-column] can be used to create
1543a semipermanent goal column to which this command always moves.
1544Then it does not try to move vertically.
1545
1546If you are thinking of using this in a Lisp program, consider using
c2e8a012 1547`forward-line' with a negative argument instead. It is usually easier
2076c87c
JB
1548to use and more reliable (no dependence on goal column, etc.)."
1549 (interactive "p")
1a2c3941
RS
1550 (if (interactive-p)
1551 (condition-case nil
1552 (line-move (- arg))
1553 ((beginning-of-buffer end-of-buffer) (ding)))
1554 (line-move (- arg)))
2076c87c
JB
1555 nil)
1556
1557(defconst track-eol nil
1558 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1559This means moving to the end of each line moved onto.
1560The beginning of a blank line does not count as the end of a line.")
1561
912c6728
RS
1562(defvar goal-column nil
1563 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.")
1564(make-variable-buffer-local 'goal-column)
2076c87c
JB
1565
1566(defvar temporary-goal-column 0
1567 "Current goal column for vertical motion.
1568It is the column where point was
1569at the start of current run of vertical motion commands.
c637ae6f 1570When the `track-eol' feature is doing its job, the value is 9999.")
2076c87c 1571
098fc1fb
RS
1572(defvar line-move-ignore-invisible nil
1573 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
1574Outline mode sets this.")
1575
2076c87c 1576(defun line-move (arg)
1d8f6c8c
RS
1577 (if (not (or (eq last-command 'next-line)
1578 (eq last-command 'previous-line)))
1579 (setq temporary-goal-column
1580 (if (and track-eol (eolp)
1581 ;; Don't count beg of empty line as end of line
1582 ;; unless we just did explicit end-of-line.
1583 (or (not (bolp)) (eq last-command 'end-of-line)))
1584 9999
1585 (current-column))))
098fc1fb
RS
1586 (if (and (not (integerp selective-display))
1587 (not line-move-ignore-invisible))
1588 ;; Use just newline characters.
3534a809
RS
1589 (or (if (> arg 0)
1590 (progn (if (> arg 1) (forward-line (1- arg)))
1591 ;; This way of moving forward ARG lines
1592 ;; verifies that we have a newline after the last one.
1593 ;; It doesn't get confused by intangible text.
1594 (end-of-line)
1595 (zerop (forward-line 1)))
1596 (and (zerop (forward-line arg))
1597 (bolp)))
da4871b3 1598 (signal (if (< arg 0)
1d8f6c8c
RS
1599 'beginning-of-buffer
1600 'end-of-buffer)
1601 nil))
1602 ;; Move by arg lines, but ignore invisible ones.
1603 (while (> arg 0)
1604 (end-of-line)
1605 (and (zerop (vertical-motion 1))
1606 (signal 'end-of-buffer nil))
098fc1fb
RS
1607 ;; If the following character is currently invisible,
1608 ;; skip all characters with that same `invisible' property value.
1609 (while (and (not (eobp))
1610 (let ((prop
1611 (get-char-property (point) 'invisible)))
1612 (if (eq buffer-invisibility-spec t)
1613 prop
937054f3
RS
1614 (or (memq prop buffer-invisibility-spec)
1615 (assq prop buffer-invisibility-spec)))))
1616 (if (get-text-property (point) 'invisible)
1617 (goto-char (next-single-property-change (point) 'invisible))
1618 (goto-char (next-overlay-change (point)))))
1d8f6c8c
RS
1619 (setq arg (1- arg)))
1620 (while (< arg 0)
1621 (beginning-of-line)
1622 (and (zerop (vertical-motion -1))
1623 (signal 'beginning-of-buffer nil))
098fc1fb
RS
1624 (while (and (not (bobp))
1625 (let ((prop
937054f3 1626 (get-char-property (1- (point)) 'invisible)))
098fc1fb
RS
1627 (if (eq buffer-invisibility-spec t)
1628 prop
937054f3
RS
1629 (or (memq prop buffer-invisibility-spec)
1630 (assq prop buffer-invisibility-spec)))))
1631 (if (get-text-property (1- (point)) 'invisible)
1632 (goto-char (previous-single-property-change (point) 'invisible))
1633 (goto-char (previous-overlay-change (point)))))
1d8f6c8c
RS
1634 (setq arg (1+ arg))))
1635 (move-to-column (or goal-column temporary-goal-column))
1636 nil)
2076c87c 1637
d5ab2033
JB
1638;;; Many people have said they rarely use this feature, and often type
1639;;; it by accident. Maybe it shouldn't even be on a key.
1640(put 'set-goal-column 'disabled t)
2076c87c
JB
1641
1642(defun set-goal-column (arg)
1643 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1644Those commands will move to this position in the line moved to
1645rather than trying to keep the same horizontal position.
1646With a non-nil argument, clears out the goal column
912c6728
RS
1647so that \\[next-line] and \\[previous-line] resume vertical motion.
1648The goal column is stored in the variable `goal-column'."
2076c87c
JB
1649 (interactive "P")
1650 (if arg
1651 (progn
1652 (setq goal-column nil)
1653 (message "No goal column"))
1654 (setq goal-column (current-column))
1655 (message (substitute-command-keys
1656 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
1657 goal-column))
1658 nil)
1659\f
0d5bbbf7
ER
1660;;; Partial support for horizontal autoscrolling. Someday, this feature
1661;;; will be built into the C level and all the (hscroll-point-visible) calls
1662;;; will go away.
1663
1664(defvar hscroll-step 0
1665 "*The number of columns to try scrolling a window by when point moves out.
1666If that fails to bring point back on frame, point is centered instead.
1667If this is zero, point is always centered after it moves off frame.")
1668
1669(defun hscroll-point-visible ()
26c5bf8e
KH
1670 "Scrolls the selected window horizontally to make point visible."
1671 (save-excursion
1672 (set-buffer (window-buffer))
1673 (if (not (or truncate-lines
1674 (> (window-hscroll) 0)
1675 (and truncate-partial-width-windows
1676 (< (window-width) (frame-width)))))
1677 ;; Point is always visible when lines are wrapped.
1678 ()
1679 ;; If point is on the invisible part of the line before window-start,
1680 ;; then hscrolling can't bring it back, so reset window-start first.
1681 (and (< (point) (window-start))
1682 (let ((ws-bol (save-excursion
1683 (goto-char (window-start))
1684 (beginning-of-line)
1685 (point))))
1686 (and (>= (point) ws-bol)
1687 (set-window-start nil ws-bol))))
1688 (let* ((here (hscroll-window-column))
1689 (left (min (window-hscroll) 1))
1690 (right (1- (window-width))))
1691 ;; Allow for the truncation glyph, if we're not exactly at eol.
1692 (if (not (and (= here right)
1693 (= (following-char) ?\n)))
1694 (setq right (1- right)))
1695 (cond
1696 ;; If too far away, just recenter. But don't show too much
1697 ;; white space off the end of the line.
1698 ((or (< here (- left hscroll-step))
1699 (> here (+ right hscroll-step)))
1700 (let ((eol (save-excursion (end-of-line) (hscroll-window-column))))
1701 (scroll-left (min (- here (/ (window-width) 2))
1702 (- eol (window-width) -5)))))
1703 ;; Within range. Scroll by one step (or maybe not at all).
1704 ((< here left)
1705 (scroll-right hscroll-step))
1706 ((> here right)
1707 (scroll-left hscroll-step)))))))
1708
1709;; This function returns the window's idea of the display column of point,
1710;; assuming that the window is already known to be truncated rather than
1711;; wrapped, and that we've already handled the case where point is on the
1712;; part of the line before window-start. We ignore window-width; if point
1713;; is beyond the right margin, we want to know how far. The return value
1714;; includes the effects of window-hscroll, window-start, and the prompt
1715;; string in the minibuffer. It may be negative due to hscroll.
1716(defun hscroll-window-column ()
1717 (let* ((hscroll (window-hscroll))
1718 (startpos (save-excursion
1719 (beginning-of-line)
1720 (if (= (point) (save-excursion
1721 (goto-char (window-start))
1722 (beginning-of-line)
1723 (point)))
1724 (goto-char (window-start)))
1725 (point)))
1726 (hpos (+ (if (and (eq (selected-window) (minibuffer-window))
1727 (= 1 (window-start))
1728 (= startpos (point-min)))
1729 (minibuffer-prompt-width)
1730 0)
1731 (min 0 (- 1 hscroll))))
1732 val)
1733 (car (cdr (compute-motion startpos (cons hpos 0)
1734 (point) (cons 0 1)
1735 1000000 (cons hscroll 0) nil)))))
1736
0d5bbbf7 1737
dff7d67f
RS
1738;; rms: (1) The definitions of arrow keys should not simply restate
1739;; what keys they are. The arrow keys should run the ordinary commands.
1740;; (2) The arrow keys are just one of many common ways of moving point
1741;; within a line. Real horizontal autoscrolling would be a good feature,
1742;; but supporting it only for arrow keys is too incomplete to be desirable.
1743
1744;;;;; Make arrow keys do the right thing for improved terminal support
1745;;;;; When we implement true horizontal autoscrolling, right-arrow and
1746;;;;; left-arrow can lose the (if truncate-lines ...) clause and become
1747;;;;; aliases. These functions are bound to the corresponding keyboard
1748;;;;; events in loaddefs.el.
1749
1750;;(defun right-arrow (arg)
1751;; "Move right one character on the screen (with prefix ARG, that many chars).
1752;;Scroll right if needed to keep point horizontally onscreen."
1753;; (interactive "P")
1754;; (forward-char arg)
1755;; (hscroll-point-visible))
1756
1757;;(defun left-arrow (arg)
1758;; "Move left one character on the screen (with prefix ARG, that many chars).
1759;;Scroll left if needed to keep point horizontally onscreen."
1760;; (interactive "P")
1761;; (backward-char arg)
1762;; (hscroll-point-visible))
7492f5a6
RS
1763
1764(defun scroll-other-window-down (lines)
1765 "Scroll the \"other window\" down."
1766 (interactive "P")
1767 (scroll-other-window
1768 ;; Just invert the argument's meaning.
1769 ;; We can do that without knowing which window it will be.
1770 (if (eq lines '-) nil
1771 (if (null lines) '-
1772 (- (prefix-numeric-value lines))))))
3aef9604
RS
1773
1774(defun beginning-of-buffer-other-window (arg)
1775 "Move point to the beginning of the buffer in the other window.
1776Leave mark at previous position.
1777With arg N, put point N/10 of the way from the true beginning."
1778 (interactive "P")
1779 (let ((orig-window (selected-window))
1780 (window (other-window-for-scrolling)))
1781 ;; We use unwind-protect rather than save-window-excursion
1782 ;; because the latter would preserve the things we want to change.
1783 (unwind-protect
1784 (progn
1785 (select-window window)
1786 ;; Set point and mark in that window's buffer.
1787 (beginning-of-buffer arg)
1788 ;; Set point accordingly.
1789 (recenter '(t)))
1790 (select-window orig-window))))
1791
1792(defun end-of-buffer-other-window (arg)
1793 "Move point to the end of the buffer in the other window.
1794Leave mark at previous position.
1795With arg N, put point N/10 of the way from the true end."
1796 (interactive "P")
1797 ;; See beginning-of-buffer-other-window for comments.
1798 (let ((orig-window (selected-window))
1799 (window (other-window-for-scrolling)))
1800 (unwind-protect
1801 (progn
1802 (select-window window)
4500ff36 1803 (end-of-buffer arg)
3aef9604
RS
1804 (recenter '(t)))
1805 (select-window orig-window))))
38ebcf29 1806\f
2076c87c
JB
1807(defun transpose-chars (arg)
1808 "Interchange characters around point, moving forward one character.
1809With prefix arg ARG, effect is to take character before point
1810and drag it forward past ARG other characters (backward if ARG negative).
1811If no argument and at end of line, the previous two chars are exchanged."
1812 (interactive "*P")
1813 (and (null arg) (eolp) (forward-char -1))
1814 (transpose-subr 'forward-char (prefix-numeric-value arg)))
1815
1816(defun transpose-words (arg)
1817 "Interchange words around point, leaving point at end of them.
1818With prefix arg ARG, effect is to take word before or around point
1819and drag it forward past ARG other words (backward if ARG negative).
1820If ARG is zero, the words around or after point and around or after mark
1821are interchanged."
1822 (interactive "*p")
1823 (transpose-subr 'forward-word arg))
1824
1825(defun transpose-sexps (arg)
1826 "Like \\[transpose-words] but applies to sexps.
1827Does not work on a sexp that point is in the middle of
1828if it is a list or string."
1829 (interactive "*p")
1830 (transpose-subr 'forward-sexp arg))
1831
1832(defun transpose-lines (arg)
1833 "Exchange current line and previous line, leaving point after both.
1834With argument ARG, takes previous line and moves it past ARG lines.
1835With argument 0, interchanges line point is in with line mark is in."
1836 (interactive "*p")
1837 (transpose-subr (function
1838 (lambda (arg)
1839 (if (= arg 1)
1840 (progn
1841 ;; Move forward over a line,
1842 ;; but create a newline if none exists yet.
1843 (end-of-line)
1844 (if (eobp)
1845 (newline)
1846 (forward-char 1)))
1847 (forward-line arg))))
1848 arg))
1849
1850(defun transpose-subr (mover arg)
1851 (let (start1 end1 start2 end2)
1852 (if (= arg 0)
1853 (progn
1854 (save-excursion
1855 (funcall mover 1)
1856 (setq end2 (point))
1857 (funcall mover -1)
1858 (setq start2 (point))
1859 (goto-char (mark))
1860 (funcall mover 1)
1861 (setq end1 (point))
1862 (funcall mover -1)
1863 (setq start1 (point))
1864 (transpose-subr-1))
1865 (exchange-point-and-mark)))
1866 (while (> arg 0)
1867 (funcall mover -1)
1868 (setq start1 (point))
1869 (funcall mover 1)
1870 (setq end1 (point))
1871 (funcall mover 1)
1872 (setq end2 (point))
1873 (funcall mover -1)
1874 (setq start2 (point))
1875 (transpose-subr-1)
1876 (goto-char end2)
1877 (setq arg (1- arg)))
1878 (while (< arg 0)
1879 (funcall mover -1)
1880 (setq start2 (point))
1881 (funcall mover -1)
1882 (setq start1 (point))
1883 (funcall mover 1)
1884 (setq end1 (point))
1885 (funcall mover 1)
1886 (setq end2 (point))
1887 (transpose-subr-1)
1888 (setq arg (1+ arg)))))
1889
1890(defun transpose-subr-1 ()
1891 (if (> (min end1 end2) (max start1 start2))
1892 (error "Don't have two things to transpose"))
1893 (let ((word1 (buffer-substring start1 end1))
1894 (word2 (buffer-substring start2 end2)))
1895 (delete-region start2 end2)
1896 (goto-char start2)
1897 (insert word1)
1898 (goto-char (if (< start1 start2) start1
1899 (+ start1 (- (length word1) (length word2)))))
1900 (delete-char (length word1))
1901 (insert word2)))
1902\f
1903(defconst comment-column 32
1904 "*Column to indent right-margin comments to.
8a8fa723
JB
1905Setting this variable automatically makes it local to the current buffer.
1906Each mode establishes a different default value for this variable; you
b492f73b 1907can set the value for a particular mode using that mode's hook.")
2076c87c
JB
1908(make-variable-buffer-local 'comment-column)
1909
1910(defconst comment-start nil
534a0de5 1911 "*String to insert to start a new comment, or nil if no comment syntax.")
2076c87c
JB
1912
1913(defconst comment-start-skip nil
1914 "*Regexp to match the start of a comment plus everything up to its body.
1915If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1916at the place matched by the close of the first pair.")
1917
1918(defconst comment-end ""
1919 "*String to insert to end a new comment.
1920Should be an empty string if comments are terminated by end-of-line.")
1921
ec9a76e3
JB
1922(defconst comment-indent-hook nil
1923 "Obsolete variable for function to compute desired indentation for a comment.
1924This function is called with no args with point at the beginning of
1925the comment's starting delimiter.")
1926
1927(defconst comment-indent-function
2076c87c
JB
1928 '(lambda () comment-column)
1929 "Function to compute desired indentation for a comment.
1930This function is called with no args with point at the beginning of
1931the comment's starting delimiter.")
1932
534a0de5
RS
1933(defconst block-comment-start nil
1934 "*String to insert to start a new comment on a line by itself.
1935If nil, use `comment-start' instead.
1936Note that the regular expression `comment-start-skip' should skip this string
1937as well as the `comment-start' string.")
1938
1939(defconst block-comment-end nil
1940 "*String to insert to end a new comment on a line by itself.
1941Should be an empty string if comments are terminated by end-of-line.
1942If nil, use `comment-end' instead.")
1943
2076c87c
JB
1944(defun indent-for-comment ()
1945 "Indent this line's comment to comment column, or insert an empty comment."
1946 (interactive "*")
1947 (beginning-of-line 1)
534a0de5
RS
1948 (let* ((empty (save-excursion (beginning-of-line)
1949 (looking-at "[ \t]*$")))
1950 (starter (or (and empty block-comment-start) comment-start))
1951 (ender (or (and empty block-comment-end) comment-end)))
1952 (if (null starter)
1953 (error "No comment syntax defined")
1954 (let* ((eolpos (save-excursion (end-of-line) (point)))
1955 cpos indent begpos)
1956 (if (re-search-forward comment-start-skip eolpos 'move)
1957 (progn (setq cpos (point-marker))
1958 ;; Find the start of the comment delimiter.
1959 ;; If there were paren-pairs in comment-start-skip,
1960 ;; position at the end of the first pair.
1961 (if (match-end 1)
1962 (goto-char (match-end 1))
1963 ;; If comment-start-skip matched a string with
1964 ;; internal whitespace (not final whitespace) then
1965 ;; the delimiter start at the end of that
1966 ;; whitespace. Otherwise, it starts at the
1967 ;; beginning of what was matched.
1968 (skip-syntax-backward " " (match-beginning 0))
1969 (skip-syntax-backward "^ " (match-beginning 0)))))
1970 (setq begpos (point))
1971 ;; Compute desired indent.
1972 (if (= (current-column)
1973 (setq indent (if comment-indent-hook
1974 (funcall comment-indent-hook)
1975 (funcall comment-indent-function))))
1976 (goto-char begpos)
1977 ;; If that's different from current, change it.
1978 (skip-chars-backward " \t")
1979 (delete-region (point) begpos)
1980 (indent-to indent))
1981 ;; An existing comment?
1982 (if cpos
1983 (progn (goto-char cpos)
1984 (set-marker cpos nil))
1985 ;; No, insert one.
1986 (insert starter)
1987 (save-excursion
1988 (insert ender)))))))
2076c87c
JB
1989
1990(defun set-comment-column (arg)
1991 "Set the comment column based on point.
1992With no arg, set the comment column to the current column.
1993With just minus as arg, kill any comment on this line.
1994With any other arg, set comment column to indentation of the previous comment
1995 and then align or create a comment on this line at that column."
1996 (interactive "P")
1997 (if (eq arg '-)
1998 (kill-comment nil)
1999 (if arg
2000 (progn
2001 (save-excursion
2002 (beginning-of-line)
2003 (re-search-backward comment-start-skip)
2004 (beginning-of-line)
2005 (re-search-forward comment-start-skip)
2006 (goto-char (match-beginning 0))
2007 (setq comment-column (current-column))
2008 (message "Comment column set to %d" comment-column))
2009 (indent-for-comment))
2010 (setq comment-column (current-column))
2011 (message "Comment column set to %d" comment-column))))
2012
2013(defun kill-comment (arg)
2014 "Kill the comment on this line, if any.
2015With argument, kill comments on that many lines starting with this one."
2016 ;; this function loses in a lot of situations. it incorrectly recognises
2017 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
2018 ;; with multi-line comments, can kill extra whitespace if comment wasn't
2019 ;; through end-of-line, et cetera.
2020 (interactive "P")
2021 (or comment-start-skip (error "No comment syntax defined"))
2022 (let ((count (prefix-numeric-value arg)) endc)
2023 (while (> count 0)
2024 (save-excursion
2025 (end-of-line)
2026 (setq endc (point))
2027 (beginning-of-line)
2028 (and (string< "" comment-end)
2029 (setq endc
2030 (progn
2031 (re-search-forward (regexp-quote comment-end) endc 'move)
2032 (skip-chars-forward " \t")
2033 (point))))
2034 (beginning-of-line)
2035 (if (re-search-forward comment-start-skip endc t)
2036 (progn
2037 (goto-char (match-beginning 0))
2038 (skip-chars-backward " \t")
2039 (kill-region (point) endc)
2040 ;; to catch comments a line beginnings
2041 (indent-according-to-mode))))
2042 (if arg (forward-line 1))
2043 (setq count (1- count)))))
2044
2045(defun comment-region (beg end &optional arg)
f28039bb
RS
2046 "Comment or uncomment each line in the region.
2047With just C-u prefix arg, uncomment each line in region.
2048Numeric prefix arg ARG means use ARG comment characters.
2076c87c
JB
2049If ARG is negative, delete that many comment characters instead.
2050Comments are terminated on each line, even for syntax in which newline does
2051not end the comment. Blank lines do not get comments."
2052 ;; if someone wants it to only put a comment-start at the beginning and
2053 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
2054 ;; is easy enough. No option is made here for other than commenting
2055 ;; every line.
f28039bb 2056 (interactive "r\nP")
2076c87c
JB
2057 (or comment-start (error "No comment syntax is defined"))
2058 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2059 (save-excursion
2060 (save-restriction
f28039bb
RS
2061 (let ((cs comment-start) (ce comment-end)
2062 numarg)
2063 (if (consp arg) (setq numarg t)
2064 (setq numarg (prefix-numeric-value arg))
2065 ;; For positive arg > 1, replicate the comment delims now,
2066 ;; then insert the replicated strings just once.
2067 (while (> numarg 1)
2068 (setq cs (concat cs comment-start)
2069 ce (concat ce comment-end))
2070 (setq numarg (1- numarg))))
2071 ;; Loop over all lines from BEG to END.
2076c87c
JB
2072 (narrow-to-region beg end)
2073 (goto-char beg)
2074 (while (not (eobp))
f28039bb
RS
2075 (if (or (eq numarg t) (< numarg 0))
2076 (progn
2077 ;; Delete comment start from beginning of line.
2078 (if (eq numarg t)
2079 (while (looking-at (regexp-quote cs))
2080 (delete-char (length cs)))
2081 (let ((count numarg))
2082 (while (and (> 1 (setq count (1+ count)))
2083 (looking-at (regexp-quote cs)))
2084 (delete-char (length cs)))))
2085 ;; Delete comment end from end of line.
2086 (if (string= "" ce)
2087 nil
2088 (if (eq numarg t)
2089 (progn
2090 (end-of-line)
2091 ;; This is questionable if comment-end ends in
2092 ;; whitespace. That is pretty brain-damaged,
2093 ;; though.
2094 (skip-chars-backward " \t")
2095 (if (and (>= (- (point) (point-min)) (length ce))
2096 (save-excursion
2097 (backward-char (length ce))
2098 (looking-at (regexp-quote ce))))
2099 (delete-char (- (length ce)))))
ee095968
RS
2100 (let ((count numarg))
2101 (while (> 1 (setq count (1+ count)))
2102 (end-of-line)
2103 ;; this is questionable if comment-end ends in whitespace
2104 ;; that is pretty brain-damaged though
2105 (skip-chars-backward " \t")
2106 (save-excursion
2107 (backward-char (length ce))
2108 (if (looking-at (regexp-quote ce))
2109 (delete-char (length ce))))))))
6e88ed49 2110 (forward-line 1))
f28039bb 2111 ;; Insert at beginning and at end.
2076c87c
JB
2112 (if (looking-at "[ \t]*$") ()
2113 (insert cs)
2114 (if (string= "" ce) ()
2115 (end-of-line)
2116 (insert ce)))
2117 (search-forward "\n" nil 'move)))))))
2118\f
2119(defun backward-word (arg)
2120 "Move backward until encountering the end of a word.
2121With argument, do this that many times.
ff1fbe3e 2122In programs, it is faster to call `forward-word' with negative arg."
9e50756b 2123 (interactive "p")
2076c87c
JB
2124 (forward-word (- arg)))
2125
2126(defun mark-word (arg)
2127 "Set mark arg words away from point."
2128 (interactive "p")
2129 (push-mark
2130 (save-excursion
2131 (forward-word arg)
fd0f4056
RS
2132 (point))
2133 nil t))
2076c87c
JB
2134
2135(defun kill-word (arg)
2136 "Kill characters forward until encountering the end of a word.
2137With argument, do this that many times."
2138 (interactive "p")
e6291fe1 2139 (kill-region (point) (progn (forward-word arg) (point))))
2076c87c
JB
2140
2141(defun backward-kill-word (arg)
2142 "Kill characters backward until encountering the end of a word.
2143With argument, do this that many times."
2144 (interactive "p")
2145 (kill-word (- arg)))
d7c64071 2146
1e8c5ac4
RS
2147(defun current-word (&optional strict)
2148 "Return the word point is on (or a nearby word) as a string.
2149If optional arg STRICT is non-nil, return nil unless point is within
2150or adjacent to a word."
d7c64071
ER
2151 (save-excursion
2152 (let ((oldpoint (point)) (start (point)) (end (point)))
2153 (skip-syntax-backward "w_") (setq start (point))
2154 (goto-char oldpoint)
2155 (skip-syntax-forward "w_") (setq end (point))
2156 (if (and (eq start oldpoint) (eq end oldpoint))
1e8c5ac4
RS
2157 ;; Point is neither within nor adjacent to a word.
2158 (and (not strict)
2159 (progn
2160 ;; Look for preceding word in same line.
2161 (skip-syntax-backward "^w_"
2162 (save-excursion (beginning-of-line)
2163 (point)))
2164 (if (bolp)
2165 ;; No preceding word in same line.
2166 ;; Look for following word in same line.
2167 (progn
2168 (skip-syntax-forward "^w_"
2169 (save-excursion (end-of-line)
2170 (point)))
2171 (setq start (point))
2172 (skip-syntax-forward "w_")
2173 (setq end (point)))
2174 (setq end (point))
2175 (skip-syntax-backward "w_")
2176 (setq start (point)))
2177 (buffer-substring start end)))
2178 (buffer-substring start end)))))
2076c87c
JB
2179\f
2180(defconst fill-prefix nil
2181 "*String for filling to insert at front of new line, or nil for none.
2182Setting this variable automatically makes it local to the current buffer.")
2183(make-variable-buffer-local 'fill-prefix)
2184
2185(defconst auto-fill-inhibit-regexp nil
2186 "*Regexp to match lines which should not be auto-filled.")
2187
2188(defun do-auto-fill ()
eed5698b 2189 (let (fc justify bol give-up)
c18465c4 2190 (if (or (not (setq justify (current-justification)))
eed5698b
RS
2191 (and (setq fc (current-fill-column)) ; make sure this gets set
2192 (eq justify 'left)
2193 (<= (current-column) (setq fc (current-fill-column))))
2194 (save-excursion (beginning-of-line)
2195 (setq bol (point))
2196 (and auto-fill-inhibit-regexp
2197 (looking-at auto-fill-inhibit-regexp))))
2198 nil ;; Auto-filling not required
3db1e3b5
BG
2199 (if (memq justify '(full center right))
2200 (save-excursion (unjustify-current-line)))
eed5698b 2201 (while (and (not give-up) (> (current-column) fc))
756811fb 2202 ;; Determine where to split the line.
2076c87c 2203 (let ((fill-point
756811fb
RS
2204 (let ((opoint (point))
2205 bounce
2206 (first t))
2076c87c 2207 (save-excursion
eed5698b 2208 (move-to-column (1+ fc))
756811fb
RS
2209 ;; Move back to a word boundary.
2210 (while (or first
2211 ;; If this is after period and a single space,
2212 ;; move back once more--we don't want to break
2213 ;; the line there and make it look like a
2214 ;; sentence end.
2215 (and (not (bobp))
2216 (not bounce)
2217 sentence-end-double-space
2218 (save-excursion (forward-char -1)
2219 (and (looking-at "\\. ")
2220 (not (looking-at "\\. "))))))
2221 (setq first nil)
2222 (skip-chars-backward "^ \t\n")
2223 ;; If we find nowhere on the line to break it,
2224 ;; break after one word. Set bounce to t
2225 ;; so we will not keep going in this while loop.
2226 (if (bolp)
2227 (progn
2228 (re-search-forward "[ \t]" opoint t)
2229 (setq bounce t)))
2230 (skip-chars-backward " \t"))
2231 ;; Let fill-point be set to the place where we end up.
2076c87c 2232 (point)))))
756811fb
RS
2233 ;; If that place is not the beginning of the line,
2234 ;; break the line there.
2076c87c
JB
2235 (if (save-excursion
2236 (goto-char fill-point)
2237 (not (bolp)))
34b45e32
RS
2238 (let ((prev-column (current-column)))
2239 ;; If point is at the fill-point, do not `save-excursion'.
2240 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2241 ;; point will end up before it rather than after it.
2242 (if (save-excursion
2243 (skip-chars-backward " \t")
2244 (= (point) fill-point))
28191e20 2245 (indent-new-comment-line t)
34b45e32
RS
2246 (save-excursion
2247 (goto-char fill-point)
28191e20 2248 (indent-new-comment-line t)))
eed5698b
RS
2249 ;; Now do justification, if required
2250 (if (not (eq justify 'left))
2251 (save-excursion
2252 (end-of-line 0)
2253 (justify-current-line justify nil t)))
34b45e32
RS
2254 ;; If making the new line didn't reduce the hpos of
2255 ;; the end of the line, then give up now;
2256 ;; trying again will not help.
2257 (if (>= (current-column) prev-column)
2258 (setq give-up t)))
2076c87c 2259 ;; No place to break => stop trying.
eed5698b
RS
2260 (setq give-up t))))
2261 ;; justify last line
2262 (justify-current-line justify t t))))
2076c87c 2263
d7465b15
RS
2264(defun auto-fill-mode (&optional arg)
2265 "Toggle auto-fill mode.
2266With arg, turn Auto-Fill mode on if and only if arg is positive.
eed5698b 2267In Auto-Fill mode, inserting a space at a column beyond `current-fill-column'
d7465b15
RS
2268automatically breaks the line at a previous space."
2269 (interactive "P")
2270 (prog1 (setq auto-fill-function
2271 (if (if (null arg)
2272 (not auto-fill-function)
2273 (> (prefix-numeric-value arg) 0))
2274 'do-auto-fill
2275 nil))
2276 ;; update mode-line
2277 (set-buffer-modified-p (buffer-modified-p))))
2278
2279;; This holds a document string used to document auto-fill-mode.
2280(defun auto-fill-function ()
2281 "Automatically break line at a previous space, in insertion of text."
2282 nil)
2283
2284(defun turn-on-auto-fill ()
2285 "Unconditionally turn on Auto Fill mode."
2286 (auto-fill-mode 1))
2287
2288(defun set-fill-column (arg)
2289 "Set `fill-column' to current column, or to argument if given.
2290The variable `fill-column' has a separate value for each buffer."
2291 (interactive "P")
2292 (setq fill-column (if (integerp arg) arg (current-column)))
2293 (message "fill-column set to %d" fill-column))
2294\f
2076c87c
JB
2295(defconst comment-multi-line nil
2296 "*Non-nil means \\[indent-new-comment-line] should continue same comment
c88ab9ce
ER
2297on new line, with no new terminator or starter.
2298This is obsolete because you might as well use \\[newline-and-indent].")
2076c87c 2299
28191e20 2300(defun indent-new-comment-line (&optional soft)
d7465b15
RS
2301 "Break line at point and indent, continuing comment if within one.
2302This indents the body of the continued comment
2303under the previous comment line.
c88ab9ce
ER
2304
2305This command is intended for styles where you write a comment per line,
2306starting a new comment (and terminating it if necessary) on each line.
28191e20
RS
2307If you want to continue one comment across several lines, use \\[newline-and-indent].
2308
2309The inserted newline is marked hard if `use-hard-newlines' is true,
2310unless optional argument SOFT is non-nil."
2311 (interactive)
2076c87c
JB
2312 (let (comcol comstart)
2313 (skip-chars-backward " \t")
2314 (delete-region (point)
2315 (progn (skip-chars-forward " \t")
2316 (point)))
eed5698b 2317 (if soft (insert-and-inherit ?\n) (newline 1))
c88ab9ce
ER
2318 (if (not comment-multi-line)
2319 (save-excursion
2320 (if (and comment-start-skip
2321 (let ((opoint (point)))
2322 (forward-line -1)
2323 (re-search-forward comment-start-skip opoint t)))
2324 ;; The old line is a comment.
2325 ;; Set WIN to the pos of the comment-start.
2326 ;; But if the comment is empty, look at preceding lines
2327 ;; to find one that has a nonempty comment.
93b03653
RS
2328
2329 ;; If comment-start-skip contains a \(...\) pair,
2330 ;; the real comment delimiter starts at the end of that pair.
2331 (let ((win (or (match-end 1) (match-beginning 0))))
c88ab9ce
ER
2332 (while (and (eolp) (not (bobp))
2333 (let (opoint)
2334 (beginning-of-line)
2335 (setq opoint (point))
2336 (forward-line -1)
2337 (re-search-forward comment-start-skip opoint t)))
93b03653 2338 (setq win (or (match-end 1) (match-beginning 0))))
c88ab9ce
ER
2339 ;; Indent this line like what we found.
2340 (goto-char win)
2341 (setq comcol (current-column))
c52e9f0b
RS
2342 (setq comstart
2343 (buffer-substring (point) (match-end 0)))))))
2076c87c
JB
2344 (if comcol
2345 (let ((comment-column comcol)
2346 (comment-start comstart)
2347 (comment-end comment-end))
2348 (and comment-end (not (equal comment-end ""))
c88ab9ce 2349; (if (not comment-multi-line)
2076c87c
JB
2350 (progn
2351 (forward-char -1)
2352 (insert comment-end)
2353 (forward-char 1))
c88ab9ce
ER
2354; (setq comment-column (+ comment-column (length comment-start))
2355; comment-start "")
2356; )
2357 )
2076c87c
JB
2358 (if (not (eolp))
2359 (setq comment-end ""))
eed5698b 2360 (insert-and-inherit ?\n)
2076c87c
JB
2361 (forward-char -1)
2362 (indent-for-comment)
2363 (save-excursion
2364 ;; Make sure we delete the newline inserted above.
2365 (end-of-line)
2366 (delete-char 1)))
3db1e3b5
BG
2367 (if (null fill-prefix)
2368 (indent-according-to-mode)
2369 (indent-to-left-margin)
2370 (insert-and-inherit fill-prefix)))))
2076c87c
JB
2371\f
2372(defun set-selective-display (arg)
ff1fbe3e
RS
2373 "Set `selective-display' to ARG; clear it if no arg.
2374When the value of `selective-display' is a number > 0,
2375lines whose indentation is >= that value are not displayed.
2376The variable `selective-display' has a separate value for each buffer."
2076c87c
JB
2377 (interactive "P")
2378 (if (eq selective-display t)
2379 (error "selective-display already in use for marked lines"))
c88ab9ce
ER
2380 (let ((current-vpos
2381 (save-restriction
2382 (narrow-to-region (point-min) (point))
2383 (goto-char (window-start))
2384 (vertical-motion (window-height)))))
2385 (setq selective-display
2386 (and arg (prefix-numeric-value arg)))
2387 (recenter current-vpos))
2076c87c
JB
2388 (set-window-start (selected-window) (window-start (selected-window)))
2389 (princ "selective-display set to " t)
2390 (prin1 selective-display t)
2391 (princ "." t))
2392
b6a22db0
JB
2393(defconst overwrite-mode-textual " Ovwrt"
2394 "The string displayed in the mode line when in overwrite mode.")
2395(defconst overwrite-mode-binary " Bin Ovwrt"
2396 "The string displayed in the mode line when in binary overwrite mode.")
2397
2076c87c
JB
2398(defun overwrite-mode (arg)
2399 "Toggle overwrite mode.
2400With arg, turn overwrite mode on iff arg is positive.
2401In overwrite mode, printing characters typed in replace existing text
b6a22db0
JB
2402on a one-for-one basis, rather than pushing it to the right. At the
2403end of a line, such characters extend the line. Before a tab,
2404such characters insert until the tab is filled in.
2405\\[quoted-insert] still inserts characters in overwrite mode; this
2406is supposed to make it easier to insert characters when necessary."
2407 (interactive "P")
2408 (setq overwrite-mode
2409 (if (if (null arg) (not overwrite-mode)
2410 (> (prefix-numeric-value arg) 0))
2411 'overwrite-mode-textual))
2412 (force-mode-line-update))
2413
2414(defun binary-overwrite-mode (arg)
2415 "Toggle binary overwrite mode.
2416With arg, turn binary overwrite mode on iff arg is positive.
2417In binary overwrite mode, printing characters typed in replace
2418existing text. Newlines are not treated specially, so typing at the
2419end of a line joins the line to the next, with the typed character
2420between them. Typing before a tab character simply replaces the tab
2421with the character typed.
2422\\[quoted-insert] replaces the text at the cursor, just as ordinary
2423typing characters do.
2424
2425Note that binary overwrite mode is not its own minor mode; it is a
2426specialization of overwrite-mode, entered by setting the
2427`overwrite-mode' variable to `overwrite-mode-binary'."
2076c87c
JB
2428 (interactive "P")
2429 (setq overwrite-mode
b6a22db0 2430 (if (if (null arg)
a61099dd 2431 (not (eq overwrite-mode 'overwrite-mode-binary))
b6a22db0
JB
2432 (> (prefix-numeric-value arg) 0))
2433 'overwrite-mode-binary))
2434 (force-mode-line-update))
2076c87c 2435\f
a61099dd
RS
2436(defvar line-number-mode nil
2437 "*Non-nil means display line number in mode line.")
2438
2439(defun line-number-mode (arg)
2440 "Toggle Line Number mode.
2441With arg, turn Line Number mode on iff arg is positive.
2442When Line Number mode is enabled, the line number appears
2443in the mode line."
2444 (interactive "P")
2445 (setq line-number-mode
2446 (if (null arg) (not line-number-mode)
2447 (> (prefix-numeric-value arg) 0)))
2448 (force-mode-line-update))
2449
2076c87c
JB
2450(defvar blink-matching-paren t
2451 "*Non-nil means show matching open-paren when close-paren is inserted.")
2452
37315725
RS
2453(defconst blink-matching-paren-distance 12000
2454 "*If non-nil, is maximum distance to search for matching open-paren.")
2076c87c 2455
72dddf8b
RS
2456(defconst blink-matching-delay 1
2457 "*The number of seconds that `blink-matching-open' will delay at a match.")
2458
2076c87c
JB
2459(defun blink-matching-open ()
2460 "Move cursor momentarily to the beginning of the sexp before point."
2461 (interactive)
2462 (and (> (point) (1+ (point-min)))
2076c87c 2463 blink-matching-paren
7e1ddd45
RS
2464 ;; Verify an even number of quoting characters precede the close.
2465 (= 1 (logand 1 (- (point)
2466 (save-excursion
2467 (forward-char -1)
2468 (skip-syntax-backward "/\\")
2469 (point)))))
2076c87c
JB
2470 (let* ((oldpos (point))
2471 (blinkpos)
2472 (mismatch))
2473 (save-excursion
2474 (save-restriction
2475 (if blink-matching-paren-distance
2476 (narrow-to-region (max (point-min)
2477 (- (point) blink-matching-paren-distance))
2478 oldpos))
2479 (condition-case ()
2480 (setq blinkpos (scan-sexps oldpos -1))
2481 (error nil)))
2482 (and blinkpos (/= (char-syntax (char-after blinkpos))
2483 ?\$)
2484 (setq mismatch
2485 (/= (char-after (1- oldpos))
7492f5a6 2486 (matching-paren (char-after blinkpos)))))
2076c87c
JB
2487 (if mismatch (setq blinkpos nil))
2488 (if blinkpos
2489 (progn
2490 (goto-char blinkpos)
2491 (if (pos-visible-in-window-p)
72dddf8b 2492 (sit-for blink-matching-delay)
2076c87c
JB
2493 (goto-char blinkpos)
2494 (message
2495 "Matches %s"
e9f1d66d 2496 ;; Show what precedes the open in its line, if anything.
2076c87c
JB
2497 (if (save-excursion
2498 (skip-chars-backward " \t")
2499 (not (bolp)))
2500 (buffer-substring (progn (beginning-of-line) (point))
2501 (1+ blinkpos))
e9f1d66d
RS
2502 ;; Show what follows the open in its line, if anything.
2503 (if (save-excursion
2504 (forward-char 1)
2505 (skip-chars-forward " \t")
2506 (not (eolp)))
2507 (buffer-substring blinkpos
2508 (progn (end-of-line) (point)))
267935b9
RS
2509 ;; Otherwise show the previous nonblank line,
2510 ;; if there is one.
2511 (if (save-excursion
2512 (skip-chars-backward "\n \t")
2513 (not (bobp)))
2514 (concat
2515 (buffer-substring (progn
2516 (skip-chars-backward "\n \t")
2517 (beginning-of-line)
2518 (point))
2519 (progn (end-of-line)
2520 (skip-chars-backward " \t")
2521 (point)))
2522 ;; Replace the newline and other whitespace with `...'.
2523 "..."
2524 (buffer-substring blinkpos (1+ blinkpos)))
2525 ;; There is nothing to show except the char itself.
2526 (buffer-substring blinkpos (1+ blinkpos))))))))
2076c87c
JB
2527 (cond (mismatch
2528 (message "Mismatched parentheses"))
2529 ((not blink-matching-paren-distance)
2530 (message "Unmatched parenthesis"))))))))
2531
2532;Turned off because it makes dbx bomb out.
2533(setq blink-paren-function 'blink-matching-open)
2534
9a1277dd
RS
2535;; This executes C-g typed while Emacs is waiting for a command.
2536;; Quitting out of a program does not go through here;
2537;; that happens in the QUIT macro at the C code level.
2076c87c 2538(defun keyboard-quit ()
af39530e
RS
2539 "Signal a quit condition.
2540During execution of Lisp code, this character causes a quit directly.
2541At top-level, as an editor command, this simply beeps."
2076c87c 2542 (interactive)
19d35374 2543 (deactivate-mark)
2076c87c
JB
2544 (signal 'quit nil))
2545
2546(define-key global-map "\C-g" 'keyboard-quit)
c66587fe 2547
1c6c6fde
RS
2548(defvar buffer-quit-function nil
2549 "Function to call to \"quit\" the current buffer, or nil if none.
2550\\[keyboard-escape-quit] calls this function when its more local actions
2551\(such as cancelling a prefix argument, minibuffer or region) do not apply.")
2552
c66587fe
RS
2553(defun keyboard-escape-quit ()
2554 "Exit the current \"mode\" (in a generalized sense of the word).
2555This command can exit an interactive command such as `query-replace',
2556can clear out a prefix argument or a region,
2557can get out of the minibuffer or other recursive edit,
1c6c6fde
RS
2558cancel the use of the current buffer (for special-purpose buffers),
2559or go back to just one window (by deleting all but the selected window)."
c66587fe
RS
2560 (interactive)
2561 (cond ((eq last-command 'mode-exited) nil)
2562 ((> (minibuffer-depth) 0)
2563 (abort-recursive-edit))
2564 (current-prefix-arg
2565 nil)
2566 ((and transient-mark-mode
2567 mark-active)
2568 (deactivate-mark))
1c6c6fde
RS
2569 (buffer-quit-function
2570 (funcall buffer-quit-function))
c66587fe
RS
2571 ((not (one-window-p t))
2572 (delete-other-windows))))
2573
1c6c6fde 2574(define-key global-map "\e\e\e" 'keyboard-escape-quit)
2076c87c
JB
2575\f
2576(defun set-variable (var val)
2577 "Set VARIABLE to VALUE. VALUE is a Lisp object.
2578When using this interactively, supply a Lisp expression for VALUE.
3a801d0c
ER
2579If you want VALUE to be a string, you must surround it with doublequotes.
2580
2581If VARIABLE has a `variable-interactive' property, that is used as if
2582it were the arg to `interactive' (which see) to interactively read the value."
2076c87c
JB
2583 (interactive
2584 (let* ((var (read-variable "Set variable: "))
2585 (minibuffer-help-form
2586 '(funcall myhelp))
2587 (myhelp
2588 (function
2589 (lambda ()
2590 (with-output-to-temp-buffer "*Help*"
2591 (prin1 var)
2592 (princ "\nDocumentation:\n")
2593 (princ (substring (documentation-property var 'variable-documentation)
2594 1))
2595 (if (boundp var)
2596 (let ((print-length 20))
2597 (princ "\n\nCurrent value: ")
2598 (prin1 (symbol-value var))))
e6bcd155
KH
2599 (save-excursion
2600 (set-buffer standard-output)
2601 (help-mode))
2076c87c
JB
2602 nil)))))
2603 (list var
3a801d0c
ER
2604 (let ((prop (get var 'variable-interactive)))
2605 (if prop
2606 ;; Use VAR's `variable-interactive' property
2607 ;; as an interactive spec for prompting.
2608 (call-interactively (list 'lambda '(arg)
2609 (list 'interactive prop)
2610 'arg))
2611 (eval-minibuffer (format "Set %s to value: " var)))))))
2076c87c 2612 (set var val))
e8a700bf
RS
2613\f
2614;; Define the major mode for lists of completions.
2615
ac29eb79
RS
2616(defvar completion-list-mode-map nil)
2617(or completion-list-mode-map
e8a700bf
RS
2618 (let ((map (make-sparse-keymap)))
2619 (define-key map [mouse-2] 'mouse-choose-completion)
eaf76065 2620 (define-key map [down-mouse-2] nil)
80298193 2621 (define-key map "\C-m" 'choose-completion)
1c6c6fde 2622 (define-key map "\e\e\e" 'delete-completion-window)
dde69dbe
RS
2623 (define-key map [left] 'previous-completion)
2624 (define-key map [right] 'next-completion)
ac29eb79 2625 (setq completion-list-mode-map map)))
e8a700bf
RS
2626
2627;; Completion mode is suitable only for specially formatted data.
ac29eb79 2628(put 'completion-list-mode 'mode-class 'special)
e8a700bf 2629
3819736b 2630;; Record the buffer that was current when the completion list was requested.
95ab9b45
RS
2631;; Initial value is nil to avoid some compiler warnings.
2632(defvar completion-reference-buffer nil)
3819736b 2633
f6b293e3
RS
2634;; This records the length of the text at the beginning of the buffer
2635;; which was not included in the completion.
2636(defvar completion-base-size nil)
2637
1c6c6fde
RS
2638(defun delete-completion-window ()
2639 "Delete the completion list window.
2640Go to the window from which completion was requested."
2641 (interactive)
2642 (let ((buf completion-reference-buffer))
2643 (delete-window (selected-window))
2644 (if (get-buffer-window buf)
2645 (select-window (get-buffer-window buf)))))
2646
dde69dbe
RS
2647(defun previous-completion (n)
2648 "Move to the previous item in the completion list."
2649 (interactive "p")
2650 (next-completion (- n)))
2651
2652(defun next-completion (n)
2653 "Move to the next item in the completion list.
2654WIth prefix argument N, move N items (negative N means move backward)."
2655 (interactive "p")
2656 (while (and (> n 0) (not (eobp)))
2657 (let ((prop (get-text-property (point) 'mouse-face)))
2658 ;; If in a completion, move to the end of it.
2659 (if prop
2660 (goto-char (next-single-property-change (point) 'mouse-face)))
2661 ;; Move to start of next one.
2662 (goto-char (next-single-property-change (point) 'mouse-face)))
2663 (setq n (1- n)))
2664 (while (and (< n 0) (not (bobp)))
2665 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
2666 ;; If in a completion, move to the start of it.
2667 (if prop
2668 (goto-char (previous-single-property-change (point) 'mouse-face)))
2669 ;; Move to end of the previous completion.
2670 (goto-char (previous-single-property-change (point) 'mouse-face))
2671 ;; Move to the start of that one.
2672 (goto-char (previous-single-property-change (point) 'mouse-face)))
2673 (setq n (1+ n))))
2674
80298193
RS
2675(defun choose-completion ()
2676 "Choose the completion that point is in or next to."
2677 (interactive)
f6b293e3
RS
2678 (let (beg end completion (buffer completion-reference-buffer)
2679 (base-size completion-base-size))
6096f362
RS
2680 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2681 (setq end (point) beg (1+ (point))))
2682 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3f299281 2683 (setq end (1- (point)) beg (point)))
6096f362
RS
2684 (if (null beg)
2685 (error "No completion here"))
2686 (setq beg (previous-single-property-change beg 'mouse-face))
88dd3c24 2687 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
ab63960f
RS
2688 (setq completion (buffer-substring beg end))
2689 (let ((owindow (selected-window)))
2690 (if (and (one-window-p t 'selected-frame)
2691 (window-dedicated-p (selected-window)))
2692 ;; This is a special buffer's frame
2693 (iconify-frame (selected-frame))
2694 (or (window-dedicated-p (selected-window))
2695 (bury-buffer)))
2696 (select-window owindow))
f6b293e3 2697 (choose-completion-string completion buffer base-size)))
80298193
RS
2698
2699;; Delete the longest partial match for STRING
2700;; that can be found before POINT.
2701(defun choose-completion-delete-max-match (string)
2702 (let ((opoint (point))
2703 (len (min (length string)
2704 (- (point) (point-min)))))
2705 (goto-char (- (point) (length string)))
61bbf6fe
RS
2706 (if completion-ignore-case
2707 (setq string (downcase string)))
80298193
RS
2708 (while (and (> len 0)
2709 (let ((tail (buffer-substring (point)
2710 (+ (point) len))))
61bbf6fe
RS
2711 (if completion-ignore-case
2712 (setq tail (downcase tail)))
80298193
RS
2713 (not (string= tail (substring string 0 len)))))
2714 (setq len (1- len))
2715 (forward-char 1))
2716 (delete-char len)))
2717
f6b293e3 2718(defun choose-completion-string (choice &optional buffer base-size)
80298193 2719 (let ((buffer (or buffer completion-reference-buffer)))
cf52ad58
RS
2720 ;; If BUFFER is a minibuffer, barf unless it's the currently
2721 ;; active minibuffer.
2722 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
2723 (or (not (minibuffer-window-active-p (minibuffer-window)))
2724 (not (equal buffer (window-buffer (minibuffer-window))))))
2725 (error "Minibuffer is not active for completion")
2726 ;; Insert the completion into the buffer where completion was requested.
2727 (set-buffer buffer)
f6b293e3
RS
2728 (if base-size
2729 (delete-region (+ base-size (point-min)) (point))
2730 (choose-completion-delete-max-match choice))
cf52ad58 2731 (insert choice)
63240af1
RS
2732 (remove-text-properties (- (point) (length choice)) (point)
2733 '(mouse-face nil))
cf52ad58
RS
2734 ;; Update point in the window that BUFFER is showing in.
2735 (let ((window (get-buffer-window buffer t)))
2736 (set-window-point window (point)))
2737 ;; If completing for the minibuffer, exit it with this choice.
2738 (and (equal buffer (window-buffer (minibuffer-window)))
8881ad9a
RS
2739 minibuffer-completion-table
2740 (exit-minibuffer)))))
80298193 2741
ac29eb79 2742(defun completion-list-mode ()
e8a700bf 2743 "Major mode for buffers showing lists of possible completions.
80298193
RS
2744Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
2745 to select the completion near point.
2746Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
2747 with the mouse."
e8a700bf
RS
2748 (interactive)
2749 (kill-all-local-variables)
ac29eb79
RS
2750 (use-local-map completion-list-mode-map)
2751 (setq mode-name "Completion List")
2752 (setq major-mode 'completion-list-mode)
f6b293e3
RS
2753 (make-local-variable 'completion-base-size)
2754 (setq completion-base-size nil)
ac29eb79 2755 (run-hooks 'completion-list-mode-hook))
e8a700bf 2756
6096f362
RS
2757(defvar completion-fixup-function nil)
2758
e8a700bf
RS
2759(defun completion-setup-function ()
2760 (save-excursion
3f299281
RS
2761 (let ((mainbuf (current-buffer))
2762 (base-size (- (point-max) (point-min))))
3819736b
RS
2763 (set-buffer standard-output)
2764 (completion-list-mode)
2765 (make-local-variable 'completion-reference-buffer)
2766 (setq completion-reference-buffer mainbuf)
3f299281 2767 (setq completion-base-size base-size)
3819736b
RS
2768 (goto-char (point-min))
2769 (if window-system
2770 (insert (substitute-command-keys
80298193
RS
2771 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
2772 (insert (substitute-command-keys
2773 "In this buffer, type \\[choose-completion] to \
c26bb96e
KH
2774select the completion near point.\n\n"))
2775 (forward-line 1)
6096f362
RS
2776 (while (re-search-forward "[^ \t\n]+\\( [^ \t\n]+\\)*" nil t)
2777 (let ((beg (match-beginning 0))
2778 (end (point)))
2779 (if completion-fixup-function
2780 (funcall completion-fixup-function))
2781 (put-text-property beg (point) 'mouse-face 'highlight)
2782 (goto-char end))))))
c88ab9ce 2783
e8a700bf 2784(add-hook 'completion-setup-hook 'completion-setup-function)
dde69dbe
RS
2785
2786(define-key minibuffer-local-completion-map [prior]
2787 'switch-to-completions)
2788(define-key minibuffer-local-must-match-map [prior]
2789 'switch-to-completions)
2790(define-key minibuffer-local-completion-map "\M-v"
2791 'switch-to-completions)
2792(define-key minibuffer-local-must-match-map "\M-v"
2793 'switch-to-completions)
2794
2795(defun switch-to-completions ()
2796 "Select the completion list window."
2797 (interactive)
2798 (select-window (get-buffer-window "*Completions*"))
2799 (goto-char (point-min))
2800 (search-forward "\n\n")
2801 (forward-line 1))
a3d1480b
JB
2802\f
2803;;;; Keypad support.
2804
2805;;; Make the keypad keys act like ordinary typing keys. If people add
2806;;; bindings for the function key symbols, then those bindings will
2807;;; override these, so this shouldn't interfere with any existing
2808;;; bindings.
2809
0d173134 2810;; Also tell read-char how to handle these keys.
a3d1480b
JB
2811(mapcar
2812 (lambda (keypad-normal)
2813 (let ((keypad (nth 0 keypad-normal))
2814 (normal (nth 1 keypad-normal)))
0d173134 2815 (put keypad 'ascii-character normal)
a3d1480b
JB
2816 (define-key function-key-map (vector keypad) (vector normal))))
2817 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
2818 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
2819 (kp-space ?\ )
2820 (kp-tab ?\t)
2821 (kp-enter ?\r)
2822 (kp-multiply ?*)
2823 (kp-add ?+)
2824 (kp-separator ?,)
2825 (kp-subtract ?-)
2826 (kp-decimal ?.)
2827 (kp-divide ?/)
2828 (kp-equal ?=)))
2829
c88ab9ce 2830;;; simple.el ends here