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 (defun minibuffer-avoid-prompt (new old)
690 "A point-motion hook for the minibuffer, that moves point out of the prompt."
691 (constrain-to-field nil (point-max)))
693 (defcustom minibuffer-history-case-insensitive-variables nil
694 "*Minibuffer history variables for which matching should ignore case.
695 If a history variable is a member of this list, then the
696 \\[previous-matching-history-element] and \\[next-matching-history-element]\
697 commands ignore case when searching it, regardless of `case-fold-search'."
698 :type '(repeat variable)
701 (defun previous-matching-history-element (regexp n)
702 "Find the previous history element that matches REGEXP.
703 \(Previous history elements refer to earlier actions.)
704 With prefix argument N, search for Nth previous match.
705 If N is negative, find the next or Nth next match.
706 An uppercase letter in REGEXP makes the search case-sensitive.
707 See also `minibuffer-history-case-insensitive-variables'."
709 (let* ((enable-recursive-minibuffers t)
710 (regexp (read-from-minibuffer "Previous element matching (regexp): "
714 'minibuffer-history-search-history)))
715 ;; Use the last regexp specified, by default, if input is empty.
716 (list (if (string= regexp "")
717 (if minibuffer-history-search-history
718 (car minibuffer-history-search-history)
719 (error "No previous history search regexp"))
721 (prefix-numeric-value current-prefix-arg))))
723 (if (and (zerop minibuffer-history-position)
724 (null minibuffer-text-before-history))
725 (setq minibuffer-text-before-history (field-string (point-max))))
726 (let ((history (symbol-value minibuffer-history-variable))
728 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
729 ;; On some systems, ignore case for file names.
730 (if (memq minibuffer-history-variable
731 minibuffer-history-case-insensitive-variables)
733 ;; Respect the user's setting for case-fold-search:
739 (pos minibuffer-history-position))
742 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
743 (when (= pos prevpos)
745 "No later matching history item"
746 "No earlier matching history item")))
748 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
749 (let ((print-level nil))
750 (prin1-to-string (nth (1- pos) history)))
751 (nth (1- pos) history)))
754 (and (string-match regexp match-string)
756 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
757 (match-beginning 1))))
759 (setq n (+ n (if (< n 0) 1 -1)))))
760 (setq minibuffer-history-position pos)
761 (goto-char (point-max))
763 (insert match-string)
764 (goto-char (+ (field-beginning) match-offset))))
765 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
766 (eq (car (car command-history)) 'next-matching-history-element))
767 (setq command-history (cdr command-history))))
769 (defun next-matching-history-element (regexp n)
770 "Find the next history element that matches REGEXP.
771 \(The next history element refers to a more recent action.)
772 With prefix argument N, search for Nth next match.
773 If N is negative, find the previous or Nth previous match.
774 An uppercase letter in REGEXP makes the search case-sensitive."
776 (let* ((enable-recursive-minibuffers t)
777 (regexp (read-from-minibuffer "Next element matching (regexp): "
781 'minibuffer-history-search-history)))
782 ;; Use the last regexp specified, by default, if input is empty.
783 (list (if (string= regexp "")
784 (setcar minibuffer-history-search-history
785 (nth 1 minibuffer-history-search-history))
787 (prefix-numeric-value current-prefix-arg))))
788 (previous-matching-history-element regexp (- n)))
790 (defvar minibuffer-temporary-goal-position nil)
792 (defun next-history-element (n)
793 "Insert the next element of the minibuffer history into the minibuffer."
796 (let ((narg (- minibuffer-history-position n))
797 (minimum (if minibuffer-default -1 0))
798 elt minibuffer-returned-to-present)
799 (if (and (zerop minibuffer-history-position)
800 (null minibuffer-text-before-history))
801 (setq minibuffer-text-before-history (field-string (point-max))))
803 (if minibuffer-default
804 (error "End of history; no next item")
805 (error "End of history; no default available")))
806 (if (> narg (length (symbol-value minibuffer-history-variable)))
807 (error "Beginning of history; no preceding item"))
808 (unless (or (eq last-command 'next-history-element)
809 (eq last-command 'previous-history-element))
810 (let ((prompt-end (field-beginning (point-max))))
811 (set (make-local-variable 'minibuffer-temporary-goal-position)
812 (cond ((<= (point) prompt-end) prompt-end)
815 (goto-char (point-max))
817 (setq minibuffer-history-position narg)
819 (setq elt minibuffer-default))
821 (setq elt (or minibuffer-text-before-history ""))
822 (setq minibuffer-returned-to-present t)
823 (setq minibuffer-text-before-history nil))
824 (t (setq elt (nth (1- minibuffer-history-position)
825 (symbol-value minibuffer-history-variable)))))
827 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
828 (not minibuffer-returned-to-present))
829 (let ((print-level nil))
830 (prin1-to-string elt))
832 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
834 (defun previous-history-element (n)
835 "Inserts the previous element of the minibuffer history into the minibuffer."
837 (next-history-element (- n)))
839 (defun next-complete-history-element (n)
840 "Get next history element which completes the minibuffer before the point.
841 The contents of the minibuffer after the point are deleted, and replaced
842 by the new completion."
844 (let ((point-at-start (point)))
845 (next-matching-history-element
847 "^" (regexp-quote (buffer-substring (field-beginning) (point))))
849 ;; next-matching-history-element always puts us at (point-min).
850 ;; Move to the position we were at before changing the buffer contents.
851 ;; This is still sensical, because the text before point has not changed.
852 (goto-char point-at-start)))
854 (defun previous-complete-history-element (n)
856 Get previous history element which completes the minibuffer before the point.
857 The contents of the minibuffer after the point are deleted, and replaced
858 by the new completion."
860 (next-complete-history-element (- n)))
862 ;; These two functions are for compatibility with the old subrs of the
865 (defun minibuffer-prompt-width ()
866 "Return the display width of the minibuffer prompt.
867 Return 0 if current buffer is not a mini-buffer."
868 ;; Return the width of everything before the field at the end of
869 ;; the buffer; this should be 0 for normal buffers.
870 (1- (field-beginning (point-max))))
872 (defun minibuffer-prompt-end ()
873 "Return the buffer position of the end of the minibuffer prompt.
874 Return 0 if current buffer is not a mini-buffer."
875 (field-beginning (point-max)))
878 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
879 (defalias 'advertised-undo 'undo)
881 (defun undo (&optional arg)
882 "Undo some previous changes.
883 Repeat this command to undo more changes.
884 A numeric argument serves as a repeat count.
886 In Transient Mark mode when the mark is active, only undo changes within
887 the current region. Similarly, when not in Transient Mark mode, just C-u
888 as an argument limits undo to changes within the current region."
890 ;; If we don't get all the way thru, make last-command indicate that
891 ;; for the following command.
892 (setq this-command t)
893 (let ((modified (buffer-modified-p))
894 (recent-save (recent-auto-save-p)))
895 (or (eq (selected-window) (minibuffer-window))
897 (unless (eq last-command 'undo)
898 (if (if transient-mark-mode mark-active (and arg (not (numberp arg))))
899 (undo-start (region-beginning) (region-end))
901 ;; get rid of initial undo boundary
904 (if (or transient-mark-mode (numberp arg))
905 (prefix-numeric-value arg)
907 ;; Don't specify a position in the undo record for the undo command.
908 ;; Instead, undoing this should move point to where the change is.
909 (let ((tail buffer-undo-list)
911 (while (and tail (not done) (not (null (car tail))))
912 (if (integerp (car tail))
915 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
916 (setq tail (cdr tail))))
917 (and modified (not (buffer-modified-p))
918 (delete-auto-save-file-if-necessary recent-save)))
919 ;; If we do get all the way thru, make this-command indicate that.
920 (setq this-command 'undo))
922 (defvar pending-undo-list nil
923 "Within a run of consecutive undo commands, list remaining to be undone.")
925 (defvar undo-in-progress nil
926 "Non-nil while performing an undo.
927 Some change-hooks test this variable to do something different.")
929 (defun undo-more (count)
930 "Undo back N undo-boundaries beyond what was already undone recently.
931 Call `undo-start' to get ready to undo recent changes,
932 then call `undo-more' one or more times to undo them."
933 (or pending-undo-list
934 (error "No further undo information"))
935 (let ((undo-in-progress t))
936 (setq pending-undo-list (primitive-undo count pending-undo-list))))
938 ;; Deep copy of a list
939 (defun undo-copy-list (list)
940 "Make a copy of undo list LIST."
941 (mapcar 'undo-copy-list-1 list))
943 (defun undo-copy-list-1 (elt)
945 (cons (car elt) (undo-copy-list-1 (cdr elt)))
948 (defun undo-start (&optional beg end)
949 "Set `pending-undo-list' to the front of the undo list.
950 The next call to `undo-more' will undo the most recently made change.
951 If BEG and END are specified, then only undo elements
952 that apply to text between BEG and END are used; other undo elements
953 are ignored. If BEG and END are nil, all undo elements are used."
954 (if (eq buffer-undo-list t)
955 (error "No undo information in this buffer"))
956 (setq pending-undo-list
957 (if (and beg end (not (= beg end)))
958 (undo-make-selective-list (min beg end) (max beg end))
961 (defvar undo-adjusted-markers)
963 (defun undo-make-selective-list (start end)
964 "Return a list of undo elements for the region START to END.
965 The elements come from `buffer-undo-list', but we keep only
966 the elements inside this region, and discard those outside this region.
967 If we find an element that crosses an edge of this region,
968 we stop and ignore all further elements."
969 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
970 (undo-list (list nil))
971 undo-adjusted-markers
973 undo-elt undo-elt temp-undo-list delta)
974 (while undo-list-copy
975 (setq undo-elt (car undo-list-copy))
977 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
978 ;; This is a "was unmodified" element.
979 ;; Keep it if we have kept everything thus far.
982 (undo-elt-in-region undo-elt start end)))))
985 (setq end (+ end (cdr (undo-delta undo-elt))))
986 ;; Don't put two nils together in the list
987 (if (not (and (eq (car undo-list) nil)
989 (setq undo-list (cons undo-elt undo-list))))
990 (if (undo-elt-crosses-region undo-elt start end)
991 (setq undo-list-copy nil)
992 (setq some-rejected t)
993 (setq temp-undo-list (cdr undo-list-copy))
994 (setq delta (undo-delta undo-elt))
996 (when (/= (cdr delta) 0)
997 (let ((position (car delta))
998 (offset (cdr delta)))
1000 ;; Loop down the earlier events adjusting their buffer positions
1001 ;; to reflect the fact that a change to the buffer isn't being
1002 ;; undone. We only need to process those element types which
1003 ;; undo-elt-in-region will return as being in the region since
1004 ;; only those types can ever get into the output
1006 (while temp-undo-list
1007 (setq undo-elt (car temp-undo-list))
1008 (cond ((integerp undo-elt)
1009 (if (>= undo-elt position)
1010 (setcar temp-undo-list (- undo-elt offset))))
1011 ((atom undo-elt) nil)
1012 ((stringp (car undo-elt))
1013 ;; (TEXT . POSITION)
1014 (let ((text-pos (abs (cdr undo-elt)))
1015 (point-at-end (< (cdr undo-elt) 0 )))
1016 (if (>= text-pos position)
1017 (setcdr undo-elt (* (if point-at-end -1 1)
1018 (- text-pos offset))))))
1019 ((integerp (car undo-elt))
1021 (when (>= (car undo-elt) position)
1022 (setcar undo-elt (- (car undo-elt) offset))
1023 (setcdr undo-elt (- (cdr undo-elt) offset))))
1024 ((null (car undo-elt))
1025 ;; (nil PROPERTY VALUE BEG . END)
1026 (let ((tail (nthcdr 3 undo-elt)))
1027 (when (>= (car tail) position)
1028 (setcar tail (- (car tail) offset))
1029 (setcdr tail (- (cdr tail) offset))))))
1030 (setq temp-undo-list (cdr temp-undo-list))))))))
1031 (setq undo-list-copy (cdr undo-list-copy)))
1032 (nreverse undo-list)))
1034 (defun undo-elt-in-region (undo-elt start end)
1035 "Determine whether UNDO-ELT falls inside the region START ... END.
1036 If it crosses the edge, we return nil."
1037 (cond ((integerp undo-elt)
1038 (and (>= undo-elt start)
1044 ((stringp (car undo-elt))
1045 ;; (TEXT . POSITION)
1046 (and (>= (abs (cdr undo-elt)) start)
1047 (< (abs (cdr undo-elt)) end)))
1048 ((and (consp undo-elt) (markerp (car undo-elt)))
1049 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1050 ;; See if MARKER is inside the region.
1051 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1053 (setq alist-elt (cons (car undo-elt)
1054 (marker-position (car undo-elt))))
1055 (setq undo-adjusted-markers
1056 (cons alist-elt undo-adjusted-markers)))
1057 (and (cdr alist-elt)
1058 (>= (cdr alist-elt) start)
1059 (< (cdr alist-elt) end))))
1060 ((null (car undo-elt))
1061 ;; (nil PROPERTY VALUE BEG . END)
1062 (let ((tail (nthcdr 3 undo-elt)))
1063 (and (>= (car tail) start)
1064 (< (cdr tail) end))))
1065 ((integerp (car undo-elt))
1067 (and (>= (car undo-elt) start)
1068 (< (cdr undo-elt) end)))))
1070 (defun undo-elt-crosses-region (undo-elt start end)
1071 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1072 This assumes we have already decided that UNDO-ELT
1073 is not *inside* the region START...END."
1074 (cond ((atom undo-elt) nil)
1075 ((null (car undo-elt))
1076 ;; (nil PROPERTY VALUE BEG . END)
1077 (let ((tail (nthcdr 3 undo-elt)))
1078 (not (or (< (car tail) end)
1079 (> (cdr tail) start)))))
1080 ((integerp (car undo-elt))
1082 (not (or (< (car undo-elt) end)
1083 (> (cdr undo-elt) start))))))
1085 ;; Return the first affected buffer position and the delta for an undo element
1086 ;; delta is defined as the change in subsequent buffer positions if we *did*
1088 (defun undo-delta (undo-elt)
1089 (if (consp undo-elt)
1090 (cond ((stringp (car undo-elt))
1091 ;; (TEXT . POSITION)
1092 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1093 ((integerp (car undo-elt))
1095 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1100 (defvar shell-command-history nil
1101 "History list for some commands that read shell commands.")
1103 (defvar shell-command-switch "-c"
1104 "Switch used to have the shell execute its command line argument.")
1106 (defvar shell-command-default-error-buffer nil
1107 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1108 This buffer is used when `shell-command' or 'shell-command-on-region'
1109 is run interactively. A value of nil means that output to stderr and
1110 stdout will be intermixed in the output stream.")
1112 (defun shell-command (command &optional output-buffer error-buffer)
1113 "Execute string COMMAND in inferior shell; display output, if any.
1115 If COMMAND ends in ampersand, execute it asynchronously.
1116 The output appears in the buffer `*Async Shell Command*'.
1117 That buffer is in shell mode.
1119 Otherwise, COMMAND is executed synchronously. The output appears in
1120 the buffer `*Shell Command Output*'. If the output is short enough to
1121 display in the echo area (which is determined by the variables
1122 `resize-mini-windows' and `max-mini-window-height'), it is shown
1123 there, but it is nonetheless available in buffer `*Shell Command
1124 Output*' even though that buffer is not automatically displayed. If
1125 there is no output, or if output is inserted in the current buffer,
1126 then `*Shell Command Output*' is deleted.
1128 To specify a coding system for converting non-ASCII characters
1129 in the shell command output, use \\[universal-coding-system-argument]
1130 before this command.
1132 Noninteractive callers can specify coding systems by binding
1133 `coding-system-for-read' and `coding-system-for-write'.
1135 The optional second argument OUTPUT-BUFFER, if non-nil,
1136 says to put the output in some other buffer.
1137 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1138 If OUTPUT-BUFFER is not a buffer and not nil,
1139 insert output in current buffer. (This cannot be done asynchronously.)
1140 In either case, the output is inserted after point (leaving mark after it).
1142 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1143 or buffer name to which to direct the command's standard error output.
1144 If it is nil, error output is mingled with regular output.
1145 In an interactive call, the variable `shell-command-default-error-buffer'
1146 specifies the value of ERROR-BUFFER."
1148 (interactive (list (read-from-minibuffer "Shell command: "
1149 nil nil nil 'shell-command-history)
1151 shell-command-default-error-buffer))
1152 ;; Look for a handler in case default-directory is a remote file name.
1154 (find-file-name-handler (directory-file-name default-directory)
1157 (funcall handler 'shell-command command output-buffer error-buffer)
1158 (if (and output-buffer
1159 (not (or (bufferp output-buffer) (stringp output-buffer))))
1163 (expand-file-name "scor"
1164 (or small-temporary-file-directory
1165 temporary-file-directory)))
1167 (barf-if-buffer-read-only)
1169 ;; We do not use -f for csh; we will not support broken use of
1170 ;; .cshrcs. Even the BSD csh manual says to use
1171 ;; "if ($?prompt) exit" before things which are not useful
1172 ;; non-interactively. Besides, if someone wants their other
1173 ;; aliases for shell commands then they can still have them.
1174 (call-process shell-file-name nil
1178 nil shell-command-switch command)
1179 (when (and error-file (file-exists-p error-file))
1180 (if (< 0 (nth 7 (file-attributes error-file)))
1181 (with-current-buffer (get-buffer-create error-buffer)
1182 (let ((pos-from-end (- (point-max) (point))))
1185 ;; Do no formatting while reading error file,
1186 ;; because that can run a shell command, and we
1187 ;; don't want that to cause an infinite recursion.
1188 (format-insert-file error-file nil)
1189 ;; Put point after the inserted errors.
1190 (goto-char (- (point-max) pos-from-end)))
1191 (display-buffer (current-buffer))))
1192 (delete-file error-file))
1193 ;; This is like exchange-point-and-mark, but doesn't
1194 ;; activate the mark. It is cleaner to avoid activation,
1195 ;; even though the command loop would deactivate the mark
1196 ;; because we inserted text.
1197 (goto-char (prog1 (mark t)
1198 (set-marker (mark-marker) (point)
1199 (current-buffer)))))
1200 ;; Preserve the match data in case called from a program.
1202 (if (string-match "[ \t]*&[ \t]*$" command)
1203 ;; Command ending with ampersand means asynchronous.
1204 (let ((buffer (get-buffer-create
1205 (or output-buffer "*Async Shell Command*")))
1206 (directory default-directory)
1208 ;; Remove the ampersand.
1209 (setq command (substring command 0 (match-beginning 0)))
1210 ;; If will kill a process, query first.
1211 (setq proc (get-buffer-process buffer))
1213 (if (yes-or-no-p "A command is running. Kill it? ")
1215 (error "Shell command in progress")))
1218 (setq buffer-read-only nil)
1220 (display-buffer buffer)
1221 (setq default-directory directory)
1222 (setq proc (start-process "Shell" buffer shell-file-name
1223 shell-command-switch command))
1224 (setq mode-line-process '(":%s"))
1225 (require 'shell) (shell-mode)
1226 (set-process-sentinel proc 'shell-command-sentinel)
1228 (shell-command-on-region (point) (point) command
1229 output-buffer nil error-buffer)))))))
1231 (defun display-message-or-buffer (message
1232 &optional buffer-name not-this-window frame)
1233 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1234 MESSAGE may be either a string or a buffer.
1236 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1237 the maximum height of the echo area, as defined by `max-mini-window-height'
1238 if `resize-mini-windows' is non-nil.
1240 Returns either the string shown in the echo area, or when a pop-up
1241 buffer is used, the window used to display it.
1243 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1244 name of the buffer used to display it in the case where a pop-up buffer
1245 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1246 string and it is displayed in the echo area, it is not specified whether
1247 the contents are inserted into the buffer anyway.
1249 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1250 and only used if a buffer is displayed."
1251 (cond ((and (stringp message) (not (string-match "\n" message)))
1252 ;; Trivial case where we can use the echo area
1253 (message "%s" message))
1254 ((and (stringp message)
1255 (= (string-match "\n" message) (1- (length message))))
1256 ;; Trivial case where we can just remove single trailing newline
1257 (message "%s" (substring message 0 (1- (length message)))))
1260 (with-current-buffer
1261 (if (bufferp message)
1263 (get-buffer-create (or buffer-name "*Message*")))
1265 (unless (bufferp message)
1270 (if (= (buffer-size) 0)
1272 (count-lines (point-min) (point-max)))))
1273 (cond ((or (<= lines 1)
1275 (if resize-mini-windows
1276 (cond ((floatp max-mini-window-height)
1278 max-mini-window-height))
1279 ((integerp max-mini-window-height)
1280 max-mini-window-height)
1285 (goto-char (point-max))
1288 (message "%s" (buffer-substring (point-min) (point))))
1291 (goto-char (point-min))
1292 (display-buffer message not-this-window frame))))))))
1295 ;; We have a sentinel to prevent insertion of a termination message
1296 ;; in the buffer itself.
1297 (defun shell-command-sentinel (process signal)
1298 (if (memq (process-status process) '(exit signal))
1300 (car (cdr (cdr (process-command process))))
1301 (substring signal 0 -1))))
1303 (defun shell-command-on-region (start end command
1304 &optional output-buffer replace
1306 "Execute string COMMAND in inferior shell with region as input.
1307 Normally display output (if any) in temp buffer `*Shell Command Output*';
1308 Prefix arg means replace the region with it. Return the exit code of
1311 To specify a coding system for converting non-ASCII characters
1312 in the input and output to the shell command, use \\[universal-coding-system-argument]
1313 before this command. By default, the input (from the current buffer)
1314 is encoded in the same coding system that will be used to save the file,
1315 `buffer-file-coding-system'. If the output is going to replace the region,
1316 then it is decoded from that same coding system.
1318 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1319 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1320 systems by binding `coding-system-for-read' and
1321 `coding-system-for-write'.
1323 If the output is short enough to display in the echo area (which is
1324 determined by the variable `max-mini-window-height' if
1325 `resize-mini-windows' is non-nil), it is shown there, but it is
1326 nonetheless available in buffer `*Shell Command Output*' even though
1327 that buffer is not automatically displayed. If there is no output, or
1328 if output is inserted in the current buffer, then `*Shell Command
1329 Output*' is deleted.
1331 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1332 that says to put the output in some other buffer.
1333 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1334 If OUTPUT-BUFFER is not a buffer and not nil,
1335 insert output in the current buffer.
1336 In either case, the output is inserted after point (leaving mark after it).
1338 If REPLACE, the optional fifth argument, is non-nil, that means insert
1339 the output in place of text from START to END, putting point and mark
1342 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1343 or buffer name to which to direct the command's standard error output.
1344 If it is nil, error output is mingled with regular output.
1345 In an interactive call, the variable `shell-command-default-error-buffer'
1346 specifies the value of ERROR-BUFFER."
1347 (interactive (let ((string
1348 ;; Do this before calling region-beginning
1349 ;; and region-end, in case subprocess output
1350 ;; relocates them while we are in the minibuffer.
1351 (read-from-minibuffer "Shell command on region: "
1353 'shell-command-history)))
1354 ;; call-interactively recognizes region-beginning and
1355 ;; region-end specially, leaving them in the history.
1356 (list (region-beginning) (region-end)
1360 shell-command-default-error-buffer)))
1364 (expand-file-name "scor"
1365 (or small-temporary-file-directory
1366 temporary-file-directory)))
1371 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1372 ;; Replace specified region with output from command.
1373 (let ((swap (and replace (< start end))))
1374 ;; Don't muck with mark unless REPLACE says we should.
1376 (and replace (push-mark))
1378 (call-process-region start end shell-file-name t
1382 nil shell-command-switch command))
1383 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1384 (and shell-buffer (not (eq shell-buffer (current-buffer)))
1385 (kill-buffer shell-buffer)))
1386 ;; Don't muck with mark unless REPLACE says we should.
1387 (and replace swap (exchange-point-and-mark)))
1388 ;; No prefix argument: put the output in a temp buffer,
1389 ;; replacing its entire contents.
1390 (let ((buffer (get-buffer-create
1391 (or output-buffer "*Shell Command Output*")))
1394 (if (eq buffer (current-buffer))
1395 ;; If the input is the same buffer as the output,
1396 ;; delete everything but the specified region,
1397 ;; then replace that region with the output.
1398 (progn (setq buffer-read-only nil)
1399 (delete-region (max start end) (point-max))
1400 (delete-region (point-min) (min start end))
1402 (call-process-region (point-min) (point-max)
1407 nil shell-command-switch
1409 ;; Clear the output buffer, then run the command with
1411 (let ((directory default-directory))
1414 (setq buffer-read-only nil)
1415 (if (not output-buffer)
1416 (setq default-directory directory))
1419 (call-process-region start end shell-file-name nil
1421 (list buffer error-file)
1423 nil shell-command-switch command)))
1424 (setq success (and exit-status (equal 0 exit-status)))
1425 ;; Report the amount of output.
1426 (if (with-current-buffer buffer (> (point-max) (point-min)))
1427 ;; There's some output, display it
1428 (display-message-or-buffer buffer)
1429 ;; No output; error?
1430 (message (if (and error-file
1431 (< 0 (nth 7 (file-attributes error-file))))
1432 "(Shell command %sed with some error output)"
1433 "(Shell command %sed with no output)")
1434 (if (equal 0 exit-status) "succeed" "fail"))
1435 (kill-buffer buffer)))))
1437 (when (and error-file (file-exists-p error-file))
1438 (if (< 0 (nth 7 (file-attributes error-file)))
1439 (with-current-buffer (get-buffer-create error-buffer)
1440 (let ((pos-from-end (- (point-max) (point))))
1443 ;; Do no formatting while reading error file,
1444 ;; because that can run a shell command, and we
1445 ;; don't want that to cause an infinite recursion.
1446 (format-insert-file error-file nil)
1447 ;; Put point after the inserted errors.
1448 (goto-char (- (point-max) pos-from-end)))
1449 (display-buffer (current-buffer))))
1450 (delete-file error-file))
1453 (defun shell-command-to-string (command)
1454 "Execute shell command COMMAND and return its output as a string."
1455 (with-output-to-string
1456 (with-current-buffer
1458 (call-process shell-file-name nil t nil shell-command-switch command))))
1460 (defvar universal-argument-map
1461 (let ((map (make-sparse-keymap)))
1462 (define-key map [t] 'universal-argument-other-key)
1463 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1464 (define-key map [switch-frame] nil)
1465 (define-key map [?\C-u] 'universal-argument-more)
1466 (define-key map [?-] 'universal-argument-minus)
1467 (define-key map [?0] 'digit-argument)
1468 (define-key map [?1] 'digit-argument)
1469 (define-key map [?2] 'digit-argument)
1470 (define-key map [?3] 'digit-argument)
1471 (define-key map [?4] 'digit-argument)
1472 (define-key map [?5] 'digit-argument)
1473 (define-key map [?6] 'digit-argument)
1474 (define-key map [?7] 'digit-argument)
1475 (define-key map [?8] 'digit-argument)
1476 (define-key map [?9] 'digit-argument)
1477 (define-key map [kp-0] 'digit-argument)
1478 (define-key map [kp-1] 'digit-argument)
1479 (define-key map [kp-2] 'digit-argument)
1480 (define-key map [kp-3] 'digit-argument)
1481 (define-key map [kp-4] 'digit-argument)
1482 (define-key map [kp-5] 'digit-argument)
1483 (define-key map [kp-6] 'digit-argument)
1484 (define-key map [kp-7] 'digit-argument)
1485 (define-key map [kp-8] 'digit-argument)
1486 (define-key map [kp-9] 'digit-argument)
1487 (define-key map [kp-subtract] 'universal-argument-minus)
1489 "Keymap used while processing \\[universal-argument].")
1491 (defvar universal-argument-num-events nil
1492 "Number of argument-specifying events read by `universal-argument'.
1493 `universal-argument-other-key' uses this to discard those events
1494 from (this-command-keys), and reread only the final command.")
1496 (defun universal-argument ()
1497 "Begin a numeric argument for the following command.
1498 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1499 \\[universal-argument] following the digits or minus sign ends the argument.
1500 \\[universal-argument] without digits or minus sign provides 4 as argument.
1501 Repeating \\[universal-argument] without digits or minus sign
1502 multiplies the argument by 4 each time.
1503 For some commands, just \\[universal-argument] by itself serves as a flag
1504 which is different in effect from any particular numeric argument.
1505 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1507 (setq prefix-arg (list 4))
1508 (setq universal-argument-num-events (length (this-command-keys)))
1509 (setq overriding-terminal-local-map universal-argument-map))
1511 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1512 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1513 (defun universal-argument-more (arg)
1516 (setq prefix-arg (list (* 4 (car arg))))
1518 (setq prefix-arg (list -4))
1519 (setq prefix-arg arg)
1520 (setq overriding-terminal-local-map nil)))
1521 (setq universal-argument-num-events (length (this-command-keys))))
1523 (defun negative-argument (arg)
1524 "Begin a negative numeric argument for the next command.
1525 \\[universal-argument] following digits or minus sign ends the argument."
1527 (cond ((integerp arg)
1528 (setq prefix-arg (- arg)))
1530 (setq prefix-arg nil))
1532 (setq prefix-arg '-)))
1533 (setq universal-argument-num-events (length (this-command-keys)))
1534 (setq overriding-terminal-local-map universal-argument-map))
1536 (defun digit-argument (arg)
1537 "Part of the numeric argument for the next command.
1538 \\[universal-argument] following digits or minus sign ends the argument."
1540 (let* ((char (if (integerp last-command-char)
1542 (get last-command-char 'ascii-character)))
1543 (digit (- (logand char ?\177) ?0)))
1544 (cond ((integerp arg)
1545 (setq prefix-arg (+ (* arg 10)
1546 (if (< arg 0) (- digit) digit))))
1548 ;; Treat -0 as just -, so that -01 will work.
1549 (setq prefix-arg (if (zerop digit) '- (- digit))))
1551 (setq prefix-arg digit))))
1552 (setq universal-argument-num-events (length (this-command-keys)))
1553 (setq overriding-terminal-local-map universal-argument-map))
1555 ;; For backward compatibility, minus with no modifiers is an ordinary
1556 ;; command if digits have already been entered.
1557 (defun universal-argument-minus (arg)
1560 (universal-argument-other-key arg)
1561 (negative-argument arg)))
1563 ;; Anything else terminates the argument and is left in the queue to be
1564 ;; executed as a command.
1565 (defun universal-argument-other-key (arg)
1567 (setq prefix-arg arg)
1568 (let* ((key (this-command-keys))
1569 (keylist (listify-key-sequence key)))
1570 (setq unread-command-events
1571 (append (nthcdr universal-argument-num-events keylist)
1572 unread-command-events)))
1573 (reset-this-command-lengths)
1574 (setq overriding-terminal-local-map nil))
1576 ;;;; Window system cut and paste hooks.
1578 (defvar interprogram-cut-function nil
1579 "Function to call to make a killed region available to other programs.
1581 Most window systems provide some sort of facility for cutting and
1582 pasting text between the windows of different programs.
1583 This variable holds a function that Emacs calls whenever text
1584 is put in the kill ring, to make the new kill available to other
1587 The function takes one or two arguments.
1588 The first argument, TEXT, is a string containing
1589 the text which should be made available.
1590 The second, PUSH, if non-nil means this is a \"new\" kill;
1591 nil means appending to an \"old\" kill.")
1593 (defvar interprogram-paste-function nil
1594 "Function to call to get text cut from other programs.
1596 Most window systems provide some sort of facility for cutting and
1597 pasting text between the windows of different programs.
1598 This variable holds a function that Emacs calls to obtain
1599 text that other programs have provided for pasting.
1601 The function should be called with no arguments. If the function
1602 returns nil, then no other program has provided such text, and the top
1603 of the Emacs kill ring should be used. If the function returns a
1604 string, that string should be put in the kill ring as the latest kill.
1606 Note that the function should return a string only if a program other
1607 than Emacs has provided a string for pasting; if Emacs provided the
1608 most recent string, the function should return nil. If it is
1609 difficult to tell whether Emacs or some other program provided the
1610 current string, it is probably good enough to return nil if the string
1611 is equal (according to `string=') to the last text Emacs provided.")
1615 ;;;; The kill ring data structure.
1617 (defvar kill-ring nil
1618 "List of killed text sequences.
1619 Since the kill ring is supposed to interact nicely with cut-and-paste
1620 facilities offered by window systems, use of this variable should
1621 interact nicely with `interprogram-cut-function' and
1622 `interprogram-paste-function'. The functions `kill-new',
1623 `kill-append', and `current-kill' are supposed to implement this
1624 interaction; you may want to use them instead of manipulating the kill
1627 (defcustom kill-ring-max 60
1628 "*Maximum length of kill ring before oldest elements are thrown away."
1632 (defvar kill-ring-yank-pointer nil
1633 "The tail of the kill ring whose car is the last thing yanked.")
1635 (defun kill-new (string &optional replace)
1636 "Make STRING the latest kill in the kill ring.
1637 Set the kill-ring-yank pointer to point to it.
1638 If `interprogram-cut-function' is non-nil, apply it to STRING.
1639 Optional second argument REPLACE non-nil means that STRING will replace
1640 the front of the kill ring, rather than being added to the list."
1641 (and (fboundp 'menu-bar-update-yank-menu)
1642 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1644 (setcar kill-ring string)
1645 (setq kill-ring (cons string kill-ring))
1646 (if (> (length kill-ring) kill-ring-max)
1647 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1648 (setq kill-ring-yank-pointer kill-ring)
1649 (if interprogram-cut-function
1650 (funcall interprogram-cut-function string (not replace))))
1652 (defun kill-append (string before-p)
1653 "Append STRING to the end of the latest kill in the kill ring.
1654 If BEFORE-P is non-nil, prepend STRING to the kill.
1655 If `interprogram-cut-function' is set, pass the resulting kill to
1657 (kill-new (if before-p
1658 (concat string (car kill-ring))
1659 (concat (car kill-ring) string)) t))
1661 (defun current-kill (n &optional do-not-move)
1662 "Rotate the yanking point by N places, and then return that kill.
1663 If N is zero, `interprogram-paste-function' is set, and calling it
1664 returns a string, then that string is added to the front of the
1665 kill ring and returned as the latest kill.
1666 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1667 yanking point; just return the Nth kill forward."
1668 (let ((interprogram-paste (and (= n 0)
1669 interprogram-paste-function
1670 (funcall interprogram-paste-function))))
1671 (if interprogram-paste
1673 ;; Disable the interprogram cut function when we add the new
1674 ;; text to the kill ring, so Emacs doesn't try to own the
1675 ;; selection, with identical text.
1676 (let ((interprogram-cut-function nil))
1677 (kill-new interprogram-paste))
1679 (or kill-ring (error "Kill ring is empty"))
1680 (let ((ARGth-kill-element
1681 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1685 (setq kill-ring-yank-pointer ARGth-kill-element))
1686 (car ARGth-kill-element)))))
1690 ;;;; Commands for manipulating the kill ring.
1692 (defcustom kill-read-only-ok nil
1693 "*Non-nil means don't signal an error for killing read-only text."
1697 (put 'text-read-only 'error-conditions
1698 '(text-read-only buffer-read-only error))
1699 (put 'text-read-only 'error-message "Text is read-only")
1701 (defun kill-region (beg end)
1702 "Kill between point and mark.
1703 The text is deleted but saved in the kill ring.
1704 The command \\[yank] can retrieve it from there.
1705 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
1706 If the buffer is read-only, Emacs will beep and refrain from deleting
1707 the text, but put the text in the kill ring anyway. This means that
1708 you can use the killing commands to copy text from a read-only buffer.
1710 This is the primitive for programs to kill text (as opposed to deleting it).
1711 Supply two arguments, character numbers indicating the stretch of text
1713 Any command that calls this function is a \"kill command\".
1714 If the previous command was also a kill command,
1715 the text killed this time appends to the text killed last time
1716 to make one entry in the kill ring."
1719 (let ((string (delete-and-extract-region beg end)))
1720 (when string ;STRING is nil if BEG = END
1721 ;; Add that string to the kill ring, one way or another.
1722 (if (eq last-command 'kill-region)
1723 (kill-append string (< end beg))
1725 (setq this-command 'kill-region))
1726 ((buffer-read-only text-read-only)
1727 ;; The code above failed because the buffer, or some of the characters
1728 ;; in the region, are read-only.
1729 ;; We should beep, in case the user just isn't aware of this.
1730 ;; However, there's no harm in putting
1731 ;; the region's text in the kill ring, anyway.
1732 (copy-region-as-kill beg end)
1733 ;; Set this-command now, so it will be set even if we get an error.
1734 (setq this-command 'kill-region)
1735 ;; This should barf, if appropriate, and give us the correct error.
1736 (if kill-read-only-ok
1737 (message "Read only text copied to kill ring")
1738 ;; Signal an error if the buffer is read-only.
1739 (barf-if-buffer-read-only)
1740 ;; If the buffer isn't read-only, the text is.
1741 (signal 'text-read-only (list (current-buffer)))))))
1743 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1744 ;; to get two copies of the text when the user accidentally types M-w and
1745 ;; then corrects it with the intended C-w.
1746 (defun copy-region-as-kill (beg end)
1747 "Save the region as if killed, but don't kill it.
1748 In Transient Mark mode, deactivate the mark.
1749 If `interprogram-cut-function' is non-nil, also save the text for a window
1750 system cut and paste."
1752 (if (eq last-command 'kill-region)
1753 (kill-append (buffer-substring beg end) (< end beg))
1754 (kill-new (buffer-substring beg end)))
1755 (if transient-mark-mode
1756 (setq deactivate-mark t))
1759 (defun kill-ring-save (beg end)
1760 "Save the region as if killed, but don't kill it.
1761 In Transient Mark mode, deactivate the mark.
1762 If `interprogram-cut-function' is non-nil, also save the text for a window
1763 system cut and paste.
1765 This command is similar to `copy-region-as-kill', except that it gives
1766 visual feedback indicating the extent of the region being copied."
1768 (copy-region-as-kill beg end)
1770 (let ((other-end (if (= (point) beg) end beg))
1772 ;; Inhibit quitting so we can make a quit here
1773 ;; look like a C-g typed as a command.
1775 (if (pos-visible-in-window-p other-end (selected-window))
1777 ;; Swap point and mark.
1778 (set-marker (mark-marker) (point) (current-buffer))
1779 (goto-char other-end)
1782 (set-marker (mark-marker) other-end (current-buffer))
1784 ;; If user quit, deactivate the mark
1785 ;; as C-g would as a command.
1786 (and quit-flag mark-active
1788 (let* ((killed-text (current-kill 0))
1789 (message-len (min (length killed-text) 40)))
1791 ;; Don't say "killed"; that is misleading.
1792 (message "Saved text until \"%s\""
1793 (substring killed-text (- message-len)))
1794 (message "Saved text from \"%s\""
1795 (substring killed-text 0 message-len))))))))
1797 (defun append-next-kill (&optional interactive)
1798 "Cause following command, if it kills, to append to previous kill.
1799 The argument is used for internal purposes; do not supply one."
1801 ;; We don't use (interactive-p), since that breaks kbd macros.
1804 (setq this-command 'kill-region)
1805 (message "If the next command is a kill, it will append"))
1806 (setq last-command 'kill-region)))
1810 (defun yank-pop (arg)
1811 "Replace just-yanked stretch of killed text with a different stretch.
1812 This command is allowed only immediately after a `yank' or a `yank-pop'.
1813 At such a time, the region contains a stretch of reinserted
1814 previously-killed text. `yank-pop' deletes that text and inserts in its
1815 place a different stretch of killed text.
1817 With no argument, the previous kill is inserted.
1818 With argument N, insert the Nth previous kill.
1819 If N is negative, this is a more recent kill.
1821 The sequence of kills wraps around, so that after the oldest one
1822 comes the newest one."
1824 (if (not (eq last-command 'yank))
1825 (error "Previous command was not a yank"))
1826 (setq this-command 'yank)
1827 (let ((inhibit-read-only t)
1828 (before (< (point) (mark t))))
1829 (delete-region (point) (mark t))
1830 (set-marker (mark-marker) (point) (current-buffer))
1831 (let ((opoint (point)))
1832 (insert (current-kill arg))
1833 (let ((inhibit-read-only t))
1834 (remove-text-properties opoint (point) '(read-only nil))))
1836 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1837 ;; It is cleaner to avoid activation, even though the command
1838 ;; loop would deactivate the mark because we inserted text.
1839 (goto-char (prog1 (mark t)
1840 (set-marker (mark-marker) (point) (current-buffer))))))
1843 (defun yank (&optional arg)
1844 "Reinsert the last stretch of killed text.
1845 More precisely, reinsert the stretch of killed text most recently
1846 killed OR yanked. Put point at end, and set mark at beginning.
1847 With just C-u as argument, same but put point at beginning (and mark at end).
1848 With argument N, reinsert the Nth most recently killed stretch of killed
1850 See also the command \\[yank-pop]."
1852 ;; If we don't get all the way thru, make last-command indicate that
1853 ;; for the following command.
1854 (setq this-command t)
1856 (let ((opoint (point)))
1857 (insert (current-kill (cond
1861 (let ((inhibit-read-only t))
1862 (remove-text-properties opoint (point) '(read-only nil))))
1864 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1865 ;; It is cleaner to avoid activation, even though the command
1866 ;; loop would deactivate the mark because we inserted text.
1867 (goto-char (prog1 (mark t)
1868 (set-marker (mark-marker) (point) (current-buffer)))))
1869 ;; If we do get all the way thru, make this-command indicate that.
1870 (setq this-command 'yank)
1873 (defun rotate-yank-pointer (arg)
1874 "Rotate the yanking point in the kill ring.
1875 With argument, rotate that many kills forward (or backward, if negative)."
1879 ;; Some kill commands.
1881 ;; Internal subroutine of delete-char
1882 (defun kill-forward-chars (arg)
1883 (if (listp arg) (setq arg (car arg)))
1884 (if (eq arg '-) (setq arg -1))
1885 (kill-region (point) (forward-point arg)))
1887 ;; Internal subroutine of backward-delete-char
1888 (defun kill-backward-chars (arg)
1889 (if (listp arg) (setq arg (car arg)))
1890 (if (eq arg '-) (setq arg -1))
1891 (kill-region (point) (forward-point (- arg))))
1893 (defcustom backward-delete-char-untabify-method 'untabify
1894 "*The method for untabifying when deleting backward.
1895 Can be `untabify' -- turn a tab to many spaces, then delete one space;
1896 `hungry' -- delete all whitespace, both tabs and spaces;
1897 `all' -- delete all whitespace, including tabs, spaces and newlines;
1898 nil -- just delete one character."
1899 :type '(choice (const untabify) (const hungry) (const all) (const nil))
1902 (defun backward-delete-char-untabify (arg &optional killp)
1903 "Delete characters backward, changing tabs into spaces.
1904 The exact behavior depends on `backward-delete-char-untabify-method'.
1905 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
1906 Interactively, ARG is the prefix arg (default 1)
1907 and KILLP is t if a prefix arg was specified."
1908 (interactive "*p\nP")
1909 (when (eq backward-delete-char-untabify-method 'untabify)
1912 (while (and (> count 0) (not (bobp)))
1913 (if (= (preceding-char) ?\t)
1914 (let ((col (current-column)))
1916 (setq col (- col (current-column)))
1917 (insert-char ?\ col)
1920 (setq count (1- count))))))
1921 (delete-backward-char
1922 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
1923 ((eq backward-delete-char-untabify-method 'all)
1926 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
1928 (+ arg (if (zerop wh) 0 (1- wh))))
1932 (defun zap-to-char (arg char)
1933 "Kill up to and including ARG'th occurrence of CHAR.
1934 Case is ignored if `case-fold-search' is non-nil in the current buffer.
1935 Goes backward if ARG is negative; error if CHAR not found."
1936 (interactive "p\ncZap to char: ")
1937 (kill-region (point) (progn
1938 (search-forward (char-to-string char) nil nil arg)
1939 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
1942 ;; kill-line and its subroutines.
1944 (defcustom kill-whole-line nil
1945 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
1949 (defun kill-line (&optional arg)
1950 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
1951 With prefix argument, kill that many lines from point.
1952 Negative arguments kill lines backward.
1953 With zero argument, kills the text before point on the current line.
1955 When calling from a program, nil means \"no arg\",
1956 a number counts as a prefix arg.
1958 To kill a whole line, when point is not at the beginning, type \
1959 \\[beginning-of-line] \\[kill-line] \\[kill-line].
1961 If `kill-whole-line' is non-nil, then this command kills the whole line
1962 including its terminating newline, when used at the beginning of a line
1963 with no argument. As a consequence, you can always kill a whole line
1964 by typing \\[beginning-of-line] \\[kill-line]."
1966 (kill-region (point)
1967 ;; It is better to move point to the other end of the kill
1968 ;; before killing. That way, in a read-only buffer, point
1969 ;; moves across the text that is copied to the kill ring.
1970 ;; The choice has no effect on undo now that undo records
1971 ;; the value of point from before the command was run.
1974 (forward-visible-line (prefix-numeric-value arg))
1976 (signal 'end-of-buffer nil))
1977 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
1978 (forward-visible-line 1)
1979 (end-of-visible-line)))
1982 (defun forward-visible-line (arg)
1983 "Move forward by ARG lines, ignoring currently invisible newlines only.
1984 If ARG is negative, move backward -ARG lines.
1985 If ARG is zero, move to the beginning of the current line."
1989 (or (zerop (forward-line 1))
1990 (signal 'end-of-buffer nil))
1991 ;; If the following character is currently invisible,
1992 ;; skip all characters with that same `invisible' property value,
1993 ;; then find the next newline.
1994 (while (and (not (eobp))
1996 (get-char-property (point) 'invisible)))
1997 (if (eq buffer-invisibility-spec t)
1999 (or (memq prop buffer-invisibility-spec)
2000 (assq prop buffer-invisibility-spec)))))
2002 (if (get-text-property (point) 'invisible)
2003 (or (next-single-property-change (point) 'invisible)
2005 (next-overlay-change (point))))
2006 (or (zerop (forward-line 1))
2007 (signal 'end-of-buffer nil)))
2008 (setq arg (1- arg)))
2010 (while (or first (< arg 0))
2013 (or (zerop (forward-line -1))
2014 (signal 'beginning-of-buffer nil)))
2015 (while (and (not (bobp))
2017 (get-char-property (1- (point)) 'invisible)))
2018 (if (eq buffer-invisibility-spec t)
2020 (or (memq prop buffer-invisibility-spec)
2021 (assq prop buffer-invisibility-spec)))))
2023 (if (get-text-property (1- (point)) 'invisible)
2024 (or (previous-single-property-change (point) 'invisible)
2026 (previous-overlay-change (point))))
2027 (or (zerop (forward-line -1))
2028 (signal 'beginning-of-buffer nil)))
2030 (setq arg (1+ arg)))))
2031 ((beginning-of-buffer end-of-buffer)
2034 (defun end-of-visible-line ()
2035 "Move to end of current visible line."
2037 ;; If the following character is currently invisible,
2038 ;; skip all characters with that same `invisible' property value,
2039 ;; then find the next newline.
2040 (while (and (not (eobp))
2042 (get-char-property (point) 'invisible)))
2043 (if (eq buffer-invisibility-spec t)
2045 (or (memq prop buffer-invisibility-spec)
2046 (assq prop buffer-invisibility-spec)))))
2047 (if (get-text-property (point) 'invisible)
2048 (goto-char (next-single-property-change (point) 'invisible))
2049 (goto-char (next-overlay-change (point))))
2052 (defun insert-buffer (buffer)
2053 "Insert after point the contents of BUFFER.
2054 Puts mark after the inserted text.
2055 BUFFER may be a buffer or a buffer name.
2057 This function is meant for the user to run interactively.
2058 Don't call it from programs!"
2062 (barf-if-buffer-read-only)
2063 (read-buffer "Insert buffer: "
2064 (if (eq (selected-window) (next-window (selected-window)))
2065 (other-buffer (current-buffer))
2066 (window-buffer (next-window (selected-window))))
2068 (or (bufferp buffer)
2069 (setq buffer (get-buffer buffer)))
2070 (let (start end newmark)
2074 (setq start (point-min) end (point-max)))
2075 (insert-buffer-substring buffer start end)
2076 (setq newmark (point)))
2077 (push-mark newmark))
2080 (defun append-to-buffer (buffer start end)
2081 "Append to specified buffer the text of the region.
2082 It is inserted into that buffer before its point.
2084 When calling from a program, give three arguments:
2085 BUFFER (or buffer name), START and END.
2086 START and END specify the portion of the current buffer to be copied."
2088 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2089 (region-beginning) (region-end)))
2090 (let ((oldbuf (current-buffer)))
2092 (let* ((append-to (get-buffer-create buffer))
2093 (windows (get-buffer-window-list append-to t t))
2095 (set-buffer append-to)
2096 (setq point (point))
2097 (barf-if-buffer-read-only)
2098 (insert-buffer-substring oldbuf start end)
2099 (dolist (window windows)
2100 (when (= (window-point window) point)
2101 (set-window-point window (point))))))))
2103 (defun prepend-to-buffer (buffer start end)
2104 "Prepend to specified buffer the text of the region.
2105 It is inserted into that buffer after its point.
2107 When calling from a program, give three arguments:
2108 BUFFER (or buffer name), START and END.
2109 START and END specify the portion of the current buffer to be copied."
2110 (interactive "BPrepend to buffer: \nr")
2111 (let ((oldbuf (current-buffer)))
2113 (set-buffer (get-buffer-create buffer))
2114 (barf-if-buffer-read-only)
2116 (insert-buffer-substring oldbuf start end)))))
2118 (defun copy-to-buffer (buffer start end)
2119 "Copy to specified buffer the text of the region.
2120 It is inserted into that buffer, replacing existing text there.
2122 When calling from a program, give three arguments:
2123 BUFFER (or buffer name), START and END.
2124 START and END specify the portion of the current buffer to be copied."
2125 (interactive "BCopy to buffer: \nr")
2126 (let ((oldbuf (current-buffer)))
2128 (set-buffer (get-buffer-create buffer))
2129 (barf-if-buffer-read-only)
2132 (insert-buffer-substring oldbuf start end)))))
2134 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2135 (put 'mark-inactive 'error-message "The mark is not active now")
2137 (defun mark (&optional force)
2138 "Return this buffer's mark value as integer; error if mark inactive.
2139 If optional argument FORCE is non-nil, access the mark value
2140 even if the mark is not currently active, and return nil
2141 if there is no mark at all.
2143 If you are using this in an editing command, you are most likely making
2144 a mistake; see the documentation of `set-mark'."
2145 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2146 (marker-position (mark-marker))
2147 (signal 'mark-inactive nil)))
2149 ;; Many places set mark-active directly, and several of them failed to also
2150 ;; run deactivate-mark-hook. This shorthand should simplify.
2151 (defsubst deactivate-mark ()
2152 "Deactivate the mark by setting `mark-active' to nil.
2153 \(That makes a difference only in Transient Mark mode.)
2154 Also runs the hook `deactivate-mark-hook'."
2155 (if transient-mark-mode
2157 (setq mark-active nil)
2158 (run-hooks 'deactivate-mark-hook))))
2160 (defun set-mark (pos)
2161 "Set this buffer's mark to POS. Don't use this function!
2162 That is to say, don't use this function unless you want
2163 the user to see that the mark has moved, and you want the previous
2164 mark position to be lost.
2166 Normally, when a new mark is set, the old one should go on the stack.
2167 This is why most applications should use push-mark, not set-mark.
2169 Novice Emacs Lisp programmers often try to use the mark for the wrong
2170 purposes. The mark saves a location for the user's convenience.
2171 Most editing commands should not alter the mark.
2172 To remember a location for internal use in the Lisp program,
2173 store it in a Lisp variable. Example:
2175 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2179 (setq mark-active t)
2180 (run-hooks 'activate-mark-hook)
2181 (set-marker (mark-marker) pos (current-buffer)))
2182 ;; Normally we never clear mark-active except in Transient Mark mode.
2183 ;; But when we actually clear out the mark value too,
2184 ;; we must clear mark-active in any mode.
2185 (setq mark-active nil)
2186 (run-hooks 'deactivate-mark-hook)
2187 (set-marker (mark-marker) nil)))
2189 (defvar mark-ring nil
2190 "The list of former marks of the current buffer, most recent first.")
2191 (make-variable-buffer-local 'mark-ring)
2192 (put 'mark-ring 'permanent-local t)
2194 (defcustom mark-ring-max 16
2195 "*Maximum size of mark ring. Start discarding off end if gets this big."
2197 :group 'editing-basics)
2199 (defvar global-mark-ring nil
2200 "The list of saved global marks, most recent first.")
2202 (defcustom global-mark-ring-max 16
2203 "*Maximum size of global mark ring. \
2204 Start discarding off end if gets this big."
2206 :group 'editing-basics)
2208 (defun set-mark-command (arg)
2209 "Set mark at where point is, or jump to mark.
2210 With no prefix argument, set mark, push old mark position on local mark
2211 ring, and push mark on global mark ring.
2212 With argument, jump to mark, and pop a new position for mark off the ring
2213 \(does not affect global mark ring\).
2215 Novice Emacs Lisp programmers often try to use the mark for the wrong
2216 purposes. See the documentation of `set-mark' for more information."
2220 (push-mark nil nil t))
2222 (error "No mark set in this buffer")
2223 (goto-char (mark t))
2226 (defun push-mark (&optional location nomsg activate)
2227 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2228 If the last global mark pushed was not in the current buffer,
2229 also push LOCATION on the global mark ring.
2230 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2231 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2233 Novice Emacs Lisp programmers often try to use the mark for the wrong
2234 purposes. See the documentation of `set-mark' for more information.
2236 In Transient Mark mode, this does not activate the mark."
2239 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2240 (if (> (length mark-ring) mark-ring-max)
2242 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2243 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
2244 (set-marker (mark-marker) (or location (point)) (current-buffer))
2245 ;; Now push the mark on the global mark ring.
2246 (if (and global-mark-ring
2247 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2248 ;; The last global mark pushed was in this same buffer.
2249 ;; Don't push another one.
2251 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2252 (if (> (length global-mark-ring) global-mark-ring-max)
2254 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
2256 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2257 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2258 (message "Mark set"))
2259 (if (or activate (not transient-mark-mode))
2260 (set-mark (mark t)))
2264 "Pop off mark ring into the buffer's actual mark.
2265 Does not set point. Does nothing if mark ring is empty."
2268 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2269 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2271 (move-marker (car mark-ring) nil)
2272 (if (null (mark t)) (ding))
2273 (setq mark-ring (cdr mark-ring)))))
2275 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2276 (defun exchange-point-and-mark ()
2277 "Put the mark where point is now, and point where the mark is now.
2278 This command works even when the mark is not active,
2279 and it reactivates the mark."
2281 (let ((omark (mark t)))
2283 (error "No mark set in this buffer"))
2288 (defun transient-mark-mode (arg)
2289 "Toggle Transient Mark mode.
2290 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2292 In Transient Mark mode, when the mark is active, the region is highlighted.
2293 Changing the buffer \"deactivates\" the mark.
2294 So do certain other operations that set the mark
2295 but whose main purpose is something else--for example,
2296 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
2298 (setq transient-mark-mode
2300 (not transient-mark-mode)
2301 (> (prefix-numeric-value arg) 0)))
2303 (if transient-mark-mode
2304 (message "Transient Mark mode enabled")
2305 (message "Transient Mark mode disabled"))))
2307 (defun pop-global-mark ()
2308 "Pop off global mark ring and jump to the top location."
2310 ;; Pop entries which refer to non-existent buffers.
2311 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2312 (setq global-mark-ring (cdr global-mark-ring)))
2313 (or global-mark-ring
2314 (error "No global mark set"))
2315 (let* ((marker (car global-mark-ring))
2316 (buffer (marker-buffer marker))
2317 (position (marker-position marker)))
2318 (setq global-mark-ring (nconc (cdr global-mark-ring)
2319 (list (car global-mark-ring))))
2321 (or (and (>= position (point-min))
2322 (<= position (point-max)))
2324 (goto-char position)
2325 (switch-to-buffer buffer)))
2327 (defcustom next-line-add-newlines t
2328 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2330 :group 'editing-basics)
2332 (defun next-line (arg)
2333 "Move cursor vertically down ARG lines.
2334 If there is no character in the target line exactly under the current column,
2335 the cursor is positioned after the character in that line which spans this
2336 column, or at the end of the line if it is not long enough.
2337 If there is no line in the buffer after this one, behavior depends on the
2338 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2339 to create a line, and moves the cursor to that line. Otherwise it moves the
2340 cursor to the end of the buffer.
2342 The command \\[set-goal-column] can be used to create
2343 a semipermanent goal column for this command.
2344 Then instead of trying to move exactly vertically (or as close as possible),
2345 this command moves to the specified goal column (or as close as possible).
2346 The goal column is stored in the variable `goal-column', which is nil
2347 when there is no goal column.
2349 If you are thinking of using this in a Lisp program, consider
2350 using `forward-line' instead. It is usually easier to use
2351 and more reliable (no dependence on goal column, etc.)."
2353 (if (and next-line-add-newlines (= arg 1))
2354 (let ((opoint (point)))
2363 ((beginning-of-buffer end-of-buffer) (ding)))
2367 (defun previous-line (arg)
2368 "Move cursor vertically up ARG lines.
2369 If there is no character in the target line exactly over the current column,
2370 the cursor is positioned after the character in that line which spans this
2371 column, or at the end of the line if it is not long enough.
2373 The command \\[set-goal-column] can be used to create
2374 a semipermanent goal column for this command.
2375 Then instead of trying to move exactly vertically (or as close as possible),
2376 this command moves to the specified goal column (or as close as possible).
2377 The goal column is stored in the variable `goal-column', which is nil
2378 when there is no goal column.
2380 If you are thinking of using this in a Lisp program, consider using
2381 `forward-line' with a negative argument instead. It is usually easier
2382 to use and more reliable (no dependence on goal column, etc.)."
2387 ((beginning-of-buffer end-of-buffer) (ding)))
2388 (line-move (- arg)))
2391 (defcustom track-eol nil
2392 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2393 This means moving to the end of each line moved onto.
2394 The beginning of a blank line does not count as the end of a line."
2396 :group 'editing-basics)
2398 (defcustom goal-column nil
2399 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2400 :type '(choice integer
2401 (const :tag "None" nil))
2402 :group 'editing-basics)
2403 (make-variable-buffer-local 'goal-column)
2405 (defvar temporary-goal-column 0
2406 "Current goal column for vertical motion.
2407 It is the column where point was
2408 at the start of current run of vertical motion commands.
2409 When the `track-eol' feature is doing its job, the value is 9999.")
2411 (defcustom line-move-ignore-invisible nil
2412 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2413 Outline mode sets this."
2415 :group 'editing-basics)
2417 ;; This is the guts of next-line and previous-line.
2418 ;; Arg says how many lines to move.
2419 (defun line-move (arg)
2420 ;; Don't run any point-motion hooks, and disregard intangibility,
2421 ;; for intermediate positions.
2422 (let ((inhibit-point-motion-hooks t)
2424 new line-end line-beg)
2427 (if (not (or (eq last-command 'next-line)
2428 (eq last-command 'previous-line)))
2429 (setq temporary-goal-column
2430 (if (and track-eol (eolp)
2431 ;; Don't count beg of empty line as end of line
2432 ;; unless we just did explicit end-of-line.
2433 (or (not (bolp)) (eq last-command 'end-of-line)))
2436 (if (and (not (integerp selective-display))
2437 (not line-move-ignore-invisible))
2438 ;; Use just newline characters.
2440 (progn (if (> arg 1) (forward-line (1- arg)))
2441 ;; This way of moving forward ARG lines
2442 ;; verifies that we have a newline after the last one.
2443 ;; It doesn't get confused by intangible text.
2445 (zerop (forward-line 1)))
2446 (and (zerop (forward-line arg))
2448 (signal (if (< arg 0)
2449 'beginning-of-buffer
2452 ;; Move by arg lines, but ignore invisible ones.
2455 (and (zerop (vertical-motion 1))
2456 (signal 'end-of-buffer nil))
2457 ;; If the following character is currently invisible,
2458 ;; skip all characters with that same `invisible' property value.
2459 (while (and (not (eobp))
2461 (get-char-property (point) 'invisible)))
2462 (if (eq buffer-invisibility-spec t)
2464 (or (memq prop buffer-invisibility-spec)
2465 (assq prop buffer-invisibility-spec)))))
2466 (if (get-text-property (point) 'invisible)
2467 (goto-char (next-single-property-change (point) 'invisible))
2468 (goto-char (next-overlay-change (point)))))
2469 (setq arg (1- arg)))
2472 (and (zerop (vertical-motion -1))
2473 (signal 'beginning-of-buffer nil))
2474 (while (and (not (bobp))
2476 (get-char-property (1- (point)) 'invisible)))
2477 (if (eq buffer-invisibility-spec t)
2479 (or (memq prop buffer-invisibility-spec)
2480 (assq prop buffer-invisibility-spec)))))
2481 (if (get-text-property (1- (point)) 'invisible)
2482 (goto-char (previous-single-property-change (point) 'invisible))
2483 (goto-char (previous-overlay-change (point)))))
2484 (setq arg (1+ arg))))
2485 (let ((buffer-invisibility-spec nil))
2486 (move-to-column (or goal-column temporary-goal-column))))
2488 ;; If we are moving into some intangible text,
2489 ;; look for following text on the same line which isn't intangible
2491 (setq line-end (save-excursion (end-of-line) (point)))
2492 (setq line-beg (save-excursion (beginning-of-line) (point)))
2493 (let ((after (and (< new (point-max))
2494 (get-char-property new 'intangible)))
2495 (before (and (> new (point-min))
2496 (get-char-property (1- new) 'intangible))))
2497 (when (and before (eq before after)
2499 (goto-char (point-min))
2500 (let ((inhibit-point-motion-hooks nil))
2502 (if (<= new line-end)
2503 (setq new (point)))))
2504 ;; NEW is where we want to move to.
2505 ;; LINE-BEG and LINE-END are the beginning and end of the line.
2506 ;; Move there in just one step, from our starting position,
2507 ;; with intangibility and point-motion hooks enabled this time.
2509 (setq inhibit-point-motion-hooks nil)
2510 (goto-char (constrain-to-field new opoint nil t
2511 'inhibit-line-move-field-capture))
2512 ;; If intangibility processing moved us to a different line,
2513 ;; readjust the horizontal position within the line we ended up at.
2514 (when (or (< (point) line-beg) (> (point) line-end))
2516 (setq inhibit-point-motion-hooks t)
2517 (setq line-end (save-excursion (end-of-line) (point)))
2519 (setq line-beg (point))
2520 (let ((buffer-invisibility-spec nil))
2521 (move-to-column (or goal-column temporary-goal-column)))
2522 (if (<= (point) line-end)
2524 (goto-char (point-min))
2525 (setq inhibit-point-motion-hooks nil)
2526 (goto-char (constrain-to-field new opoint nil t
2527 'inhibit-line-move-field-capture))
2531 ;;; Many people have said they rarely use this feature, and often type
2532 ;;; it by accident. Maybe it shouldn't even be on a key.
2533 (put 'set-goal-column 'disabled t)
2535 (defun set-goal-column (arg)
2536 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2537 Those commands will move to this position in the line moved to
2538 rather than trying to keep the same horizontal position.
2539 With a non-nil argument, clears out the goal column
2540 so that \\[next-line] and \\[previous-line] resume vertical motion.
2541 The goal column is stored in the variable `goal-column'."
2545 (setq goal-column nil)
2546 (message "No goal column"))
2547 (setq goal-column (current-column))
2548 (message (substitute-command-keys
2549 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2554 (defun scroll-other-window-down (lines)
2555 "Scroll the \"other window\" down.
2556 For more details, see the documentation for `scroll-other-window'."
2558 (scroll-other-window
2559 ;; Just invert the argument's meaning.
2560 ;; We can do that without knowing which window it will be.
2561 (if (eq lines '-) nil
2563 (- (prefix-numeric-value lines))))))
2564 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2566 (defun beginning-of-buffer-other-window (arg)
2567 "Move point to the beginning of the buffer in the other window.
2568 Leave mark at previous position.
2569 With arg N, put point N/10 of the way from the true beginning."
2571 (let ((orig-window (selected-window))
2572 (window (other-window-for-scrolling)))
2573 ;; We use unwind-protect rather than save-window-excursion
2574 ;; because the latter would preserve the things we want to change.
2577 (select-window window)
2578 ;; Set point and mark in that window's buffer.
2579 (beginning-of-buffer arg)
2580 ;; Set point accordingly.
2582 (select-window orig-window))))
2584 (defun end-of-buffer-other-window (arg)
2585 "Move point to the end of the buffer in the other window.
2586 Leave mark at previous position.
2587 With arg N, put point N/10 of the way from the true end."
2589 ;; See beginning-of-buffer-other-window for comments.
2590 (let ((orig-window (selected-window))
2591 (window (other-window-for-scrolling)))
2594 (select-window window)
2597 (select-window orig-window))))
2599 (defun transpose-chars (arg)
2600 "Interchange characters around point, moving forward one character.
2601 With prefix arg ARG, effect is to take character before point
2602 and drag it forward past ARG other characters (backward if ARG negative).
2603 If no argument and at end of line, the previous two chars are exchanged."
2605 (and (null arg) (eolp) (forward-char -1))
2606 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2608 (defun transpose-words (arg)
2609 "Interchange words around point, leaving point at end of them.
2610 With prefix arg ARG, effect is to take word before or around point
2611 and drag it forward past ARG other words (backward if ARG negative).
2612 If ARG is zero, the words around or after point and around or after mark
2615 (transpose-subr 'forward-word arg))
2617 (defun transpose-sexps (arg)
2618 "Like \\[transpose-words] but applies to sexps.
2619 Does not work on a sexp that point is in the middle of
2620 if it is a list or string."
2622 (transpose-subr 'forward-sexp arg))
2624 (defun transpose-lines (arg)
2625 "Exchange current line and previous line, leaving point after both.
2626 With argument ARG, takes previous line and moves it past ARG lines.
2627 With argument 0, interchanges line point is in with line mark is in."
2629 (transpose-subr (function
2633 ;; Move forward over ARG lines,
2634 ;; but create newlines if necessary.
2635 (setq arg (forward-line arg))
2636 (if (/= (preceding-char) ?\n)
2637 (setq arg (1+ arg)))
2640 (forward-line arg))))
2643 (defvar transpose-subr-start1)
2644 (defvar transpose-subr-start2)
2645 (defvar transpose-subr-end1)
2646 (defvar transpose-subr-end2)
2648 (defun transpose-subr (mover arg)
2649 (let (transpose-subr-start1
2651 transpose-subr-start2
2652 transpose-subr-end2)
2657 (setq transpose-subr-end2 (point))
2659 (setq transpose-subr-start2 (point))
2662 (setq transpose-subr-end1 (point))
2664 (setq transpose-subr-start1 (point))
2666 (exchange-point-and-mark))
2670 (setq transpose-subr-start1 (point))
2672 (setq transpose-subr-end1 (point))
2674 (setq transpose-subr-end2 (point))
2675 (funcall mover (- arg))
2676 (setq transpose-subr-start2 (point))
2678 (goto-char transpose-subr-end2))
2680 (setq transpose-subr-start2 (point))
2682 (setq transpose-subr-end2 (point))
2683 (funcall mover (1- arg))
2684 (setq transpose-subr-start1 (point))
2685 (funcall mover (- arg))
2686 (setq transpose-subr-end1 (point))
2687 (transpose-subr-1)))))
2689 (defun transpose-subr-1 ()
2690 (if (> (min transpose-subr-end1 transpose-subr-end2)
2691 (max transpose-subr-start1 transpose-subr-start2))
2692 (error "Don't have two things to transpose"))
2693 (let* ((word1 (buffer-substring transpose-subr-start1 transpose-subr-end1))
2694 (len1 (length word1))
2695 (word2 (buffer-substring transpose-subr-start2 transpose-subr-end2))
2696 (len2 (length word2)))
2697 (delete-region transpose-subr-start2 transpose-subr-end2)
2698 (goto-char transpose-subr-start2)
2700 (goto-char (if (< transpose-subr-start1 transpose-subr-start2)
2701 transpose-subr-start1
2702 (+ transpose-subr-start1 (- len1 len2))))
2703 (delete-region (point) (+ (point) len1))
2706 (defun backward-word (arg)
2707 "Move backward until encountering the end of a word.
2708 With argument, do this that many times."
2710 (forward-word (- arg)))
2712 (defun mark-word (arg)
2713 "Set mark arg words away from point."
2721 (defun kill-word (arg)
2722 "Kill characters forward until encountering the end of a word.
2723 With argument, do this that many times."
2725 (kill-region (point) (progn (forward-word arg) (point))))
2727 (defun backward-kill-word (arg)
2728 "Kill characters backward until encountering the end of a word.
2729 With argument, do this that many times."
2731 (kill-word (- arg)))
2733 (defun current-word (&optional strict)
2734 "Return the word point is on (or a nearby word) as a string.
2735 If optional arg STRICT is non-nil, return nil unless point is within
2736 or adjacent to a word."
2738 (let ((oldpoint (point)) (start (point)) (end (point)))
2739 (skip-syntax-backward "w_") (setq start (point))
2740 (goto-char oldpoint)
2741 (skip-syntax-forward "w_") (setq end (point))
2742 (if (and (eq start oldpoint) (eq end oldpoint))
2743 ;; Point is neither within nor adjacent to a word.
2746 ;; Look for preceding word in same line.
2747 (skip-syntax-backward "^w_"
2748 (save-excursion (beginning-of-line)
2751 ;; No preceding word in same line.
2752 ;; Look for following word in same line.
2754 (skip-syntax-forward "^w_"
2755 (save-excursion (end-of-line)
2757 (setq start (point))
2758 (skip-syntax-forward "w_")
2761 (skip-syntax-backward "w_")
2762 (setq start (point)))
2763 (buffer-substring-no-properties start end)))
2764 (buffer-substring-no-properties start end)))))
2766 (defcustom fill-prefix nil
2767 "*String for filling to insert at front of new line, or nil for none.
2768 Setting this variable automatically makes it local to the current buffer."
2769 :type '(choice (const :tag "None" nil)
2772 (make-variable-buffer-local 'fill-prefix)
2774 (defcustom auto-fill-inhibit-regexp nil
2775 "*Regexp to match lines which should not be auto-filled."
2776 :type '(choice (const :tag "None" nil)
2780 (defvar comment-line-break-function 'indent-new-comment-line
2781 "*Mode-specific function which line breaks and continues a comment.
2783 This function is only called during auto-filling of a comment section.
2784 The function should take a single optional argument, which is a flag
2785 indicating whether it should use soft newlines.
2787 Setting this variable automatically makes it local to the current buffer.")
2789 ;; This function is used as the auto-fill-function of a buffer
2790 ;; when Auto-Fill mode is enabled.
2791 ;; It returns t if it really did any work.
2792 ;; (Actually some major modes use a different auto-fill function,
2793 ;; but this one is the default one.)
2794 (defun do-auto-fill ()
2795 (let (fc justify bol give-up
2796 (fill-prefix fill-prefix))
2797 (if (or (not (setq justify (current-justification)))
2798 (null (setq fc (current-fill-column)))
2799 (and (eq justify 'left)
2800 (<= (current-column) fc))
2801 (save-excursion (beginning-of-line)
2803 (and auto-fill-inhibit-regexp
2804 (looking-at auto-fill-inhibit-regexp))))
2805 nil ;; Auto-filling not required
2806 (if (memq justify '(full center right))
2807 (save-excursion (unjustify-current-line)))
2809 ;; Choose a fill-prefix automatically.
2810 (if (and adaptive-fill-mode
2811 (or (null fill-prefix) (string= fill-prefix "")))
2813 (fill-context-prefix
2814 (save-excursion (backward-paragraph 1) (point))
2815 (save-excursion (forward-paragraph 1) (point)))))
2816 (and prefix (not (equal prefix ""))
2817 (setq fill-prefix prefix))))
2819 (while (and (not give-up) (> (current-column) fc))
2820 ;; Determine where to split the line.
2823 (let ((opoint (point))
2828 (setq after-prefix (point))
2830 (looking-at (regexp-quote fill-prefix))
2831 (setq after-prefix (match-end 0)))
2832 (move-to-column (1+ fc))
2833 ;; Move back to the point where we can break the line.
2834 ;; We break the line between word or
2835 ;; after/before the character which has character
2836 ;; category `|'. We search space, \c| followed by
2837 ;; a character, or \c| following a character. If
2838 ;; not found, place the point at beginning of line.
2840 ;; If this is after period and a single space,
2841 ;; move back once more--we don't want to break
2842 ;; the line there and make it look like a
2846 sentence-end-double-space
2847 (save-excursion (forward-char -1)
2848 (and (looking-at "\\. ")
2849 (not (looking-at "\\. ")))))
2852 fill-nobreak-predicate
2853 (funcall fill-nobreak-predicate)))
2855 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
2856 ;; If we find nowhere on the line to break it,
2857 ;; break after one word. Set bounce to t
2858 ;; so we will not keep going in this while loop.
2859 (if (<= (point) after-prefix)
2861 (goto-char after-prefix)
2862 (re-search-forward "[ \t]" opoint t)
2864 (if (looking-at "[ \t]")
2865 ;; Break the line at word boundary.
2866 (skip-chars-backward " \t")
2867 ;; Break the line after/before \c|.
2869 (if enable-multibyte-characters
2870 ;; If we are going to break the line after or
2871 ;; before a non-ascii character, we may have
2872 ;; to run a special function for the charset
2873 ;; of the character to find the correct break
2875 (if (not (and (eq (charset-after (1- (point))) 'ascii)
2876 (eq (charset-after (point)) 'ascii)))
2877 (fill-find-break-point after-prefix)))
2879 ;; Let fill-point be set to the place where we end up.
2880 ;; But move back before any whitespace here.
2881 (skip-chars-backward " \t")
2884 ;; See whether the place we found is any good.
2886 (goto-char fill-point)
2888 ;; There is no use breaking at end of line.
2889 (not (save-excursion (skip-chars-forward " ") (eolp)))
2890 ;; It is futile to split at the end of the prefix
2891 ;; since we would just insert the prefix again.
2892 (not (and after-prefix (<= (point) after-prefix)))
2893 ;; Don't split right after a comment starter
2894 ;; since we would just make another comment starter.
2895 (not (and comment-start-skip
2896 (let ((limit (point)))
2898 (and (re-search-forward comment-start-skip
2900 (eq (point) limit)))))))
2901 ;; Ok, we have a useful place to break the line. Do it.
2902 (let ((prev-column (current-column)))
2903 ;; If point is at the fill-point, do not `save-excursion'.
2904 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2905 ;; point will end up before it rather than after it.
2907 (skip-chars-backward " \t")
2908 (= (point) fill-point))
2909 (funcall comment-line-break-function t)
2911 (goto-char fill-point)
2912 (funcall comment-line-break-function t)))
2913 ;; Now do justification, if required
2914 (if (not (eq justify 'left))
2917 (justify-current-line justify nil t)))
2918 ;; If making the new line didn't reduce the hpos of
2919 ;; the end of the line, then give up now;
2920 ;; trying again will not help.
2921 (if (>= (current-column) prev-column)
2923 ;; No good place to break => stop trying.
2925 ;; Justify last line.
2926 (justify-current-line justify t t)
2929 (defvar normal-auto-fill-function 'do-auto-fill
2930 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
2931 Some major modes set this.")
2933 (defun auto-fill-mode (&optional arg)
2934 "Toggle Auto Fill mode.
2935 With arg, turn Auto Fill mode on if and only if arg is positive.
2936 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
2937 automatically breaks the line at a previous space.
2939 The value of `normal-auto-fill-function' specifies the function to use
2940 for `auto-fill-function' when turning Auto Fill mode on."
2942 (prog1 (setq auto-fill-function
2944 (not auto-fill-function)
2945 (> (prefix-numeric-value arg) 0))
2946 normal-auto-fill-function
2948 (force-mode-line-update)))
2950 ;; This holds a document string used to document auto-fill-mode.
2951 (defun auto-fill-function ()
2952 "Automatically break line at a previous space, in insertion of text."
2955 (defun turn-on-auto-fill ()
2956 "Unconditionally turn on Auto Fill mode."
2959 (defun turn-off-auto-fill ()
2960 "Unconditionally turn off Auto Fill mode."
2961 (auto-fill-mode -1))
2963 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
2965 (defun set-fill-column (arg)
2966 "Set `fill-column' to specified argument.
2967 Use \\[universal-argument] followed by a number to specify a column.
2968 Just \\[universal-argument] as argument means to use the current column."
2971 (setq arg (current-column)))
2972 (if (not (integerp arg))
2973 ;; Disallow missing argument; it's probably a typo for C-x C-f.
2974 (error "set-fill-column requires an explicit argument")
2975 (message "Fill column set to %d (was %d)" arg fill-column)
2976 (setq fill-column arg)))
2978 (defun set-selective-display (arg)
2979 "Set `selective-display' to ARG; clear it if no arg.
2980 When the value of `selective-display' is a number > 0,
2981 lines whose indentation is >= that value are not displayed.
2982 The variable `selective-display' has a separate value for each buffer."
2984 (if (eq selective-display t)
2985 (error "selective-display already in use for marked lines"))
2988 (narrow-to-region (point-min) (point))
2989 (goto-char (window-start))
2990 (vertical-motion (window-height)))))
2991 (setq selective-display
2992 (and arg (prefix-numeric-value arg)))
2993 (recenter current-vpos))
2994 (set-window-start (selected-window) (window-start (selected-window)))
2995 (princ "selective-display set to " t)
2996 (prin1 selective-display t)
2999 (defvar overwrite-mode-textual " Ovwrt"
3000 "The string displayed in the mode line when in overwrite mode.")
3001 (defvar overwrite-mode-binary " Bin Ovwrt"
3002 "The string displayed in the mode line when in binary overwrite mode.")
3004 (defun overwrite-mode (arg)
3005 "Toggle overwrite mode.
3006 With arg, turn overwrite mode on iff arg is positive.
3007 In overwrite mode, printing characters typed in replace existing text
3008 on a one-for-one basis, rather than pushing it to the right. At the
3009 end of a line, such characters extend the line. Before a tab,
3010 such characters insert until the tab is filled in.
3011 \\[quoted-insert] still inserts characters in overwrite mode; this
3012 is supposed to make it easier to insert characters when necessary."
3014 (setq overwrite-mode
3015 (if (if (null arg) (not overwrite-mode)
3016 (> (prefix-numeric-value arg) 0))
3017 'overwrite-mode-textual))
3018 (force-mode-line-update))
3020 (defun binary-overwrite-mode (arg)
3021 "Toggle binary overwrite mode.
3022 With arg, turn binary overwrite mode on iff arg is positive.
3023 In binary overwrite mode, printing characters typed in replace
3024 existing text. Newlines are not treated specially, so typing at the
3025 end of a line joins the line to the next, with the typed character
3026 between them. Typing before a tab character simply replaces the tab
3027 with the character typed.
3028 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3029 typing characters do.
3031 Note that binary overwrite mode is not its own minor mode; it is a
3032 specialization of overwrite-mode, entered by setting the
3033 `overwrite-mode' variable to `overwrite-mode-binary'."
3035 (setq overwrite-mode
3037 (not (eq overwrite-mode 'overwrite-mode-binary))
3038 (> (prefix-numeric-value arg) 0))
3039 'overwrite-mode-binary))
3040 (force-mode-line-update))
3042 (defcustom line-number-mode t
3043 "*Non-nil means display line number in mode line."
3045 :group 'editing-basics)
3047 (defun line-number-mode (arg)
3048 "Toggle Line Number mode.
3049 With arg, turn Line Number mode on iff arg is positive.
3050 When Line Number mode is enabled, the line number appears
3053 Line numbers do not appear for very large buffers, see variable
3054 `line-number-display-limit'."
3056 (setq line-number-mode
3057 (if (null arg) (not line-number-mode)
3058 (> (prefix-numeric-value arg) 0)))
3059 (force-mode-line-update))
3061 (defcustom column-number-mode nil
3062 "*Non-nil means display column number in mode line."
3064 :group 'editing-basics)
3066 (defun column-number-mode (arg)
3067 "Toggle Column Number mode.
3068 With arg, turn Column Number mode on iff arg is positive.
3069 When Column Number mode is enabled, the column number appears
3072 (setq column-number-mode
3073 (if (null arg) (not column-number-mode)
3074 (> (prefix-numeric-value arg) 0)))
3075 (force-mode-line-update))
3077 (defgroup paren-blinking nil
3078 "Blinking matching of parens and expressions."
3079 :prefix "blink-matching-"
3080 :group 'paren-matching)
3082 (defcustom blink-matching-paren t
3083 "*Non-nil means show matching open-paren when close-paren is inserted."
3085 :group 'paren-blinking)
3087 (defcustom blink-matching-paren-on-screen t
3088 "*Non-nil means show matching open-paren when it is on screen.
3089 If nil, means don't show it (but the open-paren can still be shown
3090 when it is off screen)."
3092 :group 'paren-blinking)
3094 (defcustom blink-matching-paren-distance (* 25 1024)
3095 "*If non-nil, is maximum distance to search for matching open-paren."
3097 :group 'paren-blinking)
3099 (defcustom blink-matching-delay 1
3100 "*Time in seconds to delay after showing a matching paren."
3102 :group 'paren-blinking)
3104 (defcustom blink-matching-paren-dont-ignore-comments nil
3105 "*Non-nil means `blink-matching-paren' will not ignore comments."
3107 :group 'paren-blinking)
3109 (defun blink-matching-open ()
3110 "Move cursor momentarily to the beginning of the sexp before point."
3112 (and (> (point) (1+ (point-min)))
3113 blink-matching-paren
3114 ;; Verify an even number of quoting characters precede the close.
3115 (= 1 (logand 1 (- (point)
3118 (skip-syntax-backward "/\\")
3120 (let* ((oldpos (point))
3125 (if blink-matching-paren-distance
3126 (narrow-to-region (max (point-min)
3127 (- (point) blink-matching-paren-distance))
3130 (let ((parse-sexp-ignore-comments
3131 (and parse-sexp-ignore-comments
3132 (not blink-matching-paren-dont-ignore-comments))))
3133 (setq blinkpos (scan-sexps oldpos -1)))
3136 (/= (char-syntax (char-after blinkpos))
3139 (or (null (matching-paren (char-after blinkpos)))
3140 (/= (char-after (1- oldpos))
3141 (matching-paren (char-after blinkpos))))))
3142 (if mismatch (setq blinkpos nil))
3144 ;; Don't log messages about paren matching.
3145 (let (message-log-max)
3146 (goto-char blinkpos)
3147 (if (pos-visible-in-window-p)
3148 (and blink-matching-paren-on-screen
3149 (sit-for blink-matching-delay))
3150 (goto-char blinkpos)
3153 ;; Show what precedes the open in its line, if anything.
3155 (skip-chars-backward " \t")
3157 (buffer-substring (progn (beginning-of-line) (point))
3159 ;; Show what follows the open in its line, if anything.
3162 (skip-chars-forward " \t")
3164 (buffer-substring blinkpos
3165 (progn (end-of-line) (point)))
3166 ;; Otherwise show the previous nonblank line,
3169 (skip-chars-backward "\n \t")
3172 (buffer-substring (progn
3173 (skip-chars-backward "\n \t")
3176 (progn (end-of-line)
3177 (skip-chars-backward " \t")
3179 ;; Replace the newline and other whitespace with `...'.
3181 (buffer-substring blinkpos (1+ blinkpos)))
3182 ;; There is nothing to show except the char itself.
3183 (buffer-substring blinkpos (1+ blinkpos))))))))
3185 (message "Mismatched parentheses"))
3186 ((not blink-matching-paren-distance)
3187 (message "Unmatched parenthesis"))))))))
3189 ;Turned off because it makes dbx bomb out.
3190 (setq blink-paren-function 'blink-matching-open)
3192 ;; This executes C-g typed while Emacs is waiting for a command.
3193 ;; Quitting out of a program does not go through here;
3194 ;; that happens in the QUIT macro at the C code level.
3195 (defun keyboard-quit ()
3196 "Signal a `quit' condition.
3197 During execution of Lisp code, this character causes a quit directly.
3198 At top-level, as an editor command, this simply beeps."
3203 (define-key global-map "\C-g" 'keyboard-quit)
3205 (defvar buffer-quit-function nil
3206 "Function to call to \"quit\" the current buffer, or nil if none.
3207 \\[keyboard-escape-quit] calls this function when its more local actions
3208 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3210 (defun keyboard-escape-quit ()
3211 "Exit the current \"mode\" (in a generalized sense of the word).
3212 This command can exit an interactive command such as `query-replace',
3213 can clear out a prefix argument or a region,
3214 can get out of the minibuffer or other recursive edit,
3215 cancel the use of the current buffer (for special-purpose buffers),
3216 or go back to just one window (by deleting all but the selected window)."
3218 (cond ((eq last-command 'mode-exited) nil)
3219 ((> (minibuffer-depth) 0)
3220 (abort-recursive-edit))
3223 ((and transient-mark-mode
3226 ((> (recursion-depth) 0)
3227 (exit-recursive-edit))
3228 (buffer-quit-function
3229 (funcall buffer-quit-function))
3230 ((not (one-window-p t))
3231 (delete-other-windows))
3232 ((string-match "^ \\*" (buffer-name (current-buffer)))
3235 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3237 (defcustom input-mode-8-bit t
3238 "Control acceptance of 8-bit keyboard input.
3239 This may be useful for inputting non-ASCII characters if your keyboard
3240 can generate them. It is not necessary to change this under a window
3241 system which can distinguish 8-bit characters and Meta keys.
3242 Setting this variable directly does not take effect;
3243 use either M-x customize or the function `set-input-mode'."
3244 :set (lambda (symbol value)
3245 (let ((mode (current-input-mode)))
3246 (set-input-mode (nth 0 mode) (nth 1 mode) value)))
3247 :initialize 'custom-initialize-default
3248 :type '(choice (const :tag "8-bit input for a Meta key" t)
3249 (const :tag "Direct 8-bit character input" 0)
3250 (const :tag "Assume top bit is parity and ignore" nil))
3252 :link '(custom-manual "Single-Byte European Support")
3255 (defcustom read-mail-command 'rmail
3256 "*Your preference for a mail reading package.
3257 This is used by some keybindings which support reading mail.
3258 See also `mail-user-agent' concerning sending mail."
3259 :type '(choice (function-item rmail)
3260 (function-item gnus)
3261 (function-item mh-rmail)
3262 (function :tag "Other"))
3266 (defcustom mail-user-agent 'sendmail-user-agent
3267 "*Your preference for a mail composition package.
3268 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3269 outgoing email message. This variable lets you specify which
3270 mail-sending package you prefer.
3272 Valid values include:
3274 `sendmail-user-agent' -- use the default Emacs Mail package.
3275 See Info node `(emacs)Sending Mail'.
3276 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3277 See Info node `(mh-e)'.
3278 `message-user-agent' -- use the Gnus Message package.
3279 See Info node `(message)'.
3280 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3281 paraphernalia, particularly the Gcc: header for
3284 Additional valid symbols may be available; check with the author of
3285 your package for details.
3287 See also `read-mail-command' concerning reading mail."
3288 :type '(radio (function-item :tag "Default Emacs mail"
3290 sendmail-user-agent)
3291 (function-item :tag "Emacs interface to MH"
3294 (function-item :tag "Gnus Message package"
3297 (function-item :tag "Gnus Message with full Gnus features"
3300 (function :tag "Other"))
3303 (defun define-mail-user-agent (symbol composefunc sendfunc
3304 &optional abortfunc hookvar)
3305 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3307 SYMBOL can be any Lisp symbol. Its function definition and/or
3308 value as a variable do not matter for this usage; we use only certain
3309 properties on its property list, to encode the rest of the arguments.
3311 COMPOSEFUNC is program callable function that composes an outgoing
3312 mail message buffer. This function should set up the basics of the
3313 buffer without requiring user interaction. It should populate the
3314 standard mail headers, leaving the `to:' and `subject:' headers blank
3317 COMPOSEFUNC should accept several optional arguments--the same
3318 arguments that `compose-mail' takes. See that function's documentation.
3320 SENDFUNC is the command a user would run to send the message.
3322 Optional ABORTFUNC is the command a user would run to abort the
3323 message. For mail packages that don't have a separate abort function,
3324 this can be `kill-buffer' (the equivalent of omitting this argument).
3326 Optional HOOKVAR is a hook variable that gets run before the message
3327 is actually sent. Callers that use the `mail-user-agent' may
3328 install a hook function temporarily on this hook variable.
3329 If HOOKVAR is nil, `mail-send-hook' is used.
3331 The properties used on SYMBOL are `composefunc', `sendfunc',
3332 `abortfunc', and `hookvar'."
3333 (put symbol 'composefunc composefunc)
3334 (put symbol 'sendfunc sendfunc)
3335 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3336 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3338 (define-mail-user-agent 'sendmail-user-agent
3339 'sendmail-user-agent-compose
3340 'mail-send-and-exit)
3342 (defun rfc822-goto-eoh ()
3343 ;; Go to header delimiter line in a mail message, following RFC822 rules
3344 (goto-char (point-min))
3345 (while (looking-at "^[^: \n]+:\\|^[ \t]")
3349 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3350 switch-function yank-action
3353 (let ((special-display-buffer-names nil)
3354 (special-display-regexps nil)
3355 (same-window-buffer-names nil)
3356 (same-window-regexps nil))
3357 (funcall switch-function "*mail*")))
3358 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3359 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers)))
3360 (body (cdr (assoc-ignore-case "body" other-headers))))
3361 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3363 (error "Message aborted"))
3366 (while other-headers
3367 (unless (member-ignore-case (car (car other-headers))
3368 '("in-reply-to" "cc" "body"))
3369 (insert (car (car other-headers)) ": "
3370 (cdr (car other-headers)) "\n"))
3371 (setq other-headers (cdr other-headers)))
3377 (define-mail-user-agent 'mh-e-user-agent
3378 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3379 'mh-before-send-letter-hook)
3381 (defun compose-mail (&optional to subject other-headers continue
3382 switch-function yank-action send-actions)
3383 "Start composing a mail message to send.
3384 This uses the user's chosen mail composition package
3385 as selected with the variable `mail-user-agent'.
3386 The optional arguments TO and SUBJECT specify recipients
3387 and the initial Subject field, respectively.
3389 OTHER-HEADERS is an alist specifying additional
3390 header fields. Elements look like (HEADER . VALUE) where both
3391 HEADER and VALUE are strings.
3393 CONTINUE, if non-nil, says to continue editing a message already
3396 SWITCH-FUNCTION, if non-nil, is a function to use to
3397 switch to and display the buffer used for mail composition.
3399 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3400 to insert the raw text of the message being replied to.
3401 It has the form (FUNCTION . ARGS). The user agent will apply
3402 FUNCTION to ARGS, to insert the raw text of the original message.
3403 \(The user agent will also run `mail-citation-hook', *after* the
3404 original text has been inserted in this way.)
3406 SEND-ACTIONS is a list of actions to call when the message is sent.
3407 Each action has the form (FUNCTION . ARGS)."
3409 (list nil nil nil current-prefix-arg))
3410 (let ((function (get mail-user-agent 'composefunc)))
3411 (funcall function to subject other-headers continue
3412 switch-function yank-action send-actions)))
3414 (defun compose-mail-other-window (&optional to subject other-headers continue
3415 yank-action send-actions)
3416 "Like \\[compose-mail], but edit the outgoing message in another window."
3418 (list nil nil nil current-prefix-arg))
3419 (compose-mail to subject other-headers continue
3420 'switch-to-buffer-other-window yank-action send-actions))
3423 (defun compose-mail-other-frame (&optional to subject other-headers continue
3424 yank-action send-actions)
3425 "Like \\[compose-mail], but edit the outgoing message in another frame."
3427 (list nil nil nil current-prefix-arg))
3428 (compose-mail to subject other-headers continue
3429 'switch-to-buffer-other-frame yank-action send-actions))
3431 (defvar set-variable-value-history nil
3432 "History of values entered with `set-variable'.")
3434 (defun set-variable (var val)
3435 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3436 When using this interactively, enter a Lisp object for VALUE.
3437 If you want VALUE to be a string, you must surround it with doublequotes.
3438 VALUE is used literally, not evaluated.
3440 If VARIABLE has a `variable-interactive' property, that is used as if
3441 it were the arg to `interactive' (which see) to interactively read VALUE.
3443 If VARIABLE has been defined with `defcustom', then the type information
3444 in the definition is used to check that VALUE is valid."
3446 (let* ((default-var (variable-at-point))
3447 (var (if (symbolp default-var)
3448 (read-variable (format "Set variable (default %s): " default-var)
3450 (read-variable "Set variable: ")))
3451 (minibuffer-help-form '(describe-variable var))
3452 (prop (get var 'variable-interactive))
3453 (prompt (format "Set %s to value: " var))
3455 ;; Use VAR's `variable-interactive' property
3456 ;; as an interactive spec for prompting.
3457 (call-interactively `(lambda (arg)
3461 (read-string prompt nil
3462 'set-variable-value-history)))))
3465 (let ((type (get var 'custom-type)))
3467 ;; Match with custom type.
3469 (setq type (widget-convert type))
3470 (unless (widget-apply type :match val)
3471 (error "Value `%S' does not match type %S of %S"
3472 val (car type) var))))
3475 ;; Define the major mode for lists of completions.
3477 (defvar completion-list-mode-map nil
3478 "Local map for completion list buffers.")
3479 (or completion-list-mode-map
3480 (let ((map (make-sparse-keymap)))
3481 (define-key map [mouse-2] 'mouse-choose-completion)
3482 (define-key map [down-mouse-2] nil)
3483 (define-key map "\C-m" 'choose-completion)
3484 (define-key map "\e\e\e" 'delete-completion-window)
3485 (define-key map [left] 'previous-completion)
3486 (define-key map [right] 'next-completion)
3487 (setq completion-list-mode-map map)))
3489 ;; Completion mode is suitable only for specially formatted data.
3490 (put 'completion-list-mode 'mode-class 'special)
3492 (defvar completion-reference-buffer nil
3493 "Record the buffer that was current when the completion list was requested.
3494 This is a local variable in the completion list buffer.
3495 Initial value is nil to avoid some compiler warnings.")
3497 (defvar completion-no-auto-exit nil
3498 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3499 This also applies to other functions such as `choose-completion'
3500 and `mouse-choose-completion'.")
3502 (defvar completion-base-size nil
3503 "Number of chars at beginning of minibuffer not involved in completion.
3504 This is a local variable in the completion list buffer
3505 but it talks about the buffer in `completion-reference-buffer'.
3506 If this is nil, it means to compare text to determine which part
3507 of the tail end of the buffer's text is involved in completion.")
3509 (defun delete-completion-window ()
3510 "Delete the completion list window.
3511 Go to the window from which completion was requested."
3513 (let ((buf completion-reference-buffer))
3514 (if (one-window-p t)
3515 (if (window-dedicated-p (selected-window))
3516 (delete-frame (selected-frame)))
3517 (delete-window (selected-window))
3518 (if (get-buffer-window buf)
3519 (select-window (get-buffer-window buf))))))
3521 (defun previous-completion (n)
3522 "Move to the previous item in the completion list."
3524 (next-completion (- n)))
3526 (defun next-completion (n)
3527 "Move to the next item in the completion list.
3528 With prefix argument N, move N items (negative N means move backward)."
3530 (while (and (> n 0) (not (eobp)))
3531 (let ((prop (get-text-property (point) 'mouse-face))
3533 ;; If in a completion, move to the end of it.
3535 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3536 ;; Move to start of next one.
3537 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3539 (while (and (< n 0) (not (bobp)))
3540 (let ((prop (get-text-property (1- (point)) 'mouse-face))
3542 ;; If in a completion, move to the start of it.
3544 (goto-char (previous-single-property-change
3545 (point) 'mouse-face nil end)))
3546 ;; Move to end of the previous completion.
3547 (goto-char (previous-single-property-change (point) 'mouse-face nil end))
3548 ;; Move to the start of that one.
3549 (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
3552 (defun choose-completion ()
3553 "Choose the completion that point is in or next to."
3555 (let (beg end completion (buffer completion-reference-buffer)
3556 (base-size completion-base-size))
3557 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3558 (setq end (point) beg (1+ (point))))
3559 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3560 (setq end (1- (point)) beg (point)))
3562 (error "No completion here"))
3563 (setq beg (previous-single-property-change beg 'mouse-face))
3564 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3565 (setq completion (buffer-substring beg end))
3566 (let ((owindow (selected-window)))
3567 (if (and (one-window-p t 'selected-frame)
3568 (window-dedicated-p (selected-window)))
3569 ;; This is a special buffer's frame
3570 (iconify-frame (selected-frame))
3571 (or (window-dedicated-p (selected-window))
3573 (select-window owindow))
3574 (choose-completion-string completion buffer base-size)))
3576 ;; Delete the longest partial match for STRING
3577 ;; that can be found before POINT.
3578 (defun choose-completion-delete-max-match (string)
3579 (let ((opoint (point))
3580 (len (min (length string)
3581 (- (point) (point-min)))))
3582 (goto-char (- (point) (length string)))
3583 (if completion-ignore-case
3584 (setq string (downcase string)))
3585 (while (and (> len 0)
3586 (let ((tail (buffer-substring (point)
3588 (if completion-ignore-case
3589 (setq tail (downcase tail)))
3590 (not (string= tail (substring string 0 len)))))
3595 ;; Switch to BUFFER and insert the completion choice CHOICE.
3596 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3597 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3599 ;; If BUFFER is the minibuffer, exit the minibuffer
3600 ;; unless it is reading a file name and CHOICE is a directory,
3601 ;; or completion-no-auto-exit is non-nil.
3602 (defun choose-completion-string (choice &optional buffer base-size)
3603 (let ((buffer (or buffer completion-reference-buffer))
3604 (mini-p (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))))
3605 ;; If BUFFER is a minibuffer, barf unless it's the currently
3606 ;; active minibuffer.
3608 (or (not (active-minibuffer-window))
3610 (window-buffer (active-minibuffer-window))))))
3611 (error "Minibuffer is not active for completion")
3612 ;; Insert the completion into the buffer where completion was requested.
3615 (delete-region (+ base-size (if mini-p
3616 (minibuffer-prompt-end)
3619 (choose-completion-delete-max-match choice))
3621 (remove-text-properties (- (point) (length choice)) (point)
3623 ;; Update point in the window that BUFFER is showing in.
3624 (let ((window (get-buffer-window buffer t)))
3625 (set-window-point window (point)))
3626 ;; If completing for the minibuffer, exit it with this choice.
3627 (and (not completion-no-auto-exit)
3628 (equal buffer (window-buffer (minibuffer-window)))
3629 minibuffer-completion-table
3630 ;; If this is reading a file name, and the file name chosen
3631 ;; is a directory, don't exit the minibuffer.
3632 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3633 (file-directory-p (field-string (point-max))))
3634 (select-window (active-minibuffer-window))
3635 (exit-minibuffer))))))
3637 (defun completion-list-mode ()
3638 "Major mode for buffers showing lists of possible completions.
3639 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3640 to select the completion near point.
3641 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3644 (kill-all-local-variables)
3645 (use-local-map completion-list-mode-map)
3646 (setq mode-name "Completion List")
3647 (setq major-mode 'completion-list-mode)
3648 (make-local-variable 'completion-base-size)
3649 (setq completion-base-size nil)
3650 (run-hooks 'completion-list-mode-hook))
3652 (defvar completion-setup-hook nil
3653 "Normal hook run at the end of setting up a completion list buffer.
3654 When this hook is run, the current buffer is the one in which the
3655 command to display the completion list buffer was run.
3656 The completion list buffer is available as the value of `standard-output'.")
3658 ;; This function goes in completion-setup-hook, so that it is called
3659 ;; after the text of the completion list buffer is written.
3661 (defun completion-setup-function ()
3663 (let ((mainbuf (current-buffer)))
3664 (set-buffer standard-output)
3665 (completion-list-mode)
3666 (make-local-variable 'completion-reference-buffer)
3667 (setq completion-reference-buffer mainbuf)
3668 (if (eq minibuffer-completion-table 'read-file-name-internal)
3669 ;; For file name completion,
3670 ;; use the number of chars before the start of the
3671 ;; last file name component.
3672 (setq completion-base-size
3674 (set-buffer mainbuf)
3675 (goto-char (point-max))
3676 (skip-chars-backward (format "^%c" directory-sep-char))
3677 (- (point) (minibuffer-prompt-end))))
3678 ;; Otherwise, in minibuffer, the whole input is being completed.
3680 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3681 (buffer-name mainbuf))
3682 (setq completion-base-size 0))))
3683 (goto-char (point-min))
3684 (if (display-mouse-p)
3685 (insert (substitute-command-keys
3686 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3687 (insert (substitute-command-keys
3688 "In this buffer, type \\[choose-completion] to \
3689 select the completion near point.\n\n")))))
3691 (add-hook 'completion-setup-hook 'completion-setup-function)
3693 (define-key minibuffer-local-completion-map [prior]
3694 'switch-to-completions)
3695 (define-key minibuffer-local-must-match-map [prior]
3696 'switch-to-completions)
3697 (define-key minibuffer-local-completion-map "\M-v"
3698 'switch-to-completions)
3699 (define-key minibuffer-local-must-match-map "\M-v"
3700 'switch-to-completions)
3702 (defun switch-to-completions ()
3703 "Select the completion list window."
3705 ;; Make sure we have a completions window.
3706 (or (get-buffer-window "*Completions*")
3707 (minibuffer-completion-help))
3708 (let ((window (get-buffer-window "*Completions*")))
3710 (select-window window)
3711 (goto-char (point-min))
3712 (search-forward "\n\n")
3715 ;; Support keyboard commands to turn on various modifiers.
3717 ;; These functions -- which are not commands -- each add one modifier
3718 ;; to the following event.
3720 (defun event-apply-alt-modifier (ignore-prompt)
3721 "Add the Alt modifier to the following event.
3722 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
3723 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3724 (defun event-apply-super-modifier (ignore-prompt)
3725 "Add the Super modifier to the following event.
3726 For example, type \\[event-apply-super-modifier] & to enter Super-&."
3727 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3728 (defun event-apply-hyper-modifier (ignore-prompt)
3729 "Add the Hyper modifier to the following event.
3730 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
3731 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3732 (defun event-apply-shift-modifier (ignore-prompt)
3733 "Add the Shift modifier to the following event.
3734 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
3735 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3736 (defun event-apply-control-modifier (ignore-prompt)
3737 "Add the Ctrl modifier to the following event.
3738 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
3739 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3740 (defun event-apply-meta-modifier (ignore-prompt)
3741 "Add the Meta modifier to the following event.
3742 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
3743 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3745 (defun event-apply-modifier (event symbol lshiftby prefix)
3746 "Apply a modifier flag to event EVENT.
3747 SYMBOL is the name of this modifier, as a symbol.
3748 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3749 PREFIX is the string that represents this modifier in an event type symbol."
3751 (cond ((eq symbol 'control)
3752 (if (and (<= (downcase event) ?z)
3753 (>= (downcase event) ?a))
3754 (- (downcase event) ?a -1)
3755 (if (and (<= (downcase event) ?Z)
3756 (>= (downcase event) ?A))
3757 (- (downcase event) ?A -1)
3758 (logior (lsh 1 lshiftby) event))))
3760 (if (and (<= (downcase event) ?z)
3761 (>= (downcase event) ?a))
3763 (logior (lsh 1 lshiftby) event)))
3765 (logior (lsh 1 lshiftby) event)))
3766 (if (memq symbol (event-modifiers event))
3768 (let ((event-type (if (symbolp event) event (car event))))
3769 (setq event-type (intern (concat prefix (symbol-name event-type))))
3772 (cons event-type (cdr event)))))))
3774 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
3775 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
3776 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
3777 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
3778 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
3779 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
3781 ;;;; Keypad support.
3783 ;;; Make the keypad keys act like ordinary typing keys. If people add
3784 ;;; bindings for the function key symbols, then those bindings will
3785 ;;; override these, so this shouldn't interfere with any existing
3788 ;; Also tell read-char how to handle these keys.
3790 (lambda (keypad-normal)
3791 (let ((keypad (nth 0 keypad-normal))
3792 (normal (nth 1 keypad-normal)))
3793 (put keypad 'ascii-character normal)
3794 (define-key function-key-map (vector keypad) (vector normal))))
3795 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
3796 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
3809 ;;;; forking a twin copy of a buffer.
3812 (defvar clone-buffer-hook nil
3813 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
3815 (defun clone-process (process &optional newname)
3816 "Create a twin copy of PROCESS.
3817 If NEWNAME is nil, it defaults to PROCESS' name;
3818 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
3819 If PROCESS is associated with a buffer, the new process will be associated
3820 with the current buffer instead.
3821 Returns nil if PROCESS has already terminated."
3822 (setq newname (or newname (process-name process)))
3823 (if (string-match "<[0-9]+>\\'" newname)
3824 (setq newname (substring newname 0 (match-beginning 0))))
3825 (when (memq (process-status process) '(run stop open))
3826 (let* ((process-connection-type (process-tty-name process))
3827 (old-kwoq (process-kill-without-query process nil))
3829 (if (memq (process-status process) '(open))
3830 (apply 'open-network-stream newname
3831 (if (process-buffer process) (current-buffer))
3832 (process-contact process))
3833 (apply 'start-process newname
3834 (if (process-buffer process) (current-buffer))
3835 (process-command process)))))
3836 (process-kill-without-query new-process old-kwoq)
3837 (process-kill-without-query process old-kwoq)
3838 (set-process-inherit-coding-system-flag
3839 new-process (process-inherit-coding-system-flag process))
3840 (set-process-filter new-process (process-filter process))
3841 (set-process-sentinel new-process (process-sentinel process))
3844 ;; things to maybe add (currently partly covered by `funcall mode':
3847 (defun clone-buffer (&optional newname display-flag)
3848 "Create a twin copy of the current buffer.
3849 If NEWNAME is nil, it defaults to the current buffer's name;
3850 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
3852 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
3853 This runs the normal hook `clone-buffer-hook' in the new buffer
3854 after it has been set up properly in other respects."
3855 (interactive (list (if current-prefix-arg (read-string "Name: "))
3857 (if buffer-file-name
3858 (error "Cannot clone a file-visiting buffer"))
3859 (if (get major-mode 'no-clone)
3860 (error "Cannot clone a buffer in %s mode" mode-name))
3861 (setq newname (or newname (buffer-name)))
3862 (if (string-match "<[0-9]+>\\'" newname)
3863 (setq newname (substring newname 0 (match-beginning 0))))
3864 (let ((buf (current-buffer))
3868 (mk (if mark-active (mark t)))
3869 (modified (buffer-modified-p))
3871 (lvars (buffer-local-variables))
3872 (process (get-buffer-process (current-buffer)))
3873 (new (generate-new-buffer (or newname (buffer-name)))))
3876 (with-current-buffer new
3877 (insert-buffer-substring buf)))
3878 (with-current-buffer new
3879 (narrow-to-region ptmin ptmax)
3881 (if mk (set-mark mk))
3882 (set-buffer-modified-p modified)
3884 ;; Clone the old buffer's process, if any.
3885 (when process (clone-process process))
3887 ;; Now set up the major mode.
3890 ;; Set up other local variables.
3892 (condition-case () ;in case var is read-only
3895 (set (make-local-variable (car v)) (cdr v)))
3899 ;; Run any hooks (typically set up by the major mode
3900 ;; for cloning to work properly).
3901 (run-hooks 'clone-buffer-hook))
3902 (if display-flag (pop-to-buffer new))
3906 (defun clone-indirect-buffer (newname display-flag &optional norecord)
3907 "Create an indirect buffer that is a twin copy of the current buffer.
3909 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
3910 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
3911 or if not called with a prefix arg, NEWNAME defaults to the current
3912 buffer's name. The name is modified by adding a `<N>' suffix to it
3913 or by incrementing the N in an existing suffix.
3915 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
3916 This is always done when called interactively.
3918 Optional last arg NORECORD non-nil means do not put this buffer at the
3919 front of the list of recently selected ones."
3920 (interactive (list (if current-prefix-arg
3921 (read-string "BName of indirect buffer: "))
3923 (setq newname (or newname (buffer-name)))
3924 (if (string-match "<[0-9]+>\\'" newname)
3925 (setq newname (substring newname 0 (match-beginning 0))))
3926 (let* ((name (generate-new-buffer-name newname))
3927 (buffer (make-indirect-buffer (current-buffer) name t)))
3929 (pop-to-buffer buffer))
3933 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
3934 "Create an indirect buffer that is a twin copy of BUFFER.
3935 Select the new buffer in another window.
3936 Optional second arg NORECORD non-nil means do not put this buffer at
3937 the front of the list of recently selected ones."
3938 (interactive "bClone buffer in other window: ")
3939 (let ((popup-windows t))
3941 (clone-indirect-buffer nil t norecord)))
3943 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
3948 (defconst syntax-code-table
3949 '((?\ 0 "whitespace")
3951 (?. 1 "punctuation")
3954 (?\( 4 "open parenthesis")
3955 (?\) 5 "close parenthesis")
3956 (?\' 6 "expression prefix")
3957 (?\" 7 "string quote")
3958 (?$ 8 "paired delimiter")
3960 (?/ 10 "character quote")
3961 (?< 11 "comment start")
3962 (?> 12 "comment end")
3964 (nil 14 "comment fence")
3965 (nil 15 "string fence"))
3966 "Alist of forms (CHAR CODE DESCRIPTION) mapping characters to syntax info.
3967 CHAR is a character that is allowed as first char in the string
3968 specifying the syntax when calling `modify-syntax-entry'. CODE is the
3969 corresponing syntax code as it is stored in a syntax cell, and
3970 can be used as value of a `syntax-table' property.
3971 DESCRIPTION is the descriptive string for the syntax.")
3976 (defun byte-compiling-files-p ()
3977 "Return t if currently byte-compiling files."
3978 (and (boundp 'byte-compile-current-file)
3979 (stringp byte-compile-current-file)))
3981 ;;; simple.el ends here