1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99, 2000
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 (autoload 'widget-convert
"wid-edit")
32 (autoload 'shell-mode
"shell")
37 "Killing and yanking commands"
40 (defgroup paren-matching nil
41 "Highlight (un)matching of parens and expressions."
45 (defun fundamental-mode ()
46 "Major mode not specialized for anything in particular.
47 Other major modes are defined by comparison with this one."
49 (kill-all-local-variables))
51 ;; Making and deleting lines.
53 (defun newline (&optional arg
)
54 "Insert a newline, and move to left margin of the new line if it's blank.
55 The newline is marked with the text-property `hard'.
56 With ARG, insert that many newlines.
57 In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
59 (barf-if-buffer-read-only)
60 ;; Inserting a newline at the end of a line produces better redisplay in
61 ;; try_window_id than inserting at the beginning of a line, and the textual
62 ;; result is the same. So, if we're at beginning of line, pretend to be at
63 ;; the end of the previous line.
64 (let ((flag (and (not (bobp))
66 ;; Make sure no functions want to be told about
67 ;; the range of the changes.
68 (not after-change-functions
)
69 (not before-change-functions
)
70 ;; Make sure there are no markers here.
71 (not (buffer-has-markers-at (1- (point))))
72 (not (buffer-has-markers-at (point)))
73 ;; Make sure no text properties want to know
74 ;; where the change was.
75 (not (get-char-property (1- (point)) 'modification-hooks
))
76 (not (get-char-property (1- (point)) 'insert-behind-hooks
))
78 (not (get-char-property (point) 'insert-in-front-hooks
)))
79 ;; Make sure the newline before point isn't intangible.
80 (not (get-char-property (1- (point)) 'intangible
))
81 ;; Make sure the newline before point isn't read-only.
82 (not (get-char-property (1- (point)) 'read-only
))
83 ;; Make sure the newline before point isn't invisible.
84 (not (get-char-property (1- (point)) 'invisible
))
85 ;; Make sure the newline before point has the same
86 ;; properties as the char before it (if any).
87 (< (or (previous-property-change (point)) -
2)
89 (was-page-start (and (bolp)
90 (looking-at page-delimiter
)))
92 (if flag
(backward-char 1))
93 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
94 ;; Set last-command-char to tell self-insert what to insert.
95 (let ((last-command-char ?
\n)
96 ;; Don't auto-fill if we have a numeric argument.
97 ;; Also not if flag is true (it would fill wrong line);
98 ;; there is no need to since we're at BOL.
99 (auto-fill-function (if (or arg flag
) nil auto-fill-function
)))
101 (self-insert-command (prefix-numeric-value arg
))
102 ;; If we get an error in self-insert-command, put point at right place.
103 (if flag
(forward-char 1))))
104 ;; Even if we did *not* get an error, keep that forward-char;
105 ;; all further processing should apply to the newline that the user
106 ;; thinks he inserted.
108 ;; Mark the newline(s) `hard'.
109 (if use-hard-newlines
110 (set-hard-newline-properties
111 (- (point) (if arg
(prefix-numeric-value arg
) 1)) (point)))
112 ;; If the newline leaves the previous line blank,
113 ;; and we have a left margin, delete that from the blank line.
116 (goto-char beforepos
)
118 (and (looking-at "[ \t]$")
119 (> (current-left-margin) 0)
120 (delete-region (point) (progn (end-of-line) (point))))))
121 ;; Indent the line after the newline, except in one case:
122 ;; when we added the newline at the beginning of a line
123 ;; which starts a page.
125 (move-to-left-margin nil t
)))
128 (defun set-hard-newline-properties (from to
)
129 (let ((sticky (get-text-property from
'rear-nonsticky
)))
130 (put-text-property from to
'hard
't
)
131 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
132 (if (and (listp sticky
) (not (memq 'hard sticky
)))
133 (put-text-property from
(point) 'rear-nonsticky
134 (cons 'hard sticky
)))))
136 (defun open-line (arg)
137 "Insert a newline and leave point before it.
138 If there is a fill prefix and/or a left-margin, insert them on the new line
139 if the line would have been blank.
140 With arg N, insert N newlines."
142 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
143 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
149 (if do-left-margin
(indent-to (current-left-margin)))
150 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
157 "Split current line, moving portion beyond point vertically down."
159 (skip-chars-forward " \t")
160 (let ((col (current-column))
166 (defun delete-indentation (&optional arg
)
167 "Join this line to previous and fix up whitespace at join.
168 If there is a fill prefix, delete it from the beginning of this line.
169 With argument, join this line to following line."
172 (if arg
(forward-line 1))
173 (if (eq (preceding-char) ?
\n)
175 (delete-region (point) (1- (point)))
176 ;; If the second line started with the fill prefix,
177 ;; delete the prefix.
179 (<= (+ (point) (length fill-prefix
)) (point-max))
181 (buffer-substring (point)
182 (+ (point) (length fill-prefix
)))))
183 (delete-region (point) (+ (point) (length fill-prefix
))))
184 (fixup-whitespace))))
186 (defalias 'join-line
#'delete-indentation
) ; easier to find
188 (defun delete-blank-lines ()
189 "On blank line, delete all surrounding blank lines, leaving just one.
190 On isolated blank line, delete that one.
191 On nonblank line, delete any immediately following blank lines."
193 (let (thisblank singleblank
)
196 (setq thisblank
(looking-at "[ \t]*$"))
197 ;; Set singleblank if there is just one blank line here.
200 (not (looking-at "[ \t]*\n[ \t]*$"))
202 (progn (forward-line -
1)
203 (not (looking-at "[ \t]*$")))))))
204 ;; Delete preceding blank lines, and this one too if it's the only one.
208 (if singleblank
(forward-line 1))
209 (delete-region (point)
210 (if (re-search-backward "[^ \t\n]" nil t
)
211 (progn (forward-line 1) (point))
213 ;; Delete following blank lines, unless the current line is blank
214 ;; and there are no following blank lines.
215 (if (not (and thisblank singleblank
))
219 (delete-region (point)
220 (if (re-search-forward "[^ \t\n]" nil t
)
221 (progn (beginning-of-line) (point))
223 ;; Handle the special case where point is followed by newline and eob.
224 ;; Delete the line, leaving point at eob.
225 (if (looking-at "^[ \t]*\n\\'")
226 (delete-region (point) (point-max)))))
228 (defun newline-and-indent ()
229 "Insert a newline, then indent according to major mode.
230 Indentation is done using the value of `indent-line-function'.
231 In programming language modes, this is the same as TAB.
232 In some text modes, where TAB inserts a tab, this command indents to the
233 column specified by the function `current-left-margin'."
235 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
237 (indent-according-to-mode))
239 (defun reindent-then-newline-and-indent ()
240 "Reindent current line, insert newline, then indent the new line.
241 Indentation of both lines is done according to the current major mode,
242 which means calling the current value of `indent-line-function'.
243 In programming language modes, this is the same as TAB.
244 In some text modes, where TAB inserts a tab, this indents to the
245 column specified by the function `current-left-margin'."
248 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
249 (indent-according-to-mode))
251 (indent-according-to-mode))
253 (defun quoted-insert (arg)
254 "Read next input character and insert it.
255 This is useful for inserting control characters.
257 If the first character you type after this command is an octal digit,
258 you should type a sequence of octal digits which specify a character code.
259 Any nondigit terminates the sequence. If the terminator is a RET,
260 it is discarded; any other terminator is used itself as input.
261 The variable `read-quoted-char-radix' specifies the radix for this feature;
262 set it to 10 or 16 to use decimal or hex instead of octal.
264 In overwrite mode, this function inserts the character anyway, and
265 does not handle octal digits specially. This means that if you use
266 overwrite as your normal editing mode, you can use this function to
267 insert characters when necessary.
269 In binary overwrite mode, this function does overwrite, and octal
270 digits are interpreted as a character code. This is intended to be
271 useful for editing binary files."
273 (let ((char (if (or (not overwrite-mode
)
274 (eq overwrite-mode
'overwrite-mode-binary
))
277 ;; Assume character codes 0240 - 0377 stand for characters in some
278 ;; single-byte character set, and convert them to Emacs
280 (if (and enable-multibyte-characters
283 (setq char
(unibyte-char-to-multibyte char
)))
285 (if (eq overwrite-mode
'overwrite-mode-binary
)
288 (insert-and-inherit char
)
289 (setq arg
(1- arg
)))))
291 (defun forward-to-indentation (arg)
292 "Move forward ARG lines and position at first nonblank character."
295 (skip-chars-forward " \t"))
297 (defun backward-to-indentation (arg)
298 "Move backward ARG lines and position at first nonblank character."
300 (forward-line (- arg
))
301 (skip-chars-forward " \t"))
303 (defun back-to-indentation ()
304 "Move point to the first non-whitespace character on this line."
306 (beginning-of-line 1)
307 (skip-chars-forward " \t"))
309 (defun fixup-whitespace ()
310 "Fixup white space between objects around point.
311 Leave one space or none, according to the context."
314 (delete-horizontal-space)
315 (if (or (looking-at "^\\|\\s)")
316 (save-excursion (forward-char -
1)
317 (looking-at "$\\|\\s(\\|\\s'")))
321 (defun delete-horizontal-space ()
322 "Delete all spaces and tabs around point."
324 (skip-chars-backward " \t")
325 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
327 (defun just-one-space ()
328 "Delete all spaces and tabs around point, leaving one space."
330 (skip-chars-backward " \t")
331 (if (= (following-char) ?
)
334 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
337 (defun beginning-of-buffer (&optional arg
)
338 "Move point to the beginning of the buffer; leave mark at previous position.
339 With arg N, put point N/10 of the way from the beginning.
341 If the buffer is narrowed, this command uses the beginning and size
342 of the accessible part of the buffer.
344 Don't use this command in Lisp programs!
345 \(goto-char (point-min)) is faster and avoids clobbering the mark."
348 (let ((size (- (point-max) (point-min))))
352 ;; Avoid overflow for large buffer sizes!
353 (* (prefix-numeric-value arg
)
355 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
357 (if arg
(forward-line 1)))
359 (defun end-of-buffer (&optional arg
)
360 "Move point to the end of the buffer; leave mark at previous position.
361 With arg N, put point N/10 of the way from the end.
363 If the buffer is narrowed, this command uses the beginning and size
364 of the accessible part of the buffer.
366 Don't use this command in Lisp programs!
367 \(goto-char (point-max)) is faster and avoids clobbering the mark."
370 (let ((size (- (point-max) (point-min))))
374 ;; Avoid overflow for large buffer sizes!
375 (* (prefix-numeric-value arg
)
377 (/ (* size
(prefix-numeric-value arg
)) 10)))
379 ;; If we went to a place in the middle of the buffer,
380 ;; adjust it to the beginning of a line.
381 (cond (arg (forward-line 1))
382 ((< (point) (window-end nil t
))
383 ;; If the end of the buffer is not already on the screen,
384 ;; then scroll specially to put it near, but not at, the bottom.
385 (overlay-recenter (point))
388 (defun mark-whole-buffer ()
389 "Put point at beginning and mark at end of buffer.
390 You probably should not use this function in Lisp programs;
391 it is usually a mistake for a Lisp function to use any subroutine
392 that uses or sets the mark."
395 (push-mark (point-max) nil t
)
396 (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 In addition, with prefix argument, show details about that character
471 in *Help* buffer. See also the command `describe-char-after'."
473 (let* ((char (following-char))
477 (total (buffer-size))
478 (percent (if (> total
50000)
479 ;; Avoid overflow from multiplying by 100!
480 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
481 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
482 (hscroll (if (= (window-hscroll) 0)
484 (format " Hscroll=%d" (window-hscroll))))
485 (col (current-column)))
487 (if (or (/= beg
1) (/= end
(1+ total
)))
488 (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
489 pos total percent beg end col hscroll
)
490 (message "point=%d of %d (%d%%) column %d %s"
491 pos total percent col hscroll
))
492 (let ((coding buffer-file-coding-system
)
493 encoded encoding-msg
)
495 (eq (coding-system-type coding
) t
))
496 (setq coding default-buffer-file-coding-system
))
497 (if (not (char-valid-p char
))
499 (format "(0%o, %d, 0x%x, invalid)" char char char
))
500 (setq encoded
(and (>= char
128) (encode-coding-char char coding
)))
503 (format "(0%o, %d, 0x%x, file %s)"
505 (if (> (length encoded
) 1)
507 (encoded-string-description encoded coding
)))
508 (format "(0%o, %d, 0x%x)" char char char
))))
510 ;; We show the detailed information about CHAR.
511 (describe-char-after (point)))
512 (if (or (/= beg
1) (/= end
(1+ total
)))
513 (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
515 (single-key-description char
)
516 (buffer-substring-no-properties (point) (1+ (point))))
517 encoding-msg pos total percent beg end col hscroll
)
518 (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
520 (single-key-description char
)
521 (buffer-substring-no-properties (point) (1+ (point))))
522 encoding-msg pos total percent col hscroll
))))))
524 (defvar read-expression-map
525 (let ((m (make-sparse-keymap)))
526 (define-key m
"\M-\t" 'lisp-complete-symbol
)
527 (set-keymap-parent m minibuffer-local-map
)
529 "Minibuffer keymap used for reading Lisp expressions.")
531 (defvar read-expression-history nil
)
533 (defcustom eval-expression-print-level
4
534 "*Value to use for `print-level' when printing value in `eval-expression'."
539 (defcustom eval-expression-print-length
12
540 "*Value to use for `print-length' when printing value in `eval-expression'."
542 :type
'(choice (const nil
) integer
)
545 (defcustom eval-expression-debug-on-error t
546 "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'.
547 If nil, don't change the value of `debug-on-error'."
552 ;; We define this, rather than making `eval' interactive,
553 ;; for the sake of completion of names like eval-region, eval-current-buffer.
554 (defun eval-expression (eval-expression-arg
555 &optional eval-expression-insert-value
)
556 "Evaluate EXPRESSION and print value in minibuffer.
557 Value is also consed on to front of the variable `values'."
559 (list (read-from-minibuffer "Eval: "
560 nil read-expression-map t
561 'read-expression-history
)
564 (if (null eval-expression-debug-on-error
)
565 (setq values
(cons (eval eval-expression-arg
) values
))
566 (let ((old-value (make-symbol "t")) new-value
)
567 ;; Bind debug-on-error to something unique so that we can
568 ;; detect when evaled code changes it.
569 (let ((debug-on-error old-value
))
570 (setq values
(cons (eval eval-expression-arg
) values
))
571 (setq new-value debug-on-error
))
572 ;; If evaled code has changed the value of debug-on-error,
573 ;; propagate that change to the global binding.
574 (unless (eq old-value new-value
)
575 (setq debug-on-error new-value
))))
577 (let ((print-length eval-expression-print-length
)
578 (print-level eval-expression-print-level
))
580 (if eval-expression-insert-value
(current-buffer) t
))))
582 (defun edit-and-eval-command (prompt command
)
583 "Prompting with PROMPT, let user edit COMMAND and eval result.
584 COMMAND is a Lisp expression. Let user edit that expression in
585 the minibuffer, then read and evaluate the result."
586 (let ((command (read-from-minibuffer prompt
587 (prin1-to-string command
)
588 read-expression-map t
589 '(command-history .
1))))
590 ;; If command was added to command-history as a string,
591 ;; get rid of that. We want only evaluable expressions there.
592 (if (stringp (car command-history
))
593 (setq command-history
(cdr command-history
)))
595 ;; If command to be redone does not match front of history,
596 ;; add it to the history.
597 (or (equal command
(car command-history
))
598 (setq command-history
(cons command command-history
)))
601 (defun repeat-complex-command (arg)
602 "Edit and re-evaluate last complex command, or ARGth from last.
603 A complex command is one which used the minibuffer.
604 The command is placed in the minibuffer as a Lisp form for editing.
605 The result is executed, repeating the command as changed.
606 If the command has been changed or is not the most recent previous command
607 it is added to the front of the command history.
608 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
609 to get different commands to edit and resubmit."
611 (let ((elt (nth (1- arg
) command-history
))
616 (let ((print-level nil
)
617 (minibuffer-history-position arg
)
618 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
619 (read-from-minibuffer
620 "Redo: " (prin1-to-string elt
) read-expression-map t
621 (cons 'command-history arg
))))
623 ;; If command was added to command-history as a string,
624 ;; get rid of that. We want only evaluable expressions there.
625 (if (stringp (car command-history
))
626 (setq command-history
(cdr command-history
)))
628 ;; If command to be redone does not match front of history,
629 ;; add it to the history.
630 (or (equal newcmd
(car command-history
))
631 (setq command-history
(cons newcmd command-history
)))
635 (defvar minibuffer-history nil
636 "Default minibuffer history list.
637 This is used for all minibuffer input
638 except when an alternate history list is specified.")
639 (defvar minibuffer-history-sexp-flag nil
640 "Non-nil when doing history operations on `command-history'.
641 More generally, indicates that the history list being acted on
642 contains expressions rather than strings.
643 It is only valid if its value equals the current minibuffer depth,
644 to handle recursive uses of the minibuffer.")
645 (setq minibuffer-history-variable
'minibuffer-history
)
646 (setq minibuffer-history-position nil
)
647 (defvar minibuffer-history-search-history nil
)
650 (lambda (key-and-command)
652 (lambda (keymap-and-completionp)
653 ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
654 ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
655 ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
656 (define-key (symbol-value (car keymap-and-completionp
))
657 (car key-and-command
)
658 (let ((command (cdr key-and-command
)))
660 ;; (and ... nil) => ... turns back on the completion-oriented
661 ;; history commands which rms turned off since they seem to
662 ;; do things he doesn't like.
663 (if (and (cdr keymap-and-completionp
) nil
) ;XXX turned off
664 (progn (error "EMACS BUG!") (cdr command
))
667 '((minibuffer-local-map . nil
)
668 (minibuffer-local-ns-map . nil
)
669 (minibuffer-local-completion-map . t
)
670 (minibuffer-local-must-match-map . t
)
671 (read-expression-map . nil
))))
672 '(("\en" .
(next-history-element . next-complete-history-element
))
673 ([next] . (next-history-element . next-complete-history-element))
674 ("\ep" . (previous-history-element . previous-complete-history-element))
675 ([prior] . (previous-history-element . previous-complete-history-element))
676 ("\er" . previous-matching-history-element)
677 ("\es" . next-matching-history-element)))
679 (defvar minibuffer-text-before-history nil
680 "Text that was in this minibuffer before any history commands.
681 This is nil if there have not yet been any history commands
682 in this use of the minibuffer.")
684 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
686 (defun minibuffer-history-initialize ()
687 (setq minibuffer-text-before-history nil))
689 (defcustom minibuffer-history-case-insensitive-variables nil
690 "*Minibuffer history variables for which matching should ignore case.
691 If a history variable is a member of this list, then the
692 \\[previous-matching-history-element] and \\[next-matching-history-element]\
693 commands ignore case when searching it, regardless of `case-fold-search'."
694 :type '(repeat variable)
697 (defun previous-matching-history-element (regexp n)
698 "Find the previous history element that matches REGEXP.
699 \(Previous history elements refer to earlier actions.)
700 With prefix argument N, search for Nth previous match.
701 If N is negative, find the next or Nth next match.
702 An uppercase letter in REGEXP makes the search case-sensitive.
703 See also `minibuffer-history-case-insensitive-variables'."
705 (let* ((enable-recursive-minibuffers t)
706 (regexp (read-from-minibuffer "Previous element matching (regexp): "
710 'minibuffer-history-search-history)))
711 ;; Use the last regexp specified, by default, if input is empty.
712 (list (if (string= regexp "")
713 (if minibuffer-history-search-history
714 (car minibuffer-history-search-history)
715 (error "No previous history search regexp"))
717 (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:
735 (pos minibuffer-history-position))
738 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
739 (when (= pos prevpos)
741 "No later matching history item"
742 "No earlier matching history item")))
744 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
745 (let ((print-level nil))
746 (prin1-to-string (nth (1- pos) history)))
747 (nth (1- pos) history)))
750 (and (string-match regexp match-string)
752 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
753 (match-beginning 1))))
755 (setq n (+ n (if (< n 0) 1 -1)))))
756 (setq minibuffer-history-position pos)
757 (goto-char (point-max))
759 (insert match-string)
760 (goto-char (+ (field-beginning) match-offset))))
761 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
762 (eq (car (car command-history)) 'next-matching-history-element))
763 (setq command-history (cdr command-history))))
765 (defun next-matching-history-element (regexp n)
766 "Find the next history element that matches REGEXP.
767 \(The next history element refers to a more recent action.)
768 With prefix argument N, search for Nth next match.
769 If N is negative, find the previous or Nth previous match.
770 An uppercase letter in REGEXP makes the search case-sensitive."
772 (let* ((enable-recursive-minibuffers t)
773 (regexp (read-from-minibuffer "Next element matching (regexp): "
777 'minibuffer-history-search-history)))
778 ;; Use the last regexp specified, by default, if input is empty.
779 (list (if (string= regexp "")
780 (setcar minibuffer-history-search-history
781 (nth 1 minibuffer-history-search-history))
783 (prefix-numeric-value current-prefix-arg))))
784 (previous-matching-history-element regexp (- n)))
786 (defvar minibuffer-temporary-goal-position nil)
788 (defun next-history-element (n)
789 "Insert the next element of the minibuffer history into the minibuffer."
792 (let ((narg (- minibuffer-history-position n))
793 (minimum (if minibuffer-default -1 0))
794 elt minibuffer-returned-to-present)
795 (if (and (zerop minibuffer-history-position)
796 (null minibuffer-text-before-history))
797 (setq minibuffer-text-before-history (field-string (point-max))))
799 (if minibuffer-default
800 (error "End of history; no next item")
801 (error "End of history; no default available")))
802 (if (> narg (length (symbol-value minibuffer-history-variable)))
803 (error "Beginning of history; no preceding item"))
804 (unless (or (eq last-command 'next-history-element)
805 (eq last-command 'previous-history-element))
806 (let ((prompt-end (field-beginning (point-max))))
807 (set (make-local-variable 'minibuffer-temporary-goal-position)
808 (cond ((<= (point) prompt-end) prompt-end)
811 (goto-char (point-max))
813 (setq minibuffer-history-position narg)
815 (setq elt minibuffer-default))
817 (setq elt (or minibuffer-text-before-history ""))
818 (setq minibuffer-returned-to-present t)
819 (setq minibuffer-text-before-history nil))
820 (t (setq elt (nth (1- minibuffer-history-position)
821 (symbol-value minibuffer-history-variable)))))
823 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
824 (not minibuffer-returned-to-present))
825 (let ((print-level nil))
826 (prin1-to-string elt))
828 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
830 (defun previous-history-element (n)
831 "Inserts the previous element of the minibuffer history into the minibuffer."
833 (next-history-element (- n)))
835 (defun next-complete-history-element (n)
836 "Get next history element which completes the minibuffer before the point.
837 The contents of the minibuffer after the point are deleted, and replaced
838 by the new completion."
840 (let ((point-at-start (point)))
841 (next-matching-history-element
843 "^" (regexp-quote (buffer-substring (field-beginning) (point))))
845 ;; next-matching-history-element always puts us at (point-min).
846 ;; Move to the position we were at before changing the buffer contents.
847 ;; This is still sensical, because the text before point has not changed.
848 (goto-char point-at-start)))
850 (defun previous-complete-history-element (n)
852 Get previous history element which completes the minibuffer before the point.
853 The contents of the minibuffer after the point are deleted, and replaced
854 by the new completion."
856 (next-complete-history-element (- n)))
858 ;; These two functions are for compatibility with the old subrs of the
861 (defun minibuffer-prompt-width ()
862 "Return the display width of the minibuffer prompt.
863 Return 0 if current buffer is not a mini-buffer."
864 ;; Return the width of everything before the field at the end of
865 ;; the buffer; this should be 0 for normal buffers.
866 (1- (field-beginning (point-max))))
868 (defun minibuffer-prompt-end ()
869 "Return the buffer position of the end of the minibuffer prompt.
870 Return 0 if current buffer is not a mini-buffer."
871 (field-beginning (point-max)))
874 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
875 (defalias 'advertised-undo 'undo)
877 (defun undo (&optional arg)
878 "Undo some previous changes.
879 Repeat this command to undo more changes.
880 A numeric argument serves as a repeat count.
882 In Transient Mark mode when the mark is active, only undo changes within
883 the current region. Similarly, when not in Transient Mark mode, just C-u
884 as an argument limits undo to changes within the current region."
886 ;; If we don't get all the way thru, make last-command indicate that
887 ;; for the following command.
888 (setq this-command t)
889 (let ((modified (buffer-modified-p))
890 (recent-save (recent-auto-save-p)))
891 (or (eq (selected-window) (minibuffer-window))
893 (unless (eq last-command 'undo)
894 (if (if transient-mark-mode mark-active (and arg (not (numberp arg))))
895 (undo-start (region-beginning) (region-end))
897 ;; get rid of initial undo boundary
900 (if (or transient-mark-mode (numberp arg))
901 (prefix-numeric-value arg)
903 ;; Don't specify a position in the undo record for the undo command.
904 ;; Instead, undoing this should move point to where the change is.
905 (let ((tail buffer-undo-list)
907 (while (and tail (not done) (not (null (car tail))))
908 (if (integerp (car tail))
911 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
912 (setq tail (cdr tail))))
913 (and modified (not (buffer-modified-p))
914 (delete-auto-save-file-if-necessary recent-save)))
915 ;; If we do get all the way thru, make this-command indicate that.
916 (setq this-command 'undo))
918 (defvar pending-undo-list nil
919 "Within a run of consecutive undo commands, list remaining to be undone.")
921 (defvar undo-in-progress nil
922 "Non-nil while performing an undo.
923 Some change-hooks test this variable to do something different.")
925 (defun undo-more (count)
926 "Undo back N undo-boundaries beyond what was already undone recently.
927 Call `undo-start' to get ready to undo recent changes,
928 then call `undo-more' one or more times to undo them."
929 (or pending-undo-list
930 (error "No further undo information"))
931 (let ((undo-in-progress t))
932 (setq pending-undo-list (primitive-undo count pending-undo-list))))
934 ;; Deep copy of a list
935 (defun undo-copy-list (list)
936 "Make a copy of undo list LIST."
937 (mapcar 'undo-copy-list-1 list))
939 (defun undo-copy-list-1 (elt)
941 (cons (car elt) (undo-copy-list-1 (cdr elt)))
944 (defun undo-start (&optional beg end)
945 "Set `pending-undo-list' to the front of the undo list.
946 The next call to `undo-more' will undo the most recently made change.
947 If BEG and END are specified, then only undo elements
948 that apply to text between BEG and END are used; other undo elements
949 are ignored. If BEG and END are nil, all undo elements are used."
950 (if (eq buffer-undo-list t)
951 (error "No undo information in this buffer"))
952 (setq pending-undo-list
953 (if (and beg end (not (= beg end)))
954 (undo-make-selective-list (min beg end) (max beg end))
957 (defvar undo-adjusted-markers)
959 (defun undo-make-selective-list (start end)
960 "Return a list of undo elements for the region START to END.
961 The elements come from `buffer-undo-list', but we keep only
962 the elements inside this region, and discard those outside this region.
963 If we find an element that crosses an edge of this region,
964 we stop and ignore all further elements."
965 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
966 (undo-list (list nil))
967 undo-adjusted-markers
969 undo-elt undo-elt temp-undo-list delta)
970 (while undo-list-copy
971 (setq undo-elt (car undo-list-copy))
973 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
974 ;; This is a "was unmodified" element.
975 ;; Keep it if we have kept everything thus far.
978 (undo-elt-in-region undo-elt start end)))))
981 (setq end (+ end (cdr (undo-delta undo-elt))))
982 ;; Don't put two nils together in the list
983 (if (not (and (eq (car undo-list) nil)
985 (setq undo-list (cons undo-elt undo-list))))
986 (if (undo-elt-crosses-region undo-elt start end)
987 (setq undo-list-copy nil)
988 (setq some-rejected t)
989 (setq temp-undo-list (cdr undo-list-copy))
990 (setq delta (undo-delta undo-elt))
992 (when (/= (cdr delta) 0)
993 (let ((position (car delta))
994 (offset (cdr delta)))
996 ;; Loop down the earlier events adjusting their buffer positions
997 ;; to reflect the fact that a change to the buffer isn't being
998 ;; undone. We only need to process those element types which
999 ;; undo-elt-in-region will return as being in the region since
1000 ;; only those types can ever get into the output
1002 (while temp-undo-list
1003 (setq undo-elt (car temp-undo-list))
1004 (cond ((integerp undo-elt)
1005 (if (>= undo-elt position)
1006 (setcar temp-undo-list (- undo-elt offset))))
1007 ((atom undo-elt) nil)
1008 ((stringp (car undo-elt))
1009 ;; (TEXT . POSITION)
1010 (let ((text-pos (abs (cdr undo-elt)))
1011 (point-at-end (< (cdr undo-elt) 0 )))
1012 (if (>= text-pos position)
1013 (setcdr undo-elt (* (if point-at-end -1 1)
1014 (- text-pos offset))))))
1015 ((integerp (car undo-elt))
1017 (when (>= (car undo-elt) position)
1018 (setcar undo-elt (- (car undo-elt) offset))
1019 (setcdr undo-elt (- (cdr undo-elt) offset))))
1020 ((null (car undo-elt))
1021 ;; (nil PROPERTY VALUE BEG . END)
1022 (let ((tail (nthcdr 3 undo-elt)))
1023 (when (>= (car tail) position)
1024 (setcar tail (- (car tail) offset))
1025 (setcdr tail (- (cdr tail) offset))))))
1026 (setq temp-undo-list (cdr temp-undo-list))))))))
1027 (setq undo-list-copy (cdr undo-list-copy)))
1028 (nreverse undo-list)))
1030 (defun undo-elt-in-region (undo-elt start end)
1031 "Determine whether UNDO-ELT falls inside the region START ... END.
1032 If it crosses the edge, we return nil."
1033 (cond ((integerp undo-elt)
1034 (and (>= undo-elt start)
1040 ((stringp (car undo-elt))
1041 ;; (TEXT . POSITION)
1042 (and (>= (abs (cdr undo-elt)) start)
1043 (< (abs (cdr undo-elt)) end)))
1044 ((and (consp undo-elt) (markerp (car undo-elt)))
1045 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1046 ;; See if MARKER is inside the region.
1047 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1049 (setq alist-elt (cons (car undo-elt)
1050 (marker-position (car undo-elt))))
1051 (setq undo-adjusted-markers
1052 (cons alist-elt undo-adjusted-markers)))
1053 (and (cdr alist-elt)
1054 (>= (cdr alist-elt) start)
1055 (< (cdr alist-elt) end))))
1056 ((null (car undo-elt))
1057 ;; (nil PROPERTY VALUE BEG . END)
1058 (let ((tail (nthcdr 3 undo-elt)))
1059 (and (>= (car tail) start)
1060 (< (cdr tail) end))))
1061 ((integerp (car undo-elt))
1063 (and (>= (car undo-elt) start)
1064 (< (cdr undo-elt) end)))))
1066 (defun undo-elt-crosses-region (undo-elt start end)
1067 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1068 This assumes we have already decided that UNDO-ELT
1069 is not *inside* the region START...END."
1070 (cond ((atom undo-elt) nil)
1071 ((null (car undo-elt))
1072 ;; (nil PROPERTY VALUE BEG . END)
1073 (let ((tail (nthcdr 3 undo-elt)))
1074 (not (or (< (car tail) end)
1075 (> (cdr tail) start)))))
1076 ((integerp (car undo-elt))
1078 (not (or (< (car undo-elt) end)
1079 (> (cdr undo-elt) start))))))
1081 ;; Return the first affected buffer position and the delta for an undo element
1082 ;; delta is defined as the change in subsequent buffer positions if we *did*
1084 (defun undo-delta (undo-elt)
1085 (if (consp undo-elt)
1086 (cond ((stringp (car undo-elt))
1087 ;; (TEXT . POSITION)
1088 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1089 ((integerp (car undo-elt))
1091 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1096 (defvar shell-command-history nil
1097 "History list for some commands that read shell commands.")
1099 (defvar shell-command-switch "-c"
1100 "Switch used to have the shell execute its command line argument.")
1102 (defvar shell-command-default-error-buffer nil
1103 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1104 This buffer is used when `shell-command' or 'shell-command-on-region'
1105 is run interactively. A value of nil means that output to stderr and
1106 stdout will be intermixed in the output stream.")
1108 (defun shell-command (command &optional output-buffer error-buffer)
1109 "Execute string COMMAND in inferior shell; display output, if any.
1111 If COMMAND ends in ampersand, execute it asynchronously.
1112 The output appears in the buffer `*Async Shell Command*'.
1113 That buffer is in shell mode.
1115 Otherwise, COMMAND is executed synchronously. The output appears in the
1116 buffer `*Shell Command Output*'. If the output is short enough to
1117 display in the echo area (which is determined by the variable
1118 `max-mini-window-height'), it is shown there, but it is nonetheless
1119 available in buffer `*Shell Command Output*' even though that buffer is
1120 not automatically displayed. If there is no output, or if output is
1121 inserted in the current buffer, then `*Shell Command Output*' is
1124 To specify a coding system for converting non-ASCII characters
1125 in the shell command output, use \\[universal-coding-system-argument]
1126 before this command.
1128 Noninteractive callers can specify coding systems by binding
1129 `coding-system-for-read' and `coding-system-for-write'.
1131 The optional second argument OUTPUT-BUFFER, if non-nil,
1132 says to put the output in some other buffer.
1133 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1134 If OUTPUT-BUFFER is not a buffer and not nil,
1135 insert output in current buffer. (This cannot be done asynchronously.)
1136 In either case, the output is inserted after point (leaving mark after it).
1138 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1139 or buffer name to which to direct the command's standard error output.
1140 If it is nil, error output is mingled with regular output.
1141 In an interactive call, the variable `shell-command-default-error-buffer'
1142 specifies the value of ERROR-BUFFER."
1144 (interactive (list (read-from-minibuffer "Shell command: "
1145 nil nil nil 'shell-command-history)
1147 shell-command-default-error-buffer))
1148 ;; Look for a handler in case default-directory is a remote file name.
1150 (find-file-name-handler (directory-file-name default-directory)
1153 (funcall handler 'shell-command command output-buffer error-buffer)
1154 (if (and output-buffer
1155 (not (or (bufferp output-buffer) (stringp output-buffer))))
1159 (expand-file-name "scor"
1160 (or small-temporary-file-directory
1161 temporary-file-directory)))
1163 (barf-if-buffer-read-only)
1165 ;; We do not use -f for csh; we will not support broken use of
1166 ;; .cshrcs. Even the BSD csh manual says to use
1167 ;; "if ($?prompt) exit" before things which are not useful
1168 ;; non-interactively. Besides, if someone wants their other
1169 ;; aliases for shell commands then they can still have them.
1170 (call-process shell-file-name nil
1174 nil shell-command-switch command)
1175 (when (and error-file (file-exists-p error-file))
1176 (if (< 0 (nth 7 (file-attributes error-file)))
1177 (with-current-buffer (get-buffer-create error-buffer)
1178 (let ((pos-from-end (- (point-max) (point))))
1181 ;; Do no formatting while reading error file,
1182 ;; because that can run a shell command, and we
1183 ;; don't want that to cause an infinite recursion.
1184 (format-insert-file error-file nil)
1185 ;; Put point after the inserted errors.
1186 (goto-char (- (point-max) pos-from-end)))
1187 (display-buffer (current-buffer))))
1188 (delete-file error-file))
1189 ;; This is like exchange-point-and-mark, but doesn't
1190 ;; activate the mark. It is cleaner to avoid activation,
1191 ;; even though the command loop would deactivate the mark
1192 ;; because we inserted text.
1193 (goto-char (prog1 (mark t)
1194 (set-marker (mark-marker) (point)
1195 (current-buffer)))))
1196 ;; Preserve the match data in case called from a program.
1198 (if (string-match "[ \t]*&[ \t]*$" command)
1199 ;; Command ending with ampersand means asynchronous.
1200 (let ((buffer (get-buffer-create
1201 (or output-buffer "*Async Shell Command*")))
1202 (directory default-directory)
1204 ;; Remove the ampersand.
1205 (setq command (substring command 0 (match-beginning 0)))
1206 ;; If will kill a process, query first.
1207 (setq proc (get-buffer-process buffer))
1209 (if (yes-or-no-p "A command is running. Kill it? ")
1211 (error "Shell command in progress")))
1214 (setq buffer-read-only nil)
1216 (display-buffer buffer)
1217 (setq default-directory directory)
1218 (setq proc (start-process "Shell" buffer shell-file-name
1219 shell-command-switch command))
1220 (setq mode-line-process '(":%s"))
1221 (require 'shell) (shell-mode)
1222 (set-process-sentinel proc 'shell-command-sentinel)
1224 (shell-command-on-region (point) (point) command
1225 output-buffer nil error-buffer)))))))
1227 (defun display-message-or-buffer (message
1228 &optional buffer-name not-this-window frame)
1229 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1230 MESSAGE may be either a string or a buffer.
1232 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1233 the maximum height of the echo area, as defined by `max-mini-window-height'.
1235 Returns either the string shown in the echo area, or when a pop-up
1236 buffer is used, the window used to display it.
1238 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1239 name of the buffer used to display it in the case where a pop-up buffer
1240 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1241 string and it is displayed in the echo area, it is not specified whether
1242 the contents are inserted into the buffer anyway.
1244 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1245 and only used if a buffer is displayed."
1246 (cond ((and (stringp message) (not (string-match "\n" message)))
1247 ;; Trivial case where we can use the echo area
1248 (message "%s" message))
1249 ((and (stringp message)
1250 (= (string-match "\n" message) (1- (length message))))
1251 ;; Trivial case where we can just remove single trailing newline
1252 (message "%s" (substring message 0 (1- (length message)))))
1255 (with-current-buffer
1256 (if (bufferp message)
1258 (get-buffer-create (or buffer-name "*Message*")))
1260 (unless (bufferp message)
1265 (if (= (buffer-size) 0)
1267 (count-lines (point-min) (point-max)))))
1268 (cond ((or (<= lines 1)
1270 (cond ((floatp max-mini-window-height)
1271 (* (frame-height) max-mini-window-height))
1272 ((integerp max-mini-window-height)
1273 max-mini-window-height)
1277 (goto-char (point-max))
1280 (message "%s" (buffer-substring (point-min) (point))))
1283 (goto-char (point-min))
1284 (display-buffer message not-this-window frame))))))))
1287 ;; We have a sentinel to prevent insertion of a termination message
1288 ;; in the buffer itself.
1289 (defun shell-command-sentinel (process signal)
1290 (if (memq (process-status process) '(exit signal))
1292 (car (cdr (cdr (process-command process))))
1293 (substring signal 0 -1))))
1295 (defun shell-command-on-region (start end command
1296 &optional output-buffer replace
1298 "Execute string COMMAND in inferior shell with region as input.
1299 Normally display output (if any) in temp buffer `*Shell Command Output*';
1300 Prefix arg means replace the region with it. Return the exit code of
1303 To specify a coding system for converting non-ASCII characters
1304 in the input and output to the shell command, use \\[universal-coding-system-argument]
1305 before this command. By default, the input (from the current buffer)
1306 is encoded in the same coding system that will be used to save the file,
1307 `buffer-file-coding-system'. If the output is going to replace the region,
1308 then it is decoded from that same coding system.
1310 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1311 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1312 systems by binding `coding-system-for-read' and
1313 `coding-system-for-write'.
1315 If the output is short enough to display in the echo area (which is
1316 determined by the variable `max-mini-window-height'), it is shown there,
1317 but it is nonetheless available in buffer `*Shell Command Output*' even
1318 though that buffer is not automatically displayed. If there is no
1319 output, or if output is inserted in the current buffer, then `*Shell
1320 Command Output*' is deleted.
1322 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1323 that says to put the output in some other buffer.
1324 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1325 If OUTPUT-BUFFER is not a buffer and not nil,
1326 insert output in the current buffer.
1327 In either case, the output is inserted after point (leaving mark after it).
1329 If REPLACE, the optional fifth argument, is non-nil, that means insert
1330 the output in place of text from START to END, putting point and mark
1333 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1334 or buffer name to which to direct the command's standard error output.
1335 If it is nil, error output is mingled with regular output.
1336 In an interactive call, the variable `shell-command-default-error-buffer'
1337 specifies the value of ERROR-BUFFER."
1338 (interactive (let ((string
1339 ;; Do this before calling region-beginning
1340 ;; and region-end, in case subprocess output
1341 ;; relocates them while we are in the minibuffer.
1342 (read-from-minibuffer "Shell command on region: "
1344 'shell-command-history)))
1345 ;; call-interactively recognizes region-beginning and
1346 ;; region-end specially, leaving them in the history.
1347 (list (region-beginning) (region-end)
1351 shell-command-default-error-buffer)))
1355 (expand-file-name "scor"
1356 (or small-temporary-file-directory
1357 temporary-file-directory)))
1362 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1363 ;; Replace specified region with output from command.
1364 (let ((swap (and replace (< start end))))
1365 ;; Don't muck with mark unless REPLACE says we should.
1367 (and replace (push-mark))
1369 (call-process-region start end shell-file-name t
1373 nil shell-command-switch command))
1374 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1375 (and shell-buffer (not (eq shell-buffer (current-buffer)))
1376 (kill-buffer shell-buffer)))
1377 ;; Don't muck with mark unless REPLACE says we should.
1378 (and replace swap (exchange-point-and-mark)))
1379 ;; No prefix argument: put the output in a temp buffer,
1380 ;; replacing its entire contents.
1381 (let ((buffer (get-buffer-create
1382 (or output-buffer "*Shell Command Output*")))
1385 (if (eq buffer (current-buffer))
1386 ;; If the input is the same buffer as the output,
1387 ;; delete everything but the specified region,
1388 ;; then replace that region with the output.
1389 (progn (setq buffer-read-only nil)
1390 (delete-region (max start end) (point-max))
1391 (delete-region (point-min) (min start end))
1393 (call-process-region (point-min) (point-max)
1398 nil shell-command-switch
1400 ;; Clear the output buffer, then run the command with
1402 (let ((directory default-directory))
1405 (setq buffer-read-only nil)
1406 (if (not output-buffer)
1407 (setq default-directory directory))
1410 (call-process-region start end shell-file-name nil
1412 (list buffer error-file)
1414 nil shell-command-switch command)))
1415 (setq success (and exit-status (equal 0 exit-status)))
1416 ;; Report the amount of output.
1417 (if (with-current-buffer buffer (> (point-max) (point-min)))
1418 ;; There's some output, display it
1419 (display-message-or-buffer buffer)
1420 ;; No output; error?
1421 (message (if (and error-file
1422 (< 0 (nth 7 (file-attributes error-file))))
1423 "(Shell command %sed with some error output)"
1424 "(Shell command %sed with no output)")
1425 (if (equal 0 exit-status) "succeed" "fail"))
1426 (kill-buffer buffer)))))
1428 (when (and error-file (file-exists-p error-file))
1429 (if (< 0 (nth 7 (file-attributes error-file)))
1430 (with-current-buffer (get-buffer-create error-buffer)
1431 (let ((pos-from-end (- (point-max) (point))))
1434 ;; Do no formatting while reading error file,
1435 ;; because that can run a shell command, and we
1436 ;; don't want that to cause an infinite recursion.
1437 (format-insert-file error-file nil)
1438 ;; Put point after the inserted errors.
1439 (goto-char (- (point-max) pos-from-end)))
1440 (display-buffer (current-buffer))))
1441 (delete-file error-file))
1444 (defun shell-command-to-string (command)
1445 "Execute shell command COMMAND and return its output as a string."
1446 (with-output-to-string
1447 (with-current-buffer
1449 (call-process shell-file-name nil t nil shell-command-switch command))))
1451 (defvar universal-argument-map
1452 (let ((map (make-sparse-keymap)))
1453 (define-key map [t] 'universal-argument-other-key)
1454 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1455 (define-key map [switch-frame] nil)
1456 (define-key map [?\C-u] 'universal-argument-more)
1457 (define-key map [?-] 'universal-argument-minus)
1458 (define-key map [?0] 'digit-argument)
1459 (define-key map [?1] 'digit-argument)
1460 (define-key map [?2] 'digit-argument)
1461 (define-key map [?3] 'digit-argument)
1462 (define-key map [?4] 'digit-argument)
1463 (define-key map [?5] 'digit-argument)
1464 (define-key map [?6] 'digit-argument)
1465 (define-key map [?7] 'digit-argument)
1466 (define-key map [?8] 'digit-argument)
1467 (define-key map [?9] 'digit-argument)
1468 (define-key map [kp-0] 'digit-argument)
1469 (define-key map [kp-1] 'digit-argument)
1470 (define-key map [kp-2] 'digit-argument)
1471 (define-key map [kp-3] 'digit-argument)
1472 (define-key map [kp-4] 'digit-argument)
1473 (define-key map [kp-5] 'digit-argument)
1474 (define-key map [kp-6] 'digit-argument)
1475 (define-key map [kp-7] 'digit-argument)
1476 (define-key map [kp-8] 'digit-argument)
1477 (define-key map [kp-9] 'digit-argument)
1478 (define-key map [kp-subtract] 'universal-argument-minus)
1480 "Keymap used while processing \\[universal-argument].")
1482 (defvar universal-argument-num-events nil
1483 "Number of argument-specifying events read by `universal-argument'.
1484 `universal-argument-other-key' uses this to discard those events
1485 from (this-command-keys), and reread only the final command.")
1487 (defun universal-argument ()
1488 "Begin a numeric argument for the following command.
1489 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1490 \\[universal-argument] following the digits or minus sign ends the argument.
1491 \\[universal-argument] without digits or minus sign provides 4 as argument.
1492 Repeating \\[universal-argument] without digits or minus sign
1493 multiplies the argument by 4 each time.
1494 For some commands, just \\[universal-argument] by itself serves as a flag
1495 which is different in effect from any particular numeric argument.
1496 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1498 (setq prefix-arg (list 4))
1499 (setq universal-argument-num-events (length (this-command-keys)))
1500 (setq overriding-terminal-local-map universal-argument-map))
1502 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1503 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1504 (defun universal-argument-more (arg)
1507 (setq prefix-arg (list (* 4 (car arg))))
1509 (setq prefix-arg (list -4))
1510 (setq prefix-arg arg)
1511 (setq overriding-terminal-local-map nil)))
1512 (setq universal-argument-num-events (length (this-command-keys))))
1514 (defun negative-argument (arg)
1515 "Begin a negative numeric argument for the next command.
1516 \\[universal-argument] following digits or minus sign ends the argument."
1518 (cond ((integerp arg)
1519 (setq prefix-arg (- arg)))
1521 (setq prefix-arg nil))
1523 (setq prefix-arg '-)))
1524 (setq universal-argument-num-events (length (this-command-keys)))
1525 (setq overriding-terminal-local-map universal-argument-map))
1527 (defun digit-argument (arg)
1528 "Part of the numeric argument for the next command.
1529 \\[universal-argument] following digits or minus sign ends the argument."
1531 (let* ((char (if (integerp last-command-char)
1533 (get last-command-char 'ascii-character)))
1534 (digit (- (logand char ?\177) ?0)))
1535 (cond ((integerp arg)
1536 (setq prefix-arg (+ (* arg 10)
1537 (if (< arg 0) (- digit) digit))))
1539 ;; Treat -0 as just -, so that -01 will work.
1540 (setq prefix-arg (if (zerop digit) '- (- digit))))
1542 (setq prefix-arg digit))))
1543 (setq universal-argument-num-events (length (this-command-keys)))
1544 (setq overriding-terminal-local-map universal-argument-map))
1546 ;; For backward compatibility, minus with no modifiers is an ordinary
1547 ;; command if digits have already been entered.
1548 (defun universal-argument-minus (arg)
1551 (universal-argument-other-key arg)
1552 (negative-argument arg)))
1554 ;; Anything else terminates the argument and is left in the queue to be
1555 ;; executed as a command.
1556 (defun universal-argument-other-key (arg)
1558 (setq prefix-arg arg)
1559 (let* ((key (this-command-keys))
1560 (keylist (listify-key-sequence key)))
1561 (setq unread-command-events
1562 (append (nthcdr universal-argument-num-events keylist)
1563 unread-command-events)))
1564 (reset-this-command-lengths)
1565 (setq overriding-terminal-local-map nil))
1567 ;;;; Window system cut and paste hooks.
1569 (defvar interprogram-cut-function nil
1570 "Function to call to make a killed region available to other programs.
1572 Most window systems provide some sort of facility for cutting and
1573 pasting text between the windows of different programs.
1574 This variable holds a function that Emacs calls whenever text
1575 is put in the kill ring, to make the new kill available to other
1578 The function takes one or two arguments.
1579 The first argument, TEXT, is a string containing
1580 the text which should be made available.
1581 The second, PUSH, if non-nil means this is a \"new\" kill;
1582 nil means appending to an \"old\" kill.")
1584 (defvar interprogram-paste-function nil
1585 "Function to call to get text cut from other programs.
1587 Most window systems provide some sort of facility for cutting and
1588 pasting text between the windows of different programs.
1589 This variable holds a function that Emacs calls to obtain
1590 text that other programs have provided for pasting.
1592 The function should be called with no arguments. If the function
1593 returns nil, then no other program has provided such text, and the top
1594 of the Emacs kill ring should be used. If the function returns a
1595 string, that string should be put in the kill ring as the latest kill.
1597 Note that the function should return a string only if a program other
1598 than Emacs has provided a string for pasting; if Emacs provided the
1599 most recent string, the function should return nil. If it is
1600 difficult to tell whether Emacs or some other program provided the
1601 current string, it is probably good enough to return nil if the string
1602 is equal (according to `string=') to the last text Emacs provided.")
1606 ;;;; The kill ring data structure.
1608 (defvar kill-ring nil
1609 "List of killed text sequences.
1610 Since the kill ring is supposed to interact nicely with cut-and-paste
1611 facilities offered by window systems, use of this variable should
1612 interact nicely with `interprogram-cut-function' and
1613 `interprogram-paste-function'. The functions `kill-new',
1614 `kill-append', and `current-kill' are supposed to implement this
1615 interaction; you may want to use them instead of manipulating the kill
1618 (defcustom kill-ring-max 60
1619 "*Maximum length of kill ring before oldest elements are thrown away."
1623 (defvar kill-ring-yank-pointer nil
1624 "The tail of the kill ring whose car is the last thing yanked.")
1626 (defun kill-new (string &optional replace)
1627 "Make STRING the latest kill in the kill ring.
1628 Set the kill-ring-yank pointer to point to it.
1629 If `interprogram-cut-function' is non-nil, apply it to STRING.
1630 Optional second argument REPLACE non-nil means that STRING will replace
1631 the front of the kill ring, rather than being added to the list."
1632 (and (fboundp 'menu-bar-update-yank-menu)
1633 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1635 (setcar kill-ring string)
1636 (setq kill-ring (cons string kill-ring))
1637 (if (> (length kill-ring) kill-ring-max)
1638 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1639 (setq kill-ring-yank-pointer kill-ring)
1640 (if interprogram-cut-function
1641 (funcall interprogram-cut-function string (not replace))))
1643 (defun kill-append (string before-p)
1644 "Append STRING to the end of the latest kill in the kill ring.
1645 If BEFORE-P is non-nil, prepend STRING to the kill.
1646 If `interprogram-cut-function' is set, pass the resulting kill to
1648 (kill-new (if before-p
1649 (concat string (car kill-ring))
1650 (concat (car kill-ring) string)) t))
1652 (defun current-kill (n &optional do-not-move)
1653 "Rotate the yanking point by N places, and then return that kill.
1654 If N is zero, `interprogram-paste-function' is set, and calling it
1655 returns a string, then that string is added to the front of the
1656 kill ring and returned as the latest kill.
1657 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1658 yanking point; just return the Nth kill forward."
1659 (let ((interprogram-paste (and (= n 0)
1660 interprogram-paste-function
1661 (funcall interprogram-paste-function))))
1662 (if interprogram-paste
1664 ;; Disable the interprogram cut function when we add the new
1665 ;; text to the kill ring, so Emacs doesn't try to own the
1666 ;; selection, with identical text.
1667 (let ((interprogram-cut-function nil))
1668 (kill-new interprogram-paste))
1670 (or kill-ring (error "Kill ring is empty"))
1671 (let ((ARGth-kill-element
1672 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1676 (setq kill-ring-yank-pointer ARGth-kill-element))
1677 (car ARGth-kill-element)))))
1681 ;;;; Commands for manipulating the kill ring.
1683 (defcustom kill-read-only-ok nil
1684 "*Non-nil means don't signal an error for killing read-only text."
1688 (put 'text-read-only 'error-conditions
1689 '(text-read-only buffer-read-only error))
1690 (put 'text-read-only 'error-message "Text is read-only")
1692 (defun kill-region (beg end)
1693 "Kill between point and mark.
1694 The text is deleted but saved in the kill ring.
1695 The command \\[yank] can retrieve it from there.
1696 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
1697 If the buffer is read-only, Emacs will beep and refrain from deleting
1698 the text, but put the text in the kill ring anyway. This means that
1699 you can use the killing commands to copy text from a read-only buffer.
1701 This is the primitive for programs to kill text (as opposed to deleting it).
1702 Supply two arguments, character numbers indicating the stretch of text
1704 Any command that calls this function is a \"kill command\".
1705 If the previous command was also a kill command,
1706 the text killed this time appends to the text killed last time
1707 to make one entry in the kill ring."
1710 (let ((string (delete-and-extract-region beg end)))
1711 (when string ;STRING is nil if BEG = END
1712 ;; Add that string to the kill ring, one way or another.
1713 (if (eq last-command 'kill-region)
1714 (kill-append string (< end beg))
1716 (setq this-command 'kill-region))
1717 ((buffer-read-only text-read-only)
1718 ;; The code above failed because the buffer, or some of the characters
1719 ;; in the region, are read-only.
1720 ;; We should beep, in case the user just isn't aware of this.
1721 ;; However, there's no harm in putting
1722 ;; the region's text in the kill ring, anyway.
1723 (copy-region-as-kill beg end)
1724 ;; Set this-command now, so it will be set even if we get an error.
1725 (setq this-command 'kill-region)
1726 ;; This should barf, if appropriate, and give us the correct error.
1727 (if kill-read-only-ok
1728 (message "Read only text copied to kill ring")
1729 ;; Signal an error if the buffer is read-only.
1730 (barf-if-buffer-read-only)
1731 ;; If the buffer isn't read-only, the text is.
1732 (signal 'text-read-only (list (current-buffer)))))))
1734 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1735 ;; to get two copies of the text when the user accidentally types M-w and
1736 ;; then corrects it with the intended C-w.
1737 (defun copy-region-as-kill (beg end)
1738 "Save the region as if killed, but don't kill it.
1739 In Transient Mark mode, deactivate the mark.
1740 If `interprogram-cut-function' is non-nil, also save the text for a window
1741 system cut and paste."
1743 (if (eq last-command 'kill-region)
1744 (kill-append (buffer-substring beg end) (< end beg))
1745 (kill-new (buffer-substring beg end)))
1746 (if transient-mark-mode
1747 (setq deactivate-mark t))
1750 (defun kill-ring-save (beg end)
1751 "Save the region as if killed, but don't kill it.
1752 In Transient Mark mode, deactivate the mark.
1753 If `interprogram-cut-function' is non-nil, also save the text for a window
1754 system cut and paste.
1756 This command is similar to `copy-region-as-kill', except that it gives
1757 visual feedback indicating the extent of the region being copied."
1759 (copy-region-as-kill beg end)
1761 (let ((other-end (if (= (point) beg) end beg))
1763 ;; Inhibit quitting so we can make a quit here
1764 ;; look like a C-g typed as a command.
1766 (if (pos-visible-in-window-p other-end (selected-window))
1768 ;; Swap point and mark.
1769 (set-marker (mark-marker) (point) (current-buffer))
1770 (goto-char other-end)
1773 (set-marker (mark-marker) other-end (current-buffer))
1775 ;; If user quit, deactivate the mark
1776 ;; as C-g would as a command.
1777 (and quit-flag mark-active
1779 (let* ((killed-text (current-kill 0))
1780 (message-len (min (length killed-text) 40)))
1782 ;; Don't say "killed"; that is misleading.
1783 (message "Saved text until \"%s\""
1784 (substring killed-text (- message-len)))
1785 (message "Saved text from \"%s\""
1786 (substring killed-text 0 message-len))))))))
1788 (defun append-next-kill (&optional interactive)
1789 "Cause following command, if it kills, to append to previous kill.
1790 The argument is used for internal purposes; do not supply one."
1792 ;; We don't use (interactive-p), since that breaks kbd macros.
1795 (setq this-command 'kill-region)
1796 (message "If the next command is a kill, it will append"))
1797 (setq last-command 'kill-region)))
1801 (defun yank-pop (arg)
1802 "Replace just-yanked stretch of killed text with a different stretch.
1803 This command is allowed only immediately after a `yank' or a `yank-pop'.
1804 At such a time, the region contains a stretch of reinserted
1805 previously-killed text. `yank-pop' deletes that text and inserts in its
1806 place a different stretch of killed text.
1808 With no argument, the previous kill is inserted.
1809 With argument N, insert the Nth previous kill.
1810 If N is negative, this is a more recent kill.
1812 The sequence of kills wraps around, so that after the oldest one
1813 comes the newest one."
1815 (if (not (eq last-command 'yank))
1816 (error "Previous command was not a yank"))
1817 (setq this-command 'yank)
1818 (let ((inhibit-read-only t)
1819 (before (< (point) (mark t))))
1820 (delete-region (point) (mark t))
1821 (set-marker (mark-marker) (point) (current-buffer))
1822 (let ((opoint (point)))
1823 (insert (current-kill arg))
1824 (let ((inhibit-read-only t))
1825 (remove-text-properties opoint (point) '(read-only nil))))
1827 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1828 ;; It is cleaner to avoid activation, even though the command
1829 ;; loop would deactivate the mark because we inserted text.
1830 (goto-char (prog1 (mark t)
1831 (set-marker (mark-marker) (point) (current-buffer))))))
1834 (defun yank (&optional arg)
1835 "Reinsert the last stretch of killed text.
1836 More precisely, reinsert the stretch of killed text most recently
1837 killed OR yanked. Put point at end, and set mark at beginning.
1838 With just C-u as argument, same but put point at beginning (and mark at end).
1839 With argument N, reinsert the Nth most recently killed stretch of killed
1841 See also the command \\[yank-pop]."
1843 ;; If we don't get all the way thru, make last-command indicate that
1844 ;; for the following command.
1845 (setq this-command t)
1847 (let ((opoint (point)))
1848 (insert (current-kill (cond
1852 (let ((inhibit-read-only t))
1853 (remove-text-properties opoint (point) '(read-only nil))))
1855 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1856 ;; It is cleaner to avoid activation, even though the command
1857 ;; loop would deactivate the mark because we inserted text.
1858 (goto-char (prog1 (mark t)
1859 (set-marker (mark-marker) (point) (current-buffer)))))
1860 ;; If we do get all the way thru, make this-command indicate that.
1861 (setq this-command 'yank)
1864 (defun rotate-yank-pointer (arg)
1865 "Rotate the yanking point in the kill ring.
1866 With argument, rotate that many kills forward (or backward, if negative)."
1870 ;; Some kill commands.
1872 ;; Internal subroutine of delete-char
1873 (defun kill-forward-chars (arg)
1874 (if (listp arg) (setq arg (car arg)))
1875 (if (eq arg '-) (setq arg -1))
1876 (kill-region (point) (forward-point arg)))
1878 ;; Internal subroutine of backward-delete-char
1879 (defun kill-backward-chars (arg)
1880 (if (listp arg) (setq arg (car arg)))
1881 (if (eq arg '-) (setq arg -1))
1882 (kill-region (point) (forward-point (- arg))))
1884 (defcustom backward-delete-char-untabify-method 'untabify
1885 "*The method for untabifying when deleting backward.
1886 Can be `untabify' -- turn a tab to many spaces, then delete one space;
1887 `hungry' -- delete all whitespace, both tabs and spaces;
1888 `all' -- delete all whitespace, including tabs, spaces and newlines;
1889 nil -- just delete one character."
1890 :type '(choice (const untabify) (const hungry) (const all) (const nil))
1893 (defun backward-delete-char-untabify (arg &optional killp)
1894 "Delete characters backward, changing tabs into spaces.
1895 The exact behavior depends on `backward-delete-char-untabify-method'.
1896 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
1897 Interactively, ARG is the prefix arg (default 1)
1898 and KILLP is t if a prefix arg was specified."
1899 (interactive "*p\nP")
1900 (when (eq backward-delete-char-untabify-method 'untabify)
1903 (while (and (> count 0) (not (bobp)))
1904 (if (= (preceding-char) ?\t)
1905 (let ((col (current-column)))
1907 (setq col (- col (current-column)))
1908 (insert-char ?\ col)
1911 (setq count (1- count))))))
1912 (delete-backward-char
1913 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
1914 ((eq backward-delete-char-untabify-method 'all)
1917 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
1919 (+ arg (if (zerop wh) 0 (1- wh))))
1923 (defun zap-to-char (arg char)
1924 "Kill up to and including ARG'th occurrence of CHAR.
1925 Case is ignored if `case-fold-search' is non-nil in the current buffer.
1926 Goes backward if ARG is negative; error if CHAR not found."
1927 (interactive "p\ncZap to char: ")
1928 (kill-region (point) (progn
1929 (search-forward (char-to-string char) nil nil arg)
1930 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
1933 ;; kill-line and its subroutines.
1935 (defcustom kill-whole-line nil
1936 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
1940 (defun kill-line (&optional arg)
1941 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
1942 With prefix argument, kill that many lines from point.
1943 Negative arguments kill lines backward.
1944 With zero argument, kills the text before point on the current line.
1946 When calling from a program, nil means \"no arg\",
1947 a number counts as a prefix arg.
1949 To kill a whole line, when point is not at the beginning, type \
1950 \\[beginning-of-line] \\[kill-line] \\[kill-line].
1952 If `kill-whole-line' is non-nil, then this command kills the whole line
1953 including its terminating newline, when used at the beginning of a line
1954 with no argument. As a consequence, you can always kill a whole line
1955 by typing \\[beginning-of-line] \\[kill-line]."
1957 (kill-region (point)
1958 ;; It is better to move point to the other end of the kill
1959 ;; before killing. That way, in a read-only buffer, point
1960 ;; moves across the text that is copied to the kill ring.
1961 ;; The choice has no effect on undo now that undo records
1962 ;; the value of point from before the command was run.
1965 (forward-visible-line (prefix-numeric-value arg))
1967 (signal 'end-of-buffer nil))
1968 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
1969 (forward-visible-line 1)
1970 (end-of-visible-line)))
1973 (defun forward-visible-line (arg)
1974 "Move forward by ARG lines, ignoring currently invisible newlines only.
1975 If ARG is negative, move backward -ARG lines.
1976 If ARG is zero, move to the beginning of the current line."
1980 (or (zerop (forward-line 1))
1981 (signal 'end-of-buffer nil))
1982 ;; If the following character is currently invisible,
1983 ;; skip all characters with that same `invisible' property value,
1984 ;; then find the next newline.
1985 (while (and (not (eobp))
1987 (get-char-property (point) 'invisible)))
1988 (if (eq buffer-invisibility-spec t)
1990 (or (memq prop buffer-invisibility-spec)
1991 (assq prop buffer-invisibility-spec)))))
1993 (if (get-text-property (point) 'invisible)
1994 (or (next-single-property-change (point) 'invisible)
1996 (next-overlay-change (point))))
1997 (or (zerop (forward-line 1))
1998 (signal 'end-of-buffer nil)))
1999 (setq arg (1- arg)))
2001 (while (or first (< arg 0))
2004 (or (zerop (forward-line -1))
2005 (signal 'beginning-of-buffer nil)))
2006 (while (and (not (bobp))
2008 (get-char-property (1- (point)) 'invisible)))
2009 (if (eq buffer-invisibility-spec t)
2011 (or (memq prop buffer-invisibility-spec)
2012 (assq prop buffer-invisibility-spec)))))
2014 (if (get-text-property (1- (point)) 'invisible)
2015 (or (previous-single-property-change (point) 'invisible)
2017 (previous-overlay-change (point))))
2018 (or (zerop (forward-line -1))
2019 (signal 'beginning-of-buffer nil)))
2021 (setq arg (1+ arg)))))
2022 ((beginning-of-buffer end-of-buffer)
2025 (defun end-of-visible-line ()
2026 "Move to end of current visible line."
2028 ;; If the following character is currently invisible,
2029 ;; skip all characters with that same `invisible' property value,
2030 ;; then find the next newline.
2031 (while (and (not (eobp))
2033 (get-char-property (point) 'invisible)))
2034 (if (eq buffer-invisibility-spec t)
2036 (or (memq prop buffer-invisibility-spec)
2037 (assq prop buffer-invisibility-spec)))))
2038 (if (get-text-property (point) 'invisible)
2039 (goto-char (next-single-property-change (point) 'invisible))
2040 (goto-char (next-overlay-change (point))))
2043 (defun insert-buffer (buffer)
2044 "Insert after point the contents of BUFFER.
2045 Puts mark after the inserted text.
2046 BUFFER may be a buffer or a buffer name.
2048 This function is meant for the user to run interactively.
2049 Don't call it from programs!"
2053 (barf-if-buffer-read-only)
2054 (read-buffer "Insert buffer: "
2055 (if (eq (selected-window) (next-window (selected-window)))
2056 (other-buffer (current-buffer))
2057 (window-buffer (next-window (selected-window))))
2059 (or (bufferp buffer)
2060 (setq buffer (get-buffer buffer)))
2061 (let (start end newmark)
2065 (setq start (point-min) end (point-max)))
2066 (insert-buffer-substring buffer start end)
2067 (setq newmark (point)))
2068 (push-mark newmark))
2071 (defun append-to-buffer (buffer start end)
2072 "Append to specified buffer the text of the region.
2073 It is inserted into that buffer before its point.
2075 When calling from a program, give three arguments:
2076 BUFFER (or buffer name), START and END.
2077 START and END specify the portion of the current buffer to be copied."
2079 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2080 (region-beginning) (region-end)))
2081 (let ((oldbuf (current-buffer)))
2083 (let* ((append-to (get-buffer-create buffer))
2084 (windows (get-buffer-window-list append-to t t))
2086 (set-buffer append-to)
2087 (setq point (point))
2088 (barf-if-buffer-read-only)
2089 (insert-buffer-substring oldbuf start end)
2090 (dolist (window windows)
2091 (when (= (window-point window) point)
2092 (set-window-point window (point))))))))
2094 (defun prepend-to-buffer (buffer start end)
2095 "Prepend to specified buffer the text of the region.
2096 It is inserted into that buffer after its point.
2098 When calling from a program, give three arguments:
2099 BUFFER (or buffer name), START and END.
2100 START and END specify the portion of the current buffer to be copied."
2101 (interactive "BPrepend to buffer: \nr")
2102 (let ((oldbuf (current-buffer)))
2104 (set-buffer (get-buffer-create buffer))
2105 (barf-if-buffer-read-only)
2107 (insert-buffer-substring oldbuf start end)))))
2109 (defun copy-to-buffer (buffer start end)
2110 "Copy to specified buffer the text of the region.
2111 It is inserted into that buffer, replacing existing text there.
2113 When calling from a program, give three arguments:
2114 BUFFER (or buffer name), START and END.
2115 START and END specify the portion of the current buffer to be copied."
2116 (interactive "BCopy to buffer: \nr")
2117 (let ((oldbuf (current-buffer)))
2119 (set-buffer (get-buffer-create buffer))
2120 (barf-if-buffer-read-only)
2123 (insert-buffer-substring oldbuf start end)))))
2125 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2126 (put 'mark-inactive 'error-message "The mark is not active now")
2128 (defun mark (&optional force)
2129 "Return this buffer's mark value as integer; error if mark inactive.
2130 If optional argument FORCE is non-nil, access the mark value
2131 even if the mark is not currently active, and return nil
2132 if there is no mark at all.
2134 If you are using this in an editing command, you are most likely making
2135 a mistake; see the documentation of `set-mark'."
2136 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2137 (marker-position (mark-marker))
2138 (signal 'mark-inactive nil)))
2140 ;; Many places set mark-active directly, and several of them failed to also
2141 ;; run deactivate-mark-hook. This shorthand should simplify.
2142 (defsubst deactivate-mark ()
2143 "Deactivate the mark by setting `mark-active' to nil.
2144 \(That makes a difference only in Transient Mark mode.)
2145 Also runs the hook `deactivate-mark-hook'."
2146 (if transient-mark-mode
2148 (setq mark-active nil)
2149 (run-hooks 'deactivate-mark-hook))))
2151 (defun set-mark (pos)
2152 "Set this buffer's mark to POS. Don't use this function!
2153 That is to say, don't use this function unless you want
2154 the user to see that the mark has moved, and you want the previous
2155 mark position to be lost.
2157 Normally, when a new mark is set, the old one should go on the stack.
2158 This is why most applications should use push-mark, not set-mark.
2160 Novice Emacs Lisp programmers often try to use the mark for the wrong
2161 purposes. The mark saves a location for the user's convenience.
2162 Most editing commands should not alter the mark.
2163 To remember a location for internal use in the Lisp program,
2164 store it in a Lisp variable. Example:
2166 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2170 (setq mark-active t)
2171 (run-hooks 'activate-mark-hook)
2172 (set-marker (mark-marker) pos (current-buffer)))
2173 ;; Normally we never clear mark-active except in Transient Mark mode.
2174 ;; But when we actually clear out the mark value too,
2175 ;; we must clear mark-active in any mode.
2176 (setq mark-active nil)
2177 (run-hooks 'deactivate-mark-hook)
2178 (set-marker (mark-marker) nil)))
2180 (defvar mark-ring nil
2181 "The list of former marks of the current buffer, most recent first.")
2182 (make-variable-buffer-local 'mark-ring)
2183 (put 'mark-ring 'permanent-local t)
2185 (defcustom mark-ring-max 16
2186 "*Maximum size of mark ring. Start discarding off end if gets this big."
2188 :group 'editing-basics)
2190 (defvar global-mark-ring nil
2191 "The list of saved global marks, most recent first.")
2193 (defcustom global-mark-ring-max 16
2194 "*Maximum size of global mark ring. \
2195 Start discarding off end if gets this big."
2197 :group 'editing-basics)
2199 (defun set-mark-command (arg)
2200 "Set mark at where point is, or jump to mark.
2201 With no prefix argument, set mark, push old mark position on local mark
2202 ring, and push mark on global mark ring.
2203 With argument, jump to mark, and pop a new position for mark off the ring
2204 \(does not affect global mark ring\).
2206 Novice Emacs Lisp programmers often try to use the mark for the wrong
2207 purposes. See the documentation of `set-mark' for more information."
2211 (push-mark nil nil t))
2213 (error "No mark set in this buffer")
2214 (goto-char (mark t))
2217 (defun push-mark (&optional location nomsg activate)
2218 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2219 If the last global mark pushed was not in the current buffer,
2220 also push LOCATION on the global mark ring.
2221 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2222 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2224 Novice Emacs Lisp programmers often try to use the mark for the wrong
2225 purposes. See the documentation of `set-mark' for more information.
2227 In Transient Mark mode, this does not activate the mark."
2230 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2231 (if (> (length mark-ring) mark-ring-max)
2233 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2234 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
2235 (set-marker (mark-marker) (or location (point)) (current-buffer))
2236 ;; Now push the mark on the global mark ring.
2237 (if (and global-mark-ring
2238 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2239 ;; The last global mark pushed was in this same buffer.
2240 ;; Don't push another one.
2242 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2243 (if (> (length global-mark-ring) global-mark-ring-max)
2245 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
2247 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2248 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2249 (message "Mark set"))
2250 (if (or activate (not transient-mark-mode))
2251 (set-mark (mark t)))
2255 "Pop off mark ring into the buffer's actual mark.
2256 Does not set point. Does nothing if mark ring is empty."
2259 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2260 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2262 (move-marker (car mark-ring) nil)
2263 (if (null (mark t)) (ding))
2264 (setq mark-ring (cdr mark-ring)))))
2266 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2267 (defun exchange-point-and-mark ()
2268 "Put the mark where point is now, and point where the mark is now.
2269 This command works even when the mark is not active,
2270 and it reactivates the mark."
2272 (let ((omark (mark t)))
2274 (error "No mark set in this buffer"))
2279 (defun transient-mark-mode (arg)
2280 "Toggle Transient Mark mode.
2281 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2283 In Transient Mark mode, when the mark is active, the region is highlighted.
2284 Changing the buffer \"deactivates\" the mark.
2285 So do certain other operations that set the mark
2286 but whose main purpose is something else--for example,
2287 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
2289 (setq transient-mark-mode
2291 (not transient-mark-mode)
2292 (> (prefix-numeric-value arg) 0)))
2294 (if transient-mark-mode
2295 (message "Transient Mark mode enabled")
2296 (message "Transient Mark mode disabled"))))
2298 (defun pop-global-mark ()
2299 "Pop off global mark ring and jump to the top location."
2301 ;; Pop entries which refer to non-existent buffers.
2302 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2303 (setq global-mark-ring (cdr global-mark-ring)))
2304 (or global-mark-ring
2305 (error "No global mark set"))
2306 (let* ((marker (car global-mark-ring))
2307 (buffer (marker-buffer marker))
2308 (position (marker-position marker)))
2309 (setq global-mark-ring (nconc (cdr global-mark-ring)
2310 (list (car global-mark-ring))))
2312 (or (and (>= position (point-min))
2313 (<= position (point-max)))
2315 (goto-char position)
2316 (switch-to-buffer buffer)))
2318 (defcustom next-line-add-newlines t
2319 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2321 :group 'editing-basics)
2323 (defun next-line (arg)
2324 "Move cursor vertically down ARG lines.
2325 If there is no character in the target line exactly under the current column,
2326 the cursor is positioned after the character in that line which spans this
2327 column, or at the end of the line if it is not long enough.
2328 If there is no line in the buffer after this one, behavior depends on the
2329 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2330 to create a line, and moves the cursor to that line. Otherwise it moves the
2331 cursor to the end of the buffer.
2333 The command \\[set-goal-column] can be used to create
2334 a semipermanent goal column for this command.
2335 Then instead of trying to move exactly vertically (or as close as possible),
2336 this command moves to the specified goal column (or as close as possible).
2337 The goal column is stored in the variable `goal-column', which is nil
2338 when there is no goal column.
2340 If you are thinking of using this in a Lisp program, consider
2341 using `forward-line' instead. It is usually easier to use
2342 and more reliable (no dependence on goal column, etc.)."
2344 (if (and next-line-add-newlines (= arg 1))
2345 (let ((opoint (point)))
2354 ((beginning-of-buffer end-of-buffer) (ding)))
2358 (defun previous-line (arg)
2359 "Move cursor vertically up ARG lines.
2360 If there is no character in the target line exactly over the current column,
2361 the cursor is positioned after the character in that line which spans this
2362 column, or at the end of the line if it is not long enough.
2364 The command \\[set-goal-column] can be used to create
2365 a semipermanent goal column for this command.
2366 Then instead of trying to move exactly vertically (or as close as possible),
2367 this command moves to the specified goal column (or as close as possible).
2368 The goal column is stored in the variable `goal-column', which is nil
2369 when there is no goal column.
2371 If you are thinking of using this in a Lisp program, consider using
2372 `forward-line' with a negative argument instead. It is usually easier
2373 to use and more reliable (no dependence on goal column, etc.)."
2378 ((beginning-of-buffer end-of-buffer) (ding)))
2379 (line-move (- arg)))
2382 (defcustom track-eol nil
2383 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2384 This means moving to the end of each line moved onto.
2385 The beginning of a blank line does not count as the end of a line."
2387 :group 'editing-basics)
2389 (defcustom goal-column nil
2390 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2391 :type '(choice integer
2392 (const :tag "None" nil))
2393 :group 'editing-basics)
2394 (make-variable-buffer-local 'goal-column)
2396 (defvar temporary-goal-column 0
2397 "Current goal column for vertical motion.
2398 It is the column where point was
2399 at the start of current run of vertical motion commands.
2400 When the `track-eol' feature is doing its job, the value is 9999.")
2402 (defcustom line-move-ignore-invisible nil
2403 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2404 Outline mode sets this."
2406 :group 'editing-basics)
2408 ;; This is the guts of next-line and previous-line.
2409 ;; Arg says how many lines to move.
2410 (defun line-move (arg)
2411 ;; Don't run any point-motion hooks, and disregard intangibility,
2412 ;; for intermediate positions.
2413 (let ((inhibit-point-motion-hooks t)
2415 new line-end line-beg)
2418 (if (not (or (eq last-command 'next-line)
2419 (eq last-command 'previous-line)))
2420 (setq temporary-goal-column
2421 (if (and track-eol (eolp)
2422 ;; Don't count beg of empty line as end of line
2423 ;; unless we just did explicit end-of-line.
2424 (or (not (bolp)) (eq last-command 'end-of-line)))
2427 (if (and (not (integerp selective-display))
2428 (not line-move-ignore-invisible))
2429 ;; Use just newline characters.
2431 (progn (if (> arg 1) (forward-line (1- arg)))
2432 ;; This way of moving forward ARG lines
2433 ;; verifies that we have a newline after the last one.
2434 ;; It doesn't get confused by intangible text.
2436 (zerop (forward-line 1)))
2437 (and (zerop (forward-line arg))
2439 (signal (if (< arg 0)
2440 'beginning-of-buffer
2443 ;; Move by arg lines, but ignore invisible ones.
2446 (and (zerop (vertical-motion 1))
2447 (signal 'end-of-buffer nil))
2448 ;; If the following character is currently invisible,
2449 ;; skip all characters with that same `invisible' property value.
2450 (while (and (not (eobp))
2452 (get-char-property (point) 'invisible)))
2453 (if (eq buffer-invisibility-spec t)
2455 (or (memq prop buffer-invisibility-spec)
2456 (assq prop buffer-invisibility-spec)))))
2457 (if (get-text-property (point) 'invisible)
2458 (goto-char (next-single-property-change (point) 'invisible))
2459 (goto-char (next-overlay-change (point)))))
2460 (setq arg (1- arg)))
2463 (and (zerop (vertical-motion -1))
2464 (signal 'beginning-of-buffer nil))
2465 (while (and (not (bobp))
2467 (get-char-property (1- (point)) 'invisible)))
2468 (if (eq buffer-invisibility-spec t)
2470 (or (memq prop buffer-invisibility-spec)
2471 (assq prop buffer-invisibility-spec)))))
2472 (if (get-text-property (1- (point)) 'invisible)
2473 (goto-char (previous-single-property-change (point) 'invisible))
2474 (goto-char (previous-overlay-change (point)))))
2475 (setq arg (1+ arg))))
2476 (let ((buffer-invisibility-spec nil))
2477 (move-to-column (or goal-column temporary-goal-column))))
2479 ;; If we are moving into some intangible text,
2480 ;; look for following text on the same line which isn't intangible
2482 (setq line-end (save-excursion (end-of-line) (point)))
2483 (setq line-beg (save-excursion (beginning-of-line) (point)))
2484 (let ((after (and (< new (point-max))
2485 (get-char-property new 'intangible)))
2486 (before (and (> new (point-min))
2487 (get-char-property (1- new) 'intangible))))
2488 (when (and before (eq before after)
2490 (goto-char (point-min))
2491 (let ((inhibit-point-motion-hooks nil))
2493 (if (<= new line-end)
2494 (setq new (point)))))
2495 ;; NEW is where we want to move to.
2496 ;; LINE-BEG and LINE-END are the beginning and end of the line.
2497 ;; Move there in just one step, from our starting position,
2498 ;; with intangibility and point-motion hooks enabled this time.
2500 (setq inhibit-point-motion-hooks nil)
2501 (goto-char (constrain-to-field new opoint nil t
2502 'inhibit-line-move-field-capture))
2503 ;; If intangibility processing moved us to a different line,
2504 ;; readjust the horizontal position within the line we ended up at.
2505 (when (or (< (point) line-beg) (> (point) line-end))
2507 (setq inhibit-point-motion-hooks t)
2508 (setq line-end (save-excursion (end-of-line) (point)))
2510 (setq line-beg (point))
2511 (let ((buffer-invisibility-spec nil))
2512 (move-to-column (or goal-column temporary-goal-column)))
2513 (if (<= (point) line-end)
2515 (goto-char (point-min))
2516 (setq inhibit-point-motion-hooks nil)
2517 (goto-char (constrain-to-field new opoint nil t
2518 'inhibit-line-move-field-capture))
2522 ;;; Many people have said they rarely use this feature, and often type
2523 ;;; it by accident. Maybe it shouldn't even be on a key.
2524 (put 'set-goal-column 'disabled t)
2526 (defun set-goal-column (arg)
2527 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2528 Those commands will move to this position in the line moved to
2529 rather than trying to keep the same horizontal position.
2530 With a non-nil argument, clears out the goal column
2531 so that \\[next-line] and \\[previous-line] resume vertical motion.
2532 The goal column is stored in the variable `goal-column'."
2536 (setq goal-column nil)
2537 (message "No goal column"))
2538 (setq goal-column (current-column))
2539 (message (substitute-command-keys
2540 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2545 (defun scroll-other-window-down (lines)
2546 "Scroll the \"other window\" down.
2547 For more details, see the documentation for `scroll-other-window'."
2549 (scroll-other-window
2550 ;; Just invert the argument's meaning.
2551 ;; We can do that without knowing which window it will be.
2552 (if (eq lines '-) nil
2554 (- (prefix-numeric-value lines))))))
2555 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2557 (defun beginning-of-buffer-other-window (arg)
2558 "Move point to the beginning of the buffer in the other window.
2559 Leave mark at previous position.
2560 With arg N, put point N/10 of the way from the true beginning."
2562 (let ((orig-window (selected-window))
2563 (window (other-window-for-scrolling)))
2564 ;; We use unwind-protect rather than save-window-excursion
2565 ;; because the latter would preserve the things we want to change.
2568 (select-window window)
2569 ;; Set point and mark in that window's buffer.
2570 (beginning-of-buffer arg)
2571 ;; Set point accordingly.
2573 (select-window orig-window))))
2575 (defun end-of-buffer-other-window (arg)
2576 "Move point to the end of the buffer in the other window.
2577 Leave mark at previous position.
2578 With arg N, put point N/10 of the way from the true end."
2580 ;; See beginning-of-buffer-other-window for comments.
2581 (let ((orig-window (selected-window))
2582 (window (other-window-for-scrolling)))
2585 (select-window window)
2588 (select-window orig-window))))
2590 (defun transpose-chars (arg)
2591 "Interchange characters around point, moving forward one character.
2592 With prefix arg ARG, effect is to take character before point
2593 and drag it forward past ARG other characters (backward if ARG negative).
2594 If no argument and at end of line, the previous two chars are exchanged."
2596 (and (null arg) (eolp) (forward-char -1))
2597 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2599 (defun transpose-words (arg)
2600 "Interchange words around point, leaving point at end of them.
2601 With prefix arg ARG, effect is to take word before or around point
2602 and drag it forward past ARG other words (backward if ARG negative).
2603 If ARG is zero, the words around or after point and around or after mark
2606 (transpose-subr 'forward-word arg))
2608 (defun transpose-sexps (arg)
2609 "Like \\[transpose-words] but applies to sexps.
2610 Does not work on a sexp that point is in the middle of
2611 if it is a list or string."
2613 (transpose-subr 'forward-sexp arg))
2615 (defun transpose-lines (arg)
2616 "Exchange current line and previous line, leaving point after both.
2617 With argument ARG, takes previous line and moves it past ARG lines.
2618 With argument 0, interchanges line point is in with line mark is in."
2620 (transpose-subr (function
2624 ;; Move forward over ARG lines,
2625 ;; but create newlines if necessary.
2626 (setq arg (forward-line arg))
2627 (if (/= (preceding-char) ?\n)
2628 (setq arg (1+ arg)))
2631 (forward-line arg))))
2634 (defvar transpose-subr-start1)
2635 (defvar transpose-subr-start2)
2636 (defvar transpose-subr-end1)
2637 (defvar transpose-subr-end2)
2639 (defun transpose-subr (mover arg)
2640 (let (transpose-subr-start1
2642 transpose-subr-start2
2643 transpose-subr-end2)
2648 (setq transpose-subr-end2 (point))
2650 (setq transpose-subr-start2 (point))
2653 (setq transpose-subr-end1 (point))
2655 (setq transpose-subr-start1 (point))
2657 (exchange-point-and-mark))
2661 (setq transpose-subr-start1 (point))
2663 (setq transpose-subr-end1 (point))
2665 (setq transpose-subr-end2 (point))
2666 (funcall mover (- arg))
2667 (setq transpose-subr-start2 (point))
2669 (goto-char transpose-subr-end2))
2671 (setq transpose-subr-start2 (point))
2673 (setq transpose-subr-end2 (point))
2674 (funcall mover (1- arg))
2675 (setq transpose-subr-start1 (point))
2676 (funcall mover (- arg))
2677 (setq transpose-subr-end1 (point))
2678 (transpose-subr-1)))))
2680 (defun transpose-subr-1 ()
2681 (if (> (min transpose-subr-end1 transpose-subr-end2)
2682 (max transpose-subr-start1 transpose-subr-start2))
2683 (error "Don't have two things to transpose"))
2684 (let* ((word1 (buffer-substring transpose-subr-start1 transpose-subr-end1))
2685 (len1 (length word1))
2686 (word2 (buffer-substring transpose-subr-start2 transpose-subr-end2))
2687 (len2 (length word2)))
2688 (delete-region transpose-subr-start2 transpose-subr-end2)
2689 (goto-char transpose-subr-start2)
2691 (goto-char (if (< transpose-subr-start1 transpose-subr-start2)
2692 transpose-subr-start1
2693 (+ transpose-subr-start1 (- len1 len2))))
2694 (delete-region (point) (+ (point) len1))
2697 (defun backward-word (arg)
2698 "Move backward until encountering the end of a word.
2699 With argument, do this that many times."
2701 (forward-word (- arg)))
2703 (defun mark-word (arg)
2704 "Set mark arg words away from point."
2712 (defun kill-word (arg)
2713 "Kill characters forward until encountering the end of a word.
2714 With argument, do this that many times."
2716 (kill-region (point) (progn (forward-word arg) (point))))
2718 (defun backward-kill-word (arg)
2719 "Kill characters backward until encountering the end of a word.
2720 With argument, do this that many times."
2722 (kill-word (- arg)))
2724 (defun current-word (&optional strict)
2725 "Return the word point is on (or a nearby word) as a string.
2726 If optional arg STRICT is non-nil, return nil unless point is within
2727 or adjacent to a word."
2729 (let ((oldpoint (point)) (start (point)) (end (point)))
2730 (skip-syntax-backward "w_") (setq start (point))
2731 (goto-char oldpoint)
2732 (skip-syntax-forward "w_") (setq end (point))
2733 (if (and (eq start oldpoint) (eq end oldpoint))
2734 ;; Point is neither within nor adjacent to a word.
2737 ;; Look for preceding word in same line.
2738 (skip-syntax-backward "^w_"
2739 (save-excursion (beginning-of-line)
2742 ;; No preceding word in same line.
2743 ;; Look for following word in same line.
2745 (skip-syntax-forward "^w_"
2746 (save-excursion (end-of-line)
2748 (setq start (point))
2749 (skip-syntax-forward "w_")
2752 (skip-syntax-backward "w_")
2753 (setq start (point)))
2754 (buffer-substring-no-properties start end)))
2755 (buffer-substring-no-properties start end)))))
2757 (defcustom fill-prefix nil
2758 "*String for filling to insert at front of new line, or nil for none.
2759 Setting this variable automatically makes it local to the current buffer."
2760 :type '(choice (const :tag "None" nil)
2763 (make-variable-buffer-local 'fill-prefix)
2765 (defcustom auto-fill-inhibit-regexp nil
2766 "*Regexp to match lines which should not be auto-filled."
2767 :type '(choice (const :tag "None" nil)
2771 (defvar comment-line-break-function 'indent-new-comment-line
2772 "*Mode-specific function which line breaks and continues a comment.
2774 This function is only called during auto-filling of a comment section.
2775 The function should take a single optional argument, which is a flag
2776 indicating whether it should use soft newlines.
2778 Setting this variable automatically makes it local to the current buffer.")
2780 ;; This function is used as the auto-fill-function of a buffer
2781 ;; when Auto-Fill mode is enabled.
2782 ;; It returns t if it really did any work.
2783 ;; (Actually some major modes use a different auto-fill function,
2784 ;; but this one is the default one.)
2785 (defun do-auto-fill ()
2786 (let (fc justify bol give-up
2787 (fill-prefix fill-prefix))
2788 (if (or (not (setq justify (current-justification)))
2789 (null (setq fc (current-fill-column)))
2790 (and (eq justify 'left)
2791 (<= (current-column) fc))
2792 (save-excursion (beginning-of-line)
2794 (and auto-fill-inhibit-regexp
2795 (looking-at auto-fill-inhibit-regexp))))
2796 nil ;; Auto-filling not required
2797 (if (memq justify '(full center right))
2798 (save-excursion (unjustify-current-line)))
2800 ;; Choose a fill-prefix automatically.
2801 (if (and adaptive-fill-mode
2802 (or (null fill-prefix) (string= fill-prefix "")))
2804 (fill-context-prefix
2805 (save-excursion (backward-paragraph 1) (point))
2806 (save-excursion (forward-paragraph 1) (point)))))
2807 (and prefix (not (equal prefix ""))
2808 (setq fill-prefix prefix))))
2810 (while (and (not give-up) (> (current-column) fc))
2811 ;; Determine where to split the line.
2814 (let ((opoint (point))
2819 (setq after-prefix (point))
2821 (looking-at (regexp-quote fill-prefix))
2822 (setq after-prefix (match-end 0)))
2823 (move-to-column (1+ fc))
2824 ;; Move back to the point where we can break the line.
2825 ;; We break the line between word or
2826 ;; after/before the character which has character
2827 ;; category `|'. We search space, \c| followed by
2828 ;; a character, or \c| following a character. If
2829 ;; not found, place the point at beginning of line.
2831 ;; If this is after period and a single space,
2832 ;; move back once more--we don't want to break
2833 ;; the line there and make it look like a
2837 sentence-end-double-space
2838 (save-excursion (forward-char -1)
2839 (and (looking-at "\\. ")
2840 (not (looking-at "\\. ")))))
2843 fill-nobreak-predicate
2844 (funcall fill-nobreak-predicate)))
2846 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
2847 ;; If we find nowhere on the line to break it,
2848 ;; break after one word. Set bounce to t
2849 ;; so we will not keep going in this while loop.
2850 (if (<= (point) after-prefix)
2852 (goto-char after-prefix)
2853 (re-search-forward "[ \t]" opoint t)
2855 (if (looking-at "[ \t]")
2856 ;; Break the line at word boundary.
2857 (skip-chars-backward " \t")
2858 ;; Break the line after/before \c|.
2860 (if enable-multibyte-characters
2861 ;; If we are going to break the line after or
2862 ;; before a non-ascii character, we may have
2863 ;; to run a special function for the charset
2864 ;; of the character to find the correct break
2866 (if (not (and (eq (charset-after (1- (point))) 'ascii)
2867 (eq (charset-after (point)) 'ascii)))
2868 (fill-find-break-point after-prefix)))
2870 ;; Let fill-point be set to the place where we end up.
2871 ;; But move back before any whitespace here.
2872 (skip-chars-backward " \t")
2875 ;; See whether the place we found is any good.
2877 (goto-char fill-point)
2879 ;; There is no use breaking at end of line.
2880 (not (save-excursion (skip-chars-forward " ") (eolp)))
2881 ;; It is futile to split at the end of the prefix
2882 ;; since we would just insert the prefix again.
2883 (not (and after-prefix (<= (point) after-prefix)))
2884 ;; Don't split right after a comment starter
2885 ;; since we would just make another comment starter.
2886 (not (and comment-start-skip
2887 (let ((limit (point)))
2889 (and (re-search-forward comment-start-skip
2891 (eq (point) limit)))))))
2892 ;; Ok, we have a useful place to break the line. Do it.
2893 (let ((prev-column (current-column)))
2894 ;; If point is at the fill-point, do not `save-excursion'.
2895 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2896 ;; point will end up before it rather than after it.
2898 (skip-chars-backward " \t")
2899 (= (point) fill-point))
2900 (funcall comment-line-break-function t)
2902 (goto-char fill-point)
2903 (funcall comment-line-break-function t)))
2904 ;; Now do justification, if required
2905 (if (not (eq justify 'left))
2908 (justify-current-line justify nil t)))
2909 ;; If making the new line didn't reduce the hpos of
2910 ;; the end of the line, then give up now;
2911 ;; trying again will not help.
2912 (if (>= (current-column) prev-column)
2914 ;; No good place to break => stop trying.
2916 ;; Justify last line.
2917 (justify-current-line justify t t)
2920 (defvar normal-auto-fill-function 'do-auto-fill
2921 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
2922 Some major modes set this.")
2924 (defun auto-fill-mode (&optional arg)
2925 "Toggle Auto Fill mode.
2926 With arg, turn Auto Fill mode on if and only if arg is positive.
2927 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
2928 automatically breaks the line at a previous space.
2930 The value of `normal-auto-fill-function' specifies the function to use
2931 for `auto-fill-function' when turning Auto Fill mode on."
2933 (prog1 (setq auto-fill-function
2935 (not auto-fill-function)
2936 (> (prefix-numeric-value arg) 0))
2937 normal-auto-fill-function
2939 (force-mode-line-update)))
2941 ;; This holds a document string used to document auto-fill-mode.
2942 (defun auto-fill-function ()
2943 "Automatically break line at a previous space, in insertion of text."
2946 (defun turn-on-auto-fill ()
2947 "Unconditionally turn on Auto Fill mode."
2950 (defun turn-off-auto-fill ()
2951 "Unconditionally turn off Auto Fill mode."
2952 (auto-fill-mode -1))
2954 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
2956 (defun set-fill-column (arg)
2957 "Set `fill-column' to specified argument.
2958 Use \\[universal-argument] followed by a number to specify a column.
2959 Just \\[universal-argument] as argument means to use the current column."
2962 (setq arg (current-column)))
2963 (if (not (integerp arg))
2964 ;; Disallow missing argument; it's probably a typo for C-x C-f.
2965 (error "set-fill-column requires an explicit argument")
2966 (message "Fill column set to %d (was %d)" arg fill-column)
2967 (setq fill-column arg)))
2969 (defun set-selective-display (arg)
2970 "Set `selective-display' to ARG; clear it if no arg.
2971 When the value of `selective-display' is a number > 0,
2972 lines whose indentation is >= that value are not displayed.
2973 The variable `selective-display' has a separate value for each buffer."
2975 (if (eq selective-display t)
2976 (error "selective-display already in use for marked lines"))
2979 (narrow-to-region (point-min) (point))
2980 (goto-char (window-start))
2981 (vertical-motion (window-height)))))
2982 (setq selective-display
2983 (and arg (prefix-numeric-value arg)))
2984 (recenter current-vpos))
2985 (set-window-start (selected-window) (window-start (selected-window)))
2986 (princ "selective-display set to " t)
2987 (prin1 selective-display t)
2990 (defvar overwrite-mode-textual " Ovwrt"
2991 "The string displayed in the mode line when in overwrite mode.")
2992 (defvar overwrite-mode-binary " Bin Ovwrt"
2993 "The string displayed in the mode line when in binary overwrite mode.")
2995 (defun overwrite-mode (arg)
2996 "Toggle overwrite mode.
2997 With arg, turn overwrite mode on iff arg is positive.
2998 In overwrite mode, printing characters typed in replace existing text
2999 on a one-for-one basis, rather than pushing it to the right. At the
3000 end of a line, such characters extend the line. Before a tab,
3001 such characters insert until the tab is filled in.
3002 \\[quoted-insert] still inserts characters in overwrite mode; this
3003 is supposed to make it easier to insert characters when necessary."
3005 (setq overwrite-mode
3006 (if (if (null arg) (not overwrite-mode)
3007 (> (prefix-numeric-value arg) 0))
3008 'overwrite-mode-textual))
3009 (force-mode-line-update))
3011 (defun binary-overwrite-mode (arg)
3012 "Toggle binary overwrite mode.
3013 With arg, turn binary overwrite mode on iff arg is positive.
3014 In binary overwrite mode, printing characters typed in replace
3015 existing text. Newlines are not treated specially, so typing at the
3016 end of a line joins the line to the next, with the typed character
3017 between them. Typing before a tab character simply replaces the tab
3018 with the character typed.
3019 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3020 typing characters do.
3022 Note that binary overwrite mode is not its own minor mode; it is a
3023 specialization of overwrite-mode, entered by setting the
3024 `overwrite-mode' variable to `overwrite-mode-binary'."
3026 (setq overwrite-mode
3028 (not (eq overwrite-mode 'overwrite-mode-binary))
3029 (> (prefix-numeric-value arg) 0))
3030 'overwrite-mode-binary))
3031 (force-mode-line-update))
3033 (defcustom line-number-mode t
3034 "*Non-nil means display line number in mode line."
3036 :group 'editing-basics)
3038 (defun line-number-mode (arg)
3039 "Toggle Line Number mode.
3040 With arg, turn Line Number mode on iff arg is positive.
3041 When Line Number mode is enabled, the line number appears
3044 Line numbers do not appear for very large buffers, see variable
3045 `line-number-display-limit'."
3047 (setq line-number-mode
3048 (if (null arg) (not line-number-mode)
3049 (> (prefix-numeric-value arg) 0)))
3050 (force-mode-line-update))
3052 (defcustom column-number-mode nil
3053 "*Non-nil means display column number in mode line."
3055 :group 'editing-basics)
3057 (defun column-number-mode (arg)
3058 "Toggle Column Number mode.
3059 With arg, turn Column Number mode on iff arg is positive.
3060 When Column Number mode is enabled, the column number appears
3063 (setq column-number-mode
3064 (if (null arg) (not column-number-mode)
3065 (> (prefix-numeric-value arg) 0)))
3066 (force-mode-line-update))
3068 (defgroup paren-blinking nil
3069 "Blinking matching of parens and expressions."
3070 :prefix "blink-matching-"
3071 :group 'paren-matching)
3073 (defcustom blink-matching-paren t
3074 "*Non-nil means show matching open-paren when close-paren is inserted."
3076 :group 'paren-blinking)
3078 (defcustom blink-matching-paren-on-screen t
3079 "*Non-nil means show matching open-paren when it is on screen.
3080 If nil, means don't show it (but the open-paren can still be shown
3081 when it is off screen)."
3083 :group 'paren-blinking)
3085 (defcustom blink-matching-paren-distance (* 25 1024)
3086 "*If non-nil, is maximum distance to search for matching open-paren."
3088 :group 'paren-blinking)
3090 (defcustom blink-matching-delay 1
3091 "*Time in seconds to delay after showing a matching paren."
3093 :group 'paren-blinking)
3095 (defcustom blink-matching-paren-dont-ignore-comments nil
3096 "*Non-nil means `blink-matching-paren' will not ignore comments."
3098 :group 'paren-blinking)
3100 (defun blink-matching-open ()
3101 "Move cursor momentarily to the beginning of the sexp before point."
3103 (and (> (point) (1+ (point-min)))
3104 blink-matching-paren
3105 ;; Verify an even number of quoting characters precede the close.
3106 (= 1 (logand 1 (- (point)
3109 (skip-syntax-backward "/\\")
3111 (let* ((oldpos (point))
3116 (if blink-matching-paren-distance
3117 (narrow-to-region (max (point-min)
3118 (- (point) blink-matching-paren-distance))
3121 (let ((parse-sexp-ignore-comments
3122 (and parse-sexp-ignore-comments
3123 (not blink-matching-paren-dont-ignore-comments))))
3124 (setq blinkpos (scan-sexps oldpos -1)))
3127 (/= (char-syntax (char-after blinkpos))
3130 (or (null (matching-paren (char-after blinkpos)))
3131 (/= (char-after (1- oldpos))
3132 (matching-paren (char-after blinkpos))))))
3133 (if mismatch (setq blinkpos nil))
3135 ;; Don't log messages about paren matching.
3136 (let (message-log-max)
3137 (goto-char blinkpos)
3138 (if (pos-visible-in-window-p)
3139 (and blink-matching-paren-on-screen
3140 (sit-for blink-matching-delay))
3141 (goto-char blinkpos)
3144 ;; Show what precedes the open in its line, if anything.
3146 (skip-chars-backward " \t")
3148 (buffer-substring (progn (beginning-of-line) (point))
3150 ;; Show what follows the open in its line, if anything.
3153 (skip-chars-forward " \t")
3155 (buffer-substring blinkpos
3156 (progn (end-of-line) (point)))
3157 ;; Otherwise show the previous nonblank line,
3160 (skip-chars-backward "\n \t")
3163 (buffer-substring (progn
3164 (skip-chars-backward "\n \t")
3167 (progn (end-of-line)
3168 (skip-chars-backward " \t")
3170 ;; Replace the newline and other whitespace with `...'.
3172 (buffer-substring blinkpos (1+ blinkpos)))
3173 ;; There is nothing to show except the char itself.
3174 (buffer-substring blinkpos (1+ blinkpos))))))))
3176 (message "Mismatched parentheses"))
3177 ((not blink-matching-paren-distance)
3178 (message "Unmatched parenthesis"))))))))
3180 ;Turned off because it makes dbx bomb out.
3181 (setq blink-paren-function 'blink-matching-open)
3183 ;; This executes C-g typed while Emacs is waiting for a command.
3184 ;; Quitting out of a program does not go through here;
3185 ;; that happens in the QUIT macro at the C code level.
3186 (defun keyboard-quit ()
3187 "Signal a `quit' condition.
3188 During execution of Lisp code, this character causes a quit directly.
3189 At top-level, as an editor command, this simply beeps."
3194 (define-key global-map "\C-g" 'keyboard-quit)
3196 (defvar buffer-quit-function nil
3197 "Function to call to \"quit\" the current buffer, or nil if none.
3198 \\[keyboard-escape-quit] calls this function when its more local actions
3199 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3201 (defun keyboard-escape-quit ()
3202 "Exit the current \"mode\" (in a generalized sense of the word).
3203 This command can exit an interactive command such as `query-replace',
3204 can clear out a prefix argument or a region,
3205 can get out of the minibuffer or other recursive edit,
3206 cancel the use of the current buffer (for special-purpose buffers),
3207 or go back to just one window (by deleting all but the selected window)."
3209 (cond ((eq last-command 'mode-exited) nil)
3210 ((> (minibuffer-depth) 0)
3211 (abort-recursive-edit))
3214 ((and transient-mark-mode
3217 ((> (recursion-depth) 0)
3218 (exit-recursive-edit))
3219 (buffer-quit-function
3220 (funcall buffer-quit-function))
3221 ((not (one-window-p t))
3222 (delete-other-windows))
3223 ((string-match "^ \\*" (buffer-name (current-buffer)))
3226 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3228 (defcustom input-mode-8-bit t
3229 "Control acceptance of 8-bit keyboard input.
3230 This may be useful for inputting non-ASCII characters if your keyboard
3231 can generate them. It is not necessary to change this under a window
3232 system which can distinguish 8-bit characters and Meta keys.
3233 Setting this variable directly does not take effect;
3234 use either M-x customize or the function `set-input-mode'."
3235 :set (lambda (symbol value)
3236 (let ((mode (current-input-mode)))
3237 (set-input-mode (nth 0 mode) (nth 1 mode) value)))
3238 :initialize 'custom-initialize-default
3239 :type '(choice (const :tag "8-bit input for a Meta key" t)
3240 (const :tag "Direct 8-bit character input" 0)
3241 (const :tag "Assume top bit is parity and ignore" nil))
3243 :link '(custom-manual "Single-Byte European Support")
3246 (defcustom read-mail-command 'rmail
3247 "*Your preference for a mail reading package.
3248 This is used by some keybindings which support reading mail.
3249 See also `mail-user-agent' concerning sending mail."
3250 :type '(choice (function-item rmail)
3251 (function-item gnus)
3252 (function-item mh-rmail)
3253 (function :tag "Other"))
3257 (defcustom mail-user-agent 'sendmail-user-agent
3258 "*Your preference for a mail composition package.
3259 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3260 outgoing email message. This variable lets you specify which
3261 mail-sending package you prefer.
3263 Valid values include:
3265 `sendmail-user-agent' -- use the default Emacs Mail package.
3266 See Info node `(emacs)Sending Mail'.
3267 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3268 See Info node `(mh-e)'.
3269 `message-user-agent' -- use the Gnus Message package.
3270 See Info node `(message)'.
3271 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3272 paraphernalia, particularly the Gcc: header for
3275 Additional valid symbols may be available; check with the author of
3276 your package for details.
3278 See also `read-mail-command' concerning reading mail."
3279 :type '(radio (function-item :tag "Default Emacs mail"
3281 sendmail-user-agent)
3282 (function-item :tag "Emacs interface to MH"
3285 (function-item :tag "Gnus Message package"
3288 (function-item :tag "Gnus Message with full Gnus features"
3291 (function :tag "Other"))
3294 (defun define-mail-user-agent (symbol composefunc sendfunc
3295 &optional abortfunc hookvar)
3296 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3298 SYMBOL can be any Lisp symbol. Its function definition and/or
3299 value as a variable do not matter for this usage; we use only certain
3300 properties on its property list, to encode the rest of the arguments.
3302 COMPOSEFUNC is program callable function that composes an outgoing
3303 mail message buffer. This function should set up the basics of the
3304 buffer without requiring user interaction. It should populate the
3305 standard mail headers, leaving the `to:' and `subject:' headers blank
3308 COMPOSEFUNC should accept several optional arguments--the same
3309 arguments that `compose-mail' takes. See that function's documentation.
3311 SENDFUNC is the command a user would run to send the message.
3313 Optional ABORTFUNC is the command a user would run to abort the
3314 message. For mail packages that don't have a separate abort function,
3315 this can be `kill-buffer' (the equivalent of omitting this argument).
3317 Optional HOOKVAR is a hook variable that gets run before the message
3318 is actually sent. Callers that use the `mail-user-agent' may
3319 install a hook function temporarily on this hook variable.
3320 If HOOKVAR is nil, `mail-send-hook' is used.
3322 The properties used on SYMBOL are `composefunc', `sendfunc',
3323 `abortfunc', and `hookvar'."
3324 (put symbol 'composefunc composefunc)
3325 (put symbol 'sendfunc sendfunc)
3326 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3327 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3329 (define-mail-user-agent 'sendmail-user-agent
3330 'sendmail-user-agent-compose
3331 'mail-send-and-exit)
3333 (defun rfc822-goto-eoh ()
3334 ;; Go to header delimiter line in a mail message, following RFC822 rules
3335 (goto-char (point-min))
3336 (while (looking-at "^[^: \n]+:\\|^[ \t]")
3340 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3341 switch-function yank-action
3344 (let ((special-display-buffer-names nil)
3345 (special-display-regexps nil)
3346 (same-window-buffer-names nil)
3347 (same-window-regexps nil))
3348 (funcall switch-function "*mail*")))
3349 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3350 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers)))
3351 (body (cdr (assoc-ignore-case "body" other-headers))))
3352 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3354 (error "Message aborted"))
3357 (while other-headers
3358 (unless (member-ignore-case (car (car other-headers))
3359 '("in-reply-to" "cc" "body"))
3360 (insert (car (car other-headers)) ": "
3361 (cdr (car other-headers)) "\n"))
3362 (setq other-headers (cdr other-headers)))
3368 (define-mail-user-agent 'mh-e-user-agent
3369 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3370 'mh-before-send-letter-hook)
3372 (defun compose-mail (&optional to subject other-headers continue
3373 switch-function yank-action send-actions)
3374 "Start composing a mail message to send.
3375 This uses the user's chosen mail composition package
3376 as selected with the variable `mail-user-agent'.
3377 The optional arguments TO and SUBJECT specify recipients
3378 and the initial Subject field, respectively.
3380 OTHER-HEADERS is an alist specifying additional
3381 header fields. Elements look like (HEADER . VALUE) where both
3382 HEADER and VALUE are strings.
3384 CONTINUE, if non-nil, says to continue editing a message already
3387 SWITCH-FUNCTION, if non-nil, is a function to use to
3388 switch to and display the buffer used for mail composition.
3390 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3391 to insert the raw text of the message being replied to.
3392 It has the form (FUNCTION . ARGS). The user agent will apply
3393 FUNCTION to ARGS, to insert the raw text of the original message.
3394 \(The user agent will also run `mail-citation-hook', *after* the
3395 original text has been inserted in this way.)
3397 SEND-ACTIONS is a list of actions to call when the message is sent.
3398 Each action has the form (FUNCTION . ARGS)."
3400 (list nil nil nil current-prefix-arg))
3401 (let ((function (get mail-user-agent 'composefunc)))
3402 (funcall function to subject other-headers continue
3403 switch-function yank-action send-actions)))
3405 (defun compose-mail-other-window (&optional to subject other-headers continue
3406 yank-action send-actions)
3407 "Like \\[compose-mail], but edit the outgoing message in another window."
3409 (list nil nil nil current-prefix-arg))
3410 (compose-mail to subject other-headers continue
3411 'switch-to-buffer-other-window yank-action send-actions))
3414 (defun compose-mail-other-frame (&optional to subject other-headers continue
3415 yank-action send-actions)
3416 "Like \\[compose-mail], but edit the outgoing message in another frame."
3418 (list nil nil nil current-prefix-arg))
3419 (compose-mail to subject other-headers continue
3420 'switch-to-buffer-other-frame yank-action send-actions))
3422 (defvar set-variable-value-history nil
3423 "History of values entered with `set-variable'.")
3425 (defun set-variable (var val)
3426 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3427 When using this interactively, enter a Lisp object for VALUE.
3428 If you want VALUE to be a string, you must surround it with doublequotes.
3429 VALUE is used literally, not evaluated.
3431 If VARIABLE has a `variable-interactive' property, that is used as if
3432 it were the arg to `interactive' (which see) to interactively read VALUE.
3434 If VARIABLE has been defined with `defcustom', then the type information
3435 in the definition is used to check that VALUE is valid."
3437 (let* ((default-var (variable-at-point))
3438 (var (if (symbolp default-var)
3439 (read-variable (format "Set variable (default %s): " default-var)
3441 (read-variable "Set variable: ")))
3442 (minibuffer-help-form '(describe-variable var))
3443 (prop (get var 'variable-interactive))
3444 (prompt (format "Set %s to value: " var))
3446 ;; Use VAR's `variable-interactive' property
3447 ;; as an interactive spec for prompting.
3448 (call-interactively `(lambda (arg)
3452 (read-string prompt nil
3453 'set-variable-value-history)))))
3456 (let ((type (get var 'custom-type)))
3458 ;; Match with custom type.
3460 (setq type (widget-convert type))
3461 (unless (widget-apply type :match val)
3462 (error "Value `%S' does not match type %S of %S"
3463 val (car type) var))))
3466 ;; Define the major mode for lists of completions.
3468 (defvar completion-list-mode-map nil
3469 "Local map for completion list buffers.")
3470 (or completion-list-mode-map
3471 (let ((map (make-sparse-keymap)))
3472 (define-key map [mouse-2] 'mouse-choose-completion)
3473 (define-key map [down-mouse-2] nil)
3474 (define-key map "\C-m" 'choose-completion)
3475 (define-key map "\e\e\e" 'delete-completion-window)
3476 (define-key map [left] 'previous-completion)
3477 (define-key map [right] 'next-completion)
3478 (setq completion-list-mode-map map)))
3480 ;; Completion mode is suitable only for specially formatted data.
3481 (put 'completion-list-mode 'mode-class 'special)
3483 (defvar completion-reference-buffer nil
3484 "Record the buffer that was current when the completion list was requested.
3485 This is a local variable in the completion list buffer.
3486 Initial value is nil to avoid some compiler warnings.")
3488 (defvar completion-no-auto-exit nil
3489 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3490 This also applies to other functions such as `choose-completion'
3491 and `mouse-choose-completion'.")
3493 (defvar completion-base-size nil
3494 "Number of chars at beginning of minibuffer not involved in completion.
3495 This is a local variable in the completion list buffer
3496 but it talks about the buffer in `completion-reference-buffer'.
3497 If this is nil, it means to compare text to determine which part
3498 of the tail end of the buffer's text is involved in completion.")
3500 (defun delete-completion-window ()
3501 "Delete the completion list window.
3502 Go to the window from which completion was requested."
3504 (let ((buf completion-reference-buffer))
3505 (if (one-window-p t)
3506 (if (window-dedicated-p (selected-window))
3507 (delete-frame (selected-frame)))
3508 (delete-window (selected-window))
3509 (if (get-buffer-window buf)
3510 (select-window (get-buffer-window buf))))))
3512 (defun previous-completion (n)
3513 "Move to the previous item in the completion list."
3515 (next-completion (- n)))
3517 (defun next-completion (n)
3518 "Move to the next item in the completion list.
3519 With prefix argument N, move N items (negative N means move backward)."
3521 (while (and (> n 0) (not (eobp)))
3522 (let ((prop (get-text-property (point) 'mouse-face))
3524 ;; If in a completion, move to the end of it.
3526 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3527 ;; Move to start of next one.
3528 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3530 (while (and (< n 0) (not (bobp)))
3531 (let ((prop (get-text-property (1- (point)) 'mouse-face))
3533 ;; If in a completion, move to the start of it.
3535 (goto-char (previous-single-property-change
3536 (point) 'mouse-face nil end)))
3537 ;; Move to end of the previous completion.
3538 (goto-char (previous-single-property-change (point) 'mouse-face nil end))
3539 ;; Move to the start of that one.
3540 (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
3543 (defun choose-completion ()
3544 "Choose the completion that point is in or next to."
3546 (let (beg end completion (buffer completion-reference-buffer)
3547 (base-size completion-base-size))
3548 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3549 (setq end (point) beg (1+ (point))))
3550 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3551 (setq end (1- (point)) beg (point)))
3553 (error "No completion here"))
3554 (setq beg (previous-single-property-change beg 'mouse-face))
3555 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3556 (setq completion (buffer-substring beg end))
3557 (let ((owindow (selected-window)))
3558 (if (and (one-window-p t 'selected-frame)
3559 (window-dedicated-p (selected-window)))
3560 ;; This is a special buffer's frame
3561 (iconify-frame (selected-frame))
3562 (or (window-dedicated-p (selected-window))
3564 (select-window owindow))
3565 (choose-completion-string completion buffer base-size)))
3567 ;; Delete the longest partial match for STRING
3568 ;; that can be found before POINT.
3569 (defun choose-completion-delete-max-match (string)
3570 (let ((opoint (point))
3571 (len (min (length string)
3572 (- (point) (point-min)))))
3573 (goto-char (- (point) (length string)))
3574 (if completion-ignore-case
3575 (setq string (downcase string)))
3576 (while (and (> len 0)
3577 (let ((tail (buffer-substring (point)
3579 (if completion-ignore-case
3580 (setq tail (downcase tail)))
3581 (not (string= tail (substring string 0 len)))))
3586 ;; Switch to BUFFER and insert the completion choice CHOICE.
3587 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3588 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3590 ;; If BUFFER is the minibuffer, exit the minibuffer
3591 ;; unless it is reading a file name and CHOICE is a directory,
3592 ;; or completion-no-auto-exit is non-nil.
3593 (defun choose-completion-string (choice &optional buffer base-size)
3594 (let ((buffer (or buffer completion-reference-buffer))
3595 (mini-p (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))))
3596 ;; If BUFFER is a minibuffer, barf unless it's the currently
3597 ;; active minibuffer.
3599 (or (not (active-minibuffer-window))
3601 (window-buffer (active-minibuffer-window))))))
3602 (error "Minibuffer is not active for completion")
3603 ;; Insert the completion into the buffer where completion was requested.
3606 (delete-region (+ base-size (if mini-p
3607 (minibuffer-prompt-end)
3610 (choose-completion-delete-max-match choice))
3612 (remove-text-properties (- (point) (length choice)) (point)
3614 ;; Update point in the window that BUFFER is showing in.
3615 (let ((window (get-buffer-window buffer t)))
3616 (set-window-point window (point)))
3617 ;; If completing for the minibuffer, exit it with this choice.
3618 (and (not completion-no-auto-exit)
3619 (equal buffer (window-buffer (minibuffer-window)))
3620 minibuffer-completion-table
3621 ;; If this is reading a file name, and the file name chosen
3622 ;; is a directory, don't exit the minibuffer.
3623 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3624 (file-directory-p (field-string (point-max))))
3625 (select-window (active-minibuffer-window))
3626 (exit-minibuffer))))))
3628 (defun completion-list-mode ()
3629 "Major mode for buffers showing lists of possible completions.
3630 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3631 to select the completion near point.
3632 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3635 (kill-all-local-variables)
3636 (use-local-map completion-list-mode-map)
3637 (setq mode-name "Completion List")
3638 (setq major-mode 'completion-list-mode)
3639 (make-local-variable 'completion-base-size)
3640 (setq completion-base-size nil)
3641 (run-hooks 'completion-list-mode-hook))
3643 (defvar completion-setup-hook nil
3644 "Normal hook run at the end of setting up a completion list buffer.
3645 When this hook is run, the current buffer is the one in which the
3646 command to display the completion list buffer was run.
3647 The completion list buffer is available as the value of `standard-output'.")
3649 ;; This function goes in completion-setup-hook, so that it is called
3650 ;; after the text of the completion list buffer is written.
3652 (defun completion-setup-function ()
3654 (let ((mainbuf (current-buffer)))
3655 (set-buffer standard-output)
3656 (completion-list-mode)
3657 (make-local-variable 'completion-reference-buffer)
3658 (setq completion-reference-buffer mainbuf)
3659 (if (eq minibuffer-completion-table 'read-file-name-internal)
3660 ;; For file name completion,
3661 ;; use the number of chars before the start of the
3662 ;; last file name component.
3663 (setq completion-base-size
3665 (set-buffer mainbuf)
3666 (goto-char (point-max))
3667 (skip-chars-backward (format "^%c" directory-sep-char))
3668 (- (point) (minibuffer-prompt-end))))
3669 ;; Otherwise, in minibuffer, the whole input is being completed.
3671 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3672 (buffer-name mainbuf))
3673 (setq completion-base-size 0))))
3674 (goto-char (point-min))
3675 (if (display-mouse-p)
3676 (insert (substitute-command-keys
3677 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3678 (insert (substitute-command-keys
3679 "In this buffer, type \\[choose-completion] to \
3680 select the completion near point.\n\n")))))
3682 (add-hook 'completion-setup-hook 'completion-setup-function)
3684 (define-key minibuffer-local-completion-map [prior]
3685 'switch-to-completions)
3686 (define-key minibuffer-local-must-match-map [prior]
3687 'switch-to-completions)
3688 (define-key minibuffer-local-completion-map "\M-v"
3689 'switch-to-completions)
3690 (define-key minibuffer-local-must-match-map "\M-v"
3691 'switch-to-completions)
3693 (defun switch-to-completions ()
3694 "Select the completion list window."
3696 ;; Make sure we have a completions window.
3697 (or (get-buffer-window "*Completions*")
3698 (minibuffer-completion-help))
3699 (let ((window (get-buffer-window "*Completions*")))
3701 (select-window window)
3702 (goto-char (point-min))
3703 (search-forward "\n\n")
3706 ;; Support keyboard commands to turn on various modifiers.
3708 ;; These functions -- which are not commands -- each add one modifier
3709 ;; to the following event.
3711 (defun event-apply-alt-modifier (ignore-prompt)
3712 "Add the Alt modifier to the following event.
3713 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
3714 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3715 (defun event-apply-super-modifier (ignore-prompt)
3716 "Add the Super modifier to the following event.
3717 For example, type \\[event-apply-super-modifier] & to enter Super-&."
3718 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3719 (defun event-apply-hyper-modifier (ignore-prompt)
3720 "Add the Hyper modifier to the following event.
3721 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
3722 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3723 (defun event-apply-shift-modifier (ignore-prompt)
3724 "Add the Shift modifier to the following event.
3725 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
3726 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3727 (defun event-apply-control-modifier (ignore-prompt)
3728 "Add the Ctrl modifier to the following event.
3729 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
3730 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3731 (defun event-apply-meta-modifier (ignore-prompt)
3732 "Add the Meta modifier to the following event.
3733 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
3734 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3736 (defun event-apply-modifier (event symbol lshiftby prefix)
3737 "Apply a modifier flag to event EVENT.
3738 SYMBOL is the name of this modifier, as a symbol.
3739 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3740 PREFIX is the string that represents this modifier in an event type symbol."
3742 (cond ((eq symbol 'control)
3743 (if (and (<= (downcase event) ?z)
3744 (>= (downcase event) ?a))
3745 (- (downcase event) ?a -1)
3746 (if (and (<= (downcase event) ?Z)
3747 (>= (downcase event) ?A))
3748 (- (downcase event) ?A -1)
3749 (logior (lsh 1 lshiftby) event))))
3751 (if (and (<= (downcase event) ?z)
3752 (>= (downcase event) ?a))
3754 (logior (lsh 1 lshiftby) event)))
3756 (logior (lsh 1 lshiftby) event)))
3757 (if (memq symbol (event-modifiers event))
3759 (let ((event-type (if (symbolp event) event (car event))))
3760 (setq event-type (intern (concat prefix (symbol-name event-type))))
3763 (cons event-type (cdr event)))))))
3765 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
3766 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
3767 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
3768 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
3769 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
3770 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
3772 ;;;; Keypad support.
3774 ;;; Make the keypad keys act like ordinary typing keys. If people add
3775 ;;; bindings for the function key symbols, then those bindings will
3776 ;;; override these, so this shouldn't interfere with any existing
3779 ;; Also tell read-char how to handle these keys.
3781 (lambda (keypad-normal)
3782 (let ((keypad (nth 0 keypad-normal))
3783 (normal (nth 1 keypad-normal)))
3784 (put keypad 'ascii-character normal)
3785 (define-key function-key-map (vector keypad) (vector normal))))
3786 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
3787 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
3800 ;;;; forking a twin copy of a buffer.
3803 (defvar clone-buffer-hook nil
3804 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
3806 (defun clone-process (process &optional newname)
3807 "Create a twin copy of PROCESS.
3808 If NEWNAME is nil, it defaults to PROCESS' name;
3809 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
3810 If PROCESS is associated with a buffer, the new process will be associated
3811 with the current buffer instead.
3812 Returns nil if PROCESS has already terminated."
3813 (setq newname (or newname (process-name process)))
3814 (if (string-match "<[0-9]+>\\'" newname)
3815 (setq newname (substring newname 0 (match-beginning 0))))
3816 (when (memq (process-status process) '(run stop open))
3817 (let* ((process-connection-type (process-tty-name process))
3818 (old-kwoq (process-kill-without-query process nil))
3820 (if (memq (process-status process) '(open))
3821 (apply 'open-network-stream newname
3822 (if (process-buffer process) (current-buffer))
3823 (process-contact process))
3824 (apply 'start-process newname
3825 (if (process-buffer process) (current-buffer))
3826 (process-command process)))))
3827 (process-kill-without-query new-process old-kwoq)
3828 (process-kill-without-query process old-kwoq)
3829 (set-process-inherit-coding-system-flag
3830 new-process (process-inherit-coding-system-flag process))
3831 (set-process-filter new-process (process-filter process))
3832 (set-process-sentinel new-process (process-sentinel process))
3835 ;; things to maybe add (currently partly covered by `funcall mode':
3838 (defun clone-buffer (&optional newname display-flag)
3839 "Create a twin copy of the current buffer.
3840 If NEWNAME is nil, it defaults to the current buffer's name;
3841 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
3843 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
3844 This runs the normal hook `clone-buffer-hook' in the new buffer
3845 after it has been set up properly in other respects."
3846 (interactive (list (if current-prefix-arg (read-string "Name: "))
3848 (if buffer-file-name
3849 (error "Cannot clone a file-visiting buffer"))
3850 (if (get major-mode 'no-clone)
3851 (error "Cannot clone a buffer in %s mode" mode-name))
3852 (setq newname (or newname (buffer-name)))
3853 (if (string-match "<[0-9]+>\\'" newname)
3854 (setq newname (substring newname 0 (match-beginning 0))))
3855 (let ((buf (current-buffer))
3859 (mk (if mark-active (mark t)))
3860 (modified (buffer-modified-p))
3862 (lvars (buffer-local-variables))
3863 (process (get-buffer-process (current-buffer)))
3864 (new (generate-new-buffer (or newname (buffer-name)))))
3867 (with-current-buffer new
3868 (insert-buffer-substring buf)))
3869 (with-current-buffer new
3870 (narrow-to-region ptmin ptmax)
3872 (if mk (set-mark mk))
3873 (set-buffer-modified-p modified)
3875 ;; Clone the old buffer's process, if any.
3876 (when process (clone-process process))
3878 ;; Now set up the major mode.
3881 ;; Set up other local variables.
3883 (condition-case () ;in case var is read-only
3886 (set (make-local-variable (car v)) (cdr v)))
3890 ;; Run any hooks (typically set up by the major mode
3891 ;; for cloning to work properly).
3892 (run-hooks 'clone-buffer-hook))
3893 (if display-flag (pop-to-buffer new))
3897 (defun clone-indirect-buffer (newname display-flag &optional norecord)
3898 "Create an indirect buffer that is a twin copy of the current buffer.
3900 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
3901 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
3902 or if not called with a prefix arg, NEWNAME defaults to the current
3903 buffer's name. The name is modified by adding a `<N>' suffix to it
3904 or by incrementing the N in an existing suffix.
3906 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
3907 This is always done when called interactively.
3909 Optional last arg NORECORD non-nil means do not put this buffer at the
3910 front of the list of recently selected ones."
3911 (interactive (list (if current-prefix-arg
3912 (read-string "BName of indirect buffer: "))
3914 (setq newname (or newname (buffer-name)))
3915 (if (string-match "<[0-9]+>\\'" newname)
3916 (setq newname (substring newname 0 (match-beginning 0))))
3917 (let* ((name (generate-new-buffer-name newname))
3918 (buffer (make-indirect-buffer (current-buffer) name t)))
3920 (pop-to-buffer buffer))
3924 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
3925 "Create an indirect buffer that is a twin copy of BUFFER.
3926 Select the new buffer in another window.
3927 Optional second arg NORECORD non-nil means do not put this buffer at
3928 the front of the list of recently selected ones."
3929 (interactive "bClone buffer in other window: ")
3930 (let ((popup-windows t))
3932 (clone-indirect-buffer nil t norecord)))
3934 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
3939 (defconst syntax-code-table
3940 '((?\ 0 "whitespace")
3942 (?. 1 "punctuation")
3945 (?\( 4 "open parenthesis")
3946 (?\) 5 "close parenthesis")
3947 (?\' 6 "expression prefix")
3948 (?\" 7 "string quote")
3949 (?$ 8 "paired delimiter")
3951 (?/ 10 "character quote")
3952 (?< 11 "comment start")
3953 (?> 12 "comment end")
3955 (nil 14 "comment fence")
3956 (nil 15 "string fence"))
3957 "Alist of forms (CHAR CODE DESCRIPTION) mapping characters to syntax info.
3958 CHAR is a character that is allowed as first char in the string
3959 specifying the syntax when calling `modify-syntax-entry'. CODE is the
3960 corresponing syntax code as it is stored in a syntax cell, and
3961 can be used as value of a `syntax-table' property.
3962 DESCRIPTION is the descriptive string for the syntax.")
3964 ;;; simple.el ends here