(defcustom): Doc fix.
[emacs.git] / lisp / simple.el
blobecf0e95a6204fbde8b9574db5f6c63ae3c4ffcc2
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 ((and (or (<= lines 1)
1347 (<= lines
1348 (if resize-mini-windows
1349 (cond ((floatp max-mini-window-height)
1350 (* (frame-height)
1351 max-mini-window-height))
1352 ((integerp max-mini-window-height)
1353 max-mini-window-height)
1356 1)))
1357 ;; Don't use the echo area if the output buffer is
1358 ;; already dispayed in the selected frame.
1359 (not (get-buffer-window (current-buffer))))
1360 ;; Echo area
1361 (goto-char (point-max))
1362 (when (bolp)
1363 (backward-char 1))
1364 (message "%s" (buffer-substring (point-min) (point))))
1366 ;; Buffer
1367 (goto-char (point-min))
1368 (display-buffer (current-buffer)
1369 not-this-window frame))))))))
1372 ;; We have a sentinel to prevent insertion of a termination message
1373 ;; in the buffer itself.
1374 (defun shell-command-sentinel (process signal)
1375 (if (memq (process-status process) '(exit signal))
1376 (message "%s: %s."
1377 (car (cdr (cdr (process-command process))))
1378 (substring signal 0 -1))))
1380 (defun shell-command-on-region (start end command
1381 &optional output-buffer replace
1382 error-buffer)
1383 "Execute string COMMAND in inferior shell with region as input.
1384 Normally display output (if any) in temp buffer `*Shell Command Output*';
1385 Prefix arg means replace the region with it. Return the exit code of
1386 COMMAND.
1388 To specify a coding system for converting non-ASCII characters
1389 in the input and output to the shell command, use \\[universal-coding-system-argument]
1390 before this command. By default, the input (from the current buffer)
1391 is encoded in the same coding system that will be used to save the file,
1392 `buffer-file-coding-system'. If the output is going to replace the region,
1393 then it is decoded from that same coding system.
1395 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1396 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1397 systems by binding `coding-system-for-read' and
1398 `coding-system-for-write'.
1400 If the command generates output, the output may be displayed
1401 in the echo area or in a buffer.
1402 If the output is short enough to display in the echo area
1403 \(determined by the variable `max-mini-window-height' if
1404 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1405 it is displayed in the buffer `*Shell Command Output*'. The output
1406 is available in that buffer in both cases.
1408 If there is output and an error, a message about the error
1409 appears at the end of the output.
1411 If there is no output, or if output is inserted in the current buffer,
1412 then `*Shell Command Output*' is deleted.
1414 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1415 that says to put the output in some other buffer.
1416 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1417 If OUTPUT-BUFFER is not a buffer and not nil,
1418 insert output in the current buffer.
1419 In either case, the output is inserted after point (leaving mark after it).
1421 If REPLACE, the optional fifth argument, is non-nil, that means insert
1422 the output in place of text from START to END, putting point and mark
1423 around it.
1425 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1426 or buffer name to which to direct the command's standard error output.
1427 If it is nil, error output is mingled with regular output.
1428 In an interactive call, the variable `shell-command-default-error-buffer'
1429 specifies the value of ERROR-BUFFER."
1430 (interactive (let (string)
1431 (unless (mark)
1432 (error "The mark is not set now, so there is no region"))
1433 ;; Do this before calling region-beginning
1434 ;; and region-end, in case subprocess output
1435 ;; relocates them while we are in the minibuffer.
1436 (setq string (read-from-minibuffer "Shell command on region: "
1437 nil nil nil
1438 'shell-command-history))
1439 ;; call-interactively recognizes region-beginning and
1440 ;; region-end specially, leaving them in the history.
1441 (list (region-beginning) (region-end)
1442 string
1443 current-prefix-arg
1444 current-prefix-arg
1445 shell-command-default-error-buffer)))
1446 (let ((error-file
1447 (if error-buffer
1448 (make-temp-file
1449 (expand-file-name "scor"
1450 (or small-temporary-file-directory
1451 temporary-file-directory)))
1452 nil))
1453 exit-status)
1454 (if (or replace
1455 (and output-buffer
1456 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1457 ;; Replace specified region with output from command.
1458 (let ((swap (and replace (< start end))))
1459 ;; Don't muck with mark unless REPLACE says we should.
1460 (goto-char start)
1461 (and replace (push-mark (point) 'nomsg))
1462 (setq exit-status
1463 (call-process-region start end shell-file-name t
1464 (if error-file
1465 (list t error-file)
1467 nil shell-command-switch command))
1468 ;; It is rude to delete a buffer which the command is not using.
1469 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1470 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1471 ;; (kill-buffer shell-buffer)))
1472 ;; Don't muck with mark unless REPLACE says we should.
1473 (and replace swap (exchange-point-and-mark)))
1474 ;; No prefix argument: put the output in a temp buffer,
1475 ;; replacing its entire contents.
1476 (let ((buffer (get-buffer-create
1477 (or output-buffer "*Shell Command Output*"))))
1478 (unwind-protect
1479 (if (eq buffer (current-buffer))
1480 ;; If the input is the same buffer as the output,
1481 ;; delete everything but the specified region,
1482 ;; then replace that region with the output.
1483 (progn (setq buffer-read-only nil)
1484 (delete-region (max start end) (point-max))
1485 (delete-region (point-min) (min start end))
1486 (setq exit-status
1487 (call-process-region (point-min) (point-max)
1488 shell-file-name t
1489 (if error-file
1490 (list t error-file)
1492 nil shell-command-switch
1493 command)))
1494 ;; Clear the output buffer, then run the command with
1495 ;; output there.
1496 (let ((directory default-directory))
1497 (save-excursion
1498 (set-buffer buffer)
1499 (setq buffer-read-only nil)
1500 (if (not output-buffer)
1501 (setq default-directory directory))
1502 (erase-buffer)))
1503 (setq exit-status
1504 (call-process-region start end shell-file-name nil
1505 (if error-file
1506 (list buffer error-file)
1507 buffer)
1508 nil shell-command-switch command)))
1509 ;; Report the output.
1510 (with-current-buffer buffer
1511 (setq mode-line-process
1512 (cond ((null exit-status)
1513 " - Error")
1514 ((stringp exit-status)
1515 (format " - Signal [%s]" exit-status))
1516 ((not (equal 0 exit-status))
1517 (format " - Exit [%d]" exit-status)))))
1518 (if (with-current-buffer buffer (> (point-max) (point-min)))
1519 ;; There's some output, display it
1520 (display-message-or-buffer buffer)
1521 ;; No output; error?
1522 (let ((output
1523 (if (and error-file
1524 (< 0 (nth 7 (file-attributes error-file))))
1525 "some error output"
1526 "no output")))
1527 (cond ((null exit-status)
1528 (message "(Shell command failed with error)"))
1529 ((equal 0 exit-status)
1530 (message "(Shell command succeeded with %s)"
1531 output))
1532 ((stringp exit-status)
1533 (message "(Shell command killed by signal %s)"
1534 exit-status))
1536 (message "(Shell command failed with code %d and %s)"
1537 exit-status output))))
1538 ;; Don't kill: there might be useful info in the undo-log.
1539 ;; (kill-buffer buffer)
1540 ))))
1542 (when (and error-file (file-exists-p error-file))
1543 (if (< 0 (nth 7 (file-attributes error-file)))
1544 (with-current-buffer (get-buffer-create error-buffer)
1545 (let ((pos-from-end (- (point-max) (point))))
1546 (or (bobp)
1547 (insert "\f\n"))
1548 ;; Do no formatting while reading error file,
1549 ;; because that can run a shell command, and we
1550 ;; don't want that to cause an infinite recursion.
1551 (format-insert-file error-file nil)
1552 ;; Put point after the inserted errors.
1553 (goto-char (- (point-max) pos-from-end)))
1554 (display-buffer (current-buffer))))
1555 (delete-file error-file))
1556 exit-status))
1558 (defun shell-command-to-string (command)
1559 "Execute shell command COMMAND and return its output as a string."
1560 (with-output-to-string
1561 (with-current-buffer
1562 standard-output
1563 (call-process shell-file-name nil t nil shell-command-switch command))))
1565 (defvar universal-argument-map
1566 (let ((map (make-sparse-keymap)))
1567 (define-key map [t] 'universal-argument-other-key)
1568 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1569 (define-key map [switch-frame] nil)
1570 (define-key map [?\C-u] 'universal-argument-more)
1571 (define-key map [?-] 'universal-argument-minus)
1572 (define-key map [?0] 'digit-argument)
1573 (define-key map [?1] 'digit-argument)
1574 (define-key map [?2] 'digit-argument)
1575 (define-key map [?3] 'digit-argument)
1576 (define-key map [?4] 'digit-argument)
1577 (define-key map [?5] 'digit-argument)
1578 (define-key map [?6] 'digit-argument)
1579 (define-key map [?7] 'digit-argument)
1580 (define-key map [?8] 'digit-argument)
1581 (define-key map [?9] 'digit-argument)
1582 (define-key map [kp-0] 'digit-argument)
1583 (define-key map [kp-1] 'digit-argument)
1584 (define-key map [kp-2] 'digit-argument)
1585 (define-key map [kp-3] 'digit-argument)
1586 (define-key map [kp-4] 'digit-argument)
1587 (define-key map [kp-5] 'digit-argument)
1588 (define-key map [kp-6] 'digit-argument)
1589 (define-key map [kp-7] 'digit-argument)
1590 (define-key map [kp-8] 'digit-argument)
1591 (define-key map [kp-9] 'digit-argument)
1592 (define-key map [kp-subtract] 'universal-argument-minus)
1593 map)
1594 "Keymap used while processing \\[universal-argument].")
1596 (defvar universal-argument-num-events nil
1597 "Number of argument-specifying events read by `universal-argument'.
1598 `universal-argument-other-key' uses this to discard those events
1599 from (this-command-keys), and reread only the final command.")
1601 (defun universal-argument ()
1602 "Begin a numeric argument for the following command.
1603 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1604 \\[universal-argument] following the digits or minus sign ends the argument.
1605 \\[universal-argument] without digits or minus sign provides 4 as argument.
1606 Repeating \\[universal-argument] without digits or minus sign
1607 multiplies the argument by 4 each time.
1608 For some commands, just \\[universal-argument] by itself serves as a flag
1609 which is different in effect from any particular numeric argument.
1610 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1611 (interactive)
1612 (setq prefix-arg (list 4))
1613 (setq universal-argument-num-events (length (this-command-keys)))
1614 (setq overriding-terminal-local-map universal-argument-map))
1616 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1617 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1618 (defun universal-argument-more (arg)
1619 (interactive "P")
1620 (if (consp arg)
1621 (setq prefix-arg (list (* 4 (car arg))))
1622 (if (eq arg '-)
1623 (setq prefix-arg (list -4))
1624 (setq prefix-arg arg)
1625 (setq overriding-terminal-local-map nil)))
1626 (setq universal-argument-num-events (length (this-command-keys))))
1628 (defun negative-argument (arg)
1629 "Begin a negative numeric argument for the next command.
1630 \\[universal-argument] following digits or minus sign ends the argument."
1631 (interactive "P")
1632 (cond ((integerp arg)
1633 (setq prefix-arg (- arg)))
1634 ((eq arg '-)
1635 (setq prefix-arg nil))
1637 (setq prefix-arg '-)))
1638 (setq universal-argument-num-events (length (this-command-keys)))
1639 (setq overriding-terminal-local-map universal-argument-map))
1641 (defun digit-argument (arg)
1642 "Part of the numeric argument for the next command.
1643 \\[universal-argument] following digits or minus sign ends the argument."
1644 (interactive "P")
1645 (let* ((char (if (integerp last-command-char)
1646 last-command-char
1647 (get last-command-char 'ascii-character)))
1648 (digit (- (logand char ?\177) ?0)))
1649 (cond ((integerp arg)
1650 (setq prefix-arg (+ (* arg 10)
1651 (if (< arg 0) (- digit) digit))))
1652 ((eq arg '-)
1653 ;; Treat -0 as just -, so that -01 will work.
1654 (setq prefix-arg (if (zerop digit) '- (- digit))))
1656 (setq prefix-arg digit))))
1657 (setq universal-argument-num-events (length (this-command-keys)))
1658 (setq overriding-terminal-local-map universal-argument-map))
1660 ;; For backward compatibility, minus with no modifiers is an ordinary
1661 ;; command if digits have already been entered.
1662 (defun universal-argument-minus (arg)
1663 (interactive "P")
1664 (if (integerp arg)
1665 (universal-argument-other-key arg)
1666 (negative-argument arg)))
1668 ;; Anything else terminates the argument and is left in the queue to be
1669 ;; executed as a command.
1670 (defun universal-argument-other-key (arg)
1671 (interactive "P")
1672 (setq prefix-arg arg)
1673 (let* ((key (this-command-keys))
1674 (keylist (listify-key-sequence key)))
1675 (setq unread-command-events
1676 (append (nthcdr universal-argument-num-events keylist)
1677 unread-command-events)))
1678 (reset-this-command-lengths)
1679 (setq overriding-terminal-local-map nil))
1681 ;;;; Window system cut and paste hooks.
1683 (defvar interprogram-cut-function nil
1684 "Function to call to make a killed region available to other programs.
1686 Most window systems provide some sort of facility for cutting and
1687 pasting text between the windows of different programs.
1688 This variable holds a function that Emacs calls whenever text
1689 is put in the kill ring, to make the new kill available to other
1690 programs.
1692 The function takes one or two arguments.
1693 The first argument, TEXT, is a string containing
1694 the text which should be made available.
1695 The second, PUSH, if non-nil means this is a \"new\" kill;
1696 nil means appending to an \"old\" kill.")
1698 (defvar interprogram-paste-function nil
1699 "Function to call to get text cut from other programs.
1701 Most window systems provide some sort of facility for cutting and
1702 pasting text between the windows of different programs.
1703 This variable holds a function that Emacs calls to obtain
1704 text that other programs have provided for pasting.
1706 The function should be called with no arguments. If the function
1707 returns nil, then no other program has provided such text, and the top
1708 of the Emacs kill ring should be used. If the function returns a
1709 string, that string should be put in the kill ring as the latest kill.
1711 Note that the function should return a string only if a program other
1712 than Emacs has provided a string for pasting; if Emacs provided the
1713 most recent string, the function should return nil. If it is
1714 difficult to tell whether Emacs or some other program provided the
1715 current string, it is probably good enough to return nil if the string
1716 is equal (according to `string=') to the last text Emacs provided.")
1720 ;;;; The kill ring data structure.
1722 (defvar kill-ring nil
1723 "List of killed text sequences.
1724 Since the kill ring is supposed to interact nicely with cut-and-paste
1725 facilities offered by window systems, use of this variable should
1726 interact nicely with `interprogram-cut-function' and
1727 `interprogram-paste-function'. The functions `kill-new',
1728 `kill-append', and `current-kill' are supposed to implement this
1729 interaction; you may want to use them instead of manipulating the kill
1730 ring directly.")
1732 (defcustom kill-ring-max 60
1733 "*Maximum length of kill ring before oldest elements are thrown away."
1734 :type 'integer
1735 :group 'killing)
1737 (defvar kill-ring-yank-pointer nil
1738 "The tail of the kill ring whose car is the last thing yanked.")
1740 (defun kill-new (string &optional replace)
1741 "Make STRING the latest kill in the kill ring.
1742 Set `kill-ring-yank-pointer' to point to it.
1743 If `interprogram-cut-function' is non-nil, apply it to STRING.
1744 Optional second argument REPLACE non-nil means that STRING will replace
1745 the front of the kill ring, rather than being added to the list."
1746 (and (fboundp 'menu-bar-update-yank-menu)
1747 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1748 (if (and replace kill-ring)
1749 (setcar kill-ring string)
1750 (setq kill-ring (cons string kill-ring))
1751 (if (> (length kill-ring) kill-ring-max)
1752 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1753 (setq kill-ring-yank-pointer kill-ring)
1754 (if interprogram-cut-function
1755 (funcall interprogram-cut-function string (not replace))))
1757 (defun kill-append (string before-p)
1758 "Append STRING to the end of the latest kill in the kill ring.
1759 If BEFORE-P is non-nil, prepend STRING to the kill.
1760 If `interprogram-cut-function' is set, pass the resulting kill to
1761 it."
1762 (kill-new (if before-p
1763 (concat string (car kill-ring))
1764 (concat (car kill-ring) string))
1767 (defun current-kill (n &optional do-not-move)
1768 "Rotate the yanking point by N places, and then return that kill.
1769 If N is zero, `interprogram-paste-function' is set, and calling it
1770 returns a string, then that string is added to the front of the
1771 kill ring and returned as the latest kill.
1772 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1773 yanking point; just return the Nth kill forward."
1774 (let ((interprogram-paste (and (= n 0)
1775 interprogram-paste-function
1776 (funcall interprogram-paste-function))))
1777 (if interprogram-paste
1778 (progn
1779 ;; Disable the interprogram cut function when we add the new
1780 ;; text to the kill ring, so Emacs doesn't try to own the
1781 ;; selection, with identical text.
1782 (let ((interprogram-cut-function nil))
1783 (kill-new interprogram-paste))
1784 interprogram-paste)
1785 (or kill-ring (error "Kill ring is empty"))
1786 (let ((ARGth-kill-element
1787 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1788 (length kill-ring))
1789 kill-ring)))
1790 (or do-not-move
1791 (setq kill-ring-yank-pointer ARGth-kill-element))
1792 (car ARGth-kill-element)))))
1796 ;;;; Commands for manipulating the kill ring.
1798 (defcustom kill-read-only-ok nil
1799 "*Non-nil means don't signal an error for killing read-only text."
1800 :type 'boolean
1801 :group 'killing)
1803 (put 'text-read-only 'error-conditions
1804 '(text-read-only buffer-read-only error))
1805 (put 'text-read-only 'error-message "Text is read-only")
1807 (defun kill-region (beg end)
1808 "Kill between point and mark.
1809 The text is deleted but saved in the kill ring.
1810 The command \\[yank] can retrieve it from there.
1811 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
1813 If you want to append the killed region to the last killed text,
1814 use \\[append-next-kill] before \\[kill-region].
1816 If the buffer is read-only, Emacs will beep and refrain from deleting
1817 the text, but put the text in the kill ring anyway. This means that
1818 you can use the killing commands to copy text from a read-only buffer.
1820 This is the primitive for programs to kill text (as opposed to deleting it).
1821 Supply two arguments, character numbers indicating the stretch of text
1822 to be killed.
1823 Any command that calls this function is a \"kill command\".
1824 If the previous command was also a kill command,
1825 the text killed this time appends to the text killed last time
1826 to make one entry in the kill ring."
1827 (interactive "r")
1828 (condition-case nil
1829 (let ((string (delete-and-extract-region beg end)))
1830 (when string ;STRING is nil if BEG = END
1831 ;; Add that string to the kill ring, one way or another.
1832 (if (eq last-command 'kill-region)
1833 (kill-append string (< end beg))
1834 (kill-new string)))
1835 (setq this-command 'kill-region))
1836 ((buffer-read-only text-read-only)
1837 ;; The code above failed because the buffer, or some of the characters
1838 ;; in the region, are read-only.
1839 ;; We should beep, in case the user just isn't aware of this.
1840 ;; However, there's no harm in putting
1841 ;; the region's text in the kill ring, anyway.
1842 (copy-region-as-kill beg end)
1843 ;; Set this-command now, so it will be set even if we get an error.
1844 (setq this-command 'kill-region)
1845 ;; This should barf, if appropriate, and give us the correct error.
1846 (if kill-read-only-ok
1847 (message "Read only text copied to kill ring")
1848 ;; Signal an error if the buffer is read-only.
1849 (barf-if-buffer-read-only)
1850 ;; If the buffer isn't read-only, the text is.
1851 (signal 'text-read-only (list (current-buffer)))))))
1853 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1854 ;; to get two copies of the text when the user accidentally types M-w and
1855 ;; then corrects it with the intended C-w.
1856 (defun copy-region-as-kill (beg end)
1857 "Save the region as if killed, but don't kill it.
1858 In Transient Mark mode, deactivate the mark.
1859 If `interprogram-cut-function' is non-nil, also save the text for a window
1860 system cut and paste."
1861 (interactive "r")
1862 (if (eq last-command 'kill-region)
1863 (kill-append (buffer-substring beg end) (< end beg))
1864 (kill-new (buffer-substring beg end)))
1865 (if transient-mark-mode
1866 (setq deactivate-mark t))
1867 nil)
1869 (defun kill-ring-save (beg end)
1870 "Save the region as if killed, but don't kill it.
1871 In Transient Mark mode, deactivate the mark.
1872 If `interprogram-cut-function' is non-nil, also save the text for a window
1873 system cut and paste.
1875 If you want to append the killed line to the last killed text,
1876 use \\[append-next-kill] before \\[kill-ring-save].
1878 This command is similar to `copy-region-as-kill', except that it gives
1879 visual feedback indicating the extent of the region being copied."
1880 (interactive "r")
1881 (copy-region-as-kill beg end)
1882 (if (interactive-p)
1883 (let ((other-end (if (= (point) beg) end beg))
1884 (opoint (point))
1885 ;; Inhibit quitting so we can make a quit here
1886 ;; look like a C-g typed as a command.
1887 (inhibit-quit t))
1888 (if (pos-visible-in-window-p other-end (selected-window))
1889 (unless transient-mark-mode
1890 ;; Swap point and mark.
1891 (set-marker (mark-marker) (point) (current-buffer))
1892 (goto-char other-end)
1893 (sit-for 1)
1894 ;; Swap back.
1895 (set-marker (mark-marker) other-end (current-buffer))
1896 (goto-char opoint)
1897 ;; If user quit, deactivate the mark
1898 ;; as C-g would as a command.
1899 (and quit-flag mark-active
1900 (deactivate-mark)))
1901 (let* ((killed-text (current-kill 0))
1902 (message-len (min (length killed-text) 40)))
1903 (if (= (point) beg)
1904 ;; Don't say "killed"; that is misleading.
1905 (message "Saved text until \"%s\""
1906 (substring killed-text (- message-len)))
1907 (message "Saved text from \"%s\""
1908 (substring killed-text 0 message-len))))))))
1910 (defun append-next-kill (&optional interactive)
1911 "Cause following command, if it kills, to append to previous kill.
1912 The argument is used for internal purposes; do not supply one."
1913 (interactive "p")
1914 ;; We don't use (interactive-p), since that breaks kbd macros.
1915 (if interactive
1916 (progn
1917 (setq this-command 'kill-region)
1918 (message "If the next command is a kill, it will append"))
1919 (setq last-command 'kill-region)))
1921 ;; Yanking.
1923 ;; This is actually used in subr.el but defcustom does not work there.
1924 (defcustom yank-excluded-properties
1925 '(read-only invisible intangible field mouse-face help-echo local-map keymap)
1926 "*Text properties to discard when yanking."
1927 :type '(choice (const :tag "All" t) (repeat symbol))
1928 :group 'editing
1929 :version "21.4")
1931 (defun yank-pop (arg)
1932 "Replace just-yanked stretch of killed text with a different stretch.
1933 This command is allowed only immediately after a `yank' or a `yank-pop'.
1934 At such a time, the region contains a stretch of reinserted
1935 previously-killed text. `yank-pop' deletes that text and inserts in its
1936 place a different stretch of killed text.
1938 With no argument, the previous kill is inserted.
1939 With argument N, insert the Nth previous kill.
1940 If N is negative, this is a more recent kill.
1942 The sequence of kills wraps around, so that after the oldest one
1943 comes the newest one."
1944 (interactive "*p")
1945 (if (not (eq last-command 'yank))
1946 (error "Previous command was not a yank"))
1947 (setq this-command 'yank)
1948 (let ((inhibit-read-only t)
1949 (before (< (point) (mark t))))
1950 (delete-region (point) (mark t))
1951 (set-marker (mark-marker) (point) (current-buffer))
1952 (insert-for-yank (current-kill arg))
1953 (if before
1954 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1955 ;; It is cleaner to avoid activation, even though the command
1956 ;; loop would deactivate the mark because we inserted text.
1957 (goto-char (prog1 (mark t)
1958 (set-marker (mark-marker) (point) (current-buffer))))))
1959 nil)
1961 (defun yank (&optional arg)
1962 "Reinsert the last stretch of killed text.
1963 More precisely, reinsert the stretch of killed text most recently
1964 killed OR yanked. Put point at end, and set mark at beginning.
1965 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
1966 With argument N, reinsert the Nth most recently killed stretch of killed
1967 text.
1968 See also the command \\[yank-pop]."
1969 (interactive "*P")
1970 ;; If we don't get all the way thru, make last-command indicate that
1971 ;; for the following command.
1972 (setq this-command t)
1973 (push-mark (point))
1974 (insert-for-yank (current-kill (cond
1975 ((listp arg) 0)
1976 ((eq arg '-) -1)
1977 (t (1- arg)))))
1978 (if (consp arg)
1979 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1980 ;; It is cleaner to avoid activation, even though the command
1981 ;; loop would deactivate the mark because we inserted text.
1982 (goto-char (prog1 (mark t)
1983 (set-marker (mark-marker) (point) (current-buffer)))))
1984 ;; If we do get all the way thru, make this-command indicate that.
1985 (setq this-command 'yank)
1986 nil)
1988 (defun rotate-yank-pointer (arg)
1989 "Rotate the yanking point in the kill ring.
1990 With argument, rotate that many kills forward (or backward, if negative)."
1991 (interactive "p")
1992 (current-kill arg))
1994 ;; Some kill commands.
1996 ;; Internal subroutine of delete-char
1997 (defun kill-forward-chars (arg)
1998 (if (listp arg) (setq arg (car arg)))
1999 (if (eq arg '-) (setq arg -1))
2000 (kill-region (point) (forward-point arg)))
2002 ;; Internal subroutine of backward-delete-char
2003 (defun kill-backward-chars (arg)
2004 (if (listp arg) (setq arg (car arg)))
2005 (if (eq arg '-) (setq arg -1))
2006 (kill-region (point) (forward-point (- arg))))
2008 (defcustom backward-delete-char-untabify-method 'untabify
2009 "*The method for untabifying when deleting backward.
2010 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2011 `hungry' -- delete all whitespace, both tabs and spaces;
2012 `all' -- delete all whitespace, including tabs, spaces and newlines;
2013 nil -- just delete one character."
2014 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2015 :version "20.3"
2016 :group 'killing)
2018 (defun backward-delete-char-untabify (arg &optional killp)
2019 "Delete characters backward, changing tabs into spaces.
2020 The exact behavior depends on `backward-delete-char-untabify-method'.
2021 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2022 Interactively, ARG is the prefix arg (default 1)
2023 and KILLP is t if a prefix arg was specified."
2024 (interactive "*p\nP")
2025 (when (eq backward-delete-char-untabify-method 'untabify)
2026 (let ((count arg))
2027 (save-excursion
2028 (while (and (> count 0) (not (bobp)))
2029 (if (= (preceding-char) ?\t)
2030 (let ((col (current-column)))
2031 (forward-char -1)
2032 (setq col (- col (current-column)))
2033 (insert-char ?\ col)
2034 (delete-char 1)))
2035 (forward-char -1)
2036 (setq count (1- count))))))
2037 (delete-backward-char
2038 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2039 ((eq backward-delete-char-untabify-method 'all)
2040 " \t\n\r"))))
2041 (if skip
2042 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2043 (point)))))
2044 (+ arg (if (zerop wh) 0 (1- wh))))
2045 arg))
2046 killp))
2048 (defun zap-to-char (arg char)
2049 "Kill up to and including ARG'th occurrence of CHAR.
2050 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2051 Goes backward if ARG is negative; error if CHAR not found."
2052 (interactive "p\ncZap to char: ")
2053 (kill-region (point) (progn
2054 (search-forward (char-to-string char) nil nil arg)
2055 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2056 (point))))
2058 ;; kill-line and its subroutines.
2060 (defcustom kill-whole-line nil
2061 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2062 :type 'boolean
2063 :group 'killing)
2065 (defun kill-line (&optional arg)
2066 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2067 With prefix argument, kill that many lines from point.
2068 Negative arguments kill lines backward.
2069 With zero argument, kills the text before point on the current line.
2071 When calling from a program, nil means \"no arg\",
2072 a number counts as a prefix arg.
2074 To kill a whole line, when point is not at the beginning, type \
2075 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2077 If `kill-whole-line' is non-nil, then this command kills the whole line
2078 including its terminating newline, when used at the beginning of a line
2079 with no argument. As a consequence, you can always kill a whole line
2080 by typing \\[beginning-of-line] \\[kill-line].
2082 If you want to append the killed line to the last killed text,
2083 use \\[append-next-kill] before \\[kill-line].
2085 If the buffer is read-only, Emacs will beep and refrain from deleting
2086 the line, but put the line in the kill ring anyway. This means that
2087 you can use this command to copy text from a read-only buffer."
2088 (interactive "P")
2089 (kill-region (point)
2090 ;; It is better to move point to the other end of the kill
2091 ;; before killing. That way, in a read-only buffer, point
2092 ;; moves across the text that is copied to the kill ring.
2093 ;; The choice has no effect on undo now that undo records
2094 ;; the value of point from before the command was run.
2095 (progn
2096 (if arg
2097 (forward-visible-line (prefix-numeric-value arg))
2098 (if (eobp)
2099 (signal 'end-of-buffer nil))
2100 (let ((end
2101 (save-excursion
2102 (end-of-visible-line) (point))))
2103 (if (or (save-excursion
2104 (skip-chars-forward " \t" end)
2105 (= (point) end))
2106 (and kill-whole-line (bolp)))
2107 (forward-visible-line 1)
2108 (goto-char end))))
2109 (point))))
2112 (defun forward-visible-line (arg)
2113 "Move forward by ARG lines, ignoring currently invisible newlines only.
2114 If ARG is negative, move backward -ARG lines.
2115 If ARG is zero, move to the beginning of the current line."
2116 (condition-case nil
2117 (if (> arg 0)
2118 (progn
2119 (while (> arg 0)
2120 (or (zerop (forward-line 1))
2121 (signal 'end-of-buffer nil))
2122 ;; If the newline we just skipped is invisible,
2123 ;; don't count it.
2124 (let ((prop
2125 (get-char-property (1- (point)) 'invisible)))
2126 (if (if (eq buffer-invisibility-spec t)
2127 prop
2128 (or (memq prop buffer-invisibility-spec)
2129 (assq prop buffer-invisibility-spec)))
2130 (setq arg (1+ arg))))
2131 (setq arg (1- arg)))
2132 ;; If invisible text follows, and it is a number of complete lines,
2133 ;; skip it.
2134 (let ((opoint (point)))
2135 (while (and (not (eobp))
2136 (let ((prop
2137 (get-char-property (point) 'invisible)))
2138 (if (eq buffer-invisibility-spec t)
2139 prop
2140 (or (memq prop buffer-invisibility-spec)
2141 (assq prop buffer-invisibility-spec)))))
2142 (goto-char
2143 (if (get-text-property (point) 'invisible)
2144 (or (next-single-property-change (point) 'invisible)
2145 (point-max))
2146 (next-overlay-change (point)))))
2147 (unless (bolp)
2148 (goto-char opoint))))
2149 (let ((first t))
2150 (while (or first (< arg 0))
2151 (if (zerop arg)
2152 (beginning-of-line)
2153 (or (zerop (forward-line -1))
2154 (signal 'beginning-of-buffer nil)))
2155 ;; If the newline we just moved to is invisible,
2156 ;; don't count it.
2157 (unless (bobp)
2158 (let ((prop
2159 (get-char-property (1- (point)) 'invisible)))
2160 (if (if (eq buffer-invisibility-spec t)
2161 prop
2162 (or (memq prop buffer-invisibility-spec)
2163 (assq prop buffer-invisibility-spec)))
2164 (setq arg (1+ arg)))))
2165 (setq first nil)
2166 (setq arg (1+ arg)))
2167 ;; If invisible text follows, and it is a number of complete lines,
2168 ;; skip it.
2169 (let ((opoint (point)))
2170 (while (and (not (bobp))
2171 (let ((prop
2172 (get-char-property (1- (point)) 'invisible)))
2173 (if (eq buffer-invisibility-spec t)
2174 prop
2175 (or (memq prop buffer-invisibility-spec)
2176 (assq prop buffer-invisibility-spec)))))
2177 (goto-char
2178 (if (get-text-property (1- (point)) 'invisible)
2179 (or (previous-single-property-change (point) 'invisible)
2180 (point-min))
2181 (previous-overlay-change (point)))))
2182 (unless (bolp)
2183 (goto-char opoint)))))
2184 ((beginning-of-buffer end-of-buffer)
2185 nil)))
2187 (defun end-of-visible-line ()
2188 "Move to end of current visible line."
2189 (end-of-line)
2190 ;; If the following character is currently invisible,
2191 ;; skip all characters with that same `invisible' property value,
2192 ;; then find the next newline.
2193 (while (and (not (eobp))
2194 (save-excursion
2195 (skip-chars-forward "^\n")
2196 (let ((prop
2197 (get-char-property (point) 'invisible)))
2198 (if (eq buffer-invisibility-spec t)
2199 prop
2200 (or (memq prop buffer-invisibility-spec)
2201 (assq prop buffer-invisibility-spec))))))
2202 (skip-chars-forward "^\n")
2203 (if (get-text-property (point) 'invisible)
2204 (goto-char (next-single-property-change (point) 'invisible))
2205 (goto-char (next-overlay-change (point))))
2206 (end-of-line)))
2208 (defun insert-buffer (buffer)
2209 "Insert after point the contents of BUFFER.
2210 Puts mark after the inserted text.
2211 BUFFER may be a buffer or a buffer name.
2213 This function is meant for the user to run interactively.
2214 Don't call it from programs!"
2215 (interactive
2216 (list
2217 (progn
2218 (barf-if-buffer-read-only)
2219 (read-buffer "Insert buffer: "
2220 (if (eq (selected-window) (next-window (selected-window)))
2221 (other-buffer (current-buffer))
2222 (window-buffer (next-window (selected-window))))
2223 t))))
2224 (or (bufferp buffer)
2225 (setq buffer (get-buffer buffer)))
2226 (let (start end newmark)
2227 (save-excursion
2228 (save-excursion
2229 (set-buffer buffer)
2230 (setq start (point-min) end (point-max)))
2231 (insert-buffer-substring buffer start end)
2232 (setq newmark (point)))
2233 (push-mark newmark))
2234 nil)
2236 (defun append-to-buffer (buffer start end)
2237 "Append to specified buffer the text of the region.
2238 It is inserted into that buffer before its point.
2240 When calling from a program, give three arguments:
2241 BUFFER (or buffer name), START and END.
2242 START and END specify the portion of the current buffer to be copied."
2243 (interactive
2244 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2245 (region-beginning) (region-end)))
2246 (let ((oldbuf (current-buffer)))
2247 (save-excursion
2248 (let* ((append-to (get-buffer-create buffer))
2249 (windows (get-buffer-window-list append-to t t))
2250 point)
2251 (set-buffer append-to)
2252 (setq point (point))
2253 (barf-if-buffer-read-only)
2254 (insert-buffer-substring oldbuf start end)
2255 (dolist (window windows)
2256 (when (= (window-point window) point)
2257 (set-window-point window (point))))))))
2259 (defun prepend-to-buffer (buffer start end)
2260 "Prepend to specified buffer the text of the region.
2261 It is inserted into that buffer after its point.
2263 When calling from a program, give three arguments:
2264 BUFFER (or buffer name), START and END.
2265 START and END specify the portion of the current buffer to be copied."
2266 (interactive "BPrepend to buffer: \nr")
2267 (let ((oldbuf (current-buffer)))
2268 (save-excursion
2269 (set-buffer (get-buffer-create buffer))
2270 (barf-if-buffer-read-only)
2271 (save-excursion
2272 (insert-buffer-substring oldbuf start end)))))
2274 (defun copy-to-buffer (buffer start end)
2275 "Copy to specified buffer the text of the region.
2276 It is inserted into that buffer, replacing existing text there.
2278 When calling from a program, give three arguments:
2279 BUFFER (or buffer name), START and END.
2280 START and END specify the portion of the current buffer to be copied."
2281 (interactive "BCopy to buffer: \nr")
2282 (let ((oldbuf (current-buffer)))
2283 (save-excursion
2284 (set-buffer (get-buffer-create buffer))
2285 (barf-if-buffer-read-only)
2286 (erase-buffer)
2287 (save-excursion
2288 (insert-buffer-substring oldbuf start end)))))
2290 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2291 (put 'mark-inactive 'error-message "The mark is not active now")
2293 (defun mark (&optional force)
2294 "Return this buffer's mark value as integer; error if mark inactive.
2295 If optional argument FORCE is non-nil, access the mark value
2296 even if the mark is not currently active, and return nil
2297 if there is no mark at all.
2299 If you are using this in an editing command, you are most likely making
2300 a mistake; see the documentation of `set-mark'."
2301 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2302 (marker-position (mark-marker))
2303 (signal 'mark-inactive nil)))
2305 ;; Many places set mark-active directly, and several of them failed to also
2306 ;; run deactivate-mark-hook. This shorthand should simplify.
2307 (defsubst deactivate-mark ()
2308 "Deactivate the mark by setting `mark-active' to nil.
2309 \(That makes a difference only in Transient Mark mode.)
2310 Also runs the hook `deactivate-mark-hook'."
2311 (cond
2312 ((eq transient-mark-mode 'lambda)
2313 (setq transient-mark-mode nil))
2314 (transient-mark-mode
2315 (setq mark-active nil)
2316 (run-hooks 'deactivate-mark-hook))))
2318 (defun set-mark (pos)
2319 "Set this buffer's mark to POS. Don't use this function!
2320 That is to say, don't use this function unless you want
2321 the user to see that the mark has moved, and you want the previous
2322 mark position to be lost.
2324 Normally, when a new mark is set, the old one should go on the stack.
2325 This is why most applications should use push-mark, not set-mark.
2327 Novice Emacs Lisp programmers often try to use the mark for the wrong
2328 purposes. The mark saves a location for the user's convenience.
2329 Most editing commands should not alter the mark.
2330 To remember a location for internal use in the Lisp program,
2331 store it in a Lisp variable. Example:
2333 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2335 (if pos
2336 (progn
2337 (setq mark-active t)
2338 (run-hooks 'activate-mark-hook)
2339 (set-marker (mark-marker) pos (current-buffer)))
2340 ;; Normally we never clear mark-active except in Transient Mark mode.
2341 ;; But when we actually clear out the mark value too,
2342 ;; we must clear mark-active in any mode.
2343 (setq mark-active nil)
2344 (run-hooks 'deactivate-mark-hook)
2345 (set-marker (mark-marker) nil)))
2347 (defvar mark-ring nil
2348 "The list of former marks of the current buffer, most recent first.")
2349 (make-variable-buffer-local 'mark-ring)
2350 (put 'mark-ring 'permanent-local t)
2352 (defcustom mark-ring-max 16
2353 "*Maximum size of mark ring. Start discarding off end if gets this big."
2354 :type 'integer
2355 :group 'editing-basics)
2357 (defvar global-mark-ring nil
2358 "The list of saved global marks, most recent first.")
2360 (defcustom global-mark-ring-max 16
2361 "*Maximum size of global mark ring. \
2362 Start discarding off end if gets this big."
2363 :type 'integer
2364 :group 'editing-basics)
2366 (defun pop-to-mark-command ()
2367 "Jump to mark, and pop a new position for mark off the ring
2368 \(does not affect global mark ring\)."
2369 (interactive)
2370 (if (null (mark t))
2371 (error "No mark set in this buffer")
2372 (goto-char (mark t))
2373 (pop-mark)))
2375 (defun push-mark-command (arg &optional nomsg)
2376 "Set mark at where point is.
2377 If no prefix arg and mark is already set there, just activate it.
2378 Display `Mark set' unless the optional second arg NOMSG is non-nil."
2379 (interactive "P")
2380 (let ((mark (marker-position (mark-marker))))
2381 (if (or arg (null mark) (/= mark (point)))
2382 (push-mark nil nomsg t)
2383 (setq mark-active t)
2384 (unless nomsg
2385 (message "Mark activated")))))
2387 (defun set-mark-command (arg)
2388 "Set mark at where point is, or jump to mark.
2389 With no prefix argument, set mark, push old mark position on local mark
2390 ring, and push mark on global mark ring. Immediately repeating the
2391 command activates `transient-mark-mode' temporarily.
2393 With argument, jump to mark, and pop a new position for mark off the ring
2394 \(does not affect global mark ring\). Repeating the command without
2395 an argument jumps to the next position off the mark ring.
2397 Novice Emacs Lisp programmers often try to use the mark for the wrong
2398 purposes. See the documentation of `set-mark' for more information."
2399 (interactive "P")
2400 (if (eq transient-mark-mode 'lambda)
2401 (setq transient-mark-mode nil))
2402 (cond
2403 ((not (eq this-command 'set-mark-command))
2404 (if arg
2405 (pop-to-mark-command)
2406 (push-mark-command t)))
2407 ((eq last-command 'pop-to-mark-command)
2408 (if (and (consp arg) (> (prefix-numeric-value arg) 4))
2409 (push-mark-command nil)
2410 (setq this-command 'pop-to-mark-command)
2411 (pop-to-mark-command)))
2412 (arg
2413 (setq this-command 'pop-to-mark-command)
2414 (pop-to-mark-command))
2415 ((and (eq last-command 'set-mark-command)
2416 mark-active (null transient-mark-mode))
2417 (setq transient-mark-mode 'lambda)
2418 (message "Transient-mark-mode temporarily enabled"))
2420 (push-mark-command nil))))
2422 (defun push-mark (&optional location nomsg activate)
2423 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2424 If the last global mark pushed was not in the current buffer,
2425 also push LOCATION on the global mark ring.
2426 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2427 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2429 Novice Emacs Lisp programmers often try to use the mark for the wrong
2430 purposes. See the documentation of `set-mark' for more information.
2432 In Transient Mark mode, this does not activate the mark."
2433 (if (null (mark t))
2435 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2436 (if (> (length mark-ring) mark-ring-max)
2437 (progn
2438 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2439 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
2440 (set-marker (mark-marker) (or location (point)) (current-buffer))
2441 ;; Now push the mark on the global mark ring.
2442 (if (and global-mark-ring
2443 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2444 ;; The last global mark pushed was in this same buffer.
2445 ;; Don't push another one.
2447 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2448 (if (> (length global-mark-ring) global-mark-ring-max)
2449 (progn
2450 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
2451 nil)
2452 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2453 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2454 (message "Mark set"))
2455 (if (or activate (not transient-mark-mode))
2456 (set-mark (mark t)))
2457 nil)
2459 (defun pop-mark ()
2460 "Pop off mark ring into the buffer's actual mark.
2461 Does not set point. Does nothing if mark ring is empty."
2462 (if mark-ring
2463 (progn
2464 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2465 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2466 (deactivate-mark)
2467 (move-marker (car mark-ring) nil)
2468 (if (null (mark t)) (ding))
2469 (setq mark-ring (cdr mark-ring)))))
2471 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2472 (defun exchange-point-and-mark (&optional arg)
2473 "Put the mark where point is now, and point where the mark is now.
2474 This command works even when the mark is not active,
2475 and it reactivates the mark.
2476 With prefix arg, `transient-mark-mode' is enabled temporarily."
2477 (interactive "P")
2478 (if arg
2479 (if mark-active
2480 (if (null transient-mark-mode)
2481 (setq transient-mark-mode 'lambda))
2482 (setq arg nil)))
2483 (unless arg
2484 (let ((omark (mark t)))
2485 (if (null omark)
2486 (error "No mark set in this buffer"))
2487 (set-mark (point))
2488 (goto-char omark)
2489 nil)))
2491 (define-minor-mode transient-mark-mode
2492 "Toggle Transient Mark mode.
2493 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2495 In Transient Mark mode, when the mark is active, the region is highlighted.
2496 Changing the buffer \"deactivates\" the mark.
2497 So do certain other operations that set the mark
2498 but whose main purpose is something else--for example,
2499 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
2501 You can also deactivate the mark by typing \\[keyboard-quit] or
2502 \\[keyboard-escape-quit].
2504 Many commands change their behavior when Transient Mark mode is in effect
2505 and the mark is active, by acting on the region instead of their usual
2506 default part of the buffer's text. Examples of such commands include
2507 \\[comment-dwim], \\[flush-lines], \\[ispell], \\[keep-lines],
2508 \\[query-replace], \\[query-replace-regexp], and \\[undo]. Invoke
2509 \\[apropos-documentation] and type \"transient\" or \"mark.*active\" at
2510 the prompt, to see the documentation of commands which are sensitive to
2511 the Transient Mark mode."
2512 :global t :group 'editing-basics :require nil)
2514 (defun pop-global-mark ()
2515 "Pop off global mark ring and jump to the top location."
2516 (interactive)
2517 ;; Pop entries which refer to non-existent buffers.
2518 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2519 (setq global-mark-ring (cdr global-mark-ring)))
2520 (or global-mark-ring
2521 (error "No global mark set"))
2522 (let* ((marker (car global-mark-ring))
2523 (buffer (marker-buffer marker))
2524 (position (marker-position marker)))
2525 (setq global-mark-ring (nconc (cdr global-mark-ring)
2526 (list (car global-mark-ring))))
2527 (set-buffer buffer)
2528 (or (and (>= position (point-min))
2529 (<= position (point-max)))
2530 (widen))
2531 (goto-char position)
2532 (switch-to-buffer buffer)))
2534 (defcustom next-line-add-newlines nil
2535 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2536 :type 'boolean
2537 :version "21.1"
2538 :group 'editing-basics)
2540 (defun next-line (&optional arg)
2541 "Move cursor vertically down ARG lines.
2542 If there is no character in the target line exactly under the current column,
2543 the cursor is positioned after the character in that line which spans this
2544 column, or at the end of the line if it is not long enough.
2545 If there is no line in the buffer after this one, behavior depends on the
2546 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2547 to create a line, and moves the cursor to that line. Otherwise it moves the
2548 cursor to the end of the buffer.
2550 The command \\[set-goal-column] can be used to create
2551 a semipermanent goal column for this command.
2552 Then instead of trying to move exactly vertically (or as close as possible),
2553 this command moves to the specified goal column (or as close as possible).
2554 The goal column is stored in the variable `goal-column', which is nil
2555 when there is no goal column.
2557 If you are thinking of using this in a Lisp program, consider
2558 using `forward-line' instead. It is usually easier to use
2559 and more reliable (no dependence on goal column, etc.)."
2560 (interactive "p")
2561 (unless arg (setq arg 1))
2562 (if (and next-line-add-newlines (= arg 1))
2563 (if (save-excursion (end-of-line) (eobp))
2564 ;; When adding a newline, don't expand an abbrev.
2565 (let ((abbrev-mode nil))
2566 (end-of-line)
2567 (insert "\n"))
2568 (line-move arg))
2569 (if (interactive-p)
2570 (condition-case nil
2571 (line-move arg)
2572 ((beginning-of-buffer end-of-buffer) (ding)))
2573 (line-move arg)))
2574 nil)
2576 (defun previous-line (&optional arg)
2577 "Move cursor vertically up ARG lines.
2578 If there is no character in the target line exactly over the current column,
2579 the cursor is positioned after the character in that line which spans this
2580 column, or at the end of the line if it is not long enough.
2582 The command \\[set-goal-column] can be used to create
2583 a semipermanent goal column for this command.
2584 Then instead of trying to move exactly vertically (or as close as possible),
2585 this command moves to the specified goal column (or as close as possible).
2586 The goal column is stored in the variable `goal-column', which is nil
2587 when there is no goal column.
2589 If you are thinking of using this in a Lisp program, consider using
2590 `forward-line' with a negative argument instead. It is usually easier
2591 to use and more reliable (no dependence on goal column, etc.)."
2592 (interactive "p")
2593 (unless arg (setq arg 1))
2594 (if (interactive-p)
2595 (condition-case nil
2596 (line-move (- arg))
2597 ((beginning-of-buffer end-of-buffer) (ding)))
2598 (line-move (- arg)))
2599 nil)
2601 (defcustom track-eol nil
2602 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2603 This means moving to the end of each line moved onto.
2604 The beginning of a blank line does not count as the end of a line."
2605 :type 'boolean
2606 :group 'editing-basics)
2608 (defcustom goal-column nil
2609 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2610 :type '(choice integer
2611 (const :tag "None" nil))
2612 :group 'editing-basics)
2613 (make-variable-buffer-local 'goal-column)
2615 (defvar temporary-goal-column 0
2616 "Current goal column for vertical motion.
2617 It is the column where point was
2618 at the start of current run of vertical motion commands.
2619 When the `track-eol' feature is doing its job, the value is 9999.")
2621 (defcustom line-move-ignore-invisible nil
2622 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2623 Outline mode sets this."
2624 :type 'boolean
2625 :group 'editing-basics)
2627 (defun line-move-invisible (pos)
2628 "Return non-nil if the character after POS is currently invisible."
2629 (let ((prop
2630 (get-char-property pos 'invisible)))
2631 (if (eq buffer-invisibility-spec t)
2632 prop
2633 (or (memq prop buffer-invisibility-spec)
2634 (assq prop buffer-invisibility-spec)))))
2636 ;; This is the guts of next-line and previous-line.
2637 ;; Arg says how many lines to move.
2638 (defun line-move (arg)
2639 ;; Don't run any point-motion hooks, and disregard intangibility,
2640 ;; for intermediate positions.
2641 (let ((inhibit-point-motion-hooks t)
2642 (opoint (point))
2643 new line-end line-beg)
2644 (unwind-protect
2645 (progn
2646 (if (not (memq last-command '(next-line previous-line)))
2647 (setq temporary-goal-column
2648 (if (and track-eol (eolp)
2649 ;; Don't count beg of empty line as end of line
2650 ;; unless we just did explicit end-of-line.
2651 (or (not (bolp)) (eq last-command 'end-of-line)))
2652 9999
2653 (current-column))))
2654 (if (and (not (integerp selective-display))
2655 (not line-move-ignore-invisible))
2656 ;; Use just newline characters.
2657 ;; Set ARG to 0 if we move as many lines as requested.
2658 (or (if (> arg 0)
2659 (progn (if (> arg 1) (forward-line (1- arg)))
2660 ;; This way of moving forward ARG lines
2661 ;; verifies that we have a newline after the last one.
2662 ;; It doesn't get confused by intangible text.
2663 (end-of-line)
2664 (if (zerop (forward-line 1))
2665 (setq arg 0)))
2666 (and (zerop (forward-line arg))
2667 (bolp)
2668 (setq arg 0)))
2669 (signal (if (< arg 0)
2670 'beginning-of-buffer
2671 'end-of-buffer)
2672 nil))
2673 ;; Move by arg lines, but ignore invisible ones.
2674 (while (> arg 0)
2675 ;; If the following character is currently invisible,
2676 ;; skip all characters with that same `invisible' property value.
2677 (while (and (not (eobp)) (line-move-invisible (point)))
2678 (goto-char (next-char-property-change (point))))
2679 ;; Now move a line.
2680 (end-of-line)
2681 (and (zerop (vertical-motion 1))
2682 (signal 'end-of-buffer nil))
2683 (setq arg (1- arg)))
2684 (while (< arg 0)
2685 (beginning-of-line)
2686 (and (zerop (vertical-motion -1))
2687 (signal 'beginning-of-buffer nil))
2688 (setq arg (1+ arg))
2689 (while (and (not (bobp)) (line-move-invisible (1- (point))))
2690 (goto-char (previous-char-property-change (point)))))))
2692 (cond ((> arg 0)
2693 ;; If we did not move down as far as desired,
2694 ;; at least go to end of line.
2695 (end-of-line))
2696 ((< arg 0)
2697 ;; If we did not move down as far as desired,
2698 ;; at least go to end of line.
2699 (beginning-of-line))
2701 (line-move-finish (or goal-column temporary-goal-column) opoint)))))
2702 nil)
2704 (defun line-move-finish (column opoint)
2705 (let ((repeat t))
2706 (while repeat
2707 ;; Set REPEAT to t to repeat the whole thing.
2708 (setq repeat nil)
2710 (let (new
2711 (line-beg (save-excursion (beginning-of-line) (point)))
2712 (line-end
2713 ;; Compute the end of the line
2714 ;; ignoring effectively intangible newlines.
2715 (let ((inhibit-point-motion-hooks nil)
2716 (inhibit-field-text-motion t))
2717 (save-excursion (end-of-line) (point)))))
2719 ;; Move to the desired column.
2720 (line-move-to-column column)
2721 (setq new (point))
2723 ;; Process intangibility within a line.
2724 ;; Move to the chosen destination position from above,
2725 ;; with intangibility processing enabled.
2727 (goto-char (point-min))
2728 (let ((inhibit-point-motion-hooks nil))
2729 (goto-char new)
2731 ;; If intangibility moves us to a different (later) place
2732 ;; in the same line, use that as the destination.
2733 (if (<= (point) line-end)
2734 (setq new (point))
2735 ;; If that position is "too late",
2736 ;; try the previous allowable position.
2737 ;; See if it is ok.
2738 (backward-char)
2739 (if (<= (point) line-end)
2740 (setq new (point))
2741 ;; As a last resort, use the end of the line.
2742 (setq new line-end))))
2744 ;; Now move to the updated destination, processing fields
2745 ;; as well as intangibility.
2746 (goto-char opoint)
2747 (let ((inhibit-point-motion-hooks nil))
2748 (goto-char
2749 (constrain-to-field new opoint nil t
2750 'inhibit-line-move-field-capture)))
2752 ;; If all this moved us to a different line,
2753 ;; retry everything within that new line.
2754 (when (or (< (point) line-beg) (> (point) line-end))
2755 ;; Repeat the intangibility and field processing.
2756 (setq repeat t))))))
2758 (defun line-move-to-column (col)
2759 "Try to find column COL, considering invisibility.
2760 This function works only in certain cases,
2761 because what we really need is for `move-to-column'
2762 and `current-column' to be able to ignore invisible text."
2763 (if (zerop col)
2764 (beginning-of-line)
2765 (move-to-column col))
2767 (when (and line-move-ignore-invisible
2768 (not (bolp)) (line-move-invisible (1- (point))))
2769 (let ((normal-location (point))
2770 (normal-column (current-column)))
2771 ;; If the following character is currently invisible,
2772 ;; skip all characters with that same `invisible' property value.
2773 (while (and (not (eobp))
2774 (line-move-invisible (point)))
2775 (goto-char (next-char-property-change (point))))
2776 ;; Have we advanced to a larger column position?
2777 (if (> (current-column) normal-column)
2778 ;; We have made some progress towards the desired column.
2779 ;; See if we can make any further progress.
2780 (line-move-to-column (+ (current-column) (- col normal-column)))
2781 ;; Otherwise, go to the place we originally found
2782 ;; and move back over invisible text.
2783 ;; that will get us to the same place on the screen
2784 ;; but with a more reasonable buffer position.
2785 (goto-char normal-location)
2786 (let ((line-beg (save-excursion (beginning-of-line) (point))))
2787 (while (and (not (bolp)) (line-move-invisible (1- (point))))
2788 (goto-char (previous-char-property-change (point) line-beg))))))))
2790 ;;; Many people have said they rarely use this feature, and often type
2791 ;;; it by accident. Maybe it shouldn't even be on a key.
2792 (put 'set-goal-column 'disabled t)
2794 (defun set-goal-column (arg)
2795 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2796 Those commands will move to this position in the line moved to
2797 rather than trying to keep the same horizontal position.
2798 With a non-nil argument, clears out the goal column
2799 so that \\[next-line] and \\[previous-line] resume vertical motion.
2800 The goal column is stored in the variable `goal-column'."
2801 (interactive "P")
2802 (if arg
2803 (progn
2804 (setq goal-column nil)
2805 (message "No goal column"))
2806 (setq goal-column (current-column))
2807 (message (substitute-command-keys
2808 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2809 goal-column))
2810 nil)
2813 (defun scroll-other-window-down (lines)
2814 "Scroll the \"other window\" down.
2815 For more details, see the documentation for `scroll-other-window'."
2816 (interactive "P")
2817 (scroll-other-window
2818 ;; Just invert the argument's meaning.
2819 ;; We can do that without knowing which window it will be.
2820 (if (eq lines '-) nil
2821 (if (null lines) '-
2822 (- (prefix-numeric-value lines))))))
2823 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2825 (defun beginning-of-buffer-other-window (arg)
2826 "Move point to the beginning of the buffer in the other window.
2827 Leave mark at previous position.
2828 With arg N, put point N/10 of the way from the true beginning."
2829 (interactive "P")
2830 (let ((orig-window (selected-window))
2831 (window (other-window-for-scrolling)))
2832 ;; We use unwind-protect rather than save-window-excursion
2833 ;; because the latter would preserve the things we want to change.
2834 (unwind-protect
2835 (progn
2836 (select-window window)
2837 ;; Set point and mark in that window's buffer.
2838 (beginning-of-buffer arg)
2839 ;; Set point accordingly.
2840 (recenter '(t)))
2841 (select-window orig-window))))
2843 (defun end-of-buffer-other-window (arg)
2844 "Move point to the end of the buffer in the other window.
2845 Leave mark at previous position.
2846 With arg N, put point N/10 of the way from the true end."
2847 (interactive "P")
2848 ;; See beginning-of-buffer-other-window for comments.
2849 (let ((orig-window (selected-window))
2850 (window (other-window-for-scrolling)))
2851 (unwind-protect
2852 (progn
2853 (select-window window)
2854 (end-of-buffer arg)
2855 (recenter '(t)))
2856 (select-window orig-window))))
2858 (defun transpose-chars (arg)
2859 "Interchange characters around point, moving forward one character.
2860 With prefix arg ARG, effect is to take character before point
2861 and drag it forward past ARG other characters (backward if ARG negative).
2862 If no argument and at end of line, the previous two chars are exchanged."
2863 (interactive "*P")
2864 (and (null arg) (eolp) (forward-char -1))
2865 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2867 (defun transpose-words (arg)
2868 "Interchange words around point, leaving point at end of them.
2869 With prefix arg ARG, effect is to take word before or around point
2870 and drag it forward past ARG other words (backward if ARG negative).
2871 If ARG is zero, the words around or after point and around or after mark
2872 are interchanged."
2873 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
2874 (interactive "*p")
2875 (transpose-subr 'forward-word arg))
2877 (defun transpose-sexps (arg)
2878 "Like \\[transpose-words] but applies to sexps.
2879 Does not work on a sexp that point is in the middle of
2880 if it is a list or string."
2881 (interactive "*p")
2882 (transpose-subr
2883 (lambda (arg)
2884 ;; Here we should try to simulate the behavior of
2885 ;; (cons (progn (forward-sexp x) (point))
2886 ;; (progn (forward-sexp (- x)) (point)))
2887 ;; Except that we don't want to rely on the second forward-sexp
2888 ;; putting us back to where we want to be, since forward-sexp-function
2889 ;; might do funny things like infix-precedence.
2890 (if (if (> arg 0)
2891 (looking-at "\\sw\\|\\s_")
2892 (and (not (bobp))
2893 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
2894 ;; Jumping over a symbol. We might be inside it, mind you.
2895 (progn (funcall (if (> arg 0)
2896 'skip-syntax-backward 'skip-syntax-forward)
2897 "w_")
2898 (cons (save-excursion (forward-sexp arg) (point)) (point)))
2899 ;; Otherwise, we're between sexps. Take a step back before jumping
2900 ;; to make sure we'll obey the same precedence no matter which direction
2901 ;; we're going.
2902 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
2903 (cons (save-excursion (forward-sexp arg) (point))
2904 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
2905 (not (zerop (funcall (if (> arg 0)
2906 'skip-syntax-forward
2907 'skip-syntax-backward)
2908 ".")))))
2909 (point)))))
2910 arg 'special))
2912 (defun transpose-lines (arg)
2913 "Exchange current line and previous line, leaving point after both.
2914 With argument ARG, takes previous line and moves it past ARG lines.
2915 With argument 0, interchanges line point is in with line mark is in."
2916 (interactive "*p")
2917 (transpose-subr (function
2918 (lambda (arg)
2919 (if (> arg 0)
2920 (progn
2921 ;; Move forward over ARG lines,
2922 ;; but create newlines if necessary.
2923 (setq arg (forward-line arg))
2924 (if (/= (preceding-char) ?\n)
2925 (setq arg (1+ arg)))
2926 (if (> arg 0)
2927 (newline arg)))
2928 (forward-line arg))))
2929 arg))
2931 (defun transpose-subr (mover arg &optional special)
2932 (let ((aux (if special mover
2933 (lambda (x)
2934 (cons (progn (funcall mover x) (point))
2935 (progn (funcall mover (- x)) (point))))))
2936 pos1 pos2)
2937 (cond
2938 ((= arg 0)
2939 (save-excursion
2940 (setq pos1 (funcall aux 1))
2941 (goto-char (mark))
2942 (setq pos2 (funcall aux 1))
2943 (transpose-subr-1 pos1 pos2))
2944 (exchange-point-and-mark))
2945 ((> arg 0)
2946 (setq pos1 (funcall aux -1))
2947 (setq pos2 (funcall aux arg))
2948 (transpose-subr-1 pos1 pos2)
2949 (goto-char (car pos2)))
2951 (setq pos1 (funcall aux -1))
2952 (goto-char (car pos1))
2953 (setq pos2 (funcall aux arg))
2954 (transpose-subr-1 pos1 pos2)))))
2956 (defun transpose-subr-1 (pos1 pos2)
2957 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
2958 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
2959 (when (> (car pos1) (car pos2))
2960 (let ((swap pos1))
2961 (setq pos1 pos2 pos2 swap)))
2962 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
2963 (atomic-change-group
2964 (let (word2)
2965 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
2966 (goto-char (car pos2))
2967 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
2968 (goto-char (car pos1))
2969 (insert word2))))
2971 (defun backward-word (arg)
2972 "Move backward until encountering the beginning of a word.
2973 With argument, do this that many times."
2974 (interactive "p")
2975 (forward-word (- arg)))
2977 (defun mark-word (arg)
2978 "Set mark arg words away from point.
2979 If this command is repeated, it marks the next ARG words after the ones
2980 already marked."
2981 (interactive "p")
2982 (cond ((and (eq last-command this-command) (mark t))
2983 (set-mark
2984 (save-excursion
2985 (goto-char (mark))
2986 (forward-word arg)
2987 (point))))
2989 (push-mark
2990 (save-excursion
2991 (forward-word arg)
2992 (point))
2993 nil t))))
2995 (defun kill-word (arg)
2996 "Kill characters forward until encountering the end of a word.
2997 With argument, do this that many times."
2998 (interactive "p")
2999 (kill-region (point) (progn (forward-word arg) (point))))
3001 (defun backward-kill-word (arg)
3002 "Kill characters backward until encountering the end of a word.
3003 With argument, do this that many times."
3004 (interactive "p")
3005 (kill-word (- arg)))
3007 (defun current-word (&optional strict)
3008 "Return the word point is on (or a nearby word) as a string.
3009 If optional arg STRICT is non-nil, return nil unless point is within
3010 or adjacent to a word."
3011 (save-excursion
3012 (let ((oldpoint (point)) (start (point)) (end (point)))
3013 (skip-syntax-backward "w_") (setq start (point))
3014 (goto-char oldpoint)
3015 (skip-syntax-forward "w_") (setq end (point))
3016 (if (and (eq start oldpoint) (eq end oldpoint))
3017 ;; Point is neither within nor adjacent to a word.
3018 (and (not strict)
3019 (progn
3020 ;; Look for preceding word in same line.
3021 (skip-syntax-backward "^w_"
3022 (save-excursion (beginning-of-line)
3023 (point)))
3024 (if (bolp)
3025 ;; No preceding word in same line.
3026 ;; Look for following word in same line.
3027 (progn
3028 (skip-syntax-forward "^w_"
3029 (save-excursion (end-of-line)
3030 (point)))
3031 (setq start (point))
3032 (skip-syntax-forward "w_")
3033 (setq end (point)))
3034 (setq end (point))
3035 (skip-syntax-backward "w_")
3036 (setq start (point)))
3037 (buffer-substring-no-properties start end)))
3038 (buffer-substring-no-properties start end)))))
3040 (defcustom fill-prefix nil
3041 "*String for filling to insert at front of new line, or nil for none."
3042 :type '(choice (const :tag "None" nil)
3043 string)
3044 :group 'fill)
3045 (make-variable-buffer-local 'fill-prefix)
3047 (defcustom auto-fill-inhibit-regexp nil
3048 "*Regexp to match lines which should not be auto-filled."
3049 :type '(choice (const :tag "None" nil)
3050 regexp)
3051 :group 'fill)
3053 (defvar comment-line-break-function 'comment-indent-new-line
3054 "*Mode-specific function which line breaks and continues a comment.
3056 This function is only called during auto-filling of a comment section.
3057 The function should take a single optional argument, which is a flag
3058 indicating whether it should use soft newlines.
3060 Setting this variable automatically makes it local to the current buffer.")
3062 ;; This function is used as the auto-fill-function of a buffer
3063 ;; when Auto-Fill mode is enabled.
3064 ;; It returns t if it really did any work.
3065 ;; (Actually some major modes use a different auto-fill function,
3066 ;; but this one is the default one.)
3067 (defun do-auto-fill ()
3068 (let (fc justify bol give-up
3069 (fill-prefix fill-prefix))
3070 (if (or (not (setq justify (current-justification)))
3071 (null (setq fc (current-fill-column)))
3072 (and (eq justify 'left)
3073 (<= (current-column) fc))
3074 (save-excursion (beginning-of-line)
3075 (setq bol (point))
3076 (and auto-fill-inhibit-regexp
3077 (looking-at auto-fill-inhibit-regexp))))
3078 nil ;; Auto-filling not required
3079 (if (memq justify '(full center right))
3080 (save-excursion (unjustify-current-line)))
3082 ;; Choose a fill-prefix automatically.
3083 (when (and adaptive-fill-mode
3084 (or (null fill-prefix) (string= fill-prefix "")))
3085 (let ((prefix
3086 (fill-context-prefix
3087 (save-excursion (backward-paragraph 1) (point))
3088 (save-excursion (forward-paragraph 1) (point)))))
3089 (and prefix (not (equal prefix ""))
3090 ;; Use auto-indentation rather than a guessed empty prefix.
3091 (not (and fill-indent-according-to-mode
3092 (string-match "\\`[ \t]*\\'" prefix)))
3093 (setq fill-prefix prefix))))
3095 (while (and (not give-up) (> (current-column) fc))
3096 ;; Determine where to split the line.
3097 (let* (after-prefix
3098 (fill-point
3099 (let ((opoint (point)))
3100 (save-excursion
3101 (beginning-of-line)
3102 (setq after-prefix (point))
3103 (and fill-prefix
3104 (looking-at (regexp-quote fill-prefix))
3105 (setq after-prefix (match-end 0)))
3106 (move-to-column (1+ fc))
3107 (fill-move-to-break-point after-prefix)
3108 (point)))))
3110 ;; See whether the place we found is any good.
3111 (if (save-excursion
3112 (goto-char fill-point)
3113 (or (bolp)
3114 ;; There is no use breaking at end of line.
3115 (save-excursion (skip-chars-forward " ") (eolp))
3116 ;; It is futile to split at the end of the prefix
3117 ;; since we would just insert the prefix again.
3118 (and after-prefix (<= (point) after-prefix))
3119 ;; Don't split right after a comment starter
3120 ;; since we would just make another comment starter.
3121 (and comment-start-skip
3122 (let ((limit (point)))
3123 (beginning-of-line)
3124 (and (re-search-forward comment-start-skip
3125 limit t)
3126 (eq (point) limit))))))
3127 ;; No good place to break => stop trying.
3128 (setq give-up t)
3129 ;; Ok, we have a useful place to break the line. Do it.
3130 (let ((prev-column (current-column)))
3131 ;; If point is at the fill-point, do not `save-excursion'.
3132 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3133 ;; point will end up before it rather than after it.
3134 (if (save-excursion
3135 (skip-chars-backward " \t")
3136 (= (point) fill-point))
3137 (funcall comment-line-break-function t)
3138 (save-excursion
3139 (goto-char fill-point)
3140 (funcall comment-line-break-function t)))
3141 ;; Now do justification, if required
3142 (if (not (eq justify 'left))
3143 (save-excursion
3144 (end-of-line 0)
3145 (justify-current-line justify nil t)))
3146 ;; If making the new line didn't reduce the hpos of
3147 ;; the end of the line, then give up now;
3148 ;; trying again will not help.
3149 (if (>= (current-column) prev-column)
3150 (setq give-up t))))))
3151 ;; Justify last line.
3152 (justify-current-line justify t t)
3153 t)))
3155 (defvar normal-auto-fill-function 'do-auto-fill
3156 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3157 Some major modes set this.")
3159 ;; FIXME: turn into a proper minor mode.
3160 ;; Add a global minor mode version of it.
3161 (defun auto-fill-mode (&optional arg)
3162 "Toggle Auto Fill mode.
3163 With arg, turn Auto Fill mode on if and only if arg is positive.
3164 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3165 automatically breaks the line at a previous space.
3167 The value of `normal-auto-fill-function' specifies the function to use
3168 for `auto-fill-function' when turning Auto Fill mode on."
3169 (interactive "P")
3170 (prog1 (setq auto-fill-function
3171 (if (if (null arg)
3172 (not auto-fill-function)
3173 (> (prefix-numeric-value arg) 0))
3174 normal-auto-fill-function
3175 nil))
3176 (force-mode-line-update)))
3178 ;; This holds a document string used to document auto-fill-mode.
3179 (defun auto-fill-function ()
3180 "Automatically break line at a previous space, in insertion of text."
3181 nil)
3183 (defun turn-on-auto-fill ()
3184 "Unconditionally turn on Auto Fill mode."
3185 (auto-fill-mode 1))
3187 (defun turn-off-auto-fill ()
3188 "Unconditionally turn off Auto Fill mode."
3189 (auto-fill-mode -1))
3191 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3193 (defun set-fill-column (arg)
3194 "Set `fill-column' to specified argument.
3195 Use \\[universal-argument] followed by a number to specify a column.
3196 Just \\[universal-argument] as argument means to use the current column."
3197 (interactive "P")
3198 (if (consp arg)
3199 (setq arg (current-column)))
3200 (if (not (integerp arg))
3201 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3202 (error "set-fill-column requires an explicit argument")
3203 (message "Fill column set to %d (was %d)" arg fill-column)
3204 (setq fill-column arg)))
3206 (defun set-selective-display (arg)
3207 "Set `selective-display' to ARG; clear it if no arg.
3208 When the value of `selective-display' is a number > 0,
3209 lines whose indentation is >= that value are not displayed.
3210 The variable `selective-display' has a separate value for each buffer."
3211 (interactive "P")
3212 (if (eq selective-display t)
3213 (error "selective-display already in use for marked lines"))
3214 (let ((current-vpos
3215 (save-restriction
3216 (narrow-to-region (point-min) (point))
3217 (goto-char (window-start))
3218 (vertical-motion (window-height)))))
3219 (setq selective-display
3220 (and arg (prefix-numeric-value arg)))
3221 (recenter current-vpos))
3222 (set-window-start (selected-window) (window-start (selected-window)))
3223 (princ "selective-display set to " t)
3224 (prin1 selective-display t)
3225 (princ "." t))
3227 (defun toggle-truncate-lines (arg)
3228 "Toggle whether to fold or truncate long lines on the screen.
3229 With arg, truncate long lines iff arg is positive.
3230 Note that in side-by-side windows, truncation is always enabled."
3231 (interactive "P")
3232 (setq truncate-lines
3233 (if (null arg)
3234 (not truncate-lines)
3235 (> (prefix-numeric-value arg) 0)))
3236 (force-mode-line-update)
3237 (message "Truncate long lines %s"
3238 (if truncate-lines "enabled" "disabled")))
3240 (defvar overwrite-mode-textual " Ovwrt"
3241 "The string displayed in the mode line when in overwrite mode.")
3242 (defvar overwrite-mode-binary " Bin Ovwrt"
3243 "The string displayed in the mode line when in binary overwrite mode.")
3245 (defun overwrite-mode (arg)
3246 "Toggle overwrite mode.
3247 With arg, turn overwrite mode on iff arg is positive.
3248 In overwrite mode, printing characters typed in replace existing text
3249 on a one-for-one basis, rather than pushing it to the right. At the
3250 end of a line, such characters extend the line. Before a tab,
3251 such characters insert until the tab is filled in.
3252 \\[quoted-insert] still inserts characters in overwrite mode; this
3253 is supposed to make it easier to insert characters when necessary."
3254 (interactive "P")
3255 (setq overwrite-mode
3256 (if (if (null arg) (not overwrite-mode)
3257 (> (prefix-numeric-value arg) 0))
3258 'overwrite-mode-textual))
3259 (force-mode-line-update))
3261 (defun binary-overwrite-mode (arg)
3262 "Toggle binary overwrite mode.
3263 With arg, turn binary overwrite mode on iff arg is positive.
3264 In binary overwrite mode, printing characters typed in replace
3265 existing text. Newlines are not treated specially, so typing at the
3266 end of a line joins the line to the next, with the typed character
3267 between them. Typing before a tab character simply replaces the tab
3268 with the character typed.
3269 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3270 typing characters do.
3272 Note that binary overwrite mode is not its own minor mode; it is a
3273 specialization of overwrite-mode, entered by setting the
3274 `overwrite-mode' variable to `overwrite-mode-binary'."
3275 (interactive "P")
3276 (setq overwrite-mode
3277 (if (if (null arg)
3278 (not (eq overwrite-mode 'overwrite-mode-binary))
3279 (> (prefix-numeric-value arg) 0))
3280 'overwrite-mode-binary))
3281 (force-mode-line-update))
3283 (define-minor-mode line-number-mode
3284 "Toggle Line Number mode.
3285 With arg, turn Line Number mode on iff arg is positive.
3286 When Line Number mode is enabled, the line number appears
3287 in the mode line.
3289 Line numbers do not appear for very large buffers and buffers
3290 with very long lines; see variables `line-number-display-limit'
3291 and `line-number-display-limit-width'."
3292 :init-value t :global t :group 'editing-basics :require nil)
3294 (define-minor-mode column-number-mode
3295 "Toggle Column Number mode.
3296 With arg, turn Column Number mode on iff arg is positive.
3297 When Column Number mode is enabled, the column number appears
3298 in the mode line."
3299 :global t :group 'editing-basics :require nil)
3301 (defgroup paren-blinking nil
3302 "Blinking matching of parens and expressions."
3303 :prefix "blink-matching-"
3304 :group 'paren-matching)
3306 (defcustom blink-matching-paren t
3307 "*Non-nil means show matching open-paren when close-paren is inserted."
3308 :type 'boolean
3309 :group 'paren-blinking)
3311 (defcustom blink-matching-paren-on-screen t
3312 "*Non-nil means show matching open-paren when it is on screen.
3313 If nil, means don't show it (but the open-paren can still be shown
3314 when it is off screen)."
3315 :type 'boolean
3316 :group 'paren-blinking)
3318 (defcustom blink-matching-paren-distance (* 25 1024)
3319 "*If non-nil, is maximum distance to search for matching open-paren."
3320 :type 'integer
3321 :group 'paren-blinking)
3323 (defcustom blink-matching-delay 1
3324 "*Time in seconds to delay after showing a matching paren."
3325 :type 'number
3326 :group 'paren-blinking)
3328 (defcustom blink-matching-paren-dont-ignore-comments nil
3329 "*Non-nil means `blink-matching-paren' will not ignore comments."
3330 :type 'boolean
3331 :group 'paren-blinking)
3333 (defun blink-matching-open ()
3334 "Move cursor momentarily to the beginning of the sexp before point."
3335 (interactive)
3336 (and (> (point) (1+ (point-min)))
3337 blink-matching-paren
3338 ;; Verify an even number of quoting characters precede the close.
3339 (= 1 (logand 1 (- (point)
3340 (save-excursion
3341 (forward-char -1)
3342 (skip-syntax-backward "/\\")
3343 (point)))))
3344 (let* ((oldpos (point))
3345 (blinkpos)
3346 (mismatch))
3347 (save-excursion
3348 (save-restriction
3349 (if blink-matching-paren-distance
3350 (narrow-to-region (max (point-min)
3351 (- (point) blink-matching-paren-distance))
3352 oldpos))
3353 (condition-case ()
3354 (let ((parse-sexp-ignore-comments
3355 (and parse-sexp-ignore-comments
3356 (not blink-matching-paren-dont-ignore-comments))))
3357 (setq blinkpos (scan-sexps oldpos -1)))
3358 (error nil)))
3359 (and blinkpos
3360 (/= (char-syntax (char-after blinkpos))
3361 ?\$)
3362 (setq mismatch
3363 (or (null (matching-paren (char-after blinkpos)))
3364 (/= (char-after (1- oldpos))
3365 (matching-paren (char-after blinkpos))))))
3366 (if mismatch (setq blinkpos nil))
3367 (if blinkpos
3368 ;; Don't log messages about paren matching.
3369 (let (message-log-max)
3370 (goto-char blinkpos)
3371 (if (pos-visible-in-window-p)
3372 (and blink-matching-paren-on-screen
3373 (sit-for blink-matching-delay))
3374 (goto-char blinkpos)
3375 (message
3376 "Matches %s"
3377 ;; Show what precedes the open in its line, if anything.
3378 (if (save-excursion
3379 (skip-chars-backward " \t")
3380 (not (bolp)))
3381 (buffer-substring (progn (beginning-of-line) (point))
3382 (1+ blinkpos))
3383 ;; Show what follows the open in its line, if anything.
3384 (if (save-excursion
3385 (forward-char 1)
3386 (skip-chars-forward " \t")
3387 (not (eolp)))
3388 (buffer-substring blinkpos
3389 (progn (end-of-line) (point)))
3390 ;; Otherwise show the previous nonblank line,
3391 ;; if there is one.
3392 (if (save-excursion
3393 (skip-chars-backward "\n \t")
3394 (not (bobp)))
3395 (concat
3396 (buffer-substring (progn
3397 (skip-chars-backward "\n \t")
3398 (beginning-of-line)
3399 (point))
3400 (progn (end-of-line)
3401 (skip-chars-backward " \t")
3402 (point)))
3403 ;; Replace the newline and other whitespace with `...'.
3404 "..."
3405 (buffer-substring blinkpos (1+ blinkpos)))
3406 ;; There is nothing to show except the char itself.
3407 (buffer-substring blinkpos (1+ blinkpos))))))))
3408 (cond (mismatch
3409 (message "Mismatched parentheses"))
3410 ((not blink-matching-paren-distance)
3411 (message "Unmatched parenthesis"))))))))
3413 ;Turned off because it makes dbx bomb out.
3414 (setq blink-paren-function 'blink-matching-open)
3416 ;; This executes C-g typed while Emacs is waiting for a command.
3417 ;; Quitting out of a program does not go through here;
3418 ;; that happens in the QUIT macro at the C code level.
3419 (defun keyboard-quit ()
3420 "Signal a `quit' condition.
3421 During execution of Lisp code, this character causes a quit directly.
3422 At top-level, as an editor command, this simply beeps."
3423 (interactive)
3424 (deactivate-mark)
3425 (setq defining-kbd-macro nil)
3426 (signal 'quit nil))
3428 (define-key global-map "\C-g" 'keyboard-quit)
3430 (defvar buffer-quit-function nil
3431 "Function to call to \"quit\" the current buffer, or nil if none.
3432 \\[keyboard-escape-quit] calls this function when its more local actions
3433 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3435 (defun keyboard-escape-quit ()
3436 "Exit the current \"mode\" (in a generalized sense of the word).
3437 This command can exit an interactive command such as `query-replace',
3438 can clear out a prefix argument or a region,
3439 can get out of the minibuffer or other recursive edit,
3440 cancel the use of the current buffer (for special-purpose buffers),
3441 or go back to just one window (by deleting all but the selected window)."
3442 (interactive)
3443 (cond ((eq last-command 'mode-exited) nil)
3444 ((> (minibuffer-depth) 0)
3445 (abort-recursive-edit))
3446 (current-prefix-arg
3447 nil)
3448 ((and transient-mark-mode
3449 mark-active)
3450 (deactivate-mark))
3451 ((> (recursion-depth) 0)
3452 (exit-recursive-edit))
3453 (buffer-quit-function
3454 (funcall buffer-quit-function))
3455 ((not (one-window-p t))
3456 (delete-other-windows))
3457 ((string-match "^ \\*" (buffer-name (current-buffer)))
3458 (bury-buffer))))
3460 (defun play-sound-file (file &optional volume device)
3461 "Play sound stored in FILE.
3462 VOLUME and DEVICE correspond to the keywords of the sound
3463 specification for `play-sound'."
3464 (interactive "fPlay sound file: ")
3465 (let ((sound (list :file file)))
3466 (if volume
3467 (plist-put sound :volume volume))
3468 (if device
3469 (plist-put sound :device device))
3470 (push 'sound sound)
3471 (play-sound sound)))
3473 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3475 (defcustom read-mail-command 'rmail
3476 "*Your preference for a mail reading package.
3477 This is used by some keybindings which support reading mail.
3478 See also `mail-user-agent' concerning sending mail."
3479 :type '(choice (function-item rmail)
3480 (function-item gnus)
3481 (function-item mh-rmail)
3482 (function :tag "Other"))
3483 :version "21.1"
3484 :group 'mail)
3486 (defcustom mail-user-agent 'sendmail-user-agent
3487 "*Your preference for a mail composition package.
3488 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3489 outgoing email message. This variable lets you specify which
3490 mail-sending package you prefer.
3492 Valid values include:
3494 `sendmail-user-agent' -- use the default Emacs Mail package.
3495 See Info node `(emacs)Sending Mail'.
3496 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3497 See Info node `(mh-e)'.
3498 `message-user-agent' -- use the Gnus Message package.
3499 See Info node `(message)'.
3500 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3501 paraphernalia, particularly the Gcc: header for
3502 archiving.
3504 Additional valid symbols may be available; check with the author of
3505 your package for details. The function should return non-nil if it
3506 succeeds.
3508 See also `read-mail-command' concerning reading mail."
3509 :type '(radio (function-item :tag "Default Emacs mail"
3510 :format "%t\n"
3511 sendmail-user-agent)
3512 (function-item :tag "Emacs interface to MH"
3513 :format "%t\n"
3514 mh-e-user-agent)
3515 (function-item :tag "Gnus Message package"
3516 :format "%t\n"
3517 message-user-agent)
3518 (function-item :tag "Gnus Message with full Gnus features"
3519 :format "%t\n"
3520 gnus-user-agent)
3521 (function :tag "Other"))
3522 :group 'mail)
3524 (define-mail-user-agent 'sendmail-user-agent
3525 'sendmail-user-agent-compose
3526 'mail-send-and-exit)
3528 (defun rfc822-goto-eoh ()
3529 ;; Go to header delimiter line in a mail message, following RFC822 rules
3530 (goto-char (point-min))
3531 (when (re-search-forward
3532 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
3533 (goto-char (match-beginning 0))))
3535 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3536 switch-function yank-action
3537 send-actions)
3538 (if switch-function
3539 (let ((special-display-buffer-names nil)
3540 (special-display-regexps nil)
3541 (same-window-buffer-names nil)
3542 (same-window-regexps nil))
3543 (funcall switch-function "*mail*")))
3544 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3545 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers)))
3546 (body (cdr (assoc-ignore-case "body" other-headers))))
3547 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3548 continue
3549 (error "Message aborted"))
3550 (save-excursion
3551 (rfc822-goto-eoh)
3552 (while other-headers
3553 (unless (member-ignore-case (car (car other-headers))
3554 '("in-reply-to" "cc" "body"))
3555 (insert (car (car other-headers)) ": "
3556 (cdr (car other-headers)) "\n"))
3557 (setq other-headers (cdr other-headers)))
3558 (when body
3559 (forward-line 1)
3560 (insert body))
3561 t)))
3563 (define-mail-user-agent 'mh-e-user-agent
3564 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3565 'mh-before-send-letter-hook)
3567 (defun compose-mail (&optional to subject other-headers continue
3568 switch-function yank-action send-actions)
3569 "Start composing a mail message to send.
3570 This uses the user's chosen mail composition package
3571 as selected with the variable `mail-user-agent'.
3572 The optional arguments TO and SUBJECT specify recipients
3573 and the initial Subject field, respectively.
3575 OTHER-HEADERS is an alist specifying additional
3576 header fields. Elements look like (HEADER . VALUE) where both
3577 HEADER and VALUE are strings.
3579 CONTINUE, if non-nil, says to continue editing a message already
3580 being composed.
3582 SWITCH-FUNCTION, if non-nil, is a function to use to
3583 switch to and display the buffer used for mail composition.
3585 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3586 to insert the raw text of the message being replied to.
3587 It has the form (FUNCTION . ARGS). The user agent will apply
3588 FUNCTION to ARGS, to insert the raw text of the original message.
3589 \(The user agent will also run `mail-citation-hook', *after* the
3590 original text has been inserted in this way.)
3592 SEND-ACTIONS is a list of actions to call when the message is sent.
3593 Each action has the form (FUNCTION . ARGS)."
3594 (interactive
3595 (list nil nil nil current-prefix-arg))
3596 (let ((function (get mail-user-agent 'composefunc)))
3597 (funcall function to subject other-headers continue
3598 switch-function yank-action send-actions)))
3600 (defun compose-mail-other-window (&optional to subject other-headers continue
3601 yank-action send-actions)
3602 "Like \\[compose-mail], but edit the outgoing message in another window."
3603 (interactive
3604 (list nil nil nil current-prefix-arg))
3605 (compose-mail to subject other-headers continue
3606 'switch-to-buffer-other-window yank-action send-actions))
3609 (defun compose-mail-other-frame (&optional to subject other-headers continue
3610 yank-action send-actions)
3611 "Like \\[compose-mail], but edit the outgoing message in another frame."
3612 (interactive
3613 (list nil nil nil current-prefix-arg))
3614 (compose-mail to subject other-headers continue
3615 'switch-to-buffer-other-frame yank-action send-actions))
3617 (defvar set-variable-value-history nil
3618 "History of values entered with `set-variable'.")
3620 (defun set-variable (var val &optional make-local)
3621 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3622 When using this interactively, enter a Lisp object for VALUE.
3623 If you want VALUE to be a string, you must surround it with doublequotes.
3624 VALUE is used literally, not evaluated.
3626 If VARIABLE has a `variable-interactive' property, that is used as if
3627 it were the arg to `interactive' (which see) to interactively read VALUE.
3629 If VARIABLE has been defined with `defcustom', then the type information
3630 in the definition is used to check that VALUE is valid.
3632 With a prefix argument, set VARIABLE to VALUE buffer-locally."
3633 (interactive
3634 (let* ((default-var (variable-at-point))
3635 (var (if (symbolp default-var)
3636 (read-variable (format "Set variable (default %s): " default-var)
3637 default-var)
3638 (read-variable "Set variable: ")))
3639 (minibuffer-help-form '(describe-variable var))
3640 (prop (get var 'variable-interactive))
3641 (prompt (format "Set %s%s to value: " var
3642 (cond ((local-variable-p var)
3643 " (buffer-local)")
3644 ((or current-prefix-arg
3645 (local-variable-if-set-p var))
3646 " buffer-locally")
3647 (t " globally"))))
3648 (val (if prop
3649 ;; Use VAR's `variable-interactive' property
3650 ;; as an interactive spec for prompting.
3651 (call-interactively `(lambda (arg)
3652 (interactive ,prop)
3653 arg))
3654 (read
3655 (read-string prompt nil
3656 'set-variable-value-history)))))
3657 (list var val current-prefix-arg)))
3659 (let ((type (get var 'custom-type)))
3660 (when type
3661 ;; Match with custom type.
3662 (require 'cus-edit)
3663 (setq type (widget-convert type))
3664 (unless (widget-apply type :match val)
3665 (error "Value `%S' does not match type %S of %S"
3666 val (car type) var))))
3668 (if make-local
3669 (make-local-variable var))
3671 (set var val)
3673 ;; Force a thorough redisplay for the case that the variable
3674 ;; has an effect on the display, like `tab-width' has.
3675 (force-mode-line-update))
3677 ;; Define the major mode for lists of completions.
3679 (defvar completion-list-mode-map nil
3680 "Local map for completion list buffers.")
3681 (or completion-list-mode-map
3682 (let ((map (make-sparse-keymap)))
3683 (define-key map [mouse-2] 'mouse-choose-completion)
3684 (define-key map [down-mouse-2] nil)
3685 (define-key map "\C-m" 'choose-completion)
3686 (define-key map "\e\e\e" 'delete-completion-window)
3687 (define-key map [left] 'previous-completion)
3688 (define-key map [right] 'next-completion)
3689 (setq completion-list-mode-map map)))
3691 ;; Completion mode is suitable only for specially formatted data.
3692 (put 'completion-list-mode 'mode-class 'special)
3694 (defvar completion-reference-buffer nil
3695 "Record the buffer that was current when the completion list was requested.
3696 This is a local variable in the completion list buffer.
3697 Initial value is nil to avoid some compiler warnings.")
3699 (defvar completion-no-auto-exit nil
3700 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3701 This also applies to other functions such as `choose-completion'
3702 and `mouse-choose-completion'.")
3704 (defvar completion-base-size nil
3705 "Number of chars at beginning of minibuffer not involved in completion.
3706 This is a local variable in the completion list buffer
3707 but it talks about the buffer in `completion-reference-buffer'.
3708 If this is nil, it means to compare text to determine which part
3709 of the tail end of the buffer's text is involved in completion.")
3711 (defun delete-completion-window ()
3712 "Delete the completion list window.
3713 Go to the window from which completion was requested."
3714 (interactive)
3715 (let ((buf completion-reference-buffer))
3716 (if (one-window-p t)
3717 (if (window-dedicated-p (selected-window))
3718 (delete-frame (selected-frame)))
3719 (delete-window (selected-window))
3720 (if (get-buffer-window buf)
3721 (select-window (get-buffer-window buf))))))
3723 (defun previous-completion (n)
3724 "Move to the previous item in the completion list."
3725 (interactive "p")
3726 (next-completion (- n)))
3728 (defun next-completion (n)
3729 "Move to the next item in the completion list.
3730 With prefix argument N, move N items (negative N means move backward)."
3731 (interactive "p")
3732 (let ((beg (point-min)) (end (point-max)))
3733 (while (and (> n 0) (not (eobp)))
3734 ;; If in a completion, move to the end of it.
3735 (when (get-text-property (point) 'mouse-face)
3736 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3737 ;; Move to start of next one.
3738 (unless (get-text-property (point) 'mouse-face)
3739 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3740 (setq n (1- n)))
3741 (while (and (< n 0) (not (bobp)))
3742 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
3743 ;; If in a completion, move to the start of it.
3744 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
3745 (goto-char (previous-single-property-change
3746 (point) 'mouse-face nil beg)))
3747 ;; Move to end of the previous completion.
3748 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
3749 (goto-char (previous-single-property-change
3750 (point) 'mouse-face nil beg)))
3751 ;; Move to the start of that one.
3752 (goto-char (previous-single-property-change
3753 (point) 'mouse-face nil beg))
3754 (setq n (1+ n))))))
3756 (defun choose-completion ()
3757 "Choose the completion that point is in or next to."
3758 (interactive)
3759 (let (beg end completion (buffer completion-reference-buffer)
3760 (base-size completion-base-size))
3761 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3762 (setq end (point) beg (1+ (point))))
3763 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3764 (setq end (1- (point)) beg (point)))
3765 (if (null beg)
3766 (error "No completion here"))
3767 (setq beg (previous-single-property-change beg 'mouse-face))
3768 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3769 (setq completion (buffer-substring beg end))
3770 (let ((owindow (selected-window)))
3771 (if (and (one-window-p t 'selected-frame)
3772 (window-dedicated-p (selected-window)))
3773 ;; This is a special buffer's frame
3774 (iconify-frame (selected-frame))
3775 (or (window-dedicated-p (selected-window))
3776 (bury-buffer)))
3777 (select-window owindow))
3778 (choose-completion-string completion buffer base-size)))
3780 ;; Delete the longest partial match for STRING
3781 ;; that can be found before POINT.
3782 (defun choose-completion-delete-max-match (string)
3783 (let ((opoint (point))
3784 len)
3785 ;; Try moving back by the length of the string.
3786 (goto-char (max (- (point) (length string))
3787 (minibuffer-prompt-end)))
3788 ;; See how far back we were actually able to move. That is the
3789 ;; upper bound on how much we can match and delete.
3790 (setq len (- opoint (point)))
3791 (if completion-ignore-case
3792 (setq string (downcase string)))
3793 (while (and (> len 0)
3794 (let ((tail (buffer-substring (point) opoint)))
3795 (if completion-ignore-case
3796 (setq tail (downcase tail)))
3797 (not (string= tail (substring string 0 len)))))
3798 (setq len (1- len))
3799 (forward-char 1))
3800 (delete-char len)))
3802 (defvar choose-completion-string-functions nil
3803 "Functions that may override the normal insertion of a completion choice.
3804 These functions are called in order with four arguments:
3805 CHOICE - the string to insert in the buffer,
3806 BUFFER - the buffer in which the choice should be inserted,
3807 MINI-P - non-nil iff BUFFER is a minibuffer, and
3808 BASE-SIZE - the number of characters in BUFFER before
3809 the string being completed.
3811 If a function in the list returns non-nil, that function is supposed
3812 to have inserted the CHOICE in the BUFFER, and possibly exited
3813 the minibuffer; no further functions will be called.
3815 If all functions in the list return nil, that means to use
3816 the default method of inserting the completion in BUFFER.")
3818 (defun choose-completion-string (choice &optional buffer base-size)
3819 "Switch to BUFFER and insert the completion choice CHOICE.
3820 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3821 to keep. If it is nil, we call `choose-completion-delete-max-match'
3822 to decide what to delete."
3824 ;; If BUFFER is the minibuffer, exit the minibuffer
3825 ;; unless it is reading a file name and CHOICE is a directory,
3826 ;; or completion-no-auto-exit is non-nil.
3828 (let ((buffer (or buffer completion-reference-buffer))
3829 (mini-p (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3830 (buffer-name buffer))))
3831 ;; If BUFFER is a minibuffer, barf unless it's the currently
3832 ;; active minibuffer.
3833 (if (and mini-p
3834 (or (not (active-minibuffer-window))
3835 (not (equal buffer
3836 (window-buffer (active-minibuffer-window))))))
3837 (error "Minibuffer is not active for completion")
3838 (unless (run-hook-with-args-until-success
3839 'choose-completion-string-functions
3840 choice buffer mini-p base-size)
3841 ;; Insert the completion into the buffer where it was requested.
3842 (set-buffer buffer)
3843 (if base-size
3844 (delete-region (+ base-size (if mini-p
3845 (minibuffer-prompt-end)
3846 (point-min)))
3847 (point))
3848 (choose-completion-delete-max-match choice))
3849 (insert choice)
3850 (remove-text-properties (- (point) (length choice)) (point)
3851 '(mouse-face nil))
3852 ;; Update point in the window that BUFFER is showing in.
3853 (let ((window (get-buffer-window buffer t)))
3854 (set-window-point window (point)))
3855 ;; If completing for the minibuffer, exit it with this choice.
3856 (and (not completion-no-auto-exit)
3857 (equal buffer (window-buffer (minibuffer-window)))
3858 minibuffer-completion-table
3859 ;; If this is reading a file name, and the file name chosen
3860 ;; is a directory, don't exit the minibuffer.
3861 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3862 (file-directory-p (field-string (point-max))))
3863 (let ((mini (active-minibuffer-window)))
3864 (select-window mini)
3865 (when minibuffer-auto-raise
3866 (raise-frame (window-frame mini))))
3867 (exit-minibuffer)))))))
3869 (defun completion-list-mode ()
3870 "Major mode for buffers showing lists of possible completions.
3871 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3872 to select the completion near point.
3873 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3874 with the mouse."
3875 (interactive)
3876 (kill-all-local-variables)
3877 (use-local-map completion-list-mode-map)
3878 (setq mode-name "Completion List")
3879 (setq major-mode 'completion-list-mode)
3880 (make-local-variable 'completion-base-size)
3881 (setq completion-base-size nil)
3882 (run-hooks 'completion-list-mode-hook))
3884 (defun completion-list-mode-finish ()
3885 "Finish setup of the completions buffer.
3886 Called from `temp-buffer-show-hook'."
3887 (when (eq major-mode 'completion-list-mode)
3888 (toggle-read-only 1)))
3890 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
3892 (defvar completion-setup-hook nil
3893 "Normal hook run at the end of setting up a completion list buffer.
3894 When this hook is run, the current buffer is the one in which the
3895 command to display the completion list buffer was run.
3896 The completion list buffer is available as the value of `standard-output'.")
3898 ;; This function goes in completion-setup-hook, so that it is called
3899 ;; after the text of the completion list buffer is written.
3901 (defun completion-setup-function ()
3902 (save-excursion
3903 (let ((mainbuf (current-buffer)))
3904 (set-buffer standard-output)
3905 (completion-list-mode)
3906 (make-local-variable 'completion-reference-buffer)
3907 (setq completion-reference-buffer mainbuf)
3908 (if (eq minibuffer-completion-table 'read-file-name-internal)
3909 ;; For file name completion,
3910 ;; use the number of chars before the start of the
3911 ;; last file name component.
3912 (setq completion-base-size
3913 (save-excursion
3914 (set-buffer mainbuf)
3915 (goto-char (point-max))
3916 (skip-chars-backward "^/")
3917 (- (point) (minibuffer-prompt-end))))
3918 ;; Otherwise, in minibuffer, the whole input is being completed.
3919 (save-match-data
3920 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3921 (buffer-name mainbuf))
3922 (setq completion-base-size 0))))
3923 (goto-char (point-min))
3924 (if (display-mouse-p)
3925 (insert (substitute-command-keys
3926 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3927 (insert (substitute-command-keys
3928 "In this buffer, type \\[choose-completion] to \
3929 select the completion near point.\n\n")))))
3931 (add-hook 'completion-setup-hook 'completion-setup-function)
3933 (define-key minibuffer-local-completion-map [prior]
3934 'switch-to-completions)
3935 (define-key minibuffer-local-must-match-map [prior]
3936 'switch-to-completions)
3937 (define-key minibuffer-local-completion-map "\M-v"
3938 'switch-to-completions)
3939 (define-key minibuffer-local-must-match-map "\M-v"
3940 'switch-to-completions)
3942 (defun switch-to-completions ()
3943 "Select the completion list window."
3944 (interactive)
3945 ;; Make sure we have a completions window.
3946 (or (get-buffer-window "*Completions*")
3947 (minibuffer-completion-help))
3948 (let ((window (get-buffer-window "*Completions*")))
3949 (when window
3950 (select-window window)
3951 (goto-char (point-min))
3952 (search-forward "\n\n")
3953 (forward-line 1))))
3955 ;; Support keyboard commands to turn on various modifiers.
3957 ;; These functions -- which are not commands -- each add one modifier
3958 ;; to the following event.
3960 (defun event-apply-alt-modifier (ignore-prompt)
3961 "Add the Alt modifier to the following event.
3962 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
3963 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3964 (defun event-apply-super-modifier (ignore-prompt)
3965 "Add the Super modifier to the following event.
3966 For example, type \\[event-apply-super-modifier] & to enter Super-&."
3967 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3968 (defun event-apply-hyper-modifier (ignore-prompt)
3969 "Add the Hyper modifier to the following event.
3970 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
3971 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3972 (defun event-apply-shift-modifier (ignore-prompt)
3973 "Add the Shift modifier to the following event.
3974 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
3975 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3976 (defun event-apply-control-modifier (ignore-prompt)
3977 "Add the Ctrl modifier to the following event.
3978 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
3979 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3980 (defun event-apply-meta-modifier (ignore-prompt)
3981 "Add the Meta modifier to the following event.
3982 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
3983 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3985 (defun event-apply-modifier (event symbol lshiftby prefix)
3986 "Apply a modifier flag to event EVENT.
3987 SYMBOL is the name of this modifier, as a symbol.
3988 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3989 PREFIX is the string that represents this modifier in an event type symbol."
3990 (if (numberp event)
3991 (cond ((eq symbol 'control)
3992 (if (and (<= (downcase event) ?z)
3993 (>= (downcase event) ?a))
3994 (- (downcase event) ?a -1)
3995 (if (and (<= (downcase event) ?Z)
3996 (>= (downcase event) ?A))
3997 (- (downcase event) ?A -1)
3998 (logior (lsh 1 lshiftby) event))))
3999 ((eq symbol 'shift)
4000 (if (and (<= (downcase event) ?z)
4001 (>= (downcase event) ?a))
4002 (upcase event)
4003 (logior (lsh 1 lshiftby) event)))
4005 (logior (lsh 1 lshiftby) event)))
4006 (if (memq symbol (event-modifiers event))
4007 event
4008 (let ((event-type (if (symbolp event) event (car event))))
4009 (setq event-type (intern (concat prefix (symbol-name event-type))))
4010 (if (symbolp event)
4011 event-type
4012 (cons event-type (cdr event)))))))
4014 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4015 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4016 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4017 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4018 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4019 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4021 ;;;; Keypad support.
4023 ;;; Make the keypad keys act like ordinary typing keys. If people add
4024 ;;; bindings for the function key symbols, then those bindings will
4025 ;;; override these, so this shouldn't interfere with any existing
4026 ;;; bindings.
4028 ;; Also tell read-char how to handle these keys.
4029 (mapc
4030 (lambda (keypad-normal)
4031 (let ((keypad (nth 0 keypad-normal))
4032 (normal (nth 1 keypad-normal)))
4033 (put keypad 'ascii-character normal)
4034 (define-key function-key-map (vector keypad) (vector normal))))
4035 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4036 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4037 (kp-space ?\ )
4038 (kp-tab ?\t)
4039 (kp-enter ?\r)
4040 (kp-multiply ?*)
4041 (kp-add ?+)
4042 (kp-separator ?,)
4043 (kp-subtract ?-)
4044 (kp-decimal ?.)
4045 (kp-divide ?/)
4046 (kp-equal ?=)))
4048 ;;;;
4049 ;;;; forking a twin copy of a buffer.
4050 ;;;;
4052 (defvar clone-buffer-hook nil
4053 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4055 (defun clone-process (process &optional newname)
4056 "Create a twin copy of PROCESS.
4057 If NEWNAME is nil, it defaults to PROCESS' name;
4058 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4059 If PROCESS is associated with a buffer, the new process will be associated
4060 with the current buffer instead.
4061 Returns nil if PROCESS has already terminated."
4062 (setq newname (or newname (process-name process)))
4063 (if (string-match "<[0-9]+>\\'" newname)
4064 (setq newname (substring newname 0 (match-beginning 0))))
4065 (when (memq (process-status process) '(run stop open))
4066 (let* ((process-connection-type (process-tty-name process))
4067 (new-process
4068 (if (memq (process-status process) '(open))
4069 (let ((args (process-contact process t)))
4070 (setq args (plist-put args :name newname))
4071 (setq args (plist-put args :buffer
4072 (if (process-buffer process) (current-buffer))))
4073 (apply 'make-network-process args))
4074 (apply 'start-process newname
4075 (if (process-buffer process) (current-buffer))
4076 (process-command process)))))
4077 (set-process-query-on-exit-flag
4078 new-process (process-query-on-exit-flag process))
4079 (set-process-inherit-coding-system-flag
4080 new-process (process-inherit-coding-system-flag process))
4081 (set-process-filter new-process (process-filter process))
4082 (set-process-sentinel new-process (process-sentinel process))
4083 new-process)))
4085 ;; things to maybe add (currently partly covered by `funcall mode'):
4086 ;; - syntax-table
4087 ;; - overlays
4088 (defun clone-buffer (&optional newname display-flag)
4089 "Create a twin copy of the current buffer.
4090 If NEWNAME is nil, it defaults to the current buffer's name;
4091 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4093 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
4094 This runs the normal hook `clone-buffer-hook' in the new buffer
4095 after it has been set up properly in other respects."
4096 (interactive
4097 (progn
4098 (if buffer-file-name
4099 (error "Cannot clone a file-visiting buffer"))
4100 (if (get major-mode 'no-clone)
4101 (error "Cannot clone a buffer in %s mode" mode-name))
4102 (list (if current-prefix-arg (read-string "Name: "))
4103 t)))
4104 (if buffer-file-name
4105 (error "Cannot clone a file-visiting buffer"))
4106 (if (get major-mode 'no-clone)
4107 (error "Cannot clone a buffer in %s mode" mode-name))
4108 (setq newname (or newname (buffer-name)))
4109 (if (string-match "<[0-9]+>\\'" newname)
4110 (setq newname (substring newname 0 (match-beginning 0))))
4111 (let ((buf (current-buffer))
4112 (ptmin (point-min))
4113 (ptmax (point-max))
4114 (pt (point))
4115 (mk (if mark-active (mark t)))
4116 (modified (buffer-modified-p))
4117 (mode major-mode)
4118 (lvars (buffer-local-variables))
4119 (process (get-buffer-process (current-buffer)))
4120 (new (generate-new-buffer (or newname (buffer-name)))))
4121 (save-restriction
4122 (widen)
4123 (with-current-buffer new
4124 (insert-buffer-substring buf)))
4125 (with-current-buffer new
4126 (narrow-to-region ptmin ptmax)
4127 (goto-char pt)
4128 (if mk (set-mark mk))
4129 (set-buffer-modified-p modified)
4131 ;; Clone the old buffer's process, if any.
4132 (when process (clone-process process))
4134 ;; Now set up the major mode.
4135 (funcall mode)
4137 ;; Set up other local variables.
4138 (mapcar (lambda (v)
4139 (condition-case () ;in case var is read-only
4140 (if (symbolp v)
4141 (makunbound v)
4142 (set (make-local-variable (car v)) (cdr v)))
4143 (error nil)))
4144 lvars)
4146 ;; Run any hooks (typically set up by the major mode
4147 ;; for cloning to work properly).
4148 (run-hooks 'clone-buffer-hook))
4149 (if display-flag (pop-to-buffer new))
4150 new))
4153 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4154 "Create an indirect buffer that is a twin copy of the current buffer.
4156 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4157 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4158 or if not called with a prefix arg, NEWNAME defaults to the current
4159 buffer's name. The name is modified by adding a `<N>' suffix to it
4160 or by incrementing the N in an existing suffix.
4162 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4163 This is always done when called interactively.
4165 Optional last arg NORECORD non-nil means do not put this buffer at the
4166 front of the list of recently selected ones."
4167 (interactive
4168 (progn
4169 (if (get major-mode 'no-clone-indirect)
4170 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4171 (list (if current-prefix-arg
4172 (read-string "BName of indirect buffer: "))
4173 t)))
4174 (if (get major-mode 'no-clone-indirect)
4175 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4176 (setq newname (or newname (buffer-name)))
4177 (if (string-match "<[0-9]+>\\'" newname)
4178 (setq newname (substring newname 0 (match-beginning 0))))
4179 (let* ((name (generate-new-buffer-name newname))
4180 (buffer (make-indirect-buffer (current-buffer) name t)))
4181 (when display-flag
4182 (pop-to-buffer buffer norecord))
4183 buffer))
4186 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4187 "Create an indirect buffer that is a twin copy of BUFFER.
4188 Select the new buffer in another window.
4189 Optional second arg NORECORD non-nil means do not put this buffer at
4190 the front of the list of recently selected ones."
4191 (interactive "bClone buffer in other window: ")
4192 (let ((pop-up-windows t))
4193 (set-buffer buffer)
4194 (clone-indirect-buffer nil t norecord)))
4196 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4198 ;;; Handling of Backspace and Delete keys.
4200 (defcustom normal-erase-is-backspace nil
4201 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4203 On window systems, the default value of this option is chosen
4204 according to the keyboard used. If the keyboard has both a Backspace
4205 key and a Delete key, and both are mapped to their usual meanings, the
4206 option's default value is set to t, so that Backspace can be used to
4207 delete backward, and Delete can be used to delete forward.
4209 If not running under a window system, customizing this option accomplishes
4210 a similar effect by mapping C-h, which is usually generated by the
4211 Backspace key, to DEL, and by mapping DEL to C-d via
4212 `keyboard-translate'. The former functionality of C-h is available on
4213 the F1 key. You should probably not use this setting if you don't
4214 have both Backspace, Delete and F1 keys.
4216 Setting this variable with setq doesn't take effect. Programmatically,
4217 call `normal-erase-is-backspace-mode' (which see) instead."
4218 :type 'boolean
4219 :group 'editing-basics
4220 :version "21.1"
4221 :set (lambda (symbol value)
4222 ;; The fboundp is because of a problem with :set when
4223 ;; dumping Emacs. It doesn't really matter.
4224 (if (fboundp 'normal-erase-is-backspace-mode)
4225 (normal-erase-is-backspace-mode (or value 0))
4226 (set-default symbol value))))
4229 (defun normal-erase-is-backspace-mode (&optional arg)
4230 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4232 With numeric arg, turn the mode on if and only if ARG is positive.
4234 On window systems, when this mode is on, Delete is mapped to C-d and
4235 Backspace is mapped to DEL; when this mode is off, both Delete and
4236 Backspace are mapped to DEL. (The remapping goes via
4237 `function-key-map', so binding Delete or Backspace in the global or
4238 local keymap will override that.)
4240 In addition, on window systems, the bindings of C-Delete, M-Delete,
4241 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4242 the global keymap in accordance with the functionality of Delete and
4243 Backspace. For example, if Delete is remapped to C-d, which deletes
4244 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4245 to DEL, which deletes backward, C-Delete is bound to
4246 `backward-kill-word'.
4248 If not running on a window system, a similar effect is accomplished by
4249 remapping C-h (normally produced by the Backspace key) and DEL via
4250 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4251 to C-d; if it's off, the keys are not remapped.
4253 When not running on a window system, and this mode is turned on, the
4254 former functionality of C-h is available on the F1 key. You should
4255 probably not turn on this mode on a text-only terminal if you don't
4256 have both Backspace, Delete and F1 keys.
4258 See also `normal-erase-is-backspace'."
4259 (interactive "P")
4260 (setq normal-erase-is-backspace
4261 (if arg
4262 (> (prefix-numeric-value arg) 0)
4263 (not normal-erase-is-backspace)))
4265 (cond ((or (memq window-system '(x w32 mac pc))
4266 (memq system-type '(ms-dos windows-nt)))
4267 (let ((bindings
4268 `(([C-delete] [C-backspace])
4269 ([M-delete] [M-backspace])
4270 ([C-M-delete] [C-M-backspace])
4271 (,esc-map
4272 [C-delete] [C-backspace])))
4273 (old-state (lookup-key function-key-map [delete])))
4275 (if normal-erase-is-backspace
4276 (progn
4277 (define-key function-key-map [delete] [?\C-d])
4278 (define-key function-key-map [kp-delete] [?\C-d])
4279 (define-key function-key-map [backspace] [?\C-?]))
4280 (define-key function-key-map [delete] [?\C-?])
4281 (define-key function-key-map [kp-delete] [?\C-?])
4282 (define-key function-key-map [backspace] [?\C-?]))
4284 ;; Maybe swap bindings of C-delete and C-backspace, etc.
4285 (unless (equal old-state (lookup-key function-key-map [delete]))
4286 (dolist (binding bindings)
4287 (let ((map global-map))
4288 (when (keymapp (car binding))
4289 (setq map (car binding) binding (cdr binding)))
4290 (let* ((key1 (nth 0 binding))
4291 (key2 (nth 1 binding))
4292 (binding1 (lookup-key map key1))
4293 (binding2 (lookup-key map key2)))
4294 (define-key map key1 binding2)
4295 (define-key map key2 binding1)))))))
4297 (if normal-erase-is-backspace
4298 (progn
4299 (keyboard-translate ?\C-h ?\C-?)
4300 (keyboard-translate ?\C-? ?\C-d))
4301 (keyboard-translate ?\C-h ?\C-h)
4302 (keyboard-translate ?\C-? ?\C-?))))
4304 (run-hooks 'normal-erase-is-backspace-hook)
4305 (if (interactive-p)
4306 (message "Delete key deletes %s"
4307 (if normal-erase-is-backspace "forward" "backward"))))
4310 ;; Minibuffer prompt stuff.
4312 ;(defun minibuffer-prompt-modification (start end)
4313 ; (error "You cannot modify the prompt"))
4316 ;(defun minibuffer-prompt-insertion (start end)
4317 ; (let ((inhibit-modification-hooks t))
4318 ; (delete-region start end)
4319 ; ;; Discard undo information for the text insertion itself
4320 ; ;; and for the text deletion.above.
4321 ; (when (consp buffer-undo-list)
4322 ; (setq buffer-undo-list (cddr buffer-undo-list)))
4323 ; (message "You cannot modify the prompt")))
4326 ;(setq minibuffer-prompt-properties
4327 ; (list 'modification-hooks '(minibuffer-prompt-modification)
4328 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
4331 (provide 'simple)
4332 ;;; simple.el ends here