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