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