1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99,
4 ;; 2000, 2001, 2002, 2003
5 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; A grab-bag of basic Emacs commands not specifically related to some
30 ;; major mode or to file-handling.
35 (autoload 'widget-convert
"wid-edit")
36 (autoload 'shell-mode
"shell"))
40 "Killing and yanking commands"
43 (defgroup paren-matching nil
44 "Highlight (un)matching of parens and expressions."
47 (define-key global-map
[?\C-x right
] 'next-buffer
)
48 (define-key global-map
[?\C-x left
] 'prev-buffer
)
50 "Switch to the next buffer in cyclic order."
52 (let ((buffer (current-buffer)))
53 (switch-to-buffer (other-buffer buffer
))
54 (bury-buffer buffer
)))
57 "Switch to the previous buffer in cyclic order."
59 (let ((list (nreverse (buffer-list)))
61 (while (and (not found
) list
)
62 (let ((buffer (car list
)))
63 (if (and (not (get-buffer-window buffer
))
64 (not (string-match "\\` " (buffer-name buffer
))))
66 (setq list
(cdr list
)))
67 (switch-to-buffer found
)))
69 (defun fundamental-mode ()
70 "Major mode not specialized for anything in particular.
71 Other major modes are defined by comparison with this one."
73 (kill-all-local-variables))
75 ;; Making and deleting lines.
77 (defun newline (&optional arg
)
78 "Insert a newline, and move to left margin of the new line if it's blank.
79 If `use-hard-newlines' is non-nil, the newline is marked with the
81 With ARG, insert that many newlines.
82 Call `auto-fill-function' if the current column number is greater
83 than the value of `fill-column' and ARG is nil."
85 (barf-if-buffer-read-only)
86 ;; Inserting a newline at the end of a line produces better redisplay in
87 ;; try_window_id than inserting at the beginning of a line, and the textual
88 ;; result is the same. So, if we're at beginning of line, pretend to be at
89 ;; the end of the previous line.
90 (let ((flag (and (not (bobp))
92 ;; Make sure no functions want to be told about
93 ;; the range of the changes.
94 (not after-change-functions
)
95 (not before-change-functions
)
96 ;; Make sure there are no markers here.
97 (not (buffer-has-markers-at (1- (point))))
98 (not (buffer-has-markers-at (point)))
99 ;; Make sure no text properties want to know
100 ;; where the change was.
101 (not (get-char-property (1- (point)) 'modification-hooks
))
102 (not (get-char-property (1- (point)) 'insert-behind-hooks
))
104 (not (get-char-property (point) 'insert-in-front-hooks
)))
105 ;; Make sure the newline before point isn't intangible.
106 (not (get-char-property (1- (point)) 'intangible
))
107 ;; Make sure the newline before point isn't read-only.
108 (not (get-char-property (1- (point)) 'read-only
))
109 ;; Make sure the newline before point isn't invisible.
110 (not (get-char-property (1- (point)) 'invisible
))
111 ;; Make sure the newline before point has the same
112 ;; properties as the char before it (if any).
113 (< (or (previous-property-change (point)) -
2)
115 (was-page-start (and (bolp)
116 (looking-at page-delimiter
)))
118 (if flag
(backward-char 1))
119 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
120 ;; Set last-command-char to tell self-insert what to insert.
121 (let ((last-command-char ?
\n)
122 ;; Don't auto-fill if we have a numeric argument.
123 ;; Also not if flag is true (it would fill wrong line);
124 ;; there is no need to since we're at BOL.
125 (auto-fill-function (if (or arg flag
) nil auto-fill-function
)))
127 (self-insert-command (prefix-numeric-value arg
))
128 ;; If we get an error in self-insert-command, put point at right place.
129 (if flag
(forward-char 1))))
130 ;; Even if we did *not* get an error, keep that forward-char;
131 ;; all further processing should apply to the newline that the user
132 ;; thinks he inserted.
134 ;; Mark the newline(s) `hard'.
135 (if use-hard-newlines
136 (set-hard-newline-properties
137 (- (point) (if arg
(prefix-numeric-value arg
) 1)) (point)))
138 ;; If the newline leaves the previous line blank,
139 ;; and we have a left margin, delete that from the blank line.
142 (goto-char beforepos
)
144 (and (looking-at "[ \t]$")
145 (> (current-left-margin) 0)
146 (delete-region (point) (progn (end-of-line) (point))))))
147 ;; Indent the line after the newline, except in one case:
148 ;; when we added the newline at the beginning of a line
149 ;; which starts a page.
151 (move-to-left-margin nil t
)))
154 (defun set-hard-newline-properties (from to
)
155 (let ((sticky (get-text-property from
'rear-nonsticky
)))
156 (put-text-property from to
'hard
't
)
157 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
158 (if (and (listp sticky
) (not (memq 'hard sticky
)))
159 (put-text-property from
(point) 'rear-nonsticky
160 (cons 'hard sticky
)))))
162 (defun open-line (arg)
163 "Insert a newline and leave point before it.
164 If there is a fill prefix and/or a left-margin, insert them on the new line
165 if the line would have been blank.
166 With arg N, insert N newlines."
168 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
169 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
171 ;; Don't expand an abbrev before point.
177 (if do-left-margin
(indent-to (current-left-margin)))
178 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
184 (defun split-line (&optional arg
)
185 "Split current line, moving portion beyond point vertically down.
186 If the current line starts with `fill-prefix', insert it on the new
187 line as well. With prefix arg, don't insert fill-prefix on new line.
189 When called from Lisp code, the arg may be a prefix string to copy."
191 (skip-chars-forward " \t")
192 (let* ((col (current-column))
194 ;; What prefix should we check for (nil means don't).
195 (prefix (cond ((stringp arg
) arg
)
198 ;; Does this line start with it?
199 (have-prfx (and prefix
202 (looking-at (regexp-quote prefix
))))))
204 (if have-prfx
(insert-and-inherit prefix
))
208 (defun delete-indentation (&optional arg
)
209 "Join this line to previous and fix up whitespace at join.
210 If there is a fill prefix, delete it from the beginning of this line.
211 With argument, join this line to following line."
214 (if arg
(forward-line 1))
215 (if (eq (preceding-char) ?
\n)
217 (delete-region (point) (1- (point)))
218 ;; If the second line started with the fill prefix,
219 ;; delete the prefix.
221 (<= (+ (point) (length fill-prefix
)) (point-max))
223 (buffer-substring (point)
224 (+ (point) (length fill-prefix
)))))
225 (delete-region (point) (+ (point) (length fill-prefix
))))
226 (fixup-whitespace))))
228 (defalias 'join-line
#'delete-indentation
) ; easier to find
230 (defun delete-blank-lines ()
231 "On blank line, delete all surrounding blank lines, leaving just one.
232 On isolated blank line, delete that one.
233 On nonblank line, delete any immediately following blank lines."
235 (let (thisblank singleblank
)
238 (setq thisblank
(looking-at "[ \t]*$"))
239 ;; Set singleblank if there is just one blank line here.
242 (not (looking-at "[ \t]*\n[ \t]*$"))
244 (progn (forward-line -
1)
245 (not (looking-at "[ \t]*$")))))))
246 ;; Delete preceding blank lines, and this one too if it's the only one.
250 (if singleblank
(forward-line 1))
251 (delete-region (point)
252 (if (re-search-backward "[^ \t\n]" nil t
)
253 (progn (forward-line 1) (point))
255 ;; Delete following blank lines, unless the current line is blank
256 ;; and there are no following blank lines.
257 (if (not (and thisblank singleblank
))
261 (delete-region (point)
262 (if (re-search-forward "[^ \t\n]" nil t
)
263 (progn (beginning-of-line) (point))
265 ;; Handle the special case where point is followed by newline and eob.
266 ;; Delete the line, leaving point at eob.
267 (if (looking-at "^[ \t]*\n\\'")
268 (delete-region (point) (point-max)))))
270 (defun delete-trailing-whitespace ()
271 "Delete all the trailing whitespace across the current buffer.
272 All whitespace after the last non-whitespace character in a line is deleted.
273 This respects narrowing, created by \\[narrow-to-region] and friends.
274 A formfeed is not considered whitespace by this function."
278 (goto-char (point-min))
279 (while (re-search-forward "\\s-$" nil t
)
280 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
281 ;; Don't delete formfeeds, even if they are considered whitespace.
283 (if (looking-at ".*\f")
284 (goto-char (match-end 0))))
285 (delete-region (point) (match-end 0))))))
287 (defun newline-and-indent ()
288 "Insert a newline, then indent according to major mode.
289 Indentation is done using the value of `indent-line-function'.
290 In programming language modes, this is the same as TAB.
291 In some text modes, where TAB inserts a tab, this command indents to the
292 column specified by the function `current-left-margin'."
294 (delete-horizontal-space t
)
296 (indent-according-to-mode))
298 (defun reindent-then-newline-and-indent ()
299 "Reindent current line, insert newline, then indent the new line.
300 Indentation of both lines is done according to the current major mode,
301 which means calling the current value of `indent-line-function'.
302 In programming language modes, this is the same as TAB.
303 In some text modes, where TAB inserts a tab, this indents to the
304 column specified by the function `current-left-margin'."
307 ;; Be careful to insert the newline before indenting the line.
308 ;; Otherwise, the indentation might be wrong.
312 (indent-according-to-mode)
313 (delete-horizontal-space t
))
314 (indent-according-to-mode)))
316 (defun quoted-insert (arg)
317 "Read next input character and insert it.
318 This is useful for inserting control characters.
320 If the first character you type after this command is an octal digit,
321 you should type a sequence of octal digits which specify a character code.
322 Any nondigit terminates the sequence. If the terminator is a RET,
323 it is discarded; any other terminator is used itself as input.
324 The variable `read-quoted-char-radix' specifies the radix for this feature;
325 set it to 10 or 16 to use decimal or hex instead of octal.
327 In overwrite mode, this function inserts the character anyway, and
328 does not handle octal digits specially. This means that if you use
329 overwrite as your normal editing mode, you can use this function to
330 insert characters when necessary.
332 In binary overwrite mode, this function does overwrite, and octal
333 digits are interpreted as a character code. This is intended to be
334 useful for editing binary files."
336 (let* ((char (let (translation-table-for-input)
337 (if (or (not overwrite-mode
)
338 (eq overwrite-mode
'overwrite-mode-binary
))
341 ;; Assume character codes 0240 - 0377 stand for characters in some
342 ;; single-byte character set, and convert them to Emacs
344 (if (and enable-multibyte-characters
347 (setq char
(unibyte-char-to-multibyte char
)))
349 (if (eq overwrite-mode
'overwrite-mode-binary
)
352 (insert-and-inherit char
)
353 (setq arg
(1- arg
)))))
355 (defun forward-to-indentation (&optional arg
)
356 "Move forward ARG lines and position at first nonblank character."
358 (forward-line (or arg
1))
359 (skip-chars-forward " \t"))
361 (defun backward-to-indentation (&optional arg
)
362 "Move backward ARG lines and position at first nonblank character."
364 (forward-line (- (or arg
1)))
365 (skip-chars-forward " \t"))
367 (defun back-to-indentation ()
368 "Move point to the first non-whitespace character on this line."
370 (beginning-of-line 1)
371 (skip-syntax-forward " " (line-end-position))
372 ;; Move back over chars that have whitespace syntax but have the p flag.
373 (backward-prefix-chars))
375 (defun fixup-whitespace ()
376 "Fixup white space between objects around point.
377 Leave one space or none, according to the context."
380 (delete-horizontal-space)
381 (if (or (looking-at "^\\|\\s)")
382 (save-excursion (forward-char -
1)
383 (looking-at "$\\|\\s(\\|\\s'")))
387 (defun delete-horizontal-space (&optional backward-only
)
388 "Delete all spaces and tabs around point.
389 If BACKWARD-ONLY is non-nil, only delete spaces before point."
391 (let ((orig-pos (point)))
396 (skip-chars-forward " \t")
397 (constrain-to-field nil orig-pos t
)))
399 (skip-chars-backward " \t")
400 (constrain-to-field nil orig-pos
)))))
402 (defun just-one-space ()
403 "Delete all spaces and tabs around point, leaving one space."
405 (let ((orig-pos (point)))
406 (skip-chars-backward " \t")
407 (constrain-to-field nil orig-pos
)
408 (if (= (following-char) ?
)
414 (skip-chars-forward " \t")
415 (constrain-to-field nil orig-pos t
)))))
417 (defun beginning-of-buffer (&optional arg
)
418 "Move point to the beginning of the buffer; leave mark at previous position.
419 With arg N, put point N/10 of the way from the beginning.
421 If the buffer is narrowed, this command uses the beginning and size
422 of the accessible part of the buffer.
424 Don't use this command in Lisp programs!
425 \(goto-char (point-min)) is faster and avoids clobbering the mark."
428 (let ((size (- (point-max) (point-min))))
432 ;; Avoid overflow for large buffer sizes!
433 (* (prefix-numeric-value arg
)
435 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
437 (if arg
(forward-line 1)))
439 (defun end-of-buffer (&optional arg
)
440 "Move point to the end of the buffer; leave mark at previous position.
441 With arg N, put point N/10 of the way from the end.
443 If the buffer is narrowed, this command uses the beginning and size
444 of the accessible part of the buffer.
446 Don't use this command in Lisp programs!
447 \(goto-char (point-max)) is faster and avoids clobbering the mark."
450 (let ((size (- (point-max) (point-min))))
454 ;; Avoid overflow for large buffer sizes!
455 (* (prefix-numeric-value arg
)
457 (/ (* size
(prefix-numeric-value arg
)) 10)))
459 ;; If we went to a place in the middle of the buffer,
460 ;; adjust it to the beginning of a line.
461 (cond (arg (forward-line 1))
462 ((> (point) (window-end nil t
))
463 ;; If the end of the buffer is not already on the screen,
464 ;; then scroll specially to put it near, but not at, the bottom.
465 (overlay-recenter (point))
468 (defun mark-whole-buffer ()
469 "Put point at beginning and mark at end of buffer.
470 You probably should not use this function in Lisp programs;
471 it is usually a mistake for a Lisp function to use any subroutine
472 that uses or sets the mark."
475 (push-mark (point-max) nil t
)
476 (goto-char (point-min)))
479 ;; Counting lines, one way or another.
481 (defun goto-line (arg)
482 "Goto line ARG, counting from line 1 at beginning of buffer."
483 (interactive "NGoto line: ")
484 (setq arg
(prefix-numeric-value arg
))
488 (if (eq selective-display t
)
489 (re-search-forward "[\n\C-m]" nil
'end
(1- arg
))
490 (forward-line (1- arg
)))))
492 (defun count-lines-region (start end
)
493 "Print number of lines and characters in the region."
495 (message "Region has %d lines, %d characters"
496 (count-lines start end
) (- end start
)))
499 "Print the current buffer line number and narrowed line number of point."
501 (let ((opoint (point)) (start (point-min))
502 (n (line-number-at-pos)))
504 (message "Line %d" n
)
508 (message "line %d (narrowed line %d)"
509 (+ n
(line-number-at-pos start
) -
1) n
))))))
511 (defun count-lines (start end
)
512 "Return number of lines between START and END.
513 This is usually the number of newlines between them,
514 but can be one more if START is not equal to END
515 and the greater of them is not at the start of a line."
518 (narrow-to-region start end
)
519 (goto-char (point-min))
520 (if (eq selective-display t
)
523 (while (re-search-forward "[\n\C-m]" nil t
40)
524 (setq done
(+ 40 done
)))
525 (while (re-search-forward "[\n\C-m]" nil t
1)
526 (setq done
(+ 1 done
)))
527 (goto-char (point-max))
528 (if (and (/= start end
)
532 (- (buffer-size) (forward-line (buffer-size)))))))
534 (defun line-number-at-pos (&optional pos
)
535 "Return (narrowed) buffer line number at position POS.
536 If POS is nil, use current buffer location."
537 (let ((opoint (or pos
(point))) start
)
539 (goto-char (point-min))
543 (1+ (count-lines start
(point))))))
545 (defun what-cursor-position (&optional detail
)
546 "Print info on cursor position (on screen and within buffer).
547 Also describe the character after point, and give its character code
548 in octal, decimal and hex.
550 For a non-ASCII multibyte character, also give its encoding in the
551 buffer's selected coding system if the coding system encodes the
552 character safely. If the character is encoded into one byte, that
553 code is shown in hex. If the character is encoded into more than one
554 byte, just \"...\" is shown.
556 In addition, with prefix argument, show details about that character
557 in *Help* buffer. See also the command `describe-char'."
559 (let* ((char (following-char))
563 (total (buffer-size))
564 (percent (if (> total
50000)
565 ;; Avoid overflow from multiplying by 100!
566 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
567 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
568 (hscroll (if (= (window-hscroll) 0)
570 (format " Hscroll=%d" (window-hscroll))))
571 (col (current-column)))
573 (if (or (/= beg
1) (/= end
(1+ total
)))
574 (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
575 pos total percent beg end col hscroll
)
576 (message "point=%d of %d (%d%%) column %d %s"
577 pos total percent col hscroll
))
578 (let ((coding buffer-file-coding-system
)
579 encoded encoding-msg
)
581 (eq (coding-system-type coding
) t
))
582 (setq coding default-buffer-file-coding-system
))
583 (if (not (char-valid-p char
))
585 (format "(0%o, %d, 0x%x, invalid)" char char char
))
586 (setq encoded
(and (>= char
128) (encode-coding-char char coding
)))
589 (format "(0%o, %d, 0x%x, file %s)"
591 (if (> (length encoded
) 1)
593 (encoded-string-description encoded coding
)))
594 (format "(0%o, %d, 0x%x)" char char char
))))
596 ;; We show the detailed information about CHAR.
597 (describe-char (point)))
598 (if (or (/= beg
1) (/= end
(1+ total
)))
599 (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
601 (single-key-description char
)
602 (buffer-substring-no-properties (point) (1+ (point))))
603 encoding-msg pos total percent beg end col hscroll
)
604 (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
606 (single-key-description char
)
607 (buffer-substring-no-properties (point) (1+ (point))))
608 encoding-msg pos total percent col hscroll
))))))
610 (defvar read-expression-map
611 (let ((m (make-sparse-keymap)))
612 (define-key m
"\M-\t" 'lisp-complete-symbol
)
613 (set-keymap-parent m minibuffer-local-map
)
615 "Minibuffer keymap used for reading Lisp expressions.")
617 (defvar read-expression-history nil
)
619 (defcustom eval-expression-print-level
4
620 "*Value to use for `print-level' when printing value in `eval-expression'.
621 A value of nil means no limit."
623 :type
'(choice (const :tag
"No Limit" nil
) integer
)
626 (defcustom eval-expression-print-length
12
627 "*Value to use for `print-length' when printing value in `eval-expression'.
628 A value of nil means no limit."
630 :type
'(choice (const :tag
"No Limit" nil
) integer
)
633 (defcustom eval-expression-debug-on-error t
634 "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'.
635 If nil, don't change the value of `debug-on-error'."
640 ;; We define this, rather than making `eval' interactive,
641 ;; for the sake of completion of names like eval-region, eval-current-buffer.
642 (defun eval-expression (eval-expression-arg
643 &optional eval-expression-insert-value
)
644 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
645 Value is also consed on to front of the variable `values'.
646 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
647 insert the result into the current buffer instead of printing it in
650 (list (read-from-minibuffer "Eval: "
651 nil read-expression-map t
652 'read-expression-history
)
655 (if (null eval-expression-debug-on-error
)
656 (setq values
(cons (eval eval-expression-arg
) values
))
657 (let ((old-value (make-symbol "t")) new-value
)
658 ;; Bind debug-on-error to something unique so that we can
659 ;; detect when evaled code changes it.
660 (let ((debug-on-error old-value
))
661 (setq values
(cons (eval eval-expression-arg
) values
))
662 (setq new-value debug-on-error
))
663 ;; If evaled code has changed the value of debug-on-error,
664 ;; propagate that change to the global binding.
665 (unless (eq old-value new-value
)
666 (setq debug-on-error new-value
))))
668 (let ((print-length eval-expression-print-length
)
669 (print-level eval-expression-print-level
))
670 (if eval-expression-insert-value
672 (let ((standard-output (current-buffer)))
673 (eval-last-sexp-print-value (car values
))))
674 (prin1 (car values
) t
))))
676 (defun edit-and-eval-command (prompt command
)
677 "Prompting with PROMPT, let user edit COMMAND and eval result.
678 COMMAND is a Lisp expression. Let user edit that expression in
679 the minibuffer, then read and evaluate the result."
681 (let ((print-level nil
)
682 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
684 (read-from-minibuffer prompt
685 (prin1-to-string command
)
686 read-expression-map t
688 ;; If command was added to command-history as a string,
689 ;; get rid of that. We want only evaluable expressions there.
690 (if (stringp (car command-history
))
691 (setq command-history
(cdr command-history
)))))))
693 ;; If command to be redone does not match front of history,
694 ;; add it to the history.
695 (or (equal command
(car command-history
))
696 (setq command-history
(cons command command-history
)))
699 (defun repeat-complex-command (arg)
700 "Edit and re-evaluate last complex command, or ARGth from last.
701 A complex command is one which used the minibuffer.
702 The command is placed in the minibuffer as a Lisp form for editing.
703 The result is executed, repeating the command as changed.
704 If the command has been changed or is not the most recent previous command
705 it is added to the front of the command history.
706 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
707 to get different commands to edit and resubmit."
709 (let ((elt (nth (1- arg
) command-history
))
714 (let ((print-level nil
)
715 (minibuffer-history-position arg
)
716 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
718 (read-from-minibuffer
719 "Redo: " (prin1-to-string elt
) read-expression-map t
720 (cons 'command-history arg
))
722 ;; If command was added to command-history as a
723 ;; string, get rid of that. We want only
724 ;; evaluable expressions there.
725 (if (stringp (car command-history
))
726 (setq command-history
(cdr command-history
))))))
728 ;; If command to be redone does not match front of history,
729 ;; add it to the history.
730 (or (equal newcmd
(car command-history
))
731 (setq command-history
(cons newcmd command-history
)))
734 (error "Argument %d is beyond length of command history" arg
)
735 (error "There are no previous complex commands to repeat")))))
737 (defvar minibuffer-history nil
738 "Default minibuffer history list.
739 This is used for all minibuffer input
740 except when an alternate history list is specified.")
741 (defvar minibuffer-history-sexp-flag nil
742 "Non-nil when doing history operations on the variable `command-history'.
743 More generally, indicates that the history list being acted on
744 contains expressions rather than strings.
745 It is only valid if its value equals the current minibuffer depth,
746 to handle recursive uses of the minibuffer.")
747 (setq minibuffer-history-variable
'minibuffer-history
)
748 (setq minibuffer-history-position nil
)
749 (defvar minibuffer-history-search-history nil
)
751 (defvar minibuffer-text-before-history nil
752 "Text that was in this minibuffer before any history commands.
753 This is nil if there have not yet been any history commands
754 in this use of the minibuffer.")
756 (add-hook 'minibuffer-setup-hook
'minibuffer-history-initialize
)
758 (defun minibuffer-history-initialize ()
759 (setq minibuffer-text-before-history nil
))
761 (defun minibuffer-avoid-prompt (new old
)
762 "A point-motion hook for the minibuffer, that moves point out of the prompt."
763 (constrain-to-field nil
(point-max)))
765 (defcustom minibuffer-history-case-insensitive-variables nil
766 "*Minibuffer history variables for which matching should ignore case.
767 If a history variable is a member of this list, then the
768 \\[previous-matching-history-element] and \\[next-matching-history-element]\
769 commands ignore case when searching it, regardless of `case-fold-search'."
770 :type
'(repeat variable
)
773 (defun previous-matching-history-element (regexp n
)
774 "Find the previous history element that matches REGEXP.
775 \(Previous history elements refer to earlier actions.)
776 With prefix argument N, search for Nth previous match.
777 If N is negative, find the next or Nth next match.
778 Normally, history elements are matched case-insensitively if
779 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
780 makes the search case-sensitive.
781 See also `minibuffer-history-case-insensitive-variables'."
783 (let* ((enable-recursive-minibuffers t
)
784 (regexp (read-from-minibuffer "Previous element matching (regexp): "
788 'minibuffer-history-search-history
)))
789 ;; Use the last regexp specified, by default, if input is empty.
790 (list (if (string= regexp
"")
791 (if minibuffer-history-search-history
792 (car minibuffer-history-search-history
)
793 (error "No previous history search regexp"))
795 (prefix-numeric-value current-prefix-arg
))))
797 (if (and (zerop minibuffer-history-position
)
798 (null minibuffer-text-before-history
))
799 (setq minibuffer-text-before-history
800 (minibuffer-contents-no-properties)))
801 (let ((history (symbol-value minibuffer-history-variable
))
803 (if (isearch-no-upper-case-p regexp t
) ; assume isearch.el is dumped
804 ;; On some systems, ignore case for file names.
805 (if (memq minibuffer-history-variable
806 minibuffer-history-case-insensitive-variables
)
808 ;; Respect the user's setting for case-fold-search:
814 (pos minibuffer-history-position
))
817 (setq pos
(min (max 1 (+ pos
(if (< n
0) -
1 1))) (length history
)))
818 (when (= pos prevpos
)
820 "No later matching history item"
821 "No earlier matching history item")))
823 (if (eq minibuffer-history-sexp-flag
(minibuffer-depth))
824 (let ((print-level nil
))
825 (prin1-to-string (nth (1- pos
) history
)))
826 (nth (1- pos
) history
)))
829 (and (string-match regexp match-string
)
831 (and (string-match (concat ".*\\(" regexp
"\\)") match-string
)
832 (match-beginning 1))))
834 (setq n
(+ n
(if (< n
0) 1 -
1)))))
835 (setq minibuffer-history-position pos
)
836 (goto-char (point-max))
837 (delete-minibuffer-contents)
838 (insert match-string
)
839 (goto-char (+ (minibuffer-prompt-end) match-offset
))))
840 (if (memq (car (car command-history
)) '(previous-matching-history-element
841 next-matching-history-element
))
842 (setq command-history
(cdr command-history
))))
844 (defun next-matching-history-element (regexp n
)
845 "Find the next history element that matches REGEXP.
846 \(The next history element refers to a more recent action.)
847 With prefix argument N, search for Nth next match.
848 If N is negative, find the previous or Nth previous match.
849 Normally, history elements are matched case-insensitively if
850 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
851 makes the search case-sensitive."
853 (let* ((enable-recursive-minibuffers t
)
854 (regexp (read-from-minibuffer "Next element matching (regexp): "
858 'minibuffer-history-search-history
)))
859 ;; Use the last regexp specified, by default, if input is empty.
860 (list (if (string= regexp
"")
861 (setcar minibuffer-history-search-history
862 (nth 1 minibuffer-history-search-history
))
864 (prefix-numeric-value current-prefix-arg
))))
865 (previous-matching-history-element regexp
(- n
)))
867 (defvar minibuffer-temporary-goal-position nil
)
869 (defun next-history-element (n)
870 "Insert the next element of the minibuffer history into the minibuffer."
873 (let ((narg (- minibuffer-history-position n
))
874 (minimum (if minibuffer-default -
1 0))
875 elt minibuffer-returned-to-present
)
876 (if (and (zerop minibuffer-history-position
)
877 (null minibuffer-text-before-history
))
878 (setq minibuffer-text-before-history
879 (minibuffer-contents-no-properties)))
881 (if minibuffer-default
882 (error "End of history; no next item")
883 (error "End of history; no default available")))
884 (if (> narg
(length (symbol-value minibuffer-history-variable
)))
885 (error "Beginning of history; no preceding item"))
886 (unless (memq last-command
'(next-history-element
887 previous-history-element
))
888 (let ((prompt-end (minibuffer-prompt-end)))
889 (set (make-local-variable 'minibuffer-temporary-goal-position
)
890 (cond ((<= (point) prompt-end
) prompt-end
)
893 (goto-char (point-max))
894 (delete-minibuffer-contents)
895 (setq minibuffer-history-position narg
)
897 (setq elt minibuffer-default
))
899 (setq elt
(or minibuffer-text-before-history
""))
900 (setq minibuffer-returned-to-present t
)
901 (setq minibuffer-text-before-history nil
))
902 (t (setq elt
(nth (1- minibuffer-history-position
)
903 (symbol-value minibuffer-history-variable
)))))
905 (if (and (eq minibuffer-history-sexp-flag
(minibuffer-depth))
906 (not minibuffer-returned-to-present
))
907 (let ((print-level nil
))
908 (prin1-to-string elt
))
910 (goto-char (or minibuffer-temporary-goal-position
(point-max))))))
912 (defun previous-history-element (n)
913 "Inserts the previous element of the minibuffer history into the minibuffer."
915 (next-history-element (- n
)))
917 (defun next-complete-history-element (n)
918 "Get next history element which completes the minibuffer before the point.
919 The contents of the minibuffer after the point are deleted, and replaced
920 by the new completion."
922 (let ((point-at-start (point)))
923 (next-matching-history-element
925 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
927 ;; next-matching-history-element always puts us at (point-min).
928 ;; Move to the position we were at before changing the buffer contents.
929 ;; This is still sensical, because the text before point has not changed.
930 (goto-char point-at-start
)))
932 (defun previous-complete-history-element (n)
934 Get previous history element which completes the minibuffer before the point.
935 The contents of the minibuffer after the point are deleted, and replaced
936 by the new completion."
938 (next-complete-history-element (- n
)))
940 ;; For compatibility with the old subr of the same name.
941 (defun minibuffer-prompt-width ()
942 "Return the display width of the minibuffer prompt.
943 Return 0 if current buffer is not a mini-buffer."
944 ;; Return the width of everything before the field at the end of
945 ;; the buffer; this should be 0 for normal buffers.
946 (1- (minibuffer-prompt-end)))
948 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
949 (defalias 'advertised-undo
'undo
)
951 (defconst undo-equiv-table
(make-hash-table :test
'eq
:weakness t
)
952 "Table mapping redo records to the corresponding undo one.")
954 (defvar undo-in-region nil
955 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
957 (defvar undo-no-redo nil
958 "If t, `undo' doesn't go through redo entries.")
960 (defun undo (&optional arg
)
961 "Undo some previous changes.
962 Repeat this command to undo more changes.
963 A numeric argument serves as a repeat count.
965 In Transient Mark mode when the mark is active, only undo changes within
966 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
967 as an argument limits undo to changes within the current region."
969 ;; Make last-command indicate for the next command that this was an undo.
970 ;; That way, another undo will undo more.
971 ;; If we get to the end of the undo history and get an error,
972 ;; another undo command will find the undo history empty
973 ;; and will get another error. To begin undoing the undos,
974 ;; you must type some other command.
975 (setq this-command
'undo
)
976 (let ((modified (buffer-modified-p))
977 (recent-save (recent-auto-save-p)))
978 (unless (eq last-command
'undo
)
980 (if transient-mark-mode mark-active
(and arg
(not (numberp arg
)))))
982 (undo-start (region-beginning) (region-end))
984 ;; get rid of initial undo boundary
986 ;; Check to see whether we're hitting a redo record, and if
987 ;; so, ask the user whether she wants to skip the redo/undo pair.
988 (let ((equiv (gethash pending-undo-list undo-equiv-table
)))
989 (or (eq (selected-window) (minibuffer-window))
990 (message (if undo-in-region
991 (if equiv
"Redo in region!" "Undo in region!")
992 (if equiv
"Redo!" "Undo!"))))
993 (when (and equiv undo-no-redo
)
994 ;; The equiv entry might point to another redo record if we have done
995 ;; undo-redo-undo-redo-... so skip to the very last equiv.
996 (while (let ((next (gethash equiv undo-equiv-table
)))
997 (if next
(setq equiv next
))))
998 (setq pending-undo-list equiv
)))
1000 (if (or transient-mark-mode
(numberp arg
))
1001 (prefix-numeric-value arg
)
1003 ;; Record the fact that the just-generated undo records come from an
1004 ;; undo operation, so we can skip them later on.
1005 ;; I don't know how to do that in the undo-in-region case.
1006 (unless undo-in-region
1007 (puthash buffer-undo-list pending-undo-list undo-equiv-table
))
1008 ;; Don't specify a position in the undo record for the undo command.
1009 ;; Instead, undoing this should move point to where the change is.
1010 (let ((tail buffer-undo-list
)
1013 (when (integerp (car tail
))
1014 (let ((pos (car tail
)))
1016 (setcdr prev
(cdr tail
))
1017 (setq buffer-undo-list
(cdr tail
)))
1018 (setq tail
(cdr tail
))
1020 (if (eq pos
(car tail
))
1022 (setcdr prev
(cdr tail
))
1023 (setq buffer-undo-list
(cdr tail
)))
1025 (setq tail
(cdr tail
)))
1027 (setq prev tail tail
(cdr tail
))))
1029 (and modified
(not (buffer-modified-p))
1030 (delete-auto-save-file-if-necessary recent-save
))))
1032 (defun undo-only (&optional arg
)
1033 "Undo some previous changes.
1034 Repeat this command to undo more changes.
1035 A numeric argument serves as a repeat count.
1036 Contrary to `undo', this will not redo a previous undo."
1038 (let ((undo-no-redo t
)) (undo arg
)))
1039 ;; Richard said that we should not use C-x <uppercase letter> and I have
1040 ;; no idea whereas to bind it. Any suggestion welcome. -stef
1041 ;; (define-key ctl-x-map "U" 'undo-only)
1043 (defvar pending-undo-list nil
1044 "Within a run of consecutive undo commands, list remaining to be undone.")
1046 (defvar undo-in-progress nil
1047 "Non-nil while performing an undo.
1048 Some change-hooks test this variable to do something different.")
1050 (defun undo-more (count)
1051 "Undo back N undo-boundaries beyond what was already undone recently.
1052 Call `undo-start' to get ready to undo recent changes,
1053 then call `undo-more' one or more times to undo them."
1054 (or pending-undo-list
1055 (error (format "No further undo information%s"
1056 (if (and transient-mark-mode mark-active
)
1057 " for region" ""))))
1058 (let ((undo-in-progress t
))
1059 (setq pending-undo-list
(primitive-undo count pending-undo-list
))))
1061 ;; Deep copy of a list
1062 (defun undo-copy-list (list)
1063 "Make a copy of undo list LIST."
1064 (mapcar 'undo-copy-list-1 list
))
1066 (defun undo-copy-list-1 (elt)
1068 (cons (car elt
) (undo-copy-list-1 (cdr elt
)))
1071 (defun undo-start (&optional beg end
)
1072 "Set `pending-undo-list' to the front of the undo list.
1073 The next call to `undo-more' will undo the most recently made change.
1074 If BEG and END are specified, then only undo elements
1075 that apply to text between BEG and END are used; other undo elements
1076 are ignored. If BEG and END are nil, all undo elements are used."
1077 (if (eq buffer-undo-list t
)
1078 (error "No undo information in this buffer"))
1079 (setq pending-undo-list
1080 (if (and beg end
(not (= beg end
)))
1081 (undo-make-selective-list (min beg end
) (max beg end
))
1084 (defvar undo-adjusted-markers
)
1086 (defun undo-make-selective-list (start end
)
1087 "Return a list of undo elements for the region START to END.
1088 The elements come from `buffer-undo-list', but we keep only
1089 the elements inside this region, and discard those outside this region.
1090 If we find an element that crosses an edge of this region,
1091 we stop and ignore all further elements."
1092 (let ((undo-list-copy (undo-copy-list buffer-undo-list
))
1093 (undo-list (list nil
))
1094 undo-adjusted-markers
1096 undo-elt undo-elt temp-undo-list delta
)
1097 (while undo-list-copy
1098 (setq undo-elt
(car undo-list-copy
))
1100 (cond ((and (consp undo-elt
) (eq (car undo-elt
) t
))
1101 ;; This is a "was unmodified" element.
1102 ;; Keep it if we have kept everything thus far.
1103 (not some-rejected
))
1105 (undo-elt-in-region undo-elt start end
)))))
1108 (setq end
(+ end
(cdr (undo-delta undo-elt
))))
1109 ;; Don't put two nils together in the list
1110 (if (not (and (eq (car undo-list
) nil
)
1112 (setq undo-list
(cons undo-elt undo-list
))))
1113 (if (undo-elt-crosses-region undo-elt start end
)
1114 (setq undo-list-copy nil
)
1115 (setq some-rejected t
)
1116 (setq temp-undo-list
(cdr undo-list-copy
))
1117 (setq delta
(undo-delta undo-elt
))
1119 (when (/= (cdr delta
) 0)
1120 (let ((position (car delta
))
1121 (offset (cdr delta
)))
1123 ;; Loop down the earlier events adjusting their buffer
1124 ;; positions to reflect the fact that a change to the buffer
1125 ;; isn't being undone. We only need to process those element
1126 ;; types which undo-elt-in-region will return as being in
1127 ;; the region since only those types can ever get into the
1130 (while temp-undo-list
1131 (setq undo-elt
(car temp-undo-list
))
1132 (cond ((integerp undo-elt
)
1133 (if (>= undo-elt position
)
1134 (setcar temp-undo-list
(- undo-elt offset
))))
1135 ((atom undo-elt
) nil
)
1136 ((stringp (car undo-elt
))
1137 ;; (TEXT . POSITION)
1138 (let ((text-pos (abs (cdr undo-elt
)))
1139 (point-at-end (< (cdr undo-elt
) 0 )))
1140 (if (>= text-pos position
)
1141 (setcdr undo-elt
(* (if point-at-end -
1 1)
1142 (- text-pos offset
))))))
1143 ((integerp (car undo-elt
))
1145 (when (>= (car undo-elt
) position
)
1146 (setcar undo-elt
(- (car undo-elt
) offset
))
1147 (setcdr undo-elt
(- (cdr undo-elt
) offset
))))
1148 ((null (car undo-elt
))
1149 ;; (nil PROPERTY VALUE BEG . END)
1150 (let ((tail (nthcdr 3 undo-elt
)))
1151 (when (>= (car tail
) position
)
1152 (setcar tail
(- (car tail
) offset
))
1153 (setcdr tail
(- (cdr tail
) offset
))))))
1154 (setq temp-undo-list
(cdr temp-undo-list
))))))))
1155 (setq undo-list-copy
(cdr undo-list-copy
)))
1156 (nreverse undo-list
)))
1158 (defun undo-elt-in-region (undo-elt start end
)
1159 "Determine whether UNDO-ELT falls inside the region START ... END.
1160 If it crosses the edge, we return nil."
1161 (cond ((integerp undo-elt
)
1162 (and (>= undo-elt start
)
1168 ((stringp (car undo-elt
))
1169 ;; (TEXT . POSITION)
1170 (and (>= (abs (cdr undo-elt
)) start
)
1171 (< (abs (cdr undo-elt
)) end
)))
1172 ((and (consp undo-elt
) (markerp (car undo-elt
)))
1173 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1174 ;; See if MARKER is inside the region.
1175 (let ((alist-elt (assq (car undo-elt
) undo-adjusted-markers
)))
1177 (setq alist-elt
(cons (car undo-elt
)
1178 (marker-position (car undo-elt
))))
1179 (setq undo-adjusted-markers
1180 (cons alist-elt undo-adjusted-markers
)))
1181 (and (cdr alist-elt
)
1182 (>= (cdr alist-elt
) start
)
1183 (<= (cdr alist-elt
) end
))))
1184 ((null (car undo-elt
))
1185 ;; (nil PROPERTY VALUE BEG . END)
1186 (let ((tail (nthcdr 3 undo-elt
)))
1187 (and (>= (car tail
) start
)
1188 (<= (cdr tail
) end
))))
1189 ((integerp (car undo-elt
))
1191 (and (>= (car undo-elt
) start
)
1192 (<= (cdr undo-elt
) end
)))))
1194 (defun undo-elt-crosses-region (undo-elt start end
)
1195 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1196 This assumes we have already decided that UNDO-ELT
1197 is not *inside* the region START...END."
1198 (cond ((atom undo-elt
) nil
)
1199 ((null (car undo-elt
))
1200 ;; (nil PROPERTY VALUE BEG . END)
1201 (let ((tail (nthcdr 3 undo-elt
)))
1202 (not (or (< (car tail
) end
)
1203 (> (cdr tail
) start
)))))
1204 ((integerp (car undo-elt
))
1206 (not (or (< (car undo-elt
) end
)
1207 (> (cdr undo-elt
) start
))))))
1209 ;; Return the first affected buffer position and the delta for an undo element
1210 ;; delta is defined as the change in subsequent buffer positions if we *did*
1212 (defun undo-delta (undo-elt)
1213 (if (consp undo-elt
)
1214 (cond ((stringp (car undo-elt
))
1215 ;; (TEXT . POSITION)
1216 (cons (abs (cdr undo-elt
)) (length (car undo-elt
))))
1217 ((integerp (car undo-elt
))
1219 (cons (car undo-elt
) (- (car undo-elt
) (cdr undo-elt
))))
1224 (defvar shell-command-history nil
1225 "History list for some commands that read shell commands.")
1227 (defvar shell-command-switch
"-c"
1228 "Switch used to have the shell execute its command line argument.")
1230 (defvar shell-command-default-error-buffer nil
1231 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1232 This buffer is used when `shell-command' or `shell-command-on-region'
1233 is run interactively. A value of nil means that output to stderr and
1234 stdout will be intermixed in the output stream.")
1236 (defun shell-command (command &optional output-buffer error-buffer
)
1237 "Execute string COMMAND in inferior shell; display output, if any.
1238 With prefix argument, insert the COMMAND's output at point.
1240 If COMMAND ends in ampersand, execute it asynchronously.
1241 The output appears in the buffer `*Async Shell Command*'.
1242 That buffer is in shell mode.
1244 Otherwise, COMMAND is executed synchronously. The output appears in
1245 the buffer `*Shell Command Output*'. If the output is short enough to
1246 display in the echo area (which is determined by the variables
1247 `resize-mini-windows' and `max-mini-window-height'), it is shown
1248 there, but it is nonetheless available in buffer `*Shell Command
1249 Output*' even though that buffer is not automatically displayed.
1251 To specify a coding system for converting non-ASCII characters
1252 in the shell command output, use \\[universal-coding-system-argument]
1253 before this command.
1255 Noninteractive callers can specify coding systems by binding
1256 `coding-system-for-read' and `coding-system-for-write'.
1258 The optional second argument OUTPUT-BUFFER, if non-nil,
1259 says to put the output in some other buffer.
1260 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1261 If OUTPUT-BUFFER is not a buffer and not nil,
1262 insert output in current buffer. (This cannot be done asynchronously.)
1263 In either case, the output is inserted after point (leaving mark after it).
1265 If the command terminates without error, but generates output,
1266 and you did not specify \"insert it in the current buffer\",
1267 the output can be displayed in the echo area or in its buffer.
1268 If the output is short enough to display in the echo area
1269 \(determined by the variable `max-mini-window-height' if
1270 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1271 the buffer containing the output is displayed.
1273 If there is output and an error, and you did not specify \"insert it
1274 in the current buffer\", a message about the error goes at the end
1277 If there is no output, or if output is inserted in the current buffer,
1278 then `*Shell Command Output*' is deleted.
1280 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1281 or buffer name to which to direct the command's standard error output.
1282 If it is nil, error output is mingled with regular output.
1283 In an interactive call, the variable `shell-command-default-error-buffer'
1284 specifies the value of ERROR-BUFFER."
1286 (interactive (list (read-from-minibuffer "Shell command: "
1287 nil nil nil
'shell-command-history
)
1289 shell-command-default-error-buffer
))
1290 ;; Look for a handler in case default-directory is a remote file name.
1292 (find-file-name-handler (directory-file-name default-directory
)
1295 (funcall handler
'shell-command command output-buffer error-buffer
)
1296 (if (and output-buffer
1297 (not (or (bufferp output-buffer
) (stringp output-buffer
))))
1298 ;; Output goes in current buffer.
1302 (expand-file-name "scor"
1303 (or small-temporary-file-directory
1304 temporary-file-directory
)))
1306 (barf-if-buffer-read-only)
1308 ;; We do not use -f for csh; we will not support broken use of
1309 ;; .cshrcs. Even the BSD csh manual says to use
1310 ;; "if ($?prompt) exit" before things which are not useful
1311 ;; non-interactively. Besides, if someone wants their other
1312 ;; aliases for shell commands then they can still have them.
1313 (call-process shell-file-name nil
1317 nil shell-command-switch command
)
1318 (when (and error-file
(file-exists-p error-file
))
1319 (if (< 0 (nth 7 (file-attributes error-file
)))
1320 (with-current-buffer (get-buffer-create error-buffer
)
1321 (let ((pos-from-end (- (point-max) (point))))
1324 ;; Do no formatting while reading error file,
1325 ;; because that can run a shell command, and we
1326 ;; don't want that to cause an infinite recursion.
1327 (format-insert-file error-file nil
)
1328 ;; Put point after the inserted errors.
1329 (goto-char (- (point-max) pos-from-end
)))
1330 (display-buffer (current-buffer))))
1331 (delete-file error-file
))
1332 ;; This is like exchange-point-and-mark, but doesn't
1333 ;; activate the mark. It is cleaner to avoid activation,
1334 ;; even though the command loop would deactivate the mark
1335 ;; because we inserted text.
1336 (goto-char (prog1 (mark t
)
1337 (set-marker (mark-marker) (point)
1338 (current-buffer)))))
1339 ;; Output goes in a separate buffer.
1340 ;; Preserve the match data in case called from a program.
1342 (if (string-match "[ \t]*&[ \t]*\\'" command
)
1343 ;; Command ending with ampersand means asynchronous.
1344 (let ((buffer (get-buffer-create
1345 (or output-buffer
"*Async Shell Command*")))
1346 (directory default-directory
)
1348 ;; Remove the ampersand.
1349 (setq command
(substring command
0 (match-beginning 0)))
1350 ;; If will kill a process, query first.
1351 (setq proc
(get-buffer-process buffer
))
1353 (if (yes-or-no-p "A command is running. Kill it? ")
1355 (error "Shell command in progress")))
1356 (with-current-buffer buffer
1357 (setq buffer-read-only nil
)
1359 (display-buffer buffer
)
1360 (setq default-directory directory
)
1361 (setq proc
(start-process "Shell" buffer shell-file-name
1362 shell-command-switch command
))
1363 (setq mode-line-process
'(":%s"))
1364 (require 'shell
) (shell-mode)
1365 (set-process-sentinel proc
'shell-command-sentinel
)
1367 (shell-command-on-region (point) (point) command
1368 output-buffer nil error-buffer
)))))))
1370 (defun display-message-or-buffer (message
1371 &optional buffer-name not-this-window frame
)
1372 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1373 MESSAGE may be either a string or a buffer.
1375 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1376 the maximum height of the echo area, as defined by `max-mini-window-height'
1377 if `resize-mini-windows' is non-nil.
1379 Returns either the string shown in the echo area, or when a pop-up
1380 buffer is used, the window used to display it.
1382 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1383 name of the buffer used to display it in the case where a pop-up buffer
1384 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1385 string and it is displayed in the echo area, it is not specified whether
1386 the contents are inserted into the buffer anyway.
1388 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1389 and only used if a buffer is displayed."
1390 (cond ((and (stringp message
) (not (string-match "\n" message
)))
1391 ;; Trivial case where we can use the echo area
1392 (message "%s" message
))
1393 ((and (stringp message
)
1394 (= (string-match "\n" message
) (1- (length message
))))
1395 ;; Trivial case where we can just remove single trailing newline
1396 (message "%s" (substring message
0 (1- (length message
)))))
1399 (with-current-buffer
1400 (if (bufferp message
)
1402 (get-buffer-create (or buffer-name
"*Message*")))
1404 (unless (bufferp message
)
1409 (if (= (buffer-size) 0)
1411 (count-lines (point-min) (point-max)))))
1413 ((and (or (<= lines
1)
1415 (if resize-mini-windows
1416 (cond ((floatp max-mini-window-height
)
1418 max-mini-window-height
))
1419 ((integerp max-mini-window-height
)
1420 max-mini-window-height
)
1424 ;; Don't use the echo area if the output buffer is
1425 ;; already dispayed in the selected frame.
1426 (not (get-buffer-window (current-buffer))))
1428 (goto-char (point-max))
1431 (message "%s" (buffer-substring (point-min) (point))))
1434 (goto-char (point-min))
1435 (display-buffer (current-buffer)
1436 not-this-window frame
))))))))
1439 ;; We have a sentinel to prevent insertion of a termination message
1440 ;; in the buffer itself.
1441 (defun shell-command-sentinel (process signal
)
1442 (if (memq (process-status process
) '(exit signal
))
1444 (car (cdr (cdr (process-command process
))))
1445 (substring signal
0 -
1))))
1447 (defun shell-command-on-region (start end command
1448 &optional output-buffer replace
1450 "Execute string COMMAND in inferior shell with region as input.
1451 Normally display output (if any) in temp buffer `*Shell Command Output*';
1452 Prefix arg means replace the region with it. Return the exit code of
1455 To specify a coding system for converting non-ASCII characters
1456 in the input and output to the shell command, use \\[universal-coding-system-argument]
1457 before this command. By default, the input (from the current buffer)
1458 is encoded in the same coding system that will be used to save the file,
1459 `buffer-file-coding-system'. If the output is going to replace the region,
1460 then it is decoded from that same coding system.
1462 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1463 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1464 systems by binding `coding-system-for-read' and
1465 `coding-system-for-write'.
1467 If the command generates output, the output may be displayed
1468 in the echo area or in a buffer.
1469 If the output is short enough to display in the echo area
1470 \(determined by the variable `max-mini-window-height' if
1471 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1472 it is displayed in the buffer `*Shell Command Output*'. The output
1473 is available in that buffer in both cases.
1475 If there is output and an error, a message about the error
1476 appears at the end of the output.
1478 If there is no output, or if output is inserted in the current buffer,
1479 then `*Shell Command Output*' is deleted.
1481 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1482 that says to put the output in some other buffer.
1483 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1484 If OUTPUT-BUFFER is not a buffer and not nil,
1485 insert output in the current buffer.
1486 In either case, the output is inserted after point (leaving mark after it).
1488 If REPLACE, the optional fifth argument, is non-nil, that means insert
1489 the output in place of text from START to END, putting point and mark
1492 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1493 or buffer name to which to direct the command's standard error output.
1494 If it is nil, error output is mingled with regular output.
1495 In an interactive call, the variable `shell-command-default-error-buffer'
1496 specifies the value of ERROR-BUFFER."
1497 (interactive (let (string)
1499 (error "The mark is not set now, so there is no region"))
1500 ;; Do this before calling region-beginning
1501 ;; and region-end, in case subprocess output
1502 ;; relocates them while we are in the minibuffer.
1503 (setq string
(read-from-minibuffer "Shell command on region: "
1505 'shell-command-history
))
1506 ;; call-interactively recognizes region-beginning and
1507 ;; region-end specially, leaving them in the history.
1508 (list (region-beginning) (region-end)
1512 shell-command-default-error-buffer
)))
1516 (expand-file-name "scor"
1517 (or small-temporary-file-directory
1518 temporary-file-directory
)))
1523 (not (or (bufferp output-buffer
) (stringp output-buffer
)))))
1524 ;; Replace specified region with output from command.
1525 (let ((swap (and replace
(< start end
))))
1526 ;; Don't muck with mark unless REPLACE says we should.
1528 (and replace
(push-mark (point) 'nomsg
))
1530 (call-process-region start end shell-file-name t
1534 nil shell-command-switch command
))
1535 ;; It is rude to delete a buffer which the command is not using.
1536 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1537 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1538 ;; (kill-buffer shell-buffer)))
1539 ;; Don't muck with mark unless REPLACE says we should.
1540 (and replace swap
(exchange-point-and-mark)))
1541 ;; No prefix argument: put the output in a temp buffer,
1542 ;; replacing its entire contents.
1543 (let ((buffer (get-buffer-create
1544 (or output-buffer
"*Shell Command Output*"))))
1546 (if (eq buffer
(current-buffer))
1547 ;; If the input is the same buffer as the output,
1548 ;; delete everything but the specified region,
1549 ;; then replace that region with the output.
1550 (progn (setq buffer-read-only nil
)
1551 (delete-region (max start end
) (point-max))
1552 (delete-region (point-min) (min start end
))
1554 (call-process-region (point-min) (point-max)
1559 nil shell-command-switch
1561 ;; Clear the output buffer, then run the command with
1563 (let ((directory default-directory
))
1566 (setq buffer-read-only nil
)
1567 (if (not output-buffer
)
1568 (setq default-directory directory
))
1571 (call-process-region start end shell-file-name nil
1573 (list buffer error-file
)
1575 nil shell-command-switch command
)))
1576 ;; Report the output.
1577 (with-current-buffer buffer
1578 (setq mode-line-process
1579 (cond ((null exit-status
)
1581 ((stringp exit-status
)
1582 (format " - Signal [%s]" exit-status
))
1583 ((not (equal 0 exit-status
))
1584 (format " - Exit [%d]" exit-status
)))))
1585 (if (with-current-buffer buffer
(> (point-max) (point-min)))
1586 ;; There's some output, display it
1587 (display-message-or-buffer buffer
)
1588 ;; No output; error?
1591 (< 0 (nth 7 (file-attributes error-file
))))
1594 (cond ((null exit-status
)
1595 (message "(Shell command failed with error)"))
1596 ((equal 0 exit-status
)
1597 (message "(Shell command succeeded with %s)"
1599 ((stringp exit-status
)
1600 (message "(Shell command killed by signal %s)"
1603 (message "(Shell command failed with code %d and %s)"
1604 exit-status output
))))
1605 ;; Don't kill: there might be useful info in the undo-log.
1606 ;; (kill-buffer buffer)
1609 (when (and error-file
(file-exists-p error-file
))
1610 (if (< 0 (nth 7 (file-attributes error-file
)))
1611 (with-current-buffer (get-buffer-create error-buffer
)
1612 (let ((pos-from-end (- (point-max) (point))))
1615 ;; Do no formatting while reading error file,
1616 ;; because that can run a shell command, and we
1617 ;; don't want that to cause an infinite recursion.
1618 (format-insert-file error-file nil
)
1619 ;; Put point after the inserted errors.
1620 (goto-char (- (point-max) pos-from-end
)))
1621 (display-buffer (current-buffer))))
1622 (delete-file error-file
))
1625 (defun shell-command-to-string (command)
1626 "Execute shell command COMMAND and return its output as a string."
1627 (with-output-to-string
1628 (with-current-buffer
1630 (call-process shell-file-name nil t nil shell-command-switch command
))))
1632 (defvar universal-argument-map
1633 (let ((map (make-sparse-keymap)))
1634 (define-key map
[t] 'universal-argument-other-key)
1635 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1636 (define-key map [switch-frame] nil)
1637 (define-key map [?\C-u] 'universal-argument-more)
1638 (define-key map [?-] 'universal-argument-minus)
1639 (define-key map [?0] 'digit-argument)
1640 (define-key map [?1] 'digit-argument)
1641 (define-key map [?2] 'digit-argument)
1642 (define-key map [?3] 'digit-argument)
1643 (define-key map [?4] 'digit-argument)
1644 (define-key map [?5] 'digit-argument)
1645 (define-key map [?6] 'digit-argument)
1646 (define-key map [?7] 'digit-argument)
1647 (define-key map [?8] 'digit-argument)
1648 (define-key map [?9] 'digit-argument)
1649 (define-key map [kp-0] 'digit-argument)
1650 (define-key map [kp-1] 'digit-argument)
1651 (define-key map [kp-2] 'digit-argument)
1652 (define-key map [kp-3] 'digit-argument)
1653 (define-key map [kp-4] 'digit-argument)
1654 (define-key map [kp-5] 'digit-argument)
1655 (define-key map [kp-6] 'digit-argument)
1656 (define-key map [kp-7] 'digit-argument)
1657 (define-key map [kp-8] 'digit-argument)
1658 (define-key map [kp-9] 'digit-argument)
1659 (define-key map [kp-subtract] 'universal-argument-minus)
1661 "Keymap used while processing \\[universal-argument].")
1663 (defvar universal-argument-num-events nil
1664 "Number of argument-specifying events read by `universal-argument'.
1665 `universal-argument-other-key' uses this to discard those events
1666 from (this-command-keys), and reread only the final command.")
1668 (defvar overriding-map-is-bound nil
1669 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
1671 (defvar saved-overriding-map nil
1672 "The saved value of `overriding-terminal-local-map'.
1673 That variable gets restored to this value on exiting \"universal
1676 (defun ensure-overriding-map-is-bound ()
1677 "Check `overriding-terminal-local-map' is `universal-argument-map'."
1678 (unless overriding-map-is-bound
1679 (setq saved-overriding-map overriding-terminal-local-map)
1680 (setq overriding-terminal-local-map universal-argument-map)
1681 (setq overriding-map-is-bound t)))
1683 (defun restore-overriding-map ()
1684 "Restore `overriding-terminal-local-map' to its saved value."
1685 (setq overriding-terminal-local-map saved-overriding-map)
1686 (setq overriding-map-is-bound nil))
1688 (defun universal-argument ()
1689 "Begin a numeric argument for the following command.
1690 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1691 \\[universal-argument] following the digits or minus sign ends the argument.
1692 \\[universal-argument] without digits or minus sign provides 4 as argument.
1693 Repeating \\[universal-argument] without digits or minus sign
1694 multiplies the argument by 4 each time.
1695 For some commands, just \\[universal-argument] by itself serves as a flag
1696 which is different in effect from any particular numeric argument.
1697 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1699 (setq prefix-arg (list 4))
1700 (setq universal-argument-num-events (length (this-command-keys)))
1701 (ensure-overriding-map-is-bound))
1703 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1704 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1705 (defun universal-argument-more (arg)
1708 (setq prefix-arg (list (* 4 (car arg))))
1710 (setq prefix-arg (list -4))
1711 (setq prefix-arg arg)
1712 (restore-overriding-map)))
1713 (setq universal-argument-num-events (length (this-command-keys))))
1715 (defun negative-argument (arg)
1716 "Begin a negative numeric argument for the next command.
1717 \\[universal-argument] following digits or minus sign ends the argument."
1719 (cond ((integerp arg)
1720 (setq prefix-arg (- arg)))
1722 (setq prefix-arg nil))
1724 (setq prefix-arg '-)))
1725 (setq universal-argument-num-events (length (this-command-keys)))
1726 (ensure-overriding-map-is-bound))
1728 (defun digit-argument (arg)
1729 "Part of the numeric argument for the next command.
1730 \\[universal-argument] following digits or minus sign ends the argument."
1732 (let* ((char (if (integerp last-command-char)
1734 (get last-command-char 'ascii-character)))
1735 (digit (- (logand char ?\177) ?0)))
1736 (cond ((integerp arg)
1737 (setq prefix-arg (+ (* arg 10)
1738 (if (< arg 0) (- digit) digit))))
1740 ;; Treat -0 as just -, so that -01 will work.
1741 (setq prefix-arg (if (zerop digit) '- (- digit))))
1743 (setq prefix-arg digit))))
1744 (setq universal-argument-num-events (length (this-command-keys)))
1745 (ensure-overriding-map-is-bound))
1747 ;; For backward compatibility, minus with no modifiers is an ordinary
1748 ;; command if digits have already been entered.
1749 (defun universal-argument-minus (arg)
1752 (universal-argument-other-key arg)
1753 (negative-argument arg)))
1755 ;; Anything else terminates the argument and is left in the queue to be
1756 ;; executed as a command.
1757 (defun universal-argument-other-key (arg)
1759 (setq prefix-arg arg)
1760 (let* ((key (this-command-keys))
1761 (keylist (listify-key-sequence key)))
1762 (setq unread-command-events
1763 (append (nthcdr universal-argument-num-events keylist)
1764 unread-command-events)))
1765 (reset-this-command-lengths)
1766 (restore-overriding-map))
1768 ;;;; Window system cut and paste hooks.
1770 (defvar interprogram-cut-function nil
1771 "Function to call to make a killed region available to other programs.
1773 Most window systems provide some sort of facility for cutting and
1774 pasting text between the windows of different programs.
1775 This variable holds a function that Emacs calls whenever text
1776 is put in the kill ring, to make the new kill available to other
1779 The function takes one or two arguments.
1780 The first argument, TEXT, is a string containing
1781 the text which should be made available.
1782 The second, PUSH, if non-nil means this is a \"new\" kill;
1783 nil means appending to an \"old\" kill.")
1785 (defvar interprogram-paste-function nil
1786 "Function to call to get text cut from other programs.
1788 Most window systems provide some sort of facility for cutting and
1789 pasting text between the windows of different programs.
1790 This variable holds a function that Emacs calls to obtain
1791 text that other programs have provided for pasting.
1793 The function should be called with no arguments. If the function
1794 returns nil, then no other program has provided such text, and the top
1795 of the Emacs kill ring should be used. If the function returns a
1796 string, that string should be put in the kill ring as the latest kill.
1798 Note that the function should return a string only if a program other
1799 than Emacs has provided a string for pasting; if Emacs provided the
1800 most recent string, the function should return nil. If it is
1801 difficult to tell whether Emacs or some other program provided the
1802 current string, it is probably good enough to return nil if the string
1803 is equal (according to `string=') to the last text Emacs provided.")
1807 ;;;; The kill ring data structure.
1809 (defvar kill-ring nil
1810 "List of killed text sequences.
1811 Since the kill ring is supposed to interact nicely with cut-and-paste
1812 facilities offered by window systems, use of this variable should
1813 interact nicely with `interprogram-cut-function' and
1814 `interprogram-paste-function'. The functions `kill-new',
1815 `kill-append', and `current-kill' are supposed to implement this
1816 interaction; you may want to use them instead of manipulating the kill
1819 (defcustom kill-ring-max 60
1820 "*Maximum length of kill ring before oldest elements are thrown away."
1824 (defvar kill-ring-yank-pointer nil
1825 "The tail of the kill ring whose car is the last thing yanked.")
1827 (defun kill-new (string &optional replace yank-handler)
1828 "Make STRING the latest kill in the kill ring.
1829 Set `kill-ring-yank-pointer' to point to it.
1830 If `interprogram-cut-function' is non-nil, apply it to STRING.
1831 Optional second argument REPLACE non-nil means that STRING will replace
1832 the front of the kill ring, rather than being added to the list.
1834 Optional third arguments YANK-HANDLER controls how the STRING is later
1835 inserted into a buffer; see `insert-for-yank' for details.
1836 When a yank handler is specified, STRING must be non-empty (the yank
1837 handler is stored as a `yank-handler'text property on STRING).
1839 When the yank handler has a non-nil PARAM element, the original STRING
1840 argument is not used by `insert-for-yank'. However, since Lisp code
1841 may access and use elements from the kill-ring directly, the STRING
1842 argument should still be a \"useful\" string for such uses."
1843 (if (> (length string) 0)
1845 (put-text-property 0 1 'yank-handler yank-handler string))
1847 (signal 'args-out-of-range
1848 (list string "yank-handler specified for empty string"))))
1849 (if (fboundp 'menu-bar-update-yank-menu)
1850 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1851 (if (and replace kill-ring)
1852 (setcar kill-ring string)
1853 (setq kill-ring (cons string kill-ring))
1854 (if (> (length kill-ring) kill-ring-max)
1855 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1856 (setq kill-ring-yank-pointer kill-ring)
1857 (if interprogram-cut-function
1858 (funcall interprogram-cut-function string (not replace))))
1860 (defun kill-append (string before-p &optional yank-handler)
1861 "Append STRING to the end of the latest kill in the kill ring.
1862 If BEFORE-P is non-nil, prepend STRING to the kill.
1863 Optional third argument YANK-HANDLER specifies the yank-handler text
1864 property to be set on the combined kill ring string. If the specified
1865 yank-handler arg differs from the yank-handler property of the latest
1866 kill string, STRING is added as a new kill ring element instead of
1867 being appending to the last kill.
1868 If `interprogram-cut-function' is set, pass the resulting kill to it."
1869 (let* ((cur (car kill-ring)))
1870 (kill-new (if before-p (concat string cur) (concat cur string))
1871 (or (= (length cur) 0)
1872 (equal yank-handler (get-text-property 0 'yank-handler cur)))
1875 (defun current-kill (n &optional do-not-move)
1876 "Rotate the yanking point by N places, and then return that kill.
1877 If N is zero, `interprogram-paste-function' is set, and calling it
1878 returns a string, then that string is added to the front of the
1879 kill ring and returned as the latest kill.
1880 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1881 yanking point; just return the Nth kill forward."
1882 (let ((interprogram-paste (and (= n 0)
1883 interprogram-paste-function
1884 (funcall interprogram-paste-function))))
1885 (if interprogram-paste
1887 ;; Disable the interprogram cut function when we add the new
1888 ;; text to the kill ring, so Emacs doesn't try to own the
1889 ;; selection, with identical text.
1890 (let ((interprogram-cut-function nil))
1891 (kill-new interprogram-paste))
1893 (or kill-ring (error "Kill ring is empty"))
1894 (let ((ARGth-kill-element
1895 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1899 (setq kill-ring-yank-pointer ARGth-kill-element))
1900 (car ARGth-kill-element)))))
1904 ;;;; Commands for manipulating the kill ring.
1906 (defcustom kill-read-only-ok nil
1907 "*Non-nil means don't signal an error for killing read-only text."
1911 (put 'text-read-only 'error-conditions
1912 '(text-read-only buffer-read-only error))
1913 (put 'text-read-only 'error-message "Text is read-only")
1915 (defun kill-region (beg end &optional yank-handler)
1916 "Kill between point and mark.
1917 The text is deleted but saved in the kill ring.
1918 The command \\[yank] can retrieve it from there.
1919 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
1921 If you want to append the killed region to the last killed text,
1922 use \\[append-next-kill] before \\[kill-region].
1924 If the buffer is read-only, Emacs will beep and refrain from deleting
1925 the text, but put the text in the kill ring anyway. This means that
1926 you can use the killing commands to copy text from a read-only buffer.
1928 This is the primitive for programs to kill text (as opposed to deleting it).
1929 Supply two arguments, character numbers indicating the stretch of text
1931 Any command that calls this function is a \"kill command\".
1932 If the previous command was also a kill command,
1933 the text killed this time appends to the text killed last time
1934 to make one entry in the kill ring.
1936 In Lisp code, optional third arg YANK-HANDLER specifies the yank-handler
1937 text property to be set on the killed text. See `insert-for-yank'."
1940 (let ((string (delete-and-extract-region beg end)))
1941 (when string ;STRING is nil if BEG = END
1942 ;; Add that string to the kill ring, one way or another.
1943 (if (eq last-command 'kill-region)
1944 (kill-append string (< end beg) yank-handler)
1945 (kill-new string nil yank-handler)))
1946 (when (or string (eq last-command 'kill-region))
1947 (setq this-command 'kill-region)))
1948 ((buffer-read-only text-read-only)
1949 ;; The code above failed because the buffer, or some of the characters
1950 ;; in the region, are read-only.
1951 ;; We should beep, in case the user just isn't aware of this.
1952 ;; However, there's no harm in putting
1953 ;; the region's text in the kill ring, anyway.
1954 (copy-region-as-kill beg end)
1955 ;; Set this-command now, so it will be set even if we get an error.
1956 (setq this-command 'kill-region)
1957 ;; This should barf, if appropriate, and give us the correct error.
1958 (if kill-read-only-ok
1959 (message "Read only text copied to kill ring")
1960 ;; Signal an error if the buffer is read-only.
1961 (barf-if-buffer-read-only)
1962 ;; If the buffer isn't read-only, the text is.
1963 (signal 'text-read-only (list (current-buffer)))))))
1965 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1966 ;; to get two copies of the text when the user accidentally types M-w and
1967 ;; then corrects it with the intended C-w.
1968 (defun copy-region-as-kill (beg end)
1969 "Save the region as if killed, but don't kill it.
1970 In Transient Mark mode, deactivate the mark.
1971 If `interprogram-cut-function' is non-nil, also save the text for a window
1972 system cut and paste."
1974 (if (eq last-command 'kill-region)
1975 (kill-append (buffer-substring beg end) (< end beg))
1976 (kill-new (buffer-substring beg end)))
1977 (if transient-mark-mode
1978 (setq deactivate-mark t))
1981 (defun kill-ring-save (beg end)
1982 "Save the region as if killed, but don't kill it.
1983 In Transient Mark mode, deactivate the mark.
1984 If `interprogram-cut-function' is non-nil, also save the text for a window
1985 system cut and paste.
1987 If you want to append the killed line to the last killed text,
1988 use \\[append-next-kill] before \\[kill-ring-save].
1990 This command is similar to `copy-region-as-kill', except that it gives
1991 visual feedback indicating the extent of the region being copied."
1993 (copy-region-as-kill beg end)
1995 (let ((other-end (if (= (point) beg) end beg))
1997 ;; Inhibit quitting so we can make a quit here
1998 ;; look like a C-g typed as a command.
2000 (if (pos-visible-in-window-p other-end (selected-window))
2001 (unless transient-mark-mode
2002 ;; Swap point and mark.
2003 (set-marker (mark-marker) (point) (current-buffer))
2004 (goto-char other-end)
2007 (set-marker (mark-marker) other-end (current-buffer))
2009 ;; If user quit, deactivate the mark
2010 ;; as C-g would as a command.
2011 (and quit-flag mark-active
2013 (let* ((killed-text (current-kill 0))
2014 (message-len (min (length killed-text) 40)))
2016 ;; Don't say "killed"; that is misleading.
2017 (message "Saved text until \"%s\""
2018 (substring killed-text (- message-len)))
2019 (message "Saved text from \"%s\""
2020 (substring killed-text 0 message-len))))))))
2022 (defun append-next-kill (&optional interactive)
2023 "Cause following command, if it kills, to append to previous kill.
2024 The argument is used for internal purposes; do not supply one."
2026 ;; We don't use (interactive-p), since that breaks kbd macros.
2029 (setq this-command 'kill-region)
2030 (message "If the next command is a kill, it will append"))
2031 (setq last-command 'kill-region)))
2035 ;; This is actually used in subr.el but defcustom does not work there.
2036 (defcustom yank-excluded-properties
2037 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2039 "*Text properties to discard when yanking.
2040 The value should be a list of text properties to discard or t,
2041 which means to discard all text properties."
2042 :type '(choice (const :tag "All" t) (repeat symbol))
2046 (defvar yank-window-start nil)
2047 (defvar yank-undo-function nil
2048 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2049 Function is called with two parameters, START and END corresponding to
2050 the value of the mark and point; it is guaranteed that START <= END.
2051 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2053 (defun yank-pop (arg)
2054 "Replace just-yanked stretch of killed text with a different stretch.
2055 This command is allowed only immediately after a `yank' or a `yank-pop'.
2056 At such a time, the region contains a stretch of reinserted
2057 previously-killed text. `yank-pop' deletes that text and inserts in its
2058 place a different stretch of killed text.
2060 With no argument, the previous kill is inserted.
2061 With argument N, insert the Nth previous kill.
2062 If N is negative, this is a more recent kill.
2064 The sequence of kills wraps around, so that after the oldest one
2065 comes the newest one."
2067 (if (not (eq last-command 'yank))
2068 (error "Previous command was not a yank"))
2069 (setq this-command 'yank)
2070 (let ((inhibit-read-only t)
2071 (before (< (point) (mark t))))
2073 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2074 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2075 (setq yank-undo-function nil)
2076 (set-marker (mark-marker) (point) (current-buffer))
2077 (insert-for-yank (current-kill arg))
2078 ;; Set the window start back where it was in the yank command,
2080 (set-window-start (selected-window) yank-window-start t)
2082 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2083 ;; It is cleaner to avoid activation, even though the command
2084 ;; loop would deactivate the mark because we inserted text.
2085 (goto-char (prog1 (mark t)
2086 (set-marker (mark-marker) (point) (current-buffer))))))
2089 (defun yank (&optional arg)
2090 "Reinsert the last stretch of killed text.
2091 More precisely, reinsert the stretch of killed text most recently
2092 killed OR yanked. Put point at end, and set mark at beginning.
2093 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2094 With argument N, reinsert the Nth most recently killed stretch of killed
2096 See also the command \\[yank-pop]."
2098 (setq yank-window-start (window-start))
2099 ;; If we don't get all the way thru, make last-command indicate that
2100 ;; for the following command.
2101 (setq this-command t)
2103 (insert-for-yank (current-kill (cond
2108 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2109 ;; It is cleaner to avoid activation, even though the command
2110 ;; loop would deactivate the mark because we inserted text.
2111 (goto-char (prog1 (mark t)
2112 (set-marker (mark-marker) (point) (current-buffer)))))
2113 ;; If we do get all the way thru, make this-command indicate that.
2114 (if (eq this-command t)
2115 (setq this-command 'yank))
2118 (defun rotate-yank-pointer (arg)
2119 "Rotate the yanking point in the kill ring.
2120 With argument, rotate that many kills forward (or backward, if negative)."
2124 ;; Some kill commands.
2126 ;; Internal subroutine of delete-char
2127 (defun kill-forward-chars (arg)
2128 (if (listp arg) (setq arg (car arg)))
2129 (if (eq arg '-) (setq arg -1))
2130 (kill-region (point) (forward-point arg)))
2132 ;; Internal subroutine of backward-delete-char
2133 (defun kill-backward-chars (arg)
2134 (if (listp arg) (setq arg (car arg)))
2135 (if (eq arg '-) (setq arg -1))
2136 (kill-region (point) (forward-point (- arg))))
2138 (defcustom backward-delete-char-untabify-method 'untabify
2139 "*The method for untabifying when deleting backward.
2140 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2141 `hungry' -- delete all whitespace, both tabs and spaces;
2142 `all' -- delete all whitespace, including tabs, spaces and newlines;
2143 nil -- just delete one character."
2144 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2148 (defun backward-delete-char-untabify (arg &optional killp)
2149 "Delete characters backward, changing tabs into spaces.
2150 The exact behavior depends on `backward-delete-char-untabify-method'.
2151 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2152 Interactively, ARG is the prefix arg (default 1)
2153 and KILLP is t if a prefix arg was specified."
2154 (interactive "*p\nP")
2155 (when (eq backward-delete-char-untabify-method 'untabify)
2158 (while (and (> count 0) (not (bobp)))
2159 (if (= (preceding-char) ?\t)
2160 (let ((col (current-column)))
2162 (setq col (- col (current-column)))
2163 (insert-char ?\ col)
2166 (setq count (1- count))))))
2167 (delete-backward-char
2168 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2169 ((eq backward-delete-char-untabify-method 'all)
2172 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2174 (+ arg (if (zerop wh) 0 (1- wh))))
2178 (defun zap-to-char (arg char)
2179 "Kill up to and including ARG'th occurrence of CHAR.
2180 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2181 Goes backward if ARG is negative; error if CHAR not found."
2182 (interactive "p\ncZap to char: ")
2183 (kill-region (point) (progn
2184 (search-forward (char-to-string char) nil nil arg)
2185 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2188 ;; kill-line and its subroutines.
2190 (defcustom kill-whole-line nil
2191 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2195 (defun kill-line (&optional arg)
2196 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2197 With prefix argument, kill that many lines from point.
2198 Negative arguments kill lines backward.
2199 With zero argument, kills the text before point on the current line.
2201 When calling from a program, nil means \"no arg\",
2202 a number counts as a prefix arg.
2204 To kill a whole line, when point is not at the beginning, type \
2205 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2207 If `kill-whole-line' is non-nil, then this command kills the whole line
2208 including its terminating newline, when used at the beginning of a line
2209 with no argument. As a consequence, you can always kill a whole line
2210 by typing \\[beginning-of-line] \\[kill-line].
2212 If you want to append the killed line to the last killed text,
2213 use \\[append-next-kill] before \\[kill-line].
2215 If the buffer is read-only, Emacs will beep and refrain from deleting
2216 the line, but put the line in the kill ring anyway. This means that
2217 you can use this command to copy text from a read-only buffer.
2218 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2221 (kill-region (point)
2222 ;; It is better to move point to the other end of the kill
2223 ;; before killing. That way, in a read-only buffer, point
2224 ;; moves across the text that is copied to the kill ring.
2225 ;; The choice has no effect on undo now that undo records
2226 ;; the value of point from before the command was run.
2229 (forward-visible-line (prefix-numeric-value arg))
2231 (signal 'end-of-buffer nil))
2234 (end-of-visible-line) (point))))
2235 (if (or (save-excursion
2236 (skip-chars-forward " \t" end)
2238 (and kill-whole-line (bolp)))
2239 (forward-visible-line 1)
2243 (defun kill-whole-line (&optional arg)
2245 With prefix arg, kill that many lines starting from the current line.
2246 If arg is negative, kill backward. Also kill the preceding newline.
2247 \(This is meant to make C-x z work well with negative arguments.\)
2248 If arg is zero, kill current line but exclude the trailing newline."
2250 (setq arg (prefix-numeric-value arg))
2251 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2252 (signal 'end-of-buffer nil))
2253 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2254 (signal 'beginning-of-buffer nil))
2255 (unless (eq last-command 'kill-region)
2257 (setq last-command 'kill-region))
2259 ;; We need to kill in two steps, because the previous command
2260 ;; could have been a kill command, in which case the text
2261 ;; before point needs to be prepended to the current kill
2262 ;; ring entry and the text after point appended. Also, we
2263 ;; need to use save-excursion to avoid copying the same text
2264 ;; twice to the kill ring in read-only buffers.
2266 (kill-region (point) (progn (forward-visible-line 0) (point))))
2267 (kill-region (point) (progn (end-of-visible-line) (point))))
2270 (kill-region (point) (progn (end-of-visible-line) (point))))
2271 (kill-region (point)
2272 (progn (forward-visible-line (1+ arg))
2273 (unless (bobp) (backward-char))
2277 (kill-region (point) (progn (forward-visible-line 0) (point))))
2278 (kill-region (point)
2279 (progn (forward-visible-line arg) (point))))))
2281 (defun forward-visible-line (arg)
2282 "Move forward by ARG lines, ignoring currently invisible newlines only.
2283 If ARG is negative, move backward -ARG lines.
2284 If ARG is zero, move to the beginning of the current line."
2289 (or (zerop (forward-line 1))
2290 (signal 'end-of-buffer nil))
2291 ;; If the newline we just skipped is invisible,
2294 (get-char-property (1- (point)) 'invisible)))
2295 (if (if (eq buffer-invisibility-spec t)
2297 (or (memq prop buffer-invisibility-spec)
2298 (assq prop buffer-invisibility-spec)))
2299 (setq arg (1+ arg))))
2300 (setq arg (1- arg)))
2301 ;; If invisible text follows, and it is a number of complete lines,
2303 (let ((opoint (point)))
2304 (while (and (not (eobp))
2306 (get-char-property (point) 'invisible)))
2307 (if (eq buffer-invisibility-spec t)
2309 (or (memq prop buffer-invisibility-spec)
2310 (assq prop buffer-invisibility-spec)))))
2312 (if (get-text-property (point) 'invisible)
2313 (or (next-single-property-change (point) 'invisible)
2315 (next-overlay-change (point)))))
2317 (goto-char opoint))))
2319 (while (or first (<= arg 0))
2322 (or (zerop (forward-line -1))
2323 (signal 'beginning-of-buffer nil)))
2324 ;; If the newline we just moved to is invisible,
2328 (get-char-property (1- (point)) 'invisible)))
2329 (unless (if (eq buffer-invisibility-spec t)
2331 (or (memq prop buffer-invisibility-spec)
2332 (assq prop buffer-invisibility-spec)))
2333 (setq arg (1+ arg)))))
2335 ;; If invisible text follows, and it is a number of complete lines,
2337 (let ((opoint (point)))
2338 (while (and (not (bobp))
2340 (get-char-property (1- (point)) 'invisible)))
2341 (if (eq buffer-invisibility-spec t)
2343 (or (memq prop buffer-invisibility-spec)
2344 (assq prop buffer-invisibility-spec)))))
2346 (if (get-text-property (1- (point)) 'invisible)
2347 (or (previous-single-property-change (point) 'invisible)
2349 (previous-overlay-change (point)))))
2351 (goto-char opoint)))))
2352 ((beginning-of-buffer end-of-buffer)
2355 (defun end-of-visible-line ()
2356 "Move to end of current visible line."
2358 ;; If the following character is currently invisible,
2359 ;; skip all characters with that same `invisible' property value,
2360 ;; then find the next newline.
2361 (while (and (not (eobp))
2363 (skip-chars-forward "^\n")
2365 (get-char-property (point) 'invisible)))
2366 (if (eq buffer-invisibility-spec t)
2368 (or (memq prop buffer-invisibility-spec)
2369 (assq prop buffer-invisibility-spec))))))
2370 (skip-chars-forward "^\n")
2371 (if (get-text-property (point) 'invisible)
2372 (goto-char (next-single-property-change (point) 'invisible))
2373 (goto-char (next-overlay-change (point))))
2376 (defun insert-buffer (buffer)
2377 "Insert after point the contents of BUFFER.
2378 Puts mark after the inserted text.
2379 BUFFER may be a buffer or a buffer name.
2381 This function is meant for the user to run interactively.
2382 Don't call it from programs: use `insert-buffer-substring' instead!"
2386 (barf-if-buffer-read-only)
2387 (read-buffer "Insert buffer: "
2388 (if (eq (selected-window) (next-window (selected-window)))
2389 (other-buffer (current-buffer))
2390 (window-buffer (next-window (selected-window))))
2394 (insert-buffer-substring (get-buffer buffer))
2398 (defun append-to-buffer (buffer start end)
2399 "Append to specified buffer the text of the region.
2400 It is inserted into that buffer before its point.
2402 When calling from a program, give three arguments:
2403 BUFFER (or buffer name), START and END.
2404 START and END specify the portion of the current buffer to be copied."
2406 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2407 (region-beginning) (region-end)))
2408 (let ((oldbuf (current-buffer)))
2410 (let* ((append-to (get-buffer-create buffer))
2411 (windows (get-buffer-window-list append-to t t))
2413 (set-buffer append-to)
2414 (setq point (point))
2415 (barf-if-buffer-read-only)
2416 (insert-buffer-substring oldbuf start end)
2417 (dolist (window windows)
2418 (when (= (window-point window) point)
2419 (set-window-point window (point))))))))
2421 (defun prepend-to-buffer (buffer start end)
2422 "Prepend to specified buffer the text of the region.
2423 It is inserted into that buffer after its point.
2425 When calling from a program, give three arguments:
2426 BUFFER (or buffer name), START and END.
2427 START and END specify the portion of the current buffer to be copied."
2428 (interactive "BPrepend to buffer: \nr")
2429 (let ((oldbuf (current-buffer)))
2431 (set-buffer (get-buffer-create buffer))
2432 (barf-if-buffer-read-only)
2434 (insert-buffer-substring oldbuf start end)))))
2436 (defun copy-to-buffer (buffer start end)
2437 "Copy to specified buffer the text of the region.
2438 It is inserted into that buffer, replacing existing text there.
2440 When calling from a program, give three arguments:
2441 BUFFER (or buffer name), START and END.
2442 START and END specify the portion of the current buffer to be copied."
2443 (interactive "BCopy to buffer: \nr")
2444 (let ((oldbuf (current-buffer)))
2446 (set-buffer (get-buffer-create buffer))
2447 (barf-if-buffer-read-only)
2450 (insert-buffer-substring oldbuf start end)))))
2452 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2453 (put 'mark-inactive 'error-message "The mark is not active now")
2455 (defun mark (&optional force)
2456 "Return this buffer's mark value as integer; error if mark inactive.
2457 If optional argument FORCE is non-nil, access the mark value
2458 even if the mark is not currently active, and return nil
2459 if there is no mark at all.
2461 If you are using this in an editing command, you are most likely making
2462 a mistake; see the documentation of `set-mark'."
2463 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2464 (marker-position (mark-marker))
2465 (signal 'mark-inactive nil)))
2467 ;; Many places set mark-active directly, and several of them failed to also
2468 ;; run deactivate-mark-hook. This shorthand should simplify.
2469 (defsubst deactivate-mark ()
2470 "Deactivate the mark by setting `mark-active' to nil.
2471 \(That makes a difference only in Transient Mark mode.)
2472 Also runs the hook `deactivate-mark-hook'."
2474 ((eq transient-mark-mode 'lambda)
2475 (setq transient-mark-mode nil))
2476 (transient-mark-mode
2477 (setq mark-active nil)
2478 (run-hooks 'deactivate-mark-hook))))
2480 (defun set-mark (pos)
2481 "Set this buffer's mark to POS. Don't use this function!
2482 That is to say, don't use this function unless you want
2483 the user to see that the mark has moved, and you want the previous
2484 mark position to be lost.
2486 Normally, when a new mark is set, the old one should go on the stack.
2487 This is why most applications should use push-mark, not set-mark.
2489 Novice Emacs Lisp programmers often try to use the mark for the wrong
2490 purposes. The mark saves a location for the user's convenience.
2491 Most editing commands should not alter the mark.
2492 To remember a location for internal use in the Lisp program,
2493 store it in a Lisp variable. Example:
2495 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2499 (setq mark-active t)
2500 (run-hooks 'activate-mark-hook)
2501 (set-marker (mark-marker) pos (current-buffer)))
2502 ;; Normally we never clear mark-active except in Transient Mark mode.
2503 ;; But when we actually clear out the mark value too,
2504 ;; we must clear mark-active in any mode.
2505 (setq mark-active nil)
2506 (run-hooks 'deactivate-mark-hook)
2507 (set-marker (mark-marker) nil)))
2509 (defvar mark-ring nil
2510 "The list of former marks of the current buffer, most recent first.")
2511 (make-variable-buffer-local 'mark-ring)
2512 (put 'mark-ring 'permanent-local t)
2514 (defcustom mark-ring-max 16
2515 "*Maximum size of mark ring. Start discarding off end if gets this big."
2517 :group 'editing-basics)
2519 (defvar global-mark-ring nil
2520 "The list of saved global marks, most recent first.")
2522 (defcustom global-mark-ring-max 16
2523 "*Maximum size of global mark ring. \
2524 Start discarding off end if gets this big."
2526 :group 'editing-basics)
2528 (defun pop-to-mark-command ()
2529 "Jump to mark, and pop a new position for mark off the ring
2530 \(does not affect global mark ring\)."
2533 (error "No mark set in this buffer")
2534 (goto-char (mark t))
2537 (defun push-mark-command (arg &optional nomsg)
2538 "Set mark at where point is.
2539 If no prefix arg and mark is already set there, just activate it.
2540 Display `Mark set' unless the optional second arg NOMSG is non-nil."
2542 (let ((mark (marker-position (mark-marker))))
2543 (if (or arg (null mark) (/= mark (point)))
2544 (push-mark nil nomsg t)
2545 (setq mark-active t)
2547 (message "Mark activated")))))
2549 (defun set-mark-command (arg)
2550 "Set mark at where point is, or jump to mark.
2551 With no prefix argument, set mark, and push old mark position on local
2552 mark ring; also push mark on global mark ring if last mark was set in
2553 another buffer. Immediately repeating the command activates
2554 `transient-mark-mode' temporarily.
2556 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
2557 jump to mark, and pop a new position
2558 for mark off the local mark ring \(this does not affect the global
2559 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
2560 mark ring \(see `pop-global-mark'\).
2562 Repeating the \\[set-mark-command] command without the prefix jumps to
2563 the next position off the local (or global) mark ring.
2565 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
2566 \\[universal-argument] \\[set-mark-command], unconditionally
2567 set mark where point is.
2569 Novice Emacs Lisp programmers often try to use the mark for the wrong
2570 purposes. See the documentation of `set-mark' for more information."
2572 (if (eq transient-mark-mode 'lambda)
2573 (setq transient-mark-mode nil))
2575 ((and (consp arg) (> (prefix-numeric-value arg) 4))
2576 (push-mark-command nil))
2577 ((not (eq this-command 'set-mark-command))
2579 (pop-to-mark-command)
2580 (push-mark-command t)))
2581 ((eq last-command 'pop-to-mark-command)
2582 (setq this-command 'pop-to-mark-command)
2583 (pop-to-mark-command))
2584 ((and (eq last-command 'pop-global-mark) (not arg))
2585 (setq this-command 'pop-global-mark)
2588 (setq this-command 'pop-to-mark-command)
2589 (pop-to-mark-command))
2590 ((and (eq last-command 'set-mark-command)
2591 mark-active (null transient-mark-mode))
2592 (setq transient-mark-mode 'lambda)
2593 (message "Transient-mark-mode temporarily enabled"))
2595 (push-mark-command nil))))
2597 (defun push-mark (&optional location nomsg activate)
2598 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2599 If the last global mark pushed was not in the current buffer,
2600 also push LOCATION on the global mark ring.
2601 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2602 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2604 Novice Emacs Lisp programmers often try to use the mark for the wrong
2605 purposes. See the documentation of `set-mark' for more information.
2607 In Transient Mark mode, this does not activate the mark."
2608 (unless (null (mark t))
2609 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2610 (when (> (length mark-ring) mark-ring-max)
2611 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2612 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
2613 (set-marker (mark-marker) (or location (point)) (current-buffer))
2614 ;; Now push the mark on the global mark ring.
2615 (if (and global-mark-ring
2616 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2617 ;; The last global mark pushed was in this same buffer.
2618 ;; Don't push another one.
2620 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2621 (when (> (length global-mark-ring) global-mark-ring-max)
2622 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
2623 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
2624 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2625 (message "Mark set"))
2626 (if (or activate (not transient-mark-mode))
2627 (set-mark (mark t)))
2631 "Pop off mark ring into the buffer's actual mark.
2632 Does not set point. Does nothing if mark ring is empty."
2634 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2635 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2637 (move-marker (car mark-ring) nil)
2638 (if (null (mark t)) (ding))
2639 (setq mark-ring (cdr mark-ring))))
2641 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2642 (defun exchange-point-and-mark (&optional arg)
2643 "Put the mark where point is now, and point where the mark is now.
2644 This command works even when the mark is not active,
2645 and it reactivates the mark.
2646 With prefix arg, `transient-mark-mode' is enabled temporarily."
2650 (if (null transient-mark-mode)
2651 (setq transient-mark-mode 'lambda))
2654 (let ((omark (mark t)))
2656 (error "No mark set in this buffer"))
2661 (define-minor-mode transient-mark-mode
2662 "Toggle Transient Mark mode.
2663 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2665 In Transient Mark mode, when the mark is active, the region is highlighted.
2666 Changing the buffer \"deactivates\" the mark.
2667 So do certain other operations that set the mark
2668 but whose main purpose is something else--for example,
2669 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
2671 You can also deactivate the mark by typing \\[keyboard-quit] or
2672 \\[keyboard-escape-quit].
2674 Many commands change their behavior when Transient Mark mode is in effect
2675 and the mark is active, by acting on the region instead of their usual
2676 default part of the buffer's text. Examples of such commands include
2677 \\[comment-dwim], \\[flush-lines], \\[ispell], \\[keep-lines],
2678 \\[query-replace], \\[query-replace-regexp], and \\[undo]. Invoke
2679 \\[apropos-documentation] and type \"transient\" or \"mark.*active\" at
2680 the prompt, to see the documentation of commands which are sensitive to
2681 the Transient Mark mode."
2682 :global t :group 'editing-basics :require nil)
2684 (defun pop-global-mark ()
2685 "Pop off global mark ring and jump to the top location."
2687 ;; Pop entries which refer to non-existent buffers.
2688 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2689 (setq global-mark-ring (cdr global-mark-ring)))
2690 (or global-mark-ring
2691 (error "No global mark set"))
2692 (let* ((marker (car global-mark-ring))
2693 (buffer (marker-buffer marker))
2694 (position (marker-position marker)))
2695 (setq global-mark-ring (nconc (cdr global-mark-ring)
2696 (list (car global-mark-ring))))
2698 (or (and (>= position (point-min))
2699 (<= position (point-max)))
2701 (goto-char position)
2702 (switch-to-buffer buffer)))
2704 (defcustom next-line-add-newlines nil
2705 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2708 :group 'editing-basics)
2710 (defun next-line (&optional arg)
2711 "Move cursor vertically down ARG lines.
2712 If there is no character in the target line exactly under the current column,
2713 the cursor is positioned after the character in that line which spans this
2714 column, or at the end of the line if it is not long enough.
2715 If there is no line in the buffer after this one, behavior depends on the
2716 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2717 to create a line, and moves the cursor to that line. Otherwise it moves the
2718 cursor to the end of the buffer.
2720 The command \\[set-goal-column] can be used to create
2721 a semipermanent goal column for this command.
2722 Then instead of trying to move exactly vertically (or as close as possible),
2723 this command moves to the specified goal column (or as close as possible).
2724 The goal column is stored in the variable `goal-column', which is nil
2725 when there is no goal column.
2727 If you are thinking of using this in a Lisp program, consider
2728 using `forward-line' instead. It is usually easier to use
2729 and more reliable (no dependence on goal column, etc.)."
2731 (or arg (setq arg 1))
2732 (if (and next-line-add-newlines (= arg 1))
2733 (if (save-excursion (end-of-line) (eobp))
2734 ;; When adding a newline, don't expand an abbrev.
2735 (let ((abbrev-mode nil))
2742 ((beginning-of-buffer end-of-buffer) (ding)))
2746 (defun previous-line (&optional arg)
2747 "Move cursor vertically up ARG lines.
2748 If there is no character in the target line exactly over the current column,
2749 the cursor is positioned after the character in that line which spans this
2750 column, or at the end of the line if it is not long enough.
2752 The command \\[set-goal-column] can be used to create
2753 a semipermanent goal column for this command.
2754 Then instead of trying to move exactly vertically (or as close as possible),
2755 this command moves to the specified goal column (or as close as possible).
2756 The goal column is stored in the variable `goal-column', which is nil
2757 when there is no goal column.
2759 If you are thinking of using this in a Lisp program, consider using
2760 `forward-line' with a negative argument instead. It is usually easier
2761 to use and more reliable (no dependence on goal column, etc.)."
2763 (or arg (setq arg 1))
2767 ((beginning-of-buffer end-of-buffer) (ding)))
2768 (line-move (- arg)))
2771 (defcustom track-eol nil
2772 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2773 This means moving to the end of each line moved onto.
2774 The beginning of a blank line does not count as the end of a line."
2776 :group 'editing-basics)
2778 (defcustom goal-column nil
2779 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2780 :type '(choice integer
2781 (const :tag "None" nil))
2782 :group 'editing-basics)
2783 (make-variable-buffer-local 'goal-column)
2785 (defvar temporary-goal-column 0
2786 "Current goal column for vertical motion.
2787 It is the column where point was
2788 at the start of current run of vertical motion commands.
2789 When the `track-eol' feature is doing its job, the value is 9999.")
2791 (defcustom line-move-ignore-invisible nil
2792 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2793 Outline mode sets this."
2795 :group 'editing-basics)
2797 (defun line-move-invisible (pos)
2798 "Return non-nil if the character after POS is currently invisible."
2800 (get-char-property pos 'invisible)))
2801 (if (eq buffer-invisibility-spec t)
2803 (or (memq prop buffer-invisibility-spec)
2804 (assq prop buffer-invisibility-spec)))))
2806 ;; This is the guts of next-line and previous-line.
2807 ;; Arg says how many lines to move.
2808 (defun line-move (arg)
2809 ;; Don't run any point-motion hooks, and disregard intangibility,
2810 ;; for intermediate positions.
2811 (let ((inhibit-point-motion-hooks t)
2813 new line-end line-beg)
2816 (if (not (memq last-command '(next-line previous-line)))
2817 (setq temporary-goal-column
2818 (if (and track-eol (eolp)
2819 ;; Don't count beg of empty line as end of line
2820 ;; unless we just did explicit end-of-line.
2821 (or (not (bolp)) (eq last-command 'end-of-line)))
2824 (if (and (not (integerp selective-display))
2825 (not line-move-ignore-invisible))
2826 ;; Use just newline characters.
2827 ;; Set ARG to 0 if we move as many lines as requested.
2829 (progn (if (> arg 1) (forward-line (1- arg)))
2830 ;; This way of moving forward ARG lines
2831 ;; verifies that we have a newline after the last one.
2832 ;; It doesn't get confused by intangible text.
2834 (if (zerop (forward-line 1))
2836 (and (zerop (forward-line arg))
2839 (signal (if (< arg 0)
2840 'beginning-of-buffer
2843 ;; Move by arg lines, but ignore invisible ones.
2845 ;; If the following character is currently invisible,
2846 ;; skip all characters with that same `invisible' property value.
2847 (while (and (not (eobp)) (line-move-invisible (point)))
2848 (goto-char (next-char-property-change (point))))
2851 (and (zerop (vertical-motion 1))
2852 (signal 'end-of-buffer nil))
2853 (setq arg (1- arg)))
2856 (and (zerop (vertical-motion -1))
2857 (signal 'beginning-of-buffer nil))
2859 (while (and (not (bobp)) (line-move-invisible (1- (point))))
2860 (goto-char (previous-char-property-change (point)))))))
2863 ;; If we did not move down as far as desired,
2864 ;; at least go to end of line.
2867 ;; If we did not move down as far as desired,
2868 ;; at least go to end of line.
2869 (beginning-of-line))
2871 (line-move-finish (or goal-column temporary-goal-column) opoint)))))
2874 (defun line-move-finish (column opoint)
2877 ;; Set REPEAT to t to repeat the whole thing.
2881 (line-beg (save-excursion (beginning-of-line) (point)))
2883 ;; Compute the end of the line
2884 ;; ignoring effectively intangible newlines.
2885 (let ((inhibit-point-motion-hooks nil)
2886 (inhibit-field-text-motion t))
2887 (save-excursion (end-of-line) (point)))))
2889 ;; Move to the desired column.
2890 (line-move-to-column column)
2893 ;; Process intangibility within a line.
2894 ;; Move to the chosen destination position from above,
2895 ;; with intangibility processing enabled.
2897 (goto-char (point-min))
2898 (let ((inhibit-point-motion-hooks nil))
2901 ;; If intangibility moves us to a different (later) place
2902 ;; in the same line, use that as the destination.
2903 (if (<= (point) line-end)
2905 ;; If that position is "too late",
2906 ;; try the previous allowable position.
2909 (if (<= (point) line-end)
2911 ;; As a last resort, use the end of the line.
2912 (setq new line-end))))
2914 ;; Now move to the updated destination, processing fields
2915 ;; as well as intangibility.
2917 (let ((inhibit-point-motion-hooks nil))
2919 (constrain-to-field new opoint nil t
2920 'inhibit-line-move-field-capture)))
2922 ;; If all this moved us to a different line,
2923 ;; retry everything within that new line.
2924 (when (or (< (point) line-beg) (> (point) line-end))
2925 ;; Repeat the intangibility and field processing.
2926 (setq repeat t))))))
2928 (defun line-move-to-column (col)
2929 "Try to find column COL, considering invisibility.
2930 This function works only in certain cases,
2931 because what we really need is for `move-to-column'
2932 and `current-column' to be able to ignore invisible text."
2935 (move-to-column col))
2937 (when (and line-move-ignore-invisible
2938 (not (bolp)) (line-move-invisible (1- (point))))
2939 (let ((normal-location (point))
2940 (normal-column (current-column)))
2941 ;; If the following character is currently invisible,
2942 ;; skip all characters with that same `invisible' property value.
2943 (while (and (not (eobp))
2944 (line-move-invisible (point)))
2945 (goto-char (next-char-property-change (point))))
2946 ;; Have we advanced to a larger column position?
2947 (if (> (current-column) normal-column)
2948 ;; We have made some progress towards the desired column.
2949 ;; See if we can make any further progress.
2950 (line-move-to-column (+ (current-column) (- col normal-column)))
2951 ;; Otherwise, go to the place we originally found
2952 ;; and move back over invisible text.
2953 ;; that will get us to the same place on the screen
2954 ;; but with a more reasonable buffer position.
2955 (goto-char normal-location)
2956 (let ((line-beg (save-excursion (beginning-of-line) (point))))
2957 (while (and (not (bolp)) (line-move-invisible (1- (point))))
2958 (goto-char (previous-char-property-change (point) line-beg))))))))
2960 ;;; Many people have said they rarely use this feature, and often type
2961 ;;; it by accident. Maybe it shouldn't even be on a key.
2962 (put 'set-goal-column 'disabled t)
2964 (defun set-goal-column (arg)
2965 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2966 Those commands will move to this position in the line moved to
2967 rather than trying to keep the same horizontal position.
2968 With a non-nil argument, clears out the goal column
2969 so that \\[next-line] and \\[previous-line] resume vertical motion.
2970 The goal column is stored in the variable `goal-column'."
2974 (setq goal-column nil)
2975 (message "No goal column"))
2976 (setq goal-column (current-column))
2977 (message (substitute-command-keys
2978 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2983 (defun scroll-other-window-down (lines)
2984 "Scroll the \"other window\" down.
2985 For more details, see the documentation for `scroll-other-window'."
2987 (scroll-other-window
2988 ;; Just invert the argument's meaning.
2989 ;; We can do that without knowing which window it will be.
2990 (if (eq lines '-) nil
2992 (- (prefix-numeric-value lines))))))
2993 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2995 (defun beginning-of-buffer-other-window (arg)
2996 "Move point to the beginning of the buffer in the other window.
2997 Leave mark at previous position.
2998 With arg N, put point N/10 of the way from the true beginning."
3000 (let ((orig-window (selected-window))
3001 (window (other-window-for-scrolling)))
3002 ;; We use unwind-protect rather than save-window-excursion
3003 ;; because the latter would preserve the things we want to change.
3006 (select-window window)
3007 ;; Set point and mark in that window's buffer.
3008 (beginning-of-buffer arg)
3009 ;; Set point accordingly.
3011 (select-window orig-window))))
3013 (defun end-of-buffer-other-window (arg)
3014 "Move point to the end of the buffer in the other window.
3015 Leave mark at previous position.
3016 With arg N, put point N/10 of the way from the true end."
3018 ;; See beginning-of-buffer-other-window for comments.
3019 (let ((orig-window (selected-window))
3020 (window (other-window-for-scrolling)))
3023 (select-window window)
3026 (select-window orig-window))))
3028 (defun transpose-chars (arg)
3029 "Interchange characters around point, moving forward one character.
3030 With prefix arg ARG, effect is to take character before point
3031 and drag it forward past ARG other characters (backward if ARG negative).
3032 If no argument and at end of line, the previous two chars are exchanged."
3034 (and (null arg) (eolp) (forward-char -1))
3035 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3037 (defun transpose-words (arg)
3038 "Interchange words around point, leaving point at end of them.
3039 With prefix arg ARG, effect is to take word before or around point
3040 and drag it forward past ARG other words (backward if ARG negative).
3041 If ARG is zero, the words around or after point and around or after mark
3043 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3045 (transpose-subr 'forward-word arg))
3047 (defun transpose-sexps (arg)
3048 "Like \\[transpose-words] but applies to sexps.
3049 Does not work on a sexp that point is in the middle of
3050 if it is a list or string."
3054 ;; Here we should try to simulate the behavior of
3055 ;; (cons (progn (forward-sexp x) (point))
3056 ;; (progn (forward-sexp (- x)) (point)))
3057 ;; Except that we don't want to rely on the second forward-sexp
3058 ;; putting us back to where we want to be, since forward-sexp-function
3059 ;; might do funny things like infix-precedence.
3061 (looking-at "\\sw\\|\\s_")
3063 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3064 ;; Jumping over a symbol. We might be inside it, mind you.
3065 (progn (funcall (if (> arg 0)
3066 'skip-syntax-backward 'skip-syntax-forward)
3068 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3069 ;; Otherwise, we're between sexps. Take a step back before jumping
3070 ;; to make sure we'll obey the same precedence no matter which direction
3072 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3073 (cons (save-excursion (forward-sexp arg) (point))
3074 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3075 (not (zerop (funcall (if (> arg 0)
3076 'skip-syntax-forward
3077 'skip-syntax-backward)
3082 (defun transpose-lines (arg)
3083 "Exchange current line and previous line, leaving point after both.
3084 With argument ARG, takes previous line and moves it past ARG lines.
3085 With argument 0, interchanges line point is in with line mark is in."
3087 (transpose-subr (function
3091 ;; Move forward over ARG lines,
3092 ;; but create newlines if necessary.
3093 (setq arg (forward-line arg))
3094 (if (/= (preceding-char) ?\n)
3095 (setq arg (1+ arg)))
3098 (forward-line arg))))
3101 (defun transpose-subr (mover arg &optional special)
3102 (let ((aux (if special mover
3104 (cons (progn (funcall mover x) (point))
3105 (progn (funcall mover (- x)) (point))))))
3110 (setq pos1 (funcall aux 1))
3112 (setq pos2 (funcall aux 1))
3113 (transpose-subr-1 pos1 pos2))
3114 (exchange-point-and-mark))
3116 (setq pos1 (funcall aux -1))
3117 (setq pos2 (funcall aux arg))
3118 (transpose-subr-1 pos1 pos2)
3119 (goto-char (car pos2)))
3121 (setq pos1 (funcall aux -1))
3122 (goto-char (car pos1))
3123 (setq pos2 (funcall aux arg))
3124 (transpose-subr-1 pos1 pos2)))))
3126 (defun transpose-subr-1 (pos1 pos2)
3127 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3128 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3129 (when (> (car pos1) (car pos2))
3131 (setq pos1 pos2 pos2 swap)))
3132 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3133 (atomic-change-group
3135 ;; FIXME: We first delete the two pieces of text, so markers that
3136 ;; used to point to after the text end up pointing to before it :-(
3137 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3138 (goto-char (car pos2))
3139 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3140 (goto-char (car pos1))
3143 (defun backward-word (&optional arg)
3144 "Move backward until encountering the beginning of a word.
3145 With argument, do this that many times."
3147 (forward-word (- (or arg 1))))
3149 (defun mark-word (arg)
3150 "Set mark arg words away from point.
3151 If this command is repeated, it marks the next ARG words after the ones
3154 (cond ((and (eq last-command this-command) (mark t))
3167 (defun kill-word (arg)
3168 "Kill characters forward until encountering the end of a word.
3169 With argument, do this that many times."
3171 (kill-region (point) (progn (forward-word arg) (point))))
3173 (defun backward-kill-word (arg)
3174 "Kill characters backward until encountering the end of a word.
3175 With argument, do this that many times."
3177 (kill-word (- arg)))
3179 (defun current-word (&optional strict really-word)
3180 "Return the symbol or word that point is on (or a nearby one) as a string.
3181 The return value includes no text properties.
3182 If optional arg STRICT is non-nil, return nil unless point is within
3183 or adjacent to a symbol or word.
3184 The function, belying its name, normally finds a symbol.
3185 If optional arg REALLY-WORD is non-nil, it finds just a word."
3187 (let* ((oldpoint (point)) (start (point)) (end (point))
3188 (syntaxes (if really-word "w" "w_"))
3189 (not-syntaxes (concat "^" syntaxes)))
3190 (skip-syntax-backward syntaxes) (setq start (point))
3191 (goto-char oldpoint)
3192 (skip-syntax-forward syntaxes) (setq end (point))
3193 (when (and (eq start oldpoint) (eq end oldpoint)
3194 ;; Point is neither within nor adjacent to a word.
3196 ;; Look for preceding word in same line.
3197 (skip-syntax-backward not-syntaxes
3198 (save-excursion (beginning-of-line)
3201 ;; No preceding word in same line.
3202 ;; Look for following word in same line.
3204 (skip-syntax-forward not-syntaxes
3205 (save-excursion (end-of-line)
3207 (setq start (point))
3208 (skip-syntax-forward syntaxes)
3211 (skip-syntax-backward syntaxes)
3212 (setq start (point))))
3213 ;; If we found something nonempty, return it as a string.
3214 (unless (= start end)
3215 (buffer-substring-no-properties start end)))))
3217 (defcustom fill-prefix nil
3218 "*String for filling to insert at front of new line, or nil for none."
3219 :type '(choice (const :tag "None" nil)
3222 (make-variable-buffer-local 'fill-prefix)
3224 (defcustom auto-fill-inhibit-regexp nil
3225 "*Regexp to match lines which should not be auto-filled."
3226 :type '(choice (const :tag "None" nil)
3230 (defvar comment-line-break-function 'comment-indent-new-line
3231 "*Mode-specific function which line breaks and continues a comment.
3233 This function is only called during auto-filling of a comment section.
3234 The function should take a single optional argument, which is a flag
3235 indicating whether it should use soft newlines.
3237 Setting this variable automatically makes it local to the current buffer.")
3239 ;; This function is used as the auto-fill-function of a buffer
3240 ;; when Auto-Fill mode is enabled.
3241 ;; It returns t if it really did any work.
3242 ;; (Actually some major modes use a different auto-fill function,
3243 ;; but this one is the default one.)
3244 (defun do-auto-fill ()
3245 (let (fc justify bol give-up
3246 (fill-prefix fill-prefix))
3247 (if (or (not (setq justify (current-justification)))
3248 (null (setq fc (current-fill-column)))
3249 (and (eq justify 'left)
3250 (<= (current-column) fc))
3251 (save-excursion (beginning-of-line)
3253 (and auto-fill-inhibit-regexp
3254 (looking-at auto-fill-inhibit-regexp))))
3255 nil ;; Auto-filling not required
3256 (if (memq justify '(full center right))
3257 (save-excursion (unjustify-current-line)))
3259 ;; Choose a fill-prefix automatically.
3260 (when (and adaptive-fill-mode
3261 (or (null fill-prefix) (string= fill-prefix "")))
3263 (fill-context-prefix
3264 (save-excursion (backward-paragraph 1) (point))
3265 (save-excursion (forward-paragraph 1) (point)))))
3266 (and prefix (not (equal prefix ""))
3267 ;; Use auto-indentation rather than a guessed empty prefix.
3268 (not (and fill-indent-according-to-mode
3269 (string-match "\\`[ \t]*\\'" prefix)))
3270 (setq fill-prefix prefix))))
3272 (while (and (not give-up) (> (current-column) fc))
3273 ;; Determine where to split the line.
3276 (let ((opoint (point)))
3279 (setq after-prefix (point))
3281 (looking-at (regexp-quote fill-prefix))
3282 (setq after-prefix (match-end 0)))
3283 (move-to-column (1+ fc))
3284 (fill-move-to-break-point after-prefix)
3287 ;; See whether the place we found is any good.
3289 (goto-char fill-point)
3291 ;; There is no use breaking at end of line.
3292 (save-excursion (skip-chars-forward " ") (eolp))
3293 ;; It is futile to split at the end of the prefix
3294 ;; since we would just insert the prefix again.
3295 (and after-prefix (<= (point) after-prefix))
3296 ;; Don't split right after a comment starter
3297 ;; since we would just make another comment starter.
3298 (and comment-start-skip
3299 (let ((limit (point)))
3301 (and (re-search-forward comment-start-skip
3303 (eq (point) limit))))))
3304 ;; No good place to break => stop trying.
3306 ;; Ok, we have a useful place to break the line. Do it.
3307 (let ((prev-column (current-column)))
3308 ;; If point is at the fill-point, do not `save-excursion'.
3309 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3310 ;; point will end up before it rather than after it.
3312 (skip-chars-backward " \t")
3313 (= (point) fill-point))
3314 (funcall comment-line-break-function t)
3316 (goto-char fill-point)
3317 (funcall comment-line-break-function t)))
3318 ;; Now do justification, if required
3319 (if (not (eq justify 'left))
3322 (justify-current-line justify nil t)))
3323 ;; If making the new line didn't reduce the hpos of
3324 ;; the end of the line, then give up now;
3325 ;; trying again will not help.
3326 (if (>= (current-column) prev-column)
3327 (setq give-up t))))))
3328 ;; Justify last line.
3329 (justify-current-line justify t t)
3332 (defvar normal-auto-fill-function 'do-auto-fill
3333 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3334 Some major modes set this.")
3336 ;; FIXME: turn into a proper minor mode.
3337 ;; Add a global minor mode version of it.
3338 (defun auto-fill-mode (&optional arg)
3339 "Toggle Auto Fill mode.
3340 With arg, turn Auto Fill mode on if and only if arg is positive.
3341 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3342 automatically breaks the line at a previous space.
3344 The value of `normal-auto-fill-function' specifies the function to use
3345 for `auto-fill-function' when turning Auto Fill mode on."
3347 (prog1 (setq auto-fill-function
3349 (not auto-fill-function)
3350 (> (prefix-numeric-value arg) 0))
3351 normal-auto-fill-function
3353 (force-mode-line-update)))
3355 ;; This holds a document string used to document auto-fill-mode.
3356 (defun auto-fill-function ()
3357 "Automatically break line at a previous space, in insertion of text."
3360 (defun turn-on-auto-fill ()
3361 "Unconditionally turn on Auto Fill mode."
3364 (defun turn-off-auto-fill ()
3365 "Unconditionally turn off Auto Fill mode."
3366 (auto-fill-mode -1))
3368 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3370 (defun set-fill-column (arg)
3371 "Set `fill-column' to specified argument.
3372 Use \\[universal-argument] followed by a number to specify a column.
3373 Just \\[universal-argument] as argument means to use the current column."
3376 (setq arg (current-column)))
3377 (if (not (integerp arg))
3378 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3379 (error "Set-fill-column requires an explicit argument")
3380 (message "Fill column set to %d (was %d)" arg fill-column)
3381 (setq fill-column arg)))
3383 (defun set-selective-display (arg)
3384 "Set `selective-display' to ARG; clear it if no arg.
3385 When the value of `selective-display' is a number > 0,
3386 lines whose indentation is >= that value are not displayed.
3387 The variable `selective-display' has a separate value for each buffer."
3389 (if (eq selective-display t)
3390 (error "selective-display already in use for marked lines"))
3393 (narrow-to-region (point-min) (point))
3394 (goto-char (window-start))
3395 (vertical-motion (window-height)))))
3396 (setq selective-display
3397 (and arg (prefix-numeric-value arg)))
3398 (recenter current-vpos))
3399 (set-window-start (selected-window) (window-start (selected-window)))
3400 (princ "selective-display set to " t)
3401 (prin1 selective-display t)
3404 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
3405 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
3407 (defun toggle-truncate-lines (arg)
3408 "Toggle whether to fold or truncate long lines on the screen.
3409 With arg, truncate long lines iff arg is positive.
3410 Note that in side-by-side windows, truncation is always enabled."
3412 (setq truncate-lines
3414 (not truncate-lines)
3415 (> (prefix-numeric-value arg) 0)))
3416 (force-mode-line-update)
3417 (unless truncate-lines
3418 (let ((buffer (current-buffer)))
3419 (walk-windows (lambda (window)
3420 (if (eq buffer (window-buffer window))
3421 (set-window-hscroll window 0)))
3423 (message "Truncate long lines %s"
3424 (if truncate-lines "enabled" "disabled")))
3426 (defvar overwrite-mode-textual " Ovwrt"
3427 "The string displayed in the mode line when in overwrite mode.")
3428 (defvar overwrite-mode-binary " Bin Ovwrt"
3429 "The string displayed in the mode line when in binary overwrite mode.")
3431 (defun overwrite-mode (arg)
3432 "Toggle overwrite mode.
3433 With arg, turn overwrite mode on iff arg is positive.
3434 In overwrite mode, printing characters typed in replace existing text
3435 on a one-for-one basis, rather than pushing it to the right. At the
3436 end of a line, such characters extend the line. Before a tab,
3437 such characters insert until the tab is filled in.
3438 \\[quoted-insert] still inserts characters in overwrite mode; this
3439 is supposed to make it easier to insert characters when necessary."
3441 (setq overwrite-mode
3442 (if (if (null arg) (not overwrite-mode)
3443 (> (prefix-numeric-value arg) 0))
3444 'overwrite-mode-textual))
3445 (force-mode-line-update))
3447 (defun binary-overwrite-mode (arg)
3448 "Toggle binary overwrite mode.
3449 With arg, turn binary overwrite mode on iff arg is positive.
3450 In binary overwrite mode, printing characters typed in replace
3451 existing text. Newlines are not treated specially, so typing at the
3452 end of a line joins the line to the next, with the typed character
3453 between them. Typing before a tab character simply replaces the tab
3454 with the character typed.
3455 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3456 typing characters do.
3458 Note that binary overwrite mode is not its own minor mode; it is a
3459 specialization of overwrite-mode, entered by setting the
3460 `overwrite-mode' variable to `overwrite-mode-binary'."
3462 (setq overwrite-mode
3464 (not (eq overwrite-mode 'overwrite-mode-binary))
3465 (> (prefix-numeric-value arg) 0))
3466 'overwrite-mode-binary))
3467 (force-mode-line-update))
3469 (define-minor-mode line-number-mode
3470 "Toggle Line Number mode.
3471 With arg, turn Line Number mode on iff arg is positive.
3472 When Line Number mode is enabled, the line number appears
3475 Line numbers do not appear for very large buffers and buffers
3476 with very long lines; see variables `line-number-display-limit'
3477 and `line-number-display-limit-width'."
3478 :init-value t :global t :group 'editing-basics :require nil)
3480 (define-minor-mode column-number-mode
3481 "Toggle Column Number mode.
3482 With arg, turn Column Number mode on iff arg is positive.
3483 When Column Number mode is enabled, the column number appears
3485 :global t :group 'editing-basics :require nil)
3487 (define-minor-mode size-indication-mode
3488 "Toggle Size Indication mode.
3489 With arg, turn Size Indication mode on iff arg is positive. When
3490 Size Indication mode is enabled, the size of the accessible part
3491 of the buffer appears in the mode line."
3492 :global t :group 'editing-basics :require nil)
3494 (defgroup paren-blinking nil
3495 "Blinking matching of parens and expressions."
3496 :prefix "blink-matching-"
3497 :group 'paren-matching)
3499 (defcustom blink-matching-paren t
3500 "*Non-nil means show matching open-paren when close-paren is inserted."
3502 :group 'paren-blinking)
3504 (defcustom blink-matching-paren-on-screen t
3505 "*Non-nil means show matching open-paren when it is on screen.
3506 If nil, means don't show it (but the open-paren can still be shown
3507 when it is off screen)."
3509 :group 'paren-blinking)
3511 (defcustom blink-matching-paren-distance (* 25 1024)
3512 "*If non-nil, is maximum distance to search for matching open-paren."
3514 :group 'paren-blinking)
3516 (defcustom blink-matching-delay 1
3517 "*Time in seconds to delay after showing a matching paren."
3519 :group 'paren-blinking)
3521 (defcustom blink-matching-paren-dont-ignore-comments nil
3522 "*Non-nil means `blink-matching-paren' will not ignore comments."
3524 :group 'paren-blinking)
3526 (defun blink-matching-open ()
3527 "Move cursor momentarily to the beginning of the sexp before point."
3529 (and (> (point) (1+ (point-min)))
3530 blink-matching-paren
3531 ;; Verify an even number of quoting characters precede the close.
3532 (= 1 (logand 1 (- (point)
3535 (skip-syntax-backward "/\\")
3537 (let* ((oldpos (point))
3543 (if blink-matching-paren-distance
3544 (narrow-to-region (max (point-min)
3545 (- (point) blink-matching-paren-distance))
3548 (let ((parse-sexp-ignore-comments
3549 (and parse-sexp-ignore-comments
3550 (not blink-matching-paren-dont-ignore-comments))))
3551 (setq blinkpos (scan-sexps oldpos -1)))
3555 (goto-char blinkpos)
3556 (not (looking-at "\\s$")))
3557 (setq matching-paren
3558 (or (and parse-sexp-lookup-properties
3559 (let ((prop (get-text-property blinkpos 'syntax-table)))
3563 (matching-paren (char-after blinkpos)))
3565 (or (null matching-paren)
3566 (/= (char-after (1- oldpos))
3568 (if mismatch (setq blinkpos nil))
3570 ;; Don't log messages about paren matching.
3571 (let (message-log-max)
3572 (goto-char blinkpos)
3573 (if (pos-visible-in-window-p)
3574 (and blink-matching-paren-on-screen
3575 (sit-for blink-matching-delay))
3576 (goto-char blinkpos)
3579 ;; Show what precedes the open in its line, if anything.
3581 (skip-chars-backward " \t")
3583 (buffer-substring (progn (beginning-of-line) (point))
3585 ;; Show what follows the open in its line, if anything.
3588 (skip-chars-forward " \t")
3590 (buffer-substring blinkpos
3591 (progn (end-of-line) (point)))
3592 ;; Otherwise show the previous nonblank line,
3595 (skip-chars-backward "\n \t")
3598 (buffer-substring (progn
3599 (skip-chars-backward "\n \t")
3602 (progn (end-of-line)
3603 (skip-chars-backward " \t")
3605 ;; Replace the newline and other whitespace with `...'.
3607 (buffer-substring blinkpos (1+ blinkpos)))
3608 ;; There is nothing to show except the char itself.
3609 (buffer-substring blinkpos (1+ blinkpos))))))))
3611 (message "Mismatched parentheses"))
3612 ((not blink-matching-paren-distance)
3613 (message "Unmatched parenthesis"))))))))
3615 ;Turned off because it makes dbx bomb out.
3616 (setq blink-paren-function 'blink-matching-open)
3618 ;; This executes C-g typed while Emacs is waiting for a command.
3619 ;; Quitting out of a program does not go through here;
3620 ;; that happens in the QUIT macro at the C code level.
3621 (defun keyboard-quit ()
3622 "Signal a `quit' condition.
3623 During execution of Lisp code, this character causes a quit directly.
3624 At top-level, as an editor command, this simply beeps."
3627 (setq defining-kbd-macro nil)
3630 (define-key global-map "\C-g" 'keyboard-quit)
3632 (defvar buffer-quit-function nil
3633 "Function to call to \"quit\" the current buffer, or nil if none.
3634 \\[keyboard-escape-quit] calls this function when its more local actions
3635 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3637 (defun keyboard-escape-quit ()
3638 "Exit the current \"mode\" (in a generalized sense of the word).
3639 This command can exit an interactive command such as `query-replace',
3640 can clear out a prefix argument or a region,
3641 can get out of the minibuffer or other recursive edit,
3642 cancel the use of the current buffer (for special-purpose buffers),
3643 or go back to just one window (by deleting all but the selected window)."
3645 (cond ((eq last-command 'mode-exited) nil)
3646 ((> (minibuffer-depth) 0)
3647 (abort-recursive-edit))
3650 ((and transient-mark-mode
3653 ((> (recursion-depth) 0)
3654 (exit-recursive-edit))
3655 (buffer-quit-function
3656 (funcall buffer-quit-function))
3657 ((not (one-window-p t))
3658 (delete-other-windows))
3659 ((string-match "^ \\*" (buffer-name (current-buffer)))
3662 (defun play-sound-file (file &optional volume device)
3663 "Play sound stored in FILE.
3664 VOLUME and DEVICE correspond to the keywords of the sound
3665 specification for `play-sound'."
3666 (interactive "fPlay sound file: ")
3667 (let ((sound (list :file file)))
3669 (plist-put sound :volume volume))
3671 (plist-put sound :device device))
3673 (play-sound sound)))
3675 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3677 (defcustom read-mail-command 'rmail
3678 "*Your preference for a mail reading package.
3679 This is used by some keybindings which support reading mail.
3680 See also `mail-user-agent' concerning sending mail."
3681 :type '(choice (function-item rmail)
3682 (function-item gnus)
3683 (function-item mh-rmail)
3684 (function :tag "Other"))
3688 (defcustom mail-user-agent 'sendmail-user-agent
3689 "*Your preference for a mail composition package.
3690 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3691 outgoing email message. This variable lets you specify which
3692 mail-sending package you prefer.
3694 Valid values include:
3696 `sendmail-user-agent' -- use the default Emacs Mail package.
3697 See Info node `(emacs)Sending Mail'.
3698 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3699 See Info node `(mh-e)'.
3700 `message-user-agent' -- use the Gnus Message package.
3701 See Info node `(message)'.
3702 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3703 paraphernalia, particularly the Gcc: header for
3706 Additional valid symbols may be available; check with the author of
3707 your package for details. The function should return non-nil if it
3710 See also `read-mail-command' concerning reading mail."
3711 :type '(radio (function-item :tag "Default Emacs mail"
3713 sendmail-user-agent)
3714 (function-item :tag "Emacs interface to MH"
3717 (function-item :tag "Gnus Message package"
3720 (function-item :tag "Gnus Message with full Gnus features"
3723 (function :tag "Other"))
3726 (define-mail-user-agent 'sendmail-user-agent
3727 'sendmail-user-agent-compose
3728 'mail-send-and-exit)
3730 (defun rfc822-goto-eoh ()
3731 ;; Go to header delimiter line in a mail message, following RFC822 rules
3732 (goto-char (point-min))
3733 (when (re-search-forward
3734 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
3735 (goto-char (match-beginning 0))))
3737 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3738 switch-function yank-action
3741 (let ((special-display-buffer-names nil)
3742 (special-display-regexps nil)
3743 (same-window-buffer-names nil)
3744 (same-window-regexps nil))
3745 (funcall switch-function "*mail*")))
3746 (let ((cc (cdr (assoc-string "cc" other-headers t)))
3747 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
3748 (body (cdr (assoc-string "body" other-headers t))))
3749 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3751 (error "Message aborted"))
3754 (while other-headers
3755 (unless (member-ignore-case (car (car other-headers))
3756 '("in-reply-to" "cc" "body"))
3757 (insert (car (car other-headers)) ": "
3758 (cdr (car other-headers)) "\n"))
3759 (setq other-headers (cdr other-headers)))
3765 (define-mail-user-agent 'mh-e-user-agent
3766 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3767 'mh-before-send-letter-hook)
3769 (defun compose-mail (&optional to subject other-headers continue
3770 switch-function yank-action send-actions)
3771 "Start composing a mail message to send.
3772 This uses the user's chosen mail composition package
3773 as selected with the variable `mail-user-agent'.
3774 The optional arguments TO and SUBJECT specify recipients
3775 and the initial Subject field, respectively.
3777 OTHER-HEADERS is an alist specifying additional
3778 header fields. Elements look like (HEADER . VALUE) where both
3779 HEADER and VALUE are strings.
3781 CONTINUE, if non-nil, says to continue editing a message already
3784 SWITCH-FUNCTION, if non-nil, is a function to use to
3785 switch to and display the buffer used for mail composition.
3787 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3788 to insert the raw text of the message being replied to.
3789 It has the form (FUNCTION . ARGS). The user agent will apply
3790 FUNCTION to ARGS, to insert the raw text of the original message.
3791 \(The user agent will also run `mail-citation-hook', *after* the
3792 original text has been inserted in this way.)
3794 SEND-ACTIONS is a list of actions to call when the message is sent.
3795 Each action has the form (FUNCTION . ARGS)."
3797 (list nil nil nil current-prefix-arg))
3798 (let ((function (get mail-user-agent 'composefunc)))
3799 (funcall function to subject other-headers continue
3800 switch-function yank-action send-actions)))
3802 (defun compose-mail-other-window (&optional to subject other-headers continue
3803 yank-action send-actions)
3804 "Like \\[compose-mail], but edit the outgoing message in another window."
3806 (list nil nil nil current-prefix-arg))
3807 (compose-mail to subject other-headers continue
3808 'switch-to-buffer-other-window yank-action send-actions))
3811 (defun compose-mail-other-frame (&optional to subject other-headers continue
3812 yank-action send-actions)
3813 "Like \\[compose-mail], but edit the outgoing message in another frame."
3815 (list nil nil nil current-prefix-arg))
3816 (compose-mail to subject other-headers continue
3817 'switch-to-buffer-other-frame yank-action send-actions))
3819 (defvar set-variable-value-history nil
3820 "History of values entered with `set-variable'.")
3822 (defun set-variable (var val &optional make-local)
3823 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3824 When using this interactively, enter a Lisp object for VALUE.
3825 If you want VALUE to be a string, you must surround it with doublequotes.
3826 VALUE is used literally, not evaluated.
3828 If VARIABLE has a `variable-interactive' property, that is used as if
3829 it were the arg to `interactive' (which see) to interactively read VALUE.
3831 If VARIABLE has been defined with `defcustom', then the type information
3832 in the definition is used to check that VALUE is valid.
3834 With a prefix argument, set VARIABLE to VALUE buffer-locally."
3836 (let* ((default-var (variable-at-point))
3837 (var (if (symbolp default-var)
3838 (read-variable (format "Set variable (default %s): " default-var)
3840 (read-variable "Set variable: ")))
3841 (minibuffer-help-form '(describe-variable var))
3842 (prop (get var 'variable-interactive))
3843 (prompt (format "Set %s%s to value: " var
3844 (cond ((local-variable-p var)
3846 ((or current-prefix-arg
3847 (local-variable-if-set-p var))
3851 ;; Use VAR's `variable-interactive' property
3852 ;; as an interactive spec for prompting.
3853 (call-interactively `(lambda (arg)
3857 (read-string prompt nil
3858 'set-variable-value-history)))))
3859 (list var val current-prefix-arg)))
3861 (and (custom-variable-p var)
3862 (not (get var 'custom-type))
3863 (custom-load-symbol var))
3864 (let ((type (get var 'custom-type)))
3866 ;; Match with custom type.
3868 (setq type (widget-convert type))
3869 (unless (widget-apply type :match val)
3870 (error "Value `%S' does not match type %S of %S"
3871 val (car type) var))))
3874 (make-local-variable var))
3878 ;; Force a thorough redisplay for the case that the variable
3879 ;; has an effect on the display, like `tab-width' has.
3880 (force-mode-line-update))
3882 ;; Define the major mode for lists of completions.
3884 (defvar completion-list-mode-map nil
3885 "Local map for completion list buffers.")
3886 (or completion-list-mode-map
3887 (let ((map (make-sparse-keymap)))
3888 (define-key map [mouse-2] 'mouse-choose-completion)
3889 (define-key map [down-mouse-2] nil)
3890 (define-key map "\C-m" 'choose-completion)
3891 (define-key map "\e\e\e" 'delete-completion-window)
3892 (define-key map [left] 'previous-completion)
3893 (define-key map [right] 'next-completion)
3894 (setq completion-list-mode-map map)))
3896 ;; Completion mode is suitable only for specially formatted data.
3897 (put 'completion-list-mode 'mode-class 'special)
3899 (defvar completion-reference-buffer nil
3900 "Record the buffer that was current when the completion list was requested.
3901 This is a local variable in the completion list buffer.
3902 Initial value is nil to avoid some compiler warnings.")
3904 (defvar completion-no-auto-exit nil
3905 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3906 This also applies to other functions such as `choose-completion'
3907 and `mouse-choose-completion'.")
3909 (defvar completion-base-size nil
3910 "Number of chars at beginning of minibuffer not involved in completion.
3911 This is a local variable in the completion list buffer
3912 but it talks about the buffer in `completion-reference-buffer'.
3913 If this is nil, it means to compare text to determine which part
3914 of the tail end of the buffer's text is involved in completion.")
3916 (defun delete-completion-window ()
3917 "Delete the completion list window.
3918 Go to the window from which completion was requested."
3920 (let ((buf completion-reference-buffer))
3921 (if (one-window-p t)
3922 (if (window-dedicated-p (selected-window))
3923 (delete-frame (selected-frame)))
3924 (delete-window (selected-window))
3925 (if (get-buffer-window buf)
3926 (select-window (get-buffer-window buf))))))
3928 (defun previous-completion (n)
3929 "Move to the previous item in the completion list."
3931 (next-completion (- n)))
3933 (defun next-completion (n)
3934 "Move to the next item in the completion list.
3935 With prefix argument N, move N items (negative N means move backward)."
3937 (let ((beg (point-min)) (end (point-max)))
3938 (while (and (> n 0) (not (eobp)))
3939 ;; If in a completion, move to the end of it.
3940 (when (get-text-property (point) 'mouse-face)
3941 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3942 ;; Move to start of next one.
3943 (unless (get-text-property (point) 'mouse-face)
3944 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3946 (while (and (< n 0) (not (bobp)))
3947 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
3948 ;; If in a completion, move to the start of it.
3949 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
3950 (goto-char (previous-single-property-change
3951 (point) 'mouse-face nil beg)))
3952 ;; Move to end of the previous completion.
3953 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
3954 (goto-char (previous-single-property-change
3955 (point) 'mouse-face nil beg)))
3956 ;; Move to the start of that one.
3957 (goto-char (previous-single-property-change
3958 (point) 'mouse-face nil beg))
3961 (defun choose-completion ()
3962 "Choose the completion that point is in or next to."
3964 (let (beg end completion (buffer completion-reference-buffer)
3965 (base-size completion-base-size))
3966 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3967 (setq end (point) beg (1+ (point))))
3968 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3969 (setq end (1- (point)) beg (point)))
3971 (error "No completion here"))
3972 (setq beg (previous-single-property-change beg 'mouse-face))
3973 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3974 (setq completion (buffer-substring beg end))
3975 (let ((owindow (selected-window)))
3976 (if (and (one-window-p t 'selected-frame)
3977 (window-dedicated-p (selected-window)))
3978 ;; This is a special buffer's frame
3979 (iconify-frame (selected-frame))
3980 (or (window-dedicated-p (selected-window))
3982 (select-window owindow))
3983 (choose-completion-string completion buffer base-size)))
3985 ;; Delete the longest partial match for STRING
3986 ;; that can be found before POINT.
3987 (defun choose-completion-delete-max-match (string)
3988 (let ((opoint (point))
3990 ;; Try moving back by the length of the string.
3991 (goto-char (max (- (point) (length string))
3992 (minibuffer-prompt-end)))
3993 ;; See how far back we were actually able to move. That is the
3994 ;; upper bound on how much we can match and delete.
3995 (setq len (- opoint (point)))
3996 (if completion-ignore-case
3997 (setq string (downcase string)))
3998 (while (and (> len 0)
3999 (let ((tail (buffer-substring (point) opoint)))
4000 (if completion-ignore-case
4001 (setq tail (downcase tail)))
4002 (not (string= tail (substring string 0 len)))))
4007 (defvar choose-completion-string-functions nil
4008 "Functions that may override the normal insertion of a completion choice.
4009 These functions are called in order with four arguments:
4010 CHOICE - the string to insert in the buffer,
4011 BUFFER - the buffer in which the choice should be inserted,
4012 MINI-P - non-nil iff BUFFER is a minibuffer, and
4013 BASE-SIZE - the number of characters in BUFFER before
4014 the string being completed.
4016 If a function in the list returns non-nil, that function is supposed
4017 to have inserted the CHOICE in the BUFFER, and possibly exited
4018 the minibuffer; no further functions will be called.
4020 If all functions in the list return nil, that means to use
4021 the default method of inserting the completion in BUFFER.")
4023 (defun choose-completion-string (choice &optional buffer base-size)
4024 "Switch to BUFFER and insert the completion choice CHOICE.
4025 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4026 to keep. If it is nil, we call `choose-completion-delete-max-match'
4027 to decide what to delete."
4029 ;; If BUFFER is the minibuffer, exit the minibuffer
4030 ;; unless it is reading a file name and CHOICE is a directory,
4031 ;; or completion-no-auto-exit is non-nil.
4033 (let* ((buffer (or buffer completion-reference-buffer))
4034 (mini-p (minibufferp buffer)))
4035 ;; If BUFFER is a minibuffer, barf unless it's the currently
4036 ;; active minibuffer.
4038 (or (not (active-minibuffer-window))
4040 (window-buffer (active-minibuffer-window))))))
4041 (error "Minibuffer is not active for completion")
4042 (unless (run-hook-with-args-until-success
4043 'choose-completion-string-functions
4044 choice buffer mini-p base-size)
4045 ;; Insert the completion into the buffer where it was requested.
4048 (delete-region (+ base-size (if mini-p
4049 (minibuffer-prompt-end)
4052 (choose-completion-delete-max-match choice))
4054 (remove-text-properties (- (point) (length choice)) (point)
4056 ;; Update point in the window that BUFFER is showing in.
4057 (let ((window (get-buffer-window buffer t)))
4058 (set-window-point window (point)))
4059 ;; If completing for the minibuffer, exit it with this choice.
4060 (and (not completion-no-auto-exit)
4061 (equal buffer (window-buffer (minibuffer-window)))
4062 minibuffer-completion-table
4063 ;; If this is reading a file name, and the file name chosen
4064 ;; is a directory, don't exit the minibuffer.
4065 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4066 (file-directory-p (field-string (point-max))))
4067 (let ((mini (active-minibuffer-window)))
4068 (select-window mini)
4069 (when minibuffer-auto-raise
4070 (raise-frame (window-frame mini))))
4071 (exit-minibuffer)))))))
4073 (defun completion-list-mode ()
4074 "Major mode for buffers showing lists of possible completions.
4075 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4076 to select the completion near point.
4077 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4080 (kill-all-local-variables)
4081 (use-local-map completion-list-mode-map)
4082 (setq mode-name "Completion List")
4083 (setq major-mode 'completion-list-mode)
4084 (make-local-variable 'completion-base-size)
4085 (setq completion-base-size nil)
4086 (run-hooks 'completion-list-mode-hook))
4088 (defun completion-list-mode-finish ()
4089 "Finish setup of the completions buffer.
4090 Called from `temp-buffer-show-hook'."
4091 (when (eq major-mode 'completion-list-mode)
4092 (toggle-read-only 1)))
4094 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4096 (defvar completion-setup-hook nil
4097 "Normal hook run at the end of setting up a completion list buffer.
4098 When this hook is run, the current buffer is the one in which the
4099 command to display the completion list buffer was run.
4100 The completion list buffer is available as the value of `standard-output'.")
4102 ;; This function goes in completion-setup-hook, so that it is called
4103 ;; after the text of the completion list buffer is written.
4105 (defun completion-setup-function ()
4107 (let ((mainbuf (current-buffer))
4108 (mbuf-contents (minibuffer-contents)))
4109 ;; When reading a file name in the minibuffer,
4110 ;; set default-directory in the minibuffer
4111 ;; so it will get copied into the completion list buffer.
4112 (if minibuffer-completing-file-name
4113 (with-current-buffer mainbuf
4114 (setq default-directory (file-name-directory mbuf-contents))))
4115 (set-buffer standard-output)
4116 (completion-list-mode)
4117 (make-local-variable 'completion-reference-buffer)
4118 (setq completion-reference-buffer mainbuf)
4119 (if minibuffer-completing-file-name
4120 ;; For file name completion,
4121 ;; use the number of chars before the start of the
4122 ;; last file name component.
4123 (setq completion-base-size
4125 (set-buffer mainbuf)
4126 (goto-char (point-max))
4127 (skip-chars-backward "^/")
4128 (- (point) (minibuffer-prompt-end))))
4129 ;; Otherwise, in minibuffer, the whole input is being completed.
4131 (if (minibufferp mainbuf)
4132 (setq completion-base-size 0))))
4133 (goto-char (point-min))
4134 (if (display-mouse-p)
4135 (insert (substitute-command-keys
4136 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4137 (insert (substitute-command-keys
4138 "In this buffer, type \\[choose-completion] to \
4139 select the completion near point.\n\n")))))
4141 (add-hook 'completion-setup-hook 'completion-setup-function)
4143 (define-key minibuffer-local-completion-map [prior]
4144 'switch-to-completions)
4145 (define-key minibuffer-local-must-match-map [prior]
4146 'switch-to-completions)
4147 (define-key minibuffer-local-completion-map "\M-v"
4148 'switch-to-completions)
4149 (define-key minibuffer-local-must-match-map "\M-v"
4150 'switch-to-completions)
4152 (defun switch-to-completions ()
4153 "Select the completion list window."
4155 ;; Make sure we have a completions window.
4156 (or (get-buffer-window "*Completions*")
4157 (minibuffer-completion-help))
4158 (let ((window (get-buffer-window "*Completions*")))
4160 (select-window window)
4161 (goto-char (point-min))
4162 (search-forward "\n\n")
4165 ;; Support keyboard commands to turn on various modifiers.
4167 ;; These functions -- which are not commands -- each add one modifier
4168 ;; to the following event.
4170 (defun event-apply-alt-modifier (ignore-prompt)
4171 "\\<function-key-map>Add the Alt modifier to the following event.
4172 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
4173 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
4174 (defun event-apply-super-modifier (ignore-prompt)
4175 "\\<function-key-map>Add the Super modifier to the following event.
4176 For example, type \\[event-apply-super-modifier] & to enter Super-&."
4177 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
4178 (defun event-apply-hyper-modifier (ignore-prompt)
4179 "\\<function-key-map>Add the Hyper modifier to the following event.
4180 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
4181 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
4182 (defun event-apply-shift-modifier (ignore-prompt)
4183 "\\<function-key-map>Add the Shift modifier to the following event.
4184 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
4185 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
4186 (defun event-apply-control-modifier (ignore-prompt)
4187 "\\<function-key-map>Add the Ctrl modifier to the following event.
4188 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
4189 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
4190 (defun event-apply-meta-modifier (ignore-prompt)
4191 "\\<function-key-map>Add the Meta modifier to the following event.
4192 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
4193 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
4195 (defun event-apply-modifier (event symbol lshiftby prefix)
4196 "Apply a modifier flag to event EVENT.
4197 SYMBOL is the name of this modifier, as a symbol.
4198 LSHIFTBY is the numeric value of this modifier, in keyboard events.
4199 PREFIX is the string that represents this modifier in an event type symbol."
4201 (cond ((eq symbol 'control)
4202 (if (and (<= (downcase event) ?z)
4203 (>= (downcase event) ?a))
4204 (- (downcase event) ?a -1)
4205 (if (and (<= (downcase event) ?Z)
4206 (>= (downcase event) ?A))
4207 (- (downcase event) ?A -1)
4208 (logior (lsh 1 lshiftby) event))))
4210 (if (and (<= (downcase event) ?z)
4211 (>= (downcase event) ?a))
4213 (logior (lsh 1 lshiftby) event)))
4215 (logior (lsh 1 lshiftby) event)))
4216 (if (memq symbol (event-modifiers event))
4218 (let ((event-type (if (symbolp event) event (car event))))
4219 (setq event-type (intern (concat prefix (symbol-name event-type))))
4222 (cons event-type (cdr event)))))))
4224 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4225 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4226 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4227 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4228 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4229 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4231 ;;;; Keypad support.
4233 ;;; Make the keypad keys act like ordinary typing keys. If people add
4234 ;;; bindings for the function key symbols, then those bindings will
4235 ;;; override these, so this shouldn't interfere with any existing
4238 ;; Also tell read-char how to handle these keys.
4240 (lambda (keypad-normal)
4241 (let ((keypad (nth 0 keypad-normal))
4242 (normal (nth 1 keypad-normal)))
4243 (put keypad 'ascii-character normal)
4244 (define-key function-key-map (vector keypad) (vector normal))))
4245 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4246 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4259 ;;;; forking a twin copy of a buffer.
4262 (defvar clone-buffer-hook nil
4263 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4265 (defun clone-process (process &optional newname)
4266 "Create a twin copy of PROCESS.
4267 If NEWNAME is nil, it defaults to PROCESS' name;
4268 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4269 If PROCESS is associated with a buffer, the new process will be associated
4270 with the current buffer instead.
4271 Returns nil if PROCESS has already terminated."
4272 (setq newname (or newname (process-name process)))
4273 (if (string-match "<[0-9]+>\\'" newname)
4274 (setq newname (substring newname 0 (match-beginning 0))))
4275 (when (memq (process-status process) '(run stop open))
4276 (let* ((process-connection-type (process-tty-name process))
4278 (if (memq (process-status process) '(open))
4279 (let ((args (process-contact process t)))
4280 (setq args (plist-put args :name newname))
4281 (setq args (plist-put args :buffer
4282 (if (process-buffer process)
4284 (apply 'make-network-process args))
4285 (apply 'start-process newname
4286 (if (process-buffer process) (current-buffer))
4287 (process-command process)))))
4288 (set-process-query-on-exit-flag
4289 new-process (process-query-on-exit-flag process))
4290 (set-process-inherit-coding-system-flag
4291 new-process (process-inherit-coding-system-flag process))
4292 (set-process-filter new-process (process-filter process))
4293 (set-process-sentinel new-process (process-sentinel process))
4294 (set-process-plist new-process (copy-sequence (process-plist process)))
4297 ;; things to maybe add (currently partly covered by `funcall mode'):
4300 (defun clone-buffer (&optional newname display-flag)
4301 "Create a twin copy of the current buffer.
4302 If NEWNAME is nil, it defaults to the current buffer's name;
4303 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4305 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
4306 This runs the normal hook `clone-buffer-hook' in the new buffer
4307 after it has been set up properly in other respects."
4310 (if buffer-file-name
4311 (error "Cannot clone a file-visiting buffer"))
4312 (if (get major-mode 'no-clone)
4313 (error "Cannot clone a buffer in %s mode" mode-name))
4314 (list (if current-prefix-arg (read-string "Name: "))
4316 (if buffer-file-name
4317 (error "Cannot clone a file-visiting buffer"))
4318 (if (get major-mode 'no-clone)
4319 (error "Cannot clone a buffer in %s mode" mode-name))
4320 (setq newname (or newname (buffer-name)))
4321 (if (string-match "<[0-9]+>\\'" newname)
4322 (setq newname (substring newname 0 (match-beginning 0))))
4323 (let ((buf (current-buffer))
4327 (mk (if mark-active (mark t)))
4328 (modified (buffer-modified-p))
4330 (lvars (buffer-local-variables))
4331 (process (get-buffer-process (current-buffer)))
4332 (new (generate-new-buffer (or newname (buffer-name)))))
4335 (with-current-buffer new
4336 (insert-buffer-substring buf)))
4337 (with-current-buffer new
4338 (narrow-to-region ptmin ptmax)
4340 (if mk (set-mark mk))
4341 (set-buffer-modified-p modified)
4343 ;; Clone the old buffer's process, if any.
4344 (when process (clone-process process))
4346 ;; Now set up the major mode.
4349 ;; Set up other local variables.
4351 (condition-case () ;in case var is read-only
4354 (set (make-local-variable (car v)) (cdr v)))
4358 ;; Run any hooks (typically set up by the major mode
4359 ;; for cloning to work properly).
4360 (run-hooks 'clone-buffer-hook))
4361 (if display-flag (pop-to-buffer new))
4365 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4366 "Create an indirect buffer that is a twin copy of the current buffer.
4368 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4369 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4370 or if not called with a prefix arg, NEWNAME defaults to the current
4371 buffer's name. The name is modified by adding a `<N>' suffix to it
4372 or by incrementing the N in an existing suffix.
4374 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4375 This is always done when called interactively.
4377 Optional last arg NORECORD non-nil means do not put this buffer at the
4378 front of the list of recently selected ones."
4381 (if (get major-mode 'no-clone-indirect)
4382 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4383 (list (if current-prefix-arg
4384 (read-string "BName of indirect buffer: "))
4386 (if (get major-mode 'no-clone-indirect)
4387 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4388 (setq newname (or newname (buffer-name)))
4389 (if (string-match "<[0-9]+>\\'" newname)
4390 (setq newname (substring newname 0 (match-beginning 0))))
4391 (let* ((name (generate-new-buffer-name newname))
4392 (buffer (make-indirect-buffer (current-buffer) name t)))
4394 (pop-to-buffer buffer norecord))
4398 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4399 "Create an indirect buffer that is a twin copy of BUFFER.
4400 Select the new buffer in another window.
4401 Optional second arg NORECORD non-nil means do not put this buffer at
4402 the front of the list of recently selected ones."
4403 (interactive "bClone buffer in other window: ")
4404 (let ((pop-up-windows t))
4406 (clone-indirect-buffer nil t norecord)))
4408 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4410 ;;; Handling of Backspace and Delete keys.
4412 (defcustom normal-erase-is-backspace nil
4413 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4415 On window systems, the default value of this option is chosen
4416 according to the keyboard used. If the keyboard has both a Backspace
4417 key and a Delete key, and both are mapped to their usual meanings, the
4418 option's default value is set to t, so that Backspace can be used to
4419 delete backward, and Delete can be used to delete forward.
4421 If not running under a window system, customizing this option accomplishes
4422 a similar effect by mapping C-h, which is usually generated by the
4423 Backspace key, to DEL, and by mapping DEL to C-d via
4424 `keyboard-translate'. The former functionality of C-h is available on
4425 the F1 key. You should probably not use this setting if you don't
4426 have both Backspace, Delete and F1 keys.
4428 Setting this variable with setq doesn't take effect. Programmatically,
4429 call `normal-erase-is-backspace-mode' (which see) instead."
4431 :group 'editing-basics
4433 :set (lambda (symbol value)
4434 ;; The fboundp is because of a problem with :set when
4435 ;; dumping Emacs. It doesn't really matter.
4436 (if (fboundp 'normal-erase-is-backspace-mode)
4437 (normal-erase-is-backspace-mode (or value 0))
4438 (set-default symbol value))))
4441 (defun normal-erase-is-backspace-mode (&optional arg)
4442 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4444 With numeric arg, turn the mode on if and only if ARG is positive.
4446 On window systems, when this mode is on, Delete is mapped to C-d and
4447 Backspace is mapped to DEL; when this mode is off, both Delete and
4448 Backspace are mapped to DEL. (The remapping goes via
4449 `function-key-map', so binding Delete or Backspace in the global or
4450 local keymap will override that.)
4452 In addition, on window systems, the bindings of C-Delete, M-Delete,
4453 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4454 the global keymap in accordance with the functionality of Delete and
4455 Backspace. For example, if Delete is remapped to C-d, which deletes
4456 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4457 to DEL, which deletes backward, C-Delete is bound to
4458 `backward-kill-word'.
4460 If not running on a window system, a similar effect is accomplished by
4461 remapping C-h (normally produced by the Backspace key) and DEL via
4462 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4463 to C-d; if it's off, the keys are not remapped.
4465 When not running on a window system, and this mode is turned on, the
4466 former functionality of C-h is available on the F1 key. You should
4467 probably not turn on this mode on a text-only terminal if you don't
4468 have both Backspace, Delete and F1 keys.
4470 See also `normal-erase-is-backspace'."
4472 (setq normal-erase-is-backspace
4474 (> (prefix-numeric-value arg) 0)
4475 (not normal-erase-is-backspace)))
4477 (cond ((or (memq window-system '(x w32 mac pc))
4478 (memq system-type '(ms-dos windows-nt)))
4480 `(([C-delete] [C-backspace])
4481 ([M-delete] [M-backspace])
4482 ([C-M-delete] [C-M-backspace])
4484 [C-delete] [C-backspace])))
4485 (old-state (lookup-key function-key-map [delete])))
4487 (if normal-erase-is-backspace
4489 (define-key function-key-map [delete] [?\C-d])
4490 (define-key function-key-map [kp-delete] [?\C-d])
4491 (define-key function-key-map [backspace] [?\C-?]))
4492 (define-key function-key-map [delete] [?\C-?])
4493 (define-key function-key-map [kp-delete] [?\C-?])
4494 (define-key function-key-map [backspace] [?\C-?]))
4496 ;; Maybe swap bindings of C-delete and C-backspace, etc.
4497 (unless (equal old-state (lookup-key function-key-map [delete]))
4498 (dolist (binding bindings)
4499 (let ((map global-map))
4500 (when (keymapp (car binding))
4501 (setq map (car binding) binding (cdr binding)))
4502 (let* ((key1 (nth 0 binding))
4503 (key2 (nth 1 binding))
4504 (binding1 (lookup-key map key1))
4505 (binding2 (lookup-key map key2)))
4506 (define-key map key1 binding2)
4507 (define-key map key2 binding1)))))))
4509 (if normal-erase-is-backspace
4511 (keyboard-translate ?\C-h ?\C-?)
4512 (keyboard-translate ?\C-? ?\C-d))
4513 (keyboard-translate ?\C-h ?\C-h)
4514 (keyboard-translate ?\C-? ?\C-?))))
4516 (run-hooks 'normal-erase-is-backspace-hook)
4518 (message "Delete key deletes %s"
4519 (if normal-erase-is-backspace "forward" "backward"))))
4521 (defcustom idle-update-delay 0.5
4522 "*Idle time delay before updating various things on the screen.
4523 Various Emacs features that update auxiliary information when point moves
4524 wait this many seconds after Emacs becomes idle before doing an update."
4529 (defvar vis-mode-saved-buffer-invisibility-spec nil
4530 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
4532 (define-minor-mode visible-mode
4533 "Toggle Visible mode.
4534 With argument ARG turn Visible mode on iff ARG is positive.
4536 Enabling Visible mode makes all invisible text temporarily visible.
4537 Disabling Visible mode turns off that effect. Visible mode
4538 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
4540 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
4541 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
4542 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
4544 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
4545 buffer-invisibility-spec)
4546 (setq buffer-invisibility-spec nil)))
4548 ;; Minibuffer prompt stuff.
4550 ;(defun minibuffer-prompt-modification (start end)
4551 ; (error "You cannot modify the prompt"))
4554 ;(defun minibuffer-prompt-insertion (start end)
4555 ; (let ((inhibit-modification-hooks t))
4556 ; (delete-region start end)
4557 ; ;; Discard undo information for the text insertion itself
4558 ; ;; and for the text deletion.above.
4559 ; (when (consp buffer-undo-list)
4560 ; (setq buffer-undo-list (cddr buffer-undo-list)))
4561 ; (message "You cannot modify the prompt")))
4564 ;(setq minibuffer-prompt-properties
4565 ; (list 'modification-hooks '(minibuffer-prompt-modification)
4566 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
4571 ;;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
4572 ;;; simple.el ends here