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