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