]> code.delx.au - gnu-emacs/blob - lisp/simple.el
(f90-mode-abbrev-table): Mark all the predefined abbrevs as "system"
[gnu-emacs] / lisp / simple.el
1 ;;; simple.el --- basic editing commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99, 2000, 2001
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 A value of 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 A value of 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 (defun line-move-invisible (pos)
2534 "Return non-nil if the character after POS is currently invisible."
2535 (let ((prop
2536 (get-char-property pos 'invisible)))
2537 (if (eq buffer-invisibility-spec t)
2538 prop
2539 (or (memq prop buffer-invisibility-spec)
2540 (assq prop buffer-invisibility-spec)))))
2541
2542 ;; This is the guts of next-line and previous-line.
2543 ;; Arg says how many lines to move.
2544 (defun line-move (arg)
2545 ;; Don't run any point-motion hooks, and disregard intangibility,
2546 ;; for intermediate positions.
2547 (let ((inhibit-point-motion-hooks t)
2548 (opoint (point))
2549 new line-end line-beg)
2550 (unwind-protect
2551 (progn
2552 (if (not (or (eq last-command 'next-line)
2553 (eq last-command 'previous-line)))
2554 (setq temporary-goal-column
2555 (if (and track-eol (eolp)
2556 ;; Don't count beg of empty line as end of line
2557 ;; unless we just did explicit end-of-line.
2558 (or (not (bolp)) (eq last-command 'end-of-line)))
2559 9999
2560 (current-column))))
2561 (if (and (not (integerp selective-display))
2562 (not line-move-ignore-invisible))
2563 ;; Use just newline characters.
2564 (or (if (> arg 0)
2565 (progn (if (> arg 1) (forward-line (1- arg)))
2566 ;; This way of moving forward ARG lines
2567 ;; verifies that we have a newline after the last one.
2568 ;; It doesn't get confused by intangible text.
2569 (end-of-line)
2570 (zerop (forward-line 1)))
2571 (and (zerop (forward-line arg))
2572 (bolp)))
2573 (signal (if (< arg 0)
2574 'beginning-of-buffer
2575 a 'end-of-buffer)
2576 nil))
2577 ;; Move by arg lines, but ignore invisible ones.
2578 (while (> arg 0)
2579 ;; If the following character is currently invisible,
2580 ;; skip all characters with that same `invisible' property value.
2581 (while (and (not (eobp)) (line-move-invisible (point)))
2582 (goto-char (next-char-property-change (point))))
2583 ;; Now move a line.
2584 (end-of-line)
2585 (and (zerop (vertical-motion 1))
2586 (signal 'end-of-buffer nil))
2587 (setq arg (1- arg)))
2588 (while (< arg 0)
2589 (beginning-of-line)
2590 (and (zerop (vertical-motion -1))
2591 (signal 'beginning-of-buffer nil))
2592 (setq arg (1+ arg))
2593 (while (and (not (bobp)) (line-move-invisible (1- (point))))
2594 (goto-char (previous-char-property-change (point)))))))
2595
2596 (line-move-finish (or goal-column temporary-goal-column) opoint)))
2597 nil)
2598
2599 (defun line-move-finish (column opoint)
2600 (let ((repeat t))
2601 (while repeat
2602 ;; Set REPEAT to t to repeat the whole thing.
2603 (setq repeat nil)
2604
2605 ;; Move to the desired column.
2606 (line-move-to-column column)
2607
2608 (let ((new (point))
2609 (line-beg (save-excursion (beginning-of-line) (point)))
2610 (line-end (save-excursion (end-of-line) (point))))
2611
2612 ;; Process intangibility within a line.
2613 ;; Move to the chosen destination position from above,
2614 ;; with intangibility processing enabled.
2615
2616 (goto-char (point-min))
2617 (let ((inhibit-point-motion-hooks nil))
2618 (goto-char new)
2619
2620 ;; If intangibility moves us to a different (later) place
2621 ;; in the same line, use that as the destination.
2622 (if (<= (point) line-end)
2623 (setq new (point))))
2624
2625 ;; Now move to the updated destination, processing fields
2626 ;; as well as intangibility.
2627 (goto-char opoint)
2628 (let ((inhibit-point-motion-hooks nil))
2629 (goto-char
2630 (constrain-to-field new opoint nil t
2631 'inhibit-line-move-field-capture)))
2632
2633 ;; If intangibility processing moved us to a different line,
2634 ;; retry everything within that new line.
2635 (when (or (< (point) line-beg) (> (point) line-end))
2636 ;; Repeat the intangibility and field processing.
2637 (setq repeat t))))))
2638
2639 (defun line-move-to-column (col)
2640 "Try to find column COL, considering invisibility.
2641 This function works only in certain cases,
2642 because what we really need is for `move-to-column'
2643 and `current-column' to be able to ignore invisible text."
2644 (move-to-column col)
2645
2646 (when (and line-move-ignore-invisible
2647 (not (bolp)) (line-move-invisible (1- (point))))
2648 (let ((normal-location (point))
2649 (normal-column (current-column)))
2650 ;; If the following character is currently invisible,
2651 ;; skip all characters with that same `invisible' property value.
2652 (while (and (not (eobp))
2653 (line-move-invisible (point)))
2654 (goto-char (next-char-property-change (point))))
2655 ;; Have we advanced to a larger column position?
2656 (if (> (current-column) normal-column)
2657 ;; We have made some progress towards the desired column.
2658 ;; See if we can make any further progress.
2659 (line-move-to-column (+ (current-column) (- col normal-column)))
2660 ;; Otherwise, go to the place we originally found
2661 ;; and move back over invisible text.
2662 ;; that will get us to the same place on the screen
2663 ;; but with a more reasonable buffer position.
2664 (goto-char normal-location)
2665 (let ((line-beg (save-excursion (beginning-of-line) (point))))
2666 (while (and (not (bolp)) (line-move-invisible (1- (point))))
2667 (goto-char (previous-char-property-change (point) line-beg))))))))
2668
2669 ;;; Many people have said they rarely use this feature, and often type
2670 ;;; it by accident. Maybe it shouldn't even be on a key.
2671 (put 'set-goal-column 'disabled t)
2672
2673 (defun set-goal-column (arg)
2674 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2675 Those commands will move to this position in the line moved to
2676 rather than trying to keep the same horizontal position.
2677 With a non-nil argument, clears out the goal column
2678 so that \\[next-line] and \\[previous-line] resume vertical motion.
2679 The goal column is stored in the variable `goal-column'."
2680 (interactive "P")
2681 (if arg
2682 (progn
2683 (setq goal-column nil)
2684 (message "No goal column"))
2685 (setq goal-column (current-column))
2686 (message (substitute-command-keys
2687 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2688 goal-column))
2689 nil)
2690
2691
2692 (defun scroll-other-window-down (lines)
2693 "Scroll the \"other window\" down.
2694 For more details, see the documentation for `scroll-other-window'."
2695 (interactive "P")
2696 (scroll-other-window
2697 ;; Just invert the argument's meaning.
2698 ;; We can do that without knowing which window it will be.
2699 (if (eq lines '-) nil
2700 (if (null lines) '-
2701 (- (prefix-numeric-value lines))))))
2702 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2703
2704 (defun beginning-of-buffer-other-window (arg)
2705 "Move point to the beginning of the buffer in the other window.
2706 Leave mark at previous position.
2707 With arg N, put point N/10 of the way from the true beginning."
2708 (interactive "P")
2709 (let ((orig-window (selected-window))
2710 (window (other-window-for-scrolling)))
2711 ;; We use unwind-protect rather than save-window-excursion
2712 ;; because the latter would preserve the things we want to change.
2713 (unwind-protect
2714 (progn
2715 (select-window window)
2716 ;; Set point and mark in that window's buffer.
2717 (beginning-of-buffer arg)
2718 ;; Set point accordingly.
2719 (recenter '(t)))
2720 (select-window orig-window))))
2721
2722 (defun end-of-buffer-other-window (arg)
2723 "Move point to the end of the buffer in the other window.
2724 Leave mark at previous position.
2725 With arg N, put point N/10 of the way from the true end."
2726 (interactive "P")
2727 ;; See beginning-of-buffer-other-window for comments.
2728 (let ((orig-window (selected-window))
2729 (window (other-window-for-scrolling)))
2730 (unwind-protect
2731 (progn
2732 (select-window window)
2733 (end-of-buffer arg)
2734 (recenter '(t)))
2735 (select-window orig-window))))
2736
2737 (defun transpose-chars (arg)
2738 "Interchange characters around point, moving forward one character.
2739 With prefix arg ARG, effect is to take character before point
2740 and drag it forward past ARG other characters (backward if ARG negative).
2741 If no argument and at end of line, the previous two chars are exchanged."
2742 (interactive "*P")
2743 (and (null arg) (eolp) (forward-char -1))
2744 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2745
2746 (defun transpose-words (arg)
2747 "Interchange words around point, leaving point at end of them.
2748 With prefix arg ARG, effect is to take word before or around point
2749 and drag it forward past ARG other words (backward if ARG negative).
2750 If ARG is zero, the words around or after point and around or after mark
2751 are interchanged."
2752 (interactive "*p")
2753 (transpose-subr 'forward-word arg))
2754
2755 (defun transpose-sexps (arg)
2756 "Like \\[transpose-words] but applies to sexps.
2757 Does not work on a sexp that point is in the middle of
2758 if it is a list or string."
2759 (interactive "*p")
2760 (transpose-subr 'forward-sexp arg))
2761
2762 (defun transpose-lines (arg)
2763 "Exchange current line and previous line, leaving point after both.
2764 With argument ARG, takes previous line and moves it past ARG lines.
2765 With argument 0, interchanges line point is in with line mark is in."
2766 (interactive "*p")
2767 (transpose-subr (function
2768 (lambda (arg)
2769 (if (> arg 0)
2770 (progn
2771 ;; Move forward over ARG lines,
2772 ;; but create newlines if necessary.
2773 (setq arg (forward-line arg))
2774 (if (/= (preceding-char) ?\n)
2775 (setq arg (1+ arg)))
2776 (if (> arg 0)
2777 (newline arg)))
2778 (forward-line arg))))
2779 arg))
2780
2781 (defun transpose-subr (mover arg &optional special)
2782 (let ((aux (if special mover
2783 (lambda (x)
2784 (cons (progn (funcall mover x) (point))
2785 (progn (funcall mover (- x)) (point))))))
2786 pos1 pos2)
2787 (cond
2788 ((= arg 0)
2789 (save-excursion
2790 (setq pos1 (funcall aux 1))
2791 (goto-char (mark))
2792 (setq pos2 (funcall aux 1))
2793 (transpose-subr-1 pos1 pos2))
2794 (exchange-point-and-mark))
2795 ((> arg 0)
2796 (setq pos1 (funcall aux -1))
2797 (setq pos2 (funcall aux arg))
2798 (transpose-subr-1 pos1 pos2)
2799 (goto-char (car pos2)))
2800 (t
2801 (setq pos1 (funcall aux -1))
2802 (goto-char (car pos1))
2803 (setq pos2 (funcall aux arg))
2804 (transpose-subr-1 pos1 pos2)))))
2805
2806 (defun transpose-subr-1 (pos1 pos2)
2807 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
2808 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
2809 (when (> (car pos1) (car pos2))
2810 (let ((swap pos1))
2811 (setq pos1 pos2 pos2 swap)))
2812 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
2813 (let ((word2 (delete-and-extract-region (car pos2) (cdr pos2))))
2814 (goto-char (car pos2))
2815 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
2816 (goto-char (car pos1))
2817 (insert word2)))
2818
2819 (defun backward-word (arg)
2820 "Move backward until encountering the beginning of a word.
2821 With argument, do this that many times."
2822 (interactive "p")
2823 (forward-word (- arg)))
2824
2825 (defun mark-word (arg)
2826 "Set mark arg words away from point."
2827 (interactive "p")
2828 (push-mark
2829 (save-excursion
2830 (forward-word arg)
2831 (point))
2832 nil t))
2833
2834 (defun kill-word (arg)
2835 "Kill characters forward until encountering the end of a word.
2836 With argument, do this that many times."
2837 (interactive "p")
2838 (kill-region (point) (progn (forward-word arg) (point))))
2839
2840 (defun backward-kill-word (arg)
2841 "Kill characters backward until encountering the end of a word.
2842 With argument, do this that many times."
2843 (interactive "p")
2844 (kill-word (- arg)))
2845
2846 (defun current-word (&optional strict)
2847 "Return the word point is on (or a nearby word) as a string.
2848 If optional arg STRICT is non-nil, return nil unless point is within
2849 or adjacent to a word."
2850 (save-excursion
2851 (let ((oldpoint (point)) (start (point)) (end (point)))
2852 (skip-syntax-backward "w_") (setq start (point))
2853 (goto-char oldpoint)
2854 (skip-syntax-forward "w_") (setq end (point))
2855 (if (and (eq start oldpoint) (eq end oldpoint))
2856 ;; Point is neither within nor adjacent to a word.
2857 (and (not strict)
2858 (progn
2859 ;; Look for preceding word in same line.
2860 (skip-syntax-backward "^w_"
2861 (save-excursion (beginning-of-line)
2862 (point)))
2863 (if (bolp)
2864 ;; No preceding word in same line.
2865 ;; Look for following word in same line.
2866 (progn
2867 (skip-syntax-forward "^w_"
2868 (save-excursion (end-of-line)
2869 (point)))
2870 (setq start (point))
2871 (skip-syntax-forward "w_")
2872 (setq end (point)))
2873 (setq end (point))
2874 (skip-syntax-backward "w_")
2875 (setq start (point)))
2876 (buffer-substring-no-properties start end)))
2877 (buffer-substring-no-properties start end)))))
2878
2879 (defcustom fill-prefix nil
2880 "*String for filling to insert at front of new line, or nil for none."
2881 :type '(choice (const :tag "None" nil)
2882 string)
2883 :group 'fill)
2884 (make-variable-buffer-local 'fill-prefix)
2885
2886 (defcustom auto-fill-inhibit-regexp nil
2887 "*Regexp to match lines which should not be auto-filled."
2888 :type '(choice (const :tag "None" nil)
2889 regexp)
2890 :group 'fill)
2891
2892 (defvar comment-line-break-function 'comment-indent-new-line
2893 "*Mode-specific function which line breaks and continues a comment.
2894
2895 This function is only called during auto-filling of a comment section.
2896 The function should take a single optional argument, which is a flag
2897 indicating whether it should use soft newlines.
2898
2899 Setting this variable automatically makes it local to the current buffer.")
2900
2901 ;; This function is used as the auto-fill-function of a buffer
2902 ;; when Auto-Fill mode is enabled.
2903 ;; It returns t if it really did any work.
2904 ;; (Actually some major modes use a different auto-fill function,
2905 ;; but this one is the default one.)
2906 (defun do-auto-fill ()
2907 (let (fc justify bol give-up
2908 (fill-prefix fill-prefix))
2909 (if (or (not (setq justify (current-justification)))
2910 (null (setq fc (current-fill-column)))
2911 (and (eq justify 'left)
2912 (<= (current-column) fc))
2913 (save-excursion (beginning-of-line)
2914 (setq bol (point))
2915 (and auto-fill-inhibit-regexp
2916 (looking-at auto-fill-inhibit-regexp))))
2917 nil ;; Auto-filling not required
2918 (if (memq justify '(full center right))
2919 (save-excursion (unjustify-current-line)))
2920
2921 ;; Choose a fill-prefix automatically.
2922 (when (and adaptive-fill-mode
2923 (or (null fill-prefix) (string= fill-prefix "")))
2924 (let ((prefix
2925 (fill-context-prefix
2926 (save-excursion (backward-paragraph 1) (point))
2927 (save-excursion (forward-paragraph 1) (point)))))
2928 (and prefix (not (equal prefix ""))
2929 ;; Use auto-indentation rather than a guessed empty prefix.
2930 (not (and fill-indent-according-to-mode
2931 (string-match "[ \t]*" prefix)))
2932 (setq fill-prefix prefix))))
2933
2934 (while (and (not give-up) (> (current-column) fc))
2935 ;; Determine where to split the line.
2936 (let* (after-prefix
2937 (fill-point
2938 (let ((opoint (point))
2939 bounce
2940 (first t))
2941 (save-excursion
2942 (beginning-of-line)
2943 (setq after-prefix (point))
2944 (and fill-prefix
2945 (looking-at (regexp-quote fill-prefix))
2946 (setq after-prefix (match-end 0)))
2947 (move-to-column (1+ fc))
2948 ;; Move back to the point where we can break the line.
2949 ;; We break the line between word or
2950 ;; after/before the character which has character
2951 ;; category `|'. We search space, \c| followed by
2952 ;; a character, or \c| following a character. If
2953 ;; not found, place the point at beginning of line.
2954 (while (or first
2955 (and (not (bobp))
2956 (not bounce)
2957 (fill-nobreak-p)))
2958 (setq first nil)
2959 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
2960 ;; If we find nowhere on the line to break it,
2961 ;; break after one word. Set bounce to t
2962 ;; so we will not keep going in this while loop.
2963 (if (<= (point) after-prefix)
2964 (progn
2965 (goto-char after-prefix)
2966 (re-search-forward "[ \t]" opoint t)
2967 (setq bounce t))
2968 (if (looking-at "[ \t]")
2969 ;; Break the line at word boundary.
2970 (skip-chars-backward " \t")
2971 ;; Break the line after/before \c|.
2972 (forward-char 1))))
2973 (if enable-multibyte-characters
2974 ;; If we are going to break the line after or
2975 ;; before a non-ascii character, we may have
2976 ;; to run a special function for the charset
2977 ;; of the character to find the correct break
2978 ;; point.
2979 (if (not (and (eq (charset-after (1- (point))) 'ascii)
2980 (eq (charset-after (point)) 'ascii)))
2981 (fill-find-break-point after-prefix)))
2982
2983 ;; Let fill-point be set to the place where we end up.
2984 ;; But move back before any whitespace here.
2985 (skip-chars-backward " \t")
2986 (point)))))
2987
2988 ;; See whether the place we found is any good.
2989 (if (save-excursion
2990 (goto-char fill-point)
2991 (and (not (bolp))
2992 ;; There is no use breaking at end of line.
2993 (not (save-excursion (skip-chars-forward " ") (eolp)))
2994 ;; It is futile to split at the end of the prefix
2995 ;; since we would just insert the prefix again.
2996 (not (and after-prefix (<= (point) after-prefix)))
2997 ;; Don't split right after a comment starter
2998 ;; since we would just make another comment starter.
2999 (not (and comment-start-skip
3000 (let ((limit (point)))
3001 (beginning-of-line)
3002 (and (re-search-forward comment-start-skip
3003 limit t)
3004 (eq (point) limit)))))))
3005 ;; Ok, we have a useful place to break the line. Do it.
3006 (let ((prev-column (current-column)))
3007 ;; If point is at the fill-point, do not `save-excursion'.
3008 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3009 ;; point will end up before it rather than after it.
3010 (if (save-excursion
3011 (skip-chars-backward " \t")
3012 (= (point) fill-point))
3013 (funcall comment-line-break-function t)
3014 (save-excursion
3015 (goto-char fill-point)
3016 (funcall comment-line-break-function t)))
3017 ;; Now do justification, if required
3018 (if (not (eq justify 'left))
3019 (save-excursion
3020 (end-of-line 0)
3021 (justify-current-line justify nil t)))
3022 ;; If making the new line didn't reduce the hpos of
3023 ;; the end of the line, then give up now;
3024 ;; trying again will not help.
3025 (if (>= (current-column) prev-column)
3026 (setq give-up t)))
3027 ;; No good place to break => stop trying.
3028 (setq give-up t))))
3029 ;; Justify last line.
3030 (justify-current-line justify t t)
3031 t)))
3032
3033 (defvar normal-auto-fill-function 'do-auto-fill
3034 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3035 Some major modes set this.")
3036
3037 (defun auto-fill-mode (&optional arg)
3038 "Toggle Auto Fill mode.
3039 With arg, turn Auto Fill mode on if and only if arg is positive.
3040 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3041 automatically breaks the line at a previous space.
3042
3043 The value of `normal-auto-fill-function' specifies the function to use
3044 for `auto-fill-function' when turning Auto Fill mode on."
3045 (interactive "P")
3046 (prog1 (setq auto-fill-function
3047 (if (if (null arg)
3048 (not auto-fill-function)
3049 (> (prefix-numeric-value arg) 0))
3050 normal-auto-fill-function
3051 nil))
3052 (force-mode-line-update)))
3053
3054 ;; This holds a document string used to document auto-fill-mode.
3055 (defun auto-fill-function ()
3056 "Automatically break line at a previous space, in insertion of text."
3057 nil)
3058
3059 (defun turn-on-auto-fill ()
3060 "Unconditionally turn on Auto Fill mode."
3061 (auto-fill-mode 1))
3062
3063 (defun turn-off-auto-fill ()
3064 "Unconditionally turn off Auto Fill mode."
3065 (auto-fill-mode -1))
3066
3067 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3068
3069 (defun set-fill-column (arg)
3070 "Set `fill-column' to specified argument.
3071 Use \\[universal-argument] followed by a number to specify a column.
3072 Just \\[universal-argument] as argument means to use the current column."
3073 (interactive "P")
3074 (if (consp arg)
3075 (setq arg (current-column)))
3076 (if (not (integerp arg))
3077 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3078 (error "set-fill-column requires an explicit argument")
3079 (message "Fill column set to %d (was %d)" arg fill-column)
3080 (setq fill-column arg)))
3081
3082 (defun set-selective-display (arg)
3083 "Set `selective-display' to ARG; clear it if no arg.
3084 When the value of `selective-display' is a number > 0,
3085 lines whose indentation is >= that value are not displayed.
3086 The variable `selective-display' has a separate value for each buffer."
3087 (interactive "P")
3088 (if (eq selective-display t)
3089 (error "selective-display already in use for marked lines"))
3090 (let ((current-vpos
3091 (save-restriction
3092 (narrow-to-region (point-min) (point))
3093 (goto-char (window-start))
3094 (vertical-motion (window-height)))))
3095 (setq selective-display
3096 (and arg (prefix-numeric-value arg)))
3097 (recenter current-vpos))
3098 (set-window-start (selected-window) (window-start (selected-window)))
3099 (princ "selective-display set to " t)
3100 (prin1 selective-display t)
3101 (princ "." t))
3102
3103 (defvar overwrite-mode-textual " Ovwrt"
3104 "The string displayed in the mode line when in overwrite mode.")
3105 (defvar overwrite-mode-binary " Bin Ovwrt"
3106 "The string displayed in the mode line when in binary overwrite mode.")
3107
3108 (defun overwrite-mode (arg)
3109 "Toggle overwrite mode.
3110 With arg, turn overwrite mode on iff arg is positive.
3111 In overwrite mode, printing characters typed in replace existing text
3112 on a one-for-one basis, rather than pushing it to the right. At the
3113 end of a line, such characters extend the line. Before a tab,
3114 such characters insert until the tab is filled in.
3115 \\[quoted-insert] still inserts characters in overwrite mode; this
3116 is supposed to make it easier to insert characters when necessary."
3117 (interactive "P")
3118 (setq overwrite-mode
3119 (if (if (null arg) (not overwrite-mode)
3120 (> (prefix-numeric-value arg) 0))
3121 'overwrite-mode-textual))
3122 (force-mode-line-update))
3123
3124 (defun binary-overwrite-mode (arg)
3125 "Toggle binary overwrite mode.
3126 With arg, turn binary overwrite mode on iff arg is positive.
3127 In binary overwrite mode, printing characters typed in replace
3128 existing text. Newlines are not treated specially, so typing at the
3129 end of a line joins the line to the next, with the typed character
3130 between them. Typing before a tab character simply replaces the tab
3131 with the character typed.
3132 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3133 typing characters do.
3134
3135 Note that binary overwrite mode is not its own minor mode; it is a
3136 specialization of overwrite-mode, entered by setting the
3137 `overwrite-mode' variable to `overwrite-mode-binary'."
3138 (interactive "P")
3139 (setq overwrite-mode
3140 (if (if (null arg)
3141 (not (eq overwrite-mode 'overwrite-mode-binary))
3142 (> (prefix-numeric-value arg) 0))
3143 'overwrite-mode-binary))
3144 (force-mode-line-update))
3145
3146 (defcustom line-number-mode t
3147 "*Non-nil means display line number in mode line."
3148 :type 'boolean
3149 :group 'editing-basics)
3150
3151 (defun line-number-mode (arg)
3152 "Toggle Line Number mode.
3153 With arg, turn Line Number mode on iff arg is positive.
3154 When Line Number mode is enabled, the line number appears
3155 in the mode line.
3156
3157 Line numbers do not appear for very large buffers and buffers
3158 with very long lines; see variables `line-number-display-limit'
3159 and `line-number-display-limit-width'."
3160 (interactive "P")
3161 (setq line-number-mode
3162 (if (null arg) (not line-number-mode)
3163 (> (prefix-numeric-value arg) 0)))
3164 (force-mode-line-update))
3165
3166 (defcustom column-number-mode nil
3167 "*Non-nil means display column number in mode line."
3168 :type 'boolean
3169 :group 'editing-basics)
3170
3171 (defun column-number-mode (arg)
3172 "Toggle Column Number mode.
3173 With arg, turn Column Number mode on iff arg is positive.
3174 When Column Number mode is enabled, the column number appears
3175 in the mode line."
3176 (interactive "P")
3177 (setq column-number-mode
3178 (if (null arg) (not column-number-mode)
3179 (> (prefix-numeric-value arg) 0)))
3180 (force-mode-line-update))
3181
3182 (defgroup paren-blinking nil
3183 "Blinking matching of parens and expressions."
3184 :prefix "blink-matching-"
3185 :group 'paren-matching)
3186
3187 (defcustom blink-matching-paren t
3188 "*Non-nil means show matching open-paren when close-paren is inserted."
3189 :type 'boolean
3190 :group 'paren-blinking)
3191
3192 (defcustom blink-matching-paren-on-screen t
3193 "*Non-nil means show matching open-paren when it is on screen.
3194 If nil, means don't show it (but the open-paren can still be shown
3195 when it is off screen)."
3196 :type 'boolean
3197 :group 'paren-blinking)
3198
3199 (defcustom blink-matching-paren-distance (* 25 1024)
3200 "*If non-nil, is maximum distance to search for matching open-paren."
3201 :type 'integer
3202 :group 'paren-blinking)
3203
3204 (defcustom blink-matching-delay 1
3205 "*Time in seconds to delay after showing a matching paren."
3206 :type 'number
3207 :group 'paren-blinking)
3208
3209 (defcustom blink-matching-paren-dont-ignore-comments nil
3210 "*Non-nil means `blink-matching-paren' will not ignore comments."
3211 :type 'boolean
3212 :group 'paren-blinking)
3213
3214 (defun blink-matching-open ()
3215 "Move cursor momentarily to the beginning of the sexp before point."
3216 (interactive)
3217 (and (> (point) (1+ (point-min)))
3218 blink-matching-paren
3219 ;; Verify an even number of quoting characters precede the close.
3220 (= 1 (logand 1 (- (point)
3221 (save-excursion
3222 (forward-char -1)
3223 (skip-syntax-backward "/\\")
3224 (point)))))
3225 (let* ((oldpos (point))
3226 (blinkpos)
3227 (mismatch))
3228 (save-excursion
3229 (save-restriction
3230 (if blink-matching-paren-distance
3231 (narrow-to-region (max (point-min)
3232 (- (point) blink-matching-paren-distance))
3233 oldpos))
3234 (condition-case ()
3235 (let ((parse-sexp-ignore-comments
3236 (and parse-sexp-ignore-comments
3237 (not blink-matching-paren-dont-ignore-comments))))
3238 (setq blinkpos (scan-sexps oldpos -1)))
3239 (error nil)))
3240 (and blinkpos
3241 (/= (char-syntax (char-after blinkpos))
3242 ?\$)
3243 (setq mismatch
3244 (or (null (matching-paren (char-after blinkpos)))
3245 (/= (char-after (1- oldpos))
3246 (matching-paren (char-after blinkpos))))))
3247 (if mismatch (setq blinkpos nil))
3248 (if blinkpos
3249 ;; Don't log messages about paren matching.
3250 (let (message-log-max)
3251 (goto-char blinkpos)
3252 (if (pos-visible-in-window-p)
3253 (and blink-matching-paren-on-screen
3254 (sit-for blink-matching-delay))
3255 (goto-char blinkpos)
3256 (message
3257 "Matches %s"
3258 ;; Show what precedes the open in its line, if anything.
3259 (if (save-excursion
3260 (skip-chars-backward " \t")
3261 (not (bolp)))
3262 (buffer-substring (progn (beginning-of-line) (point))
3263 (1+ blinkpos))
3264 ;; Show what follows the open in its line, if anything.
3265 (if (save-excursion
3266 (forward-char 1)
3267 (skip-chars-forward " \t")
3268 (not (eolp)))
3269 (buffer-substring blinkpos
3270 (progn (end-of-line) (point)))
3271 ;; Otherwise show the previous nonblank line,
3272 ;; if there is one.
3273 (if (save-excursion
3274 (skip-chars-backward "\n \t")
3275 (not (bobp)))
3276 (concat
3277 (buffer-substring (progn
3278 (skip-chars-backward "\n \t")
3279 (beginning-of-line)
3280 (point))
3281 (progn (end-of-line)
3282 (skip-chars-backward " \t")
3283 (point)))
3284 ;; Replace the newline and other whitespace with `...'.
3285 "..."
3286 (buffer-substring blinkpos (1+ blinkpos)))
3287 ;; There is nothing to show except the char itself.
3288 (buffer-substring blinkpos (1+ blinkpos))))))))
3289 (cond (mismatch
3290 (message "Mismatched parentheses"))
3291 ((not blink-matching-paren-distance)
3292 (message "Unmatched parenthesis"))))))))
3293
3294 ;Turned off because it makes dbx bomb out.
3295 (setq blink-paren-function 'blink-matching-open)
3296
3297 ;; This executes C-g typed while Emacs is waiting for a command.
3298 ;; Quitting out of a program does not go through here;
3299 ;; that happens in the QUIT macro at the C code level.
3300 (defun keyboard-quit ()
3301 "Signal a `quit' condition.
3302 During execution of Lisp code, this character causes a quit directly.
3303 At top-level, as an editor command, this simply beeps."
3304 (interactive)
3305 (deactivate-mark)
3306 (signal 'quit nil))
3307
3308 (define-key global-map "\C-g" 'keyboard-quit)
3309
3310 (defvar buffer-quit-function nil
3311 "Function to call to \"quit\" the current buffer, or nil if none.
3312 \\[keyboard-escape-quit] calls this function when its more local actions
3313 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3314
3315 (defun keyboard-escape-quit ()
3316 "Exit the current \"mode\" (in a generalized sense of the word).
3317 This command can exit an interactive command such as `query-replace',
3318 can clear out a prefix argument or a region,
3319 can get out of the minibuffer or other recursive edit,
3320 cancel the use of the current buffer (for special-purpose buffers),
3321 or go back to just one window (by deleting all but the selected window)."
3322 (interactive)
3323 (cond ((eq last-command 'mode-exited) nil)
3324 ((> (minibuffer-depth) 0)
3325 (abort-recursive-edit))
3326 (current-prefix-arg
3327 nil)
3328 ((and transient-mark-mode
3329 mark-active)
3330 (deactivate-mark))
3331 ((> (recursion-depth) 0)
3332 (exit-recursive-edit))
3333 (buffer-quit-function
3334 (funcall buffer-quit-function))
3335 ((not (one-window-p t))
3336 (delete-other-windows))
3337 ((string-match "^ \\*" (buffer-name (current-buffer)))
3338 (bury-buffer))))
3339
3340 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3341
3342 (defcustom read-mail-command 'rmail
3343 "*Your preference for a mail reading package.
3344 This is used by some keybindings which support reading mail.
3345 See also `mail-user-agent' concerning sending mail."
3346 :type '(choice (function-item rmail)
3347 (function-item gnus)
3348 (function-item mh-rmail)
3349 (function :tag "Other"))
3350 :version "21.1"
3351 :group 'mail)
3352
3353 (defcustom mail-user-agent 'sendmail-user-agent
3354 "*Your preference for a mail composition package.
3355 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3356 outgoing email message. This variable lets you specify which
3357 mail-sending package you prefer.
3358
3359 Valid values include:
3360
3361 `sendmail-user-agent' -- use the default Emacs Mail package.
3362 See Info node `(emacs)Sending Mail'.
3363 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3364 See Info node `(mh-e)'.
3365 `message-user-agent' -- use the Gnus Message package.
3366 See Info node `(message)'.
3367 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3368 paraphernalia, particularly the Gcc: header for
3369 archiving.
3370
3371 Additional valid symbols may be available; check with the author of
3372 your package for details. The function should return non-nil if it
3373 succeeds.
3374
3375 See also `read-mail-command' concerning reading mail."
3376 :type '(radio (function-item :tag "Default Emacs mail"
3377 :format "%t\n"
3378 sendmail-user-agent)
3379 (function-item :tag "Emacs interface to MH"
3380 :format "%t\n"
3381 mh-e-user-agent)
3382 (function-item :tag "Gnus Message package"
3383 :format "%t\n"
3384 message-user-agent)
3385 (function-item :tag "Gnus Message with full Gnus features"
3386 :format "%t\n"
3387 gnus-user-agent)
3388 (function :tag "Other"))
3389 :group 'mail)
3390
3391 (defun define-mail-user-agent (symbol composefunc sendfunc
3392 &optional abortfunc hookvar)
3393 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3394
3395 SYMBOL can be any Lisp symbol. Its function definition and/or
3396 value as a variable do not matter for this usage; we use only certain
3397 properties on its property list, to encode the rest of the arguments.
3398
3399 COMPOSEFUNC is program callable function that composes an outgoing
3400 mail message buffer. This function should set up the basics of the
3401 buffer without requiring user interaction. It should populate the
3402 standard mail headers, leaving the `to:' and `subject:' headers blank
3403 by default.
3404
3405 COMPOSEFUNC should accept several optional arguments--the same
3406 arguments that `compose-mail' takes. See that function's documentation.
3407
3408 SENDFUNC is the command a user would run to send the message.
3409
3410 Optional ABORTFUNC is the command a user would run to abort the
3411 message. For mail packages that don't have a separate abort function,
3412 this can be `kill-buffer' (the equivalent of omitting this argument).
3413
3414 Optional HOOKVAR is a hook variable that gets run before the message
3415 is actually sent. Callers that use the `mail-user-agent' may
3416 install a hook function temporarily on this hook variable.
3417 If HOOKVAR is nil, `mail-send-hook' is used.
3418
3419 The properties used on SYMBOL are `composefunc', `sendfunc',
3420 `abortfunc', and `hookvar'."
3421 (put symbol 'composefunc composefunc)
3422 (put symbol 'sendfunc sendfunc)
3423 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3424 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3425
3426 (define-mail-user-agent 'sendmail-user-agent
3427 'sendmail-user-agent-compose
3428 'mail-send-and-exit)
3429
3430 (defun rfc822-goto-eoh ()
3431 ;; Go to header delimiter line in a mail message, following RFC822 rules
3432 (goto-char (point-min))
3433 (when (re-search-forward
3434 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
3435 (goto-char (match-beginning 0))))
3436
3437 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3438 switch-function yank-action
3439 send-actions)
3440 (if switch-function
3441 (let ((special-display-buffer-names nil)
3442 (special-display-regexps nil)
3443 (same-window-buffer-names nil)
3444 (same-window-regexps nil))
3445 (funcall switch-function "*mail*")))
3446 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3447 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers)))
3448 (body (cdr (assoc-ignore-case "body" other-headers))))
3449 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3450 continue
3451 (error "Message aborted"))
3452 (save-excursion
3453 (rfc822-goto-eoh)
3454 (while other-headers
3455 (unless (member-ignore-case (car (car other-headers))
3456 '("in-reply-to" "cc" "body"))
3457 (insert (car (car other-headers)) ": "
3458 (cdr (car other-headers)) "\n"))
3459 (setq other-headers (cdr other-headers)))
3460 (when body
3461 (forward-line 1)
3462 (insert body))
3463 t)))
3464
3465 (define-mail-user-agent 'mh-e-user-agent
3466 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3467 'mh-before-send-letter-hook)
3468
3469 (defun compose-mail (&optional to subject other-headers continue
3470 switch-function yank-action send-actions)
3471 "Start composing a mail message to send.
3472 This uses the user's chosen mail composition package
3473 as selected with the variable `mail-user-agent'.
3474 The optional arguments TO and SUBJECT specify recipients
3475 and the initial Subject field, respectively.
3476
3477 OTHER-HEADERS is an alist specifying additional
3478 header fields. Elements look like (HEADER . VALUE) where both
3479 HEADER and VALUE are strings.
3480
3481 CONTINUE, if non-nil, says to continue editing a message already
3482 being composed.
3483
3484 SWITCH-FUNCTION, if non-nil, is a function to use to
3485 switch to and display the buffer used for mail composition.
3486
3487 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3488 to insert the raw text of the message being replied to.
3489 It has the form (FUNCTION . ARGS). The user agent will apply
3490 FUNCTION to ARGS, to insert the raw text of the original message.
3491 \(The user agent will also run `mail-citation-hook', *after* the
3492 original text has been inserted in this way.)
3493
3494 SEND-ACTIONS is a list of actions to call when the message is sent.
3495 Each action has the form (FUNCTION . ARGS)."
3496 (interactive
3497 (list nil nil nil current-prefix-arg))
3498 (let ((function (get mail-user-agent 'composefunc)))
3499 (funcall function to subject other-headers continue
3500 switch-function yank-action send-actions)))
3501
3502 (defun compose-mail-other-window (&optional to subject other-headers continue
3503 yank-action send-actions)
3504 "Like \\[compose-mail], but edit the outgoing message in another window."
3505 (interactive
3506 (list nil nil nil current-prefix-arg))
3507 (compose-mail to subject other-headers continue
3508 'switch-to-buffer-other-window yank-action send-actions))
3509
3510
3511 (defun compose-mail-other-frame (&optional to subject other-headers continue
3512 yank-action send-actions)
3513 "Like \\[compose-mail], but edit the outgoing message in another frame."
3514 (interactive
3515 (list nil nil nil current-prefix-arg))
3516 (compose-mail to subject other-headers continue
3517 'switch-to-buffer-other-frame yank-action send-actions))
3518
3519 (defvar set-variable-value-history nil
3520 "History of values entered with `set-variable'.")
3521
3522 (defun set-variable (var val)
3523 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3524 When using this interactively, enter a Lisp object for VALUE.
3525 If you want VALUE to be a string, you must surround it with doublequotes.
3526 VALUE is used literally, not evaluated.
3527
3528 If VARIABLE has a `variable-interactive' property, that is used as if
3529 it were the arg to `interactive' (which see) to interactively read VALUE.
3530
3531 If VARIABLE has been defined with `defcustom', then the type information
3532 in the definition is used to check that VALUE is valid."
3533 (interactive
3534 (let* ((default-var (variable-at-point))
3535 (var (if (symbolp default-var)
3536 (read-variable (format "Set variable (default %s): " default-var)
3537 default-var)
3538 (read-variable "Set variable: ")))
3539 (minibuffer-help-form '(describe-variable var))
3540 (prop (get var 'variable-interactive))
3541 (prompt (format "Set %s to value: " var))
3542 (val (if prop
3543 ;; Use VAR's `variable-interactive' property
3544 ;; as an interactive spec for prompting.
3545 (call-interactively `(lambda (arg)
3546 (interactive ,prop)
3547 arg))
3548 (read
3549 (read-string prompt nil
3550 'set-variable-value-history)))))
3551 (list var val)))
3552
3553 (let ((type (get var 'custom-type)))
3554 (when type
3555 ;; Match with custom type.
3556 (require 'cus-edit)
3557 (setq type (widget-convert type))
3558 (unless (widget-apply type :match val)
3559 (error "Value `%S' does not match type %S of %S"
3560 val (car type) var))))
3561 (set var val)
3562
3563 ;; Force a thorough redisplay for the case that the variable
3564 ;; has an effect on the display, like `tab-width' has.
3565 (force-mode-line-update))
3566
3567 ;; Define the major mode for lists of completions.
3568
3569 (defvar completion-list-mode-map nil
3570 "Local map for completion list buffers.")
3571 (or completion-list-mode-map
3572 (let ((map (make-sparse-keymap)))
3573 (define-key map [mouse-2] 'mouse-choose-completion)
3574 (define-key map [down-mouse-2] nil)
3575 (define-key map "\C-m" 'choose-completion)
3576 (define-key map "\e\e\e" 'delete-completion-window)
3577 (define-key map [left] 'previous-completion)
3578 (define-key map [right] 'next-completion)
3579 (setq completion-list-mode-map map)))
3580
3581 ;; Completion mode is suitable only for specially formatted data.
3582 (put 'completion-list-mode 'mode-class 'special)
3583
3584 (defvar completion-reference-buffer nil
3585 "Record the buffer that was current when the completion list was requested.
3586 This is a local variable in the completion list buffer.
3587 Initial value is nil to avoid some compiler warnings.")
3588
3589 (defvar completion-no-auto-exit nil
3590 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3591 This also applies to other functions such as `choose-completion'
3592 and `mouse-choose-completion'.")
3593
3594 (defvar completion-base-size nil
3595 "Number of chars at beginning of minibuffer not involved in completion.
3596 This is a local variable in the completion list buffer
3597 but it talks about the buffer in `completion-reference-buffer'.
3598 If this is nil, it means to compare text to determine which part
3599 of the tail end of the buffer's text is involved in completion.")
3600
3601 (defun delete-completion-window ()
3602 "Delete the completion list window.
3603 Go to the window from which completion was requested."
3604 (interactive)
3605 (let ((buf completion-reference-buffer))
3606 (if (one-window-p t)
3607 (if (window-dedicated-p (selected-window))
3608 (delete-frame (selected-frame)))
3609 (delete-window (selected-window))
3610 (if (get-buffer-window buf)
3611 (select-window (get-buffer-window buf))))))
3612
3613 (defun previous-completion (n)
3614 "Move to the previous item in the completion list."
3615 (interactive "p")
3616 (next-completion (- n)))
3617
3618 (defun next-completion (n)
3619 "Move to the next item in the completion list.
3620 With prefix argument N, move N items (negative N means move backward)."
3621 (interactive "p")
3622 (let ((beg (point-min)) (end (point-max)))
3623 (while (and (> n 0) (not (eobp)))
3624 ;; If in a completion, move to the end of it.
3625 (when (get-text-property (point) 'mouse-face)
3626 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3627 ;; Move to start of next one.
3628 (unless (get-text-property (point) 'mouse-face)
3629 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3630 (setq n (1- n)))
3631 (while (and (< n 0) (not (bobp)))
3632 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
3633 ;; If in a completion, move to the start of it.
3634 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
3635 (goto-char (previous-single-property-change
3636 (point) 'mouse-face nil beg)))
3637 ;; Move to end of the previous completion.
3638 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
3639 (goto-char (previous-single-property-change
3640 (point) 'mouse-face nil beg)))
3641 ;; Move to the start of that one.
3642 (goto-char (previous-single-property-change
3643 (point) 'mouse-face nil beg))
3644 (setq n (1+ n))))))
3645
3646 (defun choose-completion ()
3647 "Choose the completion that point is in or next to."
3648 (interactive)
3649 (let (beg end completion (buffer completion-reference-buffer)
3650 (base-size completion-base-size))
3651 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3652 (setq end (point) beg (1+ (point))))
3653 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3654 (setq end (1- (point)) beg (point)))
3655 (if (null beg)
3656 (error "No completion here"))
3657 (setq beg (previous-single-property-change beg 'mouse-face))
3658 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3659 (setq completion (buffer-substring beg end))
3660 (let ((owindow (selected-window)))
3661 (if (and (one-window-p t 'selected-frame)
3662 (window-dedicated-p (selected-window)))
3663 ;; This is a special buffer's frame
3664 (iconify-frame (selected-frame))
3665 (or (window-dedicated-p (selected-window))
3666 (bury-buffer)))
3667 (select-window owindow))
3668 (choose-completion-string completion buffer base-size)))
3669
3670 ;; Delete the longest partial match for STRING
3671 ;; that can be found before POINT.
3672 (defun choose-completion-delete-max-match (string)
3673 (let ((opoint (point))
3674 (len (min (length string)
3675 (- (point) (point-min)))))
3676 (goto-char (- (point) (length string)))
3677 (if completion-ignore-case
3678 (setq string (downcase string)))
3679 (while (and (> len 0)
3680 (let ((tail (buffer-substring (point)
3681 (+ (point) len))))
3682 (if completion-ignore-case
3683 (setq tail (downcase tail)))
3684 (not (string= tail (substring string 0 len)))))
3685 (setq len (1- len))
3686 (forward-char 1))
3687 (delete-char len)))
3688
3689 ;; Switch to BUFFER and insert the completion choice CHOICE.
3690 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3691 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3692
3693 ;; If BUFFER is the minibuffer, exit the minibuffer
3694 ;; unless it is reading a file name and CHOICE is a directory,
3695 ;; or completion-no-auto-exit is non-nil.
3696 (defun choose-completion-string (choice &optional buffer base-size)
3697 (let ((buffer (or buffer completion-reference-buffer))
3698 (mini-p (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))))
3699 ;; If BUFFER is a minibuffer, barf unless it's the currently
3700 ;; active minibuffer.
3701 (if (and mini-p
3702 (or (not (active-minibuffer-window))
3703 (not (equal buffer
3704 (window-buffer (active-minibuffer-window))))))
3705 (error "Minibuffer is not active for completion")
3706 ;; Insert the completion into the buffer where completion was requested.
3707 (set-buffer buffer)
3708 (if base-size
3709 (delete-region (+ base-size (if mini-p
3710 (minibuffer-prompt-end)
3711 (point-min)))
3712 (point))
3713 (choose-completion-delete-max-match choice))
3714 (insert choice)
3715 (remove-text-properties (- (point) (length choice)) (point)
3716 '(mouse-face nil))
3717 ;; Update point in the window that BUFFER is showing in.
3718 (let ((window (get-buffer-window buffer t)))
3719 (set-window-point window (point)))
3720 ;; If completing for the minibuffer, exit it with this choice.
3721 (and (not completion-no-auto-exit)
3722 (equal buffer (window-buffer (minibuffer-window)))
3723 minibuffer-completion-table
3724 ;; If this is reading a file name, and the file name chosen
3725 ;; is a directory, don't exit the minibuffer.
3726 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3727 (file-directory-p (field-string (point-max))))
3728 (let ((mini (active-minibuffer-window)))
3729 (select-window mini)
3730 (when minibuffer-auto-raise
3731 (raise-frame (window-frame mini))))
3732 (exit-minibuffer))))))
3733
3734 (defun completion-list-mode ()
3735 "Major mode for buffers showing lists of possible completions.
3736 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3737 to select the completion near point.
3738 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3739 with the mouse."
3740 (interactive)
3741 (kill-all-local-variables)
3742 (use-local-map completion-list-mode-map)
3743 (setq mode-name "Completion List")
3744 (setq major-mode 'completion-list-mode)
3745 (make-local-variable 'completion-base-size)
3746 (setq completion-base-size nil)
3747 (run-hooks 'completion-list-mode-hook))
3748
3749 (defun completion-list-mode-finish ()
3750 "Finish setup of the completions buffer.
3751 Called from `temp-buffer-show-hook'."
3752 (when (eq major-mode 'completion-list-mode)
3753 (toggle-read-only 1)))
3754
3755 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
3756
3757 (defvar completion-setup-hook nil
3758 "Normal hook run at the end of setting up a completion list buffer.
3759 When this hook is run, the current buffer is the one in which the
3760 command to display the completion list buffer was run.
3761 The completion list buffer is available as the value of `standard-output'.")
3762
3763 ;; This function goes in completion-setup-hook, so that it is called
3764 ;; after the text of the completion list buffer is written.
3765
3766 (defun completion-setup-function ()
3767 (save-excursion
3768 (let ((mainbuf (current-buffer)))
3769 (set-buffer standard-output)
3770 (completion-list-mode)
3771 (make-local-variable 'completion-reference-buffer)
3772 (setq completion-reference-buffer mainbuf)
3773 (if (eq minibuffer-completion-table 'read-file-name-internal)
3774 ;; For file name completion,
3775 ;; use the number of chars before the start of the
3776 ;; last file name component.
3777 (setq completion-base-size
3778 (save-excursion
3779 (set-buffer mainbuf)
3780 (goto-char (point-max))
3781 (skip-chars-backward (format "^%c" directory-sep-char))
3782 (- (point) (minibuffer-prompt-end))))
3783 ;; Otherwise, in minibuffer, the whole input is being completed.
3784 (save-match-data
3785 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3786 (buffer-name mainbuf))
3787 (setq completion-base-size 0))))
3788 (goto-char (point-min))
3789 (if (display-mouse-p)
3790 (insert (substitute-command-keys
3791 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3792 (insert (substitute-command-keys
3793 "In this buffer, type \\[choose-completion] to \
3794 select the completion near point.\n\n")))))
3795
3796 (add-hook 'completion-setup-hook 'completion-setup-function)
3797
3798 (define-key minibuffer-local-completion-map [prior]
3799 'switch-to-completions)
3800 (define-key minibuffer-local-must-match-map [prior]
3801 'switch-to-completions)
3802 (define-key minibuffer-local-completion-map "\M-v"
3803 'switch-to-completions)
3804 (define-key minibuffer-local-must-match-map "\M-v"
3805 'switch-to-completions)
3806
3807 (defun switch-to-completions ()
3808 "Select the completion list window."
3809 (interactive)
3810 ;; Make sure we have a completions window.
3811 (or (get-buffer-window "*Completions*")
3812 (minibuffer-completion-help))
3813 (let ((window (get-buffer-window "*Completions*")))
3814 (when window
3815 (select-window window)
3816 (goto-char (point-min))
3817 (search-forward "\n\n")
3818 (forward-line 1))))
3819
3820 ;; Support keyboard commands to turn on various modifiers.
3821
3822 ;; These functions -- which are not commands -- each add one modifier
3823 ;; to the following event.
3824
3825 (defun event-apply-alt-modifier (ignore-prompt)
3826 "Add the Alt modifier to the following event.
3827 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
3828 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3829 (defun event-apply-super-modifier (ignore-prompt)
3830 "Add the Super modifier to the following event.
3831 For example, type \\[event-apply-super-modifier] & to enter Super-&."
3832 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3833 (defun event-apply-hyper-modifier (ignore-prompt)
3834 "Add the Hyper modifier to the following event.
3835 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
3836 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3837 (defun event-apply-shift-modifier (ignore-prompt)
3838 "Add the Shift modifier to the following event.
3839 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
3840 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3841 (defun event-apply-control-modifier (ignore-prompt)
3842 "Add the Ctrl modifier to the following event.
3843 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
3844 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3845 (defun event-apply-meta-modifier (ignore-prompt)
3846 "Add the Meta modifier to the following event.
3847 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
3848 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3849
3850 (defun event-apply-modifier (event symbol lshiftby prefix)
3851 "Apply a modifier flag to event EVENT.
3852 SYMBOL is the name of this modifier, as a symbol.
3853 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3854 PREFIX is the string that represents this modifier in an event type symbol."
3855 (if (numberp event)
3856 (cond ((eq symbol 'control)
3857 (if (and (<= (downcase event) ?z)
3858 (>= (downcase event) ?a))
3859 (- (downcase event) ?a -1)
3860 (if (and (<= (downcase event) ?Z)
3861 (>= (downcase event) ?A))
3862 (- (downcase event) ?A -1)
3863 (logior (lsh 1 lshiftby) event))))
3864 ((eq symbol 'shift)
3865 (if (and (<= (downcase event) ?z)
3866 (>= (downcase event) ?a))
3867 (upcase event)
3868 (logior (lsh 1 lshiftby) event)))
3869 (t
3870 (logior (lsh 1 lshiftby) event)))
3871 (if (memq symbol (event-modifiers event))
3872 event
3873 (let ((event-type (if (symbolp event) event (car event))))
3874 (setq event-type (intern (concat prefix (symbol-name event-type))))
3875 (if (symbolp event)
3876 event-type
3877 (cons event-type (cdr event)))))))
3878
3879 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
3880 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
3881 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
3882 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
3883 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
3884 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
3885
3886 ;;;; Keypad support.
3887
3888 ;;; Make the keypad keys act like ordinary typing keys. If people add
3889 ;;; bindings for the function key symbols, then those bindings will
3890 ;;; override these, so this shouldn't interfere with any existing
3891 ;;; bindings.
3892
3893 ;; Also tell read-char how to handle these keys.
3894 (mapc
3895 (lambda (keypad-normal)
3896 (let ((keypad (nth 0 keypad-normal))
3897 (normal (nth 1 keypad-normal)))
3898 (put keypad 'ascii-character normal)
3899 (define-key function-key-map (vector keypad) (vector normal))))
3900 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
3901 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
3902 (kp-space ?\ )
3903 (kp-tab ?\t)
3904 (kp-enter ?\r)
3905 (kp-multiply ?*)
3906 (kp-add ?+)
3907 (kp-separator ?,)
3908 (kp-subtract ?-)
3909 (kp-decimal ?.)
3910 (kp-divide ?/)
3911 (kp-equal ?=)))
3912
3913 ;;;;
3914 ;;;; forking a twin copy of a buffer.
3915 ;;;;
3916
3917 (defvar clone-buffer-hook nil
3918 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
3919
3920 (defun clone-process (process &optional newname)
3921 "Create a twin copy of PROCESS.
3922 If NEWNAME is nil, it defaults to PROCESS' name;
3923 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
3924 If PROCESS is associated with a buffer, the new process will be associated
3925 with the current buffer instead.
3926 Returns nil if PROCESS has already terminated."
3927 (setq newname (or newname (process-name process)))
3928 (if (string-match "<[0-9]+>\\'" newname)
3929 (setq newname (substring newname 0 (match-beginning 0))))
3930 (when (memq (process-status process) '(run stop open))
3931 (let* ((process-connection-type (process-tty-name process))
3932 (old-kwoq (process-kill-without-query process nil))
3933 (new-process
3934 (if (memq (process-status process) '(open))
3935 (apply 'open-network-stream newname
3936 (if (process-buffer process) (current-buffer))
3937 (process-contact process))
3938 (apply 'start-process newname
3939 (if (process-buffer process) (current-buffer))
3940 (process-command process)))))
3941 (process-kill-without-query new-process old-kwoq)
3942 (process-kill-without-query process old-kwoq)
3943 (set-process-inherit-coding-system-flag
3944 new-process (process-inherit-coding-system-flag process))
3945 (set-process-filter new-process (process-filter process))
3946 (set-process-sentinel new-process (process-sentinel process))
3947 new-process)))
3948
3949 ;; things to maybe add (currently partly covered by `funcall mode':
3950 ;; - syntax-table
3951 ;; - overlays
3952 (defun clone-buffer (&optional newname display-flag)
3953 "Create a twin copy of the current buffer.
3954 If NEWNAME is nil, it defaults to the current buffer's name;
3955 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
3956
3957 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
3958 This runs the normal hook `clone-buffer-hook' in the new buffer
3959 after it has been set up properly in other respects."
3960 (interactive
3961 (progn
3962 (if buffer-file-name
3963 (error "Cannot clone a file-visiting buffer"))
3964 (if (get major-mode 'no-clone)
3965 (error "Cannot clone a buffer in %s mode" mode-name))
3966 (list (if current-prefix-arg (read-string "Name: "))
3967 t)))
3968 (if buffer-file-name
3969 (error "Cannot clone a file-visiting buffer"))
3970 (if (get major-mode 'no-clone)
3971 (error "Cannot clone a buffer in %s mode" mode-name))
3972 (setq newname (or newname (buffer-name)))
3973 (if (string-match "<[0-9]+>\\'" newname)
3974 (setq newname (substring newname 0 (match-beginning 0))))
3975 (let ((buf (current-buffer))
3976 (ptmin (point-min))
3977 (ptmax (point-max))
3978 (pt (point))
3979 (mk (if mark-active (mark t)))
3980 (modified (buffer-modified-p))
3981 (mode major-mode)
3982 (lvars (buffer-local-variables))
3983 (process (get-buffer-process (current-buffer)))
3984 (new (generate-new-buffer (or newname (buffer-name)))))
3985 (save-restriction
3986 (widen)
3987 (with-current-buffer new
3988 (insert-buffer-substring buf)))
3989 (with-current-buffer new
3990 (narrow-to-region ptmin ptmax)
3991 (goto-char pt)
3992 (if mk (set-mark mk))
3993 (set-buffer-modified-p modified)
3994
3995 ;; Clone the old buffer's process, if any.
3996 (when process (clone-process process))
3997
3998 ;; Now set up the major mode.
3999 (funcall mode)
4000
4001 ;; Set up other local variables.
4002 (mapcar (lambda (v)
4003 (condition-case () ;in case var is read-only
4004 (if (symbolp v)
4005 (makunbound v)
4006 (set (make-local-variable (car v)) (cdr v)))
4007 (error nil)))
4008 lvars)
4009
4010 ;; Run any hooks (typically set up by the major mode
4011 ;; for cloning to work properly).
4012 (run-hooks 'clone-buffer-hook))
4013 (if display-flag (pop-to-buffer new))
4014 new))
4015
4016
4017 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4018 "Create an indirect buffer that is a twin copy of the current buffer.
4019
4020 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4021 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4022 or if not called with a prefix arg, NEWNAME defaults to the current
4023 buffer's name. The name is modified by adding a `<N>' suffix to it
4024 or by incrementing the N in an existing suffix.
4025
4026 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4027 This is always done when called interactively.
4028
4029 Optional last arg NORECORD non-nil means do not put this buffer at the
4030 front of the list of recently selected ones."
4031 (interactive
4032 (progn
4033 (if (get major-mode 'no-clone-indirect)
4034 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4035 (list (if current-prefix-arg
4036 (read-string "BName of indirect buffer: "))
4037 t)))
4038 (if (get major-mode 'no-clone-indirect)
4039 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4040 (setq newname (or newname (buffer-name)))
4041 (if (string-match "<[0-9]+>\\'" newname)
4042 (setq newname (substring newname 0 (match-beginning 0))))
4043 (let* ((name (generate-new-buffer-name newname))
4044 (buffer (make-indirect-buffer (current-buffer) name t)))
4045 (when display-flag
4046 (pop-to-buffer buffer norecord))
4047 buffer))
4048
4049
4050 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4051 "Create an indirect buffer that is a twin copy of BUFFER.
4052 Select the new buffer in another window.
4053 Optional second arg NORECORD non-nil means do not put this buffer at
4054 the front of the list of recently selected ones."
4055 (interactive "bClone buffer in other window: ")
4056 (let ((popup-windows t))
4057 (set-buffer buffer)
4058 (clone-indirect-buffer nil t norecord)))
4059
4060 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4061
4062
4063 ;;; Syntax stuff.
4064
4065 (defconst syntax-code-table
4066 '((?\ 0 "whitespace")
4067 (?- 0 "whitespace")
4068 (?. 1 "punctuation")
4069 (?w 2 "word")
4070 (?_ 3 "symbol")
4071 (?\( 4 "open parenthesis")
4072 (?\) 5 "close parenthesis")
4073 (?\' 6 "expression prefix")
4074 (?\" 7 "string quote")
4075 (?$ 8 "paired delimiter")
4076 (?\\ 9 "escape")
4077 (?/ 10 "character quote")
4078 (?< 11 "comment start")
4079 (?> 12 "comment end")
4080 (?@ 13 "inherit")
4081 (nil 14 "comment fence")
4082 (nil 15 "string fence"))
4083 "Alist of forms (CHAR CODE DESCRIPTION) mapping characters to syntax info.
4084 CHAR is a character that is allowed as first char in the string
4085 specifying the syntax when calling `modify-syntax-entry'. CODE is the
4086 corresponing syntax code as it is stored in a syntax cell, and
4087 can be used as value of a `syntax-table' property.
4088 DESCRIPTION is the descriptive string for the syntax.")
4089
4090
4091 ;;; Handling of Backspace and Delete keys.
4092
4093 (defcustom normal-erase-is-backspace nil
4094 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4095
4096 On window systems, the default value of this option is chosen
4097 according to the keyboard used. If the keyboard has both a Backspace
4098 key and a Delete key, and both are mapped to their usual meanings, the
4099 option's default value is set to t, so that Backspace can be used to
4100 delete backward, and Delete can be used to delete forward.
4101
4102 If not running under a window system, customizing this option accomplishes
4103 a similar effect by mapping C-h, which is usually generated by the
4104 Backspace key, to DEL, and by mapping DEL to C-d via
4105 `keyboard-translate'. The former functionality of C-h is available on
4106 the F1 key. You should probably not use this setting if you don't
4107 have both Backspace, Delete and F1 keys.
4108
4109 Setting this variable with setq doesn't take effect. Programmatically,
4110 call `normal-erase-is-backspace-mode' (which see) instead."
4111 :type 'boolean
4112 :group 'editing-basics
4113 :version "21.1"
4114 :set (lambda (symbol value)
4115 ;; The fboundp is because of a problem with :set when
4116 ;; dumping Emacs. It doesn't really matter.
4117 (if (fboundp 'normal-erase-is-backspace-mode)
4118 (normal-erase-is-backspace-mode (or value 0))
4119 (set-default symbol value))))
4120
4121
4122 (defun normal-erase-is-backspace-mode (&optional arg)
4123 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4124
4125 With numeric arg, turn the mode on if and only if ARG is positive.
4126
4127 On window systems, when this mode is on, Delete is mapped to C-d and
4128 Backspace is mapped to DEL; when this mode is off, both Delete and
4129 Backspace are mapped to DEL. (The remapping goes via
4130 `function-key-map', so binding Delete or Backspace in the global or
4131 local keymap will override that.)
4132
4133 In addition, on window systems, the bindings of C-Delete, M-Delete,
4134 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4135 the global keymap in accordance with the functionality of Delete and
4136 Backspace. For example, if Delete is remapped to C-d, which deletes
4137 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4138 to DEL, which deletes backward, C-Delete is bound to
4139 `backward-kill-word'.
4140
4141 If not running on a window system, a similar effect is accomplished by
4142 remapping C-h (normally produced by the Backspace key) and DEL via
4143 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4144 to C-d; if it's off, the keys are not remapped.
4145
4146 When not running on a window system, and this mode is turned on, the
4147 former functionality of C-h is available on the F1 key. You should
4148 probably not turn on this mode on a text-only terminal if you don't
4149 have both Backspace, Delete and F1 keys.
4150
4151 See also `normal-erase-is-backspace'."
4152 (interactive "P")
4153 (setq normal-erase-is-backspace
4154 (if arg
4155 (> (prefix-numeric-value arg) 0)
4156 (not normal-erase-is-backspace)))
4157
4158 (cond ((or (memq window-system '(x w32 mac pc))
4159 (memq system-type '(ms-dos windows-nt)))
4160 (let ((bindings
4161 `(([C-delete] [C-backspace])
4162 ([M-delete] [M-backspace])
4163 ([C-M-delete] [C-M-backspace])
4164 (,esc-map
4165 [C-delete] [C-backspace])))
4166 (old-state (lookup-key function-key-map [delete])))
4167
4168 (if normal-erase-is-backspace
4169 (progn
4170 (define-key function-key-map [delete] [?\C-d])
4171 (define-key function-key-map [kp-delete] [?\C-d])
4172 (define-key function-key-map [backspace] [?\C-?]))
4173 (define-key function-key-map [delete] [?\C-?])
4174 (define-key function-key-map [kp-delete] [?\C-?])
4175 (define-key function-key-map [backspace] [?\C-?]))
4176
4177 ;; Maybe swap bindings of C-delete and C-backspace, etc.
4178 (unless (equal old-state (lookup-key function-key-map [delete]))
4179 (dolist (binding bindings)
4180 (let ((map global-map))
4181 (when (keymapp (car binding))
4182 (setq map (car binding) binding (cdr binding)))
4183 (let* ((key1 (nth 0 binding))
4184 (key2 (nth 1 binding))
4185 (binding1 (lookup-key map key1))
4186 (binding2 (lookup-key map key2)))
4187 (define-key map key1 binding2)
4188 (define-key map key2 binding1)))))))
4189 (t
4190 (if normal-erase-is-backspace
4191 (progn
4192 (keyboard-translate ?\C-h ?\C-?)
4193 (keyboard-translate ?\C-? ?\C-d))
4194 (keyboard-translate ?\C-h ?\C-h)
4195 (keyboard-translate ?\C-? ?\C-?))))
4196
4197 (run-hooks 'normal-erase-is-backspace-hook)
4198 (if (interactive-p)
4199 (message "Delete key deletes %s"
4200 (if normal-erase-is-backspace "forward" "backward"))))
4201
4202
4203 ;;; Misc
4204
4205 (defun byte-compiling-files-p ()
4206 "Return t if currently byte-compiling files."
4207 (and (boundp 'byte-compile-current-file)
4208 (stringp byte-compile-current-file)))
4209
4210
4211 ;; Minibuffer prompt stuff.
4212
4213 ;(defun minibuffer-prompt-modification (start end)
4214 ; (error "You cannot modify the prompt"))
4215 ;
4216 ;
4217 ;(defun minibuffer-prompt-insertion (start end)
4218 ; (let ((inhibit-modification-hooks t))
4219 ; (delete-region start end)
4220 ; ;; Discard undo information for the text insertion itself
4221 ; ;; and for the text deletion.above.
4222 ; (when (consp buffer-undo-list)
4223 ; (setq buffer-undo-list (cddr buffer-undo-list)))
4224 ; (message "You cannot modify the prompt")))
4225 ;
4226 ;
4227 ;(setq minibuffer-prompt-properties
4228 ; (list 'modification-hooks '(minibuffer-prompt-modification)
4229 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
4230 ;
4231
4232 ;;; simple.el ends here