1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 1999
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 fundamental-mode ()
45 "Major mode not specialized for anything in particular.
46 Other major modes are defined by comparison with this one."
48 (kill-all-local-variables))
50 ;; Making and deleting lines.
52 (defun newline (&optional arg
)
53 "Insert a newline, and move to left margin of the new line if it's blank.
54 The newline is marked with the text-property `hard'.
55 With arg, insert that many newlines.
56 In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
58 (barf-if-buffer-read-only)
59 ;; Inserting a newline at the end of a line produces better redisplay in
60 ;; try_window_id than inserting at the beginning of a line, and the textual
61 ;; result is the same. So, if we're at beginning of line, pretend to be at
62 ;; the end of the previous line.
63 (let ((flag (and (not (bobp))
65 ;; Make sure no functions want to be told about
66 ;; the range of the changes.
67 (not after-change-function
)
68 (not before-change-function
)
69 (not after-change-functions
)
70 (not before-change-functions
)
71 ;; Make sure there are no markers here.
72 (not (buffer-has-markers-at (1- (point))))
73 (not (buffer-has-markers-at (point)))
74 ;; Make sure no text properties want to know
75 ;; where the change was.
76 (not (get-char-property (1- (point)) 'modification-hooks
))
77 (not (get-char-property (1- (point)) 'insert-behind-hooks
))
79 (not (get-char-property (point) 'insert-in-front-hooks
)))
80 ;; Make sure the newline before point isn't intangible.
81 (not (get-char-property (1- (point)) 'intangible
))
82 ;; Make sure the newline before point isn't read-only.
83 (not (get-char-property (1- (point)) 'read-only
))
84 ;; Make sure the newline before point isn't invisible.
85 (not (get-char-property (1- (point)) 'invisible
))
86 ;; Make sure the newline before point has the same
87 ;; properties as the char before it (if any).
88 (< (or (previous-property-change (point)) -
2)
90 (was-page-start (and (bolp)
91 (looking-at page-delimiter
)))
93 (if flag
(backward-char 1))
94 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
95 ;; Set last-command-char to tell self-insert what to insert.
96 (let ((last-command-char ?
\n)
97 ;; Don't auto-fill if we have a numeric argument.
98 ;; Also not if flag is true (it would fill wrong line);
99 ;; there is no need to since we're at BOL.
100 (auto-fill-function (if (or arg flag
) nil auto-fill-function
)))
102 (self-insert-command (prefix-numeric-value arg
))
103 ;; If we get an error in self-insert-command, put point at right place.
104 (if flag
(forward-char 1))))
105 ;; Even if we did *not* get an error, keep that forward-char;
106 ;; all further processing should apply to the newline that the user
107 ;; thinks he inserted.
109 ;; Mark the newline(s) `hard'.
110 (if use-hard-newlines
111 (set-hard-newline-properties
112 (- (point) (if arg
(prefix-numeric-value arg
) 1)) (point)))
113 ;; If the newline leaves the previous line blank,
114 ;; and we have a left margin, delete that from the blank line.
117 (goto-char beforepos
)
119 (and (looking-at "[ \t]$")
120 (> (current-left-margin) 0)
121 (delete-region (point) (progn (end-of-line) (point))))))
122 ;; Indent the line after the newline, except in one case:
123 ;; when we added the newline at the beginning of a line
124 ;; which starts a page.
126 (move-to-left-margin nil t
)))
129 (defun set-hard-newline-properties (from to
)
130 (let ((sticky (get-text-property from
'rear-nonsticky
)))
131 (put-text-property from to
'hard
't
)
132 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
133 (if (and (listp sticky
) (not (memq 'hard sticky
)))
134 (put-text-property from
(point) 'rear-nonsticky
135 (cons 'hard sticky
)))))
137 (defun open-line (arg)
138 "Insert a newline and leave point before it.
139 If there is a fill prefix and/or a left-margin, insert them on the new line
140 if the line would have been blank.
141 With arg N, insert N newlines."
143 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
144 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
150 (if do-left-margin
(indent-to (current-left-margin)))
151 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
158 "Split current line, moving portion beyond point vertically down."
160 (skip-chars-forward " \t")
161 (let ((col (current-column))
167 (defun delete-indentation (&optional arg
)
168 "Join this line to previous and fix up whitespace at join.
169 If there is a fill prefix, delete it from the beginning of this line.
170 With argument, join this line to following line."
173 (if arg
(forward-line 1))
174 (if (eq (preceding-char) ?
\n)
176 (delete-region (point) (1- (point)))
177 ;; If the second line started with the fill prefix,
178 ;; delete the prefix.
180 (<= (+ (point) (length fill-prefix
)) (point-max))
182 (buffer-substring (point)
183 (+ (point) (length fill-prefix
)))))
184 (delete-region (point) (+ (point) (length fill-prefix
))))
185 (fixup-whitespace))))
187 (defalias 'join-line
#'delete-indentation
) ; easier to find
189 (defun delete-blank-lines ()
190 "On blank line, delete all surrounding blank lines, leaving just one.
191 On isolated blank line, delete that one.
192 On nonblank line, delete any immediately following blank lines."
194 (let (thisblank singleblank
)
197 (setq thisblank
(looking-at "[ \t]*$"))
198 ;; Set singleblank if there is just one blank line here.
201 (not (looking-at "[ \t]*\n[ \t]*$"))
203 (progn (forward-line -
1)
204 (not (looking-at "[ \t]*$")))))))
205 ;; Delete preceding blank lines, and this one too if it's the only one.
209 (if singleblank
(forward-line 1))
210 (delete-region (point)
211 (if (re-search-backward "[^ \t\n]" nil t
)
212 (progn (forward-line 1) (point))
214 ;; Delete following blank lines, unless the current line is blank
215 ;; and there are no following blank lines.
216 (if (not (and thisblank singleblank
))
220 (delete-region (point)
221 (if (re-search-forward "[^ \t\n]" nil t
)
222 (progn (beginning-of-line) (point))
224 ;; Handle the special case where point is followed by newline and eob.
225 ;; Delete the line, leaving point at eob.
226 (if (looking-at "^[ \t]*\n\\'")
227 (delete-region (point) (point-max)))))
229 (defun newline-and-indent ()
230 "Insert a newline, then indent according to major mode.
231 Indentation is done using the value of `indent-line-function'.
232 In programming language modes, this is the same as TAB.
233 In some text modes, where TAB inserts a tab, this command indents to the
234 column specified by the function `current-left-margin'."
236 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
238 (indent-according-to-mode))
240 (defun reindent-then-newline-and-indent ()
241 "Reindent current line, insert newline, then indent the new line.
242 Indentation of both lines is done according to the current major mode,
243 which means calling the current value of `indent-line-function'.
244 In programming language modes, this is the same as TAB.
245 In some text modes, where TAB inserts a tab, this indents to the
246 column specified by the function `current-left-margin'."
249 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
250 (indent-according-to-mode))
252 (indent-according-to-mode))
254 (defun quoted-insert (arg)
255 "Read next input character and insert it.
256 This is useful for inserting control characters.
258 If the first character you type after this command is an octal digit,
259 you should type a sequence of octal digits which specify a character code.
260 Any nondigit terminates the sequence. If the terminator is a RET,
261 it is discarded; any other terminator is used itself as input.
262 The variable `read-quoted-char-radix' specifies the radix for this feature;
263 set it to 10 or 16 to use decimal or hex instead of octal.
265 In overwrite mode, this function inserts the character anyway, and
266 does not handle octal digits specially. This means that if you use
267 overwrite as your normal editing mode, you can use this function to
268 insert characters when necessary.
270 In binary overwrite mode, this function does overwrite, and octal
271 digits are interpreted as a character code. This is intended to be
272 useful for editing binary files."
274 (let ((char (if (or (not overwrite-mode
)
275 (eq overwrite-mode
'overwrite-mode-binary
))
278 ;; Assume character codes 0240 - 0377 stand for characters in some
279 ;; single-byte character set, and convert them to Emacs
281 (if (and enable-multibyte-characters
284 (setq char
(unibyte-char-to-multibyte char
)))
286 (if (eq overwrite-mode
'overwrite-mode-binary
)
289 (insert-and-inherit char
)
290 (setq arg
(1- arg
)))))
292 (defun forward-to-indentation (arg)
293 "Move forward ARG lines and position at first nonblank character."
296 (skip-chars-forward " \t"))
298 (defun backward-to-indentation (arg)
299 "Move backward ARG lines and position at first nonblank character."
301 (forward-line (- arg
))
302 (skip-chars-forward " \t"))
304 (defun back-to-indentation ()
305 "Move point to the first non-whitespace character on this line."
307 (beginning-of-line 1)
308 (skip-chars-forward " \t"))
310 (defun fixup-whitespace ()
311 "Fixup white space between objects around point.
312 Leave one space or none, according to the context."
315 (delete-horizontal-space)
316 (if (or (looking-at "^\\|\\s)")
317 (save-excursion (forward-char -
1)
318 (looking-at "$\\|\\s(\\|\\s'")))
322 (defun delete-horizontal-space ()
323 "Delete all spaces and tabs around point."
325 (skip-chars-backward " \t")
326 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
328 (defun just-one-space ()
329 "Delete all spaces and tabs around point, leaving one space."
331 (skip-chars-backward " \t")
332 (if (= (following-char) ?
)
335 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
338 (defun beginning-of-buffer (&optional arg
)
339 "Move point to the beginning of the buffer; leave mark at previous position.
340 With arg N, put point N/10 of the way from the beginning.
342 If the buffer is narrowed, this command uses the beginning and size
343 of the accessible part of the buffer.
345 Don't use this command in Lisp programs!
346 \(goto-char (point-min)) is faster and avoids clobbering the mark."
349 (let ((size (- (point-max) (point-min))))
353 ;; Avoid overflow for large buffer sizes!
354 (* (prefix-numeric-value arg
)
356 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
358 (if arg
(forward-line 1)))
360 (defun end-of-buffer (&optional arg
)
361 "Move point to the end of the buffer; leave mark at previous position.
362 With arg N, put point N/10 of the way from the end.
364 If the buffer is narrowed, this command uses the beginning and size
365 of the accessible part of the buffer.
367 Don't use this command in Lisp programs!
368 \(goto-char (point-max)) is faster and avoids clobbering the mark."
371 (let ((size (- (point-max) (point-min))))
375 ;; Avoid overflow for large buffer sizes!
376 (* (prefix-numeric-value arg
)
378 (/ (* size
(prefix-numeric-value arg
)) 10)))
380 ;; If we went to a place in the middle of the buffer,
381 ;; adjust it to the beginning of a line.
382 (cond (arg (forward-line 1))
383 ((< (point) (window-end nil t
))
384 ;; If the end of the buffer is not already on the screen,
385 ;; then scroll specially to put it near, but not at, the bottom.
386 (overlay-recenter (point))
389 (defun mark-whole-buffer ()
390 "Put point at beginning and mark at end of buffer.
391 You probably should not use this function in Lisp programs;
392 it is usually a mistake for a Lisp function to use any subroutine
393 that uses or sets the mark."
396 (push-mark (point-max) nil t
)
397 (goto-char (point-min)))
399 ;; Counting lines, one way or another.
401 (defun goto-line (arg)
402 "Goto line ARG, counting from line 1 at beginning of buffer."
403 (interactive "NGoto line: ")
404 (setq arg
(prefix-numeric-value arg
))
408 (if (eq selective-display t
)
409 (re-search-forward "[\n\C-m]" nil
'end
(1- arg
))
410 (forward-line (1- arg
)))))
412 (defun count-lines-region (start end
)
413 "Print number of lines and characters in the region."
415 (message "Region has %d lines, %d characters"
416 (count-lines start end
) (- end start
)))
419 "Print the current buffer line number and narrowed line number of point."
421 (let ((opoint (point)) start
)
424 (goto-char (point-min))
431 (message "line %d (narrowed line %d)"
432 (1+ (count-lines 1 (point)))
433 (1+ (count-lines start
(point))))
434 (message "Line %d" (1+ (count-lines 1 (point)))))))))
436 (defun count-lines (start end
)
437 "Return number of lines between START and END.
438 This is usually the number of newlines between them,
439 but can be one more if START is not equal to END
440 and the greater of them is not at the start of a line."
443 (narrow-to-region start end
)
444 (goto-char (point-min))
445 (if (eq selective-display t
)
448 (while (re-search-forward "[\n\C-m]" nil t
40)
449 (setq done
(+ 40 done
)))
450 (while (re-search-forward "[\n\C-m]" nil t
1)
451 (setq done
(+ 1 done
)))
452 (goto-char (point-max))
453 (if (and (/= start end
)
457 (- (buffer-size) (forward-line (buffer-size)))))))
459 (defun what-cursor-position (&optional detail
)
460 "Print info on cursor position (on screen and within buffer).
461 Also describe the character after point, and give its character code
462 in octal, decimal and hex.
464 For a non-ASCII multibyte character, also give its encoding in the
465 buffer's selected coding system if the coding system encodes the
466 character safely. If the character is encoded into one byte, that
467 code is shown in hex. If the character is encoded into more than one
468 byte, just \"...\" is shown.
470 With prefix argument, print additional details about that character,
471 instead of the cursor position. This includes the character set name,
472 the codes that identify the character within that character set. In
473 addition, the encoding is fully shown."
475 (let* ((char (following-char))
479 (total (buffer-size))
480 (percent (if (> total
50000)
481 ;; Avoid overflow from multiplying by 100!
482 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
483 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
484 (hscroll (if (= (window-hscroll) 0)
486 (format " Hscroll=%d" (window-hscroll))))
487 (col (current-column)))
489 (if (or (/= beg
1) (/= end
(1+ total
)))
490 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
491 pos total percent beg end col hscroll
)
492 (message "point=%d of %d(%d%%) column %d %s"
493 pos total percent col hscroll
))
494 (let ((coding buffer-file-coding-system
)
495 encoded encoding-msg
)
497 (eq (coding-system-type coding
) t
))
498 (setq coding default-buffer-file-coding-system
))
499 (if (not (char-valid-p char
))
501 (format "(0%o, %d, 0x%x, invalid)" char char char
))
502 (setq encoded
(and (>= char
128) (encode-coding-char char coding
)))
505 (format "(0%o, %d, 0x%x, ext %s)"
507 (if (and (not detail
)
508 (> (length encoded
) 1))
511 (encoded-string-description encoded coding
)
512 (if (cmpcharp char
) "..." ""))))
513 (format "(0%o, %d, 0x%x)" char char char
))))
515 ;; We show the detailed information of CHAR.
518 ;; For a composite character, we show the
520 (concat "(composed \""
521 (decompose-composite-char char
)
524 (message "Char: %s %s %s"
526 (single-key-description char
)
527 (buffer-substring (point) (1+ (point))))
528 encoding-msg internal
))
529 (if (or (/= beg
1) (/= end
(1+ total
)))
530 (message "Char: %s %s point=%d of %d(%d%%) <%d - %d> column %d %s"
532 (single-key-description char
)
533 (buffer-substring (point) (1+ (point))))
534 encoding-msg pos total percent beg end col hscroll
)
535 (message "Char: %s %s point=%d of %d(%d%%) column %d %s"
537 (single-key-description char
)
538 (buffer-substring (point) (1+ (point))))
539 encoding-msg pos total percent col hscroll
)))))))
541 (defvar read-expression-map
(cons 'keymap minibuffer-local-map
)
542 "Minibuffer keymap used for reading Lisp expressions.")
543 (define-key read-expression-map
"\M-\t" 'lisp-complete-symbol
)
545 (defvar read-expression-history nil
)
547 (defcustom eval-expression-print-level
4
548 "*Value to use for `print-level' when printing value in `eval-expression'."
553 (defcustom eval-expression-print-length
12
554 "*Value to use for `print-length' when printing value in `eval-expression'."
559 (defcustom eval-expression-debug-on-error t
560 "*Value to use for `debug-on-error' when evaluating in `eval-expression'."
565 ;; We define this, rather than making `eval' interactive,
566 ;; for the sake of completion of names like eval-region, eval-current-buffer.
567 (defun eval-expression (eval-expression-arg
568 &optional eval-expression-insert-value
)
569 "Evaluate EXPRESSION and print value in minibuffer.
570 Value is also consed on to front of the variable `values'."
572 (list (read-from-minibuffer "Eval: "
573 nil read-expression-map t
574 'read-expression-history
)
576 (let ((debug-on-error eval-expression-debug-on-error
))
577 (setq values
(cons (eval eval-expression-arg
) values
)))
578 (let ((print-length eval-expression-print-length
)
579 (print-level eval-expression-print-level
))
581 (if eval-expression-insert-value
(current-buffer) t
))))
583 (defun edit-and-eval-command (prompt command
)
584 "Prompting with PROMPT, let user edit COMMAND and eval result.
585 COMMAND is a Lisp expression. Let user edit that expression in
586 the minibuffer, then read and evaluate the result."
587 (let ((command (read-from-minibuffer prompt
588 (prin1-to-string command
)
589 read-expression-map t
590 '(command-history .
1))))
591 ;; If command was added to command-history as a string,
592 ;; get rid of that. We want only evaluable expressions there.
593 (if (stringp (car command-history
))
594 (setq command-history
(cdr command-history
)))
596 ;; If command to be redone does not match front of history,
597 ;; add it to the history.
598 (or (equal command
(car command-history
))
599 (setq command-history
(cons command command-history
)))
602 (defun repeat-complex-command (arg)
603 "Edit and re-evaluate last complex command, or ARGth from last.
604 A complex command is one which used the minibuffer.
605 The command is placed in the minibuffer as a Lisp form for editing.
606 The result is executed, repeating the command as changed.
607 If the command has been changed or is not the most recent previous command
608 it is added to the front of the command history.
609 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
610 to get different commands to edit and resubmit."
612 (let ((elt (nth (1- arg
) command-history
))
617 (let ((print-level nil
)
618 (minibuffer-history-position arg
)
619 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
620 (read-from-minibuffer
621 "Redo: " (prin1-to-string elt
) read-expression-map t
622 (cons 'command-history arg
))))
624 ;; If command was added to command-history as a string,
625 ;; get rid of that. We want only evaluable expressions there.
626 (if (stringp (car command-history
))
627 (setq command-history
(cdr command-history
)))
629 ;; If command to be redone does not match front of history,
630 ;; add it to the history.
631 (or (equal newcmd
(car command-history
))
632 (setq command-history
(cons newcmd command-history
)))
636 (defvar minibuffer-history nil
637 "Default minibuffer history list.
638 This is used for all minibuffer input
639 except when an alternate history list is specified.")
640 (defvar minibuffer-history-sexp-flag nil
641 "Non-nil when doing history operations on `command-history'.
642 More generally, indicates that the history list being acted on
643 contains expressions rather than strings.
644 It is only valid if its value equals the current minibuffer depth,
645 to handle recursive uses of the minibuffer.")
646 (setq minibuffer-history-variable
'minibuffer-history
)
647 (setq minibuffer-history-position nil
)
648 (defvar minibuffer-history-search-history nil
)
651 (lambda (key-and-command)
653 (lambda (keymap-and-completionp)
654 ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
655 ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
656 ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
657 (define-key (symbol-value (car keymap-and-completionp
))
658 (car key-and-command
)
659 (let ((command (cdr key-and-command
)))
661 ;; (and ... nil) => ... turns back on the completion-oriented
662 ;; history commands which rms turned off since they seem to
663 ;; do things he doesn't like.
664 (if (and (cdr keymap-and-completionp
) nil
) ;XXX turned off
665 (progn (error "EMACS BUG!") (cdr command
))
668 '((minibuffer-local-map . nil
)
669 (minibuffer-local-ns-map . nil
)
670 (minibuffer-local-completion-map . t
)
671 (minibuffer-local-must-match-map . t
)
672 (read-expression-map . nil
))))
673 '(("\en" .
(next-history-element . next-complete-history-element
))
674 ([next] . (next-history-element . next-complete-history-element))
675 ("\ep" . (previous-history-element . previous-complete-history-element))
676 ([prior] . (previous-history-element . previous-complete-history-element))
677 ("\er" . previous-matching-history-element)
678 ("\es" . next-matching-history-element)))
680 (defvar minibuffer-text-before-history nil
681 "Text that was in this minibuffer before any history commands.
682 This is nil if there have not yet been any history commands
683 in this use of the minibuffer.")
685 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
687 (defun minibuffer-history-initialize ()
688 (setq minibuffer-text-before-history nil))
690 (defcustom minibuffer-history-case-insensitive-variables nil
691 "*Minibuffer history variables for which matching should ignore case.
692 If a history variable is a member of this list, then the
693 \\[previous-matching-history-element] and \\[next-matching-history-element]\
694 commands ignore case when searching it, regardless of `case-fold-search'."
695 :type '(repeat variable)
698 (defun previous-matching-history-element (regexp n)
699 "Find the previous history element that matches REGEXP.
700 \(Previous history elements refer to earlier actions.)
701 With prefix argument N, search for Nth previous match.
702 If N is negative, find the next or Nth next match.
703 An uppercase letter in REGEXP makes the search case-sensitive.
704 See also `minibuffer-history-case-insensitive-variables'."
706 (let* ((enable-recursive-minibuffers t)
707 (regexp (read-from-minibuffer "Previous element matching (regexp): "
711 'minibuffer-history-search-history)))
712 ;; Use the last regexp specified, by default, if input is empty.
713 (list (if (string= regexp "")
714 (if minibuffer-history-search-history
715 (car minibuffer-history-search-history)
716 (error "No previous history search regexp"))
718 (prefix-numeric-value current-prefix-arg))))
719 (if (and (zerop minibuffer-history-position)
720 (null minibuffer-text-before-history))
721 (setq minibuffer-text-before-history (field-string (point-max))))
722 (let ((history (symbol-value minibuffer-history-variable))
724 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
725 ;; On some systems, ignore case for file names.
726 (if (memq minibuffer-history-variable
727 minibuffer-history-case-insensitive-variables)
729 ;; Respect the user's setting for case-fold-search:
733 (pos minibuffer-history-position))
736 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
739 "No later matching history item"
740 "No earlier matching history item")))
741 (if (string-match regexp
742 (if (eq minibuffer-history-sexp-flag
744 (let ((print-level nil))
745 (prin1-to-string (nth (1- pos) history)))
746 (nth (1- pos) history)))
747 (setq n (+ n (if (< n 0) 1 -1)))))
748 (setq minibuffer-history-position pos)
749 (goto-char (point-max))
751 (let ((elt (nth (1- pos) history)))
752 (insert (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
753 (let ((print-level nil))
754 (prin1-to-string elt))
756 (goto-char (field-beginning)))
757 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
758 (eq (car (car command-history)) 'next-matching-history-element))
759 (setq command-history (cdr command-history))))
761 (defun next-matching-history-element (regexp n)
762 "Find the next history element that matches REGEXP.
763 \(The next history element refers to a more recent action.)
764 With prefix argument N, search for Nth next match.
765 If N is negative, find the previous or Nth previous match.
766 An uppercase letter in REGEXP makes the search case-sensitive."
768 (let* ((enable-recursive-minibuffers t)
769 (regexp (read-from-minibuffer "Next element matching (regexp): "
773 'minibuffer-history-search-history)))
774 ;; Use the last regexp specified, by default, if input is empty.
775 (list (if (string= regexp "")
776 (setcar minibuffer-history-search-history
777 (nth 1 minibuffer-history-search-history))
779 (prefix-numeric-value current-prefix-arg))))
780 (previous-matching-history-element regexp (- n)))
782 (defun next-history-element (n)
783 "Insert the next element of the minibuffer history into the minibuffer."
786 (let ((narg (- minibuffer-history-position n))
787 (minimum (if minibuffer-default -1 0))
788 elt minibuffer-returned-to-present)
789 (if (and (zerop minibuffer-history-position)
790 (null minibuffer-text-before-history))
791 (setq minibuffer-text-before-history (field-string (point-max))))
793 (if minibuffer-default
794 (error "End of history; no next item")
795 (error "End of history; no default available")))
796 (if (> narg (length (symbol-value minibuffer-history-variable)))
797 (error "Beginning of history; no preceding item"))
798 (goto-char (point-max))
800 (setq minibuffer-history-position narg)
802 (setq elt minibuffer-default))
804 (setq elt (or minibuffer-text-before-history ""))
805 (setq minibuffer-returned-to-present t)
806 (setq minibuffer-text-before-history nil))
807 (t (setq elt (nth (1- minibuffer-history-position)
808 (symbol-value minibuffer-history-variable)))))
810 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
811 (not minibuffer-returned-to-present))
812 (let ((print-level nil))
813 (prin1-to-string elt))
815 (goto-char (field-beginning)))))
817 (defun previous-history-element (n)
818 "Inserts the previous element of the minibuffer history into the minibuffer."
820 (next-history-element (- n)))
822 (defun next-complete-history-element (n)
823 "Get next history element which completes the minibuffer before the point.
824 The contents of the minibuffer after the point are deleted, and replaced
825 by the new completion."
827 (let ((point-at-start (point)))
828 (next-matching-history-element
830 "^" (regexp-quote (buffer-substring (field-beginning) (point))))
832 ;; next-matching-history-element always puts us at (point-min).
833 ;; Move to the position we were at before changing the buffer contents.
834 ;; This is still sensical, because the text before point has not changed.
835 (goto-char point-at-start)))
837 (defun previous-complete-history-element (n)
839 Get previous history element which completes the minibuffer before the point.
840 The contents of the minibuffer after the point are deleted, and replaced
841 by the new completion."
843 (next-complete-history-element (- n)))
845 ;; These two functions are for compatibility with the old subrs of the
848 (defun minibuffer-prompt-width ()
849 "Return the display width of the minibuffer prompt.
850 Return 0 if current buffer is not a mini-buffer."
851 ;; Return the width of everything before the field at the end of
852 ;; the buffer; this should be 0 for normal buffers.
853 (1- (field-beginning (point-max))))
855 (defun minibuffer-prompt-end ()
856 "Return the buffer position of the end of the minibuffer prompt.
857 Return 0 if current buffer is not a mini-buffer."
858 (field-beginning (point-max)))
861 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
862 (defalias 'advertised-undo 'undo)
864 (defun undo (&optional arg)
865 "Undo some previous changes.
866 Repeat this command to undo more changes.
867 A numeric argument serves as a repeat count.
869 Just C-u as argument requests selective undo,
870 limited to changes within the current region.
871 Likewise in Transient Mark mode when the mark is active."
873 ;; If we don't get all the way thru, make last-command indicate that
874 ;; for the following command.
875 (setq this-command t)
876 (let ((modified (buffer-modified-p))
877 (recent-save (recent-auto-save-p)))
878 (or (eq (selected-window) (minibuffer-window))
880 (or (eq last-command 'undo)
881 (progn (if (or arg (and transient-mark-mode mark-active))
882 (undo-start (region-beginning) (region-end))
885 (undo-more (if arg (prefix-numeric-value arg) 1))
886 ;; Don't specify a position in the undo record for the undo command.
887 ;; Instead, undoing this should move point to where the change is.
888 (let ((tail buffer-undo-list)
890 (while (and tail (not done) (not (null (car tail))))
891 (if (integerp (car tail))
894 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
895 (setq tail (cdr tail))))
896 (and modified (not (buffer-modified-p))
897 (delete-auto-save-file-if-necessary recent-save)))
898 ;; If we do get all the way thru, make this-command indicate that.
899 (setq this-command 'undo))
901 (defvar pending-undo-list nil
902 "Within a run of consecutive undo commands, list remaining to be undone.")
904 (defvar undo-in-progress nil
905 "Non-nil while performing an undo.
906 Some change-hooks test this variable to do something different.")
908 (defun undo-more (count)
909 "Undo back N undo-boundaries beyond what was already undone recently.
910 Call `undo-start' to get ready to undo recent changes,
911 then call `undo-more' one or more times to undo them."
912 (or pending-undo-list
913 (error "No further undo information"))
914 (let ((undo-in-progress t))
915 (setq pending-undo-list (primitive-undo count pending-undo-list))))
917 ;; Deep copy of a list
918 (defun undo-copy-list (list)
919 "Make a copy of undo list LIST."
920 (mapcar 'undo-copy-list-1 list))
922 (defun undo-copy-list-1 (elt)
924 (cons (car elt) (undo-copy-list-1 (cdr elt)))
927 (defun undo-start (&optional beg end)
928 "Set `pending-undo-list' to the front of the undo list.
929 The next call to `undo-more' will undo the most recently made change.
930 If BEG and END are specified, then only undo elements
931 that apply to text between BEG and END are used; other undo elements
932 are ignored. If BEG and END are nil, all undo elements are used."
933 (if (eq buffer-undo-list t)
934 (error "No undo information in this buffer"))
935 (setq pending-undo-list
936 (if (and beg end (not (= beg end)))
937 (undo-make-selective-list (min beg end) (max beg end))
940 (defvar undo-adjusted-markers)
942 (defun undo-make-selective-list (start end)
943 "Return a list of undo elements for the region START to END.
944 The elements come from `buffer-undo-list', but we keep only
945 the elements inside this region, and discard those outside this region.
946 If we find an element that crosses an edge of this region,
947 we stop and ignore all further elements."
948 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
949 (undo-list (list nil))
950 undo-adjusted-markers
952 undo-elt undo-elt temp-undo-list delta)
953 (while undo-list-copy
954 (setq undo-elt (car undo-list-copy))
956 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
957 ;; This is a "was unmodified" element.
958 ;; Keep it if we have kept everything thus far.
961 (undo-elt-in-region undo-elt start end)))))
964 (setq end (+ end (cdr (undo-delta undo-elt))))
965 ;; Don't put two nils together in the list
966 (if (not (and (eq (car undo-list) nil)
968 (setq undo-list (cons undo-elt undo-list))))
969 (if (undo-elt-crosses-region undo-elt start end)
970 (setq undo-list-copy nil)
971 (setq some-rejected t)
972 (setq temp-undo-list (cdr undo-list-copy))
973 (setq delta (undo-delta undo-elt))
975 (when (/= (cdr delta) 0)
976 (let ((position (car delta))
977 (offset (cdr delta)))
979 ;; Loop down the earlier events adjusting their buffer positions
980 ;; to reflect the fact that a change to the buffer isn't being
981 ;; undone. We only need to process those element types which
982 ;; undo-elt-in-region will return as being in the region since
983 ;; only those types can ever get into the output
985 (while temp-undo-list
986 (setq undo-elt (car temp-undo-list))
987 (cond ((integerp undo-elt)
988 (if (>= undo-elt position)
989 (setcar temp-undo-list (- undo-elt offset))))
990 ((atom undo-elt) nil)
991 ((stringp (car undo-elt))
993 (let ((text-pos (abs (cdr undo-elt)))
994 (point-at-end (< (cdr undo-elt) 0 )))
995 (if (>= text-pos position)
996 (setcdr undo-elt (* (if point-at-end -1 1)
997 (- text-pos offset))))))
998 ((integerp (car undo-elt))
1000 (when (>= (car undo-elt) position)
1001 (setcar undo-elt (- (car undo-elt) offset))
1002 (setcdr undo-elt (- (cdr undo-elt) offset))))
1003 ((null (car undo-elt))
1004 ;; (nil PROPERTY VALUE BEG . END)
1005 (let ((tail (nthcdr 3 undo-elt)))
1006 (when (>= (car tail) position)
1007 (setcar tail (- (car tail) offset))
1008 (setcdr tail (- (cdr tail) offset))))))
1009 (setq temp-undo-list (cdr temp-undo-list))))))))
1010 (setq undo-list-copy (cdr undo-list-copy)))
1011 (nreverse undo-list)))
1013 (defun undo-elt-in-region (undo-elt start end)
1014 "Determine whether UNDO-ELT falls inside the region START ... END.
1015 If it crosses the edge, we return nil."
1016 (cond ((integerp undo-elt)
1017 (and (>= undo-elt start)
1023 ((stringp (car undo-elt))
1024 ;; (TEXT . POSITION)
1025 (and (>= (abs (cdr undo-elt)) start)
1026 (< (abs (cdr undo-elt)) end)))
1027 ((and (consp undo-elt) (markerp (car undo-elt)))
1028 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1029 ;; See if MARKER is inside the region.
1030 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1032 (setq alist-elt (cons (car undo-elt)
1033 (marker-position (car undo-elt))))
1034 (setq undo-adjusted-markers
1035 (cons alist-elt undo-adjusted-markers)))
1036 (and (cdr alist-elt)
1037 (>= (cdr alist-elt) start)
1038 (< (cdr alist-elt) end))))
1039 ((null (car undo-elt))
1040 ;; (nil PROPERTY VALUE BEG . END)
1041 (let ((tail (nthcdr 3 undo-elt)))
1042 (and (>= (car tail) start)
1043 (< (cdr tail) end))))
1044 ((integerp (car undo-elt))
1046 (and (>= (car undo-elt) start)
1047 (< (cdr undo-elt) end)))))
1049 (defun undo-elt-crosses-region (undo-elt start end)
1050 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1051 This assumes we have already decided that UNDO-ELT
1052 is not *inside* the region START...END."
1053 (cond ((atom undo-elt) nil)
1054 ((null (car undo-elt))
1055 ;; (nil PROPERTY VALUE BEG . END)
1056 (let ((tail (nthcdr 3 undo-elt)))
1057 (not (or (< (car tail) end)
1058 (> (cdr tail) start)))))
1059 ((integerp (car undo-elt))
1061 (not (or (< (car undo-elt) end)
1062 (> (cdr undo-elt) start))))))
1064 ;; Return the first affected buffer position and the delta for an undo element
1065 ;; delta is defined as the change in subsequent buffer positions if we *did*
1067 (defun undo-delta (undo-elt)
1068 (if (consp undo-elt)
1069 (cond ((stringp (car undo-elt))
1070 ;; (TEXT . POSITION)
1071 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1072 ((integerp (car undo-elt))
1074 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1079 (defvar shell-command-history nil
1080 "History list for some commands that read shell commands.")
1082 (defvar shell-command-switch "-c"
1083 "Switch used to have the shell execute its command line argument.")
1085 (defvar shell-command-default-error-buffer nil
1086 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1087 This buffer is used when `shell-command' or 'shell-command-on-region'
1088 is run interactively. A value of nil means that output to stderr and
1089 stdout will be intermixed in the output stream.")
1091 (defun shell-command (command &optional output-buffer error-buffer)
1092 "Execute string COMMAND in inferior shell; display output, if any.
1094 If COMMAND ends in ampersand, execute it asynchronously.
1095 The output appears in the buffer `*Async Shell Command*'.
1096 That buffer is in shell mode.
1098 Otherwise, COMMAND is executed synchronously. The output appears in the
1099 buffer `*Shell Command Output*'.
1100 If the output is one line, it is displayed in the echo area *as well*,
1101 but it is nonetheless available in buffer `*Shell Command Output*',
1102 even though that buffer is not automatically displayed.
1103 If there is no output, or if output is inserted in the current buffer,
1104 then `*Shell Command Output*' is deleted.
1106 To specify a coding system for converting non-ASCII characters
1107 in the shell command output, use \\[universal-coding-system-argument]
1108 before this command.
1110 Noninteractive callers can specify coding systems by binding
1111 `coding-system-for-read' and `coding-system-for-write'.
1113 The optional second argument OUTPUT-BUFFER, if non-nil,
1114 says to put the output in some other buffer.
1115 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1116 If OUTPUT-BUFFER is not a buffer and not nil,
1117 insert output in current buffer. (This cannot be done asynchronously.)
1118 In either case, the output is inserted after point (leaving mark after it).
1120 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1121 or buffer name to which to direct the command's standard error output.
1122 If it is nil, error output is mingled with regular output.
1123 In an interactive call, the variable `shell-command-default-error-buffer'
1124 specifies the value of ERROR-BUFFER."
1126 (interactive (list (read-from-minibuffer "Shell command: "
1127 nil nil nil 'shell-command-history)
1129 shell-command-default-error-buffer))
1130 ;; Look for a handler in case default-directory is a remote file name.
1132 (find-file-name-handler (directory-file-name default-directory)
1135 (funcall handler 'shell-command command output-buffer error-buffer)
1136 (if (and output-buffer
1137 (not (or (bufferp output-buffer) (stringp output-buffer))))
1141 (expand-file-name "scor"
1142 (or small-temporary-file-directory
1143 temporary-file-directory)))
1145 (barf-if-buffer-read-only)
1147 ;; We do not use -f for csh; we will not support broken use of
1148 ;; .cshrcs. Even the BSD csh manual says to use
1149 ;; "if ($?prompt) exit" before things which are not useful
1150 ;; non-interactively. Besides, if someone wants their other
1151 ;; aliases for shell commands then they can still have them.
1152 (call-process shell-file-name nil
1156 nil shell-command-switch command)
1157 (when (and error-file (file-exists-p error-file))
1158 (if (< 0 (nth 7 (file-attributes error-file)))
1159 (with-current-buffer (get-buffer-create error-buffer)
1160 (let ((pos-from-end (- (point-max) (point))))
1163 ;; Do no formatting while reading error file,
1164 ;; because that can run a shell command, and we
1165 ;; don't want that to cause an infinite recursion.
1166 (format-insert-file error-file nil)
1167 ;; Put point after the inserted errors.
1168 (goto-char (- (point-max) pos-from-end)))
1169 (display-buffer (current-buffer))))
1170 (delete-file error-file))
1171 ;; This is like exchange-point-and-mark, but doesn't
1172 ;; activate the mark. It is cleaner to avoid activation,
1173 ;; even though the command loop would deactivate the mark
1174 ;; because we inserted text.
1175 (goto-char (prog1 (mark t)
1176 (set-marker (mark-marker) (point)
1177 (current-buffer)))))
1178 ;; Preserve the match data in case called from a program.
1180 (if (string-match "[ \t]*&[ \t]*$" command)
1181 ;; Command ending with ampersand means asynchronous.
1182 (let ((buffer (get-buffer-create
1183 (or output-buffer "*Async Shell Command*")))
1184 (directory default-directory)
1186 ;; Remove the ampersand.
1187 (setq command (substring command 0 (match-beginning 0)))
1188 ;; If will kill a process, query first.
1189 (setq proc (get-buffer-process buffer))
1191 (if (yes-or-no-p "A command is running. Kill it? ")
1193 (error "Shell command in progress")))
1196 (setq buffer-read-only nil)
1198 (display-buffer buffer)
1199 (setq default-directory directory)
1200 (setq proc (start-process "Shell" buffer shell-file-name
1201 shell-command-switch command))
1202 (setq mode-line-process '(":%s"))
1203 (require 'shell) (shell-mode)
1204 (set-process-sentinel proc 'shell-command-sentinel)
1206 (shell-command-on-region (point) (point) command
1207 output-buffer nil error-buffer)))))))
1209 ;; We have a sentinel to prevent insertion of a termination message
1210 ;; in the buffer itself.
1211 (defun shell-command-sentinel (process signal)
1212 (if (memq (process-status process) '(exit signal))
1214 (car (cdr (cdr (process-command process))))
1215 (substring signal 0 -1))))
1217 (defun shell-command-on-region (start end command
1218 &optional output-buffer replace
1220 "Execute string COMMAND in inferior shell with region as input.
1221 Normally display output (if any) in temp buffer `*Shell Command Output*';
1222 Prefix arg means replace the region with it. Return the exit code of
1225 To specify a coding system for converting non-ASCII characters
1226 in the input and output to the shell command, use \\[universal-coding-system-argument]
1227 before this command. By default, the input (from the current buffer)
1228 is encoded in the same coding system that will be used to save the file,
1229 `buffer-file-coding-system'. If the output is going to replace the region,
1230 then it is decoded from that same coding system.
1232 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1233 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1234 systems by binding `coding-system-for-read' and
1235 `coding-system-for-write'.
1237 If the output is one line, it is displayed in the echo area,
1238 but it is nonetheless available in buffer `*Shell Command Output*'
1239 even though that buffer is not automatically displayed.
1240 If there is no output, or if output is inserted in the current buffer,
1241 then `*Shell Command Output*' is deleted.
1243 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1244 that says to put the output in some other buffer.
1245 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1246 If OUTPUT-BUFFER is not a buffer and not nil,
1247 insert output in the current buffer.
1248 In either case, the output is inserted after point (leaving mark after it).
1250 If REPLACE, the optional fifth argument, is non-nil, that means insert
1251 the output in place of text from START to END, putting point and mark
1254 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1255 or buffer name to which to direct the command's standard error output.
1256 If it is nil, error output is mingled with regular output.
1257 In an interactive call, the variable `shell-command-default-error-buffer'
1258 specifies the value of ERROR-BUFFER."
1259 (interactive (let ((string
1260 ;; Do this before calling region-beginning
1261 ;; and region-end, in case subprocess output
1262 ;; relocates them while we are in the minibuffer.
1263 (read-from-minibuffer "Shell command on region: "
1265 'shell-command-history)))
1266 ;; call-interactively recognizes region-beginning and
1267 ;; region-end specially, leaving them in the history.
1268 (list (region-beginning) (region-end)
1272 shell-command-default-error-buffer)))
1276 (expand-file-name "scor"
1277 (or small-temporary-file-directory
1278 temporary-file-directory)))
1283 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1284 ;; Replace specified region with output from command.
1285 (let ((swap (and replace (< start end))))
1286 ;; Don't muck with mark unless REPLACE says we should.
1288 (and replace (push-mark))
1290 (call-process-region start end shell-file-name t
1294 nil shell-command-switch command))
1295 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1296 (and shell-buffer (not (eq shell-buffer (current-buffer)))
1297 (kill-buffer shell-buffer)))
1298 ;; Don't muck with mark unless REPLACE says we should.
1299 (and replace swap (exchange-point-and-mark)))
1300 ;; No prefix argument: put the output in a temp buffer,
1301 ;; replacing its entire contents.
1302 (let ((buffer (get-buffer-create
1303 (or output-buffer "*Shell Command Output*")))
1306 (if (eq buffer (current-buffer))
1307 ;; If the input is the same buffer as the output,
1308 ;; delete everything but the specified region,
1309 ;; then replace that region with the output.
1310 (progn (setq buffer-read-only nil)
1311 (delete-region (max start end) (point-max))
1312 (delete-region (point-min) (min start end))
1314 (call-process-region (point-min) (point-max)
1319 nil shell-command-switch
1321 ;; Clear the output buffer, then run the command with
1325 (setq buffer-read-only nil)
1328 (call-process-region start end shell-file-name nil
1330 (list buffer error-file)
1332 nil shell-command-switch command)))
1333 (setq success (and exit-status (equal 0 exit-status)))
1334 ;; Report the amount of output.
1335 (let ((lines (save-excursion
1337 (if (= (buffer-size) 0)
1339 (count-lines (point-min) (point-max))))))
1342 (< 0 (nth 7 (file-attributes error-file))))
1343 (message "(Shell command %sed with some error output)"
1344 (if (equal 0 exit-status)
1347 (message "(Shell command %sed with no output)"
1348 (if (equal 0 exit-status)
1351 (kill-buffer buffer))
1356 (goto-char (point-min))
1357 (buffer-substring (point)
1358 (progn (end-of-line) (point))))))
1362 (goto-char (point-min)))
1363 (display-buffer buffer)))))))
1364 (when (and error-file (file-exists-p error-file))
1365 (if (< 0 (nth 7 (file-attributes error-file)))
1366 (with-current-buffer (get-buffer-create error-buffer)
1367 (let ((pos-from-end (- (point-max) (point))))
1370 ;; Do no formatting while reading error file,
1371 ;; because that can run a shell command, and we
1372 ;; don't want that to cause an infinite recursion.
1373 (format-insert-file error-file nil)
1374 ;; Put point after the inserted errors.
1375 (goto-char (- (point-max) pos-from-end)))
1376 (display-buffer (current-buffer))))
1377 (delete-file error-file))
1380 (defun shell-command-to-string (command)
1381 "Execute shell command COMMAND and return its output as a string."
1382 (with-output-to-string
1383 (with-current-buffer
1385 (call-process shell-file-name nil t nil shell-command-switch command))))
1387 (defvar universal-argument-map
1388 (let ((map (make-sparse-keymap)))
1389 (define-key map [t] 'universal-argument-other-key)
1390 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1391 (define-key map [switch-frame] nil)
1392 (define-key map [?\C-u] 'universal-argument-more)
1393 (define-key map [?-] 'universal-argument-minus)
1394 (define-key map [?0] 'digit-argument)
1395 (define-key map [?1] 'digit-argument)
1396 (define-key map [?2] 'digit-argument)
1397 (define-key map [?3] 'digit-argument)
1398 (define-key map [?4] 'digit-argument)
1399 (define-key map [?5] 'digit-argument)
1400 (define-key map [?6] 'digit-argument)
1401 (define-key map [?7] 'digit-argument)
1402 (define-key map [?8] 'digit-argument)
1403 (define-key map [?9] 'digit-argument)
1405 "Keymap used while processing \\[universal-argument].")
1407 (defvar universal-argument-num-events nil
1408 "Number of argument-specifying events read by `universal-argument'.
1409 `universal-argument-other-key' uses this to discard those events
1410 from (this-command-keys), and reread only the final command.")
1412 (defun universal-argument ()
1413 "Begin a numeric argument for the following command.
1414 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1415 \\[universal-argument] following the digits or minus sign ends the argument.
1416 \\[universal-argument] without digits or minus sign provides 4 as argument.
1417 Repeating \\[universal-argument] without digits or minus sign
1418 multiplies the argument by 4 each time.
1419 For some commands, just \\[universal-argument] by itself serves as a flag
1420 which is different in effect from any particular numeric argument.
1421 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1423 (setq prefix-arg (list 4))
1424 (setq universal-argument-num-events (length (this-command-keys)))
1425 (setq overriding-terminal-local-map universal-argument-map))
1427 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1428 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1429 (defun universal-argument-more (arg)
1432 (setq prefix-arg (list (* 4 (car arg))))
1434 (setq prefix-arg (list -4))
1435 (setq prefix-arg arg)
1436 (setq overriding-terminal-local-map nil)))
1437 (setq universal-argument-num-events (length (this-command-keys))))
1439 (defun negative-argument (arg)
1440 "Begin a negative numeric argument for the next command.
1441 \\[universal-argument] following digits or minus sign ends the argument."
1443 (cond ((integerp arg)
1444 (setq prefix-arg (- arg)))
1446 (setq prefix-arg nil))
1448 (setq prefix-arg '-)))
1449 (setq universal-argument-num-events (length (this-command-keys)))
1450 (setq overriding-terminal-local-map universal-argument-map))
1452 (defun digit-argument (arg)
1453 "Part of the numeric argument for the next command.
1454 \\[universal-argument] following digits or minus sign ends the argument."
1456 (let ((digit (- (logand last-command-char ?\177) ?0)))
1457 (cond ((integerp arg)
1458 (setq prefix-arg (+ (* arg 10)
1459 (if (< arg 0) (- digit) digit))))
1461 ;; Treat -0 as just -, so that -01 will work.
1462 (setq prefix-arg (if (zerop digit) '- (- digit))))
1464 (setq prefix-arg digit))))
1465 (setq universal-argument-num-events (length (this-command-keys)))
1466 (setq overriding-terminal-local-map universal-argument-map))
1468 ;; For backward compatibility, minus with no modifiers is an ordinary
1469 ;; command if digits have already been entered.
1470 (defun universal-argument-minus (arg)
1473 (universal-argument-other-key arg)
1474 (negative-argument arg)))
1476 ;; Anything else terminates the argument and is left in the queue to be
1477 ;; executed as a command.
1478 (defun universal-argument-other-key (arg)
1480 (setq prefix-arg arg)
1481 (let* ((key (this-command-keys))
1482 (keylist (listify-key-sequence key)))
1483 (setq unread-command-events
1484 (append (nthcdr universal-argument-num-events keylist)
1485 unread-command-events)))
1486 (reset-this-command-lengths)
1487 (setq overriding-terminal-local-map nil))
1489 ;;;; Window system cut and paste hooks.
1491 (defvar interprogram-cut-function nil
1492 "Function to call to make a killed region available to other programs.
1494 Most window systems provide some sort of facility for cutting and
1495 pasting text between the windows of different programs.
1496 This variable holds a function that Emacs calls whenever text
1497 is put in the kill ring, to make the new kill available to other
1500 The function takes one or two arguments.
1501 The first argument, TEXT, is a string containing
1502 the text which should be made available.
1503 The second, PUSH, if non-nil means this is a \"new\" kill;
1504 nil means appending to an \"old\" kill.")
1506 (defvar interprogram-paste-function nil
1507 "Function to call to get text cut from other programs.
1509 Most window systems provide some sort of facility for cutting and
1510 pasting text between the windows of different programs.
1511 This variable holds a function that Emacs calls to obtain
1512 text that other programs have provided for pasting.
1514 The function should be called with no arguments. If the function
1515 returns nil, then no other program has provided such text, and the top
1516 of the Emacs kill ring should be used. If the function returns a
1517 string, that string should be put in the kill ring as the latest kill.
1519 Note that the function should return a string only if a program other
1520 than Emacs has provided a string for pasting; if Emacs provided the
1521 most recent string, the function should return nil. If it is
1522 difficult to tell whether Emacs or some other program provided the
1523 current string, it is probably good enough to return nil if the string
1524 is equal (according to `string=') to the last text Emacs provided.")
1528 ;;;; The kill ring data structure.
1530 (defvar kill-ring nil
1531 "List of killed text sequences.
1532 Since the kill ring is supposed to interact nicely with cut-and-paste
1533 facilities offered by window systems, use of this variable should
1534 interact nicely with `interprogram-cut-function' and
1535 `interprogram-paste-function'. The functions `kill-new',
1536 `kill-append', and `current-kill' are supposed to implement this
1537 interaction; you may want to use them instead of manipulating the kill
1540 (defcustom kill-ring-max 60
1541 "*Maximum length of kill ring before oldest elements are thrown away."
1545 (defvar kill-ring-yank-pointer nil
1546 "The tail of the kill ring whose car is the last thing yanked.")
1548 (defun kill-new (string &optional replace)
1549 "Make STRING the latest kill in the kill ring.
1550 Set the kill-ring-yank pointer to point to it.
1551 If `interprogram-cut-function' is non-nil, apply it to STRING.
1552 Optional second argument REPLACE non-nil means that STRING will replace
1553 the front of the kill ring, rather than being added to the list."
1554 (and (fboundp 'menu-bar-update-yank-menu)
1555 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1557 (setcar kill-ring string)
1558 (setq kill-ring (cons string kill-ring))
1559 (if (> (length kill-ring) kill-ring-max)
1560 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1561 (setq kill-ring-yank-pointer kill-ring)
1562 (if interprogram-cut-function
1563 (funcall interprogram-cut-function string (not replace))))
1565 (defun kill-append (string before-p)
1566 "Append STRING to the end of the latest kill in the kill ring.
1567 If BEFORE-P is non-nil, prepend STRING to the kill.
1568 If `interprogram-cut-function' is set, pass the resulting kill to
1570 (kill-new (if before-p
1571 (concat string (car kill-ring))
1572 (concat (car kill-ring) string)) t))
1574 (defun current-kill (n &optional do-not-move)
1575 "Rotate the yanking point by N places, and then return that kill.
1576 If N is zero, `interprogram-paste-function' is set, and calling it
1577 returns a string, then that string is added to the front of the
1578 kill ring and returned as the latest kill.
1579 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1580 yanking point; just return the Nth kill forward."
1581 (let ((interprogram-paste (and (= n 0)
1582 interprogram-paste-function
1583 (funcall interprogram-paste-function))))
1584 (if interprogram-paste
1586 ;; Disable the interprogram cut function when we add the new
1587 ;; text to the kill ring, so Emacs doesn't try to own the
1588 ;; selection, with identical text.
1589 (let ((interprogram-cut-function nil))
1590 (kill-new interprogram-paste))
1592 (or kill-ring (error "Kill ring is empty"))
1593 (let ((ARGth-kill-element
1594 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1598 (setq kill-ring-yank-pointer ARGth-kill-element))
1599 (car ARGth-kill-element)))))
1603 ;;;; Commands for manipulating the kill ring.
1605 (defcustom kill-read-only-ok nil
1606 "*Non-nil means don't signal an error for killing read-only text."
1610 (put 'text-read-only 'error-conditions
1611 '(text-read-only buffer-read-only error))
1612 (put 'text-read-only 'error-message "Text is read-only")
1614 (defun kill-region (beg end)
1615 "Kill between point and mark.
1616 The text is deleted but saved in the kill ring.
1617 The command \\[yank] can retrieve it from there.
1618 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
1619 If the buffer is read-only, Emacs will beep and refrain from deleting
1620 the text, but put the text in the kill ring anyway. This means that
1621 you can use the killing commands to copy text from a read-only buffer.
1623 This is the primitive for programs to kill text (as opposed to deleting it).
1624 Supply two arguments, character numbers indicating the stretch of text
1626 Any command that calls this function is a \"kill command\".
1627 If the previous command was also a kill command,
1628 the text killed this time appends to the text killed last time
1629 to make one entry in the kill ring."
1632 ;; Don't let the undo list be truncated before we can even access it.
1633 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))
1634 (old-list buffer-undo-list)
1636 ;; If we can't rely on finding the killed text
1637 ;; in the undo list, save it now as a string.
1638 (string (if (or (eq buffer-undo-list t)
1640 (buffer-substring beg end))))
1641 (delete-region beg end)
1642 ;; Search back in buffer-undo-list for this string,
1643 ;; in case a change hook made property changes.
1644 (setq tail buffer-undo-list)
1646 (while (not (stringp (car (car tail))))
1647 (setq tail (cdr tail)))
1648 ;; If we did not already make the string to use,
1649 ;; use the same one that undo made for us.
1650 (setq string (car (car tail))))
1651 ;; Add that string to the kill ring, one way or another.
1652 (if (eq last-command 'kill-region)
1653 (kill-append string (< end beg))
1655 (setq this-command 'kill-region))
1656 ((buffer-read-only text-read-only)
1657 ;; The code above failed because the buffer, or some of the characters
1658 ;; in the region, are read-only.
1659 ;; We should beep, in case the user just isn't aware of this.
1660 ;; However, there's no harm in putting
1661 ;; the region's text in the kill ring, anyway.
1662 (copy-region-as-kill beg end)
1663 ;; Set this-command now, so it will be set even if we get an error.
1664 (setq this-command 'kill-region)
1665 ;; This should barf, if appropriate, and give us the correct error.
1666 (if kill-read-only-ok
1667 (message "Read only text copied to kill ring")
1668 ;; Signal an error if the buffer is read-only.
1669 (barf-if-buffer-read-only)
1670 ;; If the buffer isn't read-only, the text is.
1671 (signal 'text-read-only (list (current-buffer)))))))
1673 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1674 ;; to get two copies of the text when the user accidentally types M-w and
1675 ;; then corrects it with the intended C-w.
1676 (defun copy-region-as-kill (beg end)
1677 "Save the region as if killed, but don't kill it.
1678 In Transient Mark mode, deactivate the mark.
1679 If `interprogram-cut-function' is non-nil, also save the text for a window
1680 system cut and paste."
1682 (if (eq last-command 'kill-region)
1683 (kill-append (buffer-substring beg end) (< end beg))
1684 (kill-new (buffer-substring beg end)))
1685 (if transient-mark-mode
1686 (setq deactivate-mark t))
1689 (defun kill-ring-save (beg end)
1690 "Save the region as if killed, but don't kill it.
1691 In Transient Mark mode, deactivate the mark.
1692 If `interprogram-cut-function' is non-nil, also save the text for a window
1693 system cut and paste.
1695 This command is similar to `copy-region-as-kill', except that it gives
1696 visual feedback indicating the extent of the region being copied."
1698 (copy-region-as-kill beg end)
1700 (let ((other-end (if (= (point) beg) end beg))
1702 ;; Inhibit quitting so we can make a quit here
1703 ;; look like a C-g typed as a command.
1705 (if (pos-visible-in-window-p other-end (selected-window))
1707 ;; Swap point and mark.
1708 (set-marker (mark-marker) (point) (current-buffer))
1709 (goto-char other-end)
1712 (set-marker (mark-marker) other-end (current-buffer))
1714 ;; If user quit, deactivate the mark
1715 ;; as C-g would as a command.
1716 (and quit-flag mark-active
1718 (let* ((killed-text (current-kill 0))
1719 (message-len (min (length killed-text) 40)))
1721 ;; Don't say "killed"; that is misleading.
1722 (message "Saved text until \"%s\""
1723 (substring killed-text (- message-len)))
1724 (message "Saved text from \"%s\""
1725 (substring killed-text 0 message-len))))))))
1727 (defun append-next-kill (&optional interactive)
1728 "Cause following command, if it kills, to append to previous kill.
1729 The argument is used for internal purposes; do not supply one."
1731 ;; We don't use (interactive-p), since that breaks kbd macros.
1734 (setq this-command 'kill-region)
1735 (message "If the next command is a kill, it will append"))
1736 (setq last-command 'kill-region)))
1740 (defun yank-pop (arg)
1741 "Replace just-yanked stretch of killed text with a different stretch.
1742 This command is allowed only immediately after a `yank' or a `yank-pop'.
1743 At such a time, the region contains a stretch of reinserted
1744 previously-killed text. `yank-pop' deletes that text and inserts in its
1745 place a different stretch of killed text.
1747 With no argument, the previous kill is inserted.
1748 With argument N, insert the Nth previous kill.
1749 If N is negative, this is a more recent kill.
1751 The sequence of kills wraps around, so that after the oldest one
1752 comes the newest one."
1754 (if (not (eq last-command 'yank))
1755 (error "Previous command was not a yank"))
1756 (setq this-command 'yank)
1757 (let ((inhibit-read-only t)
1758 (before (< (point) (mark t))))
1759 (delete-region (point) (mark t))
1760 (set-marker (mark-marker) (point) (current-buffer))
1761 (let ((opoint (point)))
1762 (insert (current-kill arg))
1763 (let ((inhibit-read-only t))
1764 (remove-text-properties opoint (point) '(read-only nil))))
1766 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1767 ;; It is cleaner to avoid activation, even though the command
1768 ;; loop would deactivate the mark because we inserted text.
1769 (goto-char (prog1 (mark t)
1770 (set-marker (mark-marker) (point) (current-buffer))))))
1773 (defun yank (&optional arg)
1774 "Reinsert the last stretch of killed text.
1775 More precisely, reinsert the stretch of killed text most recently
1776 killed OR yanked. Put point at end, and set mark at beginning.
1777 With just C-u as argument, same but put point at beginning (and mark at end).
1778 With argument N, reinsert the Nth most recently killed stretch of killed
1780 See also the command \\[yank-pop]."
1782 ;; If we don't get all the way thru, make last-command indicate that
1783 ;; for the following command.
1784 (setq this-command t)
1786 (let ((opoint (point)))
1787 (insert (current-kill (cond
1791 (let ((inhibit-read-only t))
1792 (remove-text-properties opoint (point) '(read-only nil))))
1794 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1795 ;; It is cleaner to avoid activation, even though the command
1796 ;; loop would deactivate the mark because we inserted text.
1797 (goto-char (prog1 (mark t)
1798 (set-marker (mark-marker) (point) (current-buffer)))))
1799 ;; If we do get all the way thru, make this-command indicate that.
1800 (setq this-command 'yank)
1803 (defun rotate-yank-pointer (arg)
1804 "Rotate the yanking point in the kill ring.
1805 With argument, rotate that many kills forward (or backward, if negative)."
1809 ;; Some kill commands.
1811 ;; Internal subroutine of delete-char
1812 (defun kill-forward-chars (arg)
1813 (if (listp arg) (setq arg (car arg)))
1814 (if (eq arg '-) (setq arg -1))
1815 (kill-region (point) (forward-point arg)))
1817 ;; Internal subroutine of backward-delete-char
1818 (defun kill-backward-chars (arg)
1819 (if (listp arg) (setq arg (car arg)))
1820 (if (eq arg '-) (setq arg -1))
1821 (kill-region (point) (forward-point (- arg))))
1823 (defcustom backward-delete-char-untabify-method 'untabify
1824 "*The method for untabifying when deleting backward.
1825 Can be `untabify' -- turn a tab to many spaces, then delete one space.
1826 `hungry' -- delete all whitespace, both tabs and spaces.
1827 nil -- just delete one character."
1828 :type '(choice (const untabify) (const hungry) (const nil))
1831 (defun backward-delete-char-untabify (arg &optional killp)
1832 "Delete characters backward, changing tabs into spaces.
1833 The exact behavior depends on `backward-delete-char-untabify-method'.
1834 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
1835 Interactively, ARG is the prefix arg (default 1)
1836 and KILLP is t if a prefix arg was specified."
1837 (interactive "*p\nP")
1838 (when (eq backward-delete-char-untabify-method 'untabify)
1841 (while (and (> count 0) (not (bobp)))
1842 (if (= (preceding-char) ?\t)
1843 (let ((col (current-column)))
1845 (setq col (- col (current-column)))
1846 (insert-char ?\ col)
1849 (setq count (1- count))))))
1850 (delete-backward-char
1851 (if (eq backward-delete-char-untabify-method 'hungry)
1852 (let ((wh (- (point) (save-excursion (skip-chars-backward " \t")
1854 (+ arg (if (zerop wh) 0 (1- wh))))
1858 (defun zap-to-char (arg char)
1859 "Kill up to and including ARG'th occurrence of CHAR.
1860 Case is ignored if `case-fold-search' is non-nil in the current buffer.
1861 Goes backward if ARG is negative; error if CHAR not found."
1862 (interactive "*p\ncZap to char: ")
1863 (kill-region (point) (progn
1864 (search-forward (char-to-string char) nil nil arg)
1865 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
1868 ;; kill-line and its subroutines.
1870 (defcustom kill-whole-line nil
1871 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
1875 (defun kill-line (&optional arg)
1876 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
1877 With prefix argument, kill that many lines from point.
1878 Negative arguments kill lines backward.
1880 When calling from a program, nil means \"no arg\",
1881 a number counts as a prefix arg.
1883 To kill a whole line, when point is not at the beginning, type \
1884 \\[beginning-of-line] \\[kill-line] \\[kill-line].
1886 If `kill-whole-line' is non-nil, then this command kills the whole line
1887 including its terminating newline, when used at the beginning of a line
1888 with no argument. As a consequence, you can always kill a whole line
1889 by typing \\[beginning-of-line] \\[kill-line]."
1891 (kill-region (point)
1892 ;; It is better to move point to the other end of the kill
1893 ;; before killing. That way, in a read-only buffer, point
1894 ;; moves across the text that is copied to the kill ring.
1895 ;; The choice has no effect on undo now that undo records
1896 ;; the value of point from before the command was run.
1899 (forward-visible-line (prefix-numeric-value arg))
1901 (signal 'end-of-buffer nil))
1902 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
1903 (forward-visible-line 1)
1904 (end-of-visible-line)))
1907 (defun forward-visible-line (arg)
1908 "Move forward by ARG lines, ignoring currently invisible newlines only.
1909 If ARG is negative, move backward -ARG lines.
1910 If ARG is zero, move to the beginning of the current line."
1914 (or (zerop (forward-line 1))
1915 (signal 'end-of-buffer nil))
1916 ;; If the following character is currently invisible,
1917 ;; skip all characters with that same `invisible' property value,
1918 ;; then find the next newline.
1919 (while (and (not (eobp))
1921 (get-char-property (point) 'invisible)))
1922 (if (eq buffer-invisibility-spec t)
1924 (or (memq prop buffer-invisibility-spec)
1925 (assq prop buffer-invisibility-spec)))))
1927 (if (get-text-property (point) 'invisible)
1928 (or (next-single-property-change (point) 'invisible)
1930 (next-overlay-change (point))))
1931 (or (zerop (forward-line 1))
1932 (signal 'end-of-buffer nil)))
1933 (setq arg (1- arg)))
1935 (while (or first (< arg 0))
1938 (or (zerop (forward-line -1))
1939 (signal 'beginning-of-buffer nil)))
1940 (while (and (not (bobp))
1942 (get-char-property (1- (point)) 'invisible)))
1943 (if (eq buffer-invisibility-spec t)
1945 (or (memq prop buffer-invisibility-spec)
1946 (assq prop buffer-invisibility-spec)))))
1948 (if (get-text-property (1- (point)) 'invisible)
1949 (or (previous-single-property-change (point) 'invisible)
1951 (previous-overlay-change (point))))
1952 (or (zerop (forward-line -1))
1953 (signal 'beginning-of-buffer nil)))
1955 (setq arg (1+ arg)))))
1956 ((beginning-of-buffer end-of-buffer)
1959 (defun end-of-visible-line ()
1960 "Move to end of current visible line."
1962 ;; If the following character is currently invisible,
1963 ;; skip all characters with that same `invisible' property value,
1964 ;; then find the next newline.
1965 (while (and (not (eobp))
1967 (get-char-property (point) 'invisible)))
1968 (if (eq buffer-invisibility-spec t)
1970 (or (memq prop buffer-invisibility-spec)
1971 (assq prop buffer-invisibility-spec)))))
1972 (if (get-text-property (point) 'invisible)
1973 (goto-char (next-single-property-change (point) 'invisible))
1974 (goto-char (next-overlay-change (point))))
1977 (defun insert-buffer (buffer)
1978 "Insert after point the contents of BUFFER.
1979 Puts mark after the inserted text.
1980 BUFFER may be a buffer or a buffer name."
1984 (barf-if-buffer-read-only)
1985 (read-buffer "Insert buffer: "
1986 (if (eq (selected-window) (next-window (selected-window)))
1987 (other-buffer (current-buffer))
1988 (window-buffer (next-window (selected-window))))
1990 (or (bufferp buffer)
1991 (setq buffer (get-buffer buffer)))
1992 (let (start end newmark)
1996 (setq start (point-min) end (point-max)))
1997 (insert-buffer-substring buffer start end)
1998 (setq newmark (point)))
1999 (push-mark newmark))
2002 (defun append-to-buffer (buffer start end)
2003 "Append to specified buffer the text of the region.
2004 It is inserted into that buffer before its point.
2006 When calling from a program, give three arguments:
2007 BUFFER (or buffer name), START and END.
2008 START and END specify the portion of the current buffer to be copied."
2010 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2011 (region-beginning) (region-end)))
2012 (let ((oldbuf (current-buffer)))
2014 (set-buffer (get-buffer-create buffer))
2015 (barf-if-buffer-read-only)
2016 (insert-buffer-substring oldbuf start end))))
2018 (defun prepend-to-buffer (buffer start end)
2019 "Prepend to specified buffer the text of the region.
2020 It is inserted into that buffer after its point.
2022 When calling from a program, give three arguments:
2023 BUFFER (or buffer name), START and END.
2024 START and END specify the portion of the current buffer to be copied."
2025 (interactive "BPrepend to buffer: \nr")
2026 (let ((oldbuf (current-buffer)))
2028 (set-buffer (get-buffer-create buffer))
2029 (barf-if-buffer-read-only)
2031 (insert-buffer-substring oldbuf start end)))))
2033 (defun copy-to-buffer (buffer start end)
2034 "Copy to specified buffer the text of the region.
2035 It is inserted into that buffer, replacing existing text there.
2037 When calling from a program, give three arguments:
2038 BUFFER (or buffer name), START and END.
2039 START and END specify the portion of the current buffer to be copied."
2040 (interactive "BCopy to buffer: \nr")
2041 (let ((oldbuf (current-buffer)))
2043 (set-buffer (get-buffer-create buffer))
2044 (barf-if-buffer-read-only)
2047 (insert-buffer-substring oldbuf start end)))))
2049 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2050 (put 'mark-inactive 'error-message "The mark is not active now")
2052 (defun mark (&optional force)
2053 "Return this buffer's mark value as integer; error if mark inactive.
2054 If optional argument FORCE is non-nil, access the mark value
2055 even if the mark is not currently active, and return nil
2056 if there is no mark at all.
2058 If you are using this in an editing command, you are most likely making
2059 a mistake; see the documentation of `set-mark'."
2060 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2061 (marker-position (mark-marker))
2062 (signal 'mark-inactive nil)))
2064 ;; Many places set mark-active directly, and several of them failed to also
2065 ;; run deactivate-mark-hook. This shorthand should simplify.
2066 (defsubst deactivate-mark ()
2067 "Deactivate the mark by setting `mark-active' to nil.
2068 \(That makes a difference only in Transient Mark mode.)
2069 Also runs the hook `deactivate-mark-hook'."
2070 (if transient-mark-mode
2072 (setq mark-active nil)
2073 (run-hooks 'deactivate-mark-hook))))
2075 (defun set-mark (pos)
2076 "Set this buffer's mark to POS. Don't use this function!
2077 That is to say, don't use this function unless you want
2078 the user to see that the mark has moved, and you want the previous
2079 mark position to be lost.
2081 Normally, when a new mark is set, the old one should go on the stack.
2082 This is why most applications should use push-mark, not set-mark.
2084 Novice Emacs Lisp programmers often try to use the mark for the wrong
2085 purposes. The mark saves a location for the user's convenience.
2086 Most editing commands should not alter the mark.
2087 To remember a location for internal use in the Lisp program,
2088 store it in a Lisp variable. Example:
2090 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2094 (setq mark-active t)
2095 (run-hooks 'activate-mark-hook)
2096 (set-marker (mark-marker) pos (current-buffer)))
2097 ;; Normally we never clear mark-active except in Transient Mark mode.
2098 ;; But when we actually clear out the mark value too,
2099 ;; we must clear mark-active in any mode.
2100 (setq mark-active nil)
2101 (run-hooks 'deactivate-mark-hook)
2102 (set-marker (mark-marker) nil)))
2104 (defvar mark-ring nil
2105 "The list of former marks of the current buffer, most recent first.")
2106 (make-variable-buffer-local 'mark-ring)
2107 (put 'mark-ring 'permanent-local t)
2109 (defcustom mark-ring-max 16
2110 "*Maximum size of mark ring. Start discarding off end if gets this big."
2112 :group 'editing-basics)
2114 (defvar global-mark-ring nil
2115 "The list of saved global marks, most recent first.")
2117 (defcustom global-mark-ring-max 16
2118 "*Maximum size of global mark ring. \
2119 Start discarding off end if gets this big."
2121 :group 'editing-basics)
2123 (defun set-mark-command (arg)
2124 "Set mark at where point is, or jump to mark.
2125 With no prefix argument, set mark, push old mark position on local mark
2126 ring, and push mark on global mark ring.
2127 With argument, jump to mark, and pop a new position for mark off the ring
2128 \(does not affect global mark ring\).
2130 Novice Emacs Lisp programmers often try to use the mark for the wrong
2131 purposes. See the documentation of `set-mark' for more information."
2135 (push-mark nil nil t))
2137 (error "No mark set in this buffer")
2138 (goto-char (mark t))
2141 (defun push-mark (&optional location nomsg activate)
2142 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2143 If the last global mark pushed was not in the current buffer,
2144 also push LOCATION on the global mark ring.
2145 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2146 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2148 Novice Emacs Lisp programmers often try to use the mark for the wrong
2149 purposes. See the documentation of `set-mark' for more information.
2151 In Transient Mark mode, this does not activate the mark."
2154 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2155 (if (> (length mark-ring) mark-ring-max)
2157 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2158 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
2159 (set-marker (mark-marker) (or location (point)) (current-buffer))
2160 ;; Now push the mark on the global mark ring.
2161 (if (and global-mark-ring
2162 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2163 ;; The last global mark pushed was in this same buffer.
2164 ;; Don't push another one.
2166 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2167 (if (> (length global-mark-ring) global-mark-ring-max)
2169 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
2171 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2172 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2173 (message "Mark set"))
2174 (if (or activate (not transient-mark-mode))
2175 (set-mark (mark t)))
2179 "Pop off mark ring into the buffer's actual mark.
2180 Does not set point. Does nothing if mark ring is empty."
2183 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2184 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2186 (move-marker (car mark-ring) nil)
2187 (if (null (mark t)) (ding))
2188 (setq mark-ring (cdr mark-ring)))))
2190 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2191 (defun exchange-point-and-mark ()
2192 "Put the mark where point is now, and point where the mark is now.
2193 This command works even when the mark is not active,
2194 and it reactivates the mark."
2196 (let ((omark (mark t)))
2198 (error "No mark set in this buffer"))
2203 (defun transient-mark-mode (arg)
2204 "Toggle Transient Mark mode.
2205 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2207 In Transient Mark mode, when the mark is active, the region is highlighted.
2208 Changing the buffer \"deactivates\" the mark.
2209 So do certain other operations that set the mark
2210 but whose main purpose is something else--for example,
2211 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
2213 (setq transient-mark-mode
2215 (not transient-mark-mode)
2216 (> (prefix-numeric-value arg) 0)))
2218 (if transient-mark-mode
2219 (message "Transient Mark mode enabled")
2220 (message "Transient Mark mode disabled"))))
2222 (defun pop-global-mark ()
2223 "Pop off global mark ring and jump to the top location."
2225 ;; Pop entries which refer to non-existent buffers.
2226 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2227 (setq global-mark-ring (cdr global-mark-ring)))
2228 (or global-mark-ring
2229 (error "No global mark set"))
2230 (let* ((marker (car global-mark-ring))
2231 (buffer (marker-buffer marker))
2232 (position (marker-position marker)))
2233 (setq global-mark-ring (nconc (cdr global-mark-ring)
2234 (list (car global-mark-ring))))
2236 (or (and (>= position (point-min))
2237 (<= position (point-max)))
2239 (goto-char position)
2240 (switch-to-buffer buffer)))
2242 (defcustom next-line-add-newlines t
2243 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2245 :group 'editing-basics)
2247 (defun next-line (arg)
2248 "Move cursor vertically down ARG lines.
2249 If there is no character in the target line exactly under the current column,
2250 the cursor is positioned after the character in that line which spans this
2251 column, or at the end of the line if it is not long enough.
2252 If there is no line in the buffer after this one, behavior depends on the
2253 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2254 to create a line, and moves the cursor to that line. Otherwise it moves the
2255 cursor to the end of the buffer.
2257 The command \\[set-goal-column] can be used to create
2258 a semipermanent goal column for this command.
2259 Then instead of trying to move exactly vertically (or as close as possible),
2260 this command moves to the specified goal column (or as close as possible).
2261 The goal column is stored in the variable `goal-column', which is nil
2262 when there is no goal column.
2264 If you are thinking of using this in a Lisp program, consider
2265 using `forward-line' instead. It is usually easier to use
2266 and more reliable (no dependence on goal column, etc.)."
2268 (if (and next-line-add-newlines (= arg 1))
2269 (let ((opoint (point)))
2278 ((beginning-of-buffer end-of-buffer) (ding)))
2282 (defun previous-line (arg)
2283 "Move cursor vertically up ARG lines.
2284 If there is no character in the target line exactly over the current column,
2285 the cursor is positioned after the character in that line which spans this
2286 column, or at the end of the line if it is not long enough.
2288 The command \\[set-goal-column] can be used to create
2289 a semipermanent goal column for this command.
2290 Then instead of trying to move exactly vertically (or as close as possible),
2291 this command moves to the specified goal column (or as close as possible).
2292 The goal column is stored in the variable `goal-column', which is nil
2293 when there is no goal column.
2295 If you are thinking of using this in a Lisp program, consider using
2296 `forward-line' with a negative argument instead. It is usually easier
2297 to use and more reliable (no dependence on goal column, etc.)."
2302 ((beginning-of-buffer end-of-buffer) (ding)))
2303 (line-move (- arg)))
2306 (defcustom track-eol nil
2307 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2308 This means moving to the end of each line moved onto.
2309 The beginning of a blank line does not count as the end of a line."
2311 :group 'editing-basics)
2313 (defcustom goal-column nil
2314 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2315 :type '(choice integer
2316 (const :tag "None" nil))
2317 :group 'editing-basics)
2318 (make-variable-buffer-local 'goal-column)
2320 (defvar temporary-goal-column 0
2321 "Current goal column for vertical motion.
2322 It is the column where point was
2323 at the start of current run of vertical motion commands.
2324 When the `track-eol' feature is doing its job, the value is 9999.")
2326 (defcustom line-move-ignore-invisible nil
2327 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2328 Outline mode sets this."
2330 :group 'editing-basics)
2332 ;; This is the guts of next-line and previous-line.
2333 ;; Arg says how many lines to move.
2334 (defun line-move (arg)
2335 ;; Don't run any point-motion hooks, and disregard intangibility,
2336 ;; for intermediate positions.
2337 (let ((inhibit-point-motion-hooks t)
2339 new line-end line-beg)
2342 (if (not (or (eq last-command 'next-line)
2343 (eq last-command 'previous-line)))
2344 (setq temporary-goal-column
2345 (if (and track-eol (eolp)
2346 ;; Don't count beg of empty line as end of line
2347 ;; unless we just did explicit end-of-line.
2348 (or (not (bolp)) (eq last-command 'end-of-line)))
2351 (if (and (not (integerp selective-display))
2352 (not line-move-ignore-invisible))
2353 ;; Use just newline characters.
2355 (progn (if (> arg 1) (forward-line (1- arg)))
2356 ;; This way of moving forward ARG lines
2357 ;; verifies that we have a newline after the last one.
2358 ;; It doesn't get confused by intangible text.
2360 (zerop (forward-line 1)))
2361 (and (zerop (forward-line arg))
2363 (signal (if (< arg 0)
2364 'beginning-of-buffer
2367 ;; Move by arg lines, but ignore invisible ones.
2370 (and (zerop (vertical-motion 1))
2371 (signal 'end-of-buffer nil))
2372 ;; If the following character is currently invisible,
2373 ;; skip all characters with that same `invisible' property value.
2374 (while (and (not (eobp))
2376 (get-char-property (point) 'invisible)))
2377 (if (eq buffer-invisibility-spec t)
2379 (or (memq prop buffer-invisibility-spec)
2380 (assq prop buffer-invisibility-spec)))))
2381 (if (get-text-property (point) 'invisible)
2382 (goto-char (next-single-property-change (point) 'invisible))
2383 (goto-char (next-overlay-change (point)))))
2384 (setq arg (1- arg)))
2387 (and (zerop (vertical-motion -1))
2388 (signal 'beginning-of-buffer nil))
2389 (while (and (not (bobp))
2391 (get-char-property (1- (point)) 'invisible)))
2392 (if (eq buffer-invisibility-spec t)
2394 (or (memq prop buffer-invisibility-spec)
2395 (assq prop buffer-invisibility-spec)))))
2396 (if (get-text-property (1- (point)) 'invisible)
2397 (goto-char (previous-single-property-change (point) 'invisible))
2398 (goto-char (previous-overlay-change (point)))))
2399 (setq arg (1+ arg))))
2400 (let ((buffer-invisibility-spec nil))
2401 (move-to-column (or goal-column temporary-goal-column))))
2403 ;; If we are moving into some intangible text,
2404 ;; look for following text on the same line which isn't intangible
2406 (setq line-end (save-excursion (end-of-line) (point)))
2407 (setq line-beg (save-excursion (beginning-of-line) (point)))
2408 (let ((after (and (< new (point-max))
2409 (get-char-property new 'intangible)))
2410 (before (and (> new (point-min))
2411 (get-char-property (1- new) 'intangible))))
2412 (when (and before (eq before after)
2414 (goto-char (point-min))
2415 (let ((inhibit-point-motion-hooks nil))
2417 (if (<= new line-end)
2418 (setq new (point)))))
2419 ;; NEW is where we want to move to.
2420 ;; LINE-BEG and LINE-END are the beginning and end of the line.
2421 ;; Move there in just one step, from our starting position,
2422 ;; with intangibility and point-motion hooks enabled this time.
2424 (setq inhibit-point-motion-hooks nil)
2425 (goto-char (constrain-to-field new opoint t t))
2426 ;; If intangibility processing moved us to a different line,
2427 ;; readjust the horizontal position within the line we ended up at.
2428 (when (or (< (point) line-beg) (> (point) line-end))
2430 (setq inhibit-point-motion-hooks t)
2431 (setq line-end (save-excursion (end-of-line) (point)))
2433 (setq line-beg (point))
2434 (let ((buffer-invisibility-spec nil))
2435 (move-to-column (or goal-column temporary-goal-column)))
2436 (if (<= (point) line-end)
2438 (goto-char (point-min))
2439 (setq inhibit-point-motion-hooks nil)
2440 (goto-char (constrain-to-field new opoint t t))
2444 ;;; Many people have said they rarely use this feature, and often type
2445 ;;; it by accident. Maybe it shouldn't even be on a key.
2446 (put 'set-goal-column 'disabled t)
2448 (defun set-goal-column (arg)
2449 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2450 Those commands will move to this position in the line moved to
2451 rather than trying to keep the same horizontal position.
2452 With a non-nil argument, clears out the goal column
2453 so that \\[next-line] and \\[previous-line] resume vertical motion.
2454 The goal column is stored in the variable `goal-column'."
2458 (setq goal-column nil)
2459 (message "No goal column"))
2460 (setq goal-column (current-column))
2461 (message (substitute-command-keys
2462 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2467 (defun scroll-other-window-down (lines)
2468 "Scroll the \"other window\" down.
2469 For more details, see the documentation for `scroll-other-window'."
2471 (scroll-other-window
2472 ;; Just invert the argument's meaning.
2473 ;; We can do that without knowing which window it will be.
2474 (if (eq lines '-) nil
2476 (- (prefix-numeric-value lines))))))
2477 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2479 (defun beginning-of-buffer-other-window (arg)
2480 "Move point to the beginning of the buffer in the other window.
2481 Leave mark at previous position.
2482 With arg N, put point N/10 of the way from the true beginning."
2484 (let ((orig-window (selected-window))
2485 (window (other-window-for-scrolling)))
2486 ;; We use unwind-protect rather than save-window-excursion
2487 ;; because the latter would preserve the things we want to change.
2490 (select-window window)
2491 ;; Set point and mark in that window's buffer.
2492 (beginning-of-buffer arg)
2493 ;; Set point accordingly.
2495 (select-window orig-window))))
2497 (defun end-of-buffer-other-window (arg)
2498 "Move point to the end of the buffer in the other window.
2499 Leave mark at previous position.
2500 With arg N, put point N/10 of the way from the true end."
2502 ;; See beginning-of-buffer-other-window for comments.
2503 (let ((orig-window (selected-window))
2504 (window (other-window-for-scrolling)))
2507 (select-window window)
2510 (select-window orig-window))))
2512 (defun transpose-chars (arg)
2513 "Interchange characters around point, moving forward one character.
2514 With prefix arg ARG, effect is to take character before point
2515 and drag it forward past ARG other characters (backward if ARG negative).
2516 If no argument and at end of line, the previous two chars are exchanged."
2518 (and (null arg) (eolp) (forward-char -1))
2519 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2521 (defun transpose-words (arg)
2522 "Interchange words around point, leaving point at end of them.
2523 With prefix arg ARG, effect is to take word before or around point
2524 and drag it forward past ARG other words (backward if ARG negative).
2525 If ARG is zero, the words around or after point and around or after mark
2528 (transpose-subr 'forward-word arg))
2530 (defun transpose-sexps (arg)
2531 "Like \\[transpose-words] but applies to sexps.
2532 Does not work on a sexp that point is in the middle of
2533 if it is a list or string."
2535 (transpose-subr 'forward-sexp arg))
2537 (defun transpose-lines (arg)
2538 "Exchange current line and previous line, leaving point after both.
2539 With argument ARG, takes previous line and moves it past ARG lines.
2540 With argument 0, interchanges line point is in with line mark is in."
2542 (transpose-subr (function
2546 ;; Move forward over ARG lines,
2547 ;; but create newlines if necessary.
2548 (setq arg (forward-line arg))
2549 (if (/= (preceding-char) ?\n)
2550 (setq arg (1+ arg)))
2553 (forward-line arg))))
2556 (defvar transpose-subr-start1)
2557 (defvar transpose-subr-start2)
2558 (defvar transpose-subr-end1)
2559 (defvar transpose-subr-end2)
2561 (defun transpose-subr (mover arg)
2562 (let (transpose-subr-start1
2564 transpose-subr-start2
2565 transpose-subr-end2)
2570 (setq transpose-subr-end2 (point))
2572 (setq transpose-subr-start2 (point))
2575 (setq transpose-subr-end1 (point))
2577 (setq transpose-subr-start1 (point))
2579 (exchange-point-and-mark))
2583 (setq transpose-subr-start1 (point))
2585 (setq transpose-subr-end1 (point))
2587 (setq transpose-subr-end2 (point))
2588 (funcall mover (- arg))
2589 (setq transpose-subr-start2 (point))
2591 (goto-char transpose-subr-end2))
2593 (setq transpose-subr-start2 (point))
2595 (setq transpose-subr-end2 (point))
2596 (funcall mover (1- arg))
2597 (setq transpose-subr-start1 (point))
2598 (funcall mover (- arg))
2599 (setq transpose-subr-end1 (point))
2600 (transpose-subr-1)))))
2602 (defun transpose-subr-1 ()
2603 (if (> (min transpose-subr-end1 transpose-subr-end2)
2604 (max transpose-subr-start1 transpose-subr-start2))
2605 (error "Don't have two things to transpose"))
2606 (let* ((word1 (buffer-substring transpose-subr-start1 transpose-subr-end1))
2607 (len1 (length word1))
2608 (word2 (buffer-substring transpose-subr-start2 transpose-subr-end2))
2609 (len2 (length word2)))
2610 (delete-region transpose-subr-start2 transpose-subr-end2)
2611 (goto-char transpose-subr-start2)
2613 (goto-char (if (< transpose-subr-start1 transpose-subr-start2)
2614 transpose-subr-start1
2615 (+ transpose-subr-start1 (- len1 len2))))
2616 (delete-region (point) (+ (point) len1))
2619 (defcustom comment-column 32
2620 "*Column to indent right-margin comments to.
2621 Setting this variable automatically makes it local to the current buffer.
2622 Each mode establishes a different default value for this variable; you
2623 can set the value for a particular mode using that mode's hook."
2625 :group 'fill-comments)
2626 (make-variable-buffer-local 'comment-column)
2628 (defcustom comment-start nil
2629 "*String to insert to start a new comment, or nil if no comment syntax."
2630 :type '(choice (const :tag "None" nil)
2632 :group 'fill-comments)
2634 (defcustom comment-start-skip nil
2635 "*Regexp to match the start of a comment plus everything up to its body.
2636 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
2637 at the place matched by the close of the first pair."
2638 :type '(choice (const :tag "None" nil)
2640 :group 'fill-comments)
2642 (defcustom comment-end ""
2643 "*String to insert to end a new comment.
2644 Should be an empty string if comments are terminated by end-of-line."
2646 :group 'fill-comments)
2648 (defvar comment-indent-hook nil
2649 "Obsolete variable for function to compute desired indentation for a comment.
2650 This function is called with no args with point at the beginning of
2651 the comment's starting delimiter.")
2653 (defvar comment-indent-function
2654 '(lambda () comment-column)
2655 "Function to compute desired indentation for a comment.
2656 This function is called with no args with point at the beginning of
2657 the comment's starting delimiter.")
2659 (defcustom block-comment-start nil
2660 "*String to insert to start a new comment on a line by itself.
2661 If nil, use `comment-start' instead.
2662 Note that the regular expression `comment-start-skip' should skip this string
2663 as well as the `comment-start' string."
2664 :type '(choice (const :tag "Use comment-start" nil)
2666 :group 'fill-comments)
2668 (defcustom block-comment-end nil
2669 "*String to insert to end a new comment on a line by itself.
2670 Should be an empty string if comments are terminated by end-of-line.
2671 If nil, use `comment-end' instead."
2672 :type '(choice (const :tag "Use comment-end" nil)
2674 :group 'fill-comments)
2676 (defun indent-for-comment ()
2677 "Indent this line's comment to comment column, or insert an empty comment."
2679 (let* ((empty (save-excursion (beginning-of-line)
2680 (looking-at "[ \t]*$")))
2681 (starter (or (and empty block-comment-start) comment-start))
2682 (ender (or (and empty block-comment-end) comment-end)))
2685 (error "No comment syntax defined"))
2686 ((null comment-start-skip)
2687 (error "This mode doesn't define `comment-start-skip'"))
2688 (t (let* ((eolpos (save-excursion (end-of-line) (point)))
2691 (if (re-search-forward comment-start-skip eolpos 'move)
2692 (progn (setq cpos (point-marker))
2693 ;; Find the start of the comment delimiter.
2694 ;; If there were paren-pairs in comment-start-skip,
2695 ;; position at the end of the first pair.
2697 (goto-char (match-end 1))
2698 ;; If comment-start-skip matched a string with
2699 ;; internal whitespace (not final whitespace) then
2700 ;; the delimiter start at the end of that
2701 ;; whitespace. Otherwise, it starts at the
2702 ;; beginning of what was matched.
2703 (skip-syntax-backward " " (match-beginning 0))
2704 (skip-syntax-backward "^ " (match-beginning 0)))))
2705 (setq begpos (point))
2706 ;; Compute desired indent.
2707 (if (= (current-column)
2708 (setq indent (if comment-indent-hook
2709 (funcall comment-indent-hook)
2710 (funcall comment-indent-function))))
2712 ;; If that's different from current, change it.
2713 (skip-chars-backward " \t")
2714 (delete-region (point) begpos)
2716 ;; An existing comment?
2718 (progn (goto-char cpos)
2719 (set-marker cpos nil))
2723 (insert ender))))))))
2725 (defun set-comment-column (arg)
2726 "Set the comment column based on point.
2727 With no arg, set the comment column to the current column.
2728 With just minus as arg, kill any comment on this line.
2729 With any other arg, set comment column to indentation of the previous comment
2730 and then align or create a comment on this line at that column."
2738 (re-search-backward comment-start-skip)
2740 (re-search-forward comment-start-skip)
2741 (goto-char (match-beginning 0))
2742 (setq comment-column (current-column))
2743 (message "Comment column set to %d" comment-column))
2744 (indent-for-comment))
2745 (setq comment-column (current-column))
2746 (message "Comment column set to %d" comment-column))))
2748 (defun kill-comment (arg)
2749 "Kill the comment on this line, if any.
2750 With argument, kill comments on that many lines starting with this one."
2751 ;; this function loses in a lot of situations. it incorrectly recognises
2752 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
2753 ;; with multi-line comments, can kill extra whitespace if comment wasn't
2754 ;; through end-of-line, et cetera.
2756 (or comment-start-skip (error "No comment syntax defined"))
2757 (let ((count (prefix-numeric-value arg)) endc)
2763 (and (string< "" comment-end)
2766 (re-search-forward (regexp-quote comment-end) endc 'move)
2767 (skip-chars-forward " \t")
2770 (if (re-search-forward comment-start-skip endc t)
2772 (goto-char (match-beginning 0))
2773 (skip-chars-backward " \t")
2774 (kill-region (point) endc)
2775 ;; to catch comments a line beginnings
2776 (indent-according-to-mode))))
2777 (if arg (forward-line 1))
2778 (setq count (1- count)))))
2780 (defvar comment-padding 1
2781 "Number of spaces `comment-region' puts between comment chars and text.
2783 Extra spacing between the comment characters and the comment text
2784 makes the comment easier to read. Default is 1. Nil means 0 and is
2787 (defun comment-region (beg end &optional arg)
2788 "Comment or uncomment each line in the region.
2789 With just C-u prefix arg, uncomment each line in region.
2790 Numeric prefix arg ARG means use ARG comment characters.
2791 If ARG is negative, delete that many comment characters instead.
2792 Comments are terminated on each line, even for syntax in which newline does
2793 not end the comment. Blank lines do not get comments."
2794 ;; if someone wants it to only put a comment-start at the beginning and
2795 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
2796 ;; is easy enough. No option is made here for other than commenting
2798 (interactive "*r\nP")
2799 (or comment-start (error "No comment syntax is defined"))
2800 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2803 (let ((cs comment-start) (ce comment-end)
2804 (cp (when comment-padding
2805 (make-string comment-padding ? )))
2807 (if (consp arg) (setq numarg t)
2808 (setq numarg (prefix-numeric-value arg))
2809 ;; For positive arg > 1, replicate the comment delims now,
2810 ;; then insert the replicated strings just once.
2812 (setq cs (concat cs comment-start)
2813 ce (concat ce comment-end))
2814 (setq numarg (1- numarg))))
2815 ;; Loop over all lines from BEG to END.
2816 (narrow-to-region beg end)
2818 (if (or (eq numarg t) (< numarg 0))
2820 (let (found-comment)
2821 ;; Delete comment start from beginning of line.
2823 (while (looking-at (regexp-quote cs))
2824 (setq found-comment t)
2825 (delete-char (length cs)))
2826 (let ((count numarg))
2827 (while (and (> 1 (setq count (1+ count)))
2828 (looking-at (regexp-quote cs)))
2829 (setq found-comment t)
2830 (delete-char (length cs)))))
2831 ;; Delete comment padding from beginning of line
2832 (when (and found-comment comment-padding
2833 (looking-at (regexp-quote cp)))
2834 (delete-char comment-padding))
2835 ;; Delete comment end from end of line.
2841 ;; This is questionable if comment-end ends in
2842 ;; whitespace. That is pretty brain-damaged,
2844 (while (progn (skip-chars-backward " \t")
2845 (and (>= (- (point) (point-min)) (length ce))
2847 (backward-char (length ce))
2848 (looking-at (regexp-quote ce)))))
2849 (delete-char (- (length ce)))))
2850 (let ((count numarg))
2851 (while (> 1 (setq count (1+ count)))
2853 ;; this is questionable if comment-end ends in whitespace
2854 ;; that is pretty brain-damaged though
2855 (skip-chars-backward " \t")
2856 (if (>= (- (point) (point-min)) (length ce))
2858 (backward-char (length ce))
2859 (if (looking-at (regexp-quote ce))
2860 (delete-char (length ce)))))))))
2863 (when comment-padding
2864 (setq cs (concat cs cp)))
2866 ;; Insert at beginning and at end.
2867 (if (looking-at "[ \t]*$") ()
2869 (if (string= "" ce) ()
2872 (search-forward "\n" nil 'move)))))))
2874 (defun backward-word (arg)
2875 "Move backward until encountering the end of a word.
2876 With argument, do this that many times.
2877 In programs, it is faster to call `forward-word' with negative arg."
2879 (forward-word (- arg)))
2881 (defun mark-word (arg)
2882 "Set mark arg words away from point."
2890 (defun kill-word (arg)
2891 "Kill characters forward until encountering the end of a word.
2892 With argument, do this that many times."
2894 (kill-region (point) (progn (forward-word arg) (point))))
2896 (defun backward-kill-word (arg)
2897 "Kill characters backward until encountering the end of a word.
2898 With argument, do this that many times."
2900 (kill-word (- arg)))
2902 (defun current-word (&optional strict)
2903 "Return the word point is on (or a nearby word) as a string.
2904 If optional arg STRICT is non-nil, return nil unless point is within
2905 or adjacent to a word."
2907 (let ((oldpoint (point)) (start (point)) (end (point)))
2908 (skip-syntax-backward "w_") (setq start (point))
2909 (goto-char oldpoint)
2910 (skip-syntax-forward "w_") (setq end (point))
2911 (if (and (eq start oldpoint) (eq end oldpoint))
2912 ;; Point is neither within nor adjacent to a word.
2915 ;; Look for preceding word in same line.
2916 (skip-syntax-backward "^w_"
2917 (save-excursion (beginning-of-line)
2920 ;; No preceding word in same line.
2921 ;; Look for following word in same line.
2923 (skip-syntax-forward "^w_"
2924 (save-excursion (end-of-line)
2926 (setq start (point))
2927 (skip-syntax-forward "w_")
2930 (skip-syntax-backward "w_")
2931 (setq start (point)))
2932 (buffer-substring-no-properties start end)))
2933 (buffer-substring-no-properties start end)))))
2935 (defcustom fill-prefix nil
2936 "*String for filling to insert at front of new line, or nil for none.
2937 Setting this variable automatically makes it local to the current buffer."
2938 :type '(choice (const :tag "None" nil)
2941 (make-variable-buffer-local 'fill-prefix)
2943 (defcustom auto-fill-inhibit-regexp nil
2944 "*Regexp to match lines which should not be auto-filled."
2945 :type '(choice (const :tag "None" nil)
2949 (defvar comment-line-break-function 'indent-new-comment-line
2950 "*Mode-specific function which line breaks and continues a comment.
2952 This function is only called during auto-filling of a comment section.
2953 The function should take a single optional argument, which is a flag
2954 indicating whether it should use soft newlines.
2956 Setting this variable automatically makes it local to the current buffer.")
2958 ;; This function is used as the auto-fill-function of a buffer
2959 ;; when Auto-Fill mode is enabled.
2960 ;; It returns t if it really did any work.
2961 ;; (Actually some major modes use a different auto-fill function,
2962 ;; but this one is the default one.)
2963 (defun do-auto-fill ()
2964 (let (fc justify bol give-up
2965 (fill-prefix fill-prefix))
2966 (if (or (not (setq justify (current-justification)))
2967 (null (setq fc (current-fill-column)))
2968 (and (eq justify 'left)
2969 (<= (current-column) fc))
2970 (save-excursion (beginning-of-line)
2972 (and auto-fill-inhibit-regexp
2973 (looking-at auto-fill-inhibit-regexp))))
2974 nil ;; Auto-filling not required
2975 (if (memq justify '(full center right))
2976 (save-excursion (unjustify-current-line)))
2978 ;; Choose a fill-prefix automatically.
2979 (if (and adaptive-fill-mode
2980 (or (null fill-prefix) (string= fill-prefix "")))
2982 (fill-context-prefix
2983 (save-excursion (backward-paragraph 1) (point))
2984 (save-excursion (forward-paragraph 1) (point)))))
2985 (and prefix (not (equal prefix ""))
2986 (setq fill-prefix prefix))))
2988 (while (and (not give-up) (> (current-column) fc))
2989 ;; Determine where to split the line.
2992 (let ((opoint (point))
2997 (setq after-prefix (point))
2999 (looking-at (regexp-quote fill-prefix))
3000 (setq after-prefix (match-end 0)))
3001 (move-to-column (1+ fc))
3002 ;; Move back to the point where we can break the line.
3003 ;; We break the line between word or
3004 ;; after/before the character which has character
3005 ;; category `|'. We search space, \c| followed by
3006 ;; a character, or \c| following a character. If
3007 ;; not found, place the point at beginning of line.
3009 ;; If this is after period and a single space,
3010 ;; move back once more--we don't want to break
3011 ;; the line there and make it look like a
3015 sentence-end-double-space
3016 (save-excursion (forward-char -1)
3017 (and (looking-at "\\. ")
3018 (not (looking-at "\\. ")))))
3021 fill-nobreak-predicate
3022 (funcall fill-nobreak-predicate)))
3024 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
3025 ;; If we find nowhere on the line to break it,
3026 ;; break after one word. Set bounce to t
3027 ;; so we will not keep going in this while loop.
3028 (if (<= (point) after-prefix)
3030 (goto-char after-prefix)
3031 (re-search-forward "[ \t]" opoint t)
3033 (if (looking-at "[ \t]")
3034 ;; Break the line at word boundary.
3035 (skip-chars-backward " \t")
3036 ;; Break the line after/before \c|.
3038 (if enable-multibyte-characters
3039 ;; If we are going to break the line after or
3040 ;; before a non-ascii character, we may have
3041 ;; to run a special function for the charset
3042 ;; of the character to find the correct break
3044 (if (not (and (eq (charset-after (1- (point))) 'ascii)
3045 (eq (charset-after (point)) 'ascii)))
3046 (fill-find-break-point after-prefix)))
3048 ;; Let fill-point be set to the place where we end up.
3049 ;; But move back before any whitespace here.
3050 (skip-chars-backward " \t")
3053 ;; See whether the place we found is any good.
3055 (goto-char fill-point)
3057 ;; There is no use breaking at end of line.
3058 (not (save-excursion (skip-chars-forward " ") (eolp)))
3059 ;; It is futile to split at the end of the prefix
3060 ;; since we would just insert the prefix again.
3061 (not (and after-prefix (<= (point) after-prefix)))
3062 ;; Don't split right after a comment starter
3063 ;; since we would just make another comment starter.
3064 (not (and comment-start-skip
3065 (let ((limit (point)))
3067 (and (re-search-forward comment-start-skip
3069 (eq (point) limit)))))))
3070 ;; Ok, we have a useful place to break the line. Do it.
3071 (let ((prev-column (current-column)))
3072 ;; If point is at the fill-point, do not `save-excursion'.
3073 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3074 ;; point will end up before it rather than after it.
3076 (skip-chars-backward " \t")
3077 (= (point) fill-point))
3078 (funcall comment-line-break-function t)
3080 (goto-char fill-point)
3081 (funcall comment-line-break-function t)))
3082 ;; Now do justification, if required
3083 (if (not (eq justify 'left))
3086 (justify-current-line justify nil t)))
3087 ;; If making the new line didn't reduce the hpos of
3088 ;; the end of the line, then give up now;
3089 ;; trying again will not help.
3090 (if (>= (current-column) prev-column)
3092 ;; No good place to break => stop trying.
3094 ;; Justify last line.
3095 (justify-current-line justify t t)
3098 (defvar normal-auto-fill-function 'do-auto-fill
3099 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3100 Some major modes set this.")
3102 (defun auto-fill-mode (&optional arg)
3103 "Toggle Auto Fill mode.
3104 With arg, turn Auto Fill mode on if and only if arg is positive.
3105 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3106 automatically breaks the line at a previous space.
3108 The value of `normal-auto-fill-function' specifies the function to use
3109 for `auto-fill-function' when turning Auto Fill mode on."
3111 (prog1 (setq auto-fill-function
3113 (not auto-fill-function)
3114 (> (prefix-numeric-value arg) 0))
3115 normal-auto-fill-function
3117 (force-mode-line-update)))
3119 ;; This holds a document string used to document auto-fill-mode.
3120 (defun auto-fill-function ()
3121 "Automatically break line at a previous space, in insertion of text."
3124 (defun turn-on-auto-fill ()
3125 "Unconditionally turn on Auto Fill mode."
3127 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3129 (defun set-fill-column (arg)
3130 "Set `fill-column' to specified argument.
3131 Use \\[universal-argument] followed by a number to specify a column.
3132 Just \\[universal-argument] as argument means to use the current column."
3135 (setq arg (current-column)))
3136 (if (not (integerp arg))
3137 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3138 (error "set-fill-column requires an explicit argument")
3139 (message "Fill column set to %d (was %d)" arg fill-column)
3140 (setq fill-column arg)))
3142 (defcustom comment-multi-line nil
3143 "*Non-nil means \\[indent-new-comment-line] should continue same comment
3144 on new line, with no new terminator or starter.
3145 This is obsolete because you might as well use \\[newline-and-indent]."
3147 :group 'fill-comments)
3149 (defun indent-new-comment-line (&optional soft)
3150 "Break line at point and indent, continuing comment if within one.
3151 This indents the body of the continued comment
3152 under the previous comment line.
3154 This command is intended for styles where you write a comment per line,
3155 starting a new comment (and terminating it if necessary) on each line.
3156 If you want to continue one comment across several lines, use \\[newline-and-indent].
3158 If a fill column is specified, it overrides the use of the comment column
3159 or comment indentation.
3161 The inserted newline is marked hard if `use-hard-newlines' is true,
3162 unless optional argument SOFT is non-nil."
3164 (let (comcol comstart)
3165 (skip-chars-backward " \t")
3166 (delete-region (point)
3167 (progn (skip-chars-forward " \t")
3169 (if soft (insert-and-inherit ?\n) (newline 1))
3172 (indent-to-left-margin)
3173 (insert-and-inherit fill-prefix))
3174 (if (not comment-multi-line)
3176 (if (and comment-start-skip
3177 (let ((opoint (1- (point)))
3180 ;; Determine (more or less) whether
3181 ;; target position is inside a comment.
3182 (while (and (re-search-forward comment-start-skip opoint t)
3183 (not (setq inside (or (equal comment-end "")
3184 (not (search-forward comment-end opoint t)))))))
3186 ;; The old line has a comment and point was inside the comment.
3187 ;; Set WIN to the pos of the comment-start.
3188 ;; But if the comment is empty, look at preceding lines
3189 ;; to find one that has a nonempty comment.
3191 ;; If comment-start-skip contains a \(...\) pair,
3192 ;; the real comment delimiter starts at the end of that pair.
3193 (let ((win (or (match-end 1) (match-beginning 0))))
3194 (while (and (eolp) (not (bobp))
3197 (setq opoint (point))
3199 (re-search-forward comment-start-skip opoint t)))
3200 (setq win (or (match-end 1) (match-beginning 0))))
3201 ;; Indent this line like what we found.
3203 (setq comcol (current-column))
3205 (buffer-substring (point) (match-end 0)))))))
3207 (let ((comment-column comcol)
3208 (comment-start comstart)
3209 (comment-end comment-end))
3210 (and comment-end (not (equal comment-end ""))
3211 ; (if (not comment-multi-line)
3214 (insert comment-end)
3216 ; (setq comment-column (+ comment-column (length comment-start))
3221 (setq comment-end ""))
3222 (insert-and-inherit ?\n)
3224 (indent-for-comment)
3226 ;; Make sure we delete the newline inserted above.
3229 (indent-according-to-mode)))))
3231 (defun set-selective-display (arg)
3232 "Set `selective-display' to ARG; clear it if no arg.
3233 When the value of `selective-display' is a number > 0,
3234 lines whose indentation is >= that value are not displayed.
3235 The variable `selective-display' has a separate value for each buffer."
3237 (if (eq selective-display t)
3238 (error "selective-display already in use for marked lines"))
3241 (narrow-to-region (point-min) (point))
3242 (goto-char (window-start))
3243 (vertical-motion (window-height)))))
3244 (setq selective-display
3245 (and arg (prefix-numeric-value arg)))
3246 (recenter current-vpos))
3247 (set-window-start (selected-window) (window-start (selected-window)))
3248 (princ "selective-display set to " t)
3249 (prin1 selective-display t)
3252 (defvar overwrite-mode-textual " Ovwrt"
3253 "The string displayed in the mode line when in overwrite mode.")
3254 (defvar overwrite-mode-binary " Bin Ovwrt"
3255 "The string displayed in the mode line when in binary overwrite mode.")
3257 (defun overwrite-mode (arg)
3258 "Toggle overwrite mode.
3259 With arg, turn overwrite mode on iff arg is positive.
3260 In overwrite mode, printing characters typed in replace existing text
3261 on a one-for-one basis, rather than pushing it to the right. At the
3262 end of a line, such characters extend the line. Before a tab,
3263 such characters insert until the tab is filled in.
3264 \\[quoted-insert] still inserts characters in overwrite mode; this
3265 is supposed to make it easier to insert characters when necessary."
3267 (setq overwrite-mode
3268 (if (if (null arg) (not overwrite-mode)
3269 (> (prefix-numeric-value arg) 0))
3270 'overwrite-mode-textual))
3271 (force-mode-line-update))
3273 (defun binary-overwrite-mode (arg)
3274 "Toggle binary overwrite mode.
3275 With arg, turn binary overwrite mode on iff arg is positive.
3276 In binary overwrite mode, printing characters typed in replace
3277 existing text. Newlines are not treated specially, so typing at the
3278 end of a line joins the line to the next, with the typed character
3279 between them. Typing before a tab character simply replaces the tab
3280 with the character typed.
3281 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3282 typing characters do.
3284 Note that binary overwrite mode is not its own minor mode; it is a
3285 specialization of overwrite-mode, entered by setting the
3286 `overwrite-mode' variable to `overwrite-mode-binary'."
3288 (setq overwrite-mode
3290 (not (eq overwrite-mode 'overwrite-mode-binary))
3291 (> (prefix-numeric-value arg) 0))
3292 'overwrite-mode-binary))
3293 (force-mode-line-update))
3295 (defcustom line-number-mode t
3296 "*Non-nil means display line number in mode line."
3298 :group 'editing-basics)
3300 (defun line-number-mode (arg)
3301 "Toggle Line Number mode.
3302 With arg, turn Line Number mode on iff arg is positive.
3303 When Line Number mode is enabled, the line number appears
3306 Line numbers do not appear for very large buffers, see variable
3307 `line-number-display-limit'."
3309 (setq line-number-mode
3310 (if (null arg) (not line-number-mode)
3311 (> (prefix-numeric-value arg) 0)))
3312 (force-mode-line-update))
3314 (defcustom column-number-mode nil
3315 "*Non-nil means display column number in mode line."
3317 :group 'editing-basics)
3319 (defun column-number-mode (arg)
3320 "Toggle Column Number mode.
3321 With arg, turn Column Number mode on iff arg is positive.
3322 When Column Number mode is enabled, the column number appears
3325 (setq column-number-mode
3326 (if (null arg) (not column-number-mode)
3327 (> (prefix-numeric-value arg) 0)))
3328 (force-mode-line-update))
3330 (defgroup paren-blinking nil
3331 "Blinking matching of parens and expressions."
3332 :prefix "blink-matching-"
3333 :group 'paren-matching)
3335 (defcustom blink-matching-paren t
3336 "*Non-nil means show matching open-paren when close-paren is inserted."
3338 :group 'paren-blinking)
3340 (defcustom blink-matching-paren-on-screen t
3341 "*Non-nil means show matching open-paren when it is on screen.
3342 If nil, means don't show it (but the open-paren can still be shown
3343 when it is off screen)."
3345 :group 'paren-blinking)
3347 (defcustom blink-matching-paren-distance (* 25 1024)
3348 "*If non-nil, is maximum distance to search for matching open-paren."
3350 :group 'paren-blinking)
3352 (defcustom blink-matching-delay 1
3353 "*Time in seconds to delay after showing a matching paren."
3355 :group 'paren-blinking)
3357 (defcustom blink-matching-paren-dont-ignore-comments nil
3358 "*Non-nil means `blink-matching-paren' will not ignore comments."
3360 :group 'paren-blinking)
3362 (defun blink-matching-open ()
3363 "Move cursor momentarily to the beginning of the sexp before point."
3365 (and (> (point) (1+ (point-min)))
3366 blink-matching-paren
3367 ;; Verify an even number of quoting characters precede the close.
3368 (= 1 (logand 1 (- (point)
3371 (skip-syntax-backward "/\\")
3373 (let* ((oldpos (point))
3378 (if blink-matching-paren-distance
3379 (narrow-to-region (max (point-min)
3380 (- (point) blink-matching-paren-distance))
3383 (let ((parse-sexp-ignore-comments
3384 (and parse-sexp-ignore-comments
3385 (not blink-matching-paren-dont-ignore-comments))))
3386 (setq blinkpos (scan-sexps oldpos -1)))
3389 (/= (char-syntax (char-after blinkpos))
3392 (or (null (matching-paren (char-after blinkpos)))
3393 (/= (char-after (1- oldpos))
3394 (matching-paren (char-after blinkpos))))))
3395 (if mismatch (setq blinkpos nil))
3397 ;; Don't log messages about paren matching.
3398 (let (message-log-max)
3399 (goto-char blinkpos)
3400 (if (pos-visible-in-window-p)
3401 (and blink-matching-paren-on-screen
3402 (sit-for blink-matching-delay))
3403 (goto-char blinkpos)
3406 ;; Show what precedes the open in its line, if anything.
3408 (skip-chars-backward " \t")
3410 (buffer-substring (progn (beginning-of-line) (point))
3412 ;; Show what follows the open in its line, if anything.
3415 (skip-chars-forward " \t")
3417 (buffer-substring blinkpos
3418 (progn (end-of-line) (point)))
3419 ;; Otherwise show the previous nonblank line,
3422 (skip-chars-backward "\n \t")
3425 (buffer-substring (progn
3426 (skip-chars-backward "\n \t")
3429 (progn (end-of-line)
3430 (skip-chars-backward " \t")
3432 ;; Replace the newline and other whitespace with `...'.
3434 (buffer-substring blinkpos (1+ blinkpos)))
3435 ;; There is nothing to show except the char itself.
3436 (buffer-substring blinkpos (1+ blinkpos))))))))
3438 (message "Mismatched parentheses"))
3439 ((not blink-matching-paren-distance)
3440 (message "Unmatched parenthesis"))))))))
3442 ;Turned off because it makes dbx bomb out.
3443 (setq blink-paren-function 'blink-matching-open)
3445 ;; This executes C-g typed while Emacs is waiting for a command.
3446 ;; Quitting out of a program does not go through here;
3447 ;; that happens in the QUIT macro at the C code level.
3448 (defun keyboard-quit ()
3449 "Signal a `quit' condition.
3450 During execution of Lisp code, this character causes a quit directly.
3451 At top-level, as an editor command, this simply beeps."
3456 (define-key global-map "\C-g" 'keyboard-quit)
3458 (defvar buffer-quit-function nil
3459 "Function to call to \"quit\" the current buffer, or nil if none.
3460 \\[keyboard-escape-quit] calls this function when its more local actions
3461 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3463 (defun keyboard-escape-quit ()
3464 "Exit the current \"mode\" (in a generalized sense of the word).
3465 This command can exit an interactive command such as `query-replace',
3466 can clear out a prefix argument or a region,
3467 can get out of the minibuffer or other recursive edit,
3468 cancel the use of the current buffer (for special-purpose buffers),
3469 or go back to just one window (by deleting all but the selected window)."
3471 (cond ((eq last-command 'mode-exited) nil)
3472 ((> (minibuffer-depth) 0)
3473 (abort-recursive-edit))
3476 ((and transient-mark-mode
3479 ((> (recursion-depth) 0)
3480 (exit-recursive-edit))
3481 (buffer-quit-function
3482 (funcall buffer-quit-function))
3483 ((not (one-window-p t))
3484 (delete-other-windows))
3485 ((string-match "^ \\*" (buffer-name (current-buffer)))
3488 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3490 (defcustom input-mode-8-bit nil
3491 "Toggle whether 8-bit keyboard input is accepted.
3492 This may be useful for inputting non-ASCII characters if your keyboard
3494 Setting this variable directly does not take effect;
3495 use either M-x customize or the function `set-input-mode'."
3496 :set (lambda (symbol value)
3497 (let ((mode (current-input-mode)))
3498 (set-input-mode (nth 0 mode) (nth 1 mode) value)))
3499 :initialize 'custom-initialize-default
3502 :link '(custom-manual "Single-Byte European Support")
3505 (defcustom mail-user-agent 'sendmail-user-agent
3506 "*Your preference for a mail composition package.
3507 Various Emacs Lisp packages (e.g. reporter) require you to compose an
3508 outgoing email message. This variable lets you specify which
3509 mail-sending package you prefer.
3511 Valid values include:
3513 sendmail-user-agent -- use the default Emacs Mail package
3514 mh-e-user-agent -- use the Emacs interface to the MH mail system
3515 message-user-agent -- use the GNUS mail sending package
3517 Additional valid symbols may be available; check with the author of
3518 your package for details."
3519 :type '(radio (function-item :tag "Default Emacs mail"
3521 sendmail-user-agent)
3522 (function-item :tag "Emacs interface to MH"
3525 (function-item :tag "Gnus mail sending package"
3528 (function :tag "Other"))
3531 (defun define-mail-user-agent (symbol composefunc sendfunc
3532 &optional abortfunc hookvar)
3533 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3535 SYMBOL can be any Lisp symbol. Its function definition and/or
3536 value as a variable do not matter for this usage; we use only certain
3537 properties on its property list, to encode the rest of the arguments.
3539 COMPOSEFUNC is program callable function that composes an outgoing
3540 mail message buffer. This function should set up the basics of the
3541 buffer without requiring user interaction. It should populate the
3542 standard mail headers, leaving the `to:' and `subject:' headers blank
3545 COMPOSEFUNC should accept several optional arguments--the same
3546 arguments that `compose-mail' takes. See that function's documentation.
3548 SENDFUNC is the command a user would run to send the message.
3550 Optional ABORTFUNC is the command a user would run to abort the
3551 message. For mail packages that don't have a separate abort function,
3552 this can be `kill-buffer' (the equivalent of omitting this argument).
3554 Optional HOOKVAR is a hook variable that gets run before the message
3555 is actually sent. Callers that use the `mail-user-agent' may
3556 install a hook function temporarily on this hook variable.
3557 If HOOKVAR is nil, `mail-send-hook' is used.
3559 The properties used on SYMBOL are `composefunc', `sendfunc',
3560 `abortfunc', and `hookvar'."
3561 (put symbol 'composefunc composefunc)
3562 (put symbol 'sendfunc sendfunc)
3563 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3564 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3566 (define-mail-user-agent 'sendmail-user-agent
3567 'sendmail-user-agent-compose
3568 'mail-send-and-exit)
3570 (defun rfc822-goto-eoh ()
3571 ;; Go to header delimiter line in a mail message, following RFC822 rules
3572 (goto-char (point-min))
3573 (while (looking-at "^[^: \n]+:\\|^[ \t]")
3577 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3578 switch-function yank-action
3581 (let ((special-display-buffer-names nil)
3582 (special-display-regexps nil)
3583 (same-window-buffer-names nil)
3584 (same-window-regexps nil))
3585 (funcall switch-function "*mail*")))
3586 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3587 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))))
3588 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3590 (error "Message aborted"))
3593 (while other-headers
3594 (if (not (assoc-ignore-case (car (car other-headers))
3595 '(("in-reply-to") ("cc"))))
3596 (insert (car (car other-headers)) ": "
3597 (cdr (car other-headers)) "\n"))
3598 (setq other-headers (cdr other-headers)))
3601 (define-mail-user-agent 'mh-e-user-agent
3602 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3603 'mh-before-send-letter-hook)
3605 (defun compose-mail (&optional to subject other-headers continue
3606 switch-function yank-action send-actions)
3607 "Start composing a mail message to send.
3608 This uses the user's chosen mail composition package
3609 as selected with the variable `mail-user-agent'.
3610 The optional arguments TO and SUBJECT specify recipients
3611 and the initial Subject field, respectively.
3613 OTHER-HEADERS is an alist specifying additional
3614 header fields. Elements look like (HEADER . VALUE) where both
3615 HEADER and VALUE are strings.
3617 CONTINUE, if non-nil, says to continue editing a message already
3620 SWITCH-FUNCTION, if non-nil, is a function to use to
3621 switch to and display the buffer used for mail composition.
3623 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3624 to insert the raw text of the message being replied to.
3625 It has the form (FUNCTION . ARGS). The user agent will apply
3626 FUNCTION to ARGS, to insert the raw text of the original message.
3627 \(The user agent will also run `mail-citation-hook', *after* the
3628 original text has been inserted in this way.)
3630 SEND-ACTIONS is a list of actions to call when the message is sent.
3631 Each action has the form (FUNCTION . ARGS)."
3633 (list nil nil nil current-prefix-arg))
3634 (let ((function (get mail-user-agent 'composefunc)))
3635 (funcall function to subject other-headers continue
3636 switch-function yank-action send-actions)))
3638 (defun compose-mail-other-window (&optional to subject other-headers continue
3639 yank-action send-actions)
3640 "Like \\[compose-mail], but edit the outgoing message in another window."
3642 (list nil nil nil current-prefix-arg))
3643 (compose-mail to subject other-headers continue
3644 'switch-to-buffer-other-window yank-action send-actions))
3647 (defun compose-mail-other-frame (&optional to subject other-headers continue
3648 yank-action send-actions)
3649 "Like \\[compose-mail], but edit the outgoing message in another frame."
3651 (list nil nil nil current-prefix-arg))
3652 (compose-mail to subject other-headers continue
3653 'switch-to-buffer-other-frame yank-action send-actions))
3655 (defvar set-variable-value-history nil
3656 "History of values entered with `set-variable'.")
3658 (defun set-variable (var val)
3659 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3660 When using this interactively, enter a Lisp object for VALUE.
3661 If you want VALUE to be a string, you must surround it with doublequotes.
3662 VALUE is used literally, not evaluated.
3664 If VARIABLE has a `variable-interactive' property, that is used as if
3665 it were the arg to `interactive' (which see) to interactively read VALUE.
3667 If VARIABLE has been defined with `defcustom', then the type information
3668 in the definition is used to check that VALUE is valid."
3670 (let* ((default-var (variable-at-point))
3671 (var (if (symbolp default-var)
3672 (read-variable (format "Set variable (default %s): " default-var)
3674 (read-variable "Set variable: ")))
3675 (minibuffer-help-form '(describe-variable var))
3676 (prop (get var 'variable-interactive))
3677 (prompt (format "Set %s to value: " var))
3679 ;; Use VAR's `variable-interactive' property
3680 ;; as an interactive spec for prompting.
3681 (call-interactively `(lambda (arg)
3685 (read-string prompt nil
3686 'set-variable-value-history)))))
3689 (let ((type (get var 'custom-type)))
3691 ;; Match with custom type.
3693 (setq type (widget-convert type))
3694 (unless (widget-apply type :match val)
3695 (error "Value `%S' does not match type %S of %S"
3696 val (car type) var))))
3699 ;; Define the major mode for lists of completions.
3701 (defvar completion-list-mode-map nil
3702 "Local map for completion list buffers.")
3703 (or completion-list-mode-map
3704 (let ((map (make-sparse-keymap)))
3705 (define-key map [mouse-2] 'mouse-choose-completion)
3706 (define-key map [down-mouse-2] nil)
3707 (define-key map "\C-m" 'choose-completion)
3708 (define-key map "\e\e\e" 'delete-completion-window)
3709 (define-key map [left] 'previous-completion)
3710 (define-key map [right] 'next-completion)
3711 (setq completion-list-mode-map map)))
3713 ;; Completion mode is suitable only for specially formatted data.
3714 (put 'completion-list-mode 'mode-class 'special)
3716 (defvar completion-reference-buffer nil
3717 "Record the buffer that was current when the completion list was requested.
3718 This is a local variable in the completion list buffer.
3719 Initial value is nil to avoid some compiler warnings.")
3721 (defvar completion-no-auto-exit nil
3722 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3723 This also applies to other functions such as `choose-completion'
3724 and `mouse-choose-completion'.")
3726 (defvar completion-base-size nil
3727 "Number of chars at beginning of minibuffer not involved in completion.
3728 This is a local variable in the completion list buffer
3729 but it talks about the buffer in `completion-reference-buffer'.
3730 If this is nil, it means to compare text to determine which part
3731 of the tail end of the buffer's text is involved in completion.")
3733 (defun delete-completion-window ()
3734 "Delete the completion list window.
3735 Go to the window from which completion was requested."
3737 (let ((buf completion-reference-buffer))
3738 (if (one-window-p t)
3739 (if (window-dedicated-p (selected-window))
3740 (delete-frame (selected-frame)))
3741 (delete-window (selected-window))
3742 (if (get-buffer-window buf)
3743 (select-window (get-buffer-window buf))))))
3745 (defun previous-completion (n)
3746 "Move to the previous item in the completion list."
3748 (next-completion (- n)))
3750 (defun next-completion (n)
3751 "Move to the next item in the completion list.
3752 With prefix argument N, move N items (negative N means move backward)."
3754 (while (and (> n 0) (not (eobp)))
3755 (let ((prop (get-text-property (point) 'mouse-face))
3757 ;; If in a completion, move to the end of it.
3759 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3760 ;; Move to start of next one.
3761 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3763 (while (and (< n 0) (not (bobp)))
3764 (let ((prop (get-text-property (1- (point)) 'mouse-face))
3766 ;; If in a completion, move to the start of it.
3768 (goto-char (previous-single-property-change
3769 (point) 'mouse-face nil end)))
3770 ;; Move to end of the previous completion.
3771 (goto-char (previous-single-property-change (point) 'mouse-face nil end))
3772 ;; Move to the start of that one.
3773 (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
3776 (defun choose-completion ()
3777 "Choose the completion that point is in or next to."
3779 (let (beg end completion (buffer completion-reference-buffer)
3780 (base-size completion-base-size))
3781 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3782 (setq end (point) beg (1+ (point))))
3783 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3784 (setq end (1- (point)) beg (point)))
3786 (error "No completion here"))
3787 (setq beg (previous-single-property-change beg 'mouse-face))
3788 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3789 (setq completion (buffer-substring beg end))
3790 (let ((owindow (selected-window)))
3791 (if (and (one-window-p t 'selected-frame)
3792 (window-dedicated-p (selected-window)))
3793 ;; This is a special buffer's frame
3794 (iconify-frame (selected-frame))
3795 (or (window-dedicated-p (selected-window))
3797 (select-window owindow))
3798 (choose-completion-string completion buffer base-size)))
3800 ;; Delete the longest partial match for STRING
3801 ;; that can be found before POINT.
3802 (defun choose-completion-delete-max-match (string)
3803 (let ((opoint (point))
3804 (len (min (length string)
3805 (- (point) (point-min)))))
3806 (goto-char (- (point) (length string)))
3807 (if completion-ignore-case
3808 (setq string (downcase string)))
3809 (while (and (> len 0)
3810 (let ((tail (buffer-substring (point)
3812 (if completion-ignore-case
3813 (setq tail (downcase tail)))
3814 (not (string= tail (substring string 0 len)))))
3819 ;; Switch to BUFFER and insert the completion choice CHOICE.
3820 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3821 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3823 ;; If BUFFER is the minibuffer, exit the minibuffer
3824 ;; unless it is reading a file name and CHOICE is a directory,
3825 ;; or completion-no-auto-exit is non-nil.
3826 (defun choose-completion-string (choice &optional buffer base-size)
3827 (let ((buffer (or buffer completion-reference-buffer)))
3828 ;; If BUFFER is a minibuffer, barf unless it's the currently
3829 ;; active minibuffer.
3830 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
3831 (or (not (active-minibuffer-window))
3833 (window-buffer (active-minibuffer-window))))))
3834 (error "Minibuffer is not active for completion")
3835 ;; Insert the completion into the buffer where completion was requested.
3838 (delete-region (+ base-size (point-min)) (point))
3839 (choose-completion-delete-max-match choice))
3841 (remove-text-properties (- (point) (length choice)) (point)
3843 ;; Update point in the window that BUFFER is showing in.
3844 (let ((window (get-buffer-window buffer t)))
3845 (set-window-point window (point)))
3846 ;; If completing for the minibuffer, exit it with this choice.
3847 (and (not completion-no-auto-exit)
3848 (equal buffer (window-buffer (minibuffer-window)))
3849 minibuffer-completion-table
3850 ;; If this is reading a file name, and the file name chosen
3851 ;; is a directory, don't exit the minibuffer.
3852 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3853 (file-directory-p (field-string (point-max))))
3854 (select-window (active-minibuffer-window))
3855 (exit-minibuffer))))))
3857 (defun completion-list-mode ()
3858 "Major mode for buffers showing lists of possible completions.
3859 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3860 to select the completion near point.
3861 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3864 (kill-all-local-variables)
3865 (use-local-map completion-list-mode-map)
3866 (setq mode-name "Completion List")
3867 (setq major-mode 'completion-list-mode)
3868 (make-local-variable 'completion-base-size)
3869 (setq completion-base-size nil)
3870 (run-hooks 'completion-list-mode-hook))
3872 (defvar completion-setup-hook nil
3873 "Normal hook run at the end of setting up a completion list buffer.
3874 When this hook is run, the current buffer is the one in which the
3875 command to display the completion list buffer was run.
3876 The completion list buffer is available as the value of `standard-output'.")
3878 ;; This function goes in completion-setup-hook, so that it is called
3879 ;; after the text of the completion list buffer is written.
3881 (defun completion-setup-function ()
3883 (let ((mainbuf (current-buffer)))
3884 (set-buffer standard-output)
3885 (completion-list-mode)
3886 (make-local-variable 'completion-reference-buffer)
3887 (setq completion-reference-buffer mainbuf)
3888 (if (eq minibuffer-completion-table 'read-file-name-internal)
3889 ;; For file name completion,
3890 ;; use the number of chars before the start of the
3891 ;; last file name component.
3892 (setq completion-base-size
3894 (set-buffer mainbuf)
3895 (goto-char (point-max))
3896 (skip-chars-backward (format "^%c" directory-sep-char))
3897 (- (point) (point-min))))
3898 ;; Otherwise, in minibuffer, the whole input is being completed.
3900 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3901 (buffer-name mainbuf))
3902 (setq completion-base-size 0))))
3903 (goto-char (point-min))
3905 (insert (substitute-command-keys
3906 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3907 (insert (substitute-command-keys
3908 "In this buffer, type \\[choose-completion] to \
3909 select the completion near point.\n\n")))))
3911 (add-hook 'completion-setup-hook 'completion-setup-function)
3913 (define-key minibuffer-local-completion-map [prior]
3914 'switch-to-completions)
3915 (define-key minibuffer-local-must-match-map [prior]
3916 'switch-to-completions)
3917 (define-key minibuffer-local-completion-map "\M-v"
3918 'switch-to-completions)
3919 (define-key minibuffer-local-must-match-map "\M-v"
3920 'switch-to-completions)
3922 (defun switch-to-completions ()
3923 "Select the completion list window."
3925 ;; Make sure we have a completions window.
3926 (or (get-buffer-window "*Completions*")
3927 (minibuffer-completion-help))
3928 (let ((window (get-buffer-window "*Completions*")))
3930 (select-window window)
3931 (goto-char (point-min))
3932 (search-forward "\n\n")
3935 ;; Support keyboard commands to turn on various modifiers.
3937 ;; These functions -- which are not commands -- each add one modifier
3938 ;; to the following event.
3940 (defun event-apply-alt-modifier (ignore-prompt)
3941 "Add the Alt modifier to the following event.
3942 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
3943 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3944 (defun event-apply-super-modifier (ignore-prompt)
3945 "Add the Super modifier to the following event.
3946 For example, type \\[event-apply-super-modifier] & to enter Super-&."
3947 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3948 (defun event-apply-hyper-modifier (ignore-prompt)
3949 "Add the Hyper modifier to the following event.
3950 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
3951 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3952 (defun event-apply-shift-modifier (ignore-prompt)
3953 "Add the Shift modifier to the following event.
3954 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
3955 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3956 (defun event-apply-control-modifier (ignore-prompt)
3957 "Add the Ctrl modifier to the following event.
3958 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
3959 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3960 (defun event-apply-meta-modifier (ignore-prompt)
3961 "Add the Meta modifier to the following event.
3962 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
3963 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3965 (defun event-apply-modifier (event symbol lshiftby prefix)
3966 "Apply a modifier flag to event EVENT.
3967 SYMBOL is the name of this modifier, as a symbol.
3968 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3969 PREFIX is the string that represents this modifier in an event type symbol."
3971 (cond ((eq symbol 'control)
3972 (if (and (<= (downcase event) ?z)
3973 (>= (downcase event) ?a))
3974 (- (downcase event) ?a -1)
3975 (if (and (<= (downcase event) ?Z)
3976 (>= (downcase event) ?A))
3977 (- (downcase event) ?A -1)
3978 (logior (lsh 1 lshiftby) event))))
3980 (if (and (<= (downcase event) ?z)
3981 (>= (downcase event) ?a))
3983 (logior (lsh 1 lshiftby) event)))
3985 (logior (lsh 1 lshiftby) event)))
3986 (if (memq symbol (event-modifiers event))
3988 (let ((event-type (if (symbolp event) event (car event))))
3989 (setq event-type (intern (concat prefix (symbol-name event-type))))
3992 (cons event-type (cdr event)))))))
3994 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
3995 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
3996 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
3997 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
3998 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
3999 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4001 ;;;; Keypad support.
4003 ;;; Make the keypad keys act like ordinary typing keys. If people add
4004 ;;; bindings for the function key symbols, then those bindings will
4005 ;;; override these, so this shouldn't interfere with any existing
4008 ;; Also tell read-char how to handle these keys.
4010 (lambda (keypad-normal)
4011 (let ((keypad (nth 0 keypad-normal))
4012 (normal (nth 1 keypad-normal)))
4013 (put keypad 'ascii-character normal)
4014 (define-key function-key-map (vector keypad) (vector normal))))
4015 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4016 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4029 ;;;; forking a twin copy of a buffer.
4032 (defvar clone-buffer-hook nil
4033 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4035 (defun clone-process (process &optional newname)
4036 "Create a twin copy of PROCESS.
4037 If NEWNAME is nil, it defaults to PROCESS' name;
4038 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4039 If PROCESS is associated with a buffer, the new process will be associated
4040 with the current buffer instead.
4041 Returns nil if PROCESS has already terminated."
4042 (setq newname (or newname (process-name process)))
4043 (if (string-match "<[0-9]+>\\'" newname)
4044 (setq newname (substring newname 0 (match-beginning 0))))
4045 (when (memq (process-status process) '(run stop open))
4046 (let* ((process-connection-type (process-tty-name process))
4047 (old-kwoq (process-kill-without-query process nil))
4049 (if (memq (process-status process) '(open))
4050 (apply 'open-network-stream newname
4051 (if (process-buffer process) (current-buffer))
4052 (process-contact process))
4053 (apply 'start-process newname
4054 (if (process-buffer process) (current-buffer))
4055 (process-command process)))))
4056 (process-kill-without-query new-process old-kwoq)
4057 (process-kill-without-query process old-kwoq)
4058 (set-process-inherit-coding-system-flag
4059 new-process (process-inherit-coding-system-flag process))
4060 (set-process-filter new-process (process-filter process))
4061 (set-process-sentinel new-process (process-sentinel process))
4064 ;; things to maybe add (currently partly covered by `funcall mode':
4067 (defun clone-buffer (&optional newname display-flag)
4068 "Create a twin copy of the current buffer.
4069 If NEWNAME is nil, it defaults to the current buffer's name;
4070 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4072 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
4073 This runs the normal hook `clone-buffer-hook' in the new buffer
4074 after it has been set up properly in other respects."
4075 (interactive (list (if current-prefix-arg (read-string "Name: "))
4077 (if buffer-file-name
4078 (error "Cannot clone a file-visiting buffer"))
4079 (if (get major-mode 'no-clone)
4080 (error "Cannot clone a buffer in %s mode" mode-name))
4081 (setq newname (or newname (buffer-name)))
4082 (if (string-match "<[0-9]+>\\'" newname)
4083 (setq newname (substring newname 0 (match-beginning 0))))
4084 (let ((buf (current-buffer))
4088 (mk (if mark-active (mark t)))
4089 (modified (buffer-modified-p))
4091 (lvars (buffer-local-variables))
4092 (process (get-buffer-process (current-buffer)))
4093 (new (generate-new-buffer (or newname (buffer-name)))))
4096 (with-current-buffer new
4097 (insert-buffer-substring buf)))
4098 (with-current-buffer new
4099 (narrow-to-region ptmin ptmax)
4101 (if mk (set-mark mk))
4102 (set-buffer-modified-p modified)
4104 ;; Clone the old buffer's process, if any.
4105 (when process (clone-process process))
4107 ;; Now set up the major mode.
4110 ;; Set up other local variables.
4112 (condition-case () ;in case var is read-only
4115 (set (make-local-variable (car v)) (cdr v)))
4119 ;; Run any hooks (typically set up by the major mode
4120 ;; for cloning to work properly).
4121 (run-hooks 'clone-buffer-hook))
4122 (if display-flag (pop-to-buffer new))
4125 ;;; simple.el ends here