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