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