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