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