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