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