1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 1997
4 ;; Free Software Foundation, Inc.
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
25 ;; A grab-bag of basic Emacs commands not specifically related to some
26 ;; major mode or to file-handling.
31 "Killing and yanking commands"
34 (defgroup fill-comments nil
35 "Indenting and filling of comments."
39 (defgroup paren-matching nil
40 "Highlight (un)matching of parens and expressions."
44 (defun newline (&optional arg
)
45 "Insert a newline, and move to left margin of the new line if it's blank.
46 The newline is marked with the text-property `hard'.
47 With arg, insert that many newlines.
48 In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
50 (barf-if-buffer-read-only)
51 ;; Inserting a newline at the end of a line produces better redisplay in
52 ;; try_window_id than inserting at the beginning of a line, and the textual
53 ;; result is the same. So, if we're at beginning of line, pretend to be at
54 ;; the end of the previous line.
55 (let ((flag (and (not (bobp))
57 ;; Make sure no functions want to be told about
58 ;; the range of the changes.
59 (not after-change-function
)
60 (not before-change-function
)
61 (not after-change-functions
)
62 (not before-change-functions
)
63 ;; Make sure there are no markers here.
64 (not (buffer-has-markers-at (1- (point))))
65 ;; Make sure no text properties want to know
66 ;; where the change was.
67 (not (get-char-property (1- (point)) 'modification-hooks
))
68 (not (get-char-property (1- (point)) 'insert-behind-hooks
))
70 (not (get-char-property (point) 'insert-in-front-hooks
)))
71 ;; Make sure the newline before point isn't intangible.
72 (not (get-char-property (1- (point)) 'intangible
))
73 ;; Make sure the newline before point isn't read-only.
74 (not (get-char-property (1- (point)) 'read-only
))
75 ;; Make sure the newline before point isn't invisible.
76 (not (get-char-property (1- (point)) 'invisible
))
77 ;; Make sure the newline before point has the same
78 ;; properties as the char before it (if any).
79 (< (or (previous-property-change (point)) -
2)
81 (was-page-start (and (bolp)
82 (looking-at page-delimiter
)))
84 (if flag
(backward-char 1))
85 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
86 ;; Set last-command-char to tell self-insert what to insert.
87 (let ((last-command-char ?
\n)
88 ;; Don't auto-fill if we have a numeric argument.
89 ;; Also not if flag is true (it would fill wrong line);
90 ;; there is no need to since we're at BOL.
91 (auto-fill-function (if (or arg flag
) nil auto-fill-function
)))
93 (self-insert-command (prefix-numeric-value arg
))
94 ;; If we get an error in self-insert-command, put point at right place.
95 (if flag
(forward-char 1))))
96 ;; If we did *not* get an error, cancel that forward-char.
97 (if flag
(backward-char 1))
98 ;; Mark the newline(s) `hard'.
100 (set-hard-newline-properties
101 (- (point) (if arg
(prefix-numeric-value arg
) 1)) (point)))
102 ;; If the newline leaves the previous line blank,
103 ;; and we have a left margin, delete that from the blank line.
106 (goto-char beforepos
)
108 (and (looking-at "[ \t]$")
109 (> (current-left-margin) 0)
110 (delete-region (point) (progn (end-of-line) (point))))))
111 (if flag
(forward-char 1))
112 ;; Indent the line after the newline, except in one case:
113 ;; when we added the newline at the beginning of a line
114 ;; which starts a page.
116 (move-to-left-margin nil t
)))
119 (defun set-hard-newline-properties (from to
)
120 (let ((sticky (get-text-property from
'rear-nonsticky
)))
121 (put-text-property from to
'hard
't
)
122 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
123 (if (and (listp sticky
) (not (memq 'hard sticky
)))
124 (put-text-property from
(point) 'rear-nonsticky
125 (cons 'hard sticky
)))))
127 (defun open-line (arg)
128 "Insert a newline and leave point before it.
129 If there is a fill prefix and/or a left-margin, insert them on the new line
130 if the line would have been blank.
131 With arg N, insert N newlines."
133 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
134 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
140 (if do-left-margin
(indent-to (current-left-margin)))
141 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
148 "Split current line, moving portion beyond point vertically down."
150 (skip-chars-forward " \t")
151 (let ((col (current-column))
157 (defun quoted-insert (arg)
158 "Read next input character and insert it.
159 This is useful for inserting control characters.
161 If the first character you type after this command is an octal digit,
162 you should type a sequence of octal digits which specify a character code.
163 Any nondigit terminates the sequence. If the terminator is a RET,
164 it is discarded; any other terminator is used itself as input.
165 The variable `read-quoted-char-radix' specifies the radix for this feature;
166 set it to 10 or 16 to use decimal or hex instead of octal.
168 In overwrite mode, this function inserts the character anyway, and
169 does not handle octal digits specially. This means that if you use
170 overwrite as your normal editing mode, you can use this function to
171 insert characters when necessary.
173 In binary overwrite mode, this function does overwrite, and octal
174 digits are interpreted as a character code. This is intended to be
175 useful for editing binary files."
177 (let ((char (if (or (not overwrite-mode
)
178 (eq overwrite-mode
'overwrite-mode-binary
))
181 ;; Assume character codes 0200 - 0377 stand for
182 ;; European characters in Latin-1, and convert them
183 ;; to Emacs characters.
184 (and enable-multibyte-characters
187 (setq char
(+ nonascii-insert-offset char
)))
189 (if (eq overwrite-mode
'overwrite-mode-binary
)
192 (insert-and-inherit char
)
193 (setq arg
(1- arg
)))))
195 (defun delete-indentation (&optional arg
)
196 "Join this line to previous and fix up whitespace at join.
197 If there is a fill prefix, delete it from the beginning of this line.
198 With argument, join this line to following line."
201 (if arg
(forward-line 1))
202 (if (eq (preceding-char) ?
\n)
204 (delete-region (point) (1- (point)))
205 ;; If the second line started with the fill prefix,
206 ;; delete the prefix.
208 (<= (+ (point) (length fill-prefix
)) (point-max))
210 (buffer-substring (point)
211 (+ (point) (length fill-prefix
)))))
212 (delete-region (point) (+ (point) (length fill-prefix
))))
213 (fixup-whitespace))))
215 (defun fixup-whitespace ()
216 "Fixup white space between objects around point.
217 Leave one space or none, according to the context."
220 (delete-horizontal-space)
221 (if (or (looking-at "^\\|\\s)")
222 (save-excursion (forward-char -
1)
223 (looking-at "$\\|\\s(\\|\\s'")))
227 (defun delete-horizontal-space ()
228 "Delete all spaces and tabs around point."
230 (skip-chars-backward " \t")
231 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
233 (defun just-one-space ()
234 "Delete all spaces and tabs around point, leaving one space."
236 (skip-chars-backward " \t")
237 (if (= (following-char) ?
)
240 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
242 (defun delete-blank-lines ()
243 "On blank line, delete all surrounding blank lines, leaving just one.
244 On isolated blank line, delete that one.
245 On nonblank line, delete any immediately following blank lines."
247 (let (thisblank singleblank
)
250 (setq thisblank
(looking-at "[ \t]*$"))
251 ;; Set singleblank if there is just one blank line here.
254 (not (looking-at "[ \t]*\n[ \t]*$"))
256 (progn (forward-line -
1)
257 (not (looking-at "[ \t]*$")))))))
258 ;; Delete preceding blank lines, and this one too if it's the only one.
262 (if singleblank
(forward-line 1))
263 (delete-region (point)
264 (if (re-search-backward "[^ \t\n]" nil t
)
265 (progn (forward-line 1) (point))
267 ;; Delete following blank lines, unless the current line is blank
268 ;; and there are no following blank lines.
269 (if (not (and thisblank singleblank
))
273 (delete-region (point)
274 (if (re-search-forward "[^ \t\n]" nil t
)
275 (progn (beginning-of-line) (point))
277 ;; Handle the special case where point is followed by newline and eob.
278 ;; Delete the line, leaving point at eob.
279 (if (looking-at "^[ \t]*\n\\'")
280 (delete-region (point) (point-max)))))
282 (defun back-to-indentation ()
283 "Move point to the first non-whitespace character on this line."
285 (beginning-of-line 1)
286 (skip-chars-forward " \t"))
288 (defun newline-and-indent ()
289 "Insert a newline, then indent according to major mode.
290 Indentation is done using the value of `indent-line-function'.
291 In programming language modes, this is the same as TAB.
292 In some text modes, where TAB inserts a tab, this command indents to the
293 column specified by the function `current-left-margin'."
295 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
297 (indent-according-to-mode))
299 (defun reindent-then-newline-and-indent ()
300 "Reindent current line, insert newline, then indent the new line.
301 Indentation of both lines is done according to the current major mode,
302 which means calling the current value of `indent-line-function'.
303 In programming language modes, this is the same as TAB.
304 In some text modes, where TAB inserts a tab, this indents to the
305 column specified by the function `current-left-margin'."
308 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
309 (indent-according-to-mode))
311 (indent-according-to-mode))
313 ;; Internal subroutine of delete-char
314 (defun kill-forward-chars (arg)
315 (if (listp arg
) (setq arg
(car arg
)))
316 (if (eq arg
'-
) (setq arg -
1))
317 (kill-region (point) (forward-point arg
)))
319 ;; Internal subroutine of backward-delete-char
320 (defun kill-backward-chars (arg)
321 (if (listp arg
) (setq arg
(car arg
)))
322 (if (eq arg
'-
) (setq arg -
1))
323 (kill-region (point) (forward-point (- arg
))))
325 (defun backward-delete-char-untabify (arg &optional killp
)
326 "Delete characters backward, changing tabs into spaces.
327 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
328 Interactively, ARG is the prefix arg (default 1)
329 and KILLP is t if a prefix arg was specified."
330 (interactive "*p\nP")
333 (while (and (> count
0) (not (bobp)))
334 (if (= (preceding-char) ?
\t)
335 (let ((col (current-column)))
337 (setq col
(- col
(current-column)))
341 (setq count
(1- count
)))))
342 (delete-backward-char arg killp
))
344 (defun zap-to-char (arg char
)
345 "Kill up to and including ARG'th occurrence of CHAR.
346 Goes backward if ARG is negative; error if CHAR not found."
347 (interactive "p\ncZap to char: ")
348 (kill-region (point) (progn
349 (search-forward (char-to-string char
) nil nil arg
)
350 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
353 (defun beginning-of-buffer (&optional arg
)
354 "Move point to the beginning of the buffer; leave mark at previous position.
355 With arg N, put point N/10 of the way from the beginning.
357 If the buffer is narrowed, this command uses the beginning and size
358 of the accessible part of the buffer.
360 Don't use this command in Lisp programs!
361 \(goto-char (point-min)) is faster and avoids clobbering the mark."
364 (let ((size (- (point-max) (point-min))))
368 ;; Avoid overflow for large buffer sizes!
369 (* (prefix-numeric-value arg
)
371 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
373 (if arg
(forward-line 1)))
375 (defun end-of-buffer (&optional arg
)
376 "Move point to the end of the buffer; leave mark at previous position.
377 With arg N, put point N/10 of the way from the end.
379 If the buffer is narrowed, this command uses the beginning and size
380 of the accessible part of the buffer.
382 Don't use this command in Lisp programs!
383 \(goto-char (point-max)) is faster and avoids clobbering the mark."
386 (let ((size (- (point-max) (point-min))))
390 ;; Avoid overflow for large buffer sizes!
391 (* (prefix-numeric-value arg
)
393 (/ (* size
(prefix-numeric-value arg
)) 10)))
395 ;; If we went to a place in the middle of the buffer,
396 ;; adjust it to the beginning of a line.
397 (if arg
(forward-line 1)
398 ;; If the end of the buffer is not already on the screen,
399 ;; then scroll specially to put it near, but not at, the bottom.
400 (if (let ((old-point (point)))
402 (goto-char (window-start))
403 (vertical-motion (window-height))
404 (< (point) old-point
)))
406 (overlay-recenter (point))
409 (defun mark-whole-buffer ()
410 "Put point at beginning and mark at end of buffer.
411 You probably should not use this function in Lisp programs;
412 it is usually a mistake for a Lisp function to use any subroutine
413 that uses or sets the mark."
416 (push-mark (point-max) nil t
)
417 (goto-char (point-min)))
419 (defun count-lines-region (start end
)
420 "Print number of lines and characters in the region."
422 (message "Region has %d lines, %d characters"
423 (count-lines start end
) (- end start
)))
426 "Print the current buffer line number and narrowed line number of point."
428 (let ((opoint (point)) start
)
431 (goto-char (point-min))
438 (message "line %d (narrowed line %d)"
439 (1+ (count-lines 1 (point)))
440 (1+ (count-lines start
(point))))
441 (message "Line %d" (1+ (count-lines 1 (point)))))))))
444 (defun count-lines (start end
)
445 "Return number of lines between START and END.
446 This is usually the number of newlines between them,
447 but can be one more if START is not equal to END
448 and the greater of them is not at the start of a line."
451 (narrow-to-region start end
)
452 (goto-char (point-min))
453 (if (eq selective-display t
)
456 (while (re-search-forward "[\n\C-m]" nil t
40)
457 (setq done
(+ 40 done
)))
458 (while (re-search-forward "[\n\C-m]" nil t
1)
459 (setq done
(+ 1 done
)))
460 (goto-char (point-max))
461 (if (and (/= start end
)
465 (- (buffer-size) (forward-line (buffer-size)))))))
467 (defun what-cursor-position (&optional detail
)
468 "Print info on cursor position (on screen and within buffer).
469 With prefix argument, print detailed info of a character on cursor position."
471 (let* ((char (following-char))
475 (total (buffer-size))
476 (percent (if (> total
50000)
477 ;; Avoid overflow from multiplying by 100!
478 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
479 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
480 (hscroll (if (= (window-hscroll) 0)
482 (format " Hscroll=%d" (window-hscroll))))
483 (col (current-column)))
485 (if (or (/= beg
1) (/= end
(1+ total
)))
486 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
487 pos total percent beg end col hscroll
)
488 (message "point=%d of %d(%d%%) column %d %s"
489 pos total percent col hscroll
))
490 (let ((str (if detail
(format " %s" (split-char char
)) "")))
491 (if (or (/= beg
1) (/= end
(1+ total
)))
492 (message "Char: %s (0%o, %d, 0x%x) %s point=%d of %d(%d%%) <%d - %d> column %d %s"
494 (single-key-description char
)
495 (char-to-string char
))
496 char char char str pos total percent beg end col hscroll
)
497 (message "Char: %s (0%o, %d, 0x%x)%s point=%d of %d(%d%%) column %d %s"
499 (single-key-description char
)
500 (char-to-string char
))
501 char char char str pos total percent col hscroll
))))))
503 (defun fundamental-mode ()
504 "Major mode not specialized for anything in particular.
505 Other major modes are defined by comparison with this one."
507 (kill-all-local-variables))
509 (defvar read-expression-map
(cons 'keymap minibuffer-local-map
)
510 "Minibuffer keymap used for reading Lisp expressions.")
511 (define-key read-expression-map
"\M-\t" 'lisp-complete-symbol
)
513 (defvar read-expression-history nil
)
515 ;; We define this, rather than making `eval' interactive,
516 ;; for the sake of completion of names like eval-region, eval-current-buffer.
517 (defun eval-expression (eval-expression-arg
518 &optional eval-expression-insert-value
)
519 "Evaluate EXPRESSION and print value in minibuffer.
520 Value is also consed on to front of the variable `values'."
522 (list (read-from-minibuffer "Eval: "
523 nil read-expression-map t
524 'read-expression-history
)
526 (setq values
(cons (eval eval-expression-arg
) values
))
528 (if eval-expression-insert-value
(current-buffer) t
)))
530 (defun edit-and-eval-command (prompt command
)
531 "Prompting with PROMPT, let user edit COMMAND and eval result.
532 COMMAND is a Lisp expression. Let user edit that expression in
533 the minibuffer, then read and evaluate the result."
534 (let ((command (read-from-minibuffer prompt
535 (prin1-to-string command
)
536 read-expression-map t
537 '(command-history .
1))))
538 ;; If command was added to command-history as a string,
539 ;; get rid of that. We want only evaluable expressions there.
540 (if (stringp (car command-history
))
541 (setq command-history
(cdr command-history
)))
543 ;; If command to be redone does not match front of history,
544 ;; add it to the history.
545 (or (equal command
(car command-history
))
546 (setq command-history
(cons command command-history
)))
549 (defun repeat-complex-command (arg)
550 "Edit and re-evaluate last complex command, or ARGth from last.
551 A complex command is one which used the minibuffer.
552 The command is placed in the minibuffer as a Lisp form for editing.
553 The result is executed, repeating the command as changed.
554 If the command has been changed or is not the most recent previous command
555 it is added to the front of the command history.
556 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
557 to get different commands to edit and resubmit."
559 (let ((elt (nth (1- arg
) command-history
))
564 (let ((print-level nil
)
565 (minibuffer-history-position arg
)
566 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
567 (read-from-minibuffer
568 "Redo: " (prin1-to-string elt
) read-expression-map t
569 (cons 'command-history arg
))))
571 ;; If command was added to command-history as a string,
572 ;; get rid of that. We want only evaluable expressions there.
573 (if (stringp (car command-history
))
574 (setq command-history
(cdr command-history
)))
576 ;; If command to be redone does not match front of history,
577 ;; add it to the history.
578 (or (equal newcmd
(car command-history
))
579 (setq command-history
(cons newcmd command-history
)))
583 (defvar minibuffer-history nil
584 "Default minibuffer history list.
585 This is used for all minibuffer input
586 except when an alternate history list is specified.")
587 (defvar minibuffer-history-sexp-flag nil
588 "Non-nil when doing history operations on `command-history'.
589 More generally, indicates that the history list being acted on
590 contains expressions rather than strings.
591 It is only valid if its value equals the current minibuffer depth,
592 to handle recursive uses of the minibuffer.")
593 (setq minibuffer-history-variable
'minibuffer-history
)
594 (setq minibuffer-history-position nil
)
595 (defvar minibuffer-history-search-history nil
)
598 (lambda (key-and-command)
600 (lambda (keymap-and-completionp)
601 ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
602 ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
603 ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
604 (define-key (symbol-value (car keymap-and-completionp
))
605 (car key-and-command
)
606 (let ((command (cdr key-and-command
)))
608 ;; (and ... nil) => ... turns back on the completion-oriented
609 ;; history commands which rms turned off since they seem to
610 ;; do things he doesn't like.
611 (if (and (cdr keymap-and-completionp
) nil
) ;XXX turned off
612 (progn (error "EMACS BUG!") (cdr command
))
615 '((minibuffer-local-map . nil
)
616 (minibuffer-local-ns-map . nil
)
617 (minibuffer-local-completion-map . t
)
618 (minibuffer-local-must-match-map . t
)
619 (read-expression-map . nil
))))
620 '(("\en" .
(next-history-element . next-complete-history-element
))
621 ([next] . (next-history-element . next-complete-history-element))
622 ("\ep" . (previous-history-element . previous-complete-history-element))
623 ([prior] . (previous-history-element . previous-complete-history-element))
624 ("\er" . previous-matching-history-element)
625 ("\es" . next-matching-history-element)))
627 (defvar minibuffer-text-before-history nil
628 "Text that was in this minibuffer before any history commands.
629 This is nil if there have not yet been any history commands
630 in this use of the minibuffer.")
632 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
634 (defun minibuffer-history-initialize ()
635 (setq minibuffer-text-before-history nil))
637 (defun previous-matching-history-element (regexp n)
638 "Find the previous history element that matches REGEXP.
639 \(Previous history elements refer to earlier actions.)
640 With prefix argument N, search for Nth previous match.
641 If N is negative, find the next or Nth next match.
642 An uppercase letter in REGEXP makes the search case-sensitive."
644 (let* ((enable-recursive-minibuffers t)
645 (regexp (read-from-minibuffer "Previous element matching (regexp): "
649 'minibuffer-history-search-history)))
650 ;; Use the last regexp specified, by default, if input is empty.
651 (list (if (string= regexp "")
652 (if minibuffer-history-search-history
653 (car minibuffer-history-search-history)
654 (error "No previous history search regexp"))
656 (prefix-numeric-value current-prefix-arg))))
657 (if (and (zerop minibuffer-history-position)
658 (null minibuffer-text-before-history))
659 (setq minibuffer-text-before-history (buffer-string)))
660 (let ((history (symbol-value minibuffer-history-variable))
662 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
663 ;; Respect the user's setting for case-fold-search:
667 (pos minibuffer-history-position))
670 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
673 "No later matching history item"
674 "No earlier matching history item")))
675 (if (string-match regexp
676 (if (eq minibuffer-history-sexp-flag
678 (let ((print-level nil))
679 (prin1-to-string (nth (1- pos) history)))
680 (nth (1- pos) history)))
681 (setq n (+ n (if (< n 0) 1 -1)))))
682 (setq minibuffer-history-position pos)
684 (let ((elt (nth (1- pos) history)))
685 (insert (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
686 (let ((print-level nil))
687 (prin1-to-string elt))
689 (goto-char (point-min)))
690 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
691 (eq (car (car command-history)) 'next-matching-history-element))
692 (setq command-history (cdr command-history))))
694 (defun next-matching-history-element (regexp n)
695 "Find the next history element that matches REGEXP.
696 \(The next history element refers to a more recent action.)
697 With prefix argument N, search for Nth next match.
698 If N is negative, find the previous or Nth previous match.
699 An uppercase letter in REGEXP makes the search case-sensitive."
701 (let* ((enable-recursive-minibuffers t)
702 (regexp (read-from-minibuffer "Next element matching (regexp): "
706 'minibuffer-history-search-history)))
707 ;; Use the last regexp specified, by default, if input is empty.
708 (list (if (string= regexp "")
709 (setcar minibuffer-history-search-history
710 (nth 1 minibuffer-history-search-history))
712 (prefix-numeric-value current-prefix-arg))))
713 (previous-matching-history-element regexp (- n)))
715 (defun next-history-element (n)
716 "Insert the next element of the minibuffer history into the minibuffer."
719 (let ((narg (- minibuffer-history-position n))
720 (minimum (if minibuffer-default -1 0))
722 (if (and (zerop minibuffer-history-position)
723 (null minibuffer-text-before-history))
724 (setq minibuffer-text-before-history (buffer-string)))
726 (error "End of history; no next item"))
727 (if (> narg (length (symbol-value minibuffer-history-variable)))
728 (error "Beginning of history; no preceding item"))
730 (setq minibuffer-history-position narg)
732 (setq elt minibuffer-default))
734 (setq elt (or minibuffer-text-before-history ""))
735 (setq minibuffer-text-before-history nil))
736 (t (setq elt (nth (1- minibuffer-history-position)
737 (symbol-value minibuffer-history-variable)))))
739 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
740 (let ((print-level nil))
741 (prin1-to-string elt))
743 (goto-char (point-min)))))
745 (defun previous-history-element (n)
746 "Inserts the previous element of the minibuffer history into the minibuffer."
748 (next-history-element (- n)))
750 (defun next-complete-history-element (n)
751 "Get next element of history which is a completion of minibuffer contents."
753 (let ((point-at-start (point)))
754 (next-matching-history-element
755 (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
756 ;; next-matching-history-element always puts us at (point-min).
757 ;; Move to the position we were at before changing the buffer contents.
758 ;; This is still sensical, because the text before point has not changed.
759 (goto-char point-at-start)))
761 (defun previous-complete-history-element (n)
763 Get previous element of history which is a completion of minibuffer contents."
765 (next-complete-history-element (- n)))
767 (defun goto-line (arg)
768 "Goto line ARG, counting from line 1 at beginning of buffer."
769 (interactive "NGoto line: ")
770 (setq arg (prefix-numeric-value arg))
774 (if (eq selective-display t)
775 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
776 (forward-line (1- arg)))))
778 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
779 (defalias 'advertised-undo 'undo)
781 (defun undo (&optional arg)
782 "Undo some previous changes.
783 Repeat this command to undo more changes.
784 A numeric argument serves as a repeat count."
786 ;; If we don't get all the way thru, make last-command indicate that
787 ;; for the following command.
788 (setq this-command t)
789 (let ((modified (buffer-modified-p))
790 (recent-save (recent-auto-save-p)))
791 (or (eq (selected-window) (minibuffer-window))
793 (or (eq last-command 'undo)
796 (undo-more (or arg 1))
797 ;; Don't specify a position in the undo record for the undo command.
798 ;; Instead, undoing this should move point to where the change is.
799 (let ((tail buffer-undo-list)
801 (while (and tail (not done) (not (null (car tail))))
802 (if (integerp (car tail))
805 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
806 (setq tail (cdr tail))))
807 (and modified (not (buffer-modified-p))
808 (delete-auto-save-file-if-necessary recent-save)))
809 ;; If we do get all the way thru, make this-command indicate that.
810 (setq this-command 'undo))
812 (defvar pending-undo-list nil
813 "Within a run of consecutive undo commands, list remaining to be undone.")
816 "Set `pending-undo-list' to the front of the undo list.
817 The next call to `undo-more' will undo the most recently made change."
818 (if (eq buffer-undo-list t)
819 (error "No undo information in this buffer"))
820 (setq pending-undo-list buffer-undo-list))
822 (defun undo-more (count)
823 "Undo back N undo-boundaries beyond what was already undone recently.
824 Call `undo-start' to get ready to undo recent changes,
825 then call `undo-more' one or more times to undo them."
826 (or pending-undo-list
827 (error "No further undo information"))
828 (setq pending-undo-list (primitive-undo count pending-undo-list)))
830 (defvar shell-command-history nil
831 "History list for some commands that read shell commands.")
833 (defvar shell-command-switch "-c"
834 "Switch used to have the shell execute its command line argument.")
836 (defun shell-command (command &optional output-buffer)
837 "Execute string COMMAND in inferior shell; display output, if any.
839 If COMMAND ends in ampersand, execute it asynchronously.
840 The output appears in the buffer `*Async Shell Command*'.
841 That buffer is in shell mode.
843 Otherwise, COMMAND is executed synchronously. The output appears in the
844 buffer `*Shell Command Output*'.
845 If the output is one line, it is displayed in the echo area *as well*,
846 but it is nonetheless available in buffer `*Shell Command Output*',
847 even though that buffer is not automatically displayed.
848 If there is no output, or if output is inserted in the current buffer,
849 then `*Shell Command Output*' is deleted.
851 To specify a coding system for converting non-ASCII characters
852 in the shell command output, use \\[universal-coding-system-argument]
855 Noninteractive callers can specify coding systems by binding
856 `coding-system-for-read' and `coding-system-for-write'.
858 The optional second argument OUTPUT-BUFFER, if non-nil,
859 says to put the output in some other buffer.
860 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
861 If OUTPUT-BUFFER is not a buffer and not nil,
862 insert output in current buffer. (This cannot be done asynchronously.)
863 In either case, the output is inserted after point (leaving mark after it)."
864 (interactive (list (read-from-minibuffer "Shell command: "
865 nil nil nil 'shell-command-history)
867 ;; Look for a handler in case default-directory is a remote file name.
869 (find-file-name-handler (directory-file-name default-directory)
872 (funcall handler 'shell-command command output-buffer)
873 (if (and output-buffer
874 (not (or (bufferp output-buffer) (stringp output-buffer))))
875 (progn (barf-if-buffer-read-only)
877 ;; We do not use -f for csh; we will not support broken use of
878 ;; .cshrcs. Even the BSD csh manual says to use
879 ;; "if ($?prompt) exit" before things which are not useful
880 ;; non-interactively. Besides, if someone wants their other
881 ;; aliases for shell commands then they can still have them.
882 (call-process shell-file-name nil t nil
883 shell-command-switch command)
884 ;; This is like exchange-point-and-mark, but doesn't
885 ;; activate the mark. It is cleaner to avoid activation,
886 ;; even though the command loop would deactivate the mark
887 ;; because we inserted text.
888 (goto-char (prog1 (mark t)
889 (set-marker (mark-marker) (point)
891 ;; Preserve the match data in case called from a program.
893 (if (string-match "[ \t]*&[ \t]*$" command)
894 ;; Command ending with ampersand means asynchronous.
895 (let ((buffer (get-buffer-create
896 (or output-buffer "*Async Shell Command*")))
897 (directory default-directory)
899 ;; Remove the ampersand.
900 (setq command (substring command 0 (match-beginning 0)))
901 ;; If will kill a process, query first.
902 (setq proc (get-buffer-process buffer))
904 (if (yes-or-no-p "A command is running. Kill it? ")
906 (error "Shell command in progress")))
909 (setq buffer-read-only nil)
911 (display-buffer buffer)
912 (setq default-directory directory)
913 (setq proc (start-process "Shell" buffer shell-file-name
914 shell-command-switch command))
915 (setq mode-line-process '(":%s"))
916 (require 'shell) (shell-mode)
917 (set-process-sentinel proc 'shell-command-sentinel)
919 (shell-command-on-region (point) (point) command output-buffer)
922 ;; We have a sentinel to prevent insertion of a termination message
923 ;; in the buffer itself.
924 (defun shell-command-sentinel (process signal)
925 (if (memq (process-status process) '(exit signal))
927 (car (cdr (cdr (process-command process))))
928 (substring signal 0 -1))))
930 (defun shell-command-on-region (start end command
931 &optional output-buffer replace
933 "Execute string COMMAND in inferior shell with region as input.
934 Normally display output (if any) in temp buffer `*Shell Command Output*';
935 Prefix arg means replace the region with it.
937 To specify a coding system for converting non-ASCII characters
938 in the input and output to the shell command, use \\[universal-coding-system-argument]
939 before this command. By default, the input (from the current buffer)
940 is encoded in the same coding system that will be used to save the file,
941 `buffer-file-coding-system'. If the output is going to replace the region,
942 then it is decoded from that same coding system.
944 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE,
945 ERROR-BUFFER. If REPLACE is non-nil, that means insert the output
946 in place of text from START to END, putting point and mark around it.
947 Noninteractive callers can specify coding systems by binding
948 `coding-system-for-read' and `coding-system-for-write'.
950 If the output is one line, it is displayed in the echo area,
951 but it is nonetheless available in buffer `*Shell Command Output*'
952 even though that buffer is not automatically displayed.
953 If there is no output, or if output is inserted in the current buffer,
954 then `*Shell Command Output*' is deleted.
956 If the optional fourth argument OUTPUT-BUFFER is non-nil,
957 that says to put the output in some other buffer.
958 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
959 If OUTPUT-BUFFER is not a buffer and not nil,
960 insert output in the current buffer.
961 In either case, the output is inserted after point (leaving mark after it).
963 If optional fifth argument ERROR-BUFFER is non-nil, it is a buffer
964 or buffer name to which to direct the command's standard error output.
965 If it is nil, error output is mingled with regular output."
966 (interactive (let ((string
967 ;; Do this before calling region-beginning
968 ;; and region-end, in case subprocess output
969 ;; relocates them while we are in the minibuffer.
970 (read-from-minibuffer "Shell command on region: "
972 'shell-command-history)))
973 ;; call-interactively recognizes region-beginning and
974 ;; region-end specially, leaving them in the history.
975 (list (region-beginning) (region-end)
978 current-prefix-arg)))
981 (concat (file-name-directory temp-file-name-pattern)
982 (make-temp-name "scor"))
986 (not (or (bufferp output-buffer) (stringp output-buffer))))
987 (equal (buffer-name (current-buffer)) "*Shell Command Output*"))
988 ;; Replace specified region with output from command.
989 (let ((swap (and replace (< start end))))
990 ;; Don't muck with mark unless REPLACE says we should.
992 (and replace (push-mark))
993 (call-process-region start end shell-file-name t
997 nil shell-command-switch command)
998 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
999 (and shell-buffer (not (eq shell-buffer (current-buffer)))
1000 (kill-buffer shell-buffer)))
1001 ;; Don't muck with mark unless REPLACE says we should.
1002 (and replace swap (exchange-point-and-mark)))
1003 ;; No prefix argument: put the output in a temp buffer,
1004 ;; replacing its entire contents.
1005 (let ((buffer (get-buffer-create
1006 (or output-buffer "*Shell Command Output*")))
1009 (if (eq buffer (current-buffer))
1010 ;; If the input is the same buffer as the output,
1011 ;; delete everything but the specified region,
1012 ;; then replace that region with the output.
1013 (progn (setq buffer-read-only nil)
1014 (delete-region (max start end) (point-max))
1015 (delete-region (point-min) (min start end))
1016 (call-process-region (point-min) (point-max)
1021 nil shell-command-switch command)
1023 ;; Clear the output buffer, then run the command with output there.
1026 (setq buffer-read-only nil)
1028 (call-process-region start end shell-file-name nil
1030 (list buffer error-file)
1032 nil shell-command-switch command)
1034 ;; Report the amount of output.
1035 (let ((lines (save-excursion
1037 (if (= (buffer-size) 0)
1039 (count-lines (point-min) (point-max))))))
1042 (message "(Shell command completed with no output)"))
1043 (kill-buffer buffer))
1044 ((and success (= lines 1))
1048 (goto-char (point-min))
1049 (buffer-substring (point)
1050 (progn (end-of-line) (point))))))
1054 (goto-char (point-min)))
1055 (display-buffer buffer)))))))
1056 (if (and error-file (file-exists-p error-file))
1058 (set-buffer (get-buffer-create error-buffer))
1059 ;; Do no formatting while reading error file, for fear of looping.
1060 (format-insert-file error-file nil)
1061 (delete-file error-file)))))
1063 (defun shell-command-to-string (command)
1064 "Execute shell command COMMAND and return its output as a string."
1065 (with-output-to-string
1066 (with-current-buffer
1068 (call-process shell-file-name nil t nil shell-command-switch command))))
1070 (defvar universal-argument-map
1071 (let ((map (make-sparse-keymap)))
1072 (define-key map [t] 'universal-argument-other-key)
1073 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1074 (define-key map [switch-frame] nil)
1075 (define-key map [?\C-u] 'universal-argument-more)
1076 (define-key map [?-] 'universal-argument-minus)
1077 (define-key map [?0] 'digit-argument)
1078 (define-key map [?1] 'digit-argument)
1079 (define-key map [?2] 'digit-argument)
1080 (define-key map [?3] 'digit-argument)
1081 (define-key map [?4] 'digit-argument)
1082 (define-key map [?5] 'digit-argument)
1083 (define-key map [?6] 'digit-argument)
1084 (define-key map [?7] 'digit-argument)
1085 (define-key map [?8] 'digit-argument)
1086 (define-key map [?9] 'digit-argument)
1088 "Keymap used while processing \\[universal-argument].")
1090 (defvar universal-argument-num-events nil
1091 "Number of argument-specifying events read by `universal-argument'.
1092 `universal-argument-other-key' uses this to discard those events
1093 from (this-command-keys), and reread only the final command.")
1095 (defun universal-argument ()
1096 "Begin a numeric argument for the following command.
1097 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1098 \\[universal-argument] following the digits or minus sign ends the argument.
1099 \\[universal-argument] without digits or minus sign provides 4 as argument.
1100 Repeating \\[universal-argument] without digits or minus sign
1101 multiplies the argument by 4 each time.
1102 For some commands, just \\[universal-argument] by itself serves as a flag
1103 which is different in effect from any particular numeric argument.
1104 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1106 (setq prefix-arg (list 4))
1107 (setq universal-argument-num-events (length (this-command-keys)))
1108 (setq overriding-terminal-local-map universal-argument-map))
1110 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1111 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1112 (defun universal-argument-more (arg)
1115 (setq prefix-arg (list (* 4 (car arg))))
1117 (setq prefix-arg (list -4))
1118 (setq prefix-arg arg)
1119 (setq overriding-terminal-local-map nil)))
1120 (setq universal-argument-num-events (length (this-command-keys))))
1122 (defun negative-argument (arg)
1123 "Begin a negative numeric argument for the next command.
1124 \\[universal-argument] following digits or minus sign ends the argument."
1126 (cond ((integerp arg)
1127 (setq prefix-arg (- arg)))
1129 (setq prefix-arg nil))
1131 (setq prefix-arg '-)))
1132 (setq universal-argument-num-events (length (this-command-keys)))
1133 (setq overriding-terminal-local-map universal-argument-map))
1135 (defun digit-argument (arg)
1136 "Part of the numeric argument for the next command.
1137 \\[universal-argument] following digits or minus sign ends the argument."
1139 (let ((digit (- (logand last-command-char ?\177) ?0)))
1140 (cond ((integerp arg)
1141 (setq prefix-arg (+ (* arg 10)
1142 (if (< arg 0) (- digit) digit))))
1144 ;; Treat -0 as just -, so that -01 will work.
1145 (setq prefix-arg (if (zerop digit) '- (- digit))))
1147 (setq prefix-arg digit))))
1148 (setq universal-argument-num-events (length (this-command-keys)))
1149 (setq overriding-terminal-local-map universal-argument-map))
1151 ;; For backward compatibility, minus with no modifiers is an ordinary
1152 ;; command if digits have already been entered.
1153 (defun universal-argument-minus (arg)
1156 (universal-argument-other-key arg)
1157 (negative-argument arg)))
1159 ;; Anything else terminates the argument and is left in the queue to be
1160 ;; executed as a command.
1161 (defun universal-argument-other-key (arg)
1163 (setq prefix-arg arg)
1164 (let* ((key (this-command-keys))
1165 (keylist (listify-key-sequence key)))
1166 (setq unread-command-events
1167 (append (nthcdr universal-argument-num-events keylist)
1168 unread-command-events)))
1169 (reset-this-command-lengths)
1170 (setq overriding-terminal-local-map nil))
1172 (defun forward-to-indentation (arg)
1173 "Move forward ARG lines and position at first nonblank character."
1176 (skip-chars-forward " \t"))
1178 (defun backward-to-indentation (arg)
1179 "Move backward ARG lines and position at first nonblank character."
1181 (forward-line (- arg))
1182 (skip-chars-forward " \t"))
1184 (defcustom kill-whole-line nil
1185 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
1189 (defun kill-line (&optional arg)
1190 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
1191 With prefix argument, kill that many lines from point.
1192 Negative arguments kill lines backward.
1194 When calling from a program, nil means \"no arg\",
1195 a number counts as a prefix arg.
1197 To kill a whole line, when point is not at the beginning, type \
1198 \\[beginning-of-line] \\[kill-line] \\[kill-line].
1200 If `kill-whole-line' is non-nil, then this command kills the whole line
1201 including its terminating newline, when used at the beginning of a line
1202 with no argument. As a consequence, you can always kill a whole line
1203 by typing \\[beginning-of-line] \\[kill-line]."
1205 (kill-region (point)
1206 ;; It is better to move point to the other end of the kill
1207 ;; before killing. That way, in a read-only buffer, point
1208 ;; moves across the text that is copied to the kill ring.
1209 ;; The choice has no effect on undo now that undo records
1210 ;; the value of point from before the command was run.
1213 (forward-visible-line (prefix-numeric-value arg))
1215 (signal 'end-of-buffer nil))
1216 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
1217 (forward-visible-line 1)
1218 (end-of-visible-line)))
1221 (defun forward-visible-line (arg)
1222 "Move forward by ARG lines, ignoring currently invisible newlines only.
1223 If ARG is negative, move backward -ARG lines.
1224 If ARG is zero, move to the beginning of the current line."
1228 (or (zerop (forward-line 1))
1229 (signal 'end-of-buffer nil))
1230 ;; If the following character is currently invisible,
1231 ;; skip all characters with that same `invisible' property value,
1232 ;; then find the next newline.
1233 (while (and (not (eobp))
1235 (get-char-property (point) 'invisible)))
1236 (if (eq buffer-invisibility-spec t)
1238 (or (memq prop buffer-invisibility-spec)
1239 (assq prop buffer-invisibility-spec)))))
1241 (if (get-text-property (point) 'invisible)
1242 (or (next-single-property-change (point) 'invisible)
1244 (next-overlay-change (point))))
1245 (or (zerop (forward-line 1))
1246 (signal 'end-of-buffer nil)))
1247 (setq arg (1- arg)))
1249 (while (or first (< arg 0))
1252 (or (zerop (forward-line -1))
1253 (signal 'beginning-of-buffer nil)))
1254 (while (and (not (bobp))
1256 (get-char-property (1- (point)) 'invisible)))
1257 (if (eq buffer-invisibility-spec t)
1259 (or (memq prop buffer-invisibility-spec)
1260 (assq prop buffer-invisibility-spec)))))
1262 (if (get-text-property (1- (point)) 'invisible)
1263 (or (previous-single-property-change (point) 'invisible)
1265 (previous-overlay-change (point))))
1266 (or (zerop (forward-line -1))
1267 (signal 'beginning-of-buffer nil)))
1269 (setq arg (1+ arg)))))
1270 ((beginning-of-buffer end-of-buffer)
1273 (defun end-of-visible-line ()
1274 "Move to end of current visible line."
1276 ;; If the following character is currently invisible,
1277 ;; skip all characters with that same `invisible' property value,
1278 ;; then find the next newline.
1279 (while (and (not (eobp))
1281 (get-char-property (point) 'invisible)))
1282 (if (eq buffer-invisibility-spec t)
1284 (or (memq prop buffer-invisibility-spec)
1285 (assq prop buffer-invisibility-spec)))))
1286 (if (get-text-property (point) 'invisible)
1287 (goto-char (next-single-property-change (point) 'invisible))
1288 (goto-char (next-overlay-change (point))))
1291 ;;;; Window system cut and paste hooks.
1293 (defvar interprogram-cut-function nil
1294 "Function to call to make a killed region available to other programs.
1296 Most window systems provide some sort of facility for cutting and
1297 pasting text between the windows of different programs.
1298 This variable holds a function that Emacs calls whenever text
1299 is put in the kill ring, to make the new kill available to other
1302 The function takes one or two arguments.
1303 The first argument, TEXT, is a string containing
1304 the text which should be made available.
1305 The second, PUSH, if non-nil means this is a \"new\" kill;
1306 nil means appending to an \"old\" kill.")
1308 (defvar interprogram-paste-function nil
1309 "Function to call to get text cut from other programs.
1311 Most window systems provide some sort of facility for cutting and
1312 pasting text between the windows of different programs.
1313 This variable holds a function that Emacs calls to obtain
1314 text that other programs have provided for pasting.
1316 The function should be called with no arguments. If the function
1317 returns nil, then no other program has provided such text, and the top
1318 of the Emacs kill ring should be used. If the function returns a
1319 string, that string should be put in the kill ring as the latest kill.
1321 Note that the function should return a string only if a program other
1322 than Emacs has provided a string for pasting; if Emacs provided the
1323 most recent string, the function should return nil. If it is
1324 difficult to tell whether Emacs or some other program provided the
1325 current string, it is probably good enough to return nil if the string
1326 is equal (according to `string=') to the last text Emacs provided.")
1330 ;;;; The kill ring data structure.
1332 (defvar kill-ring nil
1333 "List of killed text sequences.
1334 Since the kill ring is supposed to interact nicely with cut-and-paste
1335 facilities offered by window systems, use of this variable should
1336 interact nicely with `interprogram-cut-function' and
1337 `interprogram-paste-function'. The functions `kill-new',
1338 `kill-append', and `current-kill' are supposed to implement this
1339 interaction; you may want to use them instead of manipulating the kill
1342 (defcustom kill-ring-max 30
1343 "*Maximum length of kill ring before oldest elements are thrown away."
1347 (defvar kill-ring-yank-pointer nil
1348 "The tail of the kill ring whose car is the last thing yanked.")
1350 (defun kill-new (string &optional replace)
1351 "Make STRING the latest kill in the kill ring.
1352 Set the kill-ring-yank pointer to point to it.
1353 If `interprogram-cut-function' is non-nil, apply it to STRING.
1354 Optional second argument REPLACE non-nil means that STRING will replace
1355 the front of the kill ring, rather than being added to the list."
1356 (and (fboundp 'menu-bar-update-yank-menu)
1357 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1359 (setcar kill-ring string)
1360 (setq kill-ring (cons string kill-ring))
1361 (if (> (length kill-ring) kill-ring-max)
1362 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1363 (setq kill-ring-yank-pointer kill-ring)
1364 (if interprogram-cut-function
1365 (funcall interprogram-cut-function string (not replace))))
1367 (defun kill-append (string before-p)
1368 "Append STRING to the end of the latest kill in the kill ring.
1369 If BEFORE-P is non-nil, prepend STRING to the kill.
1370 If `interprogram-cut-function' is set, pass the resulting kill to
1372 (kill-new (if before-p
1373 (concat string (car kill-ring))
1374 (concat (car kill-ring) string)) t))
1376 (defun current-kill (n &optional do-not-move)
1377 "Rotate the yanking point by N places, and then return that kill.
1378 If N is zero, `interprogram-paste-function' is set, and calling it
1379 returns a string, then that string is added to the front of the
1380 kill ring and returned as the latest kill.
1381 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1382 yanking point; just return the Nth kill forward."
1383 (let ((interprogram-paste (and (= n 0)
1384 interprogram-paste-function
1385 (funcall interprogram-paste-function))))
1386 (if interprogram-paste
1388 ;; Disable the interprogram cut function when we add the new
1389 ;; text to the kill ring, so Emacs doesn't try to own the
1390 ;; selection, with identical text.
1391 (let ((interprogram-cut-function nil))
1392 (kill-new interprogram-paste))
1394 (or kill-ring (error "Kill ring is empty"))
1395 (let ((ARGth-kill-element
1396 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1400 (setq kill-ring-yank-pointer ARGth-kill-element))
1401 (car ARGth-kill-element)))))
1405 ;;;; Commands for manipulating the kill ring.
1407 (defcustom kill-read-only-ok nil
1408 "*Non-nil means don't signal an error for killing read-only text."
1412 (put 'text-read-only 'error-conditions
1413 '(text-read-only buffer-read-only error))
1414 (put 'text-read-only 'error-message "Text is read-only")
1416 (defun kill-region (beg end)
1417 "Kill between point and mark.
1418 The text is deleted but saved in the kill ring.
1419 The command \\[yank] can retrieve it from there.
1420 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
1421 If the buffer is read-only, Emacs will beep and refrain from deleting
1422 the text, but put the text in the kill ring anyway. This means that
1423 you can use the killing commands to copy text from a read-only buffer.
1425 This is the primitive for programs to kill text (as opposed to deleting it).
1426 Supply two arguments, character numbers indicating the stretch of text
1428 Any command that calls this function is a \"kill command\".
1429 If the previous command was also a kill command,
1430 the text killed this time appends to the text killed last time
1431 to make one entry in the kill ring."
1434 ;; Don't let the undo list be truncated before we can even access it.
1435 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))
1436 (old-list buffer-undo-list)
1438 ;; If we can't rely on finding the killed text
1439 ;; in the undo list, save it now as a string.
1440 (string (if (or (eq buffer-undo-list t)
1442 (buffer-substring beg end))))
1443 (delete-region beg end)
1444 ;; Search back in buffer-undo-list for this string,
1445 ;; in case a change hook made property changes.
1446 (setq tail buffer-undo-list)
1448 (while (not (stringp (car (car tail))))
1449 (setq tail (cdr tail)))
1450 ;; If we did not already make the string to use,
1451 ;; use the same one that undo made for us.
1452 (setq string (car (car tail))))
1453 ;; Add that string to the kill ring, one way or another.
1454 (if (eq last-command 'kill-region)
1455 (kill-append string (< end beg))
1457 (setq this-command 'kill-region))
1458 ((buffer-read-only text-read-only)
1459 ;; The code above failed because the buffer, or some of the characters
1460 ;; in the region, are read-only.
1461 ;; We should beep, in case the user just isn't aware of this.
1462 ;; However, there's no harm in putting
1463 ;; the region's text in the kill ring, anyway.
1464 (copy-region-as-kill beg end)
1465 ;; This should always barf, and give us the correct error.
1466 (if kill-read-only-ok
1467 (message "Read only text copied to kill ring")
1468 (setq this-command 'kill-region)
1469 ;; Signal an error if the buffer is read-only.
1470 (barf-if-buffer-read-only)
1471 ;; If the buffer isn't read-only, the text is.
1472 (signal 'text-read-only (list (current-buffer)))))))
1474 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1475 ;; to get two copies of the text when the user accidentally types M-w and
1476 ;; then corrects it with the intended C-w.
1477 (defun copy-region-as-kill (beg end)
1478 "Save the region as if killed, but don't kill it.
1479 In Transient Mark mode, deactivate the mark.
1480 If `interprogram-cut-function' is non-nil, also save the text for a window
1481 system cut and paste."
1483 (if (eq last-command 'kill-region)
1484 (kill-append (buffer-substring beg end) (< end beg))
1485 (kill-new (buffer-substring beg end)))
1486 (if transient-mark-mode
1487 (setq mark-active nil))
1490 (defun kill-ring-save (beg end)
1491 "Save the region as if killed, but don't kill it.
1492 In Transient Mark mode, deactivate the mark.
1493 If `interprogram-cut-function' is non-nil, also save the text for a window
1494 system cut and paste.
1496 This command is similar to `copy-region-as-kill', except that it gives
1497 visual feedback indicating the extent of the region being copied."
1499 (copy-region-as-kill beg end)
1501 (let ((other-end (if (= (point) beg) end beg))
1503 ;; Inhibit quitting so we can make a quit here
1504 ;; look like a C-g typed as a command.
1506 (if (pos-visible-in-window-p other-end (selected-window))
1508 ;; Swap point and mark.
1509 (set-marker (mark-marker) (point) (current-buffer))
1510 (goto-char other-end)
1513 (set-marker (mark-marker) other-end (current-buffer))
1515 ;; If user quit, deactivate the mark
1516 ;; as C-g would as a command.
1517 (and quit-flag mark-active
1519 (let* ((killed-text (current-kill 0))
1520 (message-len (min (length killed-text) 40)))
1522 ;; Don't say "killed"; that is misleading.
1523 (message "Saved text until \"%s\""
1524 (substring killed-text (- message-len)))
1525 (message "Saved text from \"%s\""
1526 (substring killed-text 0 message-len))))))))
1528 (defun append-next-kill ()
1529 "Cause following command, if it kills, to append to previous kill."
1533 (setq this-command 'kill-region)
1534 (message "If the next command is a kill, it will append"))
1535 (setq last-command 'kill-region)))
1537 (defun yank-pop (arg)
1538 "Replace just-yanked stretch of killed text with a different stretch.
1539 This command is allowed only immediately after a `yank' or a `yank-pop'.
1540 At such a time, the region contains a stretch of reinserted
1541 previously-killed text. `yank-pop' deletes that text and inserts in its
1542 place a different stretch of killed text.
1544 With no argument, the previous kill is inserted.
1545 With argument N, insert the Nth previous kill.
1546 If N is negative, this is a more recent kill.
1548 The sequence of kills wraps around, so that after the oldest one
1549 comes the newest one."
1551 (if (not (eq last-command 'yank))
1552 (error "Previous command was not a yank"))
1553 (setq this-command 'yank)
1554 (let ((inhibit-read-only t)
1555 (before (< (point) (mark t))))
1556 (delete-region (point) (mark t))
1557 (set-marker (mark-marker) (point) (current-buffer))
1558 (let ((opoint (point)))
1559 (insert (current-kill arg))
1560 (let ((inhibit-read-only t))
1561 (remove-text-properties opoint (point) '(read-only nil))))
1563 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1564 ;; It is cleaner to avoid activation, even though the command
1565 ;; loop would deactivate the mark because we inserted text.
1566 (goto-char (prog1 (mark t)
1567 (set-marker (mark-marker) (point) (current-buffer))))))
1570 (defun yank (&optional arg)
1571 "Reinsert the last stretch of killed text.
1572 More precisely, reinsert the stretch of killed text most recently
1573 killed OR yanked. Put point at end, and set mark at beginning.
1574 With just C-u as argument, same but put point at beginning (and mark at end).
1575 With argument N, reinsert the Nth most recently killed stretch of killed
1577 See also the command \\[yank-pop]."
1579 ;; If we don't get all the way thru, make last-command indicate that
1580 ;; for the following command.
1581 (setq this-command t)
1583 (let ((opoint (point)))
1584 (insert (current-kill (cond
1588 (let ((inhibit-read-only t))
1589 (remove-text-properties opoint (point) '(read-only nil))))
1591 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1592 ;; It is cleaner to avoid activation, even though the command
1593 ;; loop would deactivate the mark because we inserted text.
1594 (goto-char (prog1 (mark t)
1595 (set-marker (mark-marker) (point) (current-buffer)))))
1596 ;; If we do get all the way thru, make this-command indicate that.
1597 (setq this-command 'yank)
1600 (defun rotate-yank-pointer (arg)
1601 "Rotate the yanking point in the kill ring.
1602 With argument, rotate that many kills forward (or backward, if negative)."
1607 (defun insert-buffer (buffer)
1608 "Insert after point the contents of BUFFER.
1609 Puts mark after the inserted text.
1610 BUFFER may be a buffer or a buffer name."
1614 (barf-if-buffer-read-only)
1615 (read-buffer "Insert buffer: "
1616 (if (eq (selected-window) (next-window (selected-window)))
1617 (other-buffer (current-buffer))
1618 (window-buffer (next-window (selected-window))))
1620 (or (bufferp buffer)
1621 (setq buffer (get-buffer buffer)))
1622 (let (start end newmark)
1626 (setq start (point-min) end (point-max)))
1627 (insert-buffer-substring buffer start end)
1628 (setq newmark (point)))
1629 (push-mark newmark))
1632 (defun append-to-buffer (buffer start end)
1633 "Append to specified buffer the text of the region.
1634 It is inserted into that buffer before its point.
1636 When calling from a program, give three arguments:
1637 BUFFER (or buffer name), START and END.
1638 START and END specify the portion of the current buffer to be copied."
1640 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
1641 (region-beginning) (region-end)))
1642 (let ((oldbuf (current-buffer)))
1644 (set-buffer (get-buffer-create buffer))
1645 (insert-buffer-substring oldbuf start end))))
1647 (defun prepend-to-buffer (buffer start end)
1648 "Prepend to specified buffer the text of the region.
1649 It is inserted into that buffer after its point.
1651 When calling from a program, give three arguments:
1652 BUFFER (or buffer name), START and END.
1653 START and END specify the portion of the current buffer to be copied."
1654 (interactive "BPrepend to buffer: \nr")
1655 (let ((oldbuf (current-buffer)))
1657 (set-buffer (get-buffer-create buffer))
1659 (insert-buffer-substring oldbuf start end)))))
1661 (defun copy-to-buffer (buffer start end)
1662 "Copy to specified buffer the text of the region.
1663 It is inserted into that buffer, replacing existing text there.
1665 When calling from a program, give three arguments:
1666 BUFFER (or buffer name), START and END.
1667 START and END specify the portion of the current buffer to be copied."
1668 (interactive "BCopy to buffer: \nr")
1669 (let ((oldbuf (current-buffer)))
1671 (set-buffer (get-buffer-create buffer))
1674 (insert-buffer-substring oldbuf start end)))))
1676 (put 'mark-inactive 'error-conditions '(mark-inactive error))
1677 (put 'mark-inactive 'error-message "The mark is not active now")
1679 (defun mark (&optional force)
1680 "Return this buffer's mark value as integer; error if mark inactive.
1681 If optional argument FORCE is non-nil, access the mark value
1682 even if the mark is not currently active, and return nil
1683 if there is no mark at all.
1685 If you are using this in an editing command, you are most likely making
1686 a mistake; see the documentation of `set-mark'."
1687 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
1688 (marker-position (mark-marker))
1689 (signal 'mark-inactive nil)))
1691 ;; Many places set mark-active directly, and several of them failed to also
1692 ;; run deactivate-mark-hook. This shorthand should simplify.
1693 (defsubst deactivate-mark ()
1694 "Deactivate the mark by setting `mark-active' to nil.
1695 \(That makes a difference only in Transient Mark mode.)
1696 Also runs the hook `deactivate-mark-hook'."
1697 (if transient-mark-mode
1699 (setq mark-active nil)
1700 (run-hooks 'deactivate-mark-hook))))
1702 (defun set-mark (pos)
1703 "Set this buffer's mark to POS. Don't use this function!
1704 That is to say, don't use this function unless you want
1705 the user to see that the mark has moved, and you want the previous
1706 mark position to be lost.
1708 Normally, when a new mark is set, the old one should go on the stack.
1709 This is why most applications should use push-mark, not set-mark.
1711 Novice Emacs Lisp programmers often try to use the mark for the wrong
1712 purposes. The mark saves a location for the user's convenience.
1713 Most editing commands should not alter the mark.
1714 To remember a location for internal use in the Lisp program,
1715 store it in a Lisp variable. Example:
1717 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1721 (setq mark-active t)
1722 (run-hooks 'activate-mark-hook)
1723 (set-marker (mark-marker) pos (current-buffer)))
1724 ;; Normally we never clear mark-active except in Transient Mark mode.
1725 ;; But when we actually clear out the mark value too,
1726 ;; we must clear mark-active in any mode.
1727 (setq mark-active nil)
1728 (run-hooks 'deactivate-mark-hook)
1729 (set-marker (mark-marker) nil)))
1731 (defvar mark-ring nil
1732 "The list of former marks of the current buffer, most recent first.")
1733 (make-variable-buffer-local 'mark-ring)
1734 (put 'mark-ring 'permanent-local t)
1736 (defcustom mark-ring-max 16
1737 "*Maximum size of mark ring. Start discarding off end if gets this big."
1739 :group 'editing-basics)
1741 (defvar global-mark-ring nil
1742 "The list of saved global marks, most recent first.")
1744 (defcustom global-mark-ring-max 16
1745 "*Maximum size of global mark ring. \
1746 Start discarding off end if gets this big."
1748 :group 'editing-basics)
1750 (defun set-mark-command (arg)
1751 "Set mark at where point is, or jump to mark.
1752 With no prefix argument, set mark, push old mark position on local mark
1753 ring, and push mark on global mark ring.
1754 With argument, jump to mark, and pop a new position for mark off the ring
1755 \(does not affect global mark ring\).
1757 Novice Emacs Lisp programmers often try to use the mark for the wrong
1758 purposes. See the documentation of `set-mark' for more information."
1762 (push-mark nil nil t))
1764 (error "No mark set in this buffer")
1765 (goto-char (mark t))
1768 (defun push-mark (&optional location nomsg activate)
1769 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1770 If the last global mark pushed was not in the current buffer,
1771 also push LOCATION on the global mark ring.
1772 Display `Mark set' unless the optional second arg NOMSG is non-nil.
1773 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
1775 Novice Emacs Lisp programmers often try to use the mark for the wrong
1776 purposes. See the documentation of `set-mark' for more information.
1778 In Transient Mark mode, this does not activate the mark."
1781 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1782 (if (> (length mark-ring) mark-ring-max)
1784 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1785 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
1786 (set-marker (mark-marker) (or location (point)) (current-buffer))
1787 ;; Now push the mark on the global mark ring.
1788 (if (and global-mark-ring
1789 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
1790 ;; The last global mark pushed was in this same buffer.
1791 ;; Don't push another one.
1793 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
1794 (if (> (length global-mark-ring) global-mark-ring-max)
1796 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
1798 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
1799 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
1800 (message "Mark set"))
1801 (if (or activate (not transient-mark-mode))
1802 (set-mark (mark t)))
1806 "Pop off mark ring into the buffer's actual mark.
1807 Does not set point. Does nothing if mark ring is empty."
1810 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
1811 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
1813 (move-marker (car mark-ring) nil)
1814 (if (null (mark t)) (ding))
1815 (setq mark-ring (cdr mark-ring)))))
1817 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
1818 (defun exchange-point-and-mark ()
1819 "Put the mark where point is now, and point where the mark is now.
1820 This command works even when the mark is not active,
1821 and it reactivates the mark."
1823 (let ((omark (mark t)))
1825 (error "No mark set in this buffer"))
1830 (defun transient-mark-mode (arg)
1831 "Toggle Transient Mark mode.
1832 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
1834 In Transient Mark mode, when the mark is active, the region is highlighted.
1835 Changing the buffer \"deactivates\" the mark.
1836 So do certain other operations that set the mark
1837 but whose main purpose is something else--for example,
1838 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
1840 (setq transient-mark-mode
1842 (not transient-mark-mode)
1843 (> (prefix-numeric-value arg) 0)))
1845 (if transient-mark-mode
1846 (message "Transient Mark mode enabled")
1847 (message "Transient Mark mode disabled"))))
1849 (defun pop-global-mark ()
1850 "Pop off global mark ring and jump to the top location."
1852 ;; Pop entries which refer to non-existent buffers.
1853 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
1854 (setq global-mark-ring (cdr global-mark-ring)))
1855 (or global-mark-ring
1856 (error "No global mark set"))
1857 (let* ((marker (car global-mark-ring))
1858 (buffer (marker-buffer marker))
1859 (position (marker-position marker)))
1860 (setq global-mark-ring (nconc (cdr global-mark-ring)
1861 (list (car global-mark-ring))))
1863 (or (and (>= position (point-min))
1864 (<= position (point-max)))
1866 (goto-char position)
1867 (switch-to-buffer buffer)))
1869 (defcustom next-line-add-newlines t
1870 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
1872 :group 'editing-basics)
1874 (defun next-line (arg)
1875 "Move cursor vertically down ARG lines.
1876 If there is no character in the target line exactly under the current column,
1877 the cursor is positioned after the character in that line which spans this
1878 column, or at the end of the line if it is not long enough.
1879 If there is no line in the buffer after this one, behavior depends on the
1880 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
1881 to create a line, and moves the cursor to that line. Otherwise it moves the
1882 cursor to the end of the buffer.
1884 The command \\[set-goal-column] can be used to create
1885 a semipermanent goal column for this command.
1886 Then instead of trying to move exactly vertically (or as close as possible),
1887 this command moves to the specified goal column (or as close as possible).
1888 The goal column is stored in the variable `goal-column', which is nil
1889 when there is no goal column.
1891 If you are thinking of using this in a Lisp program, consider
1892 using `forward-line' instead. It is usually easier to use
1893 and more reliable (no dependence on goal column, etc.)."
1895 (if (and next-line-add-newlines (= arg 1))
1896 (let ((opoint (point)))
1905 ((beginning-of-buffer end-of-buffer) (ding)))
1909 (defun previous-line (arg)
1910 "Move cursor vertically up ARG lines.
1911 If there is no character in the target line exactly over the current column,
1912 the cursor is positioned after the character in that line which spans this
1913 column, or at the end of the line if it is not long enough.
1915 The command \\[set-goal-column] can be used to create
1916 a semipermanent goal column for this command.
1917 Then instead of trying to move exactly vertically (or as close as possible),
1918 this command moves to the specified goal column (or as close as possible).
1919 The goal column is stored in the variable `goal-column', which is nil
1920 when there is no goal column.
1922 If you are thinking of using this in a Lisp program, consider using
1923 `forward-line' with a negative argument instead. It is usually easier
1924 to use and more reliable (no dependence on goal column, etc.)."
1929 ((beginning-of-buffer end-of-buffer) (ding)))
1930 (line-move (- arg)))
1933 (defcustom track-eol nil
1934 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1935 This means moving to the end of each line moved onto.
1936 The beginning of a blank line does not count as the end of a line."
1938 :group 'editing-basics)
1940 (defcustom goal-column nil
1941 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
1942 :type '(choice integer
1943 (const :tag "None" nil))
1944 :group 'editing-basics)
1945 (make-variable-buffer-local 'goal-column)
1947 (defvar temporary-goal-column 0
1948 "Current goal column for vertical motion.
1949 It is the column where point was
1950 at the start of current run of vertical motion commands.
1951 When the `track-eol' feature is doing its job, the value is 9999.")
1953 (defcustom line-move-ignore-invisible nil
1954 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
1955 Outline mode sets this."
1957 :group 'editing-basics)
1959 ;; This is the guts of next-line and previous-line.
1960 ;; Arg says how many lines to move.
1961 (defun line-move (arg)
1962 ;; Don't run any point-motion hooks, and disregard intangibility,
1963 ;; for intermediate positions.
1964 (let ((inhibit-point-motion-hooks t)
1966 new line-end line-beg)
1969 (if (not (or (eq last-command 'next-line)
1970 (eq last-command 'previous-line)))
1971 (setq temporary-goal-column
1972 (if (and track-eol (eolp)
1973 ;; Don't count beg of empty line as end of line
1974 ;; unless we just did explicit end-of-line.
1975 (or (not (bolp)) (eq last-command 'end-of-line)))
1978 (if (and (not (integerp selective-display))
1979 (not line-move-ignore-invisible))
1980 ;; Use just newline characters.
1982 (progn (if (> arg 1) (forward-line (1- arg)))
1983 ;; This way of moving forward ARG lines
1984 ;; verifies that we have a newline after the last one.
1985 ;; It doesn't get confused by intangible text.
1987 (zerop (forward-line 1)))
1988 (and (zerop (forward-line arg))
1990 (signal (if (< arg 0)
1991 'beginning-of-buffer
1994 ;; Move by arg lines, but ignore invisible ones.
1997 (and (zerop (vertical-motion 1))
1998 (signal 'end-of-buffer nil))
1999 ;; If the following character is currently invisible,
2000 ;; skip all characters with that same `invisible' property value.
2001 (while (and (not (eobp))
2003 (get-char-property (point) 'invisible)))
2004 (if (eq buffer-invisibility-spec t)
2006 (or (memq prop buffer-invisibility-spec)
2007 (assq prop buffer-invisibility-spec)))))
2008 (if (get-text-property (point) 'invisible)
2009 (goto-char (next-single-property-change (point) 'invisible))
2010 (goto-char (next-overlay-change (point)))))
2011 (setq arg (1- arg)))
2014 (and (zerop (vertical-motion -1))
2015 (signal 'beginning-of-buffer nil))
2016 (while (and (not (bobp))
2018 (get-char-property (1- (point)) 'invisible)))
2019 (if (eq buffer-invisibility-spec t)
2021 (or (memq prop buffer-invisibility-spec)
2022 (assq prop buffer-invisibility-spec)))))
2023 (if (get-text-property (1- (point)) 'invisible)
2024 (goto-char (previous-single-property-change (point) 'invisible))
2025 (goto-char (previous-overlay-change (point)))))
2026 (setq arg (1+ arg))))
2027 (let ((buffer-invisibility-spec nil))
2028 (move-to-column (or goal-column temporary-goal-column))))
2030 ;; If we are moving into some intangible text,
2031 ;; look for following text on the same line which isn't intangible
2033 (setq line-end (save-excursion (end-of-line) (point)))
2034 (setq line-beg (save-excursion (beginning-of-line) (point)))
2035 (let ((after (and (< new (point-max))
2036 (get-char-property new 'intangible)))
2037 (before (and (> new (point-min))
2038 (get-char-property (1- new) 'intangible))))
2039 (when (and before (eq before after)
2041 (goto-char (point-min))
2042 (let ((inhibit-point-motion-hooks nil))
2044 (if (<= new line-end)
2045 (setq new (point)))))
2046 ;; NEW is where we want to move to.
2047 ;; LINE-BEG and LINE-END are the beginning and end of the line.
2048 ;; Move there in just one step, from our starting position,
2049 ;; with intangibility and point-motion hooks enabled this time.
2051 (setq inhibit-point-motion-hooks nil)
2053 ;; If intangibility processing moved us to a different line,
2054 ;; readjust the horizontal position within the line we ended up at.
2055 (when (or (< (point) line-beg) (> (point) line-end))
2057 (setq inhibit-point-motion-hooks t)
2058 (setq line-end (save-excursion (end-of-line) (point)))
2060 (setq line-beg (point))
2061 (let ((buffer-invisibility-spec nil))
2062 (move-to-column (or goal-column temporary-goal-column)))
2063 (if (<= (point) line-end)
2065 (goto-char (point-min))
2066 (setq inhibit-point-motion-hooks nil)
2071 ;;; Many people have said they rarely use this feature, and often type
2072 ;;; it by accident. Maybe it shouldn't even be on a key.
2073 (put 'set-goal-column 'disabled t)
2075 (defun set-goal-column (arg)
2076 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2077 Those commands will move to this position in the line moved to
2078 rather than trying to keep the same horizontal position.
2079 With a non-nil argument, clears out the goal column
2080 so that \\[next-line] and \\[previous-line] resume vertical motion.
2081 The goal column is stored in the variable `goal-column'."
2085 (setq goal-column nil)
2086 (message "No goal column"))
2087 (setq goal-column (current-column))
2088 (message (substitute-command-keys
2089 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2093 ;;; Partial support for horizontal autoscrolling. Someday, this feature
2094 ;;; will be built into the C level and all the (hscroll-point-visible) calls
2097 (defcustom hscroll-step 0
2098 "*The number of columns to try scrolling a window by when point moves out.
2099 If that fails to bring point back on frame, point is centered instead.
2100 If this is zero, point is always centered after it moves off frame."
2101 :type '(choice (const :tag "Alway Center" 0)
2102 (integer :format "%v" 1))
2103 :group 'editing-basics)
2105 (defun hscroll-point-visible ()
2106 "Scrolls the selected window horizontally to make point visible."
2108 (set-buffer (window-buffer))
2109 (if (not (or truncate-lines
2110 (> (window-hscroll) 0)
2111 (and truncate-partial-width-windows
2112 (< (window-width) (frame-width)))))
2113 ;; Point is always visible when lines are wrapped.
2115 ;; If point is on the invisible part of the line before window-start,
2116 ;; then hscrolling can't bring it back, so reset window-start first.
2117 (and (< (point) (window-start))
2118 (let ((ws-bol (save-excursion
2119 (goto-char (window-start))
2122 (and (>= (point) ws-bol)
2123 (set-window-start nil ws-bol))))
2124 (let* ((here (hscroll-window-column))
2125 (left (min (window-hscroll) 1))
2126 (right (1- (window-width))))
2127 ;; Allow for the truncation glyph, if we're not exactly at eol.
2128 (if (not (and (= here right)
2129 (= (following-char) ?\n)))
2130 (setq right (1- right)))
2132 ;; If too far away, just recenter. But don't show too much
2133 ;; white space off the end of the line.
2134 ((or (< here (- left hscroll-step))
2135 (> here (+ right hscroll-step)))
2136 (let ((eol (save-excursion (end-of-line) (hscroll-window-column))))
2137 (scroll-left (min (- here (/ (window-width) 2))
2138 (- eol (window-width) -5)))))
2139 ;; Within range. Scroll by one step (or maybe not at all).
2141 (scroll-right hscroll-step))
2143 (scroll-left hscroll-step)))))))
2145 ;; This function returns the window's idea of the display column of point,
2146 ;; assuming that the window is already known to be truncated rather than
2147 ;; wrapped, and that we've already handled the case where point is on the
2148 ;; part of the line before window-start. We ignore window-width; if point
2149 ;; is beyond the right margin, we want to know how far. The return value
2150 ;; includes the effects of window-hscroll, window-start, and the prompt
2151 ;; string in the minibuffer. It may be negative due to hscroll.
2152 (defun hscroll-window-column ()
2153 (let* ((hscroll (window-hscroll))
2154 (startpos (save-excursion
2156 (if (= (point) (save-excursion
2157 (goto-char (window-start))
2160 (goto-char (window-start)))
2162 (hpos (+ (if (and (eq (selected-window) (minibuffer-window))
2163 (= 1 (window-start))
2164 (= startpos (point-min)))
2165 (minibuffer-prompt-width)
2167 (min 0 (- 1 hscroll))))
2169 (car (cdr (compute-motion startpos (cons hpos 0)
2171 1000000 (cons hscroll 0) nil)))))
2174 ;; rms: (1) The definitions of arrow keys should not simply restate
2175 ;; what keys they are. The arrow keys should run the ordinary commands.
2176 ;; (2) The arrow keys are just one of many common ways of moving point
2177 ;; within a line. Real horizontal autoscrolling would be a good feature,
2178 ;; but supporting it only for arrow keys is too incomplete to be desirable.
2180 ;;;;; Make arrow keys do the right thing for improved terminal support
2181 ;;;;; When we implement true horizontal autoscrolling, right-arrow and
2182 ;;;;; left-arrow can lose the (if truncate-lines ...) clause and become
2183 ;;;;; aliases. These functions are bound to the corresponding keyboard
2184 ;;;;; events in loaddefs.el.
2186 ;;(defun right-arrow (arg)
2187 ;; "Move right one character on the screen (with prefix ARG, that many chars).
2188 ;;Scroll right if needed to keep point horizontally onscreen."
2189 ;; (interactive "P")
2190 ;; (forward-char arg)
2191 ;; (hscroll-point-visible))
2193 ;;(defun left-arrow (arg)
2194 ;; "Move left one character on the screen (with prefix ARG, that many chars).
2195 ;;Scroll left if needed to keep point horizontally onscreen."
2196 ;; (interactive "P")
2197 ;; (backward-char arg)
2198 ;; (hscroll-point-visible))
2200 (defun scroll-other-window-down (lines)
2201 "Scroll the \"other window\" down.
2202 For more details, see the documentation for `scroll-other-window'."
2204 (scroll-other-window
2205 ;; Just invert the argument's meaning.
2206 ;; We can do that without knowing which window it will be.
2207 (if (eq lines '-) nil
2209 (- (prefix-numeric-value lines))))))
2210 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2212 (defun beginning-of-buffer-other-window (arg)
2213 "Move point to the beginning of the buffer in the other window.
2214 Leave mark at previous position.
2215 With arg N, put point N/10 of the way from the true beginning."
2217 (let ((orig-window (selected-window))
2218 (window (other-window-for-scrolling)))
2219 ;; We use unwind-protect rather than save-window-excursion
2220 ;; because the latter would preserve the things we want to change.
2223 (select-window window)
2224 ;; Set point and mark in that window's buffer.
2225 (beginning-of-buffer arg)
2226 ;; Set point accordingly.
2228 (select-window orig-window))))
2230 (defun end-of-buffer-other-window (arg)
2231 "Move point to the end of the buffer in the other window.
2232 Leave mark at previous position.
2233 With arg N, put point N/10 of the way from the true end."
2235 ;; See beginning-of-buffer-other-window for comments.
2236 (let ((orig-window (selected-window))
2237 (window (other-window-for-scrolling)))
2240 (select-window window)
2243 (select-window orig-window))))
2245 (defun transpose-chars (arg)
2246 "Interchange characters around point, moving forward one character.
2247 With prefix arg ARG, effect is to take character before point
2248 and drag it forward past ARG other characters (backward if ARG negative).
2249 If no argument and at end of line, the previous two chars are exchanged."
2251 (and (null arg) (eolp) (forward-char -1))
2252 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2254 (defun transpose-words (arg)
2255 "Interchange words around point, leaving point at end of them.
2256 With prefix arg ARG, effect is to take word before or around point
2257 and drag it forward past ARG other words (backward if ARG negative).
2258 If ARG is zero, the words around or after point and around or after mark
2261 (transpose-subr 'forward-word arg))
2263 (defun transpose-sexps (arg)
2264 "Like \\[transpose-words] but applies to sexps.
2265 Does not work on a sexp that point is in the middle of
2266 if it is a list or string."
2268 (transpose-subr 'forward-sexp arg))
2270 (defun transpose-lines (arg)
2271 "Exchange current line and previous line, leaving point after both.
2272 With argument ARG, takes previous line and moves it past ARG lines.
2273 With argument 0, interchanges line point is in with line mark is in."
2275 (transpose-subr (function
2279 ;; Move forward over ARG lines,
2280 ;; but create newlines if necessary.
2281 (setq arg (forward-line arg))
2282 (if (/= (preceding-char) ?\n)
2283 (setq arg (1+ arg)))
2286 (forward-line arg))))
2289 (defun transpose-subr (mover arg)
2290 (let (start1 end1 start2 end2)
2297 (setq start2 (point))
2302 (setq start1 (point))
2304 (exchange-point-and-mark))
2308 (setq start1 (point))
2313 (funcall mover (- arg))
2314 (setq start2 (point))
2318 (setq start2 (point))
2321 (funcall mover (1- arg))
2322 (setq start1 (point))
2323 (funcall mover (- arg))
2325 (transpose-subr-1)))))
2327 (defun transpose-subr-1 ()
2328 (if (> (min end1 end2) (max start1 start2))
2329 (error "Don't have two things to transpose"))
2330 (let* ((word1 (buffer-substring start1 end1))
2331 (len1 (length word1))
2332 (word2 (buffer-substring start2 end2))
2333 (len2 (length word2)))
2334 (delete-region start2 end2)
2337 (goto-char (if (< start1 start2) start1
2338 (+ start1 (- len1 len2))))
2339 (delete-region (point) (+ (point) len1))
2342 (defcustom comment-column 32
2343 "*Column to indent right-margin comments to.
2344 Setting this variable automatically makes it local to the current buffer.
2345 Each mode establishes a different default value for this variable; you
2346 can set the value for a particular mode using that mode's hook."
2348 :group 'fill-comments)
2349 (make-variable-buffer-local 'comment-column)
2351 (defcustom comment-start nil
2352 "*String to insert to start a new comment, or nil if no comment syntax."
2353 :type '(choice (const :tag "None" nil)
2355 :group 'fill-comments)
2357 (defcustom comment-start-skip nil
2358 "*Regexp to match the start of a comment plus everything up to its body.
2359 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
2360 at the place matched by the close of the first pair."
2361 :type '(choice (const :tag "None" nil)
2363 :group 'fill-comments)
2365 (defcustom comment-end ""
2366 "*String to insert to end a new comment.
2367 Should be an empty string if comments are terminated by end-of-line."
2369 :group 'fill-comments)
2371 (defvar comment-indent-hook nil
2372 "Obsolete variable for function to compute desired indentation for a comment.
2373 This function is called with no args with point at the beginning of
2374 the comment's starting delimiter.")
2376 (defvar comment-indent-function
2377 '(lambda () comment-column)
2378 "Function to compute desired indentation for a comment.
2379 This function is called with no args with point at the beginning of
2380 the comment's starting delimiter.")
2382 (defcustom block-comment-start nil
2383 "*String to insert to start a new comment on a line by itself.
2384 If nil, use `comment-start' instead.
2385 Note that the regular expression `comment-start-skip' should skip this string
2386 as well as the `comment-start' string."
2387 :type '(choice (const :tag "Use comment-start" nil)
2389 :group 'fill-comments)
2391 (defcustom block-comment-end nil
2392 "*String to insert to end a new comment on a line by itself.
2393 Should be an empty string if comments are terminated by end-of-line.
2394 If nil, use `comment-end' instead."
2395 :type '(choice (const :tag "Use comment-end" nil)
2397 :group 'fill-comments)
2399 (defun indent-for-comment ()
2400 "Indent this line's comment to comment column, or insert an empty comment."
2402 (let* ((empty (save-excursion (beginning-of-line)
2403 (looking-at "[ \t]*$")))
2404 (starter (or (and empty block-comment-start) comment-start))
2405 (ender (or (and empty block-comment-end) comment-end)))
2408 (error "No comment syntax defined"))
2409 ((null comment-start-skip)
2410 (error "This mode doesn't define `comment-start-skip'"))
2411 (t (let* ((eolpos (save-excursion (end-of-line) (point)))
2414 (if (re-search-forward comment-start-skip eolpos 'move)
2415 (progn (setq cpos (point-marker))
2416 ;; Find the start of the comment delimiter.
2417 ;; If there were paren-pairs in comment-start-skip,
2418 ;; position at the end of the first pair.
2420 (goto-char (match-end 1))
2421 ;; If comment-start-skip matched a string with
2422 ;; internal whitespace (not final whitespace) then
2423 ;; the delimiter start at the end of that
2424 ;; whitespace. Otherwise, it starts at the
2425 ;; beginning of what was matched.
2426 (skip-syntax-backward " " (match-beginning 0))
2427 (skip-syntax-backward "^ " (match-beginning 0)))))
2428 (setq begpos (point))
2429 ;; Compute desired indent.
2430 (if (= (current-column)
2431 (setq indent (if comment-indent-hook
2432 (funcall comment-indent-hook)
2433 (funcall comment-indent-function))))
2435 ;; If that's different from current, change it.
2436 (skip-chars-backward " \t")
2437 (delete-region (point) begpos)
2439 ;; An existing comment?
2441 (progn (goto-char cpos)
2442 (set-marker cpos nil))
2446 (insert ender))))))))
2448 (defun set-comment-column (arg)
2449 "Set the comment column based on point.
2450 With no arg, set the comment column to the current column.
2451 With just minus as arg, kill any comment on this line.
2452 With any other arg, set comment column to indentation of the previous comment
2453 and then align or create a comment on this line at that column."
2461 (re-search-backward comment-start-skip)
2463 (re-search-forward comment-start-skip)
2464 (goto-char (match-beginning 0))
2465 (setq comment-column (current-column))
2466 (message "Comment column set to %d" comment-column))
2467 (indent-for-comment))
2468 (setq comment-column (current-column))
2469 (message "Comment column set to %d" comment-column))))
2471 (defun kill-comment (arg)
2472 "Kill the comment on this line, if any.
2473 With argument, kill comments on that many lines starting with this one."
2474 ;; this function loses in a lot of situations. it incorrectly recognises
2475 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
2476 ;; with multi-line comments, can kill extra whitespace if comment wasn't
2477 ;; through end-of-line, et cetera.
2479 (or comment-start-skip (error "No comment syntax defined"))
2480 (let ((count (prefix-numeric-value arg)) endc)
2486 (and (string< "" comment-end)
2489 (re-search-forward (regexp-quote comment-end) endc 'move)
2490 (skip-chars-forward " \t")
2493 (if (re-search-forward comment-start-skip endc t)
2495 (goto-char (match-beginning 0))
2496 (skip-chars-backward " \t")
2497 (kill-region (point) endc)
2498 ;; to catch comments a line beginnings
2499 (indent-according-to-mode))))
2500 (if arg (forward-line 1))
2501 (setq count (1- count)))))
2503 (defvar comment-padding 1
2504 "Number of spaces `comment-region' puts between comment chars and text.
2506 Extra spacing between the comment characters and the comment text
2507 makes the comment easier to read. Default is 1. Nil means 0 and is
2510 (defun comment-region (beg end &optional arg)
2511 "Comment or uncomment each line in the region.
2512 With just C-u prefix arg, uncomment each line in region.
2513 Numeric prefix arg ARG means use ARG comment characters.
2514 If ARG is negative, delete that many comment characters instead.
2515 Comments are terminated on each line, even for syntax in which newline does
2516 not end the comment. Blank lines do not get comments."
2517 ;; if someone wants it to only put a comment-start at the beginning and
2518 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
2519 ;; is easy enough. No option is made here for other than commenting
2521 (interactive "r\nP")
2522 (or comment-start (error "No comment syntax is defined"))
2523 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2526 (let ((cs comment-start) (ce comment-end)
2528 (if (consp arg) (setq numarg t)
2529 (setq numarg (prefix-numeric-value arg))
2530 ;; For positive arg > 1, replicate the comment delims now,
2531 ;; then insert the replicated strings just once.
2533 (setq cs (concat cs comment-start)
2534 ce (concat ce comment-end))
2535 (setq numarg (1- numarg))))
2536 (when comment-padding
2537 (setq cs (concat cs (make-string comment-padding ? ))))
2538 ;; Loop over all lines from BEG to END.
2539 (narrow-to-region beg end)
2542 (if (or (eq numarg t) (< numarg 0))
2544 ;; Delete comment start from beginning of line.
2546 (while (looking-at (regexp-quote cs))
2547 (delete-char (length cs)))
2548 (let ((count numarg))
2549 (while (and (> 1 (setq count (1+ count)))
2550 (looking-at (regexp-quote cs)))
2551 (delete-char (length cs)))))
2552 ;; Delete comment end from end of line.
2558 ;; This is questionable if comment-end ends in
2559 ;; whitespace. That is pretty brain-damaged,
2561 (while (progn (skip-chars-backward " \t")
2562 (and (>= (- (point) (point-min)) (length ce))
2564 (backward-char (length ce))
2565 (looking-at (regexp-quote ce)))))
2566 (delete-char (- (length ce)))))
2567 (let ((count numarg))
2568 (while (> 1 (setq count (1+ count)))
2570 ;; this is questionable if comment-end ends in whitespace
2571 ;; that is pretty brain-damaged though
2572 (skip-chars-backward " \t")
2574 (backward-char (length ce))
2575 (if (looking-at (regexp-quote ce))
2576 (delete-char (length ce))))))))
2578 ;; Insert at beginning and at end.
2579 (if (looking-at "[ \t]*$") ()
2581 (if (string= "" ce) ()
2584 (search-forward "\n" nil 'move)))))))
2586 (defun backward-word (arg)
2587 "Move backward until encountering the end of a word.
2588 With argument, do this that many times.
2589 In programs, it is faster to call `forward-word' with negative arg."
2591 (forward-word (- arg)))
2593 (defun mark-word (arg)
2594 "Set mark arg words away from point."
2602 (defun kill-word (arg)
2603 "Kill characters forward until encountering the end of a word.
2604 With argument, do this that many times."
2606 (kill-region (point) (progn (forward-word arg) (point))))
2608 (defun backward-kill-word (arg)
2609 "Kill characters backward until encountering the end of a word.
2610 With argument, do this that many times."
2612 (kill-word (- arg)))
2614 (defun current-word (&optional strict)
2615 "Return the word point is on (or a nearby word) as a string.
2616 If optional arg STRICT is non-nil, return nil unless point is within
2617 or adjacent to a word."
2619 (let ((oldpoint (point)) (start (point)) (end (point)))
2620 (skip-syntax-backward "w_") (setq start (point))
2621 (goto-char oldpoint)
2622 (skip-syntax-forward "w_") (setq end (point))
2623 (if (and (eq start oldpoint) (eq end oldpoint))
2624 ;; Point is neither within nor adjacent to a word.
2627 ;; Look for preceding word in same line.
2628 (skip-syntax-backward "^w_"
2629 (save-excursion (beginning-of-line)
2632 ;; No preceding word in same line.
2633 ;; Look for following word in same line.
2635 (skip-syntax-forward "^w_"
2636 (save-excursion (end-of-line)
2638 (setq start (point))
2639 (skip-syntax-forward "w_")
2642 (skip-syntax-backward "w_")
2643 (setq start (point)))
2644 (buffer-substring-no-properties start end)))
2645 (buffer-substring-no-properties start end)))))
2647 (defcustom fill-prefix nil
2648 "*String for filling to insert at front of new line, or nil for none.
2649 Setting this variable automatically makes it local to the current buffer."
2650 :type '(choice (const :tag "None" nil)
2653 (make-variable-buffer-local 'fill-prefix)
2655 (defcustom auto-fill-inhibit-regexp nil
2656 "*Regexp to match lines which should not be auto-filled."
2657 :type '(choice (const :tag "None" nil)
2661 (defvar comment-line-break-function 'indent-new-comment-line
2662 "*Mode-specific function which line breaks and continues a comment.
2664 This function is only called during auto-filling of a comment section.
2665 The function should take a single optional argument, which is a flag
2666 indicating whether it should use soft newlines.
2668 Setting this variable automatically makes it local to the current buffer.")
2670 ;; This function is the auto-fill-function of a buffer
2671 ;; when Auto-Fill mode is enabled.
2672 ;; It returns t if it really did any work.
2673 (defun do-auto-fill ()
2674 (let (fc justify bol give-up
2675 (fill-prefix fill-prefix))
2676 (if (or (not (setq justify (current-justification)))
2677 (null (setq fc (current-fill-column)))
2678 (and (eq justify 'left)
2679 (<= (current-column) fc))
2680 (save-excursion (beginning-of-line)
2682 (and auto-fill-inhibit-regexp
2683 (looking-at auto-fill-inhibit-regexp))))
2684 nil ;; Auto-filling not required
2685 (if (memq justify '(full center right))
2686 (save-excursion (unjustify-current-line)))
2688 ;; Choose a fill-prefix automatically.
2689 (if (and adaptive-fill-mode
2690 (or (null fill-prefix) (string= fill-prefix "")))
2692 (fill-context-prefix
2693 (save-excursion (backward-paragraph 1) (point))
2694 (save-excursion (forward-paragraph 1) (point)))))
2695 (and prefix (not (equal prefix ""))
2696 (setq fill-prefix prefix))))
2698 (while (and (not give-up) (> (current-column) fc))
2699 ;; Determine where to split the line.
2702 (let ((opoint (point))
2707 (setq after-prefix (point))
2709 (looking-at (regexp-quote fill-prefix))
2710 (setq after-prefix (match-end 0)))
2711 (move-to-column (1+ fc))
2712 ;; Move back to the point where we can break the
2713 ;; line at. We break the line between word or
2714 ;; after/before the character which has character
2715 ;; category `|'. We search space, \c| followed by
2716 ;; a character, or \c| follwoing a character. If
2717 ;; not found, place the point at beginning of line.
2719 ;; If this is after period and a single space,
2720 ;; move back once more--we don't want to break
2721 ;; the line there and make it look like a
2725 sentence-end-double-space
2726 (save-excursion (forward-char -1)
2727 (and (looking-at "\\. ")
2728 (not (looking-at "\\. "))))))
2730 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
2731 ;; If we find nowhere on the line to break it,
2732 ;; break after one word. Set bounce to t
2733 ;; so we will not keep going in this while loop.
2734 (if (<= (point) after-prefix)
2736 (goto-char after-prefix)
2737 (re-search-forward "[ \t]" opoint t)
2739 (if (looking-at "[ \t]")
2740 ;; Break the line at word boundary.
2741 (skip-chars-backward " \t")
2742 ;; Break the line after/before \c|.
2744 (if (and enable-kinsoku enable-multibyte-characters)
2745 (kinsoku (save-excursion
2746 (forward-line 0) (point))))
2747 ;; Let fill-point be set to the place where we end up.
2750 ;; See whether the place we found is any good.
2752 (goto-char fill-point)
2754 ;; There is no use breaking at end of line.
2755 (not (save-excursion (skip-chars-forward " ") (eolp)))
2756 ;; It is futile to split at the end of the prefix
2757 ;; since we would just insert the prefix again.
2758 (not (and after-prefix (<= (point) after-prefix)))
2759 ;; Don't split right after a comment starter
2760 ;; since we would just make another comment starter.
2761 (not (and comment-start-skip
2762 (let ((limit (point)))
2764 (and (re-search-forward comment-start-skip
2766 (eq (point) limit)))))))
2767 ;; Ok, we have a useful place to break the line. Do it.
2768 (let ((prev-column (current-column)))
2769 ;; If point is at the fill-point, do not `save-excursion'.
2770 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2771 ;; point will end up before it rather than after it.
2773 (skip-chars-backward " \t")
2774 (= (point) fill-point))
2775 (funcall comment-line-break-function t)
2777 (goto-char fill-point)
2778 (funcall comment-line-break-function t)))
2779 ;; Now do justification, if required
2780 (if (not (eq justify 'left))
2783 (justify-current-line justify nil t)))
2784 ;; If making the new line didn't reduce the hpos of
2785 ;; the end of the line, then give up now;
2786 ;; trying again will not help.
2787 (if (>= (current-column) prev-column)
2789 ;; No good place to break => stop trying.
2791 ;; Justify last line.
2792 (justify-current-line justify t t)
2795 (defvar normal-auto-fill-function 'do-auto-fill
2796 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
2797 Some major modes set this.")
2799 (defun auto-fill-mode (&optional arg)
2800 "Toggle Auto Fill mode.
2801 With arg, turn Auto Fill mode on if and only if arg is positive.
2802 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
2803 automatically breaks the line at a previous space.
2805 The value of `normal-auto-fill-function' specifies the function to use
2806 for `auto-fill-function' when turning Auto Fill mode on."
2808 (prog1 (setq auto-fill-function
2810 (not auto-fill-function)
2811 (> (prefix-numeric-value arg) 0))
2812 normal-auto-fill-function
2814 (force-mode-line-update)))
2816 ;; This holds a document string used to document auto-fill-mode.
2817 (defun auto-fill-function ()
2818 "Automatically break line at a previous space, in insertion of text."
2821 (defun turn-on-auto-fill ()
2822 "Unconditionally turn on Auto Fill mode."
2825 (defun set-fill-column (arg)
2826 "Set `fill-column' to specified argument.
2827 Just \\[universal-argument] as argument means to use the current column."
2830 (setq arg (current-column)))
2831 (if (not (integerp arg))
2832 ;; Disallow missing argument; it's probably a typo for C-x C-f.
2833 (error "set-fill-column requires an explicit argument")
2834 (message "Fill column set to %d (was %d)" arg fill-column)
2835 (setq fill-column arg)))
2837 (defcustom comment-multi-line nil
2838 "*Non-nil means \\[indent-new-comment-line] should continue same comment
2839 on new line, with no new terminator or starter.
2840 This is obsolete because you might as well use \\[newline-and-indent]."
2842 :group 'fill-comments)
2844 (defun indent-new-comment-line (&optional soft)
2845 "Break line at point and indent, continuing comment if within one.
2846 This indents the body of the continued comment
2847 under the previous comment line.
2849 This command is intended for styles where you write a comment per line,
2850 starting a new comment (and terminating it if necessary) on each line.
2851 If you want to continue one comment across several lines, use \\[newline-and-indent].
2853 If a fill column is specified, it overrides the use of the comment column
2854 or comment indentation.
2856 The inserted newline is marked hard if `use-hard-newlines' is true,
2857 unless optional argument SOFT is non-nil."
2859 (let (comcol comstart)
2860 (skip-chars-backward " \t")
2861 (delete-region (point)
2862 (progn (skip-chars-forward " \t")
2864 (if soft (insert-and-inherit ?\n) (newline 1))
2867 (indent-to-left-margin)
2868 (insert-and-inherit fill-prefix))
2869 (if (not comment-multi-line)
2871 (if (and comment-start-skip
2872 (let ((opoint (point)))
2874 (re-search-forward comment-start-skip opoint t)))
2875 ;; The old line is a comment.
2876 ;; Set WIN to the pos of the comment-start.
2877 ;; But if the comment is empty, look at preceding lines
2878 ;; to find one that has a nonempty comment.
2880 ;; If comment-start-skip contains a \(...\) pair,
2881 ;; the real comment delimiter starts at the end of that pair.
2882 (let ((win (or (match-end 1) (match-beginning 0))))
2883 (while (and (eolp) (not (bobp))
2886 (setq opoint (point))
2888 (re-search-forward comment-start-skip opoint t)))
2889 (setq win (or (match-end 1) (match-beginning 0))))
2890 ;; Indent this line like what we found.
2892 (setq comcol (current-column))
2894 (buffer-substring (point) (match-end 0)))))))
2896 (let ((comment-column comcol)
2897 (comment-start comstart)
2898 (comment-end comment-end))
2899 (and comment-end (not (equal comment-end ""))
2900 ; (if (not comment-multi-line)
2903 (insert comment-end)
2905 ; (setq comment-column (+ comment-column (length comment-start))
2910 (setq comment-end ""))
2911 (insert-and-inherit ?\n)
2913 (indent-for-comment)
2915 ;; Make sure we delete the newline inserted above.
2918 (indent-according-to-mode)))))
2920 (defun set-selective-display (arg)
2921 "Set `selective-display' to ARG; clear it if no arg.
2922 When the value of `selective-display' is a number > 0,
2923 lines whose indentation is >= that value are not displayed.
2924 The variable `selective-display' has a separate value for each buffer."
2926 (if (eq selective-display t)
2927 (error "selective-display already in use for marked lines"))
2930 (narrow-to-region (point-min) (point))
2931 (goto-char (window-start))
2932 (vertical-motion (window-height)))))
2933 (setq selective-display
2934 (and arg (prefix-numeric-value arg)))
2935 (recenter current-vpos))
2936 (set-window-start (selected-window) (window-start (selected-window)))
2937 (princ "selective-display set to " t)
2938 (prin1 selective-display t)
2941 (defvar overwrite-mode-textual " Ovwrt"
2942 "The string displayed in the mode line when in overwrite mode.")
2943 (defvar overwrite-mode-binary " Bin Ovwrt"
2944 "The string displayed in the mode line when in binary overwrite mode.")
2946 (defun overwrite-mode (arg)
2947 "Toggle overwrite mode.
2948 With arg, turn overwrite mode on iff arg is positive.
2949 In overwrite mode, printing characters typed in replace existing text
2950 on a one-for-one basis, rather than pushing it to the right. At the
2951 end of a line, such characters extend the line. Before a tab,
2952 such characters insert until the tab is filled in.
2953 \\[quoted-insert] still inserts characters in overwrite mode; this
2954 is supposed to make it easier to insert characters when necessary."
2956 (setq overwrite-mode
2957 (if (if (null arg) (not overwrite-mode)
2958 (> (prefix-numeric-value arg) 0))
2959 'overwrite-mode-textual))
2960 (force-mode-line-update))
2962 (defun binary-overwrite-mode (arg)
2963 "Toggle binary overwrite mode.
2964 With arg, turn binary overwrite mode on iff arg is positive.
2965 In binary overwrite mode, printing characters typed in replace
2966 existing text. Newlines are not treated specially, so typing at the
2967 end of a line joins the line to the next, with the typed character
2968 between them. Typing before a tab character simply replaces the tab
2969 with the character typed.
2970 \\[quoted-insert] replaces the text at the cursor, just as ordinary
2971 typing characters do.
2973 Note that binary overwrite mode is not its own minor mode; it is a
2974 specialization of overwrite-mode, entered by setting the
2975 `overwrite-mode' variable to `overwrite-mode-binary'."
2977 (setq overwrite-mode
2979 (not (eq overwrite-mode 'overwrite-mode-binary))
2980 (> (prefix-numeric-value arg) 0))
2981 'overwrite-mode-binary))
2982 (force-mode-line-update))
2984 (defcustom line-number-mode t
2985 "*Non-nil means display line number in mode line."
2987 :group 'editing-basics)
2989 (defun line-number-mode (arg)
2990 "Toggle Line Number mode.
2991 With arg, turn Line Number mode on iff arg is positive.
2992 When Line Number mode is enabled, the line number appears
2995 (setq line-number-mode
2996 (if (null arg) (not line-number-mode)
2997 (> (prefix-numeric-value arg) 0)))
2998 (force-mode-line-update))
3000 (defcustom column-number-mode nil
3001 "*Non-nil means display column number in mode line."
3003 :group 'editing-basics)
3005 (defun column-number-mode (arg)
3006 "Toggle Column Number mode.
3007 With arg, turn Column Number mode on iff arg is positive.
3008 When Column Number mode is enabled, the column number appears
3011 (setq column-number-mode
3012 (if (null arg) (not column-number-mode)
3013 (> (prefix-numeric-value arg) 0)))
3014 (force-mode-line-update))
3016 (defgroup paren-blinking nil
3017 "Blinking matching of parens and expressions."
3018 :prefix "blink-matching-"
3019 :group 'paren-matching)
3021 (defcustom blink-matching-paren t
3022 "*Non-nil means show matching open-paren when close-paren is inserted."
3024 :group 'paren-blinking)
3026 (defcustom blink-matching-paren-on-screen t
3027 "*Non-nil means show matching open-paren when it is on screen.
3028 If nil, means don't show it (but the open-paren can still be shown
3029 when it is off screen)."
3031 :group 'paren-blinking)
3033 (defcustom blink-matching-paren-distance (* 25 1024)
3034 "*If non-nil, is maximum distance to search for matching open-paren."
3036 :group 'paren-blinking)
3038 (defcustom blink-matching-delay 1
3039 "*Time in seconds to delay after showing a matching paren."
3041 :group 'paren-blinking)
3043 (defcustom blink-matching-paren-dont-ignore-comments nil
3044 "*Non-nil means `blink-matching-paren' will not ignore comments."
3046 :group 'paren-blinking)
3048 (defun blink-matching-open ()
3049 "Move cursor momentarily to the beginning of the sexp before point."
3051 (and (> (point) (1+ (point-min)))
3052 blink-matching-paren
3053 ;; Verify an even number of quoting characters precede the close.
3054 (= 1 (logand 1 (- (point)
3057 (skip-syntax-backward "/\\")
3059 (let* ((oldpos (point))
3064 (if blink-matching-paren-distance
3065 (narrow-to-region (max (point-min)
3066 (- (point) blink-matching-paren-distance))
3069 (let ((parse-sexp-ignore-comments
3070 (and parse-sexp-ignore-comments
3071 (not blink-matching-paren-dont-ignore-comments))))
3072 (setq blinkpos (scan-sexps oldpos -1)))
3075 (/= (char-syntax (char-after blinkpos))
3078 (or (null (matching-paren (char-after blinkpos)))
3079 (/= (char-after (1- oldpos))
3080 (matching-paren (char-after blinkpos))))))
3081 (if mismatch (setq blinkpos nil))
3084 (goto-char blinkpos)
3085 (if (pos-visible-in-window-p)
3086 (and blink-matching-paren-on-screen
3087 (sit-for blink-matching-delay))
3088 (goto-char blinkpos)
3091 ;; Show what precedes the open in its line, if anything.
3093 (skip-chars-backward " \t")
3095 (buffer-substring (progn (beginning-of-line) (point))
3097 ;; Show what follows the open in its line, if anything.
3100 (skip-chars-forward " \t")
3102 (buffer-substring blinkpos
3103 (progn (end-of-line) (point)))
3104 ;; Otherwise show the previous nonblank line,
3107 (skip-chars-backward "\n \t")
3110 (buffer-substring (progn
3111 (skip-chars-backward "\n \t")
3114 (progn (end-of-line)
3115 (skip-chars-backward " \t")
3117 ;; Replace the newline and other whitespace with `...'.
3119 (buffer-substring blinkpos (1+ blinkpos)))
3120 ;; There is nothing to show except the char itself.
3121 (buffer-substring blinkpos (1+ blinkpos))))))))
3123 (message "Mismatched parentheses"))
3124 ((not blink-matching-paren-distance)
3125 (message "Unmatched parenthesis"))))))))
3127 ;Turned off because it makes dbx bomb out.
3128 (setq blink-paren-function 'blink-matching-open)
3130 ;; This executes C-g typed while Emacs is waiting for a command.
3131 ;; Quitting out of a program does not go through here;
3132 ;; that happens in the QUIT macro at the C code level.
3133 (defun keyboard-quit ()
3134 "Signal a quit condition.
3135 During execution of Lisp code, this character causes a quit directly.
3136 At top-level, as an editor command, this simply beeps."
3141 (define-key global-map "\C-g" 'keyboard-quit)
3143 (defvar buffer-quit-function nil
3144 "Function to call to \"quit\" the current buffer, or nil if none.
3145 \\[keyboard-escape-quit] calls this function when its more local actions
3146 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3148 (defun keyboard-escape-quit ()
3149 "Exit the current \"mode\" (in a generalized sense of the word).
3150 This command can exit an interactive command such as `query-replace',
3151 can clear out a prefix argument or a region,
3152 can get out of the minibuffer or other recursive edit,
3153 cancel the use of the current buffer (for special-purpose buffers),
3154 or go back to just one window (by deleting all but the selected window)."
3156 (cond ((eq last-command 'mode-exited) nil)
3157 ((> (minibuffer-depth) 0)
3158 (abort-recursive-edit))
3161 ((and transient-mark-mode
3164 ((> (recursion-depth) 0)
3165 (exit-recursive-edit))
3166 (buffer-quit-function
3167 (funcall buffer-quit-function))
3168 ((not (one-window-p t))
3169 (delete-other-windows))
3170 ((string-match "^ \\*" (buffer-name (current-buffer)))
3173 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3175 (defcustom mail-user-agent 'sendmail-user-agent
3176 "*Your preference for a mail composition package.
3177 Various Emacs Lisp packages (e.g. reporter) require you to compose an
3178 outgoing email message. This variable lets you specify which
3179 mail-sending package you prefer.
3181 Valid values include:
3183 sendmail-user-agent -- use the default Emacs Mail package
3184 mh-e-user-agent -- use the Emacs interface to the MH mail system
3185 message-user-agent -- use the GNUS mail sending package
3187 Additional valid symbols may be available; check with the author of
3188 your package for details."
3189 :type '(radio (function-item :tag "Default Emacs mail"
3191 sendmail-user-agent)
3192 (function-item :tag "Emacs interface to MH"
3195 (function-item :tag "Gnus mail sending package"
3198 (function :tag "Other"))
3201 (defun define-mail-user-agent (symbol composefunc sendfunc
3202 &optional abortfunc hookvar)
3203 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3205 SYMBOL can be any Lisp symbol. Its function definition and/or
3206 value as a variable do not matter for this usage; we use only certain
3207 properties on its property list, to encode the rest of the arguments.
3209 COMPOSEFUNC is program callable function that composes an outgoing
3210 mail message buffer. This function should set up the basics of the
3211 buffer without requiring user interaction. It should populate the
3212 standard mail headers, leaving the `to:' and `subject:' headers blank
3215 COMPOSEFUNC should accept several optional arguments--the same
3216 arguments that `compose-mail' takes. See that function's documentation.
3218 SENDFUNC is the command a user would run to send the message.
3220 Optional ABORTFUNC is the command a user would run to abort the
3221 message. For mail packages that don't have a separate abort function,
3222 this can be `kill-buffer' (the equivalent of omitting this argument).
3224 Optional HOOKVAR is a hook variable that gets run before the message
3225 is actually sent. Callers that use the `mail-user-agent' may
3226 install a hook function temporarily on this hook variable.
3227 If HOOKVAR is nil, `mail-send-hook' is used.
3229 The properties used on SYMBOL are `composefunc', `sendfunc',
3230 `abortfunc', and `hookvar'."
3231 (put symbol 'composefunc composefunc)
3232 (put symbol 'sendfunc sendfunc)
3233 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3234 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3236 (defun assoc-ignore-case (key alist)
3237 "Like `assoc', but assumes KEY is a string and ignores case when comparing."
3238 (setq key (downcase key))
3240 (while (and alist (not element))
3241 (if (equal key (downcase (car (car alist))))
3242 (setq element (car alist)))
3243 (setq alist (cdr alist)))
3246 (define-mail-user-agent 'sendmail-user-agent
3247 'sendmail-user-agent-compose
3248 'mail-send-and-exit)
3250 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3251 switch-function yank-action
3254 (let ((special-display-buffer-names nil)
3255 (special-display-regexps nil)
3256 (same-window-buffer-names nil)
3257 (same-window-regexps nil))
3258 (funcall switch-function "*mail*")))
3259 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3260 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))))
3261 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3263 (error "Message aborted"))
3265 (goto-char (point-min))
3266 (search-forward mail-header-separator)
3268 (while other-headers
3269 (if (not (member (car (car other-headers)) '("in-reply-to" "cc")))
3270 (insert (car (car other-headers)) ": "
3271 (cdr (car other-headers)) "\n"))
3272 (setq other-headers (cdr other-headers)))
3275 (define-mail-user-agent 'mh-e-user-agent
3276 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3277 'mh-before-send-letter-hook)
3279 (defun compose-mail (&optional to subject other-headers continue
3280 switch-function yank-action send-actions)
3281 "Start composing a mail message to send.
3282 This uses the user's chosen mail composition package
3283 as selected with the variable `mail-user-agent'.
3284 The optional arguments TO and SUBJECT specify recipients
3285 and the initial Subject field, respectively.
3287 OTHER-HEADERS is an alist specifying additional
3288 header fields. Elements look like (HEADER . VALUE) where both
3289 HEADER and VALUE are strings.
3291 CONTINUE, if non-nil, says to continue editing a message already
3294 SWITCH-FUNCTION, if non-nil, is a function to use to
3295 switch to and display the buffer used for mail composition.
3297 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3298 to insert the raw text of the message being replied to.
3299 It has the form (FUNCTION . ARGS). The user agent will apply
3300 FUNCTION to ARGS, to insert the raw text of the original message.
3301 \(The user agent will also run `mail-citation-hook', *after* the
3302 original text has been inserted in this way.)
3304 SEND-ACTIONS is a list of actions to call when the message is sent.
3305 Each action has the form (FUNCTION . ARGS)."
3307 (list nil nil nil current-prefix-arg))
3308 (let ((function (get mail-user-agent 'composefunc)))
3309 (funcall function to subject other-headers continue
3310 switch-function yank-action send-actions)))
3312 (defun compose-mail-other-window (&optional to subject other-headers continue
3313 yank-action send-actions)
3314 "Like \\[compose-mail], but edit the outgoing message in another window."
3316 (list nil nil nil current-prefix-arg))
3317 (compose-mail to subject other-headers continue
3318 'switch-to-buffer-other-window yank-action send-actions))
3321 (defun compose-mail-other-frame (&optional to subject other-headers continue
3322 yank-action send-actions)
3323 "Like \\[compose-mail], but edit the outgoing message in another frame."
3325 (list nil nil nil current-prefix-arg))
3326 (compose-mail to subject other-headers continue
3327 'switch-to-buffer-other-frame yank-action send-actions))
3329 (defvar set-variable-value-history nil
3330 "History of values entered with `set-variable'.")
3332 (defun set-variable (var val)
3333 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3334 When using this interactively, enter a Lisp object for VALUE.
3335 If you want VALUE to be a string, you must surround it with doublequotes.
3336 VALUE is used literally, not evaluated.
3338 If VARIABLE has a `variable-interactive' property, that is used as if
3339 it were the arg to `interactive' (which see) to interactively read VALUE.
3341 If VARIABLE has been defined with `defcustom', then the type information
3342 in the definition is used to check that VALUE is valid."
3343 (interactive (let* ((var (read-variable "Set variable: "))
3344 (minibuffer-help-form '(describe-variable var))
3345 (prop (get var 'variable-interactive))
3346 (prompt (format "Set %s to value: " var))
3348 ;; Use VAR's `variable-interactive' property
3349 ;; as an interactive spec for prompting.
3350 (call-interactively `(lambda (arg)
3354 (read-string prompt nil
3355 'set-variable-value-history)))))
3358 (let ((type (get var 'custom-type)))
3360 ;; Match with custom type.
3362 (setq type (widget-convert type))
3363 (unless (widget-apply type :match val)
3364 (error "Value `%S' does not match type %S of %S"
3365 val (car type) var))))
3368 ;; Define the major mode for lists of completions.
3370 (defvar completion-list-mode-map nil
3371 "Local map for completion list buffers.")
3372 (or completion-list-mode-map
3373 (let ((map (make-sparse-keymap)))
3374 (define-key map [mouse-2] 'mouse-choose-completion)
3375 (define-key map [down-mouse-2] nil)
3376 (define-key map "\C-m" 'choose-completion)
3377 (define-key map "\e\e\e" 'delete-completion-window)
3378 (define-key map [left] 'previous-completion)
3379 (define-key map [right] 'next-completion)
3380 (setq completion-list-mode-map map)))
3382 ;; Completion mode is suitable only for specially formatted data.
3383 (put 'completion-list-mode 'mode-class 'special)
3385 (defvar completion-reference-buffer nil
3386 "Record the buffer that was current when the completion list was requested.
3387 This is a local variable in the completion list buffer.
3388 Initial value is nil to avoid some compiler warnings.")
3390 (defvar completion-no-auto-exit nil
3391 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3392 This also applies to other functions such as `choose-completion'
3393 and `mouse-choose-completion'.")
3395 (defvar completion-base-size nil
3396 "Number of chars at beginning of minibuffer not involved in completion.
3397 This is a local variable in the completion list buffer
3398 but it talks about the buffer in `completion-reference-buffer'.
3399 If this is nil, it means to compare text to determine which part
3400 of the tail end of the buffer's text is involved in completion.")
3402 (defun delete-completion-window ()
3403 "Delete the completion list window.
3404 Go to the window from which completion was requested."
3406 (let ((buf completion-reference-buffer))
3407 (if (one-window-p t)
3408 (if (window-dedicated-p (selected-window))
3409 (delete-frame (selected-frame)))
3410 (delete-window (selected-window))
3411 (if (get-buffer-window buf)
3412 (select-window (get-buffer-window buf))))))
3414 (defun previous-completion (n)
3415 "Move to the previous item in the completion list."
3417 (next-completion (- n)))
3419 (defun next-completion (n)
3420 "Move to the next item in the completion list.
3421 With prefix argument N, move N items (negative N means move backward)."
3423 (while (and (> n 0) (not (eobp)))
3424 (let ((prop (get-text-property (point) 'mouse-face))
3426 ;; If in a completion, move to the end of it.
3428 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3429 ;; Move to start of next one.
3430 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3432 (while (and (< n 0) (not (bobp)))
3433 (let ((prop (get-text-property (1- (point)) 'mouse-face))
3435 ;; If in a completion, move to the start of it.
3437 (goto-char (previous-single-property-change
3438 (point) 'mouse-face nil end)))
3439 ;; Move to end of the previous completion.
3440 (goto-char (previous-single-property-change (point) 'mouse-face nil end))
3441 ;; Move to the start of that one.
3442 (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
3445 (defun choose-completion ()
3446 "Choose the completion that point is in or next to."
3448 (let (beg end completion (buffer completion-reference-buffer)
3449 (base-size completion-base-size))
3450 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3451 (setq end (point) beg (1+ (point))))
3452 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3453 (setq end (1- (point)) beg (point)))
3455 (error "No completion here"))
3456 (setq beg (previous-single-property-change beg 'mouse-face))
3457 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3458 (setq completion (buffer-substring beg end))
3459 (let ((owindow (selected-window)))
3460 (if (and (one-window-p t 'selected-frame)
3461 (window-dedicated-p (selected-window)))
3462 ;; This is a special buffer's frame
3463 (iconify-frame (selected-frame))
3464 (or (window-dedicated-p (selected-window))
3466 (select-window owindow))
3467 (choose-completion-string completion buffer base-size)))
3469 ;; Delete the longest partial match for STRING
3470 ;; that can be found before POINT.
3471 (defun choose-completion-delete-max-match (string)
3472 (let ((opoint (point))
3473 (len (min (length string)
3474 (- (point) (point-min)))))
3475 (goto-char (- (point) (length string)))
3476 (if completion-ignore-case
3477 (setq string (downcase string)))
3478 (while (and (> len 0)
3479 (let ((tail (buffer-substring (point)
3481 (if completion-ignore-case
3482 (setq tail (downcase tail)))
3483 (not (string= tail (substring string 0 len)))))
3488 ;; Switch to BUFFER and insert the completion choice CHOICE.
3489 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3490 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3492 ;; If BUFFER is the minibuffer, exit the minibuffer
3493 ;; unless it is reading a file name and CHOICE is a directory,
3494 ;; or completion-no-auto-exit is non-nil.
3495 (defun choose-completion-string (choice &optional buffer base-size)
3496 (let ((buffer (or buffer completion-reference-buffer)))
3497 ;; If BUFFER is a minibuffer, barf unless it's the currently
3498 ;; active minibuffer.
3499 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
3500 (or (not (active-minibuffer-window))
3502 (window-buffer (active-minibuffer-window))))))
3503 (error "Minibuffer is not active for completion")
3504 ;; Insert the completion into the buffer where completion was requested.
3507 (delete-region (+ base-size (point-min)) (point))
3508 (choose-completion-delete-max-match choice))
3510 (remove-text-properties (- (point) (length choice)) (point)
3512 ;; Update point in the window that BUFFER is showing in.
3513 (let ((window (get-buffer-window buffer t)))
3514 (set-window-point window (point)))
3515 ;; If completing for the minibuffer, exit it with this choice.
3516 (and (not completion-no-auto-exit)
3517 (equal buffer (window-buffer (minibuffer-window)))
3518 minibuffer-completion-table
3519 ;; If this is reading a file name, and the file name chosen
3520 ;; is a directory, don't exit the minibuffer.
3521 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3522 (file-directory-p (buffer-string)))
3523 (select-window (active-minibuffer-window))
3524 (exit-minibuffer))))))
3526 (defun completion-list-mode ()
3527 "Major mode for buffers showing lists of possible completions.
3528 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3529 to select the completion near point.
3530 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3533 (kill-all-local-variables)
3534 (use-local-map completion-list-mode-map)
3535 (setq mode-name "Completion List")
3536 (setq major-mode 'completion-list-mode)
3537 (make-local-variable 'completion-base-size)
3538 (setq completion-base-size nil)
3539 (run-hooks 'completion-list-mode-hook))
3541 (defvar completion-fixup-function nil
3542 "A function to customize how completions are identified in completion lists.
3543 `completion-setup-function' calls this function with no arguments
3544 each time it has found what it thinks is one completion.
3545 Point is at the end of the completion in the completion list buffer.
3546 If this function moves point, it can alter the end of that completion.")
3548 (defvar completion-setup-hook nil
3549 "Normal hook run at the end of setting up a completion list buffer.
3550 When this hook is run, the current buffer is the one in which the
3551 command to display the completion list buffer was run.
3552 The completion list buffer is available as the value of `standard-output'.")
3554 ;; This function goes in completion-setup-hook, so that it is called
3555 ;; after the text of the completion list buffer is written.
3557 (defun completion-setup-function ()
3559 (let ((mainbuf (current-buffer)))
3560 (set-buffer standard-output)
3561 (completion-list-mode)
3562 (make-local-variable 'completion-reference-buffer)
3563 (setq completion-reference-buffer mainbuf)
3564 (if (eq minibuffer-completion-table 'read-file-name-internal)
3565 ;; For file name completion,
3566 ;; use the number of chars before the start of the
3567 ;; last file name component.
3568 (setq completion-base-size
3570 (set-buffer mainbuf)
3571 (goto-char (point-max))
3572 (skip-chars-backward (format "^%c" directory-sep-char))
3573 (- (point) (point-min))))
3574 ;; Otherwise, in minibuffer, the whole input is being completed.
3576 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3577 (buffer-name mainbuf))
3578 (setq completion-base-size 0))))
3579 (goto-char (point-min))
3581 (insert (substitute-command-keys
3582 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3583 (insert (substitute-command-keys
3584 "In this buffer, type \\[choose-completion] to \
3585 select the completion near point.\n\n"))
3587 (while (re-search-forward "[^ \t\n]+\\( [^ \t\n]+\\)*" nil t)
3588 (let ((beg (match-beginning 0))
3590 (if completion-fixup-function
3591 (funcall completion-fixup-function))
3592 (put-text-property beg (point) 'mouse-face 'highlight)
3593 (goto-char end))))))
3595 (add-hook 'completion-setup-hook 'completion-setup-function)
3597 (define-key minibuffer-local-completion-map [prior]
3598 'switch-to-completions)
3599 (define-key minibuffer-local-must-match-map [prior]
3600 'switch-to-completions)
3601 (define-key minibuffer-local-completion-map "\M-v"
3602 'switch-to-completions)
3603 (define-key minibuffer-local-must-match-map "\M-v"
3604 'switch-to-completions)
3606 (defun switch-to-completions ()
3607 "Select the completion list window."
3609 ;; Make sure we have a completions window.
3610 (or (get-buffer-window "*Completions*")
3611 (minibuffer-completion-help))
3612 (select-window (get-buffer-window "*Completions*"))
3613 (goto-char (point-min))
3614 (search-forward "\n\n")
3617 ;; Support keyboard commands to turn on various modifiers.
3619 ;; These functions -- which are not commands -- each add one modifier
3620 ;; to the following event.
3622 (defun event-apply-alt-modifier (ignore-prompt)
3623 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3624 (defun event-apply-super-modifier (ignore-prompt)
3625 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3626 (defun event-apply-hyper-modifier (ignore-prompt)
3627 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3628 (defun event-apply-shift-modifier (ignore-prompt)
3629 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3630 (defun event-apply-control-modifier (ignore-prompt)
3631 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3632 (defun event-apply-meta-modifier (ignore-prompt)
3633 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3635 (defun event-apply-modifier (event symbol lshiftby prefix)
3636 "Apply a modifier flag to event EVENT.
3637 SYMBOL is the name of this modifier, as a symbol.
3638 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3639 PREFIX is the string that represents this modifier in an event type symbol."
3641 (cond ((eq symbol 'control)
3642 (if (and (<= (downcase event) ?z)
3643 (>= (downcase event) ?a))
3644 (- (downcase event) ?a -1)
3645 (if (and (<= (downcase event) ?Z)
3646 (>= (downcase event) ?A))
3647 (- (downcase event) ?A -1)
3648 (logior (lsh 1 lshiftby) event))))
3650 (if (and (<= (downcase event) ?z)
3651 (>= (downcase event) ?a))
3653 (logior (lsh 1 lshiftby) event)))
3655 (logior (lsh 1 lshiftby) event)))
3656 (if (memq symbol (event-modifiers event))
3658 (let ((event-type (if (symbolp event) event (car event))))
3659 (setq event-type (intern (concat prefix (symbol-name event-type))))
3662 (cons event-type (cdr event)))))))
3664 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
3665 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
3666 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
3667 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
3668 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
3669 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
3671 ;;;; Keypad support.
3673 ;;; Make the keypad keys act like ordinary typing keys. If people add
3674 ;;; bindings for the function key symbols, then those bindings will
3675 ;;; override these, so this shouldn't interfere with any existing
3678 ;; Also tell read-char how to handle these keys.
3680 (lambda (keypad-normal)
3681 (let ((keypad (nth 0 keypad-normal))
3682 (normal (nth 1 keypad-normal)))
3683 (put keypad 'ascii-character normal)
3684 (define-key function-key-map (vector keypad) (vector normal))))
3685 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
3686 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
3698 ;;; simple.el ends here