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