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