1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99,
4 ;; 2000, 01, 02, 03, 04
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 ;;; next-error support framework
70 (defvar next-error-last-buffer nil
71 "The most recent next-error buffer.
72 A buffer becomes most recent when its compilation, grep, or
73 similar mode is started, or when it is used with \\[next-error]
74 or \\[compile-goto-error].")
76 (defvar next-error-function nil
77 "Function to use to find the next error in the current buffer.
78 The function is called with 2 parameters:
79 ARG is an integer specifying by how many errors to move.
80 RESET is a boolean which, if non-nil, says to go back to the beginning
81 of the errors before moving.
82 Major modes providing compile-like functionality should set this variable
83 to indicate to `next-error' that this is a candidate buffer and how
86 (make-variable-buffer-local 'next-error-function)
88 (defsubst next-error-buffer-p (buffer &optional extra-test)
89 "Test if BUFFER is a next-error capable buffer."
90 (with-current-buffer buffer
91 (or (and extra-test (funcall extra-test))
92 next-error-function)))
94 ;; Return a next-error capable buffer according to the following rules:
95 ;; 1. If the current buffer is a next-error capable buffer, return it.
96 ;; 2. If one window on the selected frame displays such buffer, return it.
97 ;; 3. If next-error-last-buffer is set to a live buffer, use that.
98 ;; 4. Otherwise, look for a next-error capable buffer in a buffer list.
99 ;; 5. Signal an error if there are none.
100 (defun next-error-find-buffer (&optional other-buffer extra-test)
101 (if (and (not other-buffer)
102 (next-error-buffer-p (current-buffer) extra-test))
103 ;; The current buffer is a next-error capable buffer.
106 (let ((window-buffers
110 (and (next-error-buffer-p (window-buffer w) extra-test)
114 (setq window-buffers (delq (current-buffer) window-buffers)))
115 (if (eq (length window-buffers) 1)
116 (car window-buffers)))
117 (if (and next-error-last-buffer (buffer-name next-error-last-buffer)
118 (next-error-buffer-p next-error-last-buffer extra-test)
119 (or (not other-buffer) (not (eq next-error-last-buffer
121 next-error-last-buffer
122 (let ((buffers (buffer-list)))
123 (while (and buffers (or (not (next-error-buffer-p (car buffers) extra-test))
125 (eq (car buffers) (current-buffer)))))
126 (setq buffers (cdr buffers)))
129 (or (and other-buffer
130 (next-error-buffer-p (current-buffer) extra-test)
131 ;; The current buffer is a next-error capable buffer.
134 (message "This is the only next-error capable buffer."))
136 (error "No next-error capable buffer found"))))))))
138 (defun next-error (arg &optional reset)
139 "Visit next next-error message and corresponding source code.
141 If all the error messages parsed so far have been processed already,
142 the message buffer is checked for new ones.
144 A prefix ARG specifies how many error messages to move;
145 negative means move back to previous error messages.
146 Just \\[universal-argument] as a prefix means reparse the error message buffer
147 and start at the first error.
149 The RESET argument specifies that we should restart from the beginning.
151 \\[next-error] normally uses the most recently started
152 compilation, grep, or occur buffer. It can also operate on any
153 buffer with output from the \\[compile], \\[grep] commands, or,
154 more generally, on any buffer in Compilation mode or with
155 Compilation Minor mode enabled, or any buffer in which
156 `next-error-function' is bound to an appropriate
157 function. To specify use of a particular buffer for error
158 messages, type \\[next-error] in that buffer.
160 Once \\[next-error] has chosen the buffer for error messages,
161 it stays with that buffer until you use it in some other buffer which
162 uses Compilation mode or Compilation Minor mode.
164 See variables `compilation-parse-errors-function' and
165 \`compilation-error-regexp-alist' for customization ideas."
167 (if (consp arg) (setq reset t arg nil))
168 (when (setq next-error-last-buffer (next-error-find-buffer))
169 ;; we know here that next-error-function is a valid symbol we can funcall
170 (with-current-buffer next-error-last-buffer
171 (funcall next-error-function (prefix-numeric-value arg) reset))))
173 (defalias 'goto-next-locus 'next-error)
174 (defalias 'next-match 'next-error)
176 (define-key ctl-x-map "`" 'next-error)
178 (defun previous-error (n)
179 "Visit previous next-error message and corresponding source code.
181 Prefix arg N says how many error messages to move backwards (or
182 forwards, if negative).
184 This operates on the output from the \\[compile] and \\[grep] commands."
188 (defun first-error (n)
189 "Restart at the first error.
190 Visit corresponding source code.
191 With prefix arg N, visit the source code of the Nth error.
192 This operates on the output from the \\[compile] command, for instance."
196 (defun next-error-no-select (n)
197 "Move point to the next error in the next-error buffer and highlight match.
198 Prefix arg N says how many error messages to move forwards (or
199 backwards, if negative).
200 Finds and highlights the source line like \\[next-error], but does not
201 select the source buffer."
204 (pop-to-buffer next-error-last-buffer))
206 (defun previous-error-no-select (n)
207 "Move point to the previous error in the next-error buffer and highlight match.
208 Prefix arg N says how many error messages to move backwards (or
209 forwards, if negative).
210 Finds and highlights the source line like \\[previous-error], but does not
211 select the source buffer."
213 (next-error-no-select (- n)))
217 (defun fundamental-mode ()
218 "Major mode not specialized for anything in particular.
219 Other major modes are defined by comparison with this one."
221 (kill-all-local-variables))
223 ;; Making and deleting lines.
225 (defun newline (&optional arg)
226 "Insert a newline, and move to left margin of the new line if it's blank.
227 If `use-hard-newlines' is non-nil, the newline is marked with the
228 text-property `hard'.
229 With ARG, insert that many newlines.
230 Call `auto-fill-function' if the current column number is greater
231 than the value of `fill-column' and ARG is nil."
233 (barf-if-buffer-read-only)
234 ;; Inserting a newline at the end of a line produces better redisplay in
235 ;; try_window_id than inserting at the beginning of a line, and the textual
236 ;; result is the same. So, if we're at beginning of line, pretend to be at
237 ;; the end of the previous line.
238 (let ((flag (and (not (bobp))
240 ;; Make sure no functions want to be told about
241 ;; the range of the changes.
242 (not after-change-functions)
243 (not before-change-functions)
244 ;; Make sure there are no markers here.
245 (not (buffer-has-markers-at (1- (point))))
246 (not (buffer-has-markers-at (point)))
247 ;; Make sure no text properties want to know
248 ;; where the change was.
249 (not (get-char-property (1- (point)) 'modification-hooks))
250 (not (get-char-property (1- (point)) 'insert-behind-hooks))
252 (not (get-char-property (point) 'insert-in-front-hooks)))
253 ;; Make sure the newline before point isn't intangible.
254 (not (get-char-property (1- (point)) 'intangible))
255 ;; Make sure the newline before point isn't read-only.
256 (not (get-char-property (1- (point)) 'read-only))
257 ;; Make sure the newline before point isn't invisible.
258 (not (get-char-property (1- (point)) 'invisible))
259 ;; Make sure the newline before point has the same
260 ;; properties as the char before it (if any).
261 (< (or (previous-property-change (point)) -2)
263 (was-page-start (and (bolp)
264 (looking-at page-delimiter)))
266 (if flag (backward-char 1))
267 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
268 ;; Set last-command-char to tell self-insert what to insert.
269 (let ((last-command-char ?\n)
270 ;; Don't auto-fill if we have a numeric argument.
271 ;; Also not if flag is true (it would fill wrong line);
272 ;; there is no need to since we're at BOL.
273 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
275 (self-insert-command (prefix-numeric-value arg))
276 ;; If we get an error in self-insert-command, put point at right place.
277 (if flag (forward-char 1))))
278 ;; Even if we did *not* get an error, keep that forward-char;
279 ;; all further processing should apply to the newline that the user
280 ;; thinks he inserted.
282 ;; Mark the newline(s) `hard'.
283 (if use-hard-newlines
284 (set-hard-newline-properties
285 (- (point) (if arg (prefix-numeric-value arg) 1)) (point)))
286 ;; If the newline leaves the previous line blank,
287 ;; and we have a left margin, delete that from the blank line.
290 (goto-char beforepos)
292 (and (looking-at "[ \t]$")
293 (> (current-left-margin) 0)
294 (delete-region (point) (progn (end-of-line) (point))))))
295 ;; Indent the line after the newline, except in one case:
296 ;; when we added the newline at the beginning of a line
297 ;; which starts a page.
299 (move-to-left-margin nil t)))
302 (defun set-hard-newline-properties (from to)
303 (let ((sticky (get-text-property from 'rear-nonsticky)))
304 (put-text-property from to 'hard 't)
305 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
306 (if (and (listp sticky) (not (memq 'hard sticky)))
307 (put-text-property from (point) 'rear-nonsticky
308 (cons 'hard sticky)))))
311 "Insert a newline and leave point before it.
312 If there is a fill prefix and/or a left-margin, insert them on the new line
313 if the line would have been blank.
314 With arg N, insert N newlines."
316 (let* ((do-fill-prefix (and fill-prefix (bolp)))
317 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
319 ;; Don't expand an abbrev before point.
325 (if do-left-margin (indent-to (current-left-margin)))
326 (if do-fill-prefix (insert-and-inherit fill-prefix))))
332 (defun split-line (&optional arg)
333 "Split current line, moving portion beyond point vertically down.
334 If the current line starts with `fill-prefix', insert it on the new
335 line as well. With prefix ARG, don't insert fill-prefix on new line.
337 When called from Lisp code, ARG may be a prefix string to copy."
339 (skip-chars-forward " \t")
340 (let* ((col (current-column))
342 ;; What prefix should we check for (nil means don't).
343 (prefix (cond ((stringp arg) arg)
346 ;; Does this line start with it?
347 (have-prfx (and prefix
350 (looking-at (regexp-quote prefix))))))
352 (if have-prfx (insert-and-inherit prefix))
356 (defun delete-indentation (&optional arg)
357 "Join this line to previous and fix up whitespace at join.
358 If there is a fill prefix, delete it from the beginning of this line.
359 With argument, join this line to following line."
362 (if arg (forward-line 1))
363 (if (eq (preceding-char) ?\n)
365 (delete-region (point) (1- (point)))
366 ;; If the second line started with the fill prefix,
367 ;; delete the prefix.
369 (<= (+ (point) (length fill-prefix)) (point-max))
371 (buffer-substring (point)
372 (+ (point) (length fill-prefix)))))
373 (delete-region (point) (+ (point) (length fill-prefix))))
374 (fixup-whitespace))))
376 (defalias 'join-line #'delete-indentation) ; easier to find
378 (defun delete-blank-lines ()
379 "On blank line, delete all surrounding blank lines, leaving just one.
380 On isolated blank line, delete that one.
381 On nonblank line, delete any immediately following blank lines."
383 (let (thisblank singleblank)
386 (setq thisblank (looking-at "[ \t]*$"))
387 ;; Set singleblank if there is just one blank line here.
390 (not (looking-at "[ \t]*\n[ \t]*$"))
392 (progn (forward-line -1)
393 (not (looking-at "[ \t]*$")))))))
394 ;; Delete preceding blank lines, and this one too if it's the only one.
398 (if singleblank (forward-line 1))
399 (delete-region (point)
400 (if (re-search-backward "[^ \t\n]" nil t)
401 (progn (forward-line 1) (point))
403 ;; Delete following blank lines, unless the current line is blank
404 ;; and there are no following blank lines.
405 (if (not (and thisblank singleblank))
409 (delete-region (point)
410 (if (re-search-forward "[^ \t\n]" nil t)
411 (progn (beginning-of-line) (point))
413 ;; Handle the special case where point is followed by newline and eob.
414 ;; Delete the line, leaving point at eob.
415 (if (looking-at "^[ \t]*\n\\'")
416 (delete-region (point) (point-max)))))
418 (defun delete-trailing-whitespace ()
419 "Delete all the trailing whitespace across the current buffer.
420 All whitespace after the last non-whitespace character in a line is deleted.
421 This respects narrowing, created by \\[narrow-to-region] and friends.
422 A formfeed is not considered whitespace by this function."
426 (goto-char (point-min))
427 (while (re-search-forward "\\s-$" nil t)
428 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
429 ;; Don't delete formfeeds, even if they are considered whitespace.
431 (if (looking-at ".*\f")
432 (goto-char (match-end 0))))
433 (delete-region (point) (match-end 0))))))
435 (defun newline-and-indent ()
436 "Insert a newline, then indent according to major mode.
437 Indentation is done using the value of `indent-line-function'.
438 In programming language modes, this is the same as TAB.
439 In some text modes, where TAB inserts a tab, this command indents to the
440 column specified by the function `current-left-margin'."
442 (delete-horizontal-space t)
444 (indent-according-to-mode))
446 (defun reindent-then-newline-and-indent ()
447 "Reindent current line, insert newline, then indent the new line.
448 Indentation of both lines is done according to the current major mode,
449 which means calling the current value of `indent-line-function'.
450 In programming language modes, this is the same as TAB.
451 In some text modes, where TAB inserts a tab, this indents to the
452 column specified by the function `current-left-margin'."
455 ;; Be careful to insert the newline before indenting the line.
456 ;; Otherwise, the indentation might be wrong.
460 (indent-according-to-mode)
461 (delete-horizontal-space t))
462 (indent-according-to-mode)))
464 (defun quoted-insert (arg)
465 "Read next input character and insert it.
466 This is useful for inserting control characters.
468 If the first character you type after this command is an octal digit,
469 you should type a sequence of octal digits which specify a character code.
470 Any nondigit terminates the sequence. If the terminator is a RET,
471 it is discarded; any other terminator is used itself as input.
472 The variable `read-quoted-char-radix' specifies the radix for this feature;
473 set it to 10 or 16 to use decimal or hex instead of octal.
475 In overwrite mode, this function inserts the character anyway, and
476 does not handle octal digits specially. This means that if you use
477 overwrite as your normal editing mode, you can use this function to
478 insert characters when necessary.
480 In binary overwrite mode, this function does overwrite, and octal
481 digits are interpreted as a character code. This is intended to be
482 useful for editing binary files."
484 (let* ((char (let (translation-table-for-input)
485 (if (or (not overwrite-mode)
486 (eq overwrite-mode 'overwrite-mode-binary))
489 ;; Assume character codes 0240 - 0377 stand for characters in some
490 ;; single-byte character set, and convert them to Emacs
492 (if (and enable-multibyte-characters
495 (setq char (unibyte-char-to-multibyte char)))
497 (if (eq overwrite-mode 'overwrite-mode-binary)
500 (insert-and-inherit char)
501 (setq arg (1- arg)))))
503 (defun forward-to-indentation (&optional arg)
504 "Move forward ARG lines and position at first nonblank character."
506 (forward-line (or arg 1))
507 (skip-chars-forward " \t"))
509 (defun backward-to-indentation (&optional arg)
510 "Move backward ARG lines and position at first nonblank character."
512 (forward-line (- (or arg 1)))
513 (skip-chars-forward " \t"))
515 (defun back-to-indentation ()
516 "Move point to the first non-whitespace character on this line."
518 (beginning-of-line 1)
519 (skip-syntax-forward " " (line-end-position))
520 ;; Move back over chars that have whitespace syntax but have the p flag.
521 (backward-prefix-chars))
523 (defun fixup-whitespace ()
524 "Fixup white space between objects around point.
525 Leave one space or none, according to the context."
528 (delete-horizontal-space)
529 (if (or (looking-at "^\\|\\s)")
530 (save-excursion (forward-char -1)
531 (looking-at "$\\|\\s(\\|\\s'")))
535 (defun delete-horizontal-space (&optional backward-only)
536 "Delete all spaces and tabs around point.
537 If BACKWARD-ONLY is non-nil, only delete spaces before point."
539 (let ((orig-pos (point)))
544 (skip-chars-forward " \t")
545 (constrain-to-field nil orig-pos t)))
547 (skip-chars-backward " \t")
548 (constrain-to-field nil orig-pos)))))
550 (defun just-one-space ()
551 "Delete all spaces and tabs around point, leaving one space."
553 (let ((orig-pos (point)))
554 (skip-chars-backward " \t")
555 (constrain-to-field nil orig-pos)
556 (if (= (following-char) ? )
562 (skip-chars-forward " \t")
563 (constrain-to-field nil orig-pos t)))))
565 (defun beginning-of-buffer (&optional arg)
566 "Move point to the beginning of the buffer; leave mark at previous position.
567 With arg N, put point N/10 of the way from the beginning.
569 If the buffer is narrowed, this command uses the beginning and size
570 of the accessible part of the buffer.
572 Don't use this command in Lisp programs!
573 \(goto-char (point-min)) is faster and avoids clobbering the mark."
576 (let ((size (- (point-max) (point-min))))
580 ;; Avoid overflow for large buffer sizes!
581 (* (prefix-numeric-value arg)
583 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
585 (if arg (forward-line 1)))
587 (defun end-of-buffer (&optional arg)
588 "Move point to the end of the buffer; leave mark at previous position.
589 With arg N, put point N/10 of the way from the end.
591 If the buffer is narrowed, this command uses the beginning and size
592 of the accessible part of the buffer.
594 Don't use this command in Lisp programs!
595 \(goto-char (point-max)) is faster and avoids clobbering the mark."
598 (let ((size (- (point-max) (point-min))))
602 ;; Avoid overflow for large buffer sizes!
603 (* (prefix-numeric-value arg)
605 (/ (* size (prefix-numeric-value arg)) 10)))
607 ;; If we went to a place in the middle of the buffer,
608 ;; adjust it to the beginning of a line.
609 (cond (arg (forward-line 1))
610 ((> (point) (window-end nil t))
611 ;; If the end of the buffer is not already on the screen,
612 ;; then scroll specially to put it near, but not at, the bottom.
613 (overlay-recenter (point))
616 (defun mark-whole-buffer ()
617 "Put point at beginning and mark at end of buffer.
618 You probably should not use this function in Lisp programs;
619 it is usually a mistake for a Lisp function to use any subroutine
620 that uses or sets the mark."
623 (push-mark (point-max) nil t)
624 (goto-char (point-min)))
627 ;; Counting lines, one way or another.
629 (defun goto-line (arg)
630 "Goto line ARG, counting from line 1 at beginning of buffer."
631 (interactive "NGoto line: ")
632 (setq arg (prefix-numeric-value arg))
636 (if (eq selective-display t)
637 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
638 (forward-line (1- arg)))))
640 (defun count-lines-region (start end)
641 "Print number of lines and characters in the region."
643 (message "Region has %d lines, %d characters"
644 (count-lines start end) (- end start)))
647 "Print the current buffer line number and narrowed line number of point."
649 (let ((opoint (point)) (start (point-min))
650 (n (line-number-at-pos)))
652 (message "Line %d" n)
656 (message "line %d (narrowed line %d)"
657 (+ n (line-number-at-pos start) -1) n))))))
659 (defun count-lines (start end)
660 "Return number of lines between START and END.
661 This is usually the number of newlines between them,
662 but can be one more if START is not equal to END
663 and the greater of them is not at the start of a line."
666 (narrow-to-region start end)
667 (goto-char (point-min))
668 (if (eq selective-display t)
671 (while (re-search-forward "[\n\C-m]" nil t 40)
672 (setq done (+ 40 done)))
673 (while (re-search-forward "[\n\C-m]" nil t 1)
674 (setq done (+ 1 done)))
675 (goto-char (point-max))
676 (if (and (/= start end)
680 (- (buffer-size) (forward-line (buffer-size)))))))
682 (defun line-number-at-pos (&optional pos)
683 "Return (narrowed) buffer line number at position POS.
684 If POS is nil, use current buffer location."
685 (let ((opoint (or pos (point))) start)
687 (goto-char (point-min))
691 (1+ (count-lines start (point))))))
693 (defun what-cursor-position (&optional detail)
694 "Print info on cursor position (on screen and within buffer).
695 Also describe the character after point, and give its character code
696 in octal, decimal and hex.
698 For a non-ASCII multibyte character, also give its encoding in the
699 buffer's selected coding system if the coding system encodes the
700 character safely. If the character is encoded into one byte, that
701 code is shown in hex. If the character is encoded into more than one
702 byte, just \"...\" is shown.
704 In addition, with prefix argument, show details about that character
705 in *Help* buffer. See also the command `describe-char'."
707 (let* ((char (following-char))
711 (total (buffer-size))
712 (percent (if (> total 50000)
713 ;; Avoid overflow from multiplying by 100!
714 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
715 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
716 (hscroll (if (= (window-hscroll) 0)
718 (format " Hscroll=%d" (window-hscroll))))
719 (col (current-column)))
721 (if (or (/= beg 1) (/= end (1+ total)))
722 (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
723 pos total percent beg end col hscroll)
724 (message "point=%d of %d (%d%%) column %d %s"
725 pos total percent col hscroll))
726 (let ((coding buffer-file-coding-system)
727 encoded encoding-msg)
729 (eq (coding-system-type coding) t))
730 (setq coding default-buffer-file-coding-system))
731 (if (eq (char-charset char) 'eight-bit)
733 (format "(0%o, %d, 0x%x, raw-byte)" char char char))
734 (setq encoded (and (>= char 128) (encode-coding-char char coding)))
737 (format "(0%o, %d, 0x%x, file %s)"
739 (if (> (length encoded) 1)
741 (encoded-string-description encoded coding)))
742 (format "(0%o, %d, 0x%x)" char char char))))
744 ;; We show the detailed information about CHAR.
745 (describe-char (point)))
746 (if (or (/= beg 1) (/= end (1+ total)))
747 (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
749 (single-key-description char)
750 (buffer-substring-no-properties (point) (1+ (point))))
751 encoding-msg pos total percent beg end col hscroll)
752 (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
754 (single-key-description char)
755 (buffer-substring-no-properties (point) (1+ (point))))
756 encoding-msg pos total percent col hscroll))))))
758 (defvar read-expression-map
759 (let ((m (make-sparse-keymap)))
760 (define-key m "\M-\t" 'lisp-complete-symbol)
761 (set-keymap-parent m minibuffer-local-map)
763 "Minibuffer keymap used for reading Lisp expressions.")
765 (defvar read-expression-history nil)
767 (defcustom eval-expression-print-level 4
768 "*Value to use for `print-level' when printing value in `eval-expression'.
769 A value of nil means no limit."
771 :type '(choice (const :tag "No Limit" nil) integer)
774 (defcustom eval-expression-print-length 12
775 "*Value to use for `print-length' when printing value in `eval-expression'.
776 A value of nil means no limit."
778 :type '(choice (const :tag "No Limit" nil) integer)
781 (defcustom eval-expression-debug-on-error t
782 "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'.
783 If nil, don't change the value of `debug-on-error'."
788 (defun eval-expression-print-format (value)
789 "Format VALUE as a result of evaluated expression.
790 Return a formatted string which is displayed in the echo area
791 in addition to the value printed by prin1 in functions which
792 display the result of expression evaluation."
793 (if (and (integerp value)
794 (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
795 (eq this-command last-command)
796 (and (boundp 'edebug-active) edebug-active)))
798 (if (or (and (boundp 'edebug-active) edebug-active)
799 (memq this-command '(eval-last-sexp eval-print-last-sexp)))
800 (prin1-char value))))
802 (format " (0%o, 0x%x) = %s" value value char-string)
803 (format " (0%o, 0x%x)" value value)))))
805 ;; We define this, rather than making `eval' interactive,
806 ;; for the sake of completion of names like eval-region, eval-current-buffer.
807 (defun eval-expression (eval-expression-arg
808 &optional eval-expression-insert-value)
809 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
810 Value is also consed on to front of the variable `values'.
811 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
812 insert the result into the current buffer instead of printing it in
815 (list (read-from-minibuffer "Eval: "
816 nil read-expression-map t
817 'read-expression-history)
820 (if (null eval-expression-debug-on-error)
821 (setq values (cons (eval eval-expression-arg) values))
822 (let ((old-value (make-symbol "t")) new-value)
823 ;; Bind debug-on-error to something unique so that we can
824 ;; detect when evaled code changes it.
825 (let ((debug-on-error old-value))
826 (setq values (cons (eval eval-expression-arg) values))
827 (setq new-value debug-on-error))
828 ;; If evaled code has changed the value of debug-on-error,
829 ;; propagate that change to the global binding.
830 (unless (eq old-value new-value)
831 (setq debug-on-error new-value))))
833 (let ((print-length eval-expression-print-length)
834 (print-level eval-expression-print-level))
835 (if eval-expression-insert-value
837 (let ((standard-output (current-buffer)))
838 (eval-last-sexp-print-value (car values))))
840 (prin1 (car values) t)
841 (let ((str (eval-expression-print-format (car values))))
842 (if str (princ str t)))))))
844 (defun edit-and-eval-command (prompt command)
845 "Prompting with PROMPT, let user edit COMMAND and eval result.
846 COMMAND is a Lisp expression. Let user edit that expression in
847 the minibuffer, then read and evaluate the result."
849 (let ((print-level nil)
850 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
852 (read-from-minibuffer prompt
853 (prin1-to-string command)
854 read-expression-map t
856 ;; If command was added to command-history as a string,
857 ;; get rid of that. We want only evaluable expressions there.
858 (if (stringp (car command-history))
859 (setq command-history (cdr command-history)))))))
861 ;; If command to be redone does not match front of history,
862 ;; add it to the history.
863 (or (equal command (car command-history))
864 (setq command-history (cons command command-history)))
867 (defun repeat-complex-command (arg)
868 "Edit and re-evaluate last complex command, or ARGth from last.
869 A complex command is one which used the minibuffer.
870 The command is placed in the minibuffer as a Lisp form for editing.
871 The result is executed, repeating the command as changed.
872 If the command has been changed or is not the most recent previous command
873 it is added to the front of the command history.
874 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
875 to get different commands to edit and resubmit."
877 (let ((elt (nth (1- arg) command-history))
882 (let ((print-level nil)
883 (minibuffer-history-position arg)
884 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
886 (read-from-minibuffer
887 "Redo: " (prin1-to-string elt) read-expression-map t
888 (cons 'command-history arg))
890 ;; If command was added to command-history as a
891 ;; string, get rid of that. We want only
892 ;; evaluable expressions there.
893 (if (stringp (car command-history))
894 (setq command-history (cdr command-history))))))
896 ;; If command to be redone does not match front of history,
897 ;; add it to the history.
898 (or (equal newcmd (car command-history))
899 (setq command-history (cons newcmd command-history)))
902 (error "Argument %d is beyond length of command history" arg)
903 (error "There are no previous complex commands to repeat")))))
905 (defvar minibuffer-history nil
906 "Default minibuffer history list.
907 This is used for all minibuffer input
908 except when an alternate history list is specified.")
909 (defvar minibuffer-history-sexp-flag nil
910 "Control whether history list elements are expressions or strings.
911 If the value of this variable equals current minibuffer depth,
912 they are expressions; otherwise they are strings.
913 \(That convention is designed to do the right thing fora
914 recursive uses of the minibuffer.)")
915 (setq minibuffer-history-variable 'minibuffer-history)
916 (setq minibuffer-history-position nil)
917 (defvar minibuffer-history-search-history nil)
919 (defvar minibuffer-text-before-history nil
920 "Text that was in this minibuffer before any history commands.
921 This is nil if there have not yet been any history commands
922 in this use of the minibuffer.")
924 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
926 (defun minibuffer-history-initialize ()
927 (setq minibuffer-text-before-history nil))
929 (defun minibuffer-avoid-prompt (new old)
930 "A point-motion hook for the minibuffer, that moves point out of the prompt."
931 (constrain-to-field nil (point-max)))
933 (defcustom minibuffer-history-case-insensitive-variables nil
934 "*Minibuffer history variables for which matching should ignore case.
935 If a history variable is a member of this list, then the
936 \\[previous-matching-history-element] and \\[next-matching-history-element]\
937 commands ignore case when searching it, regardless of `case-fold-search'."
938 :type '(repeat variable)
941 (defun previous-matching-history-element (regexp n)
942 "Find the previous history element that matches REGEXP.
943 \(Previous history elements refer to earlier actions.)
944 With prefix argument N, search for Nth previous match.
945 If N is negative, find the next or Nth next match.
946 Normally, history elements are matched case-insensitively if
947 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
948 makes the search case-sensitive.
949 See also `minibuffer-history-case-insensitive-variables'."
951 (let* ((enable-recursive-minibuffers t)
952 (regexp (read-from-minibuffer "Previous element matching (regexp): "
956 'minibuffer-history-search-history
957 (car minibuffer-history-search-history))))
958 ;; Use the last regexp specified, by default, if input is empty.
959 (list (if (string= regexp "")
960 (if minibuffer-history-search-history
961 (car minibuffer-history-search-history)
962 (error "No previous history search regexp"))
964 (prefix-numeric-value current-prefix-arg))))
966 (if (and (zerop minibuffer-history-position)
967 (null minibuffer-text-before-history))
968 (setq minibuffer-text-before-history
969 (minibuffer-contents-no-properties)))
970 (let ((history (symbol-value minibuffer-history-variable))
972 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
973 ;; On some systems, ignore case for file names.
974 (if (memq minibuffer-history-variable
975 minibuffer-history-case-insensitive-variables)
977 ;; Respect the user's setting for case-fold-search:
983 (pos minibuffer-history-position))
986 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
987 (when (= pos prevpos)
989 "No later matching history item"
990 "No earlier matching history item")))
992 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
993 (let ((print-level nil))
994 (prin1-to-string (nth (1- pos) history)))
995 (nth (1- pos) history)))
998 (and (string-match regexp match-string)
1000 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
1001 (match-beginning 1))))
1003 (setq n (+ n (if (< n 0) 1 -1)))))
1004 (setq minibuffer-history-position pos)
1005 (goto-char (point-max))
1006 (delete-minibuffer-contents)
1007 (insert match-string)
1008 (goto-char (+ (minibuffer-prompt-end) match-offset))))
1009 (if (memq (car (car command-history)) '(previous-matching-history-element
1010 next-matching-history-element))
1011 (setq command-history (cdr command-history))))
1013 (defun next-matching-history-element (regexp n)
1014 "Find the next history element that matches REGEXP.
1015 \(The next history element refers to a more recent action.)
1016 With prefix argument N, search for Nth next match.
1017 If N is negative, find the previous or Nth previous match.
1018 Normally, history elements are matched case-insensitively if
1019 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1020 makes the search case-sensitive."
1022 (let* ((enable-recursive-minibuffers t)
1023 (regexp (read-from-minibuffer "Next element matching (regexp): "
1025 minibuffer-local-map
1027 'minibuffer-history-search-history)))
1028 ;; Use the last regexp specified, by default, if input is empty.
1029 (list (if (string= regexp "")
1030 (setcar minibuffer-history-search-history
1031 (nth 1 minibuffer-history-search-history))
1033 (prefix-numeric-value current-prefix-arg))))
1034 (previous-matching-history-element regexp (- n)))
1036 (defvar minibuffer-temporary-goal-position nil)
1038 (defun next-history-element (n)
1039 "Insert the next element of the minibuffer history into the minibuffer."
1042 (let ((narg (- minibuffer-history-position n))
1043 (minimum (if minibuffer-default -1 0))
1044 elt minibuffer-returned-to-present)
1045 (if (and (zerop minibuffer-history-position)
1046 (null minibuffer-text-before-history))
1047 (setq minibuffer-text-before-history
1048 (minibuffer-contents-no-properties)))
1049 (if (< narg minimum)
1050 (if minibuffer-default
1051 (error "End of history; no next item")
1052 (error "End of history; no default available")))
1053 (if (> narg (length (symbol-value minibuffer-history-variable)))
1054 (error "Beginning of history; no preceding item"))
1055 (unless (memq last-command '(next-history-element
1056 previous-history-element))
1057 (let ((prompt-end (minibuffer-prompt-end)))
1058 (set (make-local-variable 'minibuffer-temporary-goal-position)
1059 (cond ((<= (point) prompt-end) prompt-end)
1062 (goto-char (point-max))
1063 (delete-minibuffer-contents)
1064 (setq minibuffer-history-position narg)
1066 (setq elt minibuffer-default))
1068 (setq elt (or minibuffer-text-before-history ""))
1069 (setq minibuffer-returned-to-present t)
1070 (setq minibuffer-text-before-history nil))
1071 (t (setq elt (nth (1- minibuffer-history-position)
1072 (symbol-value minibuffer-history-variable)))))
1074 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
1075 (not minibuffer-returned-to-present))
1076 (let ((print-level nil))
1077 (prin1-to-string elt))
1079 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
1081 (defun previous-history-element (n)
1082 "Inserts the previous element of the minibuffer history into the minibuffer."
1084 (next-history-element (- n)))
1086 (defun next-complete-history-element (n)
1087 "Get next history element which completes the minibuffer before the point.
1088 The contents of the minibuffer after the point are deleted, and replaced
1089 by the new completion."
1091 (let ((point-at-start (point)))
1092 (next-matching-history-element
1094 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1096 ;; next-matching-history-element always puts us at (point-min).
1097 ;; Move to the position we were at before changing the buffer contents.
1098 ;; This is still sensical, because the text before point has not changed.
1099 (goto-char point-at-start)))
1101 (defun previous-complete-history-element (n)
1103 Get previous history element which completes the minibuffer before the point.
1104 The contents of the minibuffer after the point are deleted, and replaced
1105 by the new completion."
1107 (next-complete-history-element (- n)))
1109 ;; For compatibility with the old subr of the same name.
1110 (defun minibuffer-prompt-width ()
1111 "Return the display width of the minibuffer prompt.
1112 Return 0 if current buffer is not a mini-buffer."
1113 ;; Return the width of everything before the field at the end of
1114 ;; the buffer; this should be 0 for normal buffers.
1115 (1- (minibuffer-prompt-end)))
1117 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1118 (defalias 'advertised-undo 'undo)
1120 (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t)
1121 "Table mapping redo records to the corresponding undo one.")
1123 (defvar undo-in-region nil
1124 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1126 (defvar undo-no-redo nil
1127 "If t, `undo' doesn't go through redo entries.")
1129 (defun undo (&optional arg)
1130 "Undo some previous changes.
1131 Repeat this command to undo more changes.
1132 A numeric argument serves as a repeat count.
1134 In Transient Mark mode when the mark is active, only undo changes within
1135 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1136 as an argument limits undo to changes within the current region."
1138 ;; Make last-command indicate for the next command that this was an undo.
1139 ;; That way, another undo will undo more.
1140 ;; If we get to the end of the undo history and get an error,
1141 ;; another undo command will find the undo history empty
1142 ;; and will get another error. To begin undoing the undos,
1143 ;; you must type some other command.
1144 (let ((modified (buffer-modified-p))
1145 (recent-save (recent-auto-save-p)))
1146 ;; If we get an error in undo-start,
1147 ;; the next command should not be a "consecutive undo".
1148 ;; So set `this-command' to something other than `undo'.
1149 (setq this-command 'undo-start)
1151 (unless (eq last-command 'undo)
1152 (setq undo-in-region
1153 (if transient-mark-mode mark-active (and arg (not (numberp arg)))))
1155 (undo-start (region-beginning) (region-end))
1157 ;; get rid of initial undo boundary
1159 ;; If we got this far, the next command should be a consecutive undo.
1160 (setq this-command 'undo)
1161 ;; Check to see whether we're hitting a redo record, and if
1162 ;; so, ask the user whether she wants to skip the redo/undo pair.
1163 (let ((equiv (gethash pending-undo-list undo-equiv-table)))
1164 (or (eq (selected-window) (minibuffer-window))
1165 (message (if undo-in-region
1166 (if equiv "Redo in region!" "Undo in region!")
1167 (if equiv "Redo!" "Undo!"))))
1168 (when (and equiv undo-no-redo)
1169 ;; The equiv entry might point to another redo record if we have done
1170 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1171 (while (let ((next (gethash equiv undo-equiv-table)))
1172 (if next (setq equiv next))))
1173 (setq pending-undo-list equiv)))
1175 (if (or transient-mark-mode (numberp arg))
1176 (prefix-numeric-value arg)
1178 ;; Record the fact that the just-generated undo records come from an
1179 ;; undo operation, so we can skip them later on.
1180 ;; I don't know how to do that in the undo-in-region case.
1181 (unless undo-in-region
1182 (puthash buffer-undo-list pending-undo-list undo-equiv-table))
1183 ;; Don't specify a position in the undo record for the undo command.
1184 ;; Instead, undoing this should move point to where the change is.
1185 (let ((tail buffer-undo-list)
1188 (when (integerp (car tail))
1189 (let ((pos (car tail)))
1191 (setcdr prev (cdr tail))
1192 (setq buffer-undo-list (cdr tail)))
1193 (setq tail (cdr tail))
1195 (if (eq pos (car tail))
1197 (setcdr prev (cdr tail))
1198 (setq buffer-undo-list (cdr tail)))
1200 (setq tail (cdr tail)))
1202 (setq prev tail tail (cdr tail))))
1204 (and modified (not (buffer-modified-p))
1205 (delete-auto-save-file-if-necessary recent-save))))
1207 (defun undo-only (&optional arg)
1208 "Undo some previous changes.
1209 Repeat this command to undo more changes.
1210 A numeric argument serves as a repeat count.
1211 Contrary to `undo', this will not redo a previous undo."
1213 (let ((undo-no-redo t)) (undo arg)))
1214 ;; Richard said that we should not use C-x <uppercase letter> and I have
1215 ;; no idea whereas to bind it. Any suggestion welcome. -stef
1216 ;; (define-key ctl-x-map "U" 'undo-only)
1218 (defvar pending-undo-list nil
1219 "Within a run of consecutive undo commands, list remaining to be undone.")
1221 (defvar undo-in-progress nil
1222 "Non-nil while performing an undo.
1223 Some change-hooks test this variable to do something different.")
1225 (defun undo-more (count)
1226 "Undo back N undo-boundaries beyond what was already undone recently.
1227 Call `undo-start' to get ready to undo recent changes,
1228 then call `undo-more' one or more times to undo them."
1229 (or pending-undo-list
1230 (error (format "No further undo information%s"
1231 (if (and transient-mark-mode mark-active)
1232 " for region" ""))))
1233 (let ((undo-in-progress t))
1234 (setq pending-undo-list (primitive-undo count pending-undo-list))))
1236 ;; Deep copy of a list
1237 (defun undo-copy-list (list)
1238 "Make a copy of undo list LIST."
1239 (mapcar 'undo-copy-list-1 list))
1241 (defun undo-copy-list-1 (elt)
1243 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1246 (defun undo-start (&optional beg end)
1247 "Set `pending-undo-list' to the front of the undo list.
1248 The next call to `undo-more' will undo the most recently made change.
1249 If BEG and END are specified, then only undo elements
1250 that apply to text between BEG and END are used; other undo elements
1251 are ignored. If BEG and END are nil, all undo elements are used."
1252 (if (eq buffer-undo-list t)
1253 (error "No undo information in this buffer"))
1254 (setq pending-undo-list
1255 (if (and beg end (not (= beg end)))
1256 (undo-make-selective-list (min beg end) (max beg end))
1259 (defvar undo-adjusted-markers)
1261 (defun undo-make-selective-list (start end)
1262 "Return a list of undo elements for the region START to END.
1263 The elements come from `buffer-undo-list', but we keep only
1264 the elements inside this region, and discard those outside this region.
1265 If we find an element that crosses an edge of this region,
1266 we stop and ignore all further elements."
1267 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1268 (undo-list (list nil))
1269 undo-adjusted-markers
1271 undo-elt undo-elt temp-undo-list delta)
1272 (while undo-list-copy
1273 (setq undo-elt (car undo-list-copy))
1275 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1276 ;; This is a "was unmodified" element.
1277 ;; Keep it if we have kept everything thus far.
1278 (not some-rejected))
1280 (undo-elt-in-region undo-elt start end)))))
1283 (setq end (+ end (cdr (undo-delta undo-elt))))
1284 ;; Don't put two nils together in the list
1285 (if (not (and (eq (car undo-list) nil)
1287 (setq undo-list (cons undo-elt undo-list))))
1288 (if (undo-elt-crosses-region undo-elt start end)
1289 (setq undo-list-copy nil)
1290 (setq some-rejected t)
1291 (setq temp-undo-list (cdr undo-list-copy))
1292 (setq delta (undo-delta undo-elt))
1294 (when (/= (cdr delta) 0)
1295 (let ((position (car delta))
1296 (offset (cdr delta)))
1298 ;; Loop down the earlier events adjusting their buffer
1299 ;; positions to reflect the fact that a change to the buffer
1300 ;; isn't being undone. We only need to process those element
1301 ;; types which undo-elt-in-region will return as being in
1302 ;; the region since only those types can ever get into the
1305 (while temp-undo-list
1306 (setq undo-elt (car temp-undo-list))
1307 (cond ((integerp undo-elt)
1308 (if (>= undo-elt position)
1309 (setcar temp-undo-list (- undo-elt offset))))
1310 ((atom undo-elt) nil)
1311 ((stringp (car undo-elt))
1312 ;; (TEXT . POSITION)
1313 (let ((text-pos (abs (cdr undo-elt)))
1314 (point-at-end (< (cdr undo-elt) 0 )))
1315 (if (>= text-pos position)
1316 (setcdr undo-elt (* (if point-at-end -1 1)
1317 (- text-pos offset))))))
1318 ((integerp (car undo-elt))
1320 (when (>= (car undo-elt) position)
1321 (setcar undo-elt (- (car undo-elt) offset))
1322 (setcdr undo-elt (- (cdr undo-elt) offset))))
1323 ((null (car undo-elt))
1324 ;; (nil PROPERTY VALUE BEG . END)
1325 (let ((tail (nthcdr 3 undo-elt)))
1326 (when (>= (car tail) position)
1327 (setcar tail (- (car tail) offset))
1328 (setcdr tail (- (cdr tail) offset))))))
1329 (setq temp-undo-list (cdr temp-undo-list))))))))
1330 (setq undo-list-copy (cdr undo-list-copy)))
1331 (nreverse undo-list)))
1333 (defun undo-elt-in-region (undo-elt start end)
1334 "Determine whether UNDO-ELT falls inside the region START ... END.
1335 If it crosses the edge, we return nil."
1336 (cond ((integerp undo-elt)
1337 (and (>= undo-elt start)
1343 ((stringp (car undo-elt))
1344 ;; (TEXT . POSITION)
1345 (and (>= (abs (cdr undo-elt)) start)
1346 (< (abs (cdr undo-elt)) end)))
1347 ((and (consp undo-elt) (markerp (car undo-elt)))
1348 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1349 ;; See if MARKER is inside the region.
1350 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1352 (setq alist-elt (cons (car undo-elt)
1353 (marker-position (car undo-elt))))
1354 (setq undo-adjusted-markers
1355 (cons alist-elt undo-adjusted-markers)))
1356 (and (cdr alist-elt)
1357 (>= (cdr alist-elt) start)
1358 (<= (cdr alist-elt) end))))
1359 ((null (car undo-elt))
1360 ;; (nil PROPERTY VALUE BEG . END)
1361 (let ((tail (nthcdr 3 undo-elt)))
1362 (and (>= (car tail) start)
1363 (<= (cdr tail) end))))
1364 ((integerp (car undo-elt))
1366 (and (>= (car undo-elt) start)
1367 (<= (cdr undo-elt) end)))))
1369 (defun undo-elt-crosses-region (undo-elt start end)
1370 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1371 This assumes we have already decided that UNDO-ELT
1372 is not *inside* the region START...END."
1373 (cond ((atom undo-elt) nil)
1374 ((null (car undo-elt))
1375 ;; (nil PROPERTY VALUE BEG . END)
1376 (let ((tail (nthcdr 3 undo-elt)))
1377 (not (or (< (car tail) end)
1378 (> (cdr tail) start)))))
1379 ((integerp (car undo-elt))
1381 (not (or (< (car undo-elt) end)
1382 (> (cdr undo-elt) start))))))
1384 ;; Return the first affected buffer position and the delta for an undo element
1385 ;; delta is defined as the change in subsequent buffer positions if we *did*
1387 (defun undo-delta (undo-elt)
1388 (if (consp undo-elt)
1389 (cond ((stringp (car undo-elt))
1390 ;; (TEXT . POSITION)
1391 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1392 ((integerp (car undo-elt))
1394 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1399 (defvar shell-command-history nil
1400 "History list for some commands that read shell commands.")
1402 (defvar shell-command-switch "-c"
1403 "Switch used to have the shell execute its command line argument.")
1405 (defvar shell-command-default-error-buffer nil
1406 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1407 This buffer is used when `shell-command' or `shell-command-on-region'
1408 is run interactively. A value of nil means that output to stderr and
1409 stdout will be intermixed in the output stream.")
1411 (defun shell-command (command &optional output-buffer error-buffer)
1412 "Execute string COMMAND in inferior shell; display output, if any.
1413 With prefix argument, insert the COMMAND's output at point.
1415 If COMMAND ends in ampersand, execute it asynchronously.
1416 The output appears in the buffer `*Async Shell Command*'.
1417 That buffer is in shell mode.
1419 Otherwise, COMMAND is executed synchronously. The output appears in
1420 the buffer `*Shell Command Output*'. If the output is short enough to
1421 display in the echo area (which is determined by the variables
1422 `resize-mini-windows' and `max-mini-window-height'), it is shown
1423 there, but it is nonetheless available in buffer `*Shell Command
1424 Output*' even though that buffer is not automatically displayed.
1426 To specify a coding system for converting non-ASCII characters
1427 in the shell command output, use \\[universal-coding-system-argument]
1428 before this command.
1430 Noninteractive callers can specify coding systems by binding
1431 `coding-system-for-read' and `coding-system-for-write'.
1433 The optional second argument OUTPUT-BUFFER, if non-nil,
1434 says to put the output in some other buffer.
1435 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1436 If OUTPUT-BUFFER is not a buffer and not nil,
1437 insert output in current buffer. (This cannot be done asynchronously.)
1438 In either case, the output is inserted after point (leaving mark after it).
1440 If the command terminates without error, but generates output,
1441 and you did not specify \"insert it in the current buffer\",
1442 the output can be displayed in the echo area or in its buffer.
1443 If the output is short enough to display in the echo area
1444 \(determined by the variable `max-mini-window-height' if
1445 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1446 the buffer containing the output is displayed.
1448 If there is output and an error, and you did not specify \"insert it
1449 in the current buffer\", a message about the error goes at the end
1452 If there is no output, or if output is inserted in the current buffer,
1453 then `*Shell Command Output*' is deleted.
1455 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1456 or buffer name to which to direct the command's standard error output.
1457 If it is nil, error output is mingled with regular output.
1458 In an interactive call, the variable `shell-command-default-error-buffer'
1459 specifies the value of ERROR-BUFFER."
1461 (interactive (list (read-from-minibuffer "Shell command: "
1462 nil nil nil 'shell-command-history)
1464 shell-command-default-error-buffer))
1465 ;; Look for a handler in case default-directory is a remote file name.
1467 (find-file-name-handler (directory-file-name default-directory)
1470 (funcall handler 'shell-command command output-buffer error-buffer)
1471 (if (and output-buffer
1472 (not (or (bufferp output-buffer) (stringp output-buffer))))
1473 ;; Output goes in current buffer.
1477 (expand-file-name "scor"
1478 (or small-temporary-file-directory
1479 temporary-file-directory)))
1481 (barf-if-buffer-read-only)
1483 ;; We do not use -f for csh; we will not support broken use of
1484 ;; .cshrcs. Even the BSD csh manual says to use
1485 ;; "if ($?prompt) exit" before things which are not useful
1486 ;; non-interactively. Besides, if someone wants their other
1487 ;; aliases for shell commands then they can still have them.
1488 (call-process shell-file-name nil
1492 nil shell-command-switch command)
1493 (when (and error-file (file-exists-p error-file))
1494 (if (< 0 (nth 7 (file-attributes error-file)))
1495 (with-current-buffer (get-buffer-create error-buffer)
1496 (let ((pos-from-end (- (point-max) (point))))
1499 ;; Do no formatting while reading error file,
1500 ;; because that can run a shell command, and we
1501 ;; don't want that to cause an infinite recursion.
1502 (format-insert-file error-file nil)
1503 ;; Put point after the inserted errors.
1504 (goto-char (- (point-max) pos-from-end)))
1505 (display-buffer (current-buffer))))
1506 (delete-file error-file))
1507 ;; This is like exchange-point-and-mark, but doesn't
1508 ;; activate the mark. It is cleaner to avoid activation,
1509 ;; even though the command loop would deactivate the mark
1510 ;; because we inserted text.
1511 (goto-char (prog1 (mark t)
1512 (set-marker (mark-marker) (point)
1513 (current-buffer)))))
1514 ;; Output goes in a separate buffer.
1515 ;; Preserve the match data in case called from a program.
1517 (if (string-match "[ \t]*&[ \t]*\\'" command)
1518 ;; Command ending with ampersand means asynchronous.
1519 (let ((buffer (get-buffer-create
1520 (or output-buffer "*Async Shell Command*")))
1521 (directory default-directory)
1523 ;; Remove the ampersand.
1524 (setq command (substring command 0 (match-beginning 0)))
1525 ;; If will kill a process, query first.
1526 (setq proc (get-buffer-process buffer))
1528 (if (yes-or-no-p "A command is running. Kill it? ")
1530 (error "Shell command in progress")))
1531 (with-current-buffer buffer
1532 (setq buffer-read-only nil)
1534 (display-buffer buffer)
1535 (setq default-directory directory)
1536 (setq proc (start-process "Shell" buffer shell-file-name
1537 shell-command-switch command))
1538 (setq mode-line-process '(":%s"))
1539 (require 'shell) (shell-mode)
1540 (set-process-sentinel proc 'shell-command-sentinel)
1542 (shell-command-on-region (point) (point) command
1543 output-buffer nil error-buffer)))))))
1545 (defun display-message-or-buffer (message
1546 &optional buffer-name not-this-window frame)
1547 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1548 MESSAGE may be either a string or a buffer.
1550 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1551 the maximum height of the echo area, as defined by `max-mini-window-height'
1552 if `resize-mini-windows' is non-nil.
1554 Returns either the string shown in the echo area, or when a pop-up
1555 buffer is used, the window used to display it.
1557 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1558 name of the buffer used to display it in the case where a pop-up buffer
1559 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1560 string and it is displayed in the echo area, it is not specified whether
1561 the contents are inserted into the buffer anyway.
1563 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1564 and only used if a buffer is displayed."
1565 (cond ((and (stringp message) (not (string-match "\n" message)))
1566 ;; Trivial case where we can use the echo area
1567 (message "%s" message))
1568 ((and (stringp message)
1569 (= (string-match "\n" message) (1- (length message))))
1570 ;; Trivial case where we can just remove single trailing newline
1571 (message "%s" (substring message 0 (1- (length message)))))
1574 (with-current-buffer
1575 (if (bufferp message)
1577 (get-buffer-create (or buffer-name "*Message*")))
1579 (unless (bufferp message)
1584 (if (= (buffer-size) 0)
1586 (count-lines (point-min) (point-max)))))
1588 ((and (or (<= lines 1)
1590 (if resize-mini-windows
1591 (cond ((floatp max-mini-window-height)
1593 max-mini-window-height))
1594 ((integerp max-mini-window-height)
1595 max-mini-window-height)
1599 ;; Don't use the echo area if the output buffer is
1600 ;; already dispayed in the selected frame.
1601 (not (get-buffer-window (current-buffer))))
1603 (goto-char (point-max))
1606 (message "%s" (buffer-substring (point-min) (point))))
1609 (goto-char (point-min))
1610 (display-buffer (current-buffer)
1611 not-this-window frame))))))))
1614 ;; We have a sentinel to prevent insertion of a termination message
1615 ;; in the buffer itself.
1616 (defun shell-command-sentinel (process signal)
1617 (if (memq (process-status process) '(exit signal))
1619 (car (cdr (cdr (process-command process))))
1620 (substring signal 0 -1))))
1622 (defun shell-command-on-region (start end command
1623 &optional output-buffer replace
1625 "Execute string COMMAND in inferior shell with region as input.
1626 Normally display output (if any) in temp buffer `*Shell Command Output*';
1627 Prefix arg means replace the region with it. Return the exit code of
1630 To specify a coding system for converting non-ASCII characters
1631 in the input and output to the shell command, use \\[universal-coding-system-argument]
1632 before this command. By default, the input (from the current buffer)
1633 is encoded in the same coding system that will be used to save the file,
1634 `buffer-file-coding-system'. If the output is going to replace the region,
1635 then it is decoded from that same coding system.
1637 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1638 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1639 systems by binding `coding-system-for-read' and
1640 `coding-system-for-write'.
1642 If the command generates output, the output may be displayed
1643 in the echo area or in a buffer.
1644 If the output is short enough to display in the echo area
1645 \(determined by the variable `max-mini-window-height' if
1646 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1647 it is displayed in the buffer `*Shell Command Output*'. The output
1648 is available in that buffer in both cases.
1650 If there is output and an error, a message about the error
1651 appears at the end of the output.
1653 If there is no output, or if output is inserted in the current buffer,
1654 then `*Shell Command Output*' is deleted.
1656 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1657 that says to put the output in some other buffer.
1658 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1659 If OUTPUT-BUFFER is not a buffer and not nil,
1660 insert output in the current buffer.
1661 In either case, the output is inserted after point (leaving mark after it).
1663 If REPLACE, the optional fifth argument, is non-nil, that means insert
1664 the output in place of text from START to END, putting point and mark
1667 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1668 or buffer name to which to direct the command's standard error output.
1669 If it is nil, error output is mingled with regular output.
1670 In an interactive call, the variable `shell-command-default-error-buffer'
1671 specifies the value of ERROR-BUFFER."
1672 (interactive (let (string)
1674 (error "The mark is not set now, so there is no region"))
1675 ;; Do this before calling region-beginning
1676 ;; and region-end, in case subprocess output
1677 ;; relocates them while we are in the minibuffer.
1678 (setq string (read-from-minibuffer "Shell command on region: "
1680 'shell-command-history))
1681 ;; call-interactively recognizes region-beginning and
1682 ;; region-end specially, leaving them in the history.
1683 (list (region-beginning) (region-end)
1687 shell-command-default-error-buffer)))
1691 (expand-file-name "scor"
1692 (or small-temporary-file-directory
1693 temporary-file-directory)))
1698 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1699 ;; Replace specified region with output from command.
1700 (let ((swap (and replace (< start end))))
1701 ;; Don't muck with mark unless REPLACE says we should.
1703 (and replace (push-mark (point) 'nomsg))
1705 (call-process-region start end shell-file-name t
1709 nil shell-command-switch command))
1710 ;; It is rude to delete a buffer which the command is not using.
1711 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1712 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1713 ;; (kill-buffer shell-buffer)))
1714 ;; Don't muck with mark unless REPLACE says we should.
1715 (and replace swap (exchange-point-and-mark)))
1716 ;; No prefix argument: put the output in a temp buffer,
1717 ;; replacing its entire contents.
1718 (let ((buffer (get-buffer-create
1719 (or output-buffer "*Shell Command Output*"))))
1721 (if (eq buffer (current-buffer))
1722 ;; If the input is the same buffer as the output,
1723 ;; delete everything but the specified region,
1724 ;; then replace that region with the output.
1725 (progn (setq buffer-read-only nil)
1726 (delete-region (max start end) (point-max))
1727 (delete-region (point-min) (min start end))
1729 (call-process-region (point-min) (point-max)
1734 nil shell-command-switch
1736 ;; Clear the output buffer, then run the command with
1738 (let ((directory default-directory))
1741 (setq buffer-read-only nil)
1742 (if (not output-buffer)
1743 (setq default-directory directory))
1746 (call-process-region start end shell-file-name nil
1748 (list buffer error-file)
1750 nil shell-command-switch command)))
1751 ;; Report the output.
1752 (with-current-buffer buffer
1753 (setq mode-line-process
1754 (cond ((null exit-status)
1756 ((stringp exit-status)
1757 (format " - Signal [%s]" exit-status))
1758 ((not (equal 0 exit-status))
1759 (format " - Exit [%d]" exit-status)))))
1760 (if (with-current-buffer buffer (> (point-max) (point-min)))
1761 ;; There's some output, display it
1762 (display-message-or-buffer buffer)
1763 ;; No output; error?
1766 (< 0 (nth 7 (file-attributes error-file))))
1769 (cond ((null exit-status)
1770 (message "(Shell command failed with error)"))
1771 ((equal 0 exit-status)
1772 (message "(Shell command succeeded with %s)"
1774 ((stringp exit-status)
1775 (message "(Shell command killed by signal %s)"
1778 (message "(Shell command failed with code %d and %s)"
1779 exit-status output))))
1780 ;; Don't kill: there might be useful info in the undo-log.
1781 ;; (kill-buffer buffer)
1784 (when (and error-file (file-exists-p error-file))
1785 (if (< 0 (nth 7 (file-attributes error-file)))
1786 (with-current-buffer (get-buffer-create error-buffer)
1787 (let ((pos-from-end (- (point-max) (point))))
1790 ;; Do no formatting while reading error file,
1791 ;; because that can run a shell command, and we
1792 ;; don't want that to cause an infinite recursion.
1793 (format-insert-file error-file nil)
1794 ;; Put point after the inserted errors.
1795 (goto-char (- (point-max) pos-from-end)))
1796 (display-buffer (current-buffer))))
1797 (delete-file error-file))
1800 (defun shell-command-to-string (command)
1801 "Execute shell command COMMAND and return its output as a string."
1802 (with-output-to-string
1803 (with-current-buffer
1805 (call-process shell-file-name nil t nil shell-command-switch command))))
1807 (defvar universal-argument-map
1808 (let ((map (make-sparse-keymap)))
1809 (define-key map [t] 'universal-argument-other-key)
1810 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1811 (define-key map [switch-frame] nil)
1812 (define-key map [?\C-u] 'universal-argument-more)
1813 (define-key map [?-] 'universal-argument-minus)
1814 (define-key map [?0] 'digit-argument)
1815 (define-key map [?1] 'digit-argument)
1816 (define-key map [?2] 'digit-argument)
1817 (define-key map [?3] 'digit-argument)
1818 (define-key map [?4] 'digit-argument)
1819 (define-key map [?5] 'digit-argument)
1820 (define-key map [?6] 'digit-argument)
1821 (define-key map [?7] 'digit-argument)
1822 (define-key map [?8] 'digit-argument)
1823 (define-key map [?9] 'digit-argument)
1824 (define-key map [kp-0] 'digit-argument)
1825 (define-key map [kp-1] 'digit-argument)
1826 (define-key map [kp-2] 'digit-argument)
1827 (define-key map [kp-3] 'digit-argument)
1828 (define-key map [kp-4] 'digit-argument)
1829 (define-key map [kp-5] 'digit-argument)
1830 (define-key map [kp-6] 'digit-argument)
1831 (define-key map [kp-7] 'digit-argument)
1832 (define-key map [kp-8] 'digit-argument)
1833 (define-key map [kp-9] 'digit-argument)
1834 (define-key map [kp-subtract] 'universal-argument-minus)
1836 "Keymap used while processing \\[universal-argument].")
1838 (defvar universal-argument-num-events nil
1839 "Number of argument-specifying events read by `universal-argument'.
1840 `universal-argument-other-key' uses this to discard those events
1841 from (this-command-keys), and reread only the final command.")
1843 (defvar overriding-map-is-bound nil
1844 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
1846 (defvar saved-overriding-map nil
1847 "The saved value of `overriding-terminal-local-map'.
1848 That variable gets restored to this value on exiting \"universal
1851 (defun ensure-overriding-map-is-bound ()
1852 "Check `overriding-terminal-local-map' is `universal-argument-map'."
1853 (unless overriding-map-is-bound
1854 (setq saved-overriding-map overriding-terminal-local-map)
1855 (setq overriding-terminal-local-map universal-argument-map)
1856 (setq overriding-map-is-bound t)))
1858 (defun restore-overriding-map ()
1859 "Restore `overriding-terminal-local-map' to its saved value."
1860 (setq overriding-terminal-local-map saved-overriding-map)
1861 (setq overriding-map-is-bound nil))
1863 (defun universal-argument ()
1864 "Begin a numeric argument for the following command.
1865 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1866 \\[universal-argument] following the digits or minus sign ends the argument.
1867 \\[universal-argument] without digits or minus sign provides 4 as argument.
1868 Repeating \\[universal-argument] without digits or minus sign
1869 multiplies the argument by 4 each time.
1870 For some commands, just \\[universal-argument] by itself serves as a flag
1871 which is different in effect from any particular numeric argument.
1872 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1874 (setq prefix-arg (list 4))
1875 (setq universal-argument-num-events (length (this-command-keys)))
1876 (ensure-overriding-map-is-bound))
1878 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1879 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1880 (defun universal-argument-more (arg)
1883 (setq prefix-arg (list (* 4 (car arg))))
1885 (setq prefix-arg (list -4))
1886 (setq prefix-arg arg)
1887 (restore-overriding-map)))
1888 (setq universal-argument-num-events (length (this-command-keys))))
1890 (defun negative-argument (arg)
1891 "Begin a negative numeric argument for the next command.
1892 \\[universal-argument] following digits or minus sign ends the argument."
1894 (cond ((integerp arg)
1895 (setq prefix-arg (- arg)))
1897 (setq prefix-arg nil))
1899 (setq prefix-arg '-)))
1900 (setq universal-argument-num-events (length (this-command-keys)))
1901 (ensure-overriding-map-is-bound))
1903 (defun digit-argument (arg)
1904 "Part of the numeric argument for the next command.
1905 \\[universal-argument] following digits or minus sign ends the argument."
1907 (let* ((char (if (integerp last-command-char)
1909 (get last-command-char 'ascii-character)))
1910 (digit (- (logand char ?\177) ?0)))
1911 (cond ((integerp arg)
1912 (setq prefix-arg (+ (* arg 10)
1913 (if (< arg 0) (- digit) digit))))
1915 ;; Treat -0 as just -, so that -01 will work.
1916 (setq prefix-arg (if (zerop digit) '- (- digit))))
1918 (setq prefix-arg digit))))
1919 (setq universal-argument-num-events (length (this-command-keys)))
1920 (ensure-overriding-map-is-bound))
1922 ;; For backward compatibility, minus with no modifiers is an ordinary
1923 ;; command if digits have already been entered.
1924 (defun universal-argument-minus (arg)
1927 (universal-argument-other-key arg)
1928 (negative-argument arg)))
1930 ;; Anything else terminates the argument and is left in the queue to be
1931 ;; executed as a command.
1932 (defun universal-argument-other-key (arg)
1934 (setq prefix-arg arg)
1935 (let* ((key (this-command-keys))
1936 (keylist (listify-key-sequence key)))
1937 (setq unread-command-events
1938 (append (nthcdr universal-argument-num-events keylist)
1939 unread-command-events)))
1940 (reset-this-command-lengths)
1941 (restore-overriding-map))
1943 ;;;; Window system cut and paste hooks.
1945 (defvar interprogram-cut-function nil
1946 "Function to call to make a killed region available to other programs.
1948 Most window systems provide some sort of facility for cutting and
1949 pasting text between the windows of different programs.
1950 This variable holds a function that Emacs calls whenever text
1951 is put in the kill ring, to make the new kill available to other
1954 The function takes one or two arguments.
1955 The first argument, TEXT, is a string containing
1956 the text which should be made available.
1957 The second, optional, argument PUSH, has the same meaning as the
1958 similar argument to `x-set-cut-buffer', which see.")
1960 (defvar interprogram-paste-function nil
1961 "Function to call to get text cut from other programs.
1963 Most window systems provide some sort of facility for cutting and
1964 pasting text between the windows of different programs.
1965 This variable holds a function that Emacs calls to obtain
1966 text that other programs have provided for pasting.
1968 The function should be called with no arguments. If the function
1969 returns nil, then no other program has provided such text, and the top
1970 of the Emacs kill ring should be used. If the function returns a
1971 string, then the caller of the function \(usually `current-kill')
1972 should put this string in the kill ring as the latest kill.
1974 Note that the function should return a string only if a program other
1975 than Emacs has provided a string for pasting; if Emacs provided the
1976 most recent string, the function should return nil. If it is
1977 difficult to tell whether Emacs or some other program provided the
1978 current string, it is probably good enough to return nil if the string
1979 is equal (according to `string=') to the last text Emacs provided.")
1983 ;;;; The kill ring data structure.
1985 (defvar kill-ring nil
1986 "List of killed text sequences.
1987 Since the kill ring is supposed to interact nicely with cut-and-paste
1988 facilities offered by window systems, use of this variable should
1989 interact nicely with `interprogram-cut-function' and
1990 `interprogram-paste-function'. The functions `kill-new',
1991 `kill-append', and `current-kill' are supposed to implement this
1992 interaction; you may want to use them instead of manipulating the kill
1995 (defcustom kill-ring-max 60
1996 "*Maximum length of kill ring before oldest elements are thrown away."
2000 (defvar kill-ring-yank-pointer nil
2001 "The tail of the kill ring whose car is the last thing yanked.")
2003 (defun kill-new (string &optional replace yank-handler)
2004 "Make STRING the latest kill in the kill ring.
2005 Set `kill-ring-yank-pointer' to point to it.
2006 If `interprogram-cut-function' is non-nil, apply it to STRING.
2007 Optional second argument REPLACE non-nil means that STRING will replace
2008 the front of the kill ring, rather than being added to the list.
2010 Optional third arguments YANK-HANDLER controls how the STRING is later
2011 inserted into a buffer; see `insert-for-yank' for details.
2012 When a yank handler is specified, STRING must be non-empty (the yank
2013 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2015 When the yank handler has a non-nil PARAM element, the original STRING
2016 argument is not used by `insert-for-yank'. However, since Lisp code
2017 may access and use elements from the kill-ring directly, the STRING
2018 argument should still be a \"useful\" string for such uses."
2019 (if (> (length string) 0)
2021 (put-text-property 0 (length string)
2022 'yank-handler yank-handler string))
2024 (signal 'args-out-of-range
2025 (list string "yank-handler specified for empty string"))))
2026 (if (fboundp 'menu-bar-update-yank-menu)
2027 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2028 (if (and replace kill-ring)
2029 (setcar kill-ring string)
2030 (setq kill-ring (cons string kill-ring))
2031 (if (> (length kill-ring) kill-ring-max)
2032 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2033 (setq kill-ring-yank-pointer kill-ring)
2034 (if interprogram-cut-function
2035 (funcall interprogram-cut-function string (not replace))))
2037 (defun kill-append (string before-p &optional yank-handler)
2038 "Append STRING to the end of the latest kill in the kill ring.
2039 If BEFORE-P is non-nil, prepend STRING to the kill.
2040 Optional third argument YANK-HANDLER, if non-nil, specifies the
2041 yank-handler text property to be set on the combined kill ring
2042 string. If the specified yank-handler arg differs from the
2043 yank-handler property of the latest kill string, this function
2044 adds the combined string to the kill ring as a new element,
2045 instead of replacing the last kill with it.
2046 If `interprogram-cut-function' is set, pass the resulting kill to it."
2047 (let* ((cur (car kill-ring)))
2048 (kill-new (if before-p (concat string cur) (concat cur string))
2049 (or (= (length cur) 0)
2050 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2053 (defun current-kill (n &optional do-not-move)
2054 "Rotate the yanking point by N places, and then return that kill.
2055 If N is zero, `interprogram-paste-function' is set, and calling it
2056 returns a string, then that string is added to the front of the
2057 kill ring and returned as the latest kill.
2058 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
2059 yanking point; just return the Nth kill forward."
2060 (let ((interprogram-paste (and (= n 0)
2061 interprogram-paste-function
2062 (funcall interprogram-paste-function))))
2063 (if interprogram-paste
2065 ;; Disable the interprogram cut function when we add the new
2066 ;; text to the kill ring, so Emacs doesn't try to own the
2067 ;; selection, with identical text.
2068 (let ((interprogram-cut-function nil))
2069 (kill-new interprogram-paste))
2071 (or kill-ring (error "Kill ring is empty"))
2072 (let ((ARGth-kill-element
2073 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2077 (setq kill-ring-yank-pointer ARGth-kill-element))
2078 (car ARGth-kill-element)))))
2082 ;;;; Commands for manipulating the kill ring.
2084 (defcustom kill-read-only-ok nil
2085 "*Non-nil means don't signal an error for killing read-only text."
2089 (put 'text-read-only 'error-conditions
2090 '(text-read-only buffer-read-only error))
2091 (put 'text-read-only 'error-message "Text is read-only")
2093 (defun kill-region (beg end &optional yank-handler)
2094 "Kill between point and mark.
2095 The text is deleted but saved in the kill ring.
2096 The command \\[yank] can retrieve it from there.
2097 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2099 If you want to append the killed region to the last killed text,
2100 use \\[append-next-kill] before \\[kill-region].
2102 If the buffer is read-only, Emacs will beep and refrain from deleting
2103 the text, but put the text in the kill ring anyway. This means that
2104 you can use the killing commands to copy text from a read-only buffer.
2106 This is the primitive for programs to kill text (as opposed to deleting it).
2107 Supply two arguments, character positions indicating the stretch of text
2109 Any command that calls this function is a \"kill command\".
2110 If the previous command was also a kill command,
2111 the text killed this time appends to the text killed last time
2112 to make one entry in the kill ring.
2114 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2115 specifies the yank-handler text property to be set on the killed
2116 text. See `insert-for-yank'."
2119 (let ((string (delete-and-extract-region beg end)))
2120 (when string ;STRING is nil if BEG = END
2121 ;; Add that string to the kill ring, one way or another.
2122 (if (eq last-command 'kill-region)
2123 (kill-append string (< end beg) yank-handler)
2124 (kill-new string nil yank-handler)))
2125 (when (or string (eq last-command 'kill-region))
2126 (setq this-command 'kill-region))
2128 ((buffer-read-only text-read-only)
2129 ;; The code above failed because the buffer, or some of the characters
2130 ;; in the region, are read-only.
2131 ;; We should beep, in case the user just isn't aware of this.
2132 ;; However, there's no harm in putting
2133 ;; the region's text in the kill ring, anyway.
2134 (copy-region-as-kill beg end)
2135 ;; Set this-command now, so it will be set even if we get an error.
2136 (setq this-command 'kill-region)
2137 ;; This should barf, if appropriate, and give us the correct error.
2138 (if kill-read-only-ok
2139 (progn (message "Read only text copied to kill ring") nil)
2140 ;; Signal an error if the buffer is read-only.
2141 (barf-if-buffer-read-only)
2142 ;; If the buffer isn't read-only, the text is.
2143 (signal 'text-read-only (list (current-buffer)))))))
2145 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2146 ;; to get two copies of the text when the user accidentally types M-w and
2147 ;; then corrects it with the intended C-w.
2148 (defun copy-region-as-kill (beg end)
2149 "Save the region as if killed, but don't kill it.
2150 In Transient Mark mode, deactivate the mark.
2151 If `interprogram-cut-function' is non-nil, also save the text for a window
2152 system cut and paste."
2154 (if (eq last-command 'kill-region)
2155 (kill-append (buffer-substring beg end) (< end beg))
2156 (kill-new (buffer-substring beg end)))
2157 (if transient-mark-mode
2158 (setq deactivate-mark t))
2161 (defun kill-ring-save (beg end)
2162 "Save the region as if killed, but don't kill it.
2163 In Transient Mark mode, deactivate the mark.
2164 If `interprogram-cut-function' is non-nil, also save the text for a window
2165 system cut and paste.
2167 If you want to append the killed line to the last killed text,
2168 use \\[append-next-kill] before \\[kill-ring-save].
2170 This command is similar to `copy-region-as-kill', except that it gives
2171 visual feedback indicating the extent of the region being copied."
2173 (copy-region-as-kill beg end)
2175 (let ((other-end (if (= (point) beg) end beg))
2177 ;; Inhibit quitting so we can make a quit here
2178 ;; look like a C-g typed as a command.
2180 (if (pos-visible-in-window-p other-end (selected-window))
2181 (unless (and transient-mark-mode
2182 (face-background 'region))
2183 ;; Swap point and mark.
2184 (set-marker (mark-marker) (point) (current-buffer))
2185 (goto-char other-end)
2186 (sit-for blink-matching-delay)
2188 (set-marker (mark-marker) other-end (current-buffer))
2190 ;; If user quit, deactivate the mark
2191 ;; as C-g would as a command.
2192 (and quit-flag mark-active
2194 (let* ((killed-text (current-kill 0))
2195 (message-len (min (length killed-text) 40)))
2197 ;; Don't say "killed"; that is misleading.
2198 (message "Saved text until \"%s\""
2199 (substring killed-text (- message-len)))
2200 (message "Saved text from \"%s\""
2201 (substring killed-text 0 message-len))))))))
2203 (defun append-next-kill (&optional interactive)
2204 "Cause following command, if it kills, to append to previous kill.
2205 The argument is used for internal purposes; do not supply one."
2207 ;; We don't use (interactive-p), since that breaks kbd macros.
2210 (setq this-command 'kill-region)
2211 (message "If the next command is a kill, it will append"))
2212 (setq last-command 'kill-region)))
2216 ;; This is actually used in subr.el but defcustom does not work there.
2217 (defcustom yank-excluded-properties
2218 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2220 "*Text properties to discard when yanking.
2221 The value should be a list of text properties to discard or t,
2222 which means to discard all text properties."
2223 :type '(choice (const :tag "All" t) (repeat symbol))
2227 (defvar yank-window-start nil)
2228 (defvar yank-undo-function nil
2229 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2230 Function is called with two parameters, START and END corresponding to
2231 the value of the mark and point; it is guaranteed that START <= END.
2232 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2234 (defun yank-pop (&optional arg)
2235 "Replace just-yanked stretch of killed text with a different stretch.
2236 This command is allowed only immediately after a `yank' or a `yank-pop'.
2237 At such a time, the region contains a stretch of reinserted
2238 previously-killed text. `yank-pop' deletes that text and inserts in its
2239 place a different stretch of killed text.
2241 With no argument, the previous kill is inserted.
2242 With argument N, insert the Nth previous kill.
2243 If N is negative, this is a more recent kill.
2245 The sequence of kills wraps around, so that after the oldest one
2246 comes the newest one."
2248 (if (not (eq last-command 'yank))
2249 (error "Previous command was not a yank"))
2250 (setq this-command 'yank)
2251 (unless arg (setq arg 1))
2252 (let ((inhibit-read-only t)
2253 (before (< (point) (mark t))))
2255 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2256 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2257 (setq yank-undo-function nil)
2258 (set-marker (mark-marker) (point) (current-buffer))
2259 (insert-for-yank (current-kill arg))
2260 ;; Set the window start back where it was in the yank command,
2262 (set-window-start (selected-window) yank-window-start t)
2264 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2265 ;; It is cleaner to avoid activation, even though the command
2266 ;; loop would deactivate the mark because we inserted text.
2267 (goto-char (prog1 (mark t)
2268 (set-marker (mark-marker) (point) (current-buffer))))))
2271 (defun yank (&optional arg)
2272 "Reinsert the last stretch of killed text.
2273 More precisely, reinsert the stretch of killed text most recently
2274 killed OR yanked. Put point at end, and set mark at beginning.
2275 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2276 With argument N, reinsert the Nth most recently killed stretch of killed
2278 See also the command \\[yank-pop]."
2280 (setq yank-window-start (window-start))
2281 ;; If we don't get all the way thru, make last-command indicate that
2282 ;; for the following command.
2283 (setq this-command t)
2285 (insert-for-yank (current-kill (cond
2290 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2291 ;; It is cleaner to avoid activation, even though the command
2292 ;; loop would deactivate the mark because we inserted text.
2293 (goto-char (prog1 (mark t)
2294 (set-marker (mark-marker) (point) (current-buffer)))))
2295 ;; If we do get all the way thru, make this-command indicate that.
2296 (if (eq this-command t)
2297 (setq this-command 'yank))
2300 (defun rotate-yank-pointer (arg)
2301 "Rotate the yanking point in the kill ring.
2302 With argument, rotate that many kills forward (or backward, if negative)."
2306 ;; Some kill commands.
2308 ;; Internal subroutine of delete-char
2309 (defun kill-forward-chars (arg)
2310 (if (listp arg) (setq arg (car arg)))
2311 (if (eq arg '-) (setq arg -1))
2312 (kill-region (point) (forward-point arg)))
2314 ;; Internal subroutine of backward-delete-char
2315 (defun kill-backward-chars (arg)
2316 (if (listp arg) (setq arg (car arg)))
2317 (if (eq arg '-) (setq arg -1))
2318 (kill-region (point) (forward-point (- arg))))
2320 (defcustom backward-delete-char-untabify-method 'untabify
2321 "*The method for untabifying when deleting backward.
2322 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2323 `hungry' -- delete all whitespace, both tabs and spaces;
2324 `all' -- delete all whitespace, including tabs, spaces and newlines;
2325 nil -- just delete one character."
2326 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2330 (defun backward-delete-char-untabify (arg &optional killp)
2331 "Delete characters backward, changing tabs into spaces.
2332 The exact behavior depends on `backward-delete-char-untabify-method'.
2333 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2334 Interactively, ARG is the prefix arg (default 1)
2335 and KILLP is t if a prefix arg was specified."
2336 (interactive "*p\nP")
2337 (when (eq backward-delete-char-untabify-method 'untabify)
2340 (while (and (> count 0) (not (bobp)))
2341 (if (= (preceding-char) ?\t)
2342 (let ((col (current-column)))
2344 (setq col (- col (current-column)))
2345 (insert-char ?\ col)
2348 (setq count (1- count))))))
2349 (delete-backward-char
2350 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2351 ((eq backward-delete-char-untabify-method 'all)
2354 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2356 (+ arg (if (zerop wh) 0 (1- wh))))
2360 (defun zap-to-char (arg char)
2361 "Kill up to and including ARG'th occurrence of CHAR.
2362 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2363 Goes backward if ARG is negative; error if CHAR not found."
2364 (interactive "p\ncZap to char: ")
2365 (kill-region (point) (progn
2366 (search-forward (char-to-string char) nil nil arg)
2367 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2370 ;; kill-line and its subroutines.
2372 (defcustom kill-whole-line nil
2373 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2377 (defun kill-line (&optional arg)
2378 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2379 With prefix argument, kill that many lines from point.
2380 Negative arguments kill lines backward.
2381 With zero argument, kills the text before point on the current line.
2383 When calling from a program, nil means \"no arg\",
2384 a number counts as a prefix arg.
2386 To kill a whole line, when point is not at the beginning, type \
2387 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2389 If `kill-whole-line' is non-nil, then this command kills the whole line
2390 including its terminating newline, when used at the beginning of a line
2391 with no argument. As a consequence, you can always kill a whole line
2392 by typing \\[beginning-of-line] \\[kill-line].
2394 If you want to append the killed line to the last killed text,
2395 use \\[append-next-kill] before \\[kill-line].
2397 If the buffer is read-only, Emacs will beep and refrain from deleting
2398 the line, but put the line in the kill ring anyway. This means that
2399 you can use this command to copy text from a read-only buffer.
2400 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2403 (kill-region (point)
2404 ;; It is better to move point to the other end of the kill
2405 ;; before killing. That way, in a read-only buffer, point
2406 ;; moves across the text that is copied to the kill ring.
2407 ;; The choice has no effect on undo now that undo records
2408 ;; the value of point from before the command was run.
2411 (forward-visible-line (prefix-numeric-value arg))
2413 (signal 'end-of-buffer nil))
2416 (end-of-visible-line) (point))))
2417 (if (or (save-excursion
2418 ;; If trailing whitespace is visible,
2419 ;; don't treat it as nothing.
2420 (unless show-trailing-whitespace
2421 (skip-chars-forward " \t" end))
2423 (and kill-whole-line (bolp)))
2424 (forward-visible-line 1)
2428 (defun kill-whole-line (&optional arg)
2430 With prefix arg, kill that many lines starting from the current line.
2431 If arg is negative, kill backward. Also kill the preceding newline.
2432 \(This is meant to make C-x z work well with negative arguments.\)
2433 If arg is zero, kill current line but exclude the trailing newline."
2435 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2436 (signal 'end-of-buffer nil))
2437 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2438 (signal 'beginning-of-buffer nil))
2439 (unless (eq last-command 'kill-region)
2441 (setq last-command 'kill-region))
2443 ;; We need to kill in two steps, because the previous command
2444 ;; could have been a kill command, in which case the text
2445 ;; before point needs to be prepended to the current kill
2446 ;; ring entry and the text after point appended. Also, we
2447 ;; need to use save-excursion to avoid copying the same text
2448 ;; twice to the kill ring in read-only buffers.
2450 (kill-region (point) (progn (forward-visible-line 0) (point))))
2451 (kill-region (point) (progn (end-of-visible-line) (point))))
2454 (kill-region (point) (progn (end-of-visible-line) (point))))
2455 (kill-region (point)
2456 (progn (forward-visible-line (1+ arg))
2457 (unless (bobp) (backward-char))
2461 (kill-region (point) (progn (forward-visible-line 0) (point))))
2462 (kill-region (point)
2463 (progn (forward-visible-line arg) (point))))))
2465 (defun forward-visible-line (arg)
2466 "Move forward by ARG lines, ignoring currently invisible newlines only.
2467 If ARG is negative, move backward -ARG lines.
2468 If ARG is zero, move to the beginning of the current line."
2473 (or (zerop (forward-line 1))
2474 (signal 'end-of-buffer nil))
2475 ;; If the newline we just skipped is invisible,
2478 (get-char-property (1- (point)) 'invisible)))
2479 (if (if (eq buffer-invisibility-spec t)
2481 (or (memq prop buffer-invisibility-spec)
2482 (assq prop buffer-invisibility-spec)))
2483 (setq arg (1+ arg))))
2484 (setq arg (1- arg)))
2485 ;; If invisible text follows, and it is a number of complete lines,
2487 (let ((opoint (point)))
2488 (while (and (not (eobp))
2490 (get-char-property (point) 'invisible)))
2491 (if (eq buffer-invisibility-spec t)
2493 (or (memq prop buffer-invisibility-spec)
2494 (assq prop buffer-invisibility-spec)))))
2496 (if (get-text-property (point) 'invisible)
2497 (or (next-single-property-change (point) 'invisible)
2499 (next-overlay-change (point)))))
2501 (goto-char opoint))))
2503 (while (or first (<= arg 0))
2506 (or (zerop (forward-line -1))
2507 (signal 'beginning-of-buffer nil)))
2508 ;; If the newline we just moved to is invisible,
2512 (get-char-property (1- (point)) 'invisible)))
2513 (unless (if (eq buffer-invisibility-spec t)
2515 (or (memq prop buffer-invisibility-spec)
2516 (assq prop buffer-invisibility-spec)))
2517 (setq arg (1+ arg)))))
2519 ;; If invisible text follows, and it is a number of complete lines,
2521 (let ((opoint (point)))
2522 (while (and (not (bobp))
2524 (get-char-property (1- (point)) 'invisible)))
2525 (if (eq buffer-invisibility-spec t)
2527 (or (memq prop buffer-invisibility-spec)
2528 (assq prop buffer-invisibility-spec)))))
2530 (if (get-text-property (1- (point)) 'invisible)
2531 (or (previous-single-property-change (point) 'invisible)
2533 (previous-overlay-change (point)))))
2535 (goto-char opoint)))))
2536 ((beginning-of-buffer end-of-buffer)
2539 (defun end-of-visible-line ()
2540 "Move to end of current visible line."
2542 ;; If the following character is currently invisible,
2543 ;; skip all characters with that same `invisible' property value,
2544 ;; then find the next newline.
2545 (while (and (not (eobp))
2547 (skip-chars-forward "^\n")
2549 (get-char-property (point) 'invisible)))
2550 (if (eq buffer-invisibility-spec t)
2552 (or (memq prop buffer-invisibility-spec)
2553 (assq prop buffer-invisibility-spec))))))
2554 (skip-chars-forward "^\n")
2555 (if (get-text-property (point) 'invisible)
2556 (goto-char (next-single-property-change (point) 'invisible))
2557 (goto-char (next-overlay-change (point))))
2560 (defun insert-buffer (buffer)
2561 "Insert after point the contents of BUFFER.
2562 Puts mark after the inserted text.
2563 BUFFER may be a buffer or a buffer name.
2565 This function is meant for the user to run interactively.
2566 Don't call it from programs: use `insert-buffer-substring' instead!"
2570 (barf-if-buffer-read-only)
2571 (read-buffer "Insert buffer: "
2572 (if (eq (selected-window) (next-window (selected-window)))
2573 (other-buffer (current-buffer))
2574 (window-buffer (next-window (selected-window))))
2578 (insert-buffer-substring (get-buffer buffer))
2582 (defun append-to-buffer (buffer start end)
2583 "Append to specified buffer the text of the region.
2584 It is inserted into that buffer before its point.
2586 When calling from a program, give three arguments:
2587 BUFFER (or buffer name), START and END.
2588 START and END specify the portion of the current buffer to be copied."
2590 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2591 (region-beginning) (region-end)))
2592 (let ((oldbuf (current-buffer)))
2594 (let* ((append-to (get-buffer-create buffer))
2595 (windows (get-buffer-window-list append-to t t))
2597 (set-buffer append-to)
2598 (setq point (point))
2599 (barf-if-buffer-read-only)
2600 (insert-buffer-substring oldbuf start end)
2601 (dolist (window windows)
2602 (when (= (window-point window) point)
2603 (set-window-point window (point))))))))
2605 (defun prepend-to-buffer (buffer start end)
2606 "Prepend to specified buffer the text of the region.
2607 It is inserted into that buffer after its point.
2609 When calling from a program, give three arguments:
2610 BUFFER (or buffer name), START and END.
2611 START and END specify the portion of the current buffer to be copied."
2612 (interactive "BPrepend to buffer: \nr")
2613 (let ((oldbuf (current-buffer)))
2615 (set-buffer (get-buffer-create buffer))
2616 (barf-if-buffer-read-only)
2618 (insert-buffer-substring oldbuf start end)))))
2620 (defun copy-to-buffer (buffer start end)
2621 "Copy to specified buffer the text of the region.
2622 It is inserted into that buffer, replacing existing text there.
2624 When calling from a program, give three arguments:
2625 BUFFER (or buffer name), START and END.
2626 START and END specify the portion of the current buffer to be copied."
2627 (interactive "BCopy to buffer: \nr")
2628 (let ((oldbuf (current-buffer)))
2630 (set-buffer (get-buffer-create buffer))
2631 (barf-if-buffer-read-only)
2634 (insert-buffer-substring oldbuf start end)))))
2636 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2637 (put 'mark-inactive 'error-message "The mark is not active now")
2639 (defun mark (&optional force)
2640 "Return this buffer's mark value as integer; error if mark inactive.
2641 If optional argument FORCE is non-nil, access the mark value
2642 even if the mark is not currently active, and return nil
2643 if there is no mark at all.
2645 If you are using this in an editing command, you are most likely making
2646 a mistake; see the documentation of `set-mark'."
2647 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2648 (marker-position (mark-marker))
2649 (signal 'mark-inactive nil)))
2651 ;; Many places set mark-active directly, and several of them failed to also
2652 ;; run deactivate-mark-hook. This shorthand should simplify.
2653 (defsubst deactivate-mark ()
2654 "Deactivate the mark by setting `mark-active' to nil.
2655 \(That makes a difference only in Transient Mark mode.)
2656 Also runs the hook `deactivate-mark-hook'."
2658 ((eq transient-mark-mode 'lambda)
2659 (setq transient-mark-mode nil))
2660 (transient-mark-mode
2661 (setq mark-active nil)
2662 (run-hooks 'deactivate-mark-hook))))
2664 (defun set-mark (pos)
2665 "Set this buffer's mark to POS. Don't use this function!
2666 That is to say, don't use this function unless you want
2667 the user to see that the mark has moved, and you want the previous
2668 mark position to be lost.
2670 Normally, when a new mark is set, the old one should go on the stack.
2671 This is why most applications should use push-mark, not set-mark.
2673 Novice Emacs Lisp programmers often try to use the mark for the wrong
2674 purposes. The mark saves a location for the user's convenience.
2675 Most editing commands should not alter the mark.
2676 To remember a location for internal use in the Lisp program,
2677 store it in a Lisp variable. Example:
2679 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2683 (setq mark-active t)
2684 (run-hooks 'activate-mark-hook)
2685 (set-marker (mark-marker) pos (current-buffer)))
2686 ;; Normally we never clear mark-active except in Transient Mark mode.
2687 ;; But when we actually clear out the mark value too,
2688 ;; we must clear mark-active in any mode.
2689 (setq mark-active nil)
2690 (run-hooks 'deactivate-mark-hook)
2691 (set-marker (mark-marker) nil)))
2693 (defvar mark-ring nil
2694 "The list of former marks of the current buffer, most recent first.")
2695 (make-variable-buffer-local 'mark-ring)
2696 (put 'mark-ring 'permanent-local t)
2698 (defcustom mark-ring-max 16
2699 "*Maximum size of mark ring. Start discarding off end if gets this big."
2701 :group 'editing-basics)
2703 (defvar global-mark-ring nil
2704 "The list of saved global marks, most recent first.")
2706 (defcustom global-mark-ring-max 16
2707 "*Maximum size of global mark ring. \
2708 Start discarding off end if gets this big."
2710 :group 'editing-basics)
2712 (defun pop-to-mark-command ()
2713 "Jump to mark, and pop a new position for mark off the ring
2714 \(does not affect global mark ring\)."
2717 (error "No mark set in this buffer")
2718 (goto-char (mark t))
2721 (defun push-mark-command (arg &optional nomsg)
2722 "Set mark at where point is.
2723 If no prefix arg and mark is already set there, just activate it.
2724 Display `Mark set' unless the optional second arg NOMSG is non-nil."
2726 (let ((mark (marker-position (mark-marker))))
2727 (if (or arg (null mark) (/= mark (point)))
2728 (push-mark nil nomsg t)
2729 (setq mark-active t)
2731 (message "Mark activated")))))
2733 (defun set-mark-command (arg)
2734 "Set mark at where point is, or jump to mark.
2735 With no prefix argument, set mark, and push old mark position on local
2736 mark ring; also push mark on global mark ring if last mark was set in
2737 another buffer. Immediately repeating the command activates
2738 `transient-mark-mode' temporarily.
2740 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
2741 jump to mark, and pop a new position
2742 for mark off the local mark ring \(this does not affect the global
2743 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
2744 mark ring \(see `pop-global-mark'\).
2746 Repeating the \\[set-mark-command] command without the prefix jumps to
2747 the next position off the local (or global) mark ring.
2749 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
2750 \\[universal-argument] \\[set-mark-command], unconditionally
2751 set mark where point is.
2753 Novice Emacs Lisp programmers often try to use the mark for the wrong
2754 purposes. See the documentation of `set-mark' for more information."
2756 (if (eq transient-mark-mode 'lambda)
2757 (setq transient-mark-mode nil))
2759 ((and (consp arg) (> (prefix-numeric-value arg) 4))
2760 (push-mark-command nil))
2761 ((not (eq this-command 'set-mark-command))
2763 (pop-to-mark-command)
2764 (push-mark-command t)))
2765 ((eq last-command 'pop-to-mark-command)
2766 (setq this-command 'pop-to-mark-command)
2767 (pop-to-mark-command))
2768 ((and (eq last-command 'pop-global-mark) (not arg))
2769 (setq this-command 'pop-global-mark)
2772 (setq this-command 'pop-to-mark-command)
2773 (pop-to-mark-command))
2774 ((and (eq last-command 'set-mark-command)
2775 mark-active (null transient-mark-mode))
2776 (setq transient-mark-mode 'lambda)
2777 (message "Transient-mark-mode temporarily enabled"))
2779 (push-mark-command nil))))
2781 (defun push-mark (&optional location nomsg activate)
2782 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2783 If the last global mark pushed was not in the current buffer,
2784 also push LOCATION on the global mark ring.
2785 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2786 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2788 Novice Emacs Lisp programmers often try to use the mark for the wrong
2789 purposes. See the documentation of `set-mark' for more information.
2791 In Transient Mark mode, this does not activate the mark."
2792 (unless (null (mark t))
2793 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2794 (when (> (length mark-ring) mark-ring-max)
2795 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2796 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
2797 (set-marker (mark-marker) (or location (point)) (current-buffer))
2798 ;; Now push the mark on the global mark ring.
2799 (if (and global-mark-ring
2800 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2801 ;; The last global mark pushed was in this same buffer.
2802 ;; Don't push another one.
2804 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2805 (when (> (length global-mark-ring) global-mark-ring-max)
2806 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
2807 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
2808 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2809 (message "Mark set"))
2810 (if (or activate (not transient-mark-mode))
2811 (set-mark (mark t)))
2815 "Pop off mark ring into the buffer's actual mark.
2816 Does not set point. Does nothing if mark ring is empty."
2818 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2819 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2821 (move-marker (car mark-ring) nil)
2822 (if (null (mark t)) (ding))
2823 (setq mark-ring (cdr mark-ring))))
2825 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2826 (defun exchange-point-and-mark (&optional arg)
2827 "Put the mark where point is now, and point where the mark is now.
2828 This command works even when the mark is not active,
2829 and it reactivates the mark.
2830 With prefix arg, `transient-mark-mode' is enabled temporarily."
2834 (if (null transient-mark-mode)
2835 (setq transient-mark-mode 'lambda))
2838 (let ((omark (mark t)))
2840 (error "No mark set in this buffer"))
2845 (define-minor-mode transient-mark-mode
2846 "Toggle Transient Mark mode.
2847 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2849 In Transient Mark mode, when the mark is active, the region is highlighted.
2850 Changing the buffer \"deactivates\" the mark.
2851 So do certain other operations that set the mark
2852 but whose main purpose is something else--for example,
2853 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
2855 You can also deactivate the mark by typing \\[keyboard-quit] or
2856 \\[keyboard-escape-quit].
2858 Many commands change their behavior when Transient Mark mode is in effect
2859 and the mark is active, by acting on the region instead of their usual
2860 default part of the buffer's text. Examples of such commands include
2861 \\[comment-dwim], \\[flush-lines], \\[ispell], \\[keep-lines],
2862 \\[query-replace], \\[query-replace-regexp], and \\[undo]. Invoke
2863 \\[apropos-documentation] and type \"transient\" or \"mark.*active\" at
2864 the prompt, to see the documentation of commands which are sensitive to
2865 the Transient Mark mode."
2866 :global t :group 'editing-basics :require nil)
2868 (defun pop-global-mark ()
2869 "Pop off global mark ring and jump to the top location."
2871 ;; Pop entries which refer to non-existent buffers.
2872 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2873 (setq global-mark-ring (cdr global-mark-ring)))
2874 (or global-mark-ring
2875 (error "No global mark set"))
2876 (let* ((marker (car global-mark-ring))
2877 (buffer (marker-buffer marker))
2878 (position (marker-position marker)))
2879 (setq global-mark-ring (nconc (cdr global-mark-ring)
2880 (list (car global-mark-ring))))
2882 (or (and (>= position (point-min))
2883 (<= position (point-max)))
2885 (goto-char position)
2886 (switch-to-buffer buffer)))
2888 (defcustom next-line-add-newlines nil
2889 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2892 :group 'editing-basics)
2894 (defun next-line (&optional arg)
2895 "Move cursor vertically down ARG lines.
2896 If there is no character in the target line exactly under the current column,
2897 the cursor is positioned after the character in that line which spans this
2898 column, or at the end of the line if it is not long enough.
2899 If there is no line in the buffer after this one, behavior depends on the
2900 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2901 to create a line, and moves the cursor to that line. Otherwise it moves the
2902 cursor to the end of the buffer.
2904 The command \\[set-goal-column] can be used to create
2905 a semipermanent goal column for this command.
2906 Then instead of trying to move exactly vertically (or as close as possible),
2907 this command moves to the specified goal column (or as close as possible).
2908 The goal column is stored in the variable `goal-column', which is nil
2909 when there is no goal column.
2911 If you are thinking of using this in a Lisp program, consider
2912 using `forward-line' instead. It is usually easier to use
2913 and more reliable (no dependence on goal column, etc.)."
2915 (or arg (setq arg 1))
2916 (if (and next-line-add-newlines (= arg 1))
2917 (if (save-excursion (end-of-line) (eobp))
2918 ;; When adding a newline, don't expand an abbrev.
2919 (let ((abbrev-mode nil))
2926 ((beginning-of-buffer end-of-buffer) (ding)))
2930 (defun previous-line (&optional arg)
2931 "Move cursor vertically up ARG lines.
2932 If there is no character in the target line exactly over the current column,
2933 the cursor is positioned after the character in that line which spans this
2934 column, or at the end of the line if it is not long enough.
2936 The command \\[set-goal-column] can be used to create
2937 a semipermanent goal column for this command.
2938 Then instead of trying to move exactly vertically (or as close as possible),
2939 this command moves to the specified goal column (or as close as possible).
2940 The goal column is stored in the variable `goal-column', which is nil
2941 when there is no goal column.
2943 If you are thinking of using this in a Lisp program, consider using
2944 `forward-line' with a negative argument instead. It is usually easier
2945 to use and more reliable (no dependence on goal column, etc.)."
2947 (or arg (setq arg 1))
2951 ((beginning-of-buffer end-of-buffer) (ding)))
2952 (line-move (- arg)))
2955 (defcustom track-eol nil
2956 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2957 This means moving to the end of each line moved onto.
2958 The beginning of a blank line does not count as the end of a line."
2960 :group 'editing-basics)
2962 (defcustom goal-column nil
2963 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2964 :type '(choice integer
2965 (const :tag "None" nil))
2966 :group 'editing-basics)
2967 (make-variable-buffer-local 'goal-column)
2969 (defvar temporary-goal-column 0
2970 "Current goal column for vertical motion.
2971 It is the column where point was
2972 at the start of current run of vertical motion commands.
2973 When the `track-eol' feature is doing its job, the value is 9999.")
2975 (defcustom line-move-ignore-invisible nil
2976 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2977 Outline mode sets this."
2979 :group 'editing-basics)
2981 (defun line-move-invisible (pos)
2982 "Return non-nil if the character after POS is currently invisible."
2984 (get-char-property pos 'invisible)))
2985 (if (eq buffer-invisibility-spec t)
2987 (or (memq prop buffer-invisibility-spec)
2988 (assq prop buffer-invisibility-spec)))))
2990 ;; This is the guts of next-line and previous-line.
2991 ;; Arg says how many lines to move.
2992 (defun line-move (arg)
2993 ;; Don't run any point-motion hooks, and disregard intangibility,
2994 ;; for intermediate positions.
2995 (let ((inhibit-point-motion-hooks t)
2997 new line-end line-beg)
3000 (if (not (memq last-command '(next-line previous-line)))
3001 (setq temporary-goal-column
3002 (if (and track-eol (eolp)
3003 ;; Don't count beg of empty line as end of line
3004 ;; unless we just did explicit end-of-line.
3005 (or (not (bolp)) (eq last-command 'end-of-line)))
3008 (if (and (not (integerp selective-display))
3009 (not line-move-ignore-invisible))
3010 ;; Use just newline characters.
3011 ;; Set ARG to 0 if we move as many lines as requested.
3013 (progn (if (> arg 1) (forward-line (1- arg)))
3014 ;; This way of moving forward ARG lines
3015 ;; verifies that we have a newline after the last one.
3016 ;; It doesn't get confused by intangible text.
3018 (if (zerop (forward-line 1))
3020 (and (zerop (forward-line arg))
3023 (signal (if (< arg 0)
3024 'beginning-of-buffer
3027 ;; Move by arg lines, but ignore invisible ones.
3029 ;; If the following character is currently invisible,
3030 ;; skip all characters with that same `invisible' property value.
3031 (while (and (not (eobp)) (line-move-invisible (point)))
3032 (goto-char (next-char-property-change (point))))
3035 (and (zerop (vertical-motion 1))
3036 (signal 'end-of-buffer nil))
3037 (setq arg (1- arg)))
3040 (and (zerop (vertical-motion -1))
3041 (signal 'beginning-of-buffer nil))
3043 (while (and (not (bobp)) (line-move-invisible (1- (point))))
3044 (goto-char (previous-char-property-change (point)))))))
3047 ;; If we did not move down as far as desired,
3048 ;; at least go to end of line.
3051 ;; If we did not move down as far as desired,
3052 ;; at least go to end of line.
3053 (beginning-of-line))
3055 (line-move-finish (or goal-column temporary-goal-column) opoint)))))
3058 (defun line-move-finish (column opoint)
3061 ;; Set REPEAT to t to repeat the whole thing.
3065 (line-beg (save-excursion (beginning-of-line) (point)))
3067 ;; Compute the end of the line
3068 ;; ignoring effectively intangible newlines.
3069 (let ((inhibit-point-motion-hooks nil)
3070 (inhibit-field-text-motion t))
3071 (save-excursion (end-of-line) (point)))))
3073 ;; Move to the desired column.
3074 (line-move-to-column column)
3077 ;; Process intangibility within a line.
3078 ;; Move to the chosen destination position from above,
3079 ;; with intangibility processing enabled.
3081 (goto-char (point-min))
3082 (let ((inhibit-point-motion-hooks nil))
3085 ;; If intangibility moves us to a different (later) place
3086 ;; in the same line, use that as the destination.
3087 (if (<= (point) line-end)
3089 ;; If that position is "too late",
3090 ;; try the previous allowable position.
3093 (if (<= (point) line-end)
3095 ;; As a last resort, use the end of the line.
3096 (setq new line-end))))
3098 ;; Now move to the updated destination, processing fields
3099 ;; as well as intangibility.
3101 (let ((inhibit-point-motion-hooks nil))
3103 (constrain-to-field new opoint nil t
3104 'inhibit-line-move-field-capture)))
3106 ;; If all this moved us to a different line,
3107 ;; retry everything within that new line.
3108 (when (or (< (point) line-beg) (> (point) line-end))
3109 ;; Repeat the intangibility and field processing.
3110 (setq repeat t))))))
3112 (defun line-move-to-column (col)
3113 "Try to find column COL, considering invisibility.
3114 This function works only in certain cases,
3115 because what we really need is for `move-to-column'
3116 and `current-column' to be able to ignore invisible text."
3119 (move-to-column col))
3121 (when (and line-move-ignore-invisible
3122 (not (bolp)) (line-move-invisible (1- (point))))
3123 (let ((normal-location (point))
3124 (normal-column (current-column)))
3125 ;; If the following character is currently invisible,
3126 ;; skip all characters with that same `invisible' property value.
3127 (while (and (not (eobp))
3128 (line-move-invisible (point)))
3129 (goto-char (next-char-property-change (point))))
3130 ;; Have we advanced to a larger column position?
3131 (if (> (current-column) normal-column)
3132 ;; We have made some progress towards the desired column.
3133 ;; See if we can make any further progress.
3134 (line-move-to-column (+ (current-column) (- col normal-column)))
3135 ;; Otherwise, go to the place we originally found
3136 ;; and move back over invisible text.
3137 ;; that will get us to the same place on the screen
3138 ;; but with a more reasonable buffer position.
3139 (goto-char normal-location)
3140 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3141 (while (and (not (bolp)) (line-move-invisible (1- (point))))
3142 (goto-char (previous-char-property-change (point) line-beg))))))))
3144 ;;; Many people have said they rarely use this feature, and often type
3145 ;;; it by accident. Maybe it shouldn't even be on a key.
3146 (put 'set-goal-column 'disabled t)
3148 (defun set-goal-column (arg)
3149 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3150 Those commands will move to this position in the line moved to
3151 rather than trying to keep the same horizontal position.
3152 With a non-nil argument, clears out the goal column
3153 so that \\[next-line] and \\[previous-line] resume vertical motion.
3154 The goal column is stored in the variable `goal-column'."
3158 (setq goal-column nil)
3159 (message "No goal column"))
3160 (setq goal-column (current-column))
3161 (message (substitute-command-keys
3162 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3167 (defun scroll-other-window-down (lines)
3168 "Scroll the \"other window\" down.
3169 For more details, see the documentation for `scroll-other-window'."
3171 (scroll-other-window
3172 ;; Just invert the argument's meaning.
3173 ;; We can do that without knowing which window it will be.
3174 (if (eq lines '-) nil
3176 (- (prefix-numeric-value lines))))))
3177 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
3179 (defun beginning-of-buffer-other-window (arg)
3180 "Move point to the beginning of the buffer in the other window.
3181 Leave mark at previous position.
3182 With arg N, put point N/10 of the way from the true beginning."
3184 (let ((orig-window (selected-window))
3185 (window (other-window-for-scrolling)))
3186 ;; We use unwind-protect rather than save-window-excursion
3187 ;; because the latter would preserve the things we want to change.
3190 (select-window window)
3191 ;; Set point and mark in that window's buffer.
3192 (beginning-of-buffer arg)
3193 ;; Set point accordingly.
3195 (select-window orig-window))))
3197 (defun end-of-buffer-other-window (arg)
3198 "Move point to the end of the buffer in the other window.
3199 Leave mark at previous position.
3200 With arg N, put point N/10 of the way from the true end."
3202 ;; See beginning-of-buffer-other-window for comments.
3203 (let ((orig-window (selected-window))
3204 (window (other-window-for-scrolling)))
3207 (select-window window)
3210 (select-window orig-window))))
3212 (defun transpose-chars (arg)
3213 "Interchange characters around point, moving forward one character.
3214 With prefix arg ARG, effect is to take character before point
3215 and drag it forward past ARG other characters (backward if ARG negative).
3216 If no argument and at end of line, the previous two chars are exchanged."
3218 (and (null arg) (eolp) (forward-char -1))
3219 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3221 (defun transpose-words (arg)
3222 "Interchange words around point, leaving point at end of them.
3223 With prefix arg ARG, effect is to take word before or around point
3224 and drag it forward past ARG other words (backward if ARG negative).
3225 If ARG is zero, the words around or after point and around or after mark
3227 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3229 (transpose-subr 'forward-word arg))
3231 (defun transpose-sexps (arg)
3232 "Like \\[transpose-words] but applies to sexps.
3233 Does not work on a sexp that point is in the middle of
3234 if it is a list or string."
3238 ;; Here we should try to simulate the behavior of
3239 ;; (cons (progn (forward-sexp x) (point))
3240 ;; (progn (forward-sexp (- x)) (point)))
3241 ;; Except that we don't want to rely on the second forward-sexp
3242 ;; putting us back to where we want to be, since forward-sexp-function
3243 ;; might do funny things like infix-precedence.
3245 (looking-at "\\sw\\|\\s_")
3247 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3248 ;; Jumping over a symbol. We might be inside it, mind you.
3249 (progn (funcall (if (> arg 0)
3250 'skip-syntax-backward 'skip-syntax-forward)
3252 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3253 ;; Otherwise, we're between sexps. Take a step back before jumping
3254 ;; to make sure we'll obey the same precedence no matter which direction
3256 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3257 (cons (save-excursion (forward-sexp arg) (point))
3258 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3259 (not (zerop (funcall (if (> arg 0)
3260 'skip-syntax-forward
3261 'skip-syntax-backward)
3266 (defun transpose-lines (arg)
3267 "Exchange current line and previous line, leaving point after both.
3268 With argument ARG, takes previous line and moves it past ARG lines.
3269 With argument 0, interchanges line point is in with line mark is in."
3271 (transpose-subr (function
3275 ;; Move forward over ARG lines,
3276 ;; but create newlines if necessary.
3277 (setq arg (forward-line arg))
3278 (if (/= (preceding-char) ?\n)
3279 (setq arg (1+ arg)))
3282 (forward-line arg))))
3285 (defun transpose-subr (mover arg &optional special)
3286 (let ((aux (if special mover
3288 (cons (progn (funcall mover x) (point))
3289 (progn (funcall mover (- x)) (point))))))
3294 (setq pos1 (funcall aux 1))
3296 (setq pos2 (funcall aux 1))
3297 (transpose-subr-1 pos1 pos2))
3298 (exchange-point-and-mark))
3300 (setq pos1 (funcall aux -1))
3301 (setq pos2 (funcall aux arg))
3302 (transpose-subr-1 pos1 pos2)
3303 (goto-char (car pos2)))
3305 (setq pos1 (funcall aux -1))
3306 (goto-char (car pos1))
3307 (setq pos2 (funcall aux arg))
3308 (transpose-subr-1 pos1 pos2)))))
3310 (defun transpose-subr-1 (pos1 pos2)
3311 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3312 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3313 (when (> (car pos1) (car pos2))
3315 (setq pos1 pos2 pos2 swap)))
3316 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3317 (atomic-change-group
3319 ;; FIXME: We first delete the two pieces of text, so markers that
3320 ;; used to point to after the text end up pointing to before it :-(
3321 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3322 (goto-char (car pos2))
3323 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3324 (goto-char (car pos1))
3327 (defun backward-word (&optional arg)
3328 "Move backward until encountering the beginning of a word.
3329 With argument, do this that many times."
3331 (forward-word (- (or arg 1))))
3333 (defun mark-word (arg)
3334 "Set mark arg words away from point.
3335 If this command is repeated, it marks the next ARG words after the ones
3338 (cond ((and (eq last-command this-command) (mark t))
3351 (defun kill-word (arg)
3352 "Kill characters forward until encountering the end of a word.
3353 With argument, do this that many times."
3355 (kill-region (point) (progn (forward-word arg) (point))))
3357 (defun backward-kill-word (arg)
3358 "Kill characters backward until encountering the end of a word.
3359 With argument, do this that many times."
3361 (kill-word (- arg)))
3363 (defun current-word (&optional strict really-word)
3364 "Return the symbol or word that point is on (or a nearby one) as a string.
3365 The return value includes no text properties.
3366 If optional arg STRICT is non-nil, return nil unless point is within
3367 or adjacent to a symbol or word.
3368 The function, belying its name, normally finds a symbol.
3369 If optional arg REALLY-WORD is non-nil, it finds just a word."
3371 (let* ((oldpoint (point)) (start (point)) (end (point))
3372 (syntaxes (if really-word "w" "w_"))
3373 (not-syntaxes (concat "^" syntaxes)))
3374 (skip-syntax-backward syntaxes) (setq start (point))
3375 (goto-char oldpoint)
3376 (skip-syntax-forward syntaxes) (setq end (point))
3377 (when (and (eq start oldpoint) (eq end oldpoint)
3378 ;; Point is neither within nor adjacent to a word.
3380 ;; Look for preceding word in same line.
3381 (skip-syntax-backward not-syntaxes
3382 (save-excursion (beginning-of-line)
3385 ;; No preceding word in same line.
3386 ;; Look for following word in same line.
3388 (skip-syntax-forward not-syntaxes
3389 (save-excursion (end-of-line)
3391 (setq start (point))
3392 (skip-syntax-forward syntaxes)
3395 (skip-syntax-backward syntaxes)
3396 (setq start (point))))
3397 ;; If we found something nonempty, return it as a string.
3398 (unless (= start end)
3399 (buffer-substring-no-properties start end)))))
3401 (defcustom fill-prefix nil
3402 "*String for filling to insert at front of new line, or nil for none."
3403 :type '(choice (const :tag "None" nil)
3406 (make-variable-buffer-local 'fill-prefix)
3408 (defcustom auto-fill-inhibit-regexp nil
3409 "*Regexp to match lines which should not be auto-filled."
3410 :type '(choice (const :tag "None" nil)
3414 (defvar comment-line-break-function 'comment-indent-new-line
3415 "*Mode-specific function which line breaks and continues a comment.
3417 This function is only called during auto-filling of a comment section.
3418 The function should take a single optional argument, which is a flag
3419 indicating whether it should use soft newlines.
3421 Setting this variable automatically makes it local to the current buffer.")
3423 ;; This function is used as the auto-fill-function of a buffer
3424 ;; when Auto-Fill mode is enabled.
3425 ;; It returns t if it really did any work.
3426 ;; (Actually some major modes use a different auto-fill function,
3427 ;; but this one is the default one.)
3428 (defun do-auto-fill ()
3429 (let (fc justify give-up
3430 (fill-prefix fill-prefix))
3431 (if (or (not (setq justify (current-justification)))
3432 (null (setq fc (current-fill-column)))
3433 (and (eq justify 'left)
3434 (<= (current-column) fc))
3435 (and auto-fill-inhibit-regexp
3436 (save-excursion (beginning-of-line)
3437 (looking-at auto-fill-inhibit-regexp))))
3438 nil ;; Auto-filling not required
3439 (if (memq justify '(full center right))
3440 (save-excursion (unjustify-current-line)))
3442 ;; Choose a fill-prefix automatically.
3443 (when (and adaptive-fill-mode
3444 (or (null fill-prefix) (string= fill-prefix "")))
3446 (fill-context-prefix
3447 (save-excursion (backward-paragraph 1) (point))
3448 (save-excursion (forward-paragraph 1) (point)))))
3449 (and prefix (not (equal prefix ""))
3450 ;; Use auto-indentation rather than a guessed empty prefix.
3451 (not (and fill-indent-according-to-mode
3452 (string-match "\\`[ \t]*\\'" prefix)))
3453 (setq fill-prefix prefix))))
3455 (while (and (not give-up) (> (current-column) fc))
3456 ;; Determine where to split the line.
3461 (setq after-prefix (point))
3463 (looking-at (regexp-quote fill-prefix))
3464 (setq after-prefix (match-end 0)))
3465 (move-to-column (1+ fc))
3466 (fill-move-to-break-point after-prefix)
3469 ;; See whether the place we found is any good.
3471 (goto-char fill-point)
3473 ;; There is no use breaking at end of line.
3474 (save-excursion (skip-chars-forward " ") (eolp))
3475 ;; It is futile to split at the end of the prefix
3476 ;; since we would just insert the prefix again.
3477 (and after-prefix (<= (point) after-prefix))
3478 ;; Don't split right after a comment starter
3479 ;; since we would just make another comment starter.
3480 (and comment-start-skip
3481 (let ((limit (point)))
3483 (and (re-search-forward comment-start-skip
3485 (eq (point) limit))))))
3486 ;; No good place to break => stop trying.
3488 ;; Ok, we have a useful place to break the line. Do it.
3489 (let ((prev-column (current-column)))
3490 ;; If point is at the fill-point, do not `save-excursion'.
3491 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3492 ;; point will end up before it rather than after it.
3494 (skip-chars-backward " \t")
3495 (= (point) fill-point))
3496 (funcall comment-line-break-function t)
3498 (goto-char fill-point)
3499 (funcall comment-line-break-function t)))
3500 ;; Now do justification, if required
3501 (if (not (eq justify 'left))
3504 (justify-current-line justify nil t)))
3505 ;; If making the new line didn't reduce the hpos of
3506 ;; the end of the line, then give up now;
3507 ;; trying again will not help.
3508 (if (>= (current-column) prev-column)
3509 (setq give-up t))))))
3510 ;; Justify last line.
3511 (justify-current-line justify t t)
3514 (defvar normal-auto-fill-function 'do-auto-fill
3515 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3516 Some major modes set this.")
3518 ;; FIXME: turn into a proper minor mode.
3519 ;; Add a global minor mode version of it.
3520 (defun auto-fill-mode (&optional arg)
3521 "Toggle Auto Fill mode.
3522 With arg, turn Auto Fill mode on if and only if arg is positive.
3523 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3524 automatically breaks the line at a previous space.
3526 The value of `normal-auto-fill-function' specifies the function to use
3527 for `auto-fill-function' when turning Auto Fill mode on."
3529 (prog1 (setq auto-fill-function
3531 (not auto-fill-function)
3532 (> (prefix-numeric-value arg) 0))
3533 normal-auto-fill-function
3535 (force-mode-line-update)))
3537 ;; This holds a document string used to document auto-fill-mode.
3538 (defun auto-fill-function ()
3539 "Automatically break line at a previous space, in insertion of text."
3542 (defun turn-on-auto-fill ()
3543 "Unconditionally turn on Auto Fill mode."
3546 (defun turn-off-auto-fill ()
3547 "Unconditionally turn off Auto Fill mode."
3548 (auto-fill-mode -1))
3550 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3552 (defun set-fill-column (arg)
3553 "Set `fill-column' to specified argument.
3554 Use \\[universal-argument] followed by a number to specify a column.
3555 Just \\[universal-argument] as argument means to use the current column."
3558 (setq arg (current-column)))
3559 (if (not (integerp arg))
3560 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3561 (error "Set-fill-column requires an explicit argument")
3562 (message "Fill column set to %d (was %d)" arg fill-column)
3563 (setq fill-column arg)))
3565 (defun set-selective-display (arg)
3566 "Set `selective-display' to ARG; clear it if no arg.
3567 When the value of `selective-display' is a number > 0,
3568 lines whose indentation is >= that value are not displayed.
3569 The variable `selective-display' has a separate value for each buffer."
3571 (if (eq selective-display t)
3572 (error "selective-display already in use for marked lines"))
3575 (narrow-to-region (point-min) (point))
3576 (goto-char (window-start))
3577 (vertical-motion (window-height)))))
3578 (setq selective-display
3579 (and arg (prefix-numeric-value arg)))
3580 (recenter current-vpos))
3581 (set-window-start (selected-window) (window-start (selected-window)))
3582 (princ "selective-display set to " t)
3583 (prin1 selective-display t)
3586 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
3587 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
3589 (defun toggle-truncate-lines (arg)
3590 "Toggle whether to fold or truncate long lines on the screen.
3591 With arg, truncate long lines iff arg is positive.
3592 Note that in side-by-side windows, truncation is always enabled."
3594 (setq truncate-lines
3596 (not truncate-lines)
3597 (> (prefix-numeric-value arg) 0)))
3598 (force-mode-line-update)
3599 (unless truncate-lines
3600 (let ((buffer (current-buffer)))
3601 (walk-windows (lambda (window)
3602 (if (eq buffer (window-buffer window))
3603 (set-window-hscroll window 0)))
3605 (message "Truncate long lines %s"
3606 (if truncate-lines "enabled" "disabled")))
3608 (defvar overwrite-mode-textual " Ovwrt"
3609 "The string displayed in the mode line when in overwrite mode.")
3610 (defvar overwrite-mode-binary " Bin Ovwrt"
3611 "The string displayed in the mode line when in binary overwrite mode.")
3613 (defun overwrite-mode (arg)
3614 "Toggle overwrite mode.
3615 With arg, turn overwrite mode on iff arg is positive.
3616 In overwrite mode, printing characters typed in replace existing text
3617 on a one-for-one basis, rather than pushing it to the right. At the
3618 end of a line, such characters extend the line. Before a tab,
3619 such characters insert until the tab is filled in.
3620 \\[quoted-insert] still inserts characters in overwrite mode; this
3621 is supposed to make it easier to insert characters when necessary."
3623 (setq overwrite-mode
3624 (if (if (null arg) (not overwrite-mode)
3625 (> (prefix-numeric-value arg) 0))
3626 'overwrite-mode-textual))
3627 (force-mode-line-update))
3629 (defun binary-overwrite-mode (arg)
3630 "Toggle binary overwrite mode.
3631 With arg, turn binary overwrite mode on iff arg is positive.
3632 In binary overwrite mode, printing characters typed in replace
3633 existing text. Newlines are not treated specially, so typing at the
3634 end of a line joins the line to the next, with the typed character
3635 between them. Typing before a tab character simply replaces the tab
3636 with the character typed.
3637 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3638 typing characters do.
3640 Note that binary overwrite mode is not its own minor mode; it is a
3641 specialization of overwrite-mode, entered by setting the
3642 `overwrite-mode' variable to `overwrite-mode-binary'."
3644 (setq overwrite-mode
3646 (not (eq overwrite-mode 'overwrite-mode-binary))
3647 (> (prefix-numeric-value arg) 0))
3648 'overwrite-mode-binary))
3649 (force-mode-line-update))
3651 (define-minor-mode line-number-mode
3652 "Toggle Line Number mode.
3653 With arg, turn Line Number mode on iff arg is positive.
3654 When Line Number mode is enabled, the line number appears
3657 Line numbers do not appear for very large buffers and buffers
3658 with very long lines; see variables `line-number-display-limit'
3659 and `line-number-display-limit-width'."
3660 :init-value t :global t :group 'editing-basics :require nil)
3662 (define-minor-mode column-number-mode
3663 "Toggle Column Number mode.
3664 With arg, turn Column Number mode on iff arg is positive.
3665 When Column Number mode is enabled, the column number appears
3667 :global t :group 'editing-basics :require nil)
3669 (define-minor-mode size-indication-mode
3670 "Toggle Size Indication mode.
3671 With arg, turn Size Indication mode on iff arg is positive. When
3672 Size Indication mode is enabled, the size of the accessible part
3673 of the buffer appears in the mode line."
3674 :global t :group 'editing-basics :require nil)
3676 (defgroup paren-blinking nil
3677 "Blinking matching of parens and expressions."
3678 :prefix "blink-matching-"
3679 :group 'paren-matching)
3681 (defcustom blink-matching-paren t
3682 "*Non-nil means show matching open-paren when close-paren is inserted."
3684 :group 'paren-blinking)
3686 (defcustom blink-matching-paren-on-screen t
3687 "*Non-nil means show matching open-paren when it is on screen.
3688 If nil, means don't show it (but the open-paren can still be shown
3689 when it is off screen)."
3691 :group 'paren-blinking)
3693 (defcustom blink-matching-paren-distance (* 25 1024)
3694 "*If non-nil, is maximum distance to search for matching open-paren."
3696 :group 'paren-blinking)
3698 (defcustom blink-matching-delay 1
3699 "*Time in seconds to delay after showing a matching paren."
3701 :group 'paren-blinking)
3703 (defcustom blink-matching-paren-dont-ignore-comments nil
3704 "*Non-nil means `blink-matching-paren' will not ignore comments."
3706 :group 'paren-blinking)
3708 (defun blink-matching-open ()
3709 "Move cursor momentarily to the beginning of the sexp before point."
3711 (and (> (point) (1+ (point-min)))
3712 blink-matching-paren
3713 ;; Verify an even number of quoting characters precede the close.
3714 (= 1 (logand 1 (- (point)
3717 (skip-syntax-backward "/\\")
3719 (let* ((oldpos (point))
3725 (if blink-matching-paren-distance
3726 (narrow-to-region (max (point-min)
3727 (- (point) blink-matching-paren-distance))
3730 (let ((parse-sexp-ignore-comments
3731 (and parse-sexp-ignore-comments
3732 (not blink-matching-paren-dont-ignore-comments))))
3733 (setq blinkpos (scan-sexps oldpos -1)))
3737 (goto-char blinkpos)
3738 (not (looking-at "\\s$")))
3739 (setq matching-paren
3740 (or (and parse-sexp-lookup-properties
3741 (let ((prop (get-text-property blinkpos 'syntax-table)))
3745 (matching-paren (char-after blinkpos)))
3747 (or (null matching-paren)
3748 (/= (char-after (1- oldpos))
3750 (if mismatch (setq blinkpos nil))
3752 ;; Don't log messages about paren matching.
3753 (let (message-log-max)
3754 (goto-char blinkpos)
3755 (if (pos-visible-in-window-p)
3756 (and blink-matching-paren-on-screen
3757 (sit-for blink-matching-delay))
3758 (goto-char blinkpos)
3761 ;; Show what precedes the open in its line, if anything.
3763 (skip-chars-backward " \t")
3765 (buffer-substring (progn (beginning-of-line) (point))
3767 ;; Show what follows the open in its line, if anything.
3770 (skip-chars-forward " \t")
3772 (buffer-substring blinkpos
3773 (progn (end-of-line) (point)))
3774 ;; Otherwise show the previous nonblank line,
3777 (skip-chars-backward "\n \t")
3780 (buffer-substring (progn
3781 (skip-chars-backward "\n \t")
3784 (progn (end-of-line)
3785 (skip-chars-backward " \t")
3787 ;; Replace the newline and other whitespace with `...'.
3789 (buffer-substring blinkpos (1+ blinkpos)))
3790 ;; There is nothing to show except the char itself.
3791 (buffer-substring blinkpos (1+ blinkpos))))))))
3793 (message "Mismatched parentheses"))
3794 ((not blink-matching-paren-distance)
3795 (message "Unmatched parenthesis"))))))))
3797 ;Turned off because it makes dbx bomb out.
3798 (setq blink-paren-function 'blink-matching-open)
3800 ;; This executes C-g typed while Emacs is waiting for a command.
3801 ;; Quitting out of a program does not go through here;
3802 ;; that happens in the QUIT macro at the C code level.
3803 (defun keyboard-quit ()
3804 "Signal a `quit' condition.
3805 During execution of Lisp code, this character causes a quit directly.
3806 At top-level, as an editor command, this simply beeps."
3809 (setq defining-kbd-macro nil)
3812 (define-key global-map "\C-g" 'keyboard-quit)
3814 (defvar buffer-quit-function nil
3815 "Function to call to \"quit\" the current buffer, or nil if none.
3816 \\[keyboard-escape-quit] calls this function when its more local actions
3817 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3819 (defun keyboard-escape-quit ()
3820 "Exit the current \"mode\" (in a generalized sense of the word).
3821 This command can exit an interactive command such as `query-replace',
3822 can clear out a prefix argument or a region,
3823 can get out of the minibuffer or other recursive edit,
3824 cancel the use of the current buffer (for special-purpose buffers),
3825 or go back to just one window (by deleting all but the selected window)."
3827 (cond ((eq last-command 'mode-exited) nil)
3828 ((> (minibuffer-depth) 0)
3829 (abort-recursive-edit))
3832 ((and transient-mark-mode
3835 ((> (recursion-depth) 0)
3836 (exit-recursive-edit))
3837 (buffer-quit-function
3838 (funcall buffer-quit-function))
3839 ((not (one-window-p t))
3840 (delete-other-windows))
3841 ((string-match "^ \\*" (buffer-name (current-buffer)))
3844 (defun play-sound-file (file &optional volume device)
3845 "Play sound stored in FILE.
3846 VOLUME and DEVICE correspond to the keywords of the sound
3847 specification for `play-sound'."
3848 (interactive "fPlay sound file: ")
3849 (let ((sound (list :file file)))
3851 (plist-put sound :volume volume))
3853 (plist-put sound :device device))
3855 (play-sound sound)))
3857 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3859 (defcustom read-mail-command 'rmail
3860 "*Your preference for a mail reading package.
3861 This is used by some keybindings which support reading mail.
3862 See also `mail-user-agent' concerning sending mail."
3863 :type '(choice (function-item rmail)
3864 (function-item gnus)
3865 (function-item mh-rmail)
3866 (function :tag "Other"))
3870 (defcustom mail-user-agent 'sendmail-user-agent
3871 "*Your preference for a mail composition package.
3872 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3873 outgoing email message. This variable lets you specify which
3874 mail-sending package you prefer.
3876 Valid values include:
3878 `sendmail-user-agent' -- use the default Emacs Mail package.
3879 See Info node `(emacs)Sending Mail'.
3880 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3881 See Info node `(mh-e)'.
3882 `message-user-agent' -- use the Gnus Message package.
3883 See Info node `(message)'.
3884 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3885 paraphernalia, particularly the Gcc: header for
3888 Additional valid symbols may be available; check with the author of
3889 your package for details. The function should return non-nil if it
3892 See also `read-mail-command' concerning reading mail."
3893 :type '(radio (function-item :tag "Default Emacs mail"
3895 sendmail-user-agent)
3896 (function-item :tag "Emacs interface to MH"
3899 (function-item :tag "Gnus Message package"
3902 (function-item :tag "Gnus Message with full Gnus features"
3905 (function :tag "Other"))
3908 (define-mail-user-agent 'sendmail-user-agent
3909 'sendmail-user-agent-compose
3910 'mail-send-and-exit)
3912 (defun rfc822-goto-eoh ()
3913 ;; Go to header delimiter line in a mail message, following RFC822 rules
3914 (goto-char (point-min))
3915 (when (re-search-forward
3916 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
3917 (goto-char (match-beginning 0))))
3919 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3920 switch-function yank-action
3923 (let ((special-display-buffer-names nil)
3924 (special-display-regexps nil)
3925 (same-window-buffer-names nil)
3926 (same-window-regexps nil))
3927 (funcall switch-function "*mail*")))
3928 (let ((cc (cdr (assoc-string "cc" other-headers t)))
3929 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
3930 (body (cdr (assoc-string "body" other-headers t))))
3931 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3933 (error "Message aborted"))
3936 (while other-headers
3937 (unless (member-ignore-case (car (car other-headers))
3938 '("in-reply-to" "cc" "body"))
3939 (insert (car (car other-headers)) ": "
3940 (cdr (car other-headers)) "\n"))
3941 (setq other-headers (cdr other-headers)))
3947 (define-mail-user-agent 'mh-e-user-agent
3948 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3949 'mh-before-send-letter-hook)
3951 (defun compose-mail (&optional to subject other-headers continue
3952 switch-function yank-action send-actions)
3953 "Start composing a mail message to send.
3954 This uses the user's chosen mail composition package
3955 as selected with the variable `mail-user-agent'.
3956 The optional arguments TO and SUBJECT specify recipients
3957 and the initial Subject field, respectively.
3959 OTHER-HEADERS is an alist specifying additional
3960 header fields. Elements look like (HEADER . VALUE) where both
3961 HEADER and VALUE are strings.
3963 CONTINUE, if non-nil, says to continue editing a message already
3966 SWITCH-FUNCTION, if non-nil, is a function to use to
3967 switch to and display the buffer used for mail composition.
3969 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3970 to insert the raw text of the message being replied to.
3971 It has the form (FUNCTION . ARGS). The user agent will apply
3972 FUNCTION to ARGS, to insert the raw text of the original message.
3973 \(The user agent will also run `mail-citation-hook', *after* the
3974 original text has been inserted in this way.)
3976 SEND-ACTIONS is a list of actions to call when the message is sent.
3977 Each action has the form (FUNCTION . ARGS)."
3979 (list nil nil nil current-prefix-arg))
3980 (let ((function (get mail-user-agent 'composefunc)))
3981 (funcall function to subject other-headers continue
3982 switch-function yank-action send-actions)))
3984 (defun compose-mail-other-window (&optional to subject other-headers continue
3985 yank-action send-actions)
3986 "Like \\[compose-mail], but edit the outgoing message in another window."
3988 (list nil nil nil current-prefix-arg))
3989 (compose-mail to subject other-headers continue
3990 'switch-to-buffer-other-window yank-action send-actions))
3993 (defun compose-mail-other-frame (&optional to subject other-headers continue
3994 yank-action send-actions)
3995 "Like \\[compose-mail], but edit the outgoing message in another frame."
3997 (list nil nil nil current-prefix-arg))
3998 (compose-mail to subject other-headers continue
3999 'switch-to-buffer-other-frame yank-action send-actions))
4001 (defvar set-variable-value-history nil
4002 "History of values entered with `set-variable'.")
4004 (defun set-variable (var val &optional make-local)
4005 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4006 When using this interactively, enter a Lisp object for VALUE.
4007 If you want VALUE to be a string, you must surround it with doublequotes.
4008 VALUE is used literally, not evaluated.
4010 If VARIABLE has a `variable-interactive' property, that is used as if
4011 it were the arg to `interactive' (which see) to interactively read VALUE.
4013 If VARIABLE has been defined with `defcustom', then the type information
4014 in the definition is used to check that VALUE is valid.
4016 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4018 (let* ((default-var (variable-at-point))
4019 (var (if (symbolp default-var)
4020 (read-variable (format "Set variable (default %s): " default-var)
4022 (read-variable "Set variable: ")))
4023 (minibuffer-help-form '(describe-variable var))
4024 (prop (get var 'variable-interactive))
4025 (prompt (format "Set %s%s to value: " var
4026 (cond ((local-variable-p var)
4028 ((or current-prefix-arg
4029 (local-variable-if-set-p var))
4033 ;; Use VAR's `variable-interactive' property
4034 ;; as an interactive spec for prompting.
4035 (call-interactively `(lambda (arg)
4039 (read-string prompt nil
4040 'set-variable-value-history)))))
4041 (list var val current-prefix-arg)))
4043 (and (custom-variable-p var)
4044 (not (get var 'custom-type))
4045 (custom-load-symbol var))
4046 (let ((type (get var 'custom-type)))
4048 ;; Match with custom type.
4050 (setq type (widget-convert type))
4051 (unless (widget-apply type :match val)
4052 (error "Value `%S' does not match type %S of %S"
4053 val (car type) var))))
4056 (make-local-variable var))
4060 ;; Force a thorough redisplay for the case that the variable
4061 ;; has an effect on the display, like `tab-width' has.
4062 (force-mode-line-update))
4064 ;; Define the major mode for lists of completions.
4066 (defvar completion-list-mode-map nil
4067 "Local map for completion list buffers.")
4068 (or completion-list-mode-map
4069 (let ((map (make-sparse-keymap)))
4070 (define-key map [mouse-2] 'mouse-choose-completion)
4071 (define-key map [down-mouse-2] nil)
4072 (define-key map "\C-m" 'choose-completion)
4073 (define-key map "\e\e\e" 'delete-completion-window)
4074 (define-key map [left] 'previous-completion)
4075 (define-key map [right] 'next-completion)
4076 (setq completion-list-mode-map map)))
4078 ;; Completion mode is suitable only for specially formatted data.
4079 (put 'completion-list-mode 'mode-class 'special)
4081 (defvar completion-reference-buffer nil
4082 "Record the buffer that was current when the completion list was requested.
4083 This is a local variable in the completion list buffer.
4084 Initial value is nil to avoid some compiler warnings.")
4086 (defvar completion-no-auto-exit nil
4087 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4088 This also applies to other functions such as `choose-completion'
4089 and `mouse-choose-completion'.")
4091 (defvar completion-base-size nil
4092 "Number of chars at beginning of minibuffer not involved in completion.
4093 This is a local variable in the completion list buffer
4094 but it talks about the buffer in `completion-reference-buffer'.
4095 If this is nil, it means to compare text to determine which part
4096 of the tail end of the buffer's text is involved in completion.")
4098 (defun delete-completion-window ()
4099 "Delete the completion list window.
4100 Go to the window from which completion was requested."
4102 (let ((buf completion-reference-buffer))
4103 (if (one-window-p t)
4104 (if (window-dedicated-p (selected-window))
4105 (delete-frame (selected-frame)))
4106 (delete-window (selected-window))
4107 (if (get-buffer-window buf)
4108 (select-window (get-buffer-window buf))))))
4110 (defun previous-completion (n)
4111 "Move to the previous item in the completion list."
4113 (next-completion (- n)))
4115 (defun next-completion (n)
4116 "Move to the next item in the completion list.
4117 With prefix argument N, move N items (negative N means move backward)."
4119 (let ((beg (point-min)) (end (point-max)))
4120 (while (and (> n 0) (not (eobp)))
4121 ;; If in a completion, move to the end of it.
4122 (when (get-text-property (point) 'mouse-face)
4123 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4124 ;; Move to start of next one.
4125 (unless (get-text-property (point) 'mouse-face)
4126 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4128 (while (and (< n 0) (not (bobp)))
4129 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4130 ;; If in a completion, move to the start of it.
4131 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4132 (goto-char (previous-single-property-change
4133 (point) 'mouse-face nil beg)))
4134 ;; Move to end of the previous completion.
4135 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4136 (goto-char (previous-single-property-change
4137 (point) 'mouse-face nil beg)))
4138 ;; Move to the start of that one.
4139 (goto-char (previous-single-property-change
4140 (point) 'mouse-face nil beg))
4143 (defun choose-completion ()
4144 "Choose the completion that point is in or next to."
4146 (let (beg end completion (buffer completion-reference-buffer)
4147 (base-size completion-base-size))
4148 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4149 (setq end (point) beg (1+ (point))))
4150 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4151 (setq end (1- (point)) beg (point)))
4153 (error "No completion here"))
4154 (setq beg (previous-single-property-change beg 'mouse-face))
4155 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4156 (setq completion (buffer-substring beg end))
4157 (let ((owindow (selected-window)))
4158 (if (and (one-window-p t 'selected-frame)
4159 (window-dedicated-p (selected-window)))
4160 ;; This is a special buffer's frame
4161 (iconify-frame (selected-frame))
4162 (or (window-dedicated-p (selected-window))
4164 (select-window owindow))
4165 (choose-completion-string completion buffer base-size)))
4167 ;; Delete the longest partial match for STRING
4168 ;; that can be found before POINT.
4169 (defun choose-completion-delete-max-match (string)
4170 (let ((opoint (point))
4172 ;; Try moving back by the length of the string.
4173 (goto-char (max (- (point) (length string))
4174 (minibuffer-prompt-end)))
4175 ;; See how far back we were actually able to move. That is the
4176 ;; upper bound on how much we can match and delete.
4177 (setq len (- opoint (point)))
4178 (if completion-ignore-case
4179 (setq string (downcase string)))
4180 (while (and (> len 0)
4181 (let ((tail (buffer-substring (point) opoint)))
4182 (if completion-ignore-case
4183 (setq tail (downcase tail)))
4184 (not (string= tail (substring string 0 len)))))
4189 (defvar choose-completion-string-functions nil
4190 "Functions that may override the normal insertion of a completion choice.
4191 These functions are called in order with four arguments:
4192 CHOICE - the string to insert in the buffer,
4193 BUFFER - the buffer in which the choice should be inserted,
4194 MINI-P - non-nil iff BUFFER is a minibuffer, and
4195 BASE-SIZE - the number of characters in BUFFER before
4196 the string being completed.
4198 If a function in the list returns non-nil, that function is supposed
4199 to have inserted the CHOICE in the BUFFER, and possibly exited
4200 the minibuffer; no further functions will be called.
4202 If all functions in the list return nil, that means to use
4203 the default method of inserting the completion in BUFFER.")
4205 (defun choose-completion-string (choice &optional buffer base-size)
4206 "Switch to BUFFER and insert the completion choice CHOICE.
4207 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4208 to keep. If it is nil, we call `choose-completion-delete-max-match'
4209 to decide what to delete."
4211 ;; If BUFFER is the minibuffer, exit the minibuffer
4212 ;; unless it is reading a file name and CHOICE is a directory,
4213 ;; or completion-no-auto-exit is non-nil.
4215 (let* ((buffer (or buffer completion-reference-buffer))
4216 (mini-p (minibufferp buffer)))
4217 ;; If BUFFER is a minibuffer, barf unless it's the currently
4218 ;; active minibuffer.
4220 (or (not (active-minibuffer-window))
4222 (window-buffer (active-minibuffer-window))))))
4223 (error "Minibuffer is not active for completion")
4224 (unless (run-hook-with-args-until-success
4225 'choose-completion-string-functions
4226 choice buffer mini-p base-size)
4227 ;; Insert the completion into the buffer where it was requested.
4230 (delete-region (+ base-size (if mini-p
4231 (minibuffer-prompt-end)
4234 (choose-completion-delete-max-match choice))
4236 (remove-text-properties (- (point) (length choice)) (point)
4238 ;; Update point in the window that BUFFER is showing in.
4239 (let ((window (get-buffer-window buffer t)))
4240 (set-window-point window (point)))
4241 ;; If completing for the minibuffer, exit it with this choice.
4242 (and (not completion-no-auto-exit)
4243 (equal buffer (window-buffer (minibuffer-window)))
4244 minibuffer-completion-table
4245 ;; If this is reading a file name, and the file name chosen
4246 ;; is a directory, don't exit the minibuffer.
4247 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4248 (file-directory-p (field-string (point-max))))
4249 (let ((mini (active-minibuffer-window)))
4250 (select-window mini)
4251 (when minibuffer-auto-raise
4252 (raise-frame (window-frame mini))))
4253 (exit-minibuffer)))))))
4255 (defun completion-list-mode ()
4256 "Major mode for buffers showing lists of possible completions.
4257 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4258 to select the completion near point.
4259 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4262 (kill-all-local-variables)
4263 (use-local-map completion-list-mode-map)
4264 (setq mode-name "Completion List")
4265 (setq major-mode 'completion-list-mode)
4266 (make-local-variable 'completion-base-size)
4267 (setq completion-base-size nil)
4268 (run-hooks 'completion-list-mode-hook))
4270 (defun completion-list-mode-finish ()
4271 "Finish setup of the completions buffer.
4272 Called from `temp-buffer-show-hook'."
4273 (when (eq major-mode 'completion-list-mode)
4274 (toggle-read-only 1)))
4276 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4278 (defvar completion-setup-hook nil
4279 "Normal hook run at the end of setting up a completion list buffer.
4280 When this hook is run, the current buffer is the one in which the
4281 command to display the completion list buffer was run.
4282 The completion list buffer is available as the value of `standard-output'.")
4284 ;; This function goes in completion-setup-hook, so that it is called
4285 ;; after the text of the completion list buffer is written.
4286 (defface completions-first-difference
4287 '((t (:inherit bold)))
4288 "Face put on the first uncommon character in completions in *Completions* buffer."
4291 (defface completions-common-part
4292 '((t (:inherit default)))
4293 "Face put on the common prefix substring in completions in *Completions* buffer.
4294 The idea of `completions-common-part' is that you can use it to
4295 make the common parts less visible than normal, so that the rest
4296 of the differing parts is, by contrast, slightly highlighted."
4299 (defun completion-setup-function ()
4300 (let ((mainbuf (current-buffer))
4301 (mbuf-contents (minibuffer-contents)))
4302 ;; When reading a file name in the minibuffer,
4303 ;; set default-directory in the minibuffer
4304 ;; so it will get copied into the completion list buffer.
4305 (if minibuffer-completing-file-name
4306 (with-current-buffer mainbuf
4307 (setq default-directory (file-name-directory mbuf-contents))))
4308 (with-current-buffer standard-output
4309 (completion-list-mode)
4310 (make-local-variable 'completion-reference-buffer)
4311 (setq completion-reference-buffer mainbuf)
4312 (if minibuffer-completing-file-name
4313 ;; For file name completion,
4314 ;; use the number of chars before the start of the
4315 ;; last file name component.
4316 (setq completion-base-size
4317 (with-current-buffer mainbuf
4319 (goto-char (point-max))
4320 (skip-chars-backward "^/")
4321 (- (point) (minibuffer-prompt-end)))))
4322 ;; Otherwise, in minibuffer, the whole input is being completed.
4323 (if (minibufferp mainbuf)
4324 (setq completion-base-size 0)))
4325 ;; Put faces on first uncommon characters and common parts.
4326 (when completion-base-size
4327 (let* ((common-string-length
4328 (- (length mbuf-contents) completion-base-size))
4329 (element-start (next-single-property-change
4333 (+ (or element-start nil) common-string-length))
4335 (while (and element-start (< element-common-end maxp))
4336 (when (and (get-char-property element-start 'mouse-face)
4337 (get-char-property element-common-end 'mouse-face))
4338 (put-text-property element-start element-common-end
4339 'font-lock-face 'completions-common-part)
4340 (put-text-property element-common-end (1+ element-common-end)
4341 'font-lock-face 'completions-first-difference))
4342 (setq element-start (next-single-property-change
4346 (setq element-common-end (+ element-start common-string-length))))))
4347 ;; Insert help string.
4348 (goto-char (point-min))
4349 (if (display-mouse-p)
4350 (insert (substitute-command-keys
4351 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4352 (insert (substitute-command-keys
4353 "In this buffer, type \\[choose-completion] to \
4354 select the completion near point.\n\n")))))
4356 (add-hook 'completion-setup-hook 'completion-setup-function)
4358 (define-key minibuffer-local-completion-map [prior]
4359 'switch-to-completions)
4360 (define-key minibuffer-local-must-match-map [prior]
4361 'switch-to-completions)
4362 (define-key minibuffer-local-completion-map "\M-v"
4363 'switch-to-completions)
4364 (define-key minibuffer-local-must-match-map "\M-v"
4365 'switch-to-completions)
4367 (defun switch-to-completions ()
4368 "Select the completion list window."
4370 ;; Make sure we have a completions window.
4371 (or (get-buffer-window "*Completions*")
4372 (minibuffer-completion-help))
4373 (let ((window (get-buffer-window "*Completions*")))
4375 (select-window window)
4376 (goto-char (point-min))
4377 (search-forward "\n\n")
4380 ;; Support keyboard commands to turn on various modifiers.
4382 ;; These functions -- which are not commands -- each add one modifier
4383 ;; to the following event.
4385 (defun event-apply-alt-modifier (ignore-prompt)
4386 "\\<function-key-map>Add the Alt modifier to the following event.
4387 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
4388 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
4389 (defun event-apply-super-modifier (ignore-prompt)
4390 "\\<function-key-map>Add the Super modifier to the following event.
4391 For example, type \\[event-apply-super-modifier] & to enter Super-&."
4392 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
4393 (defun event-apply-hyper-modifier (ignore-prompt)
4394 "\\<function-key-map>Add the Hyper modifier to the following event.
4395 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
4396 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
4397 (defun event-apply-shift-modifier (ignore-prompt)
4398 "\\<function-key-map>Add the Shift modifier to the following event.
4399 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
4400 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
4401 (defun event-apply-control-modifier (ignore-prompt)
4402 "\\<function-key-map>Add the Ctrl modifier to the following event.
4403 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
4404 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
4405 (defun event-apply-meta-modifier (ignore-prompt)
4406 "\\<function-key-map>Add the Meta modifier to the following event.
4407 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
4408 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
4410 (defun event-apply-modifier (event symbol lshiftby prefix)
4411 "Apply a modifier flag to event EVENT.
4412 SYMBOL is the name of this modifier, as a symbol.
4413 LSHIFTBY is the numeric value of this modifier, in keyboard events.
4414 PREFIX is the string that represents this modifier in an event type symbol."
4416 (cond ((eq symbol 'control)
4417 (if (and (<= (downcase event) ?z)
4418 (>= (downcase event) ?a))
4419 (- (downcase event) ?a -1)
4420 (if (and (<= (downcase event) ?Z)
4421 (>= (downcase event) ?A))
4422 (- (downcase event) ?A -1)
4423 (logior (lsh 1 lshiftby) event))))
4425 (if (and (<= (downcase event) ?z)
4426 (>= (downcase event) ?a))
4428 (logior (lsh 1 lshiftby) event)))
4430 (logior (lsh 1 lshiftby) event)))
4431 (if (memq symbol (event-modifiers event))
4433 (let ((event-type (if (symbolp event) event (car event))))
4434 (setq event-type (intern (concat prefix (symbol-name event-type))))
4437 (cons event-type (cdr event)))))))
4439 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4440 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4441 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4442 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4443 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4444 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4446 ;;;; Keypad support.
4448 ;;; Make the keypad keys act like ordinary typing keys. If people add
4449 ;;; bindings for the function key symbols, then those bindings will
4450 ;;; override these, so this shouldn't interfere with any existing
4453 ;; Also tell read-char how to handle these keys.
4455 (lambda (keypad-normal)
4456 (let ((keypad (nth 0 keypad-normal))
4457 (normal (nth 1 keypad-normal)))
4458 (put keypad 'ascii-character normal)
4459 (define-key function-key-map (vector keypad) (vector normal))))
4460 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4461 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4474 ;;;; forking a twin copy of a buffer.
4477 (defvar clone-buffer-hook nil
4478 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4480 (defun clone-process (process &optional newname)
4481 "Create a twin copy of PROCESS.
4482 If NEWNAME is nil, it defaults to PROCESS' name;
4483 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4484 If PROCESS is associated with a buffer, the new process will be associated
4485 with the current buffer instead.
4486 Returns nil if PROCESS has already terminated."
4487 (setq newname (or newname (process-name process)))
4488 (if (string-match "<[0-9]+>\\'" newname)
4489 (setq newname (substring newname 0 (match-beginning 0))))
4490 (when (memq (process-status process) '(run stop open))
4491 (let* ((process-connection-type (process-tty-name process))
4493 (if (memq (process-status process) '(open))
4494 (let ((args (process-contact process t)))
4495 (setq args (plist-put args :name newname))
4496 (setq args (plist-put args :buffer
4497 (if (process-buffer process)
4499 (apply 'make-network-process args))
4500 (apply 'start-process newname
4501 (if (process-buffer process) (current-buffer))
4502 (process-command process)))))
4503 (set-process-query-on-exit-flag
4504 new-process (process-query-on-exit-flag process))
4505 (set-process-inherit-coding-system-flag
4506 new-process (process-inherit-coding-system-flag process))
4507 (set-process-filter new-process (process-filter process))
4508 (set-process-sentinel new-process (process-sentinel process))
4509 (set-process-plist new-process (copy-sequence (process-plist process)))
4512 ;; things to maybe add (currently partly covered by `funcall mode'):
4515 (defun clone-buffer (&optional newname display-flag)
4516 "Create and return a twin copy of the current buffer.
4517 Unlike an indirect buffer, the new buffer can be edited
4518 independently of the old one (if it is not read-only).
4519 NEWNAME is the name of the new buffer. It may be modified by
4520 adding or incrementing <N> at the end as necessary to create a
4521 unique buffer name. If nil, it defaults to the name of the
4522 current buffer, with the proper suffix. If DISPLAY-FLAG is
4523 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
4524 clone a file-visiting buffer, or a buffer whose major mode symbol
4525 has a non-nil `no-clone' property, results in an error.
4527 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
4528 current buffer with appropriate suffix. However, if a prefix
4529 argument is given, then the command prompts for NEWNAME in the
4532 This runs the normal hook `clone-buffer-hook' in the new buffer
4533 after it has been set up properly in other respects."
4536 (if buffer-file-name
4537 (error "Cannot clone a file-visiting buffer"))
4538 (if (get major-mode 'no-clone)
4539 (error "Cannot clone a buffer in %s mode" mode-name))
4540 (list (if current-prefix-arg (read-string "Name: "))
4542 (if buffer-file-name
4543 (error "Cannot clone a file-visiting buffer"))
4544 (if (get major-mode 'no-clone)
4545 (error "Cannot clone a buffer in %s mode" mode-name))
4546 (setq newname (or newname (buffer-name)))
4547 (if (string-match "<[0-9]+>\\'" newname)
4548 (setq newname (substring newname 0 (match-beginning 0))))
4549 (let ((buf (current-buffer))
4553 (mk (if mark-active (mark t)))
4554 (modified (buffer-modified-p))
4556 (lvars (buffer-local-variables))
4557 (process (get-buffer-process (current-buffer)))
4558 (new (generate-new-buffer (or newname (buffer-name)))))
4561 (with-current-buffer new
4562 (insert-buffer-substring buf)))
4563 (with-current-buffer new
4564 (narrow-to-region ptmin ptmax)
4566 (if mk (set-mark mk))
4567 (set-buffer-modified-p modified)
4569 ;; Clone the old buffer's process, if any.
4570 (when process (clone-process process))
4572 ;; Now set up the major mode.
4575 ;; Set up other local variables.
4577 (condition-case () ;in case var is read-only
4580 (set (make-local-variable (car v)) (cdr v)))
4584 ;; Run any hooks (typically set up by the major mode
4585 ;; for cloning to work properly).
4586 (run-hooks 'clone-buffer-hook))
4587 (if display-flag (pop-to-buffer new))
4591 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4592 "Create an indirect buffer that is a twin copy of the current buffer.
4594 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4595 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4596 or if not called with a prefix arg, NEWNAME defaults to the current
4597 buffer's name. The name is modified by adding a `<N>' suffix to it
4598 or by incrementing the N in an existing suffix.
4600 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4601 This is always done when called interactively.
4603 Optional last arg NORECORD non-nil means do not put this buffer at the
4604 front of the list of recently selected ones."
4607 (if (get major-mode 'no-clone-indirect)
4608 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4609 (list (if current-prefix-arg
4610 (read-string "BName of indirect buffer: "))
4612 (if (get major-mode 'no-clone-indirect)
4613 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4614 (setq newname (or newname (buffer-name)))
4615 (if (string-match "<[0-9]+>\\'" newname)
4616 (setq newname (substring newname 0 (match-beginning 0))))
4617 (let* ((name (generate-new-buffer-name newname))
4618 (buffer (make-indirect-buffer (current-buffer) name t)))
4620 (pop-to-buffer buffer norecord))
4624 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4625 "Create an indirect buffer that is a twin copy of BUFFER.
4626 Select the new buffer in another window.
4627 Optional second arg NORECORD non-nil means do not put this buffer at
4628 the front of the list of recently selected ones."
4629 (interactive "bClone buffer in other window: ")
4630 (let ((pop-up-windows t))
4632 (clone-indirect-buffer nil t norecord)))
4634 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4636 ;;; Handling of Backspace and Delete keys.
4638 (defcustom normal-erase-is-backspace nil
4639 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4641 On window systems, the default value of this option is chosen
4642 according to the keyboard used. If the keyboard has both a Backspace
4643 key and a Delete key, and both are mapped to their usual meanings, the
4644 option's default value is set to t, so that Backspace can be used to
4645 delete backward, and Delete can be used to delete forward.
4647 If not running under a window system, customizing this option accomplishes
4648 a similar effect by mapping C-h, which is usually generated by the
4649 Backspace key, to DEL, and by mapping DEL to C-d via
4650 `keyboard-translate'. The former functionality of C-h is available on
4651 the F1 key. You should probably not use this setting if you don't
4652 have both Backspace, Delete and F1 keys.
4654 Setting this variable with setq doesn't take effect. Programmatically,
4655 call `normal-erase-is-backspace-mode' (which see) instead."
4657 :group 'editing-basics
4659 :set (lambda (symbol value)
4660 ;; The fboundp is because of a problem with :set when
4661 ;; dumping Emacs. It doesn't really matter.
4662 (if (fboundp 'normal-erase-is-backspace-mode)
4663 (normal-erase-is-backspace-mode (or value 0))
4664 (set-default symbol value))))
4667 (defun normal-erase-is-backspace-mode (&optional arg)
4668 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4670 With numeric arg, turn the mode on if and only if ARG is positive.
4672 On window systems, when this mode is on, Delete is mapped to C-d and
4673 Backspace is mapped to DEL; when this mode is off, both Delete and
4674 Backspace are mapped to DEL. (The remapping goes via
4675 `function-key-map', so binding Delete or Backspace in the global or
4676 local keymap will override that.)
4678 In addition, on window systems, the bindings of C-Delete, M-Delete,
4679 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4680 the global keymap in accordance with the functionality of Delete and
4681 Backspace. For example, if Delete is remapped to C-d, which deletes
4682 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4683 to DEL, which deletes backward, C-Delete is bound to
4684 `backward-kill-word'.
4686 If not running on a window system, a similar effect is accomplished by
4687 remapping C-h (normally produced by the Backspace key) and DEL via
4688 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4689 to C-d; if it's off, the keys are not remapped.
4691 When not running on a window system, and this mode is turned on, the
4692 former functionality of C-h is available on the F1 key. You should
4693 probably not turn on this mode on a text-only terminal if you don't
4694 have both Backspace, Delete and F1 keys.
4696 See also `normal-erase-is-backspace'."
4698 (setq normal-erase-is-backspace
4700 (> (prefix-numeric-value arg) 0)
4701 (not normal-erase-is-backspace)))
4703 (cond ((or (memq window-system '(x w32 mac pc))
4704 (memq system-type '(ms-dos windows-nt)))
4706 `(([C-delete] [C-backspace])
4707 ([M-delete] [M-backspace])
4708 ([C-M-delete] [C-M-backspace])
4710 [C-delete] [C-backspace])))
4711 (old-state (lookup-key function-key-map [delete])))
4713 (if normal-erase-is-backspace
4715 (define-key function-key-map [delete] [?\C-d])
4716 (define-key function-key-map [kp-delete] [?\C-d])
4717 (define-key function-key-map [backspace] [?\C-?]))
4718 (define-key function-key-map [delete] [?\C-?])
4719 (define-key function-key-map [kp-delete] [?\C-?])
4720 (define-key function-key-map [backspace] [?\C-?]))
4722 ;; Maybe swap bindings of C-delete and C-backspace, etc.
4723 (unless (equal old-state (lookup-key function-key-map [delete]))
4724 (dolist (binding bindings)
4725 (let ((map global-map))
4726 (when (keymapp (car binding))
4727 (setq map (car binding) binding (cdr binding)))
4728 (let* ((key1 (nth 0 binding))
4729 (key2 (nth 1 binding))
4730 (binding1 (lookup-key map key1))
4731 (binding2 (lookup-key map key2)))
4732 (define-key map key1 binding2)
4733 (define-key map key2 binding1)))))))
4735 (if normal-erase-is-backspace
4737 (keyboard-translate ?\C-h ?\C-?)
4738 (keyboard-translate ?\C-? ?\C-d))
4739 (keyboard-translate ?\C-h ?\C-h)
4740 (keyboard-translate ?\C-? ?\C-?))))
4742 (run-hooks 'normal-erase-is-backspace-hook)
4744 (message "Delete key deletes %s"
4745 (if normal-erase-is-backspace "forward" "backward"))))
4747 (defcustom idle-update-delay 0.5
4748 "*Idle time delay before updating various things on the screen.
4749 Various Emacs features that update auxiliary information when point moves
4750 wait this many seconds after Emacs becomes idle before doing an update."
4755 (defvar vis-mode-saved-buffer-invisibility-spec nil
4756 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
4758 (define-minor-mode visible-mode
4759 "Toggle Visible mode.
4760 With argument ARG turn Visible mode on iff ARG is positive.
4762 Enabling Visible mode makes all invisible text temporarily visible.
4763 Disabling Visible mode turns off that effect. Visible mode
4764 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
4766 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
4767 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
4768 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
4770 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
4771 buffer-invisibility-spec)
4772 (setq buffer-invisibility-spec nil)))
4774 ;; Minibuffer prompt stuff.
4776 ;(defun minibuffer-prompt-modification (start end)
4777 ; (error "You cannot modify the prompt"))
4780 ;(defun minibuffer-prompt-insertion (start end)
4781 ; (let ((inhibit-modification-hooks t))
4782 ; (delete-region start end)
4783 ; ;; Discard undo information for the text insertion itself
4784 ; ;; and for the text deletion.above.
4785 ; (when (consp buffer-undo-list)
4786 ; (setq buffer-undo-list (cddr buffer-undo-list)))
4787 ; (message "You cannot modify the prompt")))
4790 ;(setq minibuffer-prompt-properties
4791 ; (list 'modification-hooks '(minibuffer-prompt-modification)
4792 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
4797 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
4798 ;;; simple.el ends here