* font-lock.el (lisp-font-lock-keywords-1): Match `deftheme'.
[emacs.git] / lisp / simple.el
blobac531e9674cb177112a79c1155d59eeb58d4a745
1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99,
4 ;; 2000, 2001, 2002, 2003
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: internal
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; A grab-bag of basic Emacs commands not specifically related to some
30 ;; major mode or to file-handling.
32 ;;; Code:
34 (eval-when-compile
35 (autoload 'widget-convert "wid-edit")
36 (autoload 'shell-mode "shell"))
39 (defgroup killing nil
40 "Killing and yanking commands"
41 :group 'editing)
43 (defgroup paren-matching nil
44 "Highlight (un)matching of parens and expressions."
45 :group 'matching)
47 (define-key global-map [?\C-x right] 'next-buffer)
48 (define-key global-map [?\C-x left] 'prev-buffer)
49 (defun next-buffer ()
50 "Switch to the next buffer in cyclic order."
51 (interactive)
52 (let ((buffer (current-buffer)))
53 (switch-to-buffer (other-buffer buffer))
54 (bury-buffer buffer)))
56 (defun prev-buffer ()
57 "Switch to the previous buffer in cyclic order."
58 (interactive)
59 (let ((list (nreverse (buffer-list)))
60 found)
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))))
65 (setq found buffer)))
66 (setq list (cdr list)))
67 (switch-to-buffer found)))
69 (defun fundamental-mode ()
70 "Major mode not specialized for anything in particular.
71 Other major modes are defined by comparison with this one."
72 (interactive)
73 (kill-all-local-variables))
75 ;; Making and deleting lines.
77 (defun newline (&optional arg)
78 "Insert a newline, and move to left margin of the new line if it's blank.
79 If `use-hard-newlines' is non-nil, the newline is marked with the
80 text-property `hard'.
81 With ARG, insert that many newlines.
82 Call `auto-fill-function' if the current column number is greater
83 than the value of `fill-column' and ARG is `nil'."
84 (interactive "*P")
85 (barf-if-buffer-read-only)
86 ;; Inserting a newline at the end of a line produces better redisplay in
87 ;; try_window_id than inserting at the beginning of a line, and the textual
88 ;; result is the same. So, if we're at beginning of line, pretend to be at
89 ;; the end of the previous line.
90 (let ((flag (and (not (bobp))
91 (bolp)
92 ;; Make sure no functions want to be told about
93 ;; the range of the changes.
94 (not after-change-functions)
95 (not before-change-functions)
96 ;; Make sure there are no markers here.
97 (not (buffer-has-markers-at (1- (point))))
98 (not (buffer-has-markers-at (point)))
99 ;; Make sure no text properties want to know
100 ;; where the change was.
101 (not (get-char-property (1- (point)) 'modification-hooks))
102 (not (get-char-property (1- (point)) 'insert-behind-hooks))
103 (or (eobp)
104 (not (get-char-property (point) 'insert-in-front-hooks)))
105 ;; Make sure the newline before point isn't intangible.
106 (not (get-char-property (1- (point)) 'intangible))
107 ;; Make sure the newline before point isn't read-only.
108 (not (get-char-property (1- (point)) 'read-only))
109 ;; Make sure the newline before point isn't invisible.
110 (not (get-char-property (1- (point)) 'invisible))
111 ;; Make sure the newline before point has the same
112 ;; properties as the char before it (if any).
113 (< (or (previous-property-change (point)) -2)
114 (- (point) 2))))
115 (was-page-start (and (bolp)
116 (looking-at page-delimiter)))
117 (beforepos (point)))
118 (if flag (backward-char 1))
119 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
120 ;; Set last-command-char to tell self-insert what to insert.
121 (let ((last-command-char ?\n)
122 ;; Don't auto-fill if we have a numeric argument.
123 ;; Also not if flag is true (it would fill wrong line);
124 ;; there is no need to since we're at BOL.
125 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
126 (unwind-protect
127 (self-insert-command (prefix-numeric-value arg))
128 ;; If we get an error in self-insert-command, put point at right place.
129 (if flag (forward-char 1))))
130 ;; Even if we did *not* get an error, keep that forward-char;
131 ;; all further processing should apply to the newline that the user
132 ;; thinks he inserted.
134 ;; Mark the newline(s) `hard'.
135 (if use-hard-newlines
136 (set-hard-newline-properties
137 (- (point) (if arg (prefix-numeric-value arg) 1)) (point)))
138 ;; If the newline leaves the previous line blank,
139 ;; and we have a left margin, delete that from the blank line.
140 (or flag
141 (save-excursion
142 (goto-char beforepos)
143 (beginning-of-line)
144 (and (looking-at "[ \t]$")
145 (> (current-left-margin) 0)
146 (delete-region (point) (progn (end-of-line) (point))))))
147 ;; Indent the line after the newline, except in one case:
148 ;; when we added the newline at the beginning of a line
149 ;; which starts a page.
150 (or was-page-start
151 (move-to-left-margin nil t)))
152 nil)
154 (defun set-hard-newline-properties (from to)
155 (let ((sticky (get-text-property from 'rear-nonsticky)))
156 (put-text-property from to 'hard 't)
157 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
158 (if (and (listp sticky) (not (memq 'hard sticky)))
159 (put-text-property from (point) 'rear-nonsticky
160 (cons 'hard sticky)))))
162 (defun open-line (arg)
163 "Insert a newline and leave point before it.
164 If there is a fill prefix and/or a left-margin, insert them on the new line
165 if the line would have been blank.
166 With arg N, insert N newlines."
167 (interactive "*p")
168 (let* ((do-fill-prefix (and fill-prefix (bolp)))
169 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
170 (loc (point))
171 ;; Don't expand an abbrev before point.
172 (abbrev-mode nil))
173 (newline arg)
174 (goto-char loc)
175 (while (> arg 0)
176 (cond ((bolp)
177 (if do-left-margin (indent-to (current-left-margin)))
178 (if do-fill-prefix (insert-and-inherit fill-prefix))))
179 (forward-line 1)
180 (setq arg (1- arg)))
181 (goto-char loc)
182 (end-of-line)))
184 (defun split-line (&optional arg)
185 "Split current line, moving portion beyond point vertically down.
186 If the current line starts with `fill-prefix', insert it on the new
187 line as well. With prefix arg, don't insert fill-prefix on new line.
189 When called from Lisp code, the arg may be a prefix string to copy."
190 (interactive "*P")
191 (skip-chars-forward " \t")
192 (let* ((col (current-column))
193 (pos (point))
194 ;; What prefix should we check for (nil means don't).
195 (prefix (cond ((stringp arg) arg)
196 (arg nil)
197 (t fill-prefix)))
198 ;; Does this line start with it?
199 (have-prfx (and prefix
200 (save-excursion
201 (beginning-of-line)
202 (looking-at (regexp-quote prefix))))))
203 (newline 1)
204 (if have-prfx (insert-and-inherit prefix))
205 (indent-to col 0)
206 (goto-char pos)))
208 (defun delete-indentation (&optional arg)
209 "Join this line to previous and fix up whitespace at join.
210 If there is a fill prefix, delete it from the beginning of this line.
211 With argument, join this line to following line."
212 (interactive "*P")
213 (beginning-of-line)
214 (if arg (forward-line 1))
215 (if (eq (preceding-char) ?\n)
216 (progn
217 (delete-region (point) (1- (point)))
218 ;; If the second line started with the fill prefix,
219 ;; delete the prefix.
220 (if (and fill-prefix
221 (<= (+ (point) (length fill-prefix)) (point-max))
222 (string= fill-prefix
223 (buffer-substring (point)
224 (+ (point) (length fill-prefix)))))
225 (delete-region (point) (+ (point) (length fill-prefix))))
226 (fixup-whitespace))))
228 (defalias 'join-line #'delete-indentation) ; easier to find
230 (defun delete-blank-lines ()
231 "On blank line, delete all surrounding blank lines, leaving just one.
232 On isolated blank line, delete that one.
233 On nonblank line, delete any immediately following blank lines."
234 (interactive "*")
235 (let (thisblank singleblank)
236 (save-excursion
237 (beginning-of-line)
238 (setq thisblank (looking-at "[ \t]*$"))
239 ;; Set singleblank if there is just one blank line here.
240 (setq singleblank
241 (and thisblank
242 (not (looking-at "[ \t]*\n[ \t]*$"))
243 (or (bobp)
244 (progn (forward-line -1)
245 (not (looking-at "[ \t]*$")))))))
246 ;; Delete preceding blank lines, and this one too if it's the only one.
247 (if thisblank
248 (progn
249 (beginning-of-line)
250 (if singleblank (forward-line 1))
251 (delete-region (point)
252 (if (re-search-backward "[^ \t\n]" nil t)
253 (progn (forward-line 1) (point))
254 (point-min)))))
255 ;; Delete following blank lines, unless the current line is blank
256 ;; and there are no following blank lines.
257 (if (not (and thisblank singleblank))
258 (save-excursion
259 (end-of-line)
260 (forward-line 1)
261 (delete-region (point)
262 (if (re-search-forward "[^ \t\n]" nil t)
263 (progn (beginning-of-line) (point))
264 (point-max)))))
265 ;; Handle the special case where point is followed by newline and eob.
266 ;; Delete the line, leaving point at eob.
267 (if (looking-at "^[ \t]*\n\\'")
268 (delete-region (point) (point-max)))))
270 (defun delete-trailing-whitespace ()
271 "Delete all the trailing whitespace across the current buffer.
272 All whitespace after the last non-whitespace character in a line is deleted.
273 This respects narrowing, created by \\[narrow-to-region] and friends.
274 A formfeed is not considered whitespace by this function."
275 (interactive "*")
276 (save-match-data
277 (save-excursion
278 (goto-char (point-min))
279 (while (re-search-forward "\\s-$" nil t)
280 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
281 ;; Don't delete formfeeds, even if they are considered whitespace.
282 (save-match-data
283 (if (looking-at ".*\f")
284 (goto-char (match-end 0))))
285 (delete-region (point) (match-end 0))))))
287 (defun newline-and-indent ()
288 "Insert a newline, then indent according to major mode.
289 Indentation is done using the value of `indent-line-function'.
290 In programming language modes, this is the same as TAB.
291 In some text modes, where TAB inserts a tab, this command indents to the
292 column specified by the function `current-left-margin'."
293 (interactive "*")
294 (delete-horizontal-space t)
295 (newline)
296 (indent-according-to-mode))
298 (defun reindent-then-newline-and-indent ()
299 "Reindent current line, insert newline, then indent the new line.
300 Indentation of both lines is done according to the current major mode,
301 which means calling the current value of `indent-line-function'.
302 In programming language modes, this is the same as TAB.
303 In some text modes, where TAB inserts a tab, this indents to the
304 column specified by the function `current-left-margin'."
305 (interactive "*")
306 (delete-horizontal-space t)
307 (let ((pos (point)))
308 ;; Be careful to insert the newline before indenting the line.
309 ;; Otherwise, the indentation might be wrong.
310 (newline)
311 (save-excursion
312 (goto-char pos)
313 (indent-according-to-mode))
314 (indent-according-to-mode)))
316 (defun quoted-insert (arg)
317 "Read next input character and insert it.
318 This is useful for inserting control characters.
320 If the first character you type after this command is an octal digit,
321 you should type a sequence of octal digits which specify a character code.
322 Any nondigit terminates the sequence. If the terminator is a RET,
323 it is discarded; any other terminator is used itself as input.
324 The variable `read-quoted-char-radix' specifies the radix for this feature;
325 set it to 10 or 16 to use decimal or hex instead of octal.
327 In overwrite mode, this function inserts the character anyway, and
328 does not handle octal digits specially. This means that if you use
329 overwrite as your normal editing mode, you can use this function to
330 insert characters when necessary.
332 In binary overwrite mode, this function does overwrite, and octal
333 digits are interpreted as a character code. This is intended to be
334 useful for editing binary files."
335 (interactive "*p")
336 (let* ((char (let (translation-table-for-input)
337 (if (or (not overwrite-mode)
338 (eq overwrite-mode 'overwrite-mode-binary))
339 (read-quoted-char)
340 (read-char)))))
341 ;; Assume character codes 0240 - 0377 stand for characters in some
342 ;; single-byte character set, and convert them to Emacs
343 ;; characters.
344 (if (and enable-multibyte-characters
345 (>= char ?\240)
346 (<= char ?\377))
347 (setq char (unibyte-char-to-multibyte char)))
348 (if (> arg 0)
349 (if (eq overwrite-mode 'overwrite-mode-binary)
350 (delete-char arg)))
351 (while (> arg 0)
352 (insert-and-inherit char)
353 (setq arg (1- arg)))))
355 (defun forward-to-indentation (arg)
356 "Move forward ARG lines and position at first nonblank character."
357 (interactive "p")
358 (forward-line arg)
359 (skip-chars-forward " \t"))
361 (defun backward-to-indentation (arg)
362 "Move backward ARG lines and position at first nonblank character."
363 (interactive "p")
364 (forward-line (- arg))
365 (skip-chars-forward " \t"))
367 (defun back-to-indentation ()
368 "Move point to the first non-whitespace character on this line."
369 (interactive)
370 (beginning-of-line 1)
371 (skip-chars-forward " \t"))
373 (defun fixup-whitespace ()
374 "Fixup white space between objects around point.
375 Leave one space or none, according to the context."
376 (interactive "*")
377 (save-excursion
378 (delete-horizontal-space)
379 (if (or (looking-at "^\\|\\s)")
380 (save-excursion (forward-char -1)
381 (looking-at "$\\|\\s(\\|\\s'")))
383 (insert ?\ ))))
385 (defun delete-horizontal-space (&optional backward-only)
386 "Delete all spaces and tabs around point.
387 If BACKWARD-ONLY is non-nil, only delete spaces before point."
388 (interactive "*")
389 (let ((orig-pos (point)))
390 (delete-region
391 (if backward-only
392 orig-pos
393 (progn
394 (skip-chars-forward " \t")
395 (constrain-to-field nil orig-pos t)))
396 (progn
397 (skip-chars-backward " \t")
398 (constrain-to-field nil orig-pos)))))
400 (defun just-one-space ()
401 "Delete all spaces and tabs around point, leaving one space."
402 (interactive "*")
403 (let ((orig-pos (point)))
404 (skip-chars-backward " \t")
405 (constrain-to-field nil orig-pos)
406 (if (= (following-char) ? )
407 (forward-char 1)
408 (insert ? ))
409 (delete-region
410 (point)
411 (progn
412 (skip-chars-forward " \t")
413 (constrain-to-field nil orig-pos t)))))
415 (defun beginning-of-buffer (&optional arg)
416 "Move point to the beginning of the buffer; leave mark at previous position.
417 With arg N, put point N/10 of the way from the beginning.
419 If the buffer is narrowed, this command uses the beginning and size
420 of the accessible part of the buffer.
422 Don't use this command in Lisp programs!
423 \(goto-char (point-min)) is faster and avoids clobbering the mark."
424 (interactive "P")
425 (push-mark)
426 (let ((size (- (point-max) (point-min))))
427 (goto-char (if arg
428 (+ (point-min)
429 (if (> size 10000)
430 ;; Avoid overflow for large buffer sizes!
431 (* (prefix-numeric-value arg)
432 (/ size 10))
433 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
434 (point-min))))
435 (if arg (forward-line 1)))
437 (defun end-of-buffer (&optional arg)
438 "Move point to the end of the buffer; leave mark at previous position.
439 With arg N, put point N/10 of the way from the end.
441 If the buffer is narrowed, this command uses the beginning and size
442 of the accessible part of the buffer.
444 Don't use this command in Lisp programs!
445 \(goto-char (point-max)) is faster and avoids clobbering the mark."
446 (interactive "P")
447 (push-mark)
448 (let ((size (- (point-max) (point-min))))
449 (goto-char (if arg
450 (- (point-max)
451 (if (> size 10000)
452 ;; Avoid overflow for large buffer sizes!
453 (* (prefix-numeric-value arg)
454 (/ size 10))
455 (/ (* size (prefix-numeric-value arg)) 10)))
456 (point-max))))
457 ;; If we went to a place in the middle of the buffer,
458 ;; adjust it to the beginning of a line.
459 (cond (arg (forward-line 1))
460 ((> (point) (window-end nil t))
461 ;; If the end of the buffer is not already on the screen,
462 ;; then scroll specially to put it near, but not at, the bottom.
463 (overlay-recenter (point))
464 (recenter -3))))
466 (defun mark-whole-buffer ()
467 "Put point at beginning and mark at end of buffer.
468 You probably should not use this function in Lisp programs;
469 it is usually a mistake for a Lisp function to use any subroutine
470 that uses or sets the mark."
471 (interactive)
472 (push-mark (point))
473 (push-mark (point-max) nil t)
474 (goto-char (point-min)))
477 ;; Counting lines, one way or another.
479 (defun goto-line (arg)
480 "Goto line ARG, counting from line 1 at beginning of buffer."
481 (interactive "NGoto line: ")
482 (setq arg (prefix-numeric-value arg))
483 (save-restriction
484 (widen)
485 (goto-char 1)
486 (if (eq selective-display t)
487 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
488 (forward-line (1- arg)))))
490 (defun count-lines-region (start end)
491 "Print number of lines and characters in the region."
492 (interactive "r")
493 (message "Region has %d lines, %d characters"
494 (count-lines start end) (- end start)))
496 (defun what-line ()
497 "Print the current buffer line number and narrowed line number of point."
498 (interactive)
499 (let ((opoint (point)) start)
500 (save-excursion
501 (save-restriction
502 (goto-char (point-min))
503 (widen)
504 (forward-line 0)
505 (setq start (point))
506 (goto-char opoint)
507 (forward-line 0)
508 (if (/= start (point-min))
509 (message "line %d (narrowed line %d)"
510 (1+ (count-lines (point-min) (point)))
511 (1+ (count-lines start (point))))
512 (message "Line %d" (1+ (count-lines (point-min) (point)))))))))
514 (defun count-lines (start end)
515 "Return number of lines between START and END.
516 This is usually the number of newlines between them,
517 but can be one more if START is not equal to END
518 and the greater of them is not at the start of a line."
519 (save-excursion
520 (save-restriction
521 (narrow-to-region start end)
522 (goto-char (point-min))
523 (if (eq selective-display t)
524 (save-match-data
525 (let ((done 0))
526 (while (re-search-forward "[\n\C-m]" nil t 40)
527 (setq done (+ 40 done)))
528 (while (re-search-forward "[\n\C-m]" nil t 1)
529 (setq done (+ 1 done)))
530 (goto-char (point-max))
531 (if (and (/= start end)
532 (not (bolp)))
533 (1+ done)
534 done)))
535 (- (buffer-size) (forward-line (buffer-size)))))))
537 (defun what-cursor-position (&optional detail)
538 "Print info on cursor position (on screen and within buffer).
539 Also describe the character after point, and give its character code
540 in octal, decimal and hex.
542 For a non-ASCII multibyte character, also give its encoding in the
543 buffer's selected coding system if the coding system encodes the
544 character safely. If the character is encoded into one byte, that
545 code is shown in hex. If the character is encoded into more than one
546 byte, just \"...\" is shown.
548 In addition, with prefix argument, show details about that character
549 in *Help* buffer. See also the command `describe-char'."
550 (interactive "P")
551 (let* ((char (following-char))
552 (beg (point-min))
553 (end (point-max))
554 (pos (point))
555 (total (buffer-size))
556 (percent (if (> total 50000)
557 ;; Avoid overflow from multiplying by 100!
558 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
559 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
560 (hscroll (if (= (window-hscroll) 0)
562 (format " Hscroll=%d" (window-hscroll))))
563 (col (current-column)))
564 (if (= pos end)
565 (if (or (/= beg 1) (/= end (1+ total)))
566 (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
567 pos total percent beg end col hscroll)
568 (message "point=%d of %d (%d%%) column %d %s"
569 pos total percent col hscroll))
570 (let ((coding buffer-file-coding-system)
571 encoded encoding-msg)
572 (if (or (not coding)
573 (eq (coding-system-type coding) t))
574 (setq coding default-buffer-file-coding-system))
575 (if (not (char-valid-p char))
576 (setq encoding-msg
577 (format "(0%o, %d, 0x%x, invalid)" char char char))
578 (setq encoded (and (>= char 128) (encode-coding-char char coding)))
579 (setq encoding-msg
580 (if encoded
581 (format "(0%o, %d, 0x%x, file %s)"
582 char char char
583 (if (> (length encoded) 1)
584 "..."
585 (encoded-string-description encoded coding)))
586 (format "(0%o, %d, 0x%x)" char char char))))
587 (if detail
588 ;; We show the detailed information about CHAR.
589 (describe-char (point)))
590 (if (or (/= beg 1) (/= end (1+ total)))
591 (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
592 (if (< char 256)
593 (single-key-description char)
594 (buffer-substring-no-properties (point) (1+ (point))))
595 encoding-msg pos total percent beg end col hscroll)
596 (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
597 (if (< char 256)
598 (single-key-description char)
599 (buffer-substring-no-properties (point) (1+ (point))))
600 encoding-msg pos total percent col hscroll))))))
602 (defvar read-expression-map
603 (let ((m (make-sparse-keymap)))
604 (define-key m "\M-\t" 'lisp-complete-symbol)
605 (set-keymap-parent m minibuffer-local-map)
607 "Minibuffer keymap used for reading Lisp expressions.")
609 (defvar read-expression-history nil)
611 (defcustom eval-expression-print-level 4
612 "*Value to use for `print-level' when printing value in `eval-expression'.
613 A value of nil means no limit."
614 :group 'lisp
615 :type '(choice (const :tag "No Limit" nil) integer)
616 :version "21.1")
618 (defcustom eval-expression-print-length 12
619 "*Value to use for `print-length' when printing value in `eval-expression'.
620 A value of nil means no limit."
621 :group 'lisp
622 :type '(choice (const :tag "No Limit" nil) integer)
623 :version "21.1")
625 (defcustom eval-expression-debug-on-error t
626 "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'.
627 If nil, don't change the value of `debug-on-error'."
628 :group 'lisp
629 :type 'boolean
630 :version "21.1")
632 ;; We define this, rather than making `eval' interactive,
633 ;; for the sake of completion of names like eval-region, eval-current-buffer.
634 (defun eval-expression (eval-expression-arg
635 &optional eval-expression-insert-value)
636 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
637 Value is also consed on to front of the variable `values'.
638 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
639 insert the result into the current buffer instead of printing it in
640 the echo area."
641 (interactive
642 (list (read-from-minibuffer "Eval: "
643 nil read-expression-map t
644 'read-expression-history)
645 current-prefix-arg))
647 (if (null eval-expression-debug-on-error)
648 (setq values (cons (eval eval-expression-arg) values))
649 (let ((old-value (make-symbol "t")) new-value)
650 ;; Bind debug-on-error to something unique so that we can
651 ;; detect when evaled code changes it.
652 (let ((debug-on-error old-value))
653 (setq values (cons (eval eval-expression-arg) values))
654 (setq new-value debug-on-error))
655 ;; If evaled code has changed the value of debug-on-error,
656 ;; propagate that change to the global binding.
657 (unless (eq old-value new-value)
658 (setq debug-on-error new-value))))
660 (let ((print-length eval-expression-print-length)
661 (print-level eval-expression-print-level))
662 (prin1 (car values)
663 (if eval-expression-insert-value (current-buffer) t))))
665 (defun edit-and-eval-command (prompt command)
666 "Prompting with PROMPT, let user edit COMMAND and eval result.
667 COMMAND is a Lisp expression. Let user edit that expression in
668 the minibuffer, then read and evaluate the result."
669 (let ((command
670 (unwind-protect
671 (read-from-minibuffer prompt
672 (prin1-to-string command)
673 read-expression-map t
674 '(command-history . 1))
675 ;; If command was added to command-history as a string,
676 ;; get rid of that. We want only evaluable expressions there.
677 (if (stringp (car command-history))
678 (setq command-history (cdr command-history))))))
680 ;; If command to be redone does not match front of history,
681 ;; add it to the history.
682 (or (equal command (car command-history))
683 (setq command-history (cons command command-history)))
684 (eval command)))
686 (defun repeat-complex-command (arg)
687 "Edit and re-evaluate last complex command, or ARGth from last.
688 A complex command is one which used the minibuffer.
689 The command is placed in the minibuffer as a Lisp form for editing.
690 The result is executed, repeating the command as changed.
691 If the command has been changed or is not the most recent previous command
692 it is added to the front of the command history.
693 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
694 to get different commands to edit and resubmit."
695 (interactive "p")
696 (let ((elt (nth (1- arg) command-history))
697 newcmd)
698 (if elt
699 (progn
700 (setq newcmd
701 (let ((print-level nil)
702 (minibuffer-history-position arg)
703 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
704 (unwind-protect
705 (read-from-minibuffer
706 "Redo: " (prin1-to-string elt) read-expression-map t
707 (cons 'command-history arg))
709 ;; If command was added to command-history as a
710 ;; string, get rid of that. We want only
711 ;; evaluable expressions there.
712 (if (stringp (car command-history))
713 (setq command-history (cdr command-history))))))
715 ;; If command to be redone does not match front of history,
716 ;; add it to the history.
717 (or (equal newcmd (car command-history))
718 (setq command-history (cons newcmd command-history)))
719 (eval newcmd))
720 (if command-history
721 (error "Argument %d is beyond length of command history" arg)
722 (error "There are no previous complex commands to repeat")))))
724 (defvar minibuffer-history nil
725 "Default minibuffer history list.
726 This is used for all minibuffer input
727 except when an alternate history list is specified.")
728 (defvar minibuffer-history-sexp-flag nil
729 "Non-nil when doing history operations on the variable `command-history'.
730 More generally, indicates that the history list being acted on
731 contains expressions rather than strings.
732 It is only valid if its value equals the current minibuffer depth,
733 to handle recursive uses of the minibuffer.")
734 (setq minibuffer-history-variable 'minibuffer-history)
735 (setq minibuffer-history-position nil)
736 (defvar minibuffer-history-search-history nil)
738 (defvar minibuffer-text-before-history nil
739 "Text that was in this minibuffer before any history commands.
740 This is nil if there have not yet been any history commands
741 in this use of the minibuffer.")
743 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
745 (defun minibuffer-history-initialize ()
746 (setq minibuffer-text-before-history nil))
748 (defun minibuffer-avoid-prompt (new old)
749 "A point-motion hook for the minibuffer, that moves point out of the prompt."
750 (constrain-to-field nil (point-max)))
752 (defcustom minibuffer-history-case-insensitive-variables nil
753 "*Minibuffer history variables for which matching should ignore case.
754 If a history variable is a member of this list, then the
755 \\[previous-matching-history-element] and \\[next-matching-history-element]\
756 commands ignore case when searching it, regardless of `case-fold-search'."
757 :type '(repeat variable)
758 :group 'minibuffer)
760 (defun previous-matching-history-element (regexp n)
761 "Find the previous history element that matches REGEXP.
762 \(Previous history elements refer to earlier actions.)
763 With prefix argument N, search for Nth previous match.
764 If N is negative, find the next or Nth next match.
765 Normally, history elements are matched case-insensitively if
766 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
767 makes the search case-sensitive.
768 See also `minibuffer-history-case-insensitive-variables'."
769 (interactive
770 (let* ((enable-recursive-minibuffers t)
771 (regexp (read-from-minibuffer "Previous element matching (regexp): "
773 minibuffer-local-map
775 'minibuffer-history-search-history)))
776 ;; Use the last regexp specified, by default, if input is empty.
777 (list (if (string= regexp "")
778 (if minibuffer-history-search-history
779 (car minibuffer-history-search-history)
780 (error "No previous history search regexp"))
781 regexp)
782 (prefix-numeric-value current-prefix-arg))))
783 (unless (zerop n)
784 (if (and (zerop minibuffer-history-position)
785 (null minibuffer-text-before-history))
786 (setq minibuffer-text-before-history
787 (minibuffer-contents-no-properties)))
788 (let ((history (symbol-value minibuffer-history-variable))
789 (case-fold-search
790 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
791 ;; On some systems, ignore case for file names.
792 (if (memq minibuffer-history-variable
793 minibuffer-history-case-insensitive-variables)
795 ;; Respect the user's setting for case-fold-search:
796 case-fold-search)
797 nil))
798 prevpos
799 match-string
800 match-offset
801 (pos minibuffer-history-position))
802 (while (/= n 0)
803 (setq prevpos pos)
804 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
805 (when (= pos prevpos)
806 (error (if (= pos 1)
807 "No later matching history item"
808 "No earlier matching history item")))
809 (setq match-string
810 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
811 (let ((print-level nil))
812 (prin1-to-string (nth (1- pos) history)))
813 (nth (1- pos) history)))
814 (setq match-offset
815 (if (< n 0)
816 (and (string-match regexp match-string)
817 (match-end 0))
818 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
819 (match-beginning 1))))
820 (when match-offset
821 (setq n (+ n (if (< n 0) 1 -1)))))
822 (setq minibuffer-history-position pos)
823 (goto-char (point-max))
824 (delete-minibuffer-contents)
825 (insert match-string)
826 (goto-char (+ (minibuffer-prompt-end) match-offset))))
827 (if (memq (car (car command-history)) '(previous-matching-history-element
828 next-matching-history-element))
829 (setq command-history (cdr command-history))))
831 (defun next-matching-history-element (regexp n)
832 "Find the next history element that matches REGEXP.
833 \(The next history element refers to a more recent action.)
834 With prefix argument N, search for Nth next match.
835 If N is negative, find the previous or Nth previous match.
836 Normally, history elements are matched case-insensitively if
837 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
838 makes the search case-sensitive."
839 (interactive
840 (let* ((enable-recursive-minibuffers t)
841 (regexp (read-from-minibuffer "Next element matching (regexp): "
843 minibuffer-local-map
845 'minibuffer-history-search-history)))
846 ;; Use the last regexp specified, by default, if input is empty.
847 (list (if (string= regexp "")
848 (setcar minibuffer-history-search-history
849 (nth 1 minibuffer-history-search-history))
850 regexp)
851 (prefix-numeric-value current-prefix-arg))))
852 (previous-matching-history-element regexp (- n)))
854 (defvar minibuffer-temporary-goal-position nil)
856 (defun next-history-element (n)
857 "Insert the next element of the minibuffer history into the minibuffer."
858 (interactive "p")
859 (or (zerop n)
860 (let ((narg (- minibuffer-history-position n))
861 (minimum (if minibuffer-default -1 0))
862 elt minibuffer-returned-to-present)
863 (if (and (zerop minibuffer-history-position)
864 (null minibuffer-text-before-history))
865 (setq minibuffer-text-before-history
866 (minibuffer-contents-no-properties)))
867 (if (< narg minimum)
868 (if minibuffer-default
869 (error "End of history; no next item")
870 (error "End of history; no default available")))
871 (if (> narg (length (symbol-value minibuffer-history-variable)))
872 (error "Beginning of history; no preceding item"))
873 (unless (memq last-command '(next-history-element
874 previous-history-element))
875 (let ((prompt-end (minibuffer-prompt-end)))
876 (set (make-local-variable 'minibuffer-temporary-goal-position)
877 (cond ((<= (point) prompt-end) prompt-end)
878 ((eobp) nil)
879 (t (point))))))
880 (goto-char (point-max))
881 (delete-minibuffer-contents)
882 (setq minibuffer-history-position narg)
883 (cond ((= narg -1)
884 (setq elt minibuffer-default))
885 ((= narg 0)
886 (setq elt (or minibuffer-text-before-history ""))
887 (setq minibuffer-returned-to-present t)
888 (setq minibuffer-text-before-history nil))
889 (t (setq elt (nth (1- minibuffer-history-position)
890 (symbol-value minibuffer-history-variable)))))
891 (insert
892 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
893 (not minibuffer-returned-to-present))
894 (let ((print-level nil))
895 (prin1-to-string elt))
896 elt))
897 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
899 (defun previous-history-element (n)
900 "Inserts the previous element of the minibuffer history into the minibuffer."
901 (interactive "p")
902 (next-history-element (- n)))
904 (defun next-complete-history-element (n)
905 "Get next history element which completes the minibuffer before the point.
906 The contents of the minibuffer after the point are deleted, and replaced
907 by the new completion."
908 (interactive "p")
909 (let ((point-at-start (point)))
910 (next-matching-history-element
911 (concat
912 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
914 ;; next-matching-history-element always puts us at (point-min).
915 ;; Move to the position we were at before changing the buffer contents.
916 ;; This is still sensical, because the text before point has not changed.
917 (goto-char point-at-start)))
919 (defun previous-complete-history-element (n)
921 Get previous history element which completes the minibuffer before the point.
922 The contents of the minibuffer after the point are deleted, and replaced
923 by the new completion."
924 (interactive "p")
925 (next-complete-history-element (- n)))
927 ;; For compatibility with the old subr of the same name.
928 (defun minibuffer-prompt-width ()
929 "Return the display width of the minibuffer prompt.
930 Return 0 if current buffer is not a mini-buffer."
931 ;; Return the width of everything before the field at the end of
932 ;; the buffer; this should be 0 for normal buffers.
933 (1- (minibuffer-prompt-end)))
935 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
936 (defalias 'advertised-undo 'undo)
938 (defun undo (&optional arg)
939 "Undo some previous changes.
940 Repeat this command to undo more changes.
941 A numeric argument serves as a repeat count.
943 In Transient Mark mode when the mark is active, only undo changes within
944 the current region. Similarly, when not in Transient Mark mode, just C-u
945 as an argument limits undo to changes within the current region."
946 (interactive "*P")
947 ;; Make last-command indicate for the next command that this was an undo.
948 ;; That way, another undo will undo more.
949 ;; If we get to the end of the undo history and get an error,
950 ;; another undo command will find the undo history empty
951 ;; and will get another error. To begin undoing the undos,
952 ;; you must type some other command.
953 (setq this-command 'undo)
954 (let ((modified (buffer-modified-p))
955 (recent-save (recent-auto-save-p)))
956 (or (eq (selected-window) (minibuffer-window))
957 (message (if (and transient-mark-mode mark-active)
958 "Undo in region!"
959 "Undo!")))
960 (unless (eq last-command 'undo)
961 (if (if transient-mark-mode mark-active (and arg (not (numberp arg))))
962 (undo-start (region-beginning) (region-end))
963 (undo-start))
964 ;; get rid of initial undo boundary
965 (undo-more 1))
966 (undo-more
967 (if (or transient-mark-mode (numberp arg))
968 (prefix-numeric-value arg)
970 ;; Don't specify a position in the undo record for the undo command.
971 ;; Instead, undoing this should move point to where the change is.
972 (let ((tail buffer-undo-list)
973 (prev nil))
974 (while (car tail)
975 (when (integerp (car tail))
976 (let ((pos (car tail)))
977 (if (null prev)
978 (setq buffer-undo-list (cdr tail))
979 (setcdr prev (cdr tail)))
980 (setq tail (cdr tail))
981 (while (car tail)
982 (if (eq pos (car tail))
983 (if prev
984 (setcdr prev (cdr tail))
985 (setq buffer-undo-list (cdr tail)))
986 (setq prev tail))
987 (setq tail (cdr tail)))
988 (setq tail nil)))
989 (setq prev tail tail (cdr tail))))
991 (and modified (not (buffer-modified-p))
992 (delete-auto-save-file-if-necessary recent-save))))
994 (defvar pending-undo-list nil
995 "Within a run of consecutive undo commands, list remaining to be undone.")
997 (defvar undo-in-progress nil
998 "Non-nil while performing an undo.
999 Some change-hooks test this variable to do something different.")
1001 (defun undo-more (count)
1002 "Undo back N undo-boundaries beyond what was already undone recently.
1003 Call `undo-start' to get ready to undo recent changes,
1004 then call `undo-more' one or more times to undo them."
1005 (or pending-undo-list
1006 (error (format "No further undo information%s"
1007 (if (and transient-mark-mode mark-active)
1008 " for region" ""))))
1009 (let ((undo-in-progress t))
1010 (setq pending-undo-list (primitive-undo count pending-undo-list))))
1012 ;; Deep copy of a list
1013 (defun undo-copy-list (list)
1014 "Make a copy of undo list LIST."
1015 (mapcar 'undo-copy-list-1 list))
1017 (defun undo-copy-list-1 (elt)
1018 (if (consp elt)
1019 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1020 elt))
1022 (defun undo-start (&optional beg end)
1023 "Set `pending-undo-list' to the front of the undo list.
1024 The next call to `undo-more' will undo the most recently made change.
1025 If BEG and END are specified, then only undo elements
1026 that apply to text between BEG and END are used; other undo elements
1027 are ignored. If BEG and END are nil, all undo elements are used."
1028 (if (eq buffer-undo-list t)
1029 (error "No undo information in this buffer"))
1030 (setq pending-undo-list
1031 (if (and beg end (not (= beg end)))
1032 (undo-make-selective-list (min beg end) (max beg end))
1033 buffer-undo-list)))
1035 (defvar undo-adjusted-markers)
1037 (defun undo-make-selective-list (start end)
1038 "Return a list of undo elements for the region START to END.
1039 The elements come from `buffer-undo-list', but we keep only
1040 the elements inside this region, and discard those outside this region.
1041 If we find an element that crosses an edge of this region,
1042 we stop and ignore all further elements."
1043 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1044 (undo-list (list nil))
1045 undo-adjusted-markers
1046 some-rejected
1047 undo-elt undo-elt temp-undo-list delta)
1048 (while undo-list-copy
1049 (setq undo-elt (car undo-list-copy))
1050 (let ((keep-this
1051 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1052 ;; This is a "was unmodified" element.
1053 ;; Keep it if we have kept everything thus far.
1054 (not some-rejected))
1056 (undo-elt-in-region undo-elt start end)))))
1057 (if keep-this
1058 (progn
1059 (setq end (+ end (cdr (undo-delta undo-elt))))
1060 ;; Don't put two nils together in the list
1061 (if (not (and (eq (car undo-list) nil)
1062 (eq undo-elt nil)))
1063 (setq undo-list (cons undo-elt undo-list))))
1064 (if (undo-elt-crosses-region undo-elt start end)
1065 (setq undo-list-copy nil)
1066 (setq some-rejected t)
1067 (setq temp-undo-list (cdr undo-list-copy))
1068 (setq delta (undo-delta undo-elt))
1070 (when (/= (cdr delta) 0)
1071 (let ((position (car delta))
1072 (offset (cdr delta)))
1074 ;; Loop down the earlier events adjusting their buffer
1075 ;; positions to reflect the fact that a change to the buffer
1076 ;; isn't being undone. We only need to process those element
1077 ;; types which undo-elt-in-region will return as being in
1078 ;; the region since only those types can ever get into the
1079 ;; output
1081 (while temp-undo-list
1082 (setq undo-elt (car temp-undo-list))
1083 (cond ((integerp undo-elt)
1084 (if (>= undo-elt position)
1085 (setcar temp-undo-list (- undo-elt offset))))
1086 ((atom undo-elt) nil)
1087 ((stringp (car undo-elt))
1088 ;; (TEXT . POSITION)
1089 (let ((text-pos (abs (cdr undo-elt)))
1090 (point-at-end (< (cdr undo-elt) 0 )))
1091 (if (>= text-pos position)
1092 (setcdr undo-elt (* (if point-at-end -1 1)
1093 (- text-pos offset))))))
1094 ((integerp (car undo-elt))
1095 ;; (BEGIN . END)
1096 (when (>= (car undo-elt) position)
1097 (setcar undo-elt (- (car undo-elt) offset))
1098 (setcdr undo-elt (- (cdr undo-elt) offset))))
1099 ((null (car undo-elt))
1100 ;; (nil PROPERTY VALUE BEG . END)
1101 (let ((tail (nthcdr 3 undo-elt)))
1102 (when (>= (car tail) position)
1103 (setcar tail (- (car tail) offset))
1104 (setcdr tail (- (cdr tail) offset))))))
1105 (setq temp-undo-list (cdr temp-undo-list))))))))
1106 (setq undo-list-copy (cdr undo-list-copy)))
1107 (nreverse undo-list)))
1109 (defun undo-elt-in-region (undo-elt start end)
1110 "Determine whether UNDO-ELT falls inside the region START ... END.
1111 If it crosses the edge, we return nil."
1112 (cond ((integerp undo-elt)
1113 (and (>= undo-elt start)
1114 (<= undo-elt end)))
1115 ((eq undo-elt nil)
1117 ((atom undo-elt)
1118 nil)
1119 ((stringp (car undo-elt))
1120 ;; (TEXT . POSITION)
1121 (and (>= (abs (cdr undo-elt)) start)
1122 (< (abs (cdr undo-elt)) end)))
1123 ((and (consp undo-elt) (markerp (car undo-elt)))
1124 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1125 ;; See if MARKER is inside the region.
1126 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1127 (unless alist-elt
1128 (setq alist-elt (cons (car undo-elt)
1129 (marker-position (car undo-elt))))
1130 (setq undo-adjusted-markers
1131 (cons alist-elt undo-adjusted-markers)))
1132 (and (cdr alist-elt)
1133 (>= (cdr alist-elt) start)
1134 (<= (cdr alist-elt) end))))
1135 ((null (car undo-elt))
1136 ;; (nil PROPERTY VALUE BEG . END)
1137 (let ((tail (nthcdr 3 undo-elt)))
1138 (and (>= (car tail) start)
1139 (<= (cdr tail) end))))
1140 ((integerp (car undo-elt))
1141 ;; (BEGIN . END)
1142 (and (>= (car undo-elt) start)
1143 (<= (cdr undo-elt) end)))))
1145 (defun undo-elt-crosses-region (undo-elt start end)
1146 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1147 This assumes we have already decided that UNDO-ELT
1148 is not *inside* the region START...END."
1149 (cond ((atom undo-elt) nil)
1150 ((null (car undo-elt))
1151 ;; (nil PROPERTY VALUE BEG . END)
1152 (let ((tail (nthcdr 3 undo-elt)))
1153 (not (or (< (car tail) end)
1154 (> (cdr tail) start)))))
1155 ((integerp (car undo-elt))
1156 ;; (BEGIN . END)
1157 (not (or (< (car undo-elt) end)
1158 (> (cdr undo-elt) start))))))
1160 ;; Return the first affected buffer position and the delta for an undo element
1161 ;; delta is defined as the change in subsequent buffer positions if we *did*
1162 ;; the undo.
1163 (defun undo-delta (undo-elt)
1164 (if (consp undo-elt)
1165 (cond ((stringp (car undo-elt))
1166 ;; (TEXT . POSITION)
1167 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1168 ((integerp (car undo-elt))
1169 ;; (BEGIN . END)
1170 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1172 '(0 . 0)))
1173 '(0 . 0)))
1175 (defvar shell-command-history nil
1176 "History list for some commands that read shell commands.")
1178 (defvar shell-command-switch "-c"
1179 "Switch used to have the shell execute its command line argument.")
1181 (defvar shell-command-default-error-buffer nil
1182 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1183 This buffer is used when `shell-command' or `shell-command-on-region'
1184 is run interactively. A value of nil means that output to stderr and
1185 stdout will be intermixed in the output stream.")
1187 (defun shell-command (command &optional output-buffer error-buffer)
1188 "Execute string COMMAND in inferior shell; display output, if any.
1189 With prefix argument, insert the COMMAND's output at point.
1191 If COMMAND ends in ampersand, execute it asynchronously.
1192 The output appears in the buffer `*Async Shell Command*'.
1193 That buffer is in shell mode.
1195 Otherwise, COMMAND is executed synchronously. The output appears in
1196 the buffer `*Shell Command Output*'. If the output is short enough to
1197 display in the echo area (which is determined by the variables
1198 `resize-mini-windows' and `max-mini-window-height'), it is shown
1199 there, but it is nonetheless available in buffer `*Shell Command
1200 Output*' even though that buffer is not automatically displayed.
1202 To specify a coding system for converting non-ASCII characters
1203 in the shell command output, use \\[universal-coding-system-argument]
1204 before this command.
1206 Noninteractive callers can specify coding systems by binding
1207 `coding-system-for-read' and `coding-system-for-write'.
1209 The optional second argument OUTPUT-BUFFER, if non-nil,
1210 says to put the output in some other buffer.
1211 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1212 If OUTPUT-BUFFER is not a buffer and not nil,
1213 insert output in current buffer. (This cannot be done asynchronously.)
1214 In either case, the output is inserted after point (leaving mark after it).
1216 If the command terminates without error, but generates output,
1217 and you did not specify \"insert it in the current buffer\",
1218 the output can be displayed in the echo area or in its buffer.
1219 If the output is short enough to display in the echo area
1220 \(determined by the variable `max-mini-window-height' if
1221 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1222 the buffer containing the output is displayed.
1224 If there is output and an error, and you did not specify \"insert it
1225 in the current buffer\", a message about the error goes at the end
1226 of the output.
1228 If there is no output, or if output is inserted in the current buffer,
1229 then `*Shell Command Output*' is deleted.
1231 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1232 or buffer name to which to direct the command's standard error output.
1233 If it is nil, error output is mingled with regular output.
1234 In an interactive call, the variable `shell-command-default-error-buffer'
1235 specifies the value of ERROR-BUFFER."
1237 (interactive (list (read-from-minibuffer "Shell command: "
1238 nil nil nil 'shell-command-history)
1239 current-prefix-arg
1240 shell-command-default-error-buffer))
1241 ;; Look for a handler in case default-directory is a remote file name.
1242 (let ((handler
1243 (find-file-name-handler (directory-file-name default-directory)
1244 'shell-command)))
1245 (if handler
1246 (funcall handler 'shell-command command output-buffer error-buffer)
1247 (if (and output-buffer
1248 (not (or (bufferp output-buffer) (stringp output-buffer))))
1249 ;; Output goes in current buffer.
1250 (let ((error-file
1251 (if error-buffer
1252 (make-temp-file
1253 (expand-file-name "scor"
1254 (or small-temporary-file-directory
1255 temporary-file-directory)))
1256 nil)))
1257 (barf-if-buffer-read-only)
1258 (push-mark nil t)
1259 ;; We do not use -f for csh; we will not support broken use of
1260 ;; .cshrcs. Even the BSD csh manual says to use
1261 ;; "if ($?prompt) exit" before things which are not useful
1262 ;; non-interactively. Besides, if someone wants their other
1263 ;; aliases for shell commands then they can still have them.
1264 (call-process shell-file-name nil
1265 (if error-file
1266 (list t error-file)
1268 nil shell-command-switch command)
1269 (when (and error-file (file-exists-p error-file))
1270 (if (< 0 (nth 7 (file-attributes error-file)))
1271 (with-current-buffer (get-buffer-create error-buffer)
1272 (let ((pos-from-end (- (point-max) (point))))
1273 (or (bobp)
1274 (insert "\f\n"))
1275 ;; Do no formatting while reading error file,
1276 ;; because that can run a shell command, and we
1277 ;; don't want that to cause an infinite recursion.
1278 (format-insert-file error-file nil)
1279 ;; Put point after the inserted errors.
1280 (goto-char (- (point-max) pos-from-end)))
1281 (display-buffer (current-buffer))))
1282 (delete-file error-file))
1283 ;; This is like exchange-point-and-mark, but doesn't
1284 ;; activate the mark. It is cleaner to avoid activation,
1285 ;; even though the command loop would deactivate the mark
1286 ;; because we inserted text.
1287 (goto-char (prog1 (mark t)
1288 (set-marker (mark-marker) (point)
1289 (current-buffer)))))
1290 ;; Output goes in a separate buffer.
1291 ;; Preserve the match data in case called from a program.
1292 (save-match-data
1293 (if (string-match "[ \t]*&[ \t]*\\'" command)
1294 ;; Command ending with ampersand means asynchronous.
1295 (let ((buffer (get-buffer-create
1296 (or output-buffer "*Async Shell Command*")))
1297 (directory default-directory)
1298 proc)
1299 ;; Remove the ampersand.
1300 (setq command (substring command 0 (match-beginning 0)))
1301 ;; If will kill a process, query first.
1302 (setq proc (get-buffer-process buffer))
1303 (if proc
1304 (if (yes-or-no-p "A command is running. Kill it? ")
1305 (kill-process proc)
1306 (error "Shell command in progress")))
1307 (save-excursion
1308 (set-buffer buffer)
1309 (setq buffer-read-only nil)
1310 (erase-buffer)
1311 (display-buffer buffer)
1312 (setq default-directory directory)
1313 (setq proc (start-process "Shell" buffer shell-file-name
1314 shell-command-switch command))
1315 (setq mode-line-process '(":%s"))
1316 (require 'shell) (shell-mode)
1317 (set-process-sentinel proc 'shell-command-sentinel)
1319 (shell-command-on-region (point) (point) command
1320 output-buffer nil error-buffer)))))))
1322 (defun display-message-or-buffer (message
1323 &optional buffer-name not-this-window frame)
1324 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1325 MESSAGE may be either a string or a buffer.
1327 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1328 the maximum height of the echo area, as defined by `max-mini-window-height'
1329 if `resize-mini-windows' is non-nil.
1331 Returns either the string shown in the echo area, or when a pop-up
1332 buffer is used, the window used to display it.
1334 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1335 name of the buffer used to display it in the case where a pop-up buffer
1336 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1337 string and it is displayed in the echo area, it is not specified whether
1338 the contents are inserted into the buffer anyway.
1340 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1341 and only used if a buffer is displayed."
1342 (cond ((and (stringp message) (not (string-match "\n" message)))
1343 ;; Trivial case where we can use the echo area
1344 (message "%s" message))
1345 ((and (stringp message)
1346 (= (string-match "\n" message) (1- (length message))))
1347 ;; Trivial case where we can just remove single trailing newline
1348 (message "%s" (substring message 0 (1- (length message)))))
1350 ;; General case
1351 (with-current-buffer
1352 (if (bufferp message)
1353 message
1354 (get-buffer-create (or buffer-name "*Message*")))
1356 (unless (bufferp message)
1357 (erase-buffer)
1358 (insert message))
1360 (let ((lines
1361 (if (= (buffer-size) 0)
1363 (count-lines (point-min) (point-max)))))
1364 (cond ((= lines 0))
1365 ((and (or (<= lines 1)
1366 (<= lines
1367 (if resize-mini-windows
1368 (cond ((floatp max-mini-window-height)
1369 (* (frame-height)
1370 max-mini-window-height))
1371 ((integerp max-mini-window-height)
1372 max-mini-window-height)
1375 1)))
1376 ;; Don't use the echo area if the output buffer is
1377 ;; already dispayed in the selected frame.
1378 (not (get-buffer-window (current-buffer))))
1379 ;; Echo area
1380 (goto-char (point-max))
1381 (when (bolp)
1382 (backward-char 1))
1383 (message "%s" (buffer-substring (point-min) (point))))
1385 ;; Buffer
1386 (goto-char (point-min))
1387 (display-buffer (current-buffer)
1388 not-this-window frame))))))))
1391 ;; We have a sentinel to prevent insertion of a termination message
1392 ;; in the buffer itself.
1393 (defun shell-command-sentinel (process signal)
1394 (if (memq (process-status process) '(exit signal))
1395 (message "%s: %s."
1396 (car (cdr (cdr (process-command process))))
1397 (substring signal 0 -1))))
1399 (defun shell-command-on-region (start end command
1400 &optional output-buffer replace
1401 error-buffer)
1402 "Execute string COMMAND in inferior shell with region as input.
1403 Normally display output (if any) in temp buffer `*Shell Command Output*';
1404 Prefix arg means replace the region with it. Return the exit code of
1405 COMMAND.
1407 To specify a coding system for converting non-ASCII characters
1408 in the input and output to the shell command, use \\[universal-coding-system-argument]
1409 before this command. By default, the input (from the current buffer)
1410 is encoded in the same coding system that will be used to save the file,
1411 `buffer-file-coding-system'. If the output is going to replace the region,
1412 then it is decoded from that same coding system.
1414 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1415 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1416 systems by binding `coding-system-for-read' and
1417 `coding-system-for-write'.
1419 If the command generates output, the output may be displayed
1420 in the echo area or in a buffer.
1421 If the output is short enough to display in the echo area
1422 \(determined by the variable `max-mini-window-height' if
1423 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1424 it is displayed in the buffer `*Shell Command Output*'. The output
1425 is available in that buffer in both cases.
1427 If there is output and an error, a message about the error
1428 appears at the end of the output.
1430 If there is no output, or if output is inserted in the current buffer,
1431 then `*Shell Command Output*' is deleted.
1433 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1434 that 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 the current buffer.
1438 In either case, the output is inserted after point (leaving mark after it).
1440 If REPLACE, the optional fifth argument, is non-nil, that means insert
1441 the output in place of text from START to END, putting point and mark
1442 around it.
1444 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1445 or buffer name to which to direct the command's standard error output.
1446 If it is nil, error output is mingled with regular output.
1447 In an interactive call, the variable `shell-command-default-error-buffer'
1448 specifies the value of ERROR-BUFFER."
1449 (interactive (let (string)
1450 (unless (mark)
1451 (error "The mark is not set now, so there is no region"))
1452 ;; Do this before calling region-beginning
1453 ;; and region-end, in case subprocess output
1454 ;; relocates them while we are in the minibuffer.
1455 (setq string (read-from-minibuffer "Shell command on region: "
1456 nil nil nil
1457 'shell-command-history))
1458 ;; call-interactively recognizes region-beginning and
1459 ;; region-end specially, leaving them in the history.
1460 (list (region-beginning) (region-end)
1461 string
1462 current-prefix-arg
1463 current-prefix-arg
1464 shell-command-default-error-buffer)))
1465 (let ((error-file
1466 (if error-buffer
1467 (make-temp-file
1468 (expand-file-name "scor"
1469 (or small-temporary-file-directory
1470 temporary-file-directory)))
1471 nil))
1472 exit-status)
1473 (if (or replace
1474 (and output-buffer
1475 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1476 ;; Replace specified region with output from command.
1477 (let ((swap (and replace (< start end))))
1478 ;; Don't muck with mark unless REPLACE says we should.
1479 (goto-char start)
1480 (and replace (push-mark (point) 'nomsg))
1481 (setq exit-status
1482 (call-process-region start end shell-file-name t
1483 (if error-file
1484 (list t error-file)
1486 nil shell-command-switch command))
1487 ;; It is rude to delete a buffer which the command is not using.
1488 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1489 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1490 ;; (kill-buffer shell-buffer)))
1491 ;; Don't muck with mark unless REPLACE says we should.
1492 (and replace swap (exchange-point-and-mark)))
1493 ;; No prefix argument: put the output in a temp buffer,
1494 ;; replacing its entire contents.
1495 (let ((buffer (get-buffer-create
1496 (or output-buffer "*Shell Command Output*"))))
1497 (unwind-protect
1498 (if (eq buffer (current-buffer))
1499 ;; If the input is the same buffer as the output,
1500 ;; delete everything but the specified region,
1501 ;; then replace that region with the output.
1502 (progn (setq buffer-read-only nil)
1503 (delete-region (max start end) (point-max))
1504 (delete-region (point-min) (min start end))
1505 (setq exit-status
1506 (call-process-region (point-min) (point-max)
1507 shell-file-name t
1508 (if error-file
1509 (list t error-file)
1511 nil shell-command-switch
1512 command)))
1513 ;; Clear the output buffer, then run the command with
1514 ;; output there.
1515 (let ((directory default-directory))
1516 (save-excursion
1517 (set-buffer buffer)
1518 (setq buffer-read-only nil)
1519 (if (not output-buffer)
1520 (setq default-directory directory))
1521 (erase-buffer)))
1522 (setq exit-status
1523 (call-process-region start end shell-file-name nil
1524 (if error-file
1525 (list buffer error-file)
1526 buffer)
1527 nil shell-command-switch command)))
1528 ;; Report the output.
1529 (with-current-buffer buffer
1530 (setq mode-line-process
1531 (cond ((null exit-status)
1532 " - Error")
1533 ((stringp exit-status)
1534 (format " - Signal [%s]" exit-status))
1535 ((not (equal 0 exit-status))
1536 (format " - Exit [%d]" exit-status)))))
1537 (if (with-current-buffer buffer (> (point-max) (point-min)))
1538 ;; There's some output, display it
1539 (display-message-or-buffer buffer)
1540 ;; No output; error?
1541 (let ((output
1542 (if (and error-file
1543 (< 0 (nth 7 (file-attributes error-file))))
1544 "some error output"
1545 "no output")))
1546 (cond ((null exit-status)
1547 (message "(Shell command failed with error)"))
1548 ((equal 0 exit-status)
1549 (message "(Shell command succeeded with %s)"
1550 output))
1551 ((stringp exit-status)
1552 (message "(Shell command killed by signal %s)"
1553 exit-status))
1555 (message "(Shell command failed with code %d and %s)"
1556 exit-status output))))
1557 ;; Don't kill: there might be useful info in the undo-log.
1558 ;; (kill-buffer buffer)
1559 ))))
1561 (when (and error-file (file-exists-p error-file))
1562 (if (< 0 (nth 7 (file-attributes error-file)))
1563 (with-current-buffer (get-buffer-create error-buffer)
1564 (let ((pos-from-end (- (point-max) (point))))
1565 (or (bobp)
1566 (insert "\f\n"))
1567 ;; Do no formatting while reading error file,
1568 ;; because that can run a shell command, and we
1569 ;; don't want that to cause an infinite recursion.
1570 (format-insert-file error-file nil)
1571 ;; Put point after the inserted errors.
1572 (goto-char (- (point-max) pos-from-end)))
1573 (display-buffer (current-buffer))))
1574 (delete-file error-file))
1575 exit-status))
1577 (defun shell-command-to-string (command)
1578 "Execute shell command COMMAND and return its output as a string."
1579 (with-output-to-string
1580 (with-current-buffer
1581 standard-output
1582 (call-process shell-file-name nil t nil shell-command-switch command))))
1584 (defvar universal-argument-map
1585 (let ((map (make-sparse-keymap)))
1586 (define-key map [t] 'universal-argument-other-key)
1587 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1588 (define-key map [switch-frame] nil)
1589 (define-key map [?\C-u] 'universal-argument-more)
1590 (define-key map [?-] 'universal-argument-minus)
1591 (define-key map [?0] 'digit-argument)
1592 (define-key map [?1] 'digit-argument)
1593 (define-key map [?2] 'digit-argument)
1594 (define-key map [?3] 'digit-argument)
1595 (define-key map [?4] 'digit-argument)
1596 (define-key map [?5] 'digit-argument)
1597 (define-key map [?6] 'digit-argument)
1598 (define-key map [?7] 'digit-argument)
1599 (define-key map [?8] 'digit-argument)
1600 (define-key map [?9] 'digit-argument)
1601 (define-key map [kp-0] 'digit-argument)
1602 (define-key map [kp-1] 'digit-argument)
1603 (define-key map [kp-2] 'digit-argument)
1604 (define-key map [kp-3] 'digit-argument)
1605 (define-key map [kp-4] 'digit-argument)
1606 (define-key map [kp-5] 'digit-argument)
1607 (define-key map [kp-6] 'digit-argument)
1608 (define-key map [kp-7] 'digit-argument)
1609 (define-key map [kp-8] 'digit-argument)
1610 (define-key map [kp-9] 'digit-argument)
1611 (define-key map [kp-subtract] 'universal-argument-minus)
1612 map)
1613 "Keymap used while processing \\[universal-argument].")
1615 (defvar universal-argument-num-events nil
1616 "Number of argument-specifying events read by `universal-argument'.
1617 `universal-argument-other-key' uses this to discard those events
1618 from (this-command-keys), and reread only the final command.")
1620 (defun universal-argument ()
1621 "Begin a numeric argument for the following command.
1622 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1623 \\[universal-argument] following the digits or minus sign ends the argument.
1624 \\[universal-argument] without digits or minus sign provides 4 as argument.
1625 Repeating \\[universal-argument] without digits or minus sign
1626 multiplies the argument by 4 each time.
1627 For some commands, just \\[universal-argument] by itself serves as a flag
1628 which is different in effect from any particular numeric argument.
1629 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1630 (interactive)
1631 (setq prefix-arg (list 4))
1632 (setq universal-argument-num-events (length (this-command-keys)))
1633 (setq overriding-terminal-local-map universal-argument-map))
1635 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1636 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1637 (defun universal-argument-more (arg)
1638 (interactive "P")
1639 (if (consp arg)
1640 (setq prefix-arg (list (* 4 (car arg))))
1641 (if (eq arg '-)
1642 (setq prefix-arg (list -4))
1643 (setq prefix-arg arg)
1644 (setq overriding-terminal-local-map nil)))
1645 (setq universal-argument-num-events (length (this-command-keys))))
1647 (defun negative-argument (arg)
1648 "Begin a negative numeric argument for the next command.
1649 \\[universal-argument] following digits or minus sign ends the argument."
1650 (interactive "P")
1651 (cond ((integerp arg)
1652 (setq prefix-arg (- arg)))
1653 ((eq arg '-)
1654 (setq prefix-arg nil))
1656 (setq prefix-arg '-)))
1657 (setq universal-argument-num-events (length (this-command-keys)))
1658 (setq overriding-terminal-local-map universal-argument-map))
1660 (defun digit-argument (arg)
1661 "Part of the numeric argument for the next command.
1662 \\[universal-argument] following digits or minus sign ends the argument."
1663 (interactive "P")
1664 (let* ((char (if (integerp last-command-char)
1665 last-command-char
1666 (get last-command-char 'ascii-character)))
1667 (digit (- (logand char ?\177) ?0)))
1668 (cond ((integerp arg)
1669 (setq prefix-arg (+ (* arg 10)
1670 (if (< arg 0) (- digit) digit))))
1671 ((eq arg '-)
1672 ;; Treat -0 as just -, so that -01 will work.
1673 (setq prefix-arg (if (zerop digit) '- (- digit))))
1675 (setq prefix-arg digit))))
1676 (setq universal-argument-num-events (length (this-command-keys)))
1677 (setq overriding-terminal-local-map universal-argument-map))
1679 ;; For backward compatibility, minus with no modifiers is an ordinary
1680 ;; command if digits have already been entered.
1681 (defun universal-argument-minus (arg)
1682 (interactive "P")
1683 (if (integerp arg)
1684 (universal-argument-other-key arg)
1685 (negative-argument arg)))
1687 ;; Anything else terminates the argument and is left in the queue to be
1688 ;; executed as a command.
1689 (defun universal-argument-other-key (arg)
1690 (interactive "P")
1691 (setq prefix-arg arg)
1692 (let* ((key (this-command-keys))
1693 (keylist (listify-key-sequence key)))
1694 (setq unread-command-events
1695 (append (nthcdr universal-argument-num-events keylist)
1696 unread-command-events)))
1697 (reset-this-command-lengths)
1698 (setq overriding-terminal-local-map nil))
1700 ;;;; Window system cut and paste hooks.
1702 (defvar interprogram-cut-function nil
1703 "Function to call to make a killed region available to other programs.
1705 Most window systems provide some sort of facility for cutting and
1706 pasting text between the windows of different programs.
1707 This variable holds a function that Emacs calls whenever text
1708 is put in the kill ring, to make the new kill available to other
1709 programs.
1711 The function takes one or two arguments.
1712 The first argument, TEXT, is a string containing
1713 the text which should be made available.
1714 The second, PUSH, if non-nil means this is a \"new\" kill;
1715 nil means appending to an \"old\" kill.")
1717 (defvar interprogram-paste-function nil
1718 "Function to call to get text cut from other programs.
1720 Most window systems provide some sort of facility for cutting and
1721 pasting text between the windows of different programs.
1722 This variable holds a function that Emacs calls to obtain
1723 text that other programs have provided for pasting.
1725 The function should be called with no arguments. If the function
1726 returns nil, then no other program has provided such text, and the top
1727 of the Emacs kill ring should be used. If the function returns a
1728 string, that string should be put in the kill ring as the latest kill.
1730 Note that the function should return a string only if a program other
1731 than Emacs has provided a string for pasting; if Emacs provided the
1732 most recent string, the function should return nil. If it is
1733 difficult to tell whether Emacs or some other program provided the
1734 current string, it is probably good enough to return nil if the string
1735 is equal (according to `string=') to the last text Emacs provided.")
1739 ;;;; The kill ring data structure.
1741 (defvar kill-ring nil
1742 "List of killed text sequences.
1743 Since the kill ring is supposed to interact nicely with cut-and-paste
1744 facilities offered by window systems, use of this variable should
1745 interact nicely with `interprogram-cut-function' and
1746 `interprogram-paste-function'. The functions `kill-new',
1747 `kill-append', and `current-kill' are supposed to implement this
1748 interaction; you may want to use them instead of manipulating the kill
1749 ring directly.")
1751 (defcustom kill-ring-max 60
1752 "*Maximum length of kill ring before oldest elements are thrown away."
1753 :type 'integer
1754 :group 'killing)
1756 (defvar kill-ring-yank-pointer nil
1757 "The tail of the kill ring whose car is the last thing yanked.")
1759 (defun kill-new (string &optional replace yank-handler)
1760 "Make STRING the latest kill in the kill ring.
1761 Set `kill-ring-yank-pointer' to point to it.
1762 If `interprogram-cut-function' is non-nil, apply it to STRING.
1763 Optional second argument REPLACE non-nil means that STRING will replace
1764 the front of the kill ring, rather than being added to the list.
1766 Optional third arguments YANK-HANDLER controls how the STRING is later
1767 inserted into a buffer; see `insert-for-yank' for details.
1768 When a yank handler is specified, STRING must be non-empty (the yank
1769 handler is stored as a `yank-handler'text property on STRING).
1771 When the yank handler has a non-nil PARAM element, the original STRING
1772 argument is not used by `insert-for-yank'. However, since Lisp code
1773 may access and use elements from the kill-ring directly, the STRING
1774 argument should still be a \"useful\" string for such uses."
1775 (if (> (length string) 0)
1776 (if yank-handler
1777 (put-text-property 0 1 'yank-handler yank-handler string)
1778 (remove-list-of-text-properties 0 1 '(yank-handler) string))
1779 (if yank-handler
1780 (signal 'args-out-of-range
1781 (list string "yank-handler specified for empty string"))))
1782 (if (fboundp 'menu-bar-update-yank-menu)
1783 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1784 (if (and replace kill-ring)
1785 (setcar kill-ring string)
1786 (setq kill-ring (cons string kill-ring))
1787 (if (> (length kill-ring) kill-ring-max)
1788 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1789 (setq kill-ring-yank-pointer kill-ring)
1790 (if interprogram-cut-function
1791 (funcall interprogram-cut-function string (not replace))))
1793 (defun kill-append (string before-p &optional yank-handler)
1794 "Append STRING to the end of the latest kill in the kill ring.
1795 If BEFORE-P is non-nil, prepend STRING to the kill.
1796 Optional third argument YANK-HANDLER specifies the yank-handler text
1797 property to be set on the combined kill ring string. If the specified
1798 yank-handler arg differs from the yank-handler property of the latest
1799 kill string, STRING is added as a new kill ring element instead of
1800 being appending to the last kill.
1801 If `interprogram-cut-function' is set, pass the resulting kill to it."
1802 (let* ((cur (car kill-ring)))
1803 (kill-new (if before-p (concat string cur) (concat cur string))
1804 (or (= (length cur) 0)
1805 (equal yank-handler (get-text-property 0 'yank-handler cur)))
1806 yank-handler)))
1808 (defun current-kill (n &optional do-not-move)
1809 "Rotate the yanking point by N places, and then return that kill.
1810 If N is zero, `interprogram-paste-function' is set, and calling it
1811 returns a string, then that string is added to the front of the
1812 kill ring and returned as the latest kill.
1813 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1814 yanking point; just return the Nth kill forward."
1815 (let ((interprogram-paste (and (= n 0)
1816 interprogram-paste-function
1817 (funcall interprogram-paste-function))))
1818 (if interprogram-paste
1819 (progn
1820 ;; Disable the interprogram cut function when we add the new
1821 ;; text to the kill ring, so Emacs doesn't try to own the
1822 ;; selection, with identical text.
1823 (let ((interprogram-cut-function nil))
1824 (kill-new interprogram-paste))
1825 interprogram-paste)
1826 (or kill-ring (error "Kill ring is empty"))
1827 (let ((ARGth-kill-element
1828 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1829 (length kill-ring))
1830 kill-ring)))
1831 (or do-not-move
1832 (setq kill-ring-yank-pointer ARGth-kill-element))
1833 (car ARGth-kill-element)))))
1837 ;;;; Commands for manipulating the kill ring.
1839 (defcustom kill-read-only-ok nil
1840 "*Non-nil means don't signal an error for killing read-only text."
1841 :type 'boolean
1842 :group 'killing)
1844 (put 'text-read-only 'error-conditions
1845 '(text-read-only buffer-read-only error))
1846 (put 'text-read-only 'error-message "Text is read-only")
1848 (defun kill-region (beg end &optional yank-handler)
1849 "Kill between point and mark.
1850 The text is deleted but saved in the kill ring.
1851 The command \\[yank] can retrieve it from there.
1852 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
1854 If you want to append the killed region to the last killed text,
1855 use \\[append-next-kill] before \\[kill-region].
1857 If the buffer is read-only, Emacs will beep and refrain from deleting
1858 the text, but put the text in the kill ring anyway. This means that
1859 you can use the killing commands to copy text from a read-only buffer.
1861 This is the primitive for programs to kill text (as opposed to deleting it).
1862 Supply two arguments, character numbers indicating the stretch of text
1863 to be killed.
1864 Any command that calls this function is a \"kill command\".
1865 If the previous command was also a kill command,
1866 the text killed this time appends to the text killed last time
1867 to make one entry in the kill ring.
1869 In Lisp code, optional third arg YANK-HANDLER specifies the yank-handler
1870 text property to be set on the killed text. See `insert-for-yank'."
1871 (interactive "r")
1872 (condition-case nil
1873 (let ((string (delete-and-extract-region beg end)))
1874 (when string ;STRING is nil if BEG = END
1875 ;; Add that string to the kill ring, one way or another.
1876 (if (eq last-command 'kill-region)
1877 (kill-append string (< end beg) yank-handler)
1878 (kill-new string nil yank-handler)))
1879 (setq this-command 'kill-region))
1880 ((buffer-read-only text-read-only)
1881 ;; The code above failed because the buffer, or some of the characters
1882 ;; in the region, are read-only.
1883 ;; We should beep, in case the user just isn't aware of this.
1884 ;; However, there's no harm in putting
1885 ;; the region's text in the kill ring, anyway.
1886 (copy-region-as-kill beg end)
1887 ;; Set this-command now, so it will be set even if we get an error.
1888 (setq this-command 'kill-region)
1889 ;; This should barf, if appropriate, and give us the correct error.
1890 (if kill-read-only-ok
1891 (message "Read only text copied to kill ring")
1892 ;; Signal an error if the buffer is read-only.
1893 (barf-if-buffer-read-only)
1894 ;; If the buffer isn't read-only, the text is.
1895 (signal 'text-read-only (list (current-buffer)))))))
1897 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1898 ;; to get two copies of the text when the user accidentally types M-w and
1899 ;; then corrects it with the intended C-w.
1900 (defun copy-region-as-kill (beg end)
1901 "Save the region as if killed, but don't kill it.
1902 In Transient Mark mode, deactivate the mark.
1903 If `interprogram-cut-function' is non-nil, also save the text for a window
1904 system cut and paste."
1905 (interactive "r")
1906 (if (eq last-command 'kill-region)
1907 (kill-append (buffer-substring beg end) (< end beg))
1908 (kill-new (buffer-substring beg end)))
1909 (if transient-mark-mode
1910 (setq deactivate-mark t))
1911 nil)
1913 (defun kill-ring-save (beg end)
1914 "Save the region as if killed, but don't kill it.
1915 In Transient Mark mode, deactivate the mark.
1916 If `interprogram-cut-function' is non-nil, also save the text for a window
1917 system cut and paste.
1919 If you want to append the killed line to the last killed text,
1920 use \\[append-next-kill] before \\[kill-ring-save].
1922 This command is similar to `copy-region-as-kill', except that it gives
1923 visual feedback indicating the extent of the region being copied."
1924 (interactive "r")
1925 (copy-region-as-kill beg end)
1926 (if (interactive-p)
1927 (let ((other-end (if (= (point) beg) end beg))
1928 (opoint (point))
1929 ;; Inhibit quitting so we can make a quit here
1930 ;; look like a C-g typed as a command.
1931 (inhibit-quit t))
1932 (if (pos-visible-in-window-p other-end (selected-window))
1933 (unless transient-mark-mode
1934 ;; Swap point and mark.
1935 (set-marker (mark-marker) (point) (current-buffer))
1936 (goto-char other-end)
1937 (sit-for 1)
1938 ;; Swap back.
1939 (set-marker (mark-marker) other-end (current-buffer))
1940 (goto-char opoint)
1941 ;; If user quit, deactivate the mark
1942 ;; as C-g would as a command.
1943 (and quit-flag mark-active
1944 (deactivate-mark)))
1945 (let* ((killed-text (current-kill 0))
1946 (message-len (min (length killed-text) 40)))
1947 (if (= (point) beg)
1948 ;; Don't say "killed"; that is misleading.
1949 (message "Saved text until \"%s\""
1950 (substring killed-text (- message-len)))
1951 (message "Saved text from \"%s\""
1952 (substring killed-text 0 message-len))))))))
1954 (defun append-next-kill (&optional interactive)
1955 "Cause following command, if it kills, to append to previous kill.
1956 The argument is used for internal purposes; do not supply one."
1957 (interactive "p")
1958 ;; We don't use (interactive-p), since that breaks kbd macros.
1959 (if interactive
1960 (progn
1961 (setq this-command 'kill-region)
1962 (message "If the next command is a kill, it will append"))
1963 (setq last-command 'kill-region)))
1965 ;; Yanking.
1967 ;; This is actually used in subr.el but defcustom does not work there.
1968 (defcustom yank-excluded-properties
1969 '(read-only invisible intangible field mouse-face help-echo local-map keymap
1970 yank-handler)
1971 "*Text properties to discard when yanking."
1972 :type '(choice (const :tag "All" t) (repeat symbol))
1973 :group 'editing
1974 :version "21.4")
1976 (defvar yank-window-start nil)
1977 (defvar yank-undo-function nil
1978 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
1979 Function is called with two parameters, START and END corresponding to
1980 the value of the mark and point; it is guaranteed that START <= END.
1981 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
1983 (defun yank-pop (arg)
1984 "Replace just-yanked stretch of killed text with a different stretch.
1985 This command is allowed only immediately after a `yank' or a `yank-pop'.
1986 At such a time, the region contains a stretch of reinserted
1987 previously-killed text. `yank-pop' deletes that text and inserts in its
1988 place a different stretch of killed text.
1990 With no argument, the previous kill is inserted.
1991 With argument N, insert the Nth previous kill.
1992 If N is negative, this is a more recent kill.
1994 The sequence of kills wraps around, so that after the oldest one
1995 comes the newest one."
1996 (interactive "*p")
1997 (if (not (eq last-command 'yank))
1998 (error "Previous command was not a yank"))
1999 (setq this-command 'yank)
2000 (let ((inhibit-read-only t)
2001 (before (< (point) (mark t))))
2002 (if before
2003 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2004 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2005 (setq yank-undo-function nil)
2006 (set-marker (mark-marker) (point) (current-buffer))
2007 (insert-for-yank (current-kill arg))
2008 ;; Set the window start back where it was in the yank command,
2009 ;; if possible.
2010 (set-window-start (selected-window) yank-window-start t)
2011 (if before
2012 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2013 ;; It is cleaner to avoid activation, even though the command
2014 ;; loop would deactivate the mark because we inserted text.
2015 (goto-char (prog1 (mark t)
2016 (set-marker (mark-marker) (point) (current-buffer))))))
2017 nil)
2019 (defun yank (&optional arg)
2020 "Reinsert the last stretch of killed text.
2021 More precisely, reinsert the stretch of killed text most recently
2022 killed OR yanked. Put point at end, and set mark at beginning.
2023 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2024 With argument N, reinsert the Nth most recently killed stretch of killed
2025 text.
2026 See also the command \\[yank-pop]."
2027 (interactive "*P")
2028 (setq yank-window-start (window-start))
2029 ;; If we don't get all the way thru, make last-command indicate that
2030 ;; for the following command.
2031 (setq this-command t)
2032 (push-mark (point))
2033 (insert-for-yank (current-kill (cond
2034 ((listp arg) 0)
2035 ((eq arg '-) -1)
2036 (t (1- arg)))))
2037 (if (consp arg)
2038 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2039 ;; It is cleaner to avoid activation, even though the command
2040 ;; loop would deactivate the mark because we inserted text.
2041 (goto-char (prog1 (mark t)
2042 (set-marker (mark-marker) (point) (current-buffer)))))
2043 ;; If we do get all the way thru, make this-command indicate that.
2044 (if (eq this-command t)
2045 (setq this-command 'yank))
2046 nil)
2048 (defun rotate-yank-pointer (arg)
2049 "Rotate the yanking point in the kill ring.
2050 With argument, rotate that many kills forward (or backward, if negative)."
2051 (interactive "p")
2052 (current-kill arg))
2054 ;; Some kill commands.
2056 ;; Internal subroutine of delete-char
2057 (defun kill-forward-chars (arg)
2058 (if (listp arg) (setq arg (car arg)))
2059 (if (eq arg '-) (setq arg -1))
2060 (kill-region (point) (forward-point arg)))
2062 ;; Internal subroutine of backward-delete-char
2063 (defun kill-backward-chars (arg)
2064 (if (listp arg) (setq arg (car arg)))
2065 (if (eq arg '-) (setq arg -1))
2066 (kill-region (point) (forward-point (- arg))))
2068 (defcustom backward-delete-char-untabify-method 'untabify
2069 "*The method for untabifying when deleting backward.
2070 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2071 `hungry' -- delete all whitespace, both tabs and spaces;
2072 `all' -- delete all whitespace, including tabs, spaces and newlines;
2073 nil -- just delete one character."
2074 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2075 :version "20.3"
2076 :group 'killing)
2078 (defun backward-delete-char-untabify (arg &optional killp)
2079 "Delete characters backward, changing tabs into spaces.
2080 The exact behavior depends on `backward-delete-char-untabify-method'.
2081 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2082 Interactively, ARG is the prefix arg (default 1)
2083 and KILLP is t if a prefix arg was specified."
2084 (interactive "*p\nP")
2085 (when (eq backward-delete-char-untabify-method 'untabify)
2086 (let ((count arg))
2087 (save-excursion
2088 (while (and (> count 0) (not (bobp)))
2089 (if (= (preceding-char) ?\t)
2090 (let ((col (current-column)))
2091 (forward-char -1)
2092 (setq col (- col (current-column)))
2093 (insert-char ?\ col)
2094 (delete-char 1)))
2095 (forward-char -1)
2096 (setq count (1- count))))))
2097 (delete-backward-char
2098 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2099 ((eq backward-delete-char-untabify-method 'all)
2100 " \t\n\r"))))
2101 (if skip
2102 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2103 (point)))))
2104 (+ arg (if (zerop wh) 0 (1- wh))))
2105 arg))
2106 killp))
2108 (defun zap-to-char (arg char)
2109 "Kill up to and including ARG'th occurrence of CHAR.
2110 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2111 Goes backward if ARG is negative; error if CHAR not found."
2112 (interactive "p\ncZap to char: ")
2113 (kill-region (point) (progn
2114 (search-forward (char-to-string char) nil nil arg)
2115 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2116 (point))))
2118 ;; kill-line and its subroutines.
2120 (defcustom kill-whole-line nil
2121 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2122 :type 'boolean
2123 :group 'killing)
2125 (defun kill-line (&optional arg)
2126 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2127 With prefix argument, kill that many lines from point.
2128 Negative arguments kill lines backward.
2129 With zero argument, kills the text before point on the current line.
2131 When calling from a program, nil means \"no arg\",
2132 a number counts as a prefix arg.
2134 To kill a whole line, when point is not at the beginning, type \
2135 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2137 If `kill-whole-line' is non-nil, then this command kills the whole line
2138 including its terminating newline, when used at the beginning of a line
2139 with no argument. As a consequence, you can always kill a whole line
2140 by typing \\[beginning-of-line] \\[kill-line].
2142 If you want to append the killed line to the last killed text,
2143 use \\[append-next-kill] before \\[kill-line].
2145 If the buffer is read-only, Emacs will beep and refrain from deleting
2146 the line, but put the line in the kill ring anyway. This means that
2147 you can use this command to copy text from a read-only buffer."
2148 (interactive "P")
2149 (kill-region (point)
2150 ;; It is better to move point to the other end of the kill
2151 ;; before killing. That way, in a read-only buffer, point
2152 ;; moves across the text that is copied to the kill ring.
2153 ;; The choice has no effect on undo now that undo records
2154 ;; the value of point from before the command was run.
2155 (progn
2156 (if arg
2157 (forward-visible-line (prefix-numeric-value arg))
2158 (if (eobp)
2159 (signal 'end-of-buffer nil))
2160 (let ((end
2161 (save-excursion
2162 (end-of-visible-line) (point))))
2163 (if (or (save-excursion
2164 (skip-chars-forward " \t" end)
2165 (= (point) end))
2166 (and kill-whole-line (bolp)))
2167 (forward-visible-line 1)
2168 (goto-char end))))
2169 (point))))
2172 (defun forward-visible-line (arg)
2173 "Move forward by ARG lines, ignoring currently invisible newlines only.
2174 If ARG is negative, move backward -ARG lines.
2175 If ARG is zero, move to the beginning of the current line."
2176 (condition-case nil
2177 (if (> arg 0)
2178 (progn
2179 (while (> arg 0)
2180 (or (zerop (forward-line 1))
2181 (signal 'end-of-buffer nil))
2182 ;; If the newline we just skipped is invisible,
2183 ;; don't count it.
2184 (let ((prop
2185 (get-char-property (1- (point)) 'invisible)))
2186 (if (if (eq buffer-invisibility-spec t)
2187 prop
2188 (or (memq prop buffer-invisibility-spec)
2189 (assq prop buffer-invisibility-spec)))
2190 (setq arg (1+ arg))))
2191 (setq arg (1- arg)))
2192 ;; If invisible text follows, and it is a number of complete lines,
2193 ;; skip it.
2194 (let ((opoint (point)))
2195 (while (and (not (eobp))
2196 (let ((prop
2197 (get-char-property (point) 'invisible)))
2198 (if (eq buffer-invisibility-spec t)
2199 prop
2200 (or (memq prop buffer-invisibility-spec)
2201 (assq prop buffer-invisibility-spec)))))
2202 (goto-char
2203 (if (get-text-property (point) 'invisible)
2204 (or (next-single-property-change (point) 'invisible)
2205 (point-max))
2206 (next-overlay-change (point)))))
2207 (unless (bolp)
2208 (goto-char opoint))))
2209 (let ((first t))
2210 (while (or first (< arg 0))
2211 (if (zerop arg)
2212 (beginning-of-line)
2213 (or (zerop (forward-line -1))
2214 (signal 'beginning-of-buffer nil)))
2215 ;; If the newline we just moved to is invisible,
2216 ;; don't count it.
2217 (unless (bobp)
2218 (let ((prop
2219 (get-char-property (1- (point)) 'invisible)))
2220 (if (if (eq buffer-invisibility-spec t)
2221 prop
2222 (or (memq prop buffer-invisibility-spec)
2223 (assq prop buffer-invisibility-spec)))
2224 (setq arg (1+ arg)))))
2225 (setq first nil)
2226 (setq arg (1+ arg)))
2227 ;; If invisible text follows, and it is a number of complete lines,
2228 ;; skip it.
2229 (let ((opoint (point)))
2230 (while (and (not (bobp))
2231 (let ((prop
2232 (get-char-property (1- (point)) 'invisible)))
2233 (if (eq buffer-invisibility-spec t)
2234 prop
2235 (or (memq prop buffer-invisibility-spec)
2236 (assq prop buffer-invisibility-spec)))))
2237 (goto-char
2238 (if (get-text-property (1- (point)) 'invisible)
2239 (or (previous-single-property-change (point) 'invisible)
2240 (point-min))
2241 (previous-overlay-change (point)))))
2242 (unless (bolp)
2243 (goto-char opoint)))))
2244 ((beginning-of-buffer end-of-buffer)
2245 nil)))
2247 (defun end-of-visible-line ()
2248 "Move to end of current visible line."
2249 (end-of-line)
2250 ;; If the following character is currently invisible,
2251 ;; skip all characters with that same `invisible' property value,
2252 ;; then find the next newline.
2253 (while (and (not (eobp))
2254 (save-excursion
2255 (skip-chars-forward "^\n")
2256 (let ((prop
2257 (get-char-property (point) 'invisible)))
2258 (if (eq buffer-invisibility-spec t)
2259 prop
2260 (or (memq prop buffer-invisibility-spec)
2261 (assq prop buffer-invisibility-spec))))))
2262 (skip-chars-forward "^\n")
2263 (if (get-text-property (point) 'invisible)
2264 (goto-char (next-single-property-change (point) 'invisible))
2265 (goto-char (next-overlay-change (point))))
2266 (end-of-line)))
2268 (defun insert-buffer (buffer)
2269 "Insert after point the contents of BUFFER.
2270 Puts mark after the inserted text.
2271 BUFFER may be a buffer or a buffer name.
2273 This function is meant for the user to run interactively.
2274 Don't call it from programs!"
2275 (interactive
2276 (list
2277 (progn
2278 (barf-if-buffer-read-only)
2279 (read-buffer "Insert buffer: "
2280 (if (eq (selected-window) (next-window (selected-window)))
2281 (other-buffer (current-buffer))
2282 (window-buffer (next-window (selected-window))))
2283 t))))
2284 (or (bufferp buffer)
2285 (setq buffer (get-buffer buffer)))
2286 (let (start end newmark)
2287 (save-excursion
2288 (save-excursion
2289 (set-buffer buffer)
2290 (setq start (point-min) end (point-max)))
2291 (insert-buffer-substring buffer start end)
2292 (setq newmark (point)))
2293 (push-mark newmark))
2294 nil)
2296 (defun append-to-buffer (buffer start end)
2297 "Append to specified buffer the text of the region.
2298 It is inserted into that buffer before its point.
2300 When calling from a program, give three arguments:
2301 BUFFER (or buffer name), START and END.
2302 START and END specify the portion of the current buffer to be copied."
2303 (interactive
2304 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2305 (region-beginning) (region-end)))
2306 (let ((oldbuf (current-buffer)))
2307 (save-excursion
2308 (let* ((append-to (get-buffer-create buffer))
2309 (windows (get-buffer-window-list append-to t t))
2310 point)
2311 (set-buffer append-to)
2312 (setq point (point))
2313 (barf-if-buffer-read-only)
2314 (insert-buffer-substring oldbuf start end)
2315 (dolist (window windows)
2316 (when (= (window-point window) point)
2317 (set-window-point window (point))))))))
2319 (defun prepend-to-buffer (buffer start end)
2320 "Prepend to specified buffer the text of the region.
2321 It is inserted into that buffer after its point.
2323 When calling from a program, give three arguments:
2324 BUFFER (or buffer name), START and END.
2325 START and END specify the portion of the current buffer to be copied."
2326 (interactive "BPrepend to buffer: \nr")
2327 (let ((oldbuf (current-buffer)))
2328 (save-excursion
2329 (set-buffer (get-buffer-create buffer))
2330 (barf-if-buffer-read-only)
2331 (save-excursion
2332 (insert-buffer-substring oldbuf start end)))))
2334 (defun copy-to-buffer (buffer start end)
2335 "Copy to specified buffer the text of the region.
2336 It is inserted into that buffer, replacing existing text there.
2338 When calling from a program, give three arguments:
2339 BUFFER (or buffer name), START and END.
2340 START and END specify the portion of the current buffer to be copied."
2341 (interactive "BCopy to buffer: \nr")
2342 (let ((oldbuf (current-buffer)))
2343 (save-excursion
2344 (set-buffer (get-buffer-create buffer))
2345 (barf-if-buffer-read-only)
2346 (erase-buffer)
2347 (save-excursion
2348 (insert-buffer-substring oldbuf start end)))))
2350 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2351 (put 'mark-inactive 'error-message "The mark is not active now")
2353 (defun mark (&optional force)
2354 "Return this buffer's mark value as integer; error if mark inactive.
2355 If optional argument FORCE is non-nil, access the mark value
2356 even if the mark is not currently active, and return nil
2357 if there is no mark at all.
2359 If you are using this in an editing command, you are most likely making
2360 a mistake; see the documentation of `set-mark'."
2361 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2362 (marker-position (mark-marker))
2363 (signal 'mark-inactive nil)))
2365 ;; Many places set mark-active directly, and several of them failed to also
2366 ;; run deactivate-mark-hook. This shorthand should simplify.
2367 (defsubst deactivate-mark ()
2368 "Deactivate the mark by setting `mark-active' to nil.
2369 \(That makes a difference only in Transient Mark mode.)
2370 Also runs the hook `deactivate-mark-hook'."
2371 (cond
2372 ((eq transient-mark-mode 'lambda)
2373 (setq transient-mark-mode nil))
2374 (transient-mark-mode
2375 (setq mark-active nil)
2376 (run-hooks 'deactivate-mark-hook))))
2378 (defun set-mark (pos)
2379 "Set this buffer's mark to POS. Don't use this function!
2380 That is to say, don't use this function unless you want
2381 the user to see that the mark has moved, and you want the previous
2382 mark position to be lost.
2384 Normally, when a new mark is set, the old one should go on the stack.
2385 This is why most applications should use push-mark, not set-mark.
2387 Novice Emacs Lisp programmers often try to use the mark for the wrong
2388 purposes. The mark saves a location for the user's convenience.
2389 Most editing commands should not alter the mark.
2390 To remember a location for internal use in the Lisp program,
2391 store it in a Lisp variable. Example:
2393 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2395 (if pos
2396 (progn
2397 (setq mark-active t)
2398 (run-hooks 'activate-mark-hook)
2399 (set-marker (mark-marker) pos (current-buffer)))
2400 ;; Normally we never clear mark-active except in Transient Mark mode.
2401 ;; But when we actually clear out the mark value too,
2402 ;; we must clear mark-active in any mode.
2403 (setq mark-active nil)
2404 (run-hooks 'deactivate-mark-hook)
2405 (set-marker (mark-marker) nil)))
2407 (defvar mark-ring nil
2408 "The list of former marks of the current buffer, most recent first.")
2409 (make-variable-buffer-local 'mark-ring)
2410 (put 'mark-ring 'permanent-local t)
2412 (defcustom mark-ring-max 16
2413 "*Maximum size of mark ring. Start discarding off end if gets this big."
2414 :type 'integer
2415 :group 'editing-basics)
2417 (defvar global-mark-ring nil
2418 "The list of saved global marks, most recent first.")
2420 (defcustom global-mark-ring-max 16
2421 "*Maximum size of global mark ring. \
2422 Start discarding off end if gets this big."
2423 :type 'integer
2424 :group 'editing-basics)
2426 (defun pop-to-mark-command ()
2427 "Jump to mark, and pop a new position for mark off the ring
2428 \(does not affect global mark ring\)."
2429 (interactive)
2430 (if (null (mark t))
2431 (error "No mark set in this buffer")
2432 (goto-char (mark t))
2433 (pop-mark)))
2435 (defun push-mark-command (arg &optional nomsg)
2436 "Set mark at where point is.
2437 If no prefix arg and mark is already set there, just activate it.
2438 Display `Mark set' unless the optional second arg NOMSG is non-nil."
2439 (interactive "P")
2440 (let ((mark (marker-position (mark-marker))))
2441 (if (or arg (null mark) (/= mark (point)))
2442 (push-mark nil nomsg t)
2443 (setq mark-active t)
2444 (unless nomsg
2445 (message "Mark activated")))))
2447 (defun set-mark-command (arg)
2448 "Set mark at where point is, or jump to mark.
2449 With no prefix argument, set mark, push old mark position on local mark
2450 ring, and push mark on global mark ring. Immediately repeating the
2451 command activates `transient-mark-mode' temporarily.
2453 With argument, jump to mark, and pop a new position for mark off the ring
2454 \(does not affect global mark ring\). Repeating the command without
2455 an argument jumps to the next position off the mark ring.
2457 Novice Emacs Lisp programmers often try to use the mark for the wrong
2458 purposes. See the documentation of `set-mark' for more information."
2459 (interactive "P")
2460 (if (eq transient-mark-mode 'lambda)
2461 (setq transient-mark-mode nil))
2462 (cond
2463 ((not (eq this-command 'set-mark-command))
2464 (if arg
2465 (pop-to-mark-command)
2466 (push-mark-command t)))
2467 ((eq last-command 'pop-to-mark-command)
2468 (if (and (consp arg) (> (prefix-numeric-value arg) 4))
2469 (push-mark-command nil)
2470 (setq this-command 'pop-to-mark-command)
2471 (pop-to-mark-command)))
2472 (arg
2473 (setq this-command 'pop-to-mark-command)
2474 (pop-to-mark-command))
2475 ((and (eq last-command 'set-mark-command)
2476 mark-active (null transient-mark-mode))
2477 (setq transient-mark-mode 'lambda)
2478 (message "Transient-mark-mode temporarily enabled"))
2480 (push-mark-command nil))))
2482 (defun push-mark (&optional location nomsg activate)
2483 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2484 If the last global mark pushed was not in the current buffer,
2485 also push LOCATION on the global mark ring.
2486 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2487 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2489 Novice Emacs Lisp programmers often try to use the mark for the wrong
2490 purposes. See the documentation of `set-mark' for more information.
2492 In Transient Mark mode, this does not activate the mark."
2493 (if (null (mark t))
2495 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2496 (if (> (length mark-ring) mark-ring-max)
2497 (progn
2498 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2499 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
2500 (set-marker (mark-marker) (or location (point)) (current-buffer))
2501 ;; Now push the mark on the global mark ring.
2502 (if (and global-mark-ring
2503 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2504 ;; The last global mark pushed was in this same buffer.
2505 ;; Don't push another one.
2507 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2508 (if (> (length global-mark-ring) global-mark-ring-max)
2509 (progn
2510 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
2511 nil)
2512 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2513 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2514 (message "Mark set"))
2515 (if (or activate (not transient-mark-mode))
2516 (set-mark (mark t)))
2517 nil)
2519 (defun pop-mark ()
2520 "Pop off mark ring into the buffer's actual mark.
2521 Does not set point. Does nothing if mark ring is empty."
2522 (if mark-ring
2523 (progn
2524 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2525 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2526 (deactivate-mark)
2527 (move-marker (car mark-ring) nil)
2528 (if (null (mark t)) (ding))
2529 (setq mark-ring (cdr mark-ring)))))
2531 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2532 (defun exchange-point-and-mark (&optional arg)
2533 "Put the mark where point is now, and point where the mark is now.
2534 This command works even when the mark is not active,
2535 and it reactivates the mark.
2536 With prefix arg, `transient-mark-mode' is enabled temporarily."
2537 (interactive "P")
2538 (if arg
2539 (if mark-active
2540 (if (null transient-mark-mode)
2541 (setq transient-mark-mode 'lambda))
2542 (setq arg nil)))
2543 (unless arg
2544 (let ((omark (mark t)))
2545 (if (null omark)
2546 (error "No mark set in this buffer"))
2547 (set-mark (point))
2548 (goto-char omark)
2549 nil)))
2551 (define-minor-mode transient-mark-mode
2552 "Toggle Transient Mark mode.
2553 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2555 In Transient Mark mode, when the mark is active, the region is highlighted.
2556 Changing the buffer \"deactivates\" the mark.
2557 So do certain other operations that set the mark
2558 but whose main purpose is something else--for example,
2559 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
2561 You can also deactivate the mark by typing \\[keyboard-quit] or
2562 \\[keyboard-escape-quit].
2564 Many commands change their behavior when Transient Mark mode is in effect
2565 and the mark is active, by acting on the region instead of their usual
2566 default part of the buffer's text. Examples of such commands include
2567 \\[comment-dwim], \\[flush-lines], \\[ispell], \\[keep-lines],
2568 \\[query-replace], \\[query-replace-regexp], and \\[undo]. Invoke
2569 \\[apropos-documentation] and type \"transient\" or \"mark.*active\" at
2570 the prompt, to see the documentation of commands which are sensitive to
2571 the Transient Mark mode."
2572 :global t :group 'editing-basics :require nil)
2574 (defun pop-global-mark ()
2575 "Pop off global mark ring and jump to the top location."
2576 (interactive)
2577 ;; Pop entries which refer to non-existent buffers.
2578 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2579 (setq global-mark-ring (cdr global-mark-ring)))
2580 (or global-mark-ring
2581 (error "No global mark set"))
2582 (let* ((marker (car global-mark-ring))
2583 (buffer (marker-buffer marker))
2584 (position (marker-position marker)))
2585 (setq global-mark-ring (nconc (cdr global-mark-ring)
2586 (list (car global-mark-ring))))
2587 (set-buffer buffer)
2588 (or (and (>= position (point-min))
2589 (<= position (point-max)))
2590 (widen))
2591 (goto-char position)
2592 (switch-to-buffer buffer)))
2594 (defcustom next-line-add-newlines nil
2595 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2596 :type 'boolean
2597 :version "21.1"
2598 :group 'editing-basics)
2600 (defun next-line (&optional arg)
2601 "Move cursor vertically down ARG lines.
2602 If there is no character in the target line exactly under the current column,
2603 the cursor is positioned after the character in that line which spans this
2604 column, or at the end of the line if it is not long enough.
2605 If there is no line in the buffer after this one, behavior depends on the
2606 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2607 to create a line, and moves the cursor to that line. Otherwise it moves the
2608 cursor to the end of the buffer.
2610 The command \\[set-goal-column] can be used to create
2611 a semipermanent goal column for this command.
2612 Then instead of trying to move exactly vertically (or as close as possible),
2613 this command moves to the specified goal column (or as close as possible).
2614 The goal column is stored in the variable `goal-column', which is nil
2615 when there is no goal column.
2617 If you are thinking of using this in a Lisp program, consider
2618 using `forward-line' instead. It is usually easier to use
2619 and more reliable (no dependence on goal column, etc.)."
2620 (interactive "p")
2621 (unless arg (setq arg 1))
2622 (if (and next-line-add-newlines (= arg 1))
2623 (if (save-excursion (end-of-line) (eobp))
2624 ;; When adding a newline, don't expand an abbrev.
2625 (let ((abbrev-mode nil))
2626 (end-of-line)
2627 (insert "\n"))
2628 (line-move arg))
2629 (if (interactive-p)
2630 (condition-case nil
2631 (line-move arg)
2632 ((beginning-of-buffer end-of-buffer) (ding)))
2633 (line-move arg)))
2634 nil)
2636 (defun previous-line (&optional arg)
2637 "Move cursor vertically up ARG lines.
2638 If there is no character in the target line exactly over the current column,
2639 the cursor is positioned after the character in that line which spans this
2640 column, or at the end of the line if it is not long enough.
2642 The command \\[set-goal-column] can be used to create
2643 a semipermanent goal column for this command.
2644 Then instead of trying to move exactly vertically (or as close as possible),
2645 this command moves to the specified goal column (or as close as possible).
2646 The goal column is stored in the variable `goal-column', which is nil
2647 when there is no goal column.
2649 If you are thinking of using this in a Lisp program, consider using
2650 `forward-line' with a negative argument instead. It is usually easier
2651 to use and more reliable (no dependence on goal column, etc.)."
2652 (interactive "p")
2653 (unless arg (setq arg 1))
2654 (if (interactive-p)
2655 (condition-case nil
2656 (line-move (- arg))
2657 ((beginning-of-buffer end-of-buffer) (ding)))
2658 (line-move (- arg)))
2659 nil)
2661 (defcustom track-eol nil
2662 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2663 This means moving to the end of each line moved onto.
2664 The beginning of a blank line does not count as the end of a line."
2665 :type 'boolean
2666 :group 'editing-basics)
2668 (defcustom goal-column nil
2669 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2670 :type '(choice integer
2671 (const :tag "None" nil))
2672 :group 'editing-basics)
2673 (make-variable-buffer-local 'goal-column)
2675 (defvar temporary-goal-column 0
2676 "Current goal column for vertical motion.
2677 It is the column where point was
2678 at the start of current run of vertical motion commands.
2679 When the `track-eol' feature is doing its job, the value is 9999.")
2681 (defcustom line-move-ignore-invisible nil
2682 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2683 Outline mode sets this."
2684 :type 'boolean
2685 :group 'editing-basics)
2687 (defun line-move-invisible (pos)
2688 "Return non-nil if the character after POS is currently invisible."
2689 (let ((prop
2690 (get-char-property pos 'invisible)))
2691 (if (eq buffer-invisibility-spec t)
2692 prop
2693 (or (memq prop buffer-invisibility-spec)
2694 (assq prop buffer-invisibility-spec)))))
2696 ;; This is the guts of next-line and previous-line.
2697 ;; Arg says how many lines to move.
2698 (defun line-move (arg)
2699 ;; Don't run any point-motion hooks, and disregard intangibility,
2700 ;; for intermediate positions.
2701 (let ((inhibit-point-motion-hooks t)
2702 (opoint (point))
2703 new line-end line-beg)
2704 (unwind-protect
2705 (progn
2706 (if (not (memq last-command '(next-line previous-line)))
2707 (setq temporary-goal-column
2708 (if (and track-eol (eolp)
2709 ;; Don't count beg of empty line as end of line
2710 ;; unless we just did explicit end-of-line.
2711 (or (not (bolp)) (eq last-command 'end-of-line)))
2712 9999
2713 (current-column))))
2714 (if (and (not (integerp selective-display))
2715 (not line-move-ignore-invisible))
2716 ;; Use just newline characters.
2717 ;; Set ARG to 0 if we move as many lines as requested.
2718 (or (if (> arg 0)
2719 (progn (if (> arg 1) (forward-line (1- arg)))
2720 ;; This way of moving forward ARG lines
2721 ;; verifies that we have a newline after the last one.
2722 ;; It doesn't get confused by intangible text.
2723 (end-of-line)
2724 (if (zerop (forward-line 1))
2725 (setq arg 0)))
2726 (and (zerop (forward-line arg))
2727 (bolp)
2728 (setq arg 0)))
2729 (signal (if (< arg 0)
2730 'beginning-of-buffer
2731 'end-of-buffer)
2732 nil))
2733 ;; Move by arg lines, but ignore invisible ones.
2734 (while (> arg 0)
2735 ;; If the following character is currently invisible,
2736 ;; skip all characters with that same `invisible' property value.
2737 (while (and (not (eobp)) (line-move-invisible (point)))
2738 (goto-char (next-char-property-change (point))))
2739 ;; Now move a line.
2740 (end-of-line)
2741 (and (zerop (vertical-motion 1))
2742 (signal 'end-of-buffer nil))
2743 (setq arg (1- arg)))
2744 (while (< arg 0)
2745 (beginning-of-line)
2746 (and (zerop (vertical-motion -1))
2747 (signal 'beginning-of-buffer nil))
2748 (setq arg (1+ arg))
2749 (while (and (not (bobp)) (line-move-invisible (1- (point))))
2750 (goto-char (previous-char-property-change (point)))))))
2752 (cond ((> arg 0)
2753 ;; If we did not move down as far as desired,
2754 ;; at least go to end of line.
2755 (end-of-line))
2756 ((< arg 0)
2757 ;; If we did not move down as far as desired,
2758 ;; at least go to end of line.
2759 (beginning-of-line))
2761 (line-move-finish (or goal-column temporary-goal-column) opoint)))))
2762 nil)
2764 (defun line-move-finish (column opoint)
2765 (let ((repeat t))
2766 (while repeat
2767 ;; Set REPEAT to t to repeat the whole thing.
2768 (setq repeat nil)
2770 (let (new
2771 (line-beg (save-excursion (beginning-of-line) (point)))
2772 (line-end
2773 ;; Compute the end of the line
2774 ;; ignoring effectively intangible newlines.
2775 (let ((inhibit-point-motion-hooks nil)
2776 (inhibit-field-text-motion t))
2777 (save-excursion (end-of-line) (point)))))
2779 ;; Move to the desired column.
2780 (line-move-to-column column)
2781 (setq new (point))
2783 ;; Process intangibility within a line.
2784 ;; Move to the chosen destination position from above,
2785 ;; with intangibility processing enabled.
2787 (goto-char (point-min))
2788 (let ((inhibit-point-motion-hooks nil))
2789 (goto-char new)
2791 ;; If intangibility moves us to a different (later) place
2792 ;; in the same line, use that as the destination.
2793 (if (<= (point) line-end)
2794 (setq new (point))
2795 ;; If that position is "too late",
2796 ;; try the previous allowable position.
2797 ;; See if it is ok.
2798 (backward-char)
2799 (if (<= (point) line-end)
2800 (setq new (point))
2801 ;; As a last resort, use the end of the line.
2802 (setq new line-end))))
2804 ;; Now move to the updated destination, processing fields
2805 ;; as well as intangibility.
2806 (goto-char opoint)
2807 (let ((inhibit-point-motion-hooks nil))
2808 (goto-char
2809 (constrain-to-field new opoint nil t
2810 'inhibit-line-move-field-capture)))
2812 ;; If all this moved us to a different line,
2813 ;; retry everything within that new line.
2814 (when (or (< (point) line-beg) (> (point) line-end))
2815 ;; Repeat the intangibility and field processing.
2816 (setq repeat t))))))
2818 (defun line-move-to-column (col)
2819 "Try to find column COL, considering invisibility.
2820 This function works only in certain cases,
2821 because what we really need is for `move-to-column'
2822 and `current-column' to be able to ignore invisible text."
2823 (if (zerop col)
2824 (beginning-of-line)
2825 (move-to-column col))
2827 (when (and line-move-ignore-invisible
2828 (not (bolp)) (line-move-invisible (1- (point))))
2829 (let ((normal-location (point))
2830 (normal-column (current-column)))
2831 ;; If the following character is currently invisible,
2832 ;; skip all characters with that same `invisible' property value.
2833 (while (and (not (eobp))
2834 (line-move-invisible (point)))
2835 (goto-char (next-char-property-change (point))))
2836 ;; Have we advanced to a larger column position?
2837 (if (> (current-column) normal-column)
2838 ;; We have made some progress towards the desired column.
2839 ;; See if we can make any further progress.
2840 (line-move-to-column (+ (current-column) (- col normal-column)))
2841 ;; Otherwise, go to the place we originally found
2842 ;; and move back over invisible text.
2843 ;; that will get us to the same place on the screen
2844 ;; but with a more reasonable buffer position.
2845 (goto-char normal-location)
2846 (let ((line-beg (save-excursion (beginning-of-line) (point))))
2847 (while (and (not (bolp)) (line-move-invisible (1- (point))))
2848 (goto-char (previous-char-property-change (point) line-beg))))))))
2850 ;;; Many people have said they rarely use this feature, and often type
2851 ;;; it by accident. Maybe it shouldn't even be on a key.
2852 (put 'set-goal-column 'disabled t)
2854 (defun set-goal-column (arg)
2855 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2856 Those commands will move to this position in the line moved to
2857 rather than trying to keep the same horizontal position.
2858 With a non-nil argument, clears out the goal column
2859 so that \\[next-line] and \\[previous-line] resume vertical motion.
2860 The goal column is stored in the variable `goal-column'."
2861 (interactive "P")
2862 (if arg
2863 (progn
2864 (setq goal-column nil)
2865 (message "No goal column"))
2866 (setq goal-column (current-column))
2867 (message (substitute-command-keys
2868 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2869 goal-column))
2870 nil)
2873 (defun scroll-other-window-down (lines)
2874 "Scroll the \"other window\" down.
2875 For more details, see the documentation for `scroll-other-window'."
2876 (interactive "P")
2877 (scroll-other-window
2878 ;; Just invert the argument's meaning.
2879 ;; We can do that without knowing which window it will be.
2880 (if (eq lines '-) nil
2881 (if (null lines) '-
2882 (- (prefix-numeric-value lines))))))
2883 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2885 (defun beginning-of-buffer-other-window (arg)
2886 "Move point to the beginning of the buffer in the other window.
2887 Leave mark at previous position.
2888 With arg N, put point N/10 of the way from the true beginning."
2889 (interactive "P")
2890 (let ((orig-window (selected-window))
2891 (window (other-window-for-scrolling)))
2892 ;; We use unwind-protect rather than save-window-excursion
2893 ;; because the latter would preserve the things we want to change.
2894 (unwind-protect
2895 (progn
2896 (select-window window)
2897 ;; Set point and mark in that window's buffer.
2898 (beginning-of-buffer arg)
2899 ;; Set point accordingly.
2900 (recenter '(t)))
2901 (select-window orig-window))))
2903 (defun end-of-buffer-other-window (arg)
2904 "Move point to the end of the buffer in the other window.
2905 Leave mark at previous position.
2906 With arg N, put point N/10 of the way from the true end."
2907 (interactive "P")
2908 ;; See beginning-of-buffer-other-window for comments.
2909 (let ((orig-window (selected-window))
2910 (window (other-window-for-scrolling)))
2911 (unwind-protect
2912 (progn
2913 (select-window window)
2914 (end-of-buffer arg)
2915 (recenter '(t)))
2916 (select-window orig-window))))
2918 (defun transpose-chars (arg)
2919 "Interchange characters around point, moving forward one character.
2920 With prefix arg ARG, effect is to take character before point
2921 and drag it forward past ARG other characters (backward if ARG negative).
2922 If no argument and at end of line, the previous two chars are exchanged."
2923 (interactive "*P")
2924 (and (null arg) (eolp) (forward-char -1))
2925 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2927 (defun transpose-words (arg)
2928 "Interchange words around point, leaving point at end of them.
2929 With prefix arg ARG, effect is to take word before or around point
2930 and drag it forward past ARG other words (backward if ARG negative).
2931 If ARG is zero, the words around or after point and around or after mark
2932 are interchanged."
2933 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
2934 (interactive "*p")
2935 (transpose-subr 'forward-word arg))
2937 (defun transpose-sexps (arg)
2938 "Like \\[transpose-words] but applies to sexps.
2939 Does not work on a sexp that point is in the middle of
2940 if it is a list or string."
2941 (interactive "*p")
2942 (transpose-subr
2943 (lambda (arg)
2944 ;; Here we should try to simulate the behavior of
2945 ;; (cons (progn (forward-sexp x) (point))
2946 ;; (progn (forward-sexp (- x)) (point)))
2947 ;; Except that we don't want to rely on the second forward-sexp
2948 ;; putting us back to where we want to be, since forward-sexp-function
2949 ;; might do funny things like infix-precedence.
2950 (if (if (> arg 0)
2951 (looking-at "\\sw\\|\\s_")
2952 (and (not (bobp))
2953 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
2954 ;; Jumping over a symbol. We might be inside it, mind you.
2955 (progn (funcall (if (> arg 0)
2956 'skip-syntax-backward 'skip-syntax-forward)
2957 "w_")
2958 (cons (save-excursion (forward-sexp arg) (point)) (point)))
2959 ;; Otherwise, we're between sexps. Take a step back before jumping
2960 ;; to make sure we'll obey the same precedence no matter which direction
2961 ;; we're going.
2962 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
2963 (cons (save-excursion (forward-sexp arg) (point))
2964 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
2965 (not (zerop (funcall (if (> arg 0)
2966 'skip-syntax-forward
2967 'skip-syntax-backward)
2968 ".")))))
2969 (point)))))
2970 arg 'special))
2972 (defun transpose-lines (arg)
2973 "Exchange current line and previous line, leaving point after both.
2974 With argument ARG, takes previous line and moves it past ARG lines.
2975 With argument 0, interchanges line point is in with line mark is in."
2976 (interactive "*p")
2977 (transpose-subr (function
2978 (lambda (arg)
2979 (if (> arg 0)
2980 (progn
2981 ;; Move forward over ARG lines,
2982 ;; but create newlines if necessary.
2983 (setq arg (forward-line arg))
2984 (if (/= (preceding-char) ?\n)
2985 (setq arg (1+ arg)))
2986 (if (> arg 0)
2987 (newline arg)))
2988 (forward-line arg))))
2989 arg))
2991 (defun transpose-subr (mover arg &optional special)
2992 (let ((aux (if special mover
2993 (lambda (x)
2994 (cons (progn (funcall mover x) (point))
2995 (progn (funcall mover (- x)) (point))))))
2996 pos1 pos2)
2997 (cond
2998 ((= arg 0)
2999 (save-excursion
3000 (setq pos1 (funcall aux 1))
3001 (goto-char (mark))
3002 (setq pos2 (funcall aux 1))
3003 (transpose-subr-1 pos1 pos2))
3004 (exchange-point-and-mark))
3005 ((> arg 0)
3006 (setq pos1 (funcall aux -1))
3007 (setq pos2 (funcall aux arg))
3008 (transpose-subr-1 pos1 pos2)
3009 (goto-char (car pos2)))
3011 (setq pos1 (funcall aux -1))
3012 (goto-char (car pos1))
3013 (setq pos2 (funcall aux arg))
3014 (transpose-subr-1 pos1 pos2)))))
3016 (defun transpose-subr-1 (pos1 pos2)
3017 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3018 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3019 (when (> (car pos1) (car pos2))
3020 (let ((swap pos1))
3021 (setq pos1 pos2 pos2 swap)))
3022 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3023 (atomic-change-group
3024 (let (word2)
3025 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3026 (goto-char (car pos2))
3027 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3028 (goto-char (car pos1))
3029 (insert word2))))
3031 (defun backward-word (arg)
3032 "Move backward until encountering the beginning of a word.
3033 With argument, do this that many times."
3034 (interactive "p")
3035 (forward-word (- arg)))
3037 (defun mark-word (arg)
3038 "Set mark arg words away from point.
3039 If this command is repeated, it marks the next ARG words after the ones
3040 already marked."
3041 (interactive "p")
3042 (cond ((and (eq last-command this-command) (mark t))
3043 (set-mark
3044 (save-excursion
3045 (goto-char (mark))
3046 (forward-word arg)
3047 (point))))
3049 (push-mark
3050 (save-excursion
3051 (forward-word arg)
3052 (point))
3053 nil t))))
3055 (defun kill-word (arg)
3056 "Kill characters forward until encountering the end of a word.
3057 With argument, do this that many times."
3058 (interactive "p")
3059 (kill-region (point) (progn (forward-word arg) (point))))
3061 (defun backward-kill-word (arg)
3062 "Kill characters backward until encountering the end of a word.
3063 With argument, do this that many times."
3064 (interactive "p")
3065 (kill-word (- arg)))
3067 (defun current-word (&optional strict)
3068 "Return the word point is on (or a nearby word) as a string.
3069 If optional arg STRICT is non-nil, return nil unless point is within
3070 or adjacent to a word."
3071 (save-excursion
3072 (let ((oldpoint (point)) (start (point)) (end (point)))
3073 (skip-syntax-backward "w_") (setq start (point))
3074 (goto-char oldpoint)
3075 (skip-syntax-forward "w_") (setq end (point))
3076 (if (and (eq start oldpoint) (eq end oldpoint))
3077 ;; Point is neither within nor adjacent to a word.
3078 (and (not strict)
3079 (progn
3080 ;; Look for preceding word in same line.
3081 (skip-syntax-backward "^w_"
3082 (save-excursion (beginning-of-line)
3083 (point)))
3084 (if (bolp)
3085 ;; No preceding word in same line.
3086 ;; Look for following word in same line.
3087 (progn
3088 (skip-syntax-forward "^w_"
3089 (save-excursion (end-of-line)
3090 (point)))
3091 (setq start (point))
3092 (skip-syntax-forward "w_")
3093 (setq end (point)))
3094 (setq end (point))
3095 (skip-syntax-backward "w_")
3096 (setq start (point)))
3097 (buffer-substring-no-properties start end)))
3098 (buffer-substring-no-properties start end)))))
3100 (defcustom fill-prefix nil
3101 "*String for filling to insert at front of new line, or nil for none."
3102 :type '(choice (const :tag "None" nil)
3103 string)
3104 :group 'fill)
3105 (make-variable-buffer-local 'fill-prefix)
3107 (defcustom auto-fill-inhibit-regexp nil
3108 "*Regexp to match lines which should not be auto-filled."
3109 :type '(choice (const :tag "None" nil)
3110 regexp)
3111 :group 'fill)
3113 (defvar comment-line-break-function 'comment-indent-new-line
3114 "*Mode-specific function which line breaks and continues a comment.
3116 This function is only called during auto-filling of a comment section.
3117 The function should take a single optional argument, which is a flag
3118 indicating whether it should use soft newlines.
3120 Setting this variable automatically makes it local to the current buffer.")
3122 ;; This function is used as the auto-fill-function of a buffer
3123 ;; when Auto-Fill mode is enabled.
3124 ;; It returns t if it really did any work.
3125 ;; (Actually some major modes use a different auto-fill function,
3126 ;; but this one is the default one.)
3127 (defun do-auto-fill ()
3128 (let (fc justify bol give-up
3129 (fill-prefix fill-prefix))
3130 (if (or (not (setq justify (current-justification)))
3131 (null (setq fc (current-fill-column)))
3132 (and (eq justify 'left)
3133 (<= (current-column) fc))
3134 (save-excursion (beginning-of-line)
3135 (setq bol (point))
3136 (and auto-fill-inhibit-regexp
3137 (looking-at auto-fill-inhibit-regexp))))
3138 nil ;; Auto-filling not required
3139 (if (memq justify '(full center right))
3140 (save-excursion (unjustify-current-line)))
3142 ;; Choose a fill-prefix automatically.
3143 (when (and adaptive-fill-mode
3144 (or (null fill-prefix) (string= fill-prefix "")))
3145 (let ((prefix
3146 (fill-context-prefix
3147 (save-excursion (backward-paragraph 1) (point))
3148 (save-excursion (forward-paragraph 1) (point)))))
3149 (and prefix (not (equal prefix ""))
3150 ;; Use auto-indentation rather than a guessed empty prefix.
3151 (not (and fill-indent-according-to-mode
3152 (string-match "\\`[ \t]*\\'" prefix)))
3153 (setq fill-prefix prefix))))
3155 (while (and (not give-up) (> (current-column) fc))
3156 ;; Determine where to split the line.
3157 (let* (after-prefix
3158 (fill-point
3159 (let ((opoint (point)))
3160 (save-excursion
3161 (beginning-of-line)
3162 (setq after-prefix (point))
3163 (and fill-prefix
3164 (looking-at (regexp-quote fill-prefix))
3165 (setq after-prefix (match-end 0)))
3166 (move-to-column (1+ fc))
3167 (fill-move-to-break-point after-prefix)
3168 (point)))))
3170 ;; See whether the place we found is any good.
3171 (if (save-excursion
3172 (goto-char fill-point)
3173 (or (bolp)
3174 ;; There is no use breaking at end of line.
3175 (save-excursion (skip-chars-forward " ") (eolp))
3176 ;; It is futile to split at the end of the prefix
3177 ;; since we would just insert the prefix again.
3178 (and after-prefix (<= (point) after-prefix))
3179 ;; Don't split right after a comment starter
3180 ;; since we would just make another comment starter.
3181 (and comment-start-skip
3182 (let ((limit (point)))
3183 (beginning-of-line)
3184 (and (re-search-forward comment-start-skip
3185 limit t)
3186 (eq (point) limit))))))
3187 ;; No good place to break => stop trying.
3188 (setq give-up t)
3189 ;; Ok, we have a useful place to break the line. Do it.
3190 (let ((prev-column (current-column)))
3191 ;; If point is at the fill-point, do not `save-excursion'.
3192 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3193 ;; point will end up before it rather than after it.
3194 (if (save-excursion
3195 (skip-chars-backward " \t")
3196 (= (point) fill-point))
3197 (funcall comment-line-break-function t)
3198 (save-excursion
3199 (goto-char fill-point)
3200 (funcall comment-line-break-function t)))
3201 ;; Now do justification, if required
3202 (if (not (eq justify 'left))
3203 (save-excursion
3204 (end-of-line 0)
3205 (justify-current-line justify nil t)))
3206 ;; If making the new line didn't reduce the hpos of
3207 ;; the end of the line, then give up now;
3208 ;; trying again will not help.
3209 (if (>= (current-column) prev-column)
3210 (setq give-up t))))))
3211 ;; Justify last line.
3212 (justify-current-line justify t t)
3213 t)))
3215 (defvar normal-auto-fill-function 'do-auto-fill
3216 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3217 Some major modes set this.")
3219 ;; FIXME: turn into a proper minor mode.
3220 ;; Add a global minor mode version of it.
3221 (defun auto-fill-mode (&optional arg)
3222 "Toggle Auto Fill mode.
3223 With arg, turn Auto Fill mode on if and only if arg is positive.
3224 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3225 automatically breaks the line at a previous space.
3227 The value of `normal-auto-fill-function' specifies the function to use
3228 for `auto-fill-function' when turning Auto Fill mode on."
3229 (interactive "P")
3230 (prog1 (setq auto-fill-function
3231 (if (if (null arg)
3232 (not auto-fill-function)
3233 (> (prefix-numeric-value arg) 0))
3234 normal-auto-fill-function
3235 nil))
3236 (force-mode-line-update)))
3238 ;; This holds a document string used to document auto-fill-mode.
3239 (defun auto-fill-function ()
3240 "Automatically break line at a previous space, in insertion of text."
3241 nil)
3243 (defun turn-on-auto-fill ()
3244 "Unconditionally turn on Auto Fill mode."
3245 (auto-fill-mode 1))
3247 (defun turn-off-auto-fill ()
3248 "Unconditionally turn off Auto Fill mode."
3249 (auto-fill-mode -1))
3251 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3253 (defun set-fill-column (arg)
3254 "Set `fill-column' to specified argument.
3255 Use \\[universal-argument] followed by a number to specify a column.
3256 Just \\[universal-argument] as argument means to use the current column."
3257 (interactive "P")
3258 (if (consp arg)
3259 (setq arg (current-column)))
3260 (if (not (integerp arg))
3261 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3262 (error "set-fill-column requires an explicit argument")
3263 (message "Fill column set to %d (was %d)" arg fill-column)
3264 (setq fill-column arg)))
3266 (defun set-selective-display (arg)
3267 "Set `selective-display' to ARG; clear it if no arg.
3268 When the value of `selective-display' is a number > 0,
3269 lines whose indentation is >= that value are not displayed.
3270 The variable `selective-display' has a separate value for each buffer."
3271 (interactive "P")
3272 (if (eq selective-display t)
3273 (error "selective-display already in use for marked lines"))
3274 (let ((current-vpos
3275 (save-restriction
3276 (narrow-to-region (point-min) (point))
3277 (goto-char (window-start))
3278 (vertical-motion (window-height)))))
3279 (setq selective-display
3280 (and arg (prefix-numeric-value arg)))
3281 (recenter current-vpos))
3282 (set-window-start (selected-window) (window-start (selected-window)))
3283 (princ "selective-display set to " t)
3284 (prin1 selective-display t)
3285 (princ "." t))
3287 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
3288 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
3290 (defun toggle-truncate-lines (arg)
3291 "Toggle whether to fold or truncate long lines on the screen.
3292 With arg, truncate long lines iff arg is positive.
3293 Note that in side-by-side windows, truncation is always enabled."
3294 (interactive "P")
3295 (setq truncate-lines
3296 (if (null arg)
3297 (not truncate-lines)
3298 (> (prefix-numeric-value arg) 0)))
3299 (force-mode-line-update)
3300 (unless truncate-lines
3301 (let ((buffer (current-buffer)))
3302 (walk-windows (lambda (window)
3303 (if (eq buffer (window-buffer window))
3304 (set-window-hscroll window 0)))
3305 nil t)))
3306 (message "Truncate long lines %s"
3307 (if truncate-lines "enabled" "disabled")))
3309 (defvar overwrite-mode-textual " Ovwrt"
3310 "The string displayed in the mode line when in overwrite mode.")
3311 (defvar overwrite-mode-binary " Bin Ovwrt"
3312 "The string displayed in the mode line when in binary overwrite mode.")
3314 (defun overwrite-mode (arg)
3315 "Toggle overwrite mode.
3316 With arg, turn overwrite mode on iff arg is positive.
3317 In overwrite mode, printing characters typed in replace existing text
3318 on a one-for-one basis, rather than pushing it to the right. At the
3319 end of a line, such characters extend the line. Before a tab,
3320 such characters insert until the tab is filled in.
3321 \\[quoted-insert] still inserts characters in overwrite mode; this
3322 is supposed to make it easier to insert characters when necessary."
3323 (interactive "P")
3324 (setq overwrite-mode
3325 (if (if (null arg) (not overwrite-mode)
3326 (> (prefix-numeric-value arg) 0))
3327 'overwrite-mode-textual))
3328 (force-mode-line-update))
3330 (defun binary-overwrite-mode (arg)
3331 "Toggle binary overwrite mode.
3332 With arg, turn binary overwrite mode on iff arg is positive.
3333 In binary overwrite mode, printing characters typed in replace
3334 existing text. Newlines are not treated specially, so typing at the
3335 end of a line joins the line to the next, with the typed character
3336 between them. Typing before a tab character simply replaces the tab
3337 with the character typed.
3338 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3339 typing characters do.
3341 Note that binary overwrite mode is not its own minor mode; it is a
3342 specialization of overwrite-mode, entered by setting the
3343 `overwrite-mode' variable to `overwrite-mode-binary'."
3344 (interactive "P")
3345 (setq overwrite-mode
3346 (if (if (null arg)
3347 (not (eq overwrite-mode 'overwrite-mode-binary))
3348 (> (prefix-numeric-value arg) 0))
3349 'overwrite-mode-binary))
3350 (force-mode-line-update))
3352 (define-minor-mode line-number-mode
3353 "Toggle Line Number mode.
3354 With arg, turn Line Number mode on iff arg is positive.
3355 When Line Number mode is enabled, the line number appears
3356 in the mode line.
3358 Line numbers do not appear for very large buffers and buffers
3359 with very long lines; see variables `line-number-display-limit'
3360 and `line-number-display-limit-width'."
3361 :init-value t :global t :group 'editing-basics :require nil)
3363 (define-minor-mode column-number-mode
3364 "Toggle Column Number mode.
3365 With arg, turn Column Number mode on iff arg is positive.
3366 When Column Number mode is enabled, the column number appears
3367 in the mode line."
3368 :global t :group 'editing-basics :require nil)
3370 (defgroup paren-blinking nil
3371 "Blinking matching of parens and expressions."
3372 :prefix "blink-matching-"
3373 :group 'paren-matching)
3375 (defcustom blink-matching-paren t
3376 "*Non-nil means show matching open-paren when close-paren is inserted."
3377 :type 'boolean
3378 :group 'paren-blinking)
3380 (defcustom blink-matching-paren-on-screen t
3381 "*Non-nil means show matching open-paren when it is on screen.
3382 If nil, means don't show it (but the open-paren can still be shown
3383 when it is off screen)."
3384 :type 'boolean
3385 :group 'paren-blinking)
3387 (defcustom blink-matching-paren-distance (* 25 1024)
3388 "*If non-nil, is maximum distance to search for matching open-paren."
3389 :type 'integer
3390 :group 'paren-blinking)
3392 (defcustom blink-matching-delay 1
3393 "*Time in seconds to delay after showing a matching paren."
3394 :type 'number
3395 :group 'paren-blinking)
3397 (defcustom blink-matching-paren-dont-ignore-comments nil
3398 "*Non-nil means `blink-matching-paren' will not ignore comments."
3399 :type 'boolean
3400 :group 'paren-blinking)
3402 (defun blink-matching-open ()
3403 "Move cursor momentarily to the beginning of the sexp before point."
3404 (interactive)
3405 (and (> (point) (1+ (point-min)))
3406 blink-matching-paren
3407 ;; Verify an even number of quoting characters precede the close.
3408 (= 1 (logand 1 (- (point)
3409 (save-excursion
3410 (forward-char -1)
3411 (skip-syntax-backward "/\\")
3412 (point)))))
3413 (let* ((oldpos (point))
3414 (blinkpos)
3415 (mismatch))
3416 (save-excursion
3417 (save-restriction
3418 (if blink-matching-paren-distance
3419 (narrow-to-region (max (point-min)
3420 (- (point) blink-matching-paren-distance))
3421 oldpos))
3422 (condition-case ()
3423 (let ((parse-sexp-ignore-comments
3424 (and parse-sexp-ignore-comments
3425 (not blink-matching-paren-dont-ignore-comments))))
3426 (setq blinkpos (scan-sexps oldpos -1)))
3427 (error nil)))
3428 (and blinkpos
3429 (/= (char-syntax (char-after blinkpos))
3430 ?\$)
3431 (setq mismatch
3432 (or (null (matching-paren (char-after blinkpos)))
3433 (/= (char-after (1- oldpos))
3434 (matching-paren (char-after blinkpos))))))
3435 (if mismatch (setq blinkpos nil))
3436 (if blinkpos
3437 ;; Don't log messages about paren matching.
3438 (let (message-log-max)
3439 (goto-char blinkpos)
3440 (if (pos-visible-in-window-p)
3441 (and blink-matching-paren-on-screen
3442 (sit-for blink-matching-delay))
3443 (goto-char blinkpos)
3444 (message
3445 "Matches %s"
3446 ;; Show what precedes the open in its line, if anything.
3447 (if (save-excursion
3448 (skip-chars-backward " \t")
3449 (not (bolp)))
3450 (buffer-substring (progn (beginning-of-line) (point))
3451 (1+ blinkpos))
3452 ;; Show what follows the open in its line, if anything.
3453 (if (save-excursion
3454 (forward-char 1)
3455 (skip-chars-forward " \t")
3456 (not (eolp)))
3457 (buffer-substring blinkpos
3458 (progn (end-of-line) (point)))
3459 ;; Otherwise show the previous nonblank line,
3460 ;; if there is one.
3461 (if (save-excursion
3462 (skip-chars-backward "\n \t")
3463 (not (bobp)))
3464 (concat
3465 (buffer-substring (progn
3466 (skip-chars-backward "\n \t")
3467 (beginning-of-line)
3468 (point))
3469 (progn (end-of-line)
3470 (skip-chars-backward " \t")
3471 (point)))
3472 ;; Replace the newline and other whitespace with `...'.
3473 "..."
3474 (buffer-substring blinkpos (1+ blinkpos)))
3475 ;; There is nothing to show except the char itself.
3476 (buffer-substring blinkpos (1+ blinkpos))))))))
3477 (cond (mismatch
3478 (message "Mismatched parentheses"))
3479 ((not blink-matching-paren-distance)
3480 (message "Unmatched parenthesis"))))))))
3482 ;Turned off because it makes dbx bomb out.
3483 (setq blink-paren-function 'blink-matching-open)
3485 ;; This executes C-g typed while Emacs is waiting for a command.
3486 ;; Quitting out of a program does not go through here;
3487 ;; that happens in the QUIT macro at the C code level.
3488 (defun keyboard-quit ()
3489 "Signal a `quit' condition.
3490 During execution of Lisp code, this character causes a quit directly.
3491 At top-level, as an editor command, this simply beeps."
3492 (interactive)
3493 (deactivate-mark)
3494 (setq defining-kbd-macro nil)
3495 (signal 'quit nil))
3497 (define-key global-map "\C-g" 'keyboard-quit)
3499 (defvar buffer-quit-function nil
3500 "Function to call to \"quit\" the current buffer, or nil if none.
3501 \\[keyboard-escape-quit] calls this function when its more local actions
3502 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3504 (defun keyboard-escape-quit ()
3505 "Exit the current \"mode\" (in a generalized sense of the word).
3506 This command can exit an interactive command such as `query-replace',
3507 can clear out a prefix argument or a region,
3508 can get out of the minibuffer or other recursive edit,
3509 cancel the use of the current buffer (for special-purpose buffers),
3510 or go back to just one window (by deleting all but the selected window)."
3511 (interactive)
3512 (cond ((eq last-command 'mode-exited) nil)
3513 ((> (minibuffer-depth) 0)
3514 (abort-recursive-edit))
3515 (current-prefix-arg
3516 nil)
3517 ((and transient-mark-mode
3518 mark-active)
3519 (deactivate-mark))
3520 ((> (recursion-depth) 0)
3521 (exit-recursive-edit))
3522 (buffer-quit-function
3523 (funcall buffer-quit-function))
3524 ((not (one-window-p t))
3525 (delete-other-windows))
3526 ((string-match "^ \\*" (buffer-name (current-buffer)))
3527 (bury-buffer))))
3529 (defun play-sound-file (file &optional volume device)
3530 "Play sound stored in FILE.
3531 VOLUME and DEVICE correspond to the keywords of the sound
3532 specification for `play-sound'."
3533 (interactive "fPlay sound file: ")
3534 (let ((sound (list :file file)))
3535 (if volume
3536 (plist-put sound :volume volume))
3537 (if device
3538 (plist-put sound :device device))
3539 (push 'sound sound)
3540 (play-sound sound)))
3542 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3544 (defcustom read-mail-command 'rmail
3545 "*Your preference for a mail reading package.
3546 This is used by some keybindings which support reading mail.
3547 See also `mail-user-agent' concerning sending mail."
3548 :type '(choice (function-item rmail)
3549 (function-item gnus)
3550 (function-item mh-rmail)
3551 (function :tag "Other"))
3552 :version "21.1"
3553 :group 'mail)
3555 (defcustom mail-user-agent 'sendmail-user-agent
3556 "*Your preference for a mail composition package.
3557 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3558 outgoing email message. This variable lets you specify which
3559 mail-sending package you prefer.
3561 Valid values include:
3563 `sendmail-user-agent' -- use the default Emacs Mail package.
3564 See Info node `(emacs)Sending Mail'.
3565 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3566 See Info node `(mh-e)'.
3567 `message-user-agent' -- use the Gnus Message package.
3568 See Info node `(message)'.
3569 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3570 paraphernalia, particularly the Gcc: header for
3571 archiving.
3573 Additional valid symbols may be available; check with the author of
3574 your package for details. The function should return non-nil if it
3575 succeeds.
3577 See also `read-mail-command' concerning reading mail."
3578 :type '(radio (function-item :tag "Default Emacs mail"
3579 :format "%t\n"
3580 sendmail-user-agent)
3581 (function-item :tag "Emacs interface to MH"
3582 :format "%t\n"
3583 mh-e-user-agent)
3584 (function-item :tag "Gnus Message package"
3585 :format "%t\n"
3586 message-user-agent)
3587 (function-item :tag "Gnus Message with full Gnus features"
3588 :format "%t\n"
3589 gnus-user-agent)
3590 (function :tag "Other"))
3591 :group 'mail)
3593 (define-mail-user-agent 'sendmail-user-agent
3594 'sendmail-user-agent-compose
3595 'mail-send-and-exit)
3597 (defun rfc822-goto-eoh ()
3598 ;; Go to header delimiter line in a mail message, following RFC822 rules
3599 (goto-char (point-min))
3600 (when (re-search-forward
3601 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
3602 (goto-char (match-beginning 0))))
3604 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3605 switch-function yank-action
3606 send-actions)
3607 (if switch-function
3608 (let ((special-display-buffer-names nil)
3609 (special-display-regexps nil)
3610 (same-window-buffer-names nil)
3611 (same-window-regexps nil))
3612 (funcall switch-function "*mail*")))
3613 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3614 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers)))
3615 (body (cdr (assoc-ignore-case "body" other-headers))))
3616 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3617 continue
3618 (error "Message aborted"))
3619 (save-excursion
3620 (rfc822-goto-eoh)
3621 (while other-headers
3622 (unless (member-ignore-case (car (car other-headers))
3623 '("in-reply-to" "cc" "body"))
3624 (insert (car (car other-headers)) ": "
3625 (cdr (car other-headers)) "\n"))
3626 (setq other-headers (cdr other-headers)))
3627 (when body
3628 (forward-line 1)
3629 (insert body))
3630 t)))
3632 (define-mail-user-agent 'mh-e-user-agent
3633 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3634 'mh-before-send-letter-hook)
3636 (defun compose-mail (&optional to subject other-headers continue
3637 switch-function yank-action send-actions)
3638 "Start composing a mail message to send.
3639 This uses the user's chosen mail composition package
3640 as selected with the variable `mail-user-agent'.
3641 The optional arguments TO and SUBJECT specify recipients
3642 and the initial Subject field, respectively.
3644 OTHER-HEADERS is an alist specifying additional
3645 header fields. Elements look like (HEADER . VALUE) where both
3646 HEADER and VALUE are strings.
3648 CONTINUE, if non-nil, says to continue editing a message already
3649 being composed.
3651 SWITCH-FUNCTION, if non-nil, is a function to use to
3652 switch to and display the buffer used for mail composition.
3654 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3655 to insert the raw text of the message being replied to.
3656 It has the form (FUNCTION . ARGS). The user agent will apply
3657 FUNCTION to ARGS, to insert the raw text of the original message.
3658 \(The user agent will also run `mail-citation-hook', *after* the
3659 original text has been inserted in this way.)
3661 SEND-ACTIONS is a list of actions to call when the message is sent.
3662 Each action has the form (FUNCTION . ARGS)."
3663 (interactive
3664 (list nil nil nil current-prefix-arg))
3665 (let ((function (get mail-user-agent 'composefunc)))
3666 (funcall function to subject other-headers continue
3667 switch-function yank-action send-actions)))
3669 (defun compose-mail-other-window (&optional to subject other-headers continue
3670 yank-action send-actions)
3671 "Like \\[compose-mail], but edit the outgoing message in another window."
3672 (interactive
3673 (list nil nil nil current-prefix-arg))
3674 (compose-mail to subject other-headers continue
3675 'switch-to-buffer-other-window yank-action send-actions))
3678 (defun compose-mail-other-frame (&optional to subject other-headers continue
3679 yank-action send-actions)
3680 "Like \\[compose-mail], but edit the outgoing message in another frame."
3681 (interactive
3682 (list nil nil nil current-prefix-arg))
3683 (compose-mail to subject other-headers continue
3684 'switch-to-buffer-other-frame yank-action send-actions))
3686 (defvar set-variable-value-history nil
3687 "History of values entered with `set-variable'.")
3689 (defun set-variable (var val &optional make-local)
3690 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3691 When using this interactively, enter a Lisp object for VALUE.
3692 If you want VALUE to be a string, you must surround it with doublequotes.
3693 VALUE is used literally, not evaluated.
3695 If VARIABLE has a `variable-interactive' property, that is used as if
3696 it were the arg to `interactive' (which see) to interactively read VALUE.
3698 If VARIABLE has been defined with `defcustom', then the type information
3699 in the definition is used to check that VALUE is valid.
3701 With a prefix argument, set VARIABLE to VALUE buffer-locally."
3702 (interactive
3703 (let* ((default-var (variable-at-point))
3704 (var (if (symbolp default-var)
3705 (read-variable (format "Set variable (default %s): " default-var)
3706 default-var)
3707 (read-variable "Set variable: ")))
3708 (minibuffer-help-form '(describe-variable var))
3709 (prop (get var 'variable-interactive))
3710 (prompt (format "Set %s%s to value: " var
3711 (cond ((local-variable-p var)
3712 " (buffer-local)")
3713 ((or current-prefix-arg
3714 (local-variable-if-set-p var))
3715 " buffer-locally")
3716 (t " globally"))))
3717 (val (if prop
3718 ;; Use VAR's `variable-interactive' property
3719 ;; as an interactive spec for prompting.
3720 (call-interactively `(lambda (arg)
3721 (interactive ,prop)
3722 arg))
3723 (read
3724 (read-string prompt nil
3725 'set-variable-value-history)))))
3726 (list var val current-prefix-arg)))
3728 (and (custom-variable-p var)
3729 (not (get var 'custom-type))
3730 (custom-load-symbol var))
3731 (let ((type (get var 'custom-type)))
3732 (when type
3733 ;; Match with custom type.
3734 (require 'cus-edit)
3735 (setq type (widget-convert type))
3736 (unless (widget-apply type :match val)
3737 (error "Value `%S' does not match type %S of %S"
3738 val (car type) var))))
3740 (if make-local
3741 (make-local-variable var))
3743 (set var val)
3745 ;; Force a thorough redisplay for the case that the variable
3746 ;; has an effect on the display, like `tab-width' has.
3747 (force-mode-line-update))
3749 ;; Define the major mode for lists of completions.
3751 (defvar completion-list-mode-map nil
3752 "Local map for completion list buffers.")
3753 (or completion-list-mode-map
3754 (let ((map (make-sparse-keymap)))
3755 (define-key map [mouse-2] 'mouse-choose-completion)
3756 (define-key map [down-mouse-2] nil)
3757 (define-key map "\C-m" 'choose-completion)
3758 (define-key map "\e\e\e" 'delete-completion-window)
3759 (define-key map [left] 'previous-completion)
3760 (define-key map [right] 'next-completion)
3761 (setq completion-list-mode-map map)))
3763 ;; Completion mode is suitable only for specially formatted data.
3764 (put 'completion-list-mode 'mode-class 'special)
3766 (defvar completion-reference-buffer nil
3767 "Record the buffer that was current when the completion list was requested.
3768 This is a local variable in the completion list buffer.
3769 Initial value is nil to avoid some compiler warnings.")
3771 (defvar completion-no-auto-exit nil
3772 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3773 This also applies to other functions such as `choose-completion'
3774 and `mouse-choose-completion'.")
3776 (defvar completion-base-size nil
3777 "Number of chars at beginning of minibuffer not involved in completion.
3778 This is a local variable in the completion list buffer
3779 but it talks about the buffer in `completion-reference-buffer'.
3780 If this is nil, it means to compare text to determine which part
3781 of the tail end of the buffer's text is involved in completion.")
3783 (defun delete-completion-window ()
3784 "Delete the completion list window.
3785 Go to the window from which completion was requested."
3786 (interactive)
3787 (let ((buf completion-reference-buffer))
3788 (if (one-window-p t)
3789 (if (window-dedicated-p (selected-window))
3790 (delete-frame (selected-frame)))
3791 (delete-window (selected-window))
3792 (if (get-buffer-window buf)
3793 (select-window (get-buffer-window buf))))))
3795 (defun previous-completion (n)
3796 "Move to the previous item in the completion list."
3797 (interactive "p")
3798 (next-completion (- n)))
3800 (defun next-completion (n)
3801 "Move to the next item in the completion list.
3802 With prefix argument N, move N items (negative N means move backward)."
3803 (interactive "p")
3804 (let ((beg (point-min)) (end (point-max)))
3805 (while (and (> n 0) (not (eobp)))
3806 ;; If in a completion, move to the end of it.
3807 (when (get-text-property (point) 'mouse-face)
3808 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3809 ;; Move to start of next one.
3810 (unless (get-text-property (point) 'mouse-face)
3811 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3812 (setq n (1- n)))
3813 (while (and (< n 0) (not (bobp)))
3814 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
3815 ;; If in a completion, move to the start of it.
3816 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
3817 (goto-char (previous-single-property-change
3818 (point) 'mouse-face nil beg)))
3819 ;; Move to end of the previous completion.
3820 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
3821 (goto-char (previous-single-property-change
3822 (point) 'mouse-face nil beg)))
3823 ;; Move to the start of that one.
3824 (goto-char (previous-single-property-change
3825 (point) 'mouse-face nil beg))
3826 (setq n (1+ n))))))
3828 (defun choose-completion ()
3829 "Choose the completion that point is in or next to."
3830 (interactive)
3831 (let (beg end completion (buffer completion-reference-buffer)
3832 (base-size completion-base-size))
3833 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3834 (setq end (point) beg (1+ (point))))
3835 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3836 (setq end (1- (point)) beg (point)))
3837 (if (null beg)
3838 (error "No completion here"))
3839 (setq beg (previous-single-property-change beg 'mouse-face))
3840 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3841 (setq completion (buffer-substring beg end))
3842 (let ((owindow (selected-window)))
3843 (if (and (one-window-p t 'selected-frame)
3844 (window-dedicated-p (selected-window)))
3845 ;; This is a special buffer's frame
3846 (iconify-frame (selected-frame))
3847 (or (window-dedicated-p (selected-window))
3848 (bury-buffer)))
3849 (select-window owindow))
3850 (choose-completion-string completion buffer base-size)))
3852 ;; Delete the longest partial match for STRING
3853 ;; that can be found before POINT.
3854 (defun choose-completion-delete-max-match (string)
3855 (let ((opoint (point))
3856 len)
3857 ;; Try moving back by the length of the string.
3858 (goto-char (max (- (point) (length string))
3859 (minibuffer-prompt-end)))
3860 ;; See how far back we were actually able to move. That is the
3861 ;; upper bound on how much we can match and delete.
3862 (setq len (- opoint (point)))
3863 (if completion-ignore-case
3864 (setq string (downcase string)))
3865 (while (and (> len 0)
3866 (let ((tail (buffer-substring (point) opoint)))
3867 (if completion-ignore-case
3868 (setq tail (downcase tail)))
3869 (not (string= tail (substring string 0 len)))))
3870 (setq len (1- len))
3871 (forward-char 1))
3872 (delete-char len)))
3874 (defvar choose-completion-string-functions nil
3875 "Functions that may override the normal insertion of a completion choice.
3876 These functions are called in order with four arguments:
3877 CHOICE - the string to insert in the buffer,
3878 BUFFER - the buffer in which the choice should be inserted,
3879 MINI-P - non-nil iff BUFFER is a minibuffer, and
3880 BASE-SIZE - the number of characters in BUFFER before
3881 the string being completed.
3883 If a function in the list returns non-nil, that function is supposed
3884 to have inserted the CHOICE in the BUFFER, and possibly exited
3885 the minibuffer; no further functions will be called.
3887 If all functions in the list return nil, that means to use
3888 the default method of inserting the completion in BUFFER.")
3890 (defun choose-completion-string (choice &optional buffer base-size)
3891 "Switch to BUFFER and insert the completion choice CHOICE.
3892 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3893 to keep. If it is nil, we call `choose-completion-delete-max-match'
3894 to decide what to delete."
3896 ;; If BUFFER is the minibuffer, exit the minibuffer
3897 ;; unless it is reading a file name and CHOICE is a directory,
3898 ;; or completion-no-auto-exit is non-nil.
3900 (let ((buffer (or buffer completion-reference-buffer))
3901 (mini-p (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3902 (buffer-name buffer))))
3903 ;; If BUFFER is a minibuffer, barf unless it's the currently
3904 ;; active minibuffer.
3905 (if (and mini-p
3906 (or (not (active-minibuffer-window))
3907 (not (equal buffer
3908 (window-buffer (active-minibuffer-window))))))
3909 (error "Minibuffer is not active for completion")
3910 (unless (run-hook-with-args-until-success
3911 'choose-completion-string-functions
3912 choice buffer mini-p base-size)
3913 ;; Insert the completion into the buffer where it was requested.
3914 (set-buffer buffer)
3915 (if base-size
3916 (delete-region (+ base-size (if mini-p
3917 (minibuffer-prompt-end)
3918 (point-min)))
3919 (point))
3920 (choose-completion-delete-max-match choice))
3921 (insert choice)
3922 (remove-text-properties (- (point) (length choice)) (point)
3923 '(mouse-face nil))
3924 ;; Update point in the window that BUFFER is showing in.
3925 (let ((window (get-buffer-window buffer t)))
3926 (set-window-point window (point)))
3927 ;; If completing for the minibuffer, exit it with this choice.
3928 (and (not completion-no-auto-exit)
3929 (equal buffer (window-buffer (minibuffer-window)))
3930 minibuffer-completion-table
3931 ;; If this is reading a file name, and the file name chosen
3932 ;; is a directory, don't exit the minibuffer.
3933 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3934 (file-directory-p (field-string (point-max))))
3935 (let ((mini (active-minibuffer-window)))
3936 (select-window mini)
3937 (when minibuffer-auto-raise
3938 (raise-frame (window-frame mini))))
3939 (exit-minibuffer)))))))
3941 (defun completion-list-mode ()
3942 "Major mode for buffers showing lists of possible completions.
3943 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3944 to select the completion near point.
3945 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3946 with the mouse."
3947 (interactive)
3948 (kill-all-local-variables)
3949 (use-local-map completion-list-mode-map)
3950 (setq mode-name "Completion List")
3951 (setq major-mode 'completion-list-mode)
3952 (make-local-variable 'completion-base-size)
3953 (setq completion-base-size nil)
3954 (run-hooks 'completion-list-mode-hook))
3956 (defun completion-list-mode-finish ()
3957 "Finish setup of the completions buffer.
3958 Called from `temp-buffer-show-hook'."
3959 (when (eq major-mode 'completion-list-mode)
3960 (toggle-read-only 1)))
3962 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
3964 (defvar completion-setup-hook nil
3965 "Normal hook run at the end of setting up a completion list buffer.
3966 When this hook is run, the current buffer is the one in which the
3967 command to display the completion list buffer was run.
3968 The completion list buffer is available as the value of `standard-output'.")
3970 ;; This function goes in completion-setup-hook, so that it is called
3971 ;; after the text of the completion list buffer is written.
3973 (defun completion-setup-function ()
3974 (save-excursion
3975 (let ((mainbuf (current-buffer))
3976 (mbuf-contents (minibuffer-contents)))
3977 ;; When reading a file name in the minibuffer,
3978 ;; set default-directory in the minibuffer
3979 ;; so it will get copied into the completion list buffer.
3980 (if minibuffer-completing-file-name
3981 (with-current-buffer mainbuf
3982 (setq default-directory (file-name-directory mbuf-contents))))
3983 (set-buffer standard-output)
3984 (completion-list-mode)
3985 (make-local-variable 'completion-reference-buffer)
3986 (setq completion-reference-buffer mainbuf)
3987 (if minibuffer-completing-file-name
3988 ;; For file name completion,
3989 ;; use the number of chars before the start of the
3990 ;; last file name component.
3991 (setq completion-base-size
3992 (save-excursion
3993 (set-buffer mainbuf)
3994 (goto-char (point-max))
3995 (skip-chars-backward "^/")
3996 (- (point) (minibuffer-prompt-end))))
3997 ;; Otherwise, in minibuffer, the whole input is being completed.
3998 (save-match-data
3999 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
4000 (buffer-name mainbuf))
4001 (setq completion-base-size 0))))
4002 (goto-char (point-min))
4003 (if (display-mouse-p)
4004 (insert (substitute-command-keys
4005 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4006 (insert (substitute-command-keys
4007 "In this buffer, type \\[choose-completion] to \
4008 select the completion near point.\n\n")))))
4010 (add-hook 'completion-setup-hook 'completion-setup-function)
4012 (define-key minibuffer-local-completion-map [prior]
4013 'switch-to-completions)
4014 (define-key minibuffer-local-must-match-map [prior]
4015 'switch-to-completions)
4016 (define-key minibuffer-local-completion-map "\M-v"
4017 'switch-to-completions)
4018 (define-key minibuffer-local-must-match-map "\M-v"
4019 'switch-to-completions)
4021 (defun switch-to-completions ()
4022 "Select the completion list window."
4023 (interactive)
4024 ;; Make sure we have a completions window.
4025 (or (get-buffer-window "*Completions*")
4026 (minibuffer-completion-help))
4027 (let ((window (get-buffer-window "*Completions*")))
4028 (when window
4029 (select-window window)
4030 (goto-char (point-min))
4031 (search-forward "\n\n")
4032 (forward-line 1))))
4034 ;; Support keyboard commands to turn on various modifiers.
4036 ;; These functions -- which are not commands -- each add one modifier
4037 ;; to the following event.
4039 (defun event-apply-alt-modifier (ignore-prompt)
4040 "Add the Alt modifier to the following event.
4041 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
4042 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
4043 (defun event-apply-super-modifier (ignore-prompt)
4044 "Add the Super modifier to the following event.
4045 For example, type \\[event-apply-super-modifier] & to enter Super-&."
4046 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
4047 (defun event-apply-hyper-modifier (ignore-prompt)
4048 "Add the Hyper modifier to the following event.
4049 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
4050 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
4051 (defun event-apply-shift-modifier (ignore-prompt)
4052 "Add the Shift modifier to the following event.
4053 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
4054 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
4055 (defun event-apply-control-modifier (ignore-prompt)
4056 "Add the Ctrl modifier to the following event.
4057 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
4058 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
4059 (defun event-apply-meta-modifier (ignore-prompt)
4060 "Add the Meta modifier to the following event.
4061 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
4062 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
4064 (defun event-apply-modifier (event symbol lshiftby prefix)
4065 "Apply a modifier flag to event EVENT.
4066 SYMBOL is the name of this modifier, as a symbol.
4067 LSHIFTBY is the numeric value of this modifier, in keyboard events.
4068 PREFIX is the string that represents this modifier in an event type symbol."
4069 (if (numberp event)
4070 (cond ((eq symbol 'control)
4071 (if (and (<= (downcase event) ?z)
4072 (>= (downcase event) ?a))
4073 (- (downcase event) ?a -1)
4074 (if (and (<= (downcase event) ?Z)
4075 (>= (downcase event) ?A))
4076 (- (downcase event) ?A -1)
4077 (logior (lsh 1 lshiftby) event))))
4078 ((eq symbol 'shift)
4079 (if (and (<= (downcase event) ?z)
4080 (>= (downcase event) ?a))
4081 (upcase event)
4082 (logior (lsh 1 lshiftby) event)))
4084 (logior (lsh 1 lshiftby) event)))
4085 (if (memq symbol (event-modifiers event))
4086 event
4087 (let ((event-type (if (symbolp event) event (car event))))
4088 (setq event-type (intern (concat prefix (symbol-name event-type))))
4089 (if (symbolp event)
4090 event-type
4091 (cons event-type (cdr event)))))))
4093 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4094 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4095 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4096 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4097 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4098 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4100 ;;;; Keypad support.
4102 ;;; Make the keypad keys act like ordinary typing keys. If people add
4103 ;;; bindings for the function key symbols, then those bindings will
4104 ;;; override these, so this shouldn't interfere with any existing
4105 ;;; bindings.
4107 ;; Also tell read-char how to handle these keys.
4108 (mapc
4109 (lambda (keypad-normal)
4110 (let ((keypad (nth 0 keypad-normal))
4111 (normal (nth 1 keypad-normal)))
4112 (put keypad 'ascii-character normal)
4113 (define-key function-key-map (vector keypad) (vector normal))))
4114 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4115 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4116 (kp-space ?\ )
4117 (kp-tab ?\t)
4118 (kp-enter ?\r)
4119 (kp-multiply ?*)
4120 (kp-add ?+)
4121 (kp-separator ?,)
4122 (kp-subtract ?-)
4123 (kp-decimal ?.)
4124 (kp-divide ?/)
4125 (kp-equal ?=)))
4127 ;;;;
4128 ;;;; forking a twin copy of a buffer.
4129 ;;;;
4131 (defvar clone-buffer-hook nil
4132 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4134 (defun clone-process (process &optional newname)
4135 "Create a twin copy of PROCESS.
4136 If NEWNAME is nil, it defaults to PROCESS' name;
4137 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4138 If PROCESS is associated with a buffer, the new process will be associated
4139 with the current buffer instead.
4140 Returns nil if PROCESS has already terminated."
4141 (setq newname (or newname (process-name process)))
4142 (if (string-match "<[0-9]+>\\'" newname)
4143 (setq newname (substring newname 0 (match-beginning 0))))
4144 (when (memq (process-status process) '(run stop open))
4145 (let* ((process-connection-type (process-tty-name process))
4146 (new-process
4147 (if (memq (process-status process) '(open))
4148 (let ((args (process-contact process t)))
4149 (setq args (plist-put args :name newname))
4150 (setq args (plist-put args :buffer
4151 (if (process-buffer process)
4152 (current-buffer))))
4153 (apply 'make-network-process args))
4154 (apply 'start-process newname
4155 (if (process-buffer process) (current-buffer))
4156 (process-command process)))))
4157 (set-process-query-on-exit-flag
4158 new-process (process-query-on-exit-flag process))
4159 (set-process-inherit-coding-system-flag
4160 new-process (process-inherit-coding-system-flag process))
4161 (set-process-filter new-process (process-filter process))
4162 (set-process-sentinel new-process (process-sentinel process))
4163 (set-process-plist new-process (copy-sequence (process-plist process)))
4164 new-process)))
4166 ;; things to maybe add (currently partly covered by `funcall mode'):
4167 ;; - syntax-table
4168 ;; - overlays
4169 (defun clone-buffer (&optional newname display-flag)
4170 "Create a twin copy of the current buffer.
4171 If NEWNAME is nil, it defaults to the current buffer's name;
4172 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4174 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
4175 This runs the normal hook `clone-buffer-hook' in the new buffer
4176 after it has been set up properly in other respects."
4177 (interactive
4178 (progn
4179 (if buffer-file-name
4180 (error "Cannot clone a file-visiting buffer"))
4181 (if (get major-mode 'no-clone)
4182 (error "Cannot clone a buffer in %s mode" mode-name))
4183 (list (if current-prefix-arg (read-string "Name: "))
4184 t)))
4185 (if buffer-file-name
4186 (error "Cannot clone a file-visiting buffer"))
4187 (if (get major-mode 'no-clone)
4188 (error "Cannot clone a buffer in %s mode" mode-name))
4189 (setq newname (or newname (buffer-name)))
4190 (if (string-match "<[0-9]+>\\'" newname)
4191 (setq newname (substring newname 0 (match-beginning 0))))
4192 (let ((buf (current-buffer))
4193 (ptmin (point-min))
4194 (ptmax (point-max))
4195 (pt (point))
4196 (mk (if mark-active (mark t)))
4197 (modified (buffer-modified-p))
4198 (mode major-mode)
4199 (lvars (buffer-local-variables))
4200 (process (get-buffer-process (current-buffer)))
4201 (new (generate-new-buffer (or newname (buffer-name)))))
4202 (save-restriction
4203 (widen)
4204 (with-current-buffer new
4205 (insert-buffer-substring buf)))
4206 (with-current-buffer new
4207 (narrow-to-region ptmin ptmax)
4208 (goto-char pt)
4209 (if mk (set-mark mk))
4210 (set-buffer-modified-p modified)
4212 ;; Clone the old buffer's process, if any.
4213 (when process (clone-process process))
4215 ;; Now set up the major mode.
4216 (funcall mode)
4218 ;; Set up other local variables.
4219 (mapcar (lambda (v)
4220 (condition-case () ;in case var is read-only
4221 (if (symbolp v)
4222 (makunbound v)
4223 (set (make-local-variable (car v)) (cdr v)))
4224 (error nil)))
4225 lvars)
4227 ;; Run any hooks (typically set up by the major mode
4228 ;; for cloning to work properly).
4229 (run-hooks 'clone-buffer-hook))
4230 (if display-flag (pop-to-buffer new))
4231 new))
4234 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4235 "Create an indirect buffer that is a twin copy of the current buffer.
4237 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4238 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4239 or if not called with a prefix arg, NEWNAME defaults to the current
4240 buffer's name. The name is modified by adding a `<N>' suffix to it
4241 or by incrementing the N in an existing suffix.
4243 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4244 This is always done when called interactively.
4246 Optional last arg NORECORD non-nil means do not put this buffer at the
4247 front of the list of recently selected ones."
4248 (interactive
4249 (progn
4250 (if (get major-mode 'no-clone-indirect)
4251 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4252 (list (if current-prefix-arg
4253 (read-string "BName of indirect buffer: "))
4254 t)))
4255 (if (get major-mode 'no-clone-indirect)
4256 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4257 (setq newname (or newname (buffer-name)))
4258 (if (string-match "<[0-9]+>\\'" newname)
4259 (setq newname (substring newname 0 (match-beginning 0))))
4260 (let* ((name (generate-new-buffer-name newname))
4261 (buffer (make-indirect-buffer (current-buffer) name t)))
4262 (when display-flag
4263 (pop-to-buffer buffer norecord))
4264 buffer))
4267 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4268 "Create an indirect buffer that is a twin copy of BUFFER.
4269 Select the new buffer in another window.
4270 Optional second arg NORECORD non-nil means do not put this buffer at
4271 the front of the list of recently selected ones."
4272 (interactive "bClone buffer in other window: ")
4273 (let ((pop-up-windows t))
4274 (set-buffer buffer)
4275 (clone-indirect-buffer nil t norecord)))
4277 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4279 ;;; Handling of Backspace and Delete keys.
4281 (defcustom normal-erase-is-backspace nil
4282 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4284 On window systems, the default value of this option is chosen
4285 according to the keyboard used. If the keyboard has both a Backspace
4286 key and a Delete key, and both are mapped to their usual meanings, the
4287 option's default value is set to t, so that Backspace can be used to
4288 delete backward, and Delete can be used to delete forward.
4290 If not running under a window system, customizing this option accomplishes
4291 a similar effect by mapping C-h, which is usually generated by the
4292 Backspace key, to DEL, and by mapping DEL to C-d via
4293 `keyboard-translate'. The former functionality of C-h is available on
4294 the F1 key. You should probably not use this setting if you don't
4295 have both Backspace, Delete and F1 keys.
4297 Setting this variable with setq doesn't take effect. Programmatically,
4298 call `normal-erase-is-backspace-mode' (which see) instead."
4299 :type 'boolean
4300 :group 'editing-basics
4301 :version "21.1"
4302 :set (lambda (symbol value)
4303 ;; The fboundp is because of a problem with :set when
4304 ;; dumping Emacs. It doesn't really matter.
4305 (if (fboundp 'normal-erase-is-backspace-mode)
4306 (normal-erase-is-backspace-mode (or value 0))
4307 (set-default symbol value))))
4310 (defun normal-erase-is-backspace-mode (&optional arg)
4311 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4313 With numeric arg, turn the mode on if and only if ARG is positive.
4315 On window systems, when this mode is on, Delete is mapped to C-d and
4316 Backspace is mapped to DEL; when this mode is off, both Delete and
4317 Backspace are mapped to DEL. (The remapping goes via
4318 `function-key-map', so binding Delete or Backspace in the global or
4319 local keymap will override that.)
4321 In addition, on window systems, the bindings of C-Delete, M-Delete,
4322 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4323 the global keymap in accordance with the functionality of Delete and
4324 Backspace. For example, if Delete is remapped to C-d, which deletes
4325 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4326 to DEL, which deletes backward, C-Delete is bound to
4327 `backward-kill-word'.
4329 If not running on a window system, a similar effect is accomplished by
4330 remapping C-h (normally produced by the Backspace key) and DEL via
4331 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4332 to C-d; if it's off, the keys are not remapped.
4334 When not running on a window system, and this mode is turned on, the
4335 former functionality of C-h is available on the F1 key. You should
4336 probably not turn on this mode on a text-only terminal if you don't
4337 have both Backspace, Delete and F1 keys.
4339 See also `normal-erase-is-backspace'."
4340 (interactive "P")
4341 (setq normal-erase-is-backspace
4342 (if arg
4343 (> (prefix-numeric-value arg) 0)
4344 (not normal-erase-is-backspace)))
4346 (cond ((or (memq window-system '(x w32 mac pc))
4347 (memq system-type '(ms-dos windows-nt)))
4348 (let ((bindings
4349 `(([C-delete] [C-backspace])
4350 ([M-delete] [M-backspace])
4351 ([C-M-delete] [C-M-backspace])
4352 (,esc-map
4353 [C-delete] [C-backspace])))
4354 (old-state (lookup-key function-key-map [delete])))
4356 (if normal-erase-is-backspace
4357 (progn
4358 (define-key function-key-map [delete] [?\C-d])
4359 (define-key function-key-map [kp-delete] [?\C-d])
4360 (define-key function-key-map [backspace] [?\C-?]))
4361 (define-key function-key-map [delete] [?\C-?])
4362 (define-key function-key-map [kp-delete] [?\C-?])
4363 (define-key function-key-map [backspace] [?\C-?]))
4365 ;; Maybe swap bindings of C-delete and C-backspace, etc.
4366 (unless (equal old-state (lookup-key function-key-map [delete]))
4367 (dolist (binding bindings)
4368 (let ((map global-map))
4369 (when (keymapp (car binding))
4370 (setq map (car binding) binding (cdr binding)))
4371 (let* ((key1 (nth 0 binding))
4372 (key2 (nth 1 binding))
4373 (binding1 (lookup-key map key1))
4374 (binding2 (lookup-key map key2)))
4375 (define-key map key1 binding2)
4376 (define-key map key2 binding1)))))))
4378 (if normal-erase-is-backspace
4379 (progn
4380 (keyboard-translate ?\C-h ?\C-?)
4381 (keyboard-translate ?\C-? ?\C-d))
4382 (keyboard-translate ?\C-h ?\C-h)
4383 (keyboard-translate ?\C-? ?\C-?))))
4385 (run-hooks 'normal-erase-is-backspace-hook)
4386 (if (interactive-p)
4387 (message "Delete key deletes %s"
4388 (if normal-erase-is-backspace "forward" "backward"))))
4391 ;; Minibuffer prompt stuff.
4393 ;(defun minibuffer-prompt-modification (start end)
4394 ; (error "You cannot modify the prompt"))
4397 ;(defun minibuffer-prompt-insertion (start end)
4398 ; (let ((inhibit-modification-hooks t))
4399 ; (delete-region start end)
4400 ; ;; Discard undo information for the text insertion itself
4401 ; ;; and for the text deletion.above.
4402 ; (when (consp buffer-undo-list)
4403 ; (setq buffer-undo-list (cddr buffer-undo-list)))
4404 ; (message "You cannot modify the prompt")))
4407 ;(setq minibuffer-prompt-properties
4408 ; (list 'modification-hooks '(minibuffer-prompt-modification)
4409 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
4412 (provide 'simple)
4413 ;;; simple.el ends here