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