*** empty log message ***
[emacs.git] / lisp / simple.el
blob3db2a418f3b50dcb23fcf38535ea99148e063039
1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; A grab-bag of basic Emacs commands not specifically related to some
30 ;; major mode or to file-handling.
32 ;;; Code:
34 (eval-when-compile
35 (autoload 'widget-convert "wid-edit")
36 (autoload 'shell-mode "shell"))
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 ;;; next-error support framework
71 (defgroup next-error nil
72 "next-error support framework."
73 :group 'compilation
74 :version "21.4")
76 (defface next-error
77 '((t (:inherit region)))
78 "Face used to highlight next error locus."
79 :group 'next-error
80 :version "21.4")
82 (defcustom next-error-highlight 0.1
83 "*Highlighting of locations in selected source buffers.
84 If number, highlight the locus in next-error face for given time in seconds.
85 If t, use persistent overlays fontified in next-error face.
86 If nil, don't highlight the locus in the source buffer.
87 If `fringe-arrow', indicate the locus by the fringe arrow."
88 :type '(choice (number :tag "Delay")
89 (const :tag "Persistent overlay" t)
90 (const :tag "No highlighting" nil)
91 (const :tag "Fringe arrow" 'fringe-arrow))
92 :group 'next-error
93 :version "21.4")
95 (defcustom next-error-highlight-no-select 0.1
96 "*Highlighting of locations in non-selected source buffers.
97 If number, highlight the locus in next-error face for given time in seconds.
98 If t, use persistent overlays fontified in next-error face.
99 If nil, don't highlight the locus in the source buffer.
100 If `fringe-arrow', indicate the locus by the fringe arrow."
101 :type '(choice (number :tag "Delay")
102 (const :tag "Persistent overlay" t)
103 (const :tag "No highlighting" nil)
104 (const :tag "Fringe arrow" 'fringe-arrow))
105 :group 'next-error
106 :version "21.4")
108 (defvar next-error-last-buffer nil
109 "The most recent next-error buffer.
110 A buffer becomes most recent when its compilation, grep, or
111 similar mode is started, or when it is used with \\[next-error]
112 or \\[compile-goto-error].")
114 (defvar next-error-function nil
115 "Function to use to find the next error in the current buffer.
116 The function is called with 2 parameters:
117 ARG is an integer specifying by how many errors to move.
118 RESET is a boolean which, if non-nil, says to go back to the beginning
119 of the errors before moving.
120 Major modes providing compile-like functionality should set this variable
121 to indicate to `next-error' that this is a candidate buffer and how
122 to navigate in it.")
124 (make-variable-buffer-local 'next-error-function)
126 (defsubst next-error-buffer-p (buffer
127 &optional avoid-current
128 extra-test-inclusive
129 extra-test-exclusive)
130 "Test if BUFFER is a next-error capable buffer.
132 If AVOID-CURRENT is non-nil, treat the current buffer
133 as an absolute last resort only.
135 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
136 that normally would not qualify. If it returns t, the buffer
137 in question is treated as usable.
139 The function EXTRA-TEST-EXCLUSIVE, if non-nil is called in each buffer
140 that would normally be considered usable. if it returns nil,
141 that buffer is rejected."
142 (and (buffer-name buffer) ;First make sure it's live.
143 (not (and avoid-current (eq buffer (current-buffer))))
144 (with-current-buffer buffer
145 (if next-error-function ; This is the normal test.
146 ;; Optionally reject some buffers.
147 (if extra-test-exclusive
148 (funcall extra-test-exclusive)
150 ;; Optionally accept some other buffers.
151 (and extra-test-inclusive
152 (funcall extra-test-inclusive))))))
154 (defun next-error-find-buffer (&optional avoid-current
155 extra-test-inclusive
156 extra-test-exclusive)
157 "Return a next-error capable buffer.
158 If AVOID-CURRENT is non-nil, treat the current buffer
159 as an absolute last resort only.
161 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffers
162 that normally would not qualify. If it returns t, the buffer
163 in question is treated as usable.
165 The function EXTRA-TEST-EXCLUSIVE, if non-nil is called in each buffer
166 that would normally be considered usable. If it returns nil,
167 that buffer is rejected."
169 ;; 1. If one window on the selected frame displays such buffer, return it.
170 (let ((window-buffers
171 (delete-dups
172 (delq nil (mapcar (lambda (w)
173 (if (next-error-buffer-p
174 (window-buffer w)
175 avoid-current
176 extra-test-inclusive extra-test-exclusive)
177 (window-buffer w)))
178 (window-list))))))
179 (if (eq (length window-buffers) 1)
180 (car window-buffers)))
181 ;; 2. If next-error-last-buffer is an acceptable buffer, use that.
182 (if (and next-error-last-buffer
183 (next-error-buffer-p next-error-last-buffer avoid-current
184 extra-test-inclusive extra-test-exclusive))
185 next-error-last-buffer)
186 ;; 3. If the current buffer is acceptable, choose it.
187 (if (next-error-buffer-p (current-buffer) avoid-current
188 extra-test-inclusive extra-test-exclusive)
189 (current-buffer))
190 ;; 4. Look for any acceptable buffer.
191 (let ((buffers (buffer-list)))
192 (while (and buffers
193 (not (next-error-buffer-p
194 (car buffers) avoid-current
195 extra-test-inclusive extra-test-exclusive)))
196 (setq buffers (cdr buffers)))
197 (car buffers))
198 ;; 5. Use the current buffer as a last resort if it qualifies,
199 ;; even despite AVOID-CURRENT.
200 (and avoid-current
201 (next-error-buffer-p (current-buffer) nil
202 extra-test-inclusive extra-test-exclusive)
203 (progn
204 (message "This is the only next-error capable buffer")
205 (current-buffer)))
206 ;; 6. Give up.
207 (error "No next-error capable buffer found")))
209 (defun next-error (&optional arg reset)
210 "Visit next next-error message and corresponding source code.
212 If all the error messages parsed so far have been processed already,
213 the message buffer is checked for new ones.
215 A prefix ARG specifies how many error messages to move;
216 negative means move back to previous error messages.
217 Just \\[universal-argument] as a prefix means reparse the error message buffer
218 and start at the first error.
220 The RESET argument specifies that we should restart from the beginning.
222 \\[next-error] normally uses the most recently started
223 compilation, grep, or occur buffer. It can also operate on any
224 buffer with output from the \\[compile], \\[grep] commands, or,
225 more generally, on any buffer in Compilation mode or with
226 Compilation Minor mode enabled, or any buffer in which
227 `next-error-function' is bound to an appropriate function.
228 To specify use of a particular buffer for error messages, type
229 \\[next-error] in that buffer when it is the only one displayed
230 in the current frame.
232 Once \\[next-error] has chosen the buffer for error messages,
233 it stays with that buffer until you use it in some other buffer which
234 uses Compilation mode or Compilation Minor mode.
236 See variables `compilation-parse-errors-function' and
237 \`compilation-error-regexp-alist' for customization ideas."
238 (interactive "P")
239 (if (consp arg) (setq reset t arg nil))
240 (when (setq next-error-last-buffer (next-error-find-buffer))
241 ;; we know here that next-error-function is a valid symbol we can funcall
242 (with-current-buffer next-error-last-buffer
243 (funcall next-error-function (prefix-numeric-value arg) reset))))
245 (defalias 'goto-next-locus 'next-error)
246 (defalias 'next-match 'next-error)
248 (define-key ctl-x-map "`" 'next-error)
250 (defun previous-error (&optional n)
251 "Visit previous next-error message and corresponding source code.
253 Prefix arg N says how many error messages to move backwards (or
254 forwards, if negative).
256 This operates on the output from the \\[compile] and \\[grep] commands."
257 (interactive "p")
258 (next-error (- (or n 1))))
260 (defun first-error (&optional n)
261 "Restart at the first error.
262 Visit corresponding source code.
263 With prefix arg N, visit the source code of the Nth error.
264 This operates on the output from the \\[compile] command, for instance."
265 (interactive "p")
266 (next-error n t))
268 (defun next-error-no-select (&optional n)
269 "Move point to the next error in the next-error buffer and highlight match.
270 Prefix arg N says how many error messages to move forwards (or
271 backwards, if negative).
272 Finds and highlights the source line like \\[next-error], but does not
273 select the source buffer."
274 (interactive "p")
275 (let ((next-error-highlight next-error-highlight-no-select))
276 (next-error n))
277 (pop-to-buffer next-error-last-buffer))
279 (defun previous-error-no-select (&optional n)
280 "Move point to the previous error in the next-error buffer and highlight match.
281 Prefix arg N says how many error messages to move backwards (or
282 forwards, if negative).
283 Finds and highlights the source line like \\[previous-error], but does not
284 select the source buffer."
285 (interactive "p")
286 (next-error-no-select (- (or n 1))))
288 ;;; Internal variable for `next-error-follow-mode-post-command-hook'.
289 (defvar next-error-follow-last-line nil)
291 (define-minor-mode next-error-follow-minor-mode
292 "Minor mode for compilation, occur and diff modes.
293 When turned on, cursor motion in the compilation, grep, occur or diff
294 buffer causes automatic display of the corresponding source code
295 location."
296 nil " Fol" nil
297 (if (not next-error-follow-minor-mode)
298 (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t)
299 (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t)
300 (make-variable-buffer-local 'next-error-follow-last-line)))
302 ;;; Used as a `post-command-hook' by `next-error-follow-mode'
303 ;;; for the *Compilation* *grep* and *Occur* buffers.
304 (defun next-error-follow-mode-post-command-hook ()
305 (unless (equal next-error-follow-last-line (line-number-at-pos))
306 (setq next-error-follow-last-line (line-number-at-pos))
307 (condition-case nil
308 (let ((compilation-context-lines nil))
309 (setq compilation-current-error (point))
310 (next-error-no-select 0))
311 (error t))))
316 (defun fundamental-mode ()
317 "Major mode not specialized for anything in particular.
318 Other major modes are defined by comparison with this one."
319 (interactive)
320 (kill-all-local-variables)
321 (run-hooks 'after-change-major-mode-hook))
323 ;; Making and deleting lines.
325 (defun newline (&optional arg)
326 "Insert a newline, and move to left margin of the new line if it's blank.
327 If `use-hard-newlines' is non-nil, the newline is marked with the
328 text-property `hard'.
329 With ARG, insert that many newlines.
330 Call `auto-fill-function' if the current column number is greater
331 than the value of `fill-column' and ARG is nil."
332 (interactive "*P")
333 (barf-if-buffer-read-only)
334 ;; Inserting a newline at the end of a line produces better redisplay in
335 ;; try_window_id than inserting at the beginning of a line, and the textual
336 ;; result is the same. So, if we're at beginning of line, pretend to be at
337 ;; the end of the previous line.
338 (let ((flag (and (not (bobp))
339 (bolp)
340 ;; Make sure no functions want to be told about
341 ;; the range of the changes.
342 (not after-change-functions)
343 (not before-change-functions)
344 ;; Make sure there are no markers here.
345 (not (buffer-has-markers-at (1- (point))))
346 (not (buffer-has-markers-at (point)))
347 ;; Make sure no text properties want to know
348 ;; where the change was.
349 (not (get-char-property (1- (point)) 'modification-hooks))
350 (not (get-char-property (1- (point)) 'insert-behind-hooks))
351 (or (eobp)
352 (not (get-char-property (point) 'insert-in-front-hooks)))
353 ;; Make sure the newline before point isn't intangible.
354 (not (get-char-property (1- (point)) 'intangible))
355 ;; Make sure the newline before point isn't read-only.
356 (not (get-char-property (1- (point)) 'read-only))
357 ;; Make sure the newline before point isn't invisible.
358 (not (get-char-property (1- (point)) 'invisible))
359 ;; Make sure the newline before point has the same
360 ;; properties as the char before it (if any).
361 (< (or (previous-property-change (point)) -2)
362 (- (point) 2))))
363 (was-page-start (and (bolp)
364 (looking-at page-delimiter)))
365 (beforepos (point)))
366 (if flag (backward-char 1))
367 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
368 ;; Set last-command-char to tell self-insert what to insert.
369 (let ((last-command-char ?\n)
370 ;; Don't auto-fill if we have a numeric argument.
371 ;; Also not if flag is true (it would fill wrong line);
372 ;; there is no need to since we're at BOL.
373 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
374 (unwind-protect
375 (self-insert-command (prefix-numeric-value arg))
376 ;; If we get an error in self-insert-command, put point at right place.
377 (if flag (forward-char 1))))
378 ;; Even if we did *not* get an error, keep that forward-char;
379 ;; all further processing should apply to the newline that the user
380 ;; thinks he inserted.
382 ;; Mark the newline(s) `hard'.
383 (if use-hard-newlines
384 (set-hard-newline-properties
385 (- (point) (if arg (prefix-numeric-value arg) 1)) (point)))
386 ;; If the newline leaves the previous line blank,
387 ;; and we have a left margin, delete that from the blank line.
388 (or flag
389 (save-excursion
390 (goto-char beforepos)
391 (beginning-of-line)
392 (and (looking-at "[ \t]$")
393 (> (current-left-margin) 0)
394 (delete-region (point) (progn (end-of-line) (point))))))
395 ;; Indent the line after the newline, except in one case:
396 ;; when we added the newline at the beginning of a line
397 ;; which starts a page.
398 (or was-page-start
399 (move-to-left-margin nil t)))
400 nil)
402 (defun set-hard-newline-properties (from to)
403 (let ((sticky (get-text-property from 'rear-nonsticky)))
404 (put-text-property from to 'hard 't)
405 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
406 (if (and (listp sticky) (not (memq 'hard sticky)))
407 (put-text-property from (point) 'rear-nonsticky
408 (cons 'hard sticky)))))
410 (defun open-line (n)
411 "Insert a newline and leave point before it.
412 If there is a fill prefix and/or a left-margin, insert them on the new line
413 if the line would have been blank.
414 With arg N, insert N newlines."
415 (interactive "*p")
416 (let* ((do-fill-prefix (and fill-prefix (bolp)))
417 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
418 (loc (point))
419 ;; Don't expand an abbrev before point.
420 (abbrev-mode nil))
421 (newline n)
422 (goto-char loc)
423 (while (> n 0)
424 (cond ((bolp)
425 (if do-left-margin (indent-to (current-left-margin)))
426 (if do-fill-prefix (insert-and-inherit fill-prefix))))
427 (forward-line 1)
428 (setq n (1- n)))
429 (goto-char loc)
430 (end-of-line)))
432 (defun split-line (&optional arg)
433 "Split current line, moving portion beyond point vertically down.
434 If the current line starts with `fill-prefix', insert it on the new
435 line as well. With prefix ARG, don't insert fill-prefix on new line.
437 When called from Lisp code, ARG may be a prefix string to copy."
438 (interactive "*P")
439 (skip-chars-forward " \t")
440 (let* ((col (current-column))
441 (pos (point))
442 ;; What prefix should we check for (nil means don't).
443 (prefix (cond ((stringp arg) arg)
444 (arg nil)
445 (t fill-prefix)))
446 ;; Does this line start with it?
447 (have-prfx (and prefix
448 (save-excursion
449 (beginning-of-line)
450 (looking-at (regexp-quote prefix))))))
451 (newline 1)
452 (if have-prfx (insert-and-inherit prefix))
453 (indent-to col 0)
454 (goto-char pos)))
456 (defun delete-indentation (&optional arg)
457 "Join this line to previous and fix up whitespace at join.
458 If there is a fill prefix, delete it from the beginning of this line.
459 With argument, join this line to following line."
460 (interactive "*P")
461 (beginning-of-line)
462 (if arg (forward-line 1))
463 (if (eq (preceding-char) ?\n)
464 (progn
465 (delete-region (point) (1- (point)))
466 ;; If the second line started with the fill prefix,
467 ;; delete the prefix.
468 (if (and fill-prefix
469 (<= (+ (point) (length fill-prefix)) (point-max))
470 (string= fill-prefix
471 (buffer-substring (point)
472 (+ (point) (length fill-prefix)))))
473 (delete-region (point) (+ (point) (length fill-prefix))))
474 (fixup-whitespace))))
476 (defalias 'join-line #'delete-indentation) ; easier to find
478 (defun delete-blank-lines ()
479 "On blank line, delete all surrounding blank lines, leaving just one.
480 On isolated blank line, delete that one.
481 On nonblank line, delete any immediately following blank lines."
482 (interactive "*")
483 (let (thisblank singleblank)
484 (save-excursion
485 (beginning-of-line)
486 (setq thisblank (looking-at "[ \t]*$"))
487 ;; Set singleblank if there is just one blank line here.
488 (setq singleblank
489 (and thisblank
490 (not (looking-at "[ \t]*\n[ \t]*$"))
491 (or (bobp)
492 (progn (forward-line -1)
493 (not (looking-at "[ \t]*$")))))))
494 ;; Delete preceding blank lines, and this one too if it's the only one.
495 (if thisblank
496 (progn
497 (beginning-of-line)
498 (if singleblank (forward-line 1))
499 (delete-region (point)
500 (if (re-search-backward "[^ \t\n]" nil t)
501 (progn (forward-line 1) (point))
502 (point-min)))))
503 ;; Delete following blank lines, unless the current line is blank
504 ;; and there are no following blank lines.
505 (if (not (and thisblank singleblank))
506 (save-excursion
507 (end-of-line)
508 (forward-line 1)
509 (delete-region (point)
510 (if (re-search-forward "[^ \t\n]" nil t)
511 (progn (beginning-of-line) (point))
512 (point-max)))))
513 ;; Handle the special case where point is followed by newline and eob.
514 ;; Delete the line, leaving point at eob.
515 (if (looking-at "^[ \t]*\n\\'")
516 (delete-region (point) (point-max)))))
518 (defun delete-trailing-whitespace ()
519 "Delete all the trailing whitespace across the current buffer.
520 All whitespace after the last non-whitespace character in a line is deleted.
521 This respects narrowing, created by \\[narrow-to-region] and friends.
522 A formfeed is not considered whitespace by this function."
523 (interactive "*")
524 (save-match-data
525 (save-excursion
526 (goto-char (point-min))
527 (while (re-search-forward "\\s-$" nil t)
528 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
529 ;; Don't delete formfeeds, even if they are considered whitespace.
530 (save-match-data
531 (if (looking-at ".*\f")
532 (goto-char (match-end 0))))
533 (delete-region (point) (match-end 0))))))
535 (defun newline-and-indent ()
536 "Insert a newline, then indent according to major mode.
537 Indentation is done using the value of `indent-line-function'.
538 In programming language modes, this is the same as TAB.
539 In some text modes, where TAB inserts a tab, this command indents to the
540 column specified by the function `current-left-margin'."
541 (interactive "*")
542 (delete-horizontal-space t)
543 (newline)
544 (indent-according-to-mode))
546 (defun reindent-then-newline-and-indent ()
547 "Reindent current line, insert newline, then indent the new line.
548 Indentation of both lines is done according to the current major mode,
549 which means calling the current value of `indent-line-function'.
550 In programming language modes, this is the same as TAB.
551 In some text modes, where TAB inserts a tab, this indents to the
552 column specified by the function `current-left-margin'."
553 (interactive "*")
554 (let ((pos (point)))
555 ;; Be careful to insert the newline before indenting the line.
556 ;; Otherwise, the indentation might be wrong.
557 (newline)
558 (save-excursion
559 (goto-char pos)
560 (indent-according-to-mode)
561 (delete-horizontal-space t))
562 (indent-according-to-mode)))
564 (defun quoted-insert (arg)
565 "Read next input character and insert it.
566 This is useful for inserting control characters.
568 If the first character you type after this command is an octal digit,
569 you should type a sequence of octal digits which specify a character code.
570 Any nondigit terminates the sequence. If the terminator is a RET,
571 it is discarded; any other terminator is used itself as input.
572 The variable `read-quoted-char-radix' specifies the radix for this feature;
573 set it to 10 or 16 to use decimal or hex instead of octal.
575 In overwrite mode, this function inserts the character anyway, and
576 does not handle octal digits specially. This means that if you use
577 overwrite as your normal editing mode, you can use this function to
578 insert characters when necessary.
580 In binary overwrite mode, this function does overwrite, and octal
581 digits are interpreted as a character code. This is intended to be
582 useful for editing binary files."
583 (interactive "*p")
584 (let* ((char (let (translation-table-for-input)
585 (if (or (not overwrite-mode)
586 (eq overwrite-mode 'overwrite-mode-binary))
587 (read-quoted-char)
588 (read-char)))))
589 ;; Assume character codes 0240 - 0377 stand for characters in some
590 ;; single-byte character set, and convert them to Emacs
591 ;; characters.
592 (if (and enable-multibyte-characters
593 (>= char ?\240)
594 (<= char ?\377))
595 (setq char (unibyte-char-to-multibyte char)))
596 (if (> arg 0)
597 (if (eq overwrite-mode 'overwrite-mode-binary)
598 (delete-char arg)))
599 (while (> arg 0)
600 (insert-and-inherit char)
601 (setq arg (1- arg)))))
603 (defun forward-to-indentation (&optional arg)
604 "Move forward ARG lines and position at first nonblank character."
605 (interactive "p")
606 (forward-line (or arg 1))
607 (skip-chars-forward " \t"))
609 (defun backward-to-indentation (&optional arg)
610 "Move backward ARG lines and position at first nonblank character."
611 (interactive "p")
612 (forward-line (- (or arg 1)))
613 (skip-chars-forward " \t"))
615 (defun back-to-indentation ()
616 "Move point to the first non-whitespace character on this line."
617 (interactive)
618 (beginning-of-line 1)
619 (skip-syntax-forward " " (line-end-position))
620 ;; Move back over chars that have whitespace syntax but have the p flag.
621 (backward-prefix-chars))
623 (defun fixup-whitespace ()
624 "Fixup white space between objects around point.
625 Leave one space or none, according to the context."
626 (interactive "*")
627 (save-excursion
628 (delete-horizontal-space)
629 (if (or (looking-at "^\\|\\s)")
630 (save-excursion (forward-char -1)
631 (looking-at "$\\|\\s(\\|\\s'")))
633 (insert ?\ ))))
635 (defun delete-horizontal-space (&optional backward-only)
636 "Delete all spaces and tabs around point.
637 If BACKWARD-ONLY is non-nil, only delete spaces before point."
638 (interactive "*")
639 (let ((orig-pos (point)))
640 (delete-region
641 (if backward-only
642 orig-pos
643 (progn
644 (skip-chars-forward " \t")
645 (constrain-to-field nil orig-pos t)))
646 (progn
647 (skip-chars-backward " \t")
648 (constrain-to-field nil orig-pos)))))
650 (defun just-one-space (&optional n)
651 "Delete all spaces and tabs around point, leaving one space (or N spaces)."
652 (interactive "*p")
653 (let ((orig-pos (point)))
654 (skip-chars-backward " \t")
655 (constrain-to-field nil orig-pos)
656 (dotimes (i (or n 1))
657 (if (= (following-char) ?\ )
658 (forward-char 1)
659 (insert ?\ )))
660 (delete-region
661 (point)
662 (progn
663 (skip-chars-forward " \t")
664 (constrain-to-field nil orig-pos t)))))
666 (defun beginning-of-buffer (&optional arg)
667 "Move point to the beginning of the buffer; leave mark at previous position.
668 With \\[universal-argument] prefix, do not set mark at previous position.
669 With numeric arg N, put point N/10 of the way from the beginning.
671 If the buffer is narrowed, this command uses the beginning and size
672 of the accessible part of the buffer.
674 Don't use this command in Lisp programs!
675 \(goto-char (point-min)) is faster and avoids clobbering the mark."
676 (interactive "P")
677 (or (consp arg)
678 (and transient-mark-mode mark-active)
679 (push-mark))
680 (let ((size (- (point-max) (point-min))))
681 (goto-char (if (and arg (not (consp arg)))
682 (+ (point-min)
683 (if (> size 10000)
684 ;; Avoid overflow for large buffer sizes!
685 (* (prefix-numeric-value arg)
686 (/ size 10))
687 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
688 (point-min))))
689 (if arg (forward-line 1)))
691 (defun end-of-buffer (&optional arg)
692 "Move point to the end of the buffer; leave mark at previous position.
693 With \\[universal-argument] prefix, do not set mark at previous position.
694 With numeric arg N, put point N/10 of the way from the end.
696 If the buffer is narrowed, this command uses the beginning and size
697 of the accessible part of the buffer.
699 Don't use this command in Lisp programs!
700 \(goto-char (point-max)) is faster and avoids clobbering the mark."
701 (interactive "P")
702 (or (consp arg)
703 (and transient-mark-mode mark-active)
704 (push-mark))
705 (let ((size (- (point-max) (point-min))))
706 (goto-char (if (and arg (not (consp arg)))
707 (- (point-max)
708 (if (> size 10000)
709 ;; Avoid overflow for large buffer sizes!
710 (* (prefix-numeric-value arg)
711 (/ size 10))
712 (/ (* size (prefix-numeric-value arg)) 10)))
713 (point-max))))
714 ;; If we went to a place in the middle of the buffer,
715 ;; adjust it to the beginning of a line.
716 (cond (arg (forward-line 1))
717 ((> (point) (window-end nil t))
718 ;; If the end of the buffer is not already on the screen,
719 ;; then scroll specially to put it near, but not at, the bottom.
720 (overlay-recenter (point))
721 (recenter -3))))
723 (defun mark-whole-buffer ()
724 "Put point at beginning and mark at end of buffer.
725 You probably should not use this function in Lisp programs;
726 it is usually a mistake for a Lisp function to use any subroutine
727 that uses or sets the mark."
728 (interactive)
729 (push-mark (point))
730 (push-mark (point-max) nil t)
731 (goto-char (point-min)))
734 ;; Counting lines, one way or another.
736 (defun goto-line (arg)
737 "Goto line ARG, counting from line 1 at beginning of buffer."
738 (interactive "NGoto line: ")
739 (setq arg (prefix-numeric-value arg))
740 (save-restriction
741 (widen)
742 (goto-char 1)
743 (if (eq selective-display t)
744 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
745 (forward-line (1- arg)))))
747 (defun count-lines-region (start end)
748 "Print number of lines and characters in the region."
749 (interactive "r")
750 (message "Region has %d lines, %d characters"
751 (count-lines start end) (- end start)))
753 (defun what-line ()
754 "Print the current buffer line number and narrowed line number of point."
755 (interactive)
756 (let ((start (point-min))
757 (n (line-number-at-pos)))
758 (if (= start 1)
759 (message "Line %d" n)
760 (save-excursion
761 (save-restriction
762 (widen)
763 (message "line %d (narrowed line %d)"
764 (+ n (line-number-at-pos start) -1) n))))))
766 (defun count-lines (start end)
767 "Return number of lines between START and END.
768 This is usually the number of newlines between them,
769 but can be one more if START is not equal to END
770 and the greater of them is not at the start of a line."
771 (save-excursion
772 (save-restriction
773 (narrow-to-region start end)
774 (goto-char (point-min))
775 (if (eq selective-display t)
776 (save-match-data
777 (let ((done 0))
778 (while (re-search-forward "[\n\C-m]" nil t 40)
779 (setq done (+ 40 done)))
780 (while (re-search-forward "[\n\C-m]" nil t 1)
781 (setq done (+ 1 done)))
782 (goto-char (point-max))
783 (if (and (/= start end)
784 (not (bolp)))
785 (1+ done)
786 done)))
787 (- (buffer-size) (forward-line (buffer-size)))))))
789 (defun line-number-at-pos (&optional pos)
790 "Return (narrowed) buffer line number at position POS.
791 If POS is nil, use current buffer location."
792 (let ((opoint (or pos (point))) start)
793 (save-excursion
794 (goto-char (point-min))
795 (setq start (point))
796 (goto-char opoint)
797 (forward-line 0)
798 (1+ (count-lines start (point))))))
800 (defun what-cursor-position (&optional detail)
801 "Print info on cursor position (on screen and within buffer).
802 Also describe the character after point, and give its character code
803 in octal, decimal and hex.
805 For a non-ASCII multibyte character, also give its encoding in the
806 buffer's selected coding system if the coding system encodes the
807 character safely. If the character is encoded into one byte, that
808 code is shown in hex. If the character is encoded into more than one
809 byte, just \"...\" is shown.
811 In addition, with prefix argument, show details about that character
812 in *Help* buffer. See also the command `describe-char'."
813 (interactive "P")
814 (let* ((char (following-char))
815 (beg (point-min))
816 (end (point-max))
817 (pos (point))
818 (total (buffer-size))
819 (percent (if (> total 50000)
820 ;; Avoid overflow from multiplying by 100!
821 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
822 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
823 (hscroll (if (= (window-hscroll) 0)
825 (format " Hscroll=%d" (window-hscroll))))
826 (col (current-column)))
827 (if (= pos end)
828 (if (or (/= beg 1) (/= end (1+ total)))
829 (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
830 pos total percent beg end col hscroll)
831 (message "point=%d of %d (%d%%) column %d %s"
832 pos total percent col hscroll))
833 (let ((coding buffer-file-coding-system)
834 encoded encoding-msg)
835 (if (or (not coding)
836 (eq (coding-system-type coding) t))
837 (setq coding default-buffer-file-coding-system))
838 (if (not (char-valid-p char))
839 (setq encoding-msg
840 (format "(0%o, %d, 0x%x, invalid)" char char char))
841 (setq encoded (and (>= char 128) (encode-coding-char char coding)))
842 (setq encoding-msg
843 (if encoded
844 (format "(0%o, %d, 0x%x, file %s)"
845 char char char
846 (if (> (length encoded) 1)
847 "..."
848 (encoded-string-description encoded coding)))
849 (format "(0%o, %d, 0x%x)" char char char))))
850 (if detail
851 ;; We show the detailed information about CHAR.
852 (describe-char (point)))
853 (if (or (/= beg 1) (/= end (1+ total)))
854 (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
855 (if (< char 256)
856 (single-key-description char)
857 (buffer-substring-no-properties (point) (1+ (point))))
858 encoding-msg pos total percent beg end col hscroll)
859 (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
860 (if (< char 256)
861 (single-key-description char)
862 (buffer-substring-no-properties (point) (1+ (point))))
863 encoding-msg pos total percent col hscroll))))))
865 (defvar read-expression-map
866 (let ((m (make-sparse-keymap)))
867 (define-key m "\M-\t" 'lisp-complete-symbol)
868 (set-keymap-parent m minibuffer-local-map)
870 "Minibuffer keymap used for reading Lisp expressions.")
872 (defvar read-expression-history nil)
874 (defcustom eval-expression-print-level 4
875 "*Value to use for `print-level' when printing value in `eval-expression'.
876 A value of nil means no limit."
877 :group 'lisp
878 :type '(choice (const :tag "No Limit" nil) integer)
879 :version "21.1")
881 (defcustom eval-expression-print-length 12
882 "*Value to use for `print-length' when printing value in `eval-expression'.
883 A value of nil means no limit."
884 :group 'lisp
885 :type '(choice (const :tag "No Limit" nil) integer)
886 :version "21.1")
888 (defcustom eval-expression-debug-on-error t
889 "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'.
890 If nil, don't change the value of `debug-on-error'."
891 :group 'lisp
892 :type 'boolean
893 :version "21.1")
895 (defun eval-expression-print-format (value)
896 "Format VALUE as a result of evaluated expression.
897 Return a formatted string which is displayed in the echo area
898 in addition to the value printed by prin1 in functions which
899 display the result of expression evaluation."
900 (if (and (integerp value)
901 (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
902 (eq this-command last-command)
903 (if (boundp 'edebug-active) edebug-active)))
904 (let ((char-string
905 (if (or (and (boundp 'edebug-active) edebug-active)
906 (memq this-command '(eval-last-sexp eval-print-last-sexp)))
907 (prin1-char value))))
908 (if char-string
909 (format " (0%o, 0x%x) = %s" value value char-string)
910 (format " (0%o, 0x%x)" value value)))))
912 ;; We define this, rather than making `eval' interactive,
913 ;; for the sake of completion of names like eval-region, eval-current-buffer.
914 (defun eval-expression (eval-expression-arg
915 &optional eval-expression-insert-value)
916 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
917 Value is also consed on to front of the variable `values'.
918 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
919 insert the result into the current buffer instead of printing it in
920 the echo area."
921 (interactive
922 (list (read-from-minibuffer "Eval: "
923 nil read-expression-map t
924 'read-expression-history)
925 current-prefix-arg))
927 (if (null eval-expression-debug-on-error)
928 (setq values (cons (eval eval-expression-arg) values))
929 (let ((old-value (make-symbol "t")) new-value)
930 ;; Bind debug-on-error to something unique so that we can
931 ;; detect when evaled code changes it.
932 (let ((debug-on-error old-value))
933 (setq values (cons (eval eval-expression-arg) values))
934 (setq new-value debug-on-error))
935 ;; If evaled code has changed the value of debug-on-error,
936 ;; propagate that change to the global binding.
937 (unless (eq old-value new-value)
938 (setq debug-on-error new-value))))
940 (let ((print-length eval-expression-print-length)
941 (print-level eval-expression-print-level))
942 (if eval-expression-insert-value
943 (with-no-warnings
944 (let ((standard-output (current-buffer)))
945 (eval-last-sexp-print-value (car values))))
946 (prog1
947 (prin1 (car values) t)
948 (let ((str (eval-expression-print-format (car values))))
949 (if str (princ str t)))))))
951 (defun edit-and-eval-command (prompt command)
952 "Prompting with PROMPT, let user edit COMMAND and eval result.
953 COMMAND is a Lisp expression. Let user edit that expression in
954 the minibuffer, then read and evaluate the result."
955 (let ((command
956 (let ((print-level nil)
957 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
958 (unwind-protect
959 (read-from-minibuffer prompt
960 (prin1-to-string command)
961 read-expression-map t
962 'command-history)
963 ;; If command was added to command-history as a string,
964 ;; get rid of that. We want only evaluable expressions there.
965 (if (stringp (car command-history))
966 (setq command-history (cdr command-history)))))))
968 ;; If command to be redone does not match front of history,
969 ;; add it to the history.
970 (or (equal command (car command-history))
971 (setq command-history (cons command command-history)))
972 (eval command)))
974 (defun repeat-complex-command (arg)
975 "Edit and re-evaluate last complex command, or ARGth from last.
976 A complex command is one which used the minibuffer.
977 The command is placed in the minibuffer as a Lisp form for editing.
978 The result is executed, repeating the command as changed.
979 If the command has been changed or is not the most recent previous command
980 it is added to the front of the command history.
981 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
982 to get different commands to edit and resubmit."
983 (interactive "p")
984 (let ((elt (nth (1- arg) command-history))
985 newcmd)
986 (if elt
987 (progn
988 (setq newcmd
989 (let ((print-level nil)
990 (minibuffer-history-position arg)
991 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
992 (unwind-protect
993 (read-from-minibuffer
994 "Redo: " (prin1-to-string elt) read-expression-map t
995 (cons 'command-history arg))
997 ;; If command was added to command-history as a
998 ;; string, get rid of that. We want only
999 ;; evaluable expressions there.
1000 (if (stringp (car command-history))
1001 (setq command-history (cdr command-history))))))
1003 ;; If command to be redone does not match front of history,
1004 ;; add it to the history.
1005 (or (equal newcmd (car command-history))
1006 (setq command-history (cons newcmd command-history)))
1007 (eval newcmd))
1008 (if command-history
1009 (error "Argument %d is beyond length of command history" arg)
1010 (error "There are no previous complex commands to repeat")))))
1012 (defvar minibuffer-history nil
1013 "Default minibuffer history list.
1014 This is used for all minibuffer input
1015 except when an alternate history list is specified.")
1016 (defvar minibuffer-history-sexp-flag nil
1017 "Control whether history list elements are expressions or strings.
1018 If the value of this variable equals current minibuffer depth,
1019 they are expressions; otherwise they are strings.
1020 \(That convention is designed to do the right thing fora
1021 recursive uses of the minibuffer.)")
1022 (setq minibuffer-history-variable 'minibuffer-history)
1023 (setq minibuffer-history-position nil)
1024 (defvar minibuffer-history-search-history nil)
1026 (defvar minibuffer-text-before-history nil
1027 "Text that was in this minibuffer before any history commands.
1028 This is nil if there have not yet been any history commands
1029 in this use of the minibuffer.")
1031 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
1033 (defun minibuffer-history-initialize ()
1034 (setq minibuffer-text-before-history nil))
1036 (defun minibuffer-avoid-prompt (new old)
1037 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1038 (constrain-to-field nil (point-max)))
1040 (defcustom minibuffer-history-case-insensitive-variables nil
1041 "*Minibuffer history variables for which matching should ignore case.
1042 If a history variable is a member of this list, then the
1043 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1044 commands ignore case when searching it, regardless of `case-fold-search'."
1045 :type '(repeat variable)
1046 :group 'minibuffer)
1048 (defun previous-matching-history-element (regexp n)
1049 "Find the previous history element that matches REGEXP.
1050 \(Previous history elements refer to earlier actions.)
1051 With prefix argument N, search for Nth previous match.
1052 If N is negative, find the next or Nth next match.
1053 Normally, history elements are matched case-insensitively if
1054 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1055 makes the search case-sensitive.
1056 See also `minibuffer-history-case-insensitive-variables'."
1057 (interactive
1058 (let* ((enable-recursive-minibuffers t)
1059 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1061 minibuffer-local-map
1063 'minibuffer-history-search-history
1064 (car minibuffer-history-search-history))))
1065 ;; Use the last regexp specified, by default, if input is empty.
1066 (list (if (string= regexp "")
1067 (if minibuffer-history-search-history
1068 (car minibuffer-history-search-history)
1069 (error "No previous history search regexp"))
1070 regexp)
1071 (prefix-numeric-value current-prefix-arg))))
1072 (unless (zerop n)
1073 (if (and (zerop minibuffer-history-position)
1074 (null minibuffer-text-before-history))
1075 (setq minibuffer-text-before-history
1076 (minibuffer-contents-no-properties)))
1077 (let ((history (symbol-value minibuffer-history-variable))
1078 (case-fold-search
1079 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
1080 ;; On some systems, ignore case for file names.
1081 (if (memq minibuffer-history-variable
1082 minibuffer-history-case-insensitive-variables)
1084 ;; Respect the user's setting for case-fold-search:
1085 case-fold-search)
1086 nil))
1087 prevpos
1088 match-string
1089 match-offset
1090 (pos minibuffer-history-position))
1091 (while (/= n 0)
1092 (setq prevpos pos)
1093 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1094 (when (= pos prevpos)
1095 (error (if (= pos 1)
1096 "No later matching history item"
1097 "No earlier matching history item")))
1098 (setq match-string
1099 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
1100 (let ((print-level nil))
1101 (prin1-to-string (nth (1- pos) history)))
1102 (nth (1- pos) history)))
1103 (setq match-offset
1104 (if (< n 0)
1105 (and (string-match regexp match-string)
1106 (match-end 0))
1107 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
1108 (match-beginning 1))))
1109 (when match-offset
1110 (setq n (+ n (if (< n 0) 1 -1)))))
1111 (setq minibuffer-history-position pos)
1112 (goto-char (point-max))
1113 (delete-minibuffer-contents)
1114 (insert match-string)
1115 (goto-char (+ (minibuffer-prompt-end) match-offset))))
1116 (if (memq (car (car command-history)) '(previous-matching-history-element
1117 next-matching-history-element))
1118 (setq command-history (cdr command-history))))
1120 (defun next-matching-history-element (regexp n)
1121 "Find the next history element that matches REGEXP.
1122 \(The next history element refers to a more recent action.)
1123 With prefix argument N, search for Nth next match.
1124 If N is negative, find the previous or Nth previous match.
1125 Normally, history elements are matched case-insensitively if
1126 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1127 makes the search case-sensitive."
1128 (interactive
1129 (let* ((enable-recursive-minibuffers t)
1130 (regexp (read-from-minibuffer "Next element matching (regexp): "
1132 minibuffer-local-map
1134 'minibuffer-history-search-history
1135 (car minibuffer-history-search-history))))
1136 ;; Use the last regexp specified, by default, if input is empty.
1137 (list (if (string= regexp "")
1138 (if minibuffer-history-search-history
1139 (car minibuffer-history-search-history)
1140 (error "No previous history search regexp"))
1141 regexp)
1142 (prefix-numeric-value current-prefix-arg))))
1143 (previous-matching-history-element regexp (- n)))
1145 (defvar minibuffer-temporary-goal-position nil)
1147 (defun next-history-element (n)
1148 "Insert the next element of the minibuffer history into the minibuffer."
1149 (interactive "p")
1150 (or (zerop n)
1151 (let ((narg (- minibuffer-history-position n))
1152 (minimum (if minibuffer-default -1 0))
1153 elt minibuffer-returned-to-present)
1154 (if (and (zerop minibuffer-history-position)
1155 (null minibuffer-text-before-history))
1156 (setq minibuffer-text-before-history
1157 (minibuffer-contents-no-properties)))
1158 (if (< narg minimum)
1159 (if minibuffer-default
1160 (error "End of history; no next item")
1161 (error "End of history; no default available")))
1162 (if (> narg (length (symbol-value minibuffer-history-variable)))
1163 (error "Beginning of history; no preceding item"))
1164 (unless (memq last-command '(next-history-element
1165 previous-history-element))
1166 (let ((prompt-end (minibuffer-prompt-end)))
1167 (set (make-local-variable 'minibuffer-temporary-goal-position)
1168 (cond ((<= (point) prompt-end) prompt-end)
1169 ((eobp) nil)
1170 (t (point))))))
1171 (goto-char (point-max))
1172 (delete-minibuffer-contents)
1173 (setq minibuffer-history-position narg)
1174 (cond ((= narg -1)
1175 (setq elt minibuffer-default))
1176 ((= narg 0)
1177 (setq elt (or minibuffer-text-before-history ""))
1178 (setq minibuffer-returned-to-present t)
1179 (setq minibuffer-text-before-history nil))
1180 (t (setq elt (nth (1- minibuffer-history-position)
1181 (symbol-value minibuffer-history-variable)))))
1182 (insert
1183 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
1184 (not minibuffer-returned-to-present))
1185 (let ((print-level nil))
1186 (prin1-to-string elt))
1187 elt))
1188 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
1190 (defun previous-history-element (n)
1191 "Inserts the previous element of the minibuffer history into the minibuffer."
1192 (interactive "p")
1193 (next-history-element (- n)))
1195 (defun next-complete-history-element (n)
1196 "Get next history element which completes the minibuffer before the point.
1197 The contents of the minibuffer after the point are deleted, and replaced
1198 by the new completion."
1199 (interactive "p")
1200 (let ((point-at-start (point)))
1201 (next-matching-history-element
1202 (concat
1203 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1205 ;; next-matching-history-element always puts us at (point-min).
1206 ;; Move to the position we were at before changing the buffer contents.
1207 ;; This is still sensical, because the text before point has not changed.
1208 (goto-char point-at-start)))
1210 (defun previous-complete-history-element (n)
1212 Get previous history element which completes the minibuffer before the point.
1213 The contents of the minibuffer after the point are deleted, and replaced
1214 by the new completion."
1215 (interactive "p")
1216 (next-complete-history-element (- n)))
1218 ;; For compatibility with the old subr of the same name.
1219 (defun minibuffer-prompt-width ()
1220 "Return the display width of the minibuffer prompt.
1221 Return 0 if current buffer is not a mini-buffer."
1222 ;; Return the width of everything before the field at the end of
1223 ;; the buffer; this should be 0 for normal buffers.
1224 (1- (minibuffer-prompt-end)))
1226 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1227 (defalias 'advertised-undo 'undo)
1229 (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t)
1230 "Table mapping redo records to the corresponding undo one.")
1232 (defvar undo-in-region nil
1233 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1235 (defvar undo-no-redo nil
1236 "If t, `undo' doesn't go through redo entries.")
1238 (defun undo (&optional arg)
1239 "Undo some previous changes.
1240 Repeat this command to undo more changes.
1241 A numeric argument serves as a repeat count.
1243 In Transient Mark mode when the mark is active, only undo changes within
1244 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1245 as an argument limits undo to changes within the current region."
1246 (interactive "*P")
1247 ;; Make last-command indicate for the next command that this was an undo.
1248 ;; That way, another undo will undo more.
1249 ;; If we get to the end of the undo history and get an error,
1250 ;; another undo command will find the undo history empty
1251 ;; and will get another error. To begin undoing the undos,
1252 ;; you must type some other command.
1253 (let ((modified (buffer-modified-p))
1254 (recent-save (recent-auto-save-p)))
1255 ;; If we get an error in undo-start,
1256 ;; the next command should not be a "consecutive undo".
1257 ;; So set `this-command' to something other than `undo'.
1258 (setq this-command 'undo-start)
1260 (unless (and (eq last-command 'undo)
1261 ;; If something (a timer or filter?) changed the buffer
1262 ;; since the previous command, don't continue the undo seq.
1263 (let ((list buffer-undo-list))
1264 (while (eq (car list) nil)
1265 (setq list (cdr list)))
1266 ;; If the last undo record made was made by undo
1267 ;; it shows nothing else happened in between.
1268 (gethash list undo-equiv-table)))
1269 (setq undo-in-region
1270 (if transient-mark-mode mark-active (and arg (not (numberp arg)))))
1271 (if undo-in-region
1272 (undo-start (region-beginning) (region-end))
1273 (undo-start))
1274 ;; get rid of initial undo boundary
1275 (undo-more 1))
1276 ;; If we got this far, the next command should be a consecutive undo.
1277 (setq this-command 'undo)
1278 ;; Check to see whether we're hitting a redo record, and if
1279 ;; so, ask the user whether she wants to skip the redo/undo pair.
1280 (let ((equiv (gethash pending-undo-list undo-equiv-table)))
1281 (or (eq (selected-window) (minibuffer-window))
1282 (message (if undo-in-region
1283 (if equiv "Redo in region!" "Undo in region!")
1284 (if equiv "Redo!" "Undo!"))))
1285 (when (and equiv undo-no-redo)
1286 ;; The equiv entry might point to another redo record if we have done
1287 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1288 (while (let ((next (gethash equiv undo-equiv-table)))
1289 (if next (setq equiv next))))
1290 (setq pending-undo-list equiv)))
1291 (undo-more
1292 (if (or transient-mark-mode (numberp arg))
1293 (prefix-numeric-value arg)
1295 ;; Record the fact that the just-generated undo records come from an
1296 ;; undo operation, so we can skip them later on.
1297 ;; I don't know how to do that in the undo-in-region case.
1298 (unless undo-in-region
1299 (puthash buffer-undo-list pending-undo-list undo-equiv-table))
1300 ;; Don't specify a position in the undo record for the undo command.
1301 ;; Instead, undoing this should move point to where the change is.
1302 (let ((tail buffer-undo-list)
1303 (prev nil))
1304 (while (car tail)
1305 (when (integerp (car tail))
1306 (let ((pos (car tail)))
1307 (if prev
1308 (setcdr prev (cdr tail))
1309 (setq buffer-undo-list (cdr tail)))
1310 (setq tail (cdr tail))
1311 (while (car tail)
1312 (if (eq pos (car tail))
1313 (if prev
1314 (setcdr prev (cdr tail))
1315 (setq buffer-undo-list (cdr tail)))
1316 (setq prev tail))
1317 (setq tail (cdr tail)))
1318 (setq tail nil)))
1319 (setq prev tail tail (cdr tail))))
1320 ;; Record what the current undo list says,
1321 ;; so the next command can tell if the buffer was modified in between.
1322 (and modified (not (buffer-modified-p))
1323 (delete-auto-save-file-if-necessary recent-save))))
1325 (defun buffer-disable-undo (&optional buffer)
1326 "Make BUFFER stop keeping undo information.
1327 No argument or nil as argument means do this for the current buffer."
1328 (interactive)
1329 (with-current-buffer (if buffer (get-buffer buffer) (current-buffer))
1330 (setq buffer-undo-list t)))
1332 (defun undo-only (&optional arg)
1333 "Undo some previous changes.
1334 Repeat this command to undo more changes.
1335 A numeric argument serves as a repeat count.
1336 Contrary to `undo', this will not redo a previous undo."
1337 (interactive "*p")
1338 (let ((undo-no-redo t)) (undo arg)))
1339 ;; Richard said that we should not use C-x <uppercase letter> and I have
1340 ;; no idea whereas to bind it. Any suggestion welcome. -stef
1341 ;; (define-key ctl-x-map "U" 'undo-only)
1343 (defvar pending-undo-list nil
1344 "Within a run of consecutive undo commands, list remaining to be undone.")
1346 (defvar undo-in-progress nil
1347 "Non-nil while performing an undo.
1348 Some change-hooks test this variable to do something different.")
1350 (defun undo-more (count)
1351 "Undo back N undo-boundaries beyond what was already undone recently.
1352 Call `undo-start' to get ready to undo recent changes,
1353 then call `undo-more' one or more times to undo them."
1354 (or pending-undo-list
1355 (error (format "No further undo information%s"
1356 (if (and transient-mark-mode mark-active)
1357 " for region" ""))))
1358 (let ((undo-in-progress t))
1359 (setq pending-undo-list (primitive-undo count pending-undo-list))))
1361 ;; Deep copy of a list
1362 (defun undo-copy-list (list)
1363 "Make a copy of undo list LIST."
1364 (mapcar 'undo-copy-list-1 list))
1366 (defun undo-copy-list-1 (elt)
1367 (if (consp elt)
1368 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1369 elt))
1371 (defun undo-start (&optional beg end)
1372 "Set `pending-undo-list' to the front of the undo list.
1373 The next call to `undo-more' will undo the most recently made change.
1374 If BEG and END are specified, then only undo elements
1375 that apply to text between BEG and END are used; other undo elements
1376 are ignored. If BEG and END are nil, all undo elements are used."
1377 (if (eq buffer-undo-list t)
1378 (error "No undo information in this buffer"))
1379 (setq pending-undo-list
1380 (if (and beg end (not (= beg end)))
1381 (undo-make-selective-list (min beg end) (max beg end))
1382 buffer-undo-list)))
1384 (defvar undo-adjusted-markers)
1386 (defun undo-make-selective-list (start end)
1387 "Return a list of undo elements for the region START to END.
1388 The elements come from `buffer-undo-list', but we keep only
1389 the elements inside this region, and discard those outside this region.
1390 If we find an element that crosses an edge of this region,
1391 we stop and ignore all further elements."
1392 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1393 (undo-list (list nil))
1394 undo-adjusted-markers
1395 some-rejected
1396 undo-elt undo-elt temp-undo-list delta)
1397 (while undo-list-copy
1398 (setq undo-elt (car undo-list-copy))
1399 (let ((keep-this
1400 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1401 ;; This is a "was unmodified" element.
1402 ;; Keep it if we have kept everything thus far.
1403 (not some-rejected))
1405 (undo-elt-in-region undo-elt start end)))))
1406 (if keep-this
1407 (progn
1408 (setq end (+ end (cdr (undo-delta undo-elt))))
1409 ;; Don't put two nils together in the list
1410 (if (not (and (eq (car undo-list) nil)
1411 (eq undo-elt nil)))
1412 (setq undo-list (cons undo-elt undo-list))))
1413 (if (undo-elt-crosses-region undo-elt start end)
1414 (setq undo-list-copy nil)
1415 (setq some-rejected t)
1416 (setq temp-undo-list (cdr undo-list-copy))
1417 (setq delta (undo-delta undo-elt))
1419 (when (/= (cdr delta) 0)
1420 (let ((position (car delta))
1421 (offset (cdr delta)))
1423 ;; Loop down the earlier events adjusting their buffer
1424 ;; positions to reflect the fact that a change to the buffer
1425 ;; isn't being undone. We only need to process those element
1426 ;; types which undo-elt-in-region will return as being in
1427 ;; the region since only those types can ever get into the
1428 ;; output
1430 (while temp-undo-list
1431 (setq undo-elt (car temp-undo-list))
1432 (cond ((integerp undo-elt)
1433 (if (>= undo-elt position)
1434 (setcar temp-undo-list (- undo-elt offset))))
1435 ((atom undo-elt) nil)
1436 ((stringp (car undo-elt))
1437 ;; (TEXT . POSITION)
1438 (let ((text-pos (abs (cdr undo-elt)))
1439 (point-at-end (< (cdr undo-elt) 0 )))
1440 (if (>= text-pos position)
1441 (setcdr undo-elt (* (if point-at-end -1 1)
1442 (- text-pos offset))))))
1443 ((integerp (car undo-elt))
1444 ;; (BEGIN . END)
1445 (when (>= (car undo-elt) position)
1446 (setcar undo-elt (- (car undo-elt) offset))
1447 (setcdr undo-elt (- (cdr undo-elt) offset))))
1448 ((null (car undo-elt))
1449 ;; (nil PROPERTY VALUE BEG . END)
1450 (let ((tail (nthcdr 3 undo-elt)))
1451 (when (>= (car tail) position)
1452 (setcar tail (- (car tail) offset))
1453 (setcdr tail (- (cdr tail) offset))))))
1454 (setq temp-undo-list (cdr temp-undo-list))))))))
1455 (setq undo-list-copy (cdr undo-list-copy)))
1456 (nreverse undo-list)))
1458 (defun undo-elt-in-region (undo-elt start end)
1459 "Determine whether UNDO-ELT falls inside the region START ... END.
1460 If it crosses the edge, we return nil."
1461 (cond ((integerp undo-elt)
1462 (and (>= undo-elt start)
1463 (<= undo-elt end)))
1464 ((eq undo-elt nil)
1466 ((atom undo-elt)
1467 nil)
1468 ((stringp (car undo-elt))
1469 ;; (TEXT . POSITION)
1470 (and (>= (abs (cdr undo-elt)) start)
1471 (< (abs (cdr undo-elt)) end)))
1472 ((and (consp undo-elt) (markerp (car undo-elt)))
1473 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1474 ;; See if MARKER is inside the region.
1475 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1476 (unless alist-elt
1477 (setq alist-elt (cons (car undo-elt)
1478 (marker-position (car undo-elt))))
1479 (setq undo-adjusted-markers
1480 (cons alist-elt undo-adjusted-markers)))
1481 (and (cdr alist-elt)
1482 (>= (cdr alist-elt) start)
1483 (<= (cdr alist-elt) end))))
1484 ((null (car undo-elt))
1485 ;; (nil PROPERTY VALUE BEG . END)
1486 (let ((tail (nthcdr 3 undo-elt)))
1487 (and (>= (car tail) start)
1488 (<= (cdr tail) end))))
1489 ((integerp (car undo-elt))
1490 ;; (BEGIN . END)
1491 (and (>= (car undo-elt) start)
1492 (<= (cdr undo-elt) end)))))
1494 (defun undo-elt-crosses-region (undo-elt start end)
1495 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1496 This assumes we have already decided that UNDO-ELT
1497 is not *inside* the region START...END."
1498 (cond ((atom undo-elt) nil)
1499 ((null (car undo-elt))
1500 ;; (nil PROPERTY VALUE BEG . END)
1501 (let ((tail (nthcdr 3 undo-elt)))
1502 (not (or (< (car tail) end)
1503 (> (cdr tail) start)))))
1504 ((integerp (car undo-elt))
1505 ;; (BEGIN . END)
1506 (not (or (< (car undo-elt) end)
1507 (> (cdr undo-elt) start))))))
1509 ;; Return the first affected buffer position and the delta for an undo element
1510 ;; delta is defined as the change in subsequent buffer positions if we *did*
1511 ;; the undo.
1512 (defun undo-delta (undo-elt)
1513 (if (consp undo-elt)
1514 (cond ((stringp (car undo-elt))
1515 ;; (TEXT . POSITION)
1516 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1517 ((integerp (car undo-elt))
1518 ;; (BEGIN . END)
1519 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1521 '(0 . 0)))
1522 '(0 . 0)))
1524 (defvar undo-extra-outer-limit nil
1525 "If non-nil, an extra level of size that's ok in an undo item.
1526 We don't ask the user about truncating the undo list until the
1527 current item gets bigger than this amount.")
1528 (make-variable-buffer-local 'undo-extra-outer-limit)
1530 ;; When the first undo batch in an undo list is longer than undo-outer-limit,
1531 ;; this function gets called to ask the user what to do.
1532 ;; Garbage collection is inhibited around the call,
1533 ;; so it had better not do a lot of consing.
1534 (setq undo-outer-limit-function 'undo-outer-limit-truncate)
1535 (defun undo-outer-limit-truncate (size)
1536 (when (or (null undo-extra-outer-limit)
1537 (> size undo-extra-outer-limit))
1538 ;; Don't ask the question again unless it gets even bigger.
1539 ;; This applies, in particular, if the user quits from the question.
1540 ;; Such a quit quits out of GC, but something else will call GC
1541 ;; again momentarily. It will call this function again,
1542 ;; but we don't want to ask the question again.
1543 (setq undo-extra-outer-limit (+ size 50000))
1544 (if (let (use-dialog-box)
1545 (yes-or-no-p (format "Buffer %s undo info is %d bytes long; discard it? "
1546 (buffer-name) size)))
1547 (progn (setq buffer-undo-list nil)
1548 (setq undo-extra-outer-limit nil)
1550 nil)))
1552 (defvar shell-command-history nil
1553 "History list for some commands that read shell commands.")
1555 (defvar shell-command-switch "-c"
1556 "Switch used to have the shell execute its command line argument.")
1558 (defvar shell-command-default-error-buffer nil
1559 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1560 This buffer is used when `shell-command' or `shell-command-on-region'
1561 is run interactively. A value of nil means that output to stderr and
1562 stdout will be intermixed in the output stream.")
1564 (defun shell-command (command &optional output-buffer error-buffer)
1565 "Execute string COMMAND in inferior shell; display output, if any.
1566 With prefix argument, insert the COMMAND's output at point.
1568 If COMMAND ends in ampersand, execute it asynchronously.
1569 The output appears in the buffer `*Async Shell Command*'.
1570 That buffer is in shell mode.
1572 Otherwise, COMMAND is executed synchronously. The output appears in
1573 the buffer `*Shell Command Output*'. If the output is short enough to
1574 display in the echo area (which is determined by the variables
1575 `resize-mini-windows' and `max-mini-window-height'), it is shown
1576 there, but it is nonetheless available in buffer `*Shell Command
1577 Output*' even though that buffer is not automatically displayed.
1579 To specify a coding system for converting non-ASCII characters
1580 in the shell command output, use \\[universal-coding-system-argument]
1581 before this command.
1583 Noninteractive callers can specify coding systems by binding
1584 `coding-system-for-read' and `coding-system-for-write'.
1586 The optional second argument OUTPUT-BUFFER, if non-nil,
1587 says to put the output in some other buffer.
1588 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1589 If OUTPUT-BUFFER is not a buffer and not nil,
1590 insert output in current buffer. (This cannot be done asynchronously.)
1591 In either case, the output is inserted after point (leaving mark after it).
1593 If the command terminates without error, but generates output,
1594 and you did not specify \"insert it in the current buffer\",
1595 the output can be displayed in the echo area or in its buffer.
1596 If the output is short enough to display in the echo area
1597 \(determined by the variable `max-mini-window-height' if
1598 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1599 the buffer containing the output is displayed.
1601 If there is output and an error, and you did not specify \"insert it
1602 in the current buffer\", a message about the error goes at the end
1603 of the output.
1605 If there is no output, or if output is inserted in the current buffer,
1606 then `*Shell Command Output*' is deleted.
1608 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1609 or buffer name to which to direct the command's standard error output.
1610 If it is nil, error output is mingled with regular output.
1611 In an interactive call, the variable `shell-command-default-error-buffer'
1612 specifies the value of ERROR-BUFFER."
1614 (interactive (list (read-from-minibuffer "Shell command: "
1615 nil nil nil 'shell-command-history)
1616 current-prefix-arg
1617 shell-command-default-error-buffer))
1618 ;; Look for a handler in case default-directory is a remote file name.
1619 (let ((handler
1620 (find-file-name-handler (directory-file-name default-directory)
1621 'shell-command)))
1622 (if handler
1623 (funcall handler 'shell-command command output-buffer error-buffer)
1624 (if (and output-buffer
1625 (not (or (bufferp output-buffer) (stringp output-buffer))))
1626 ;; Output goes in current buffer.
1627 (let ((error-file
1628 (if error-buffer
1629 (make-temp-file
1630 (expand-file-name "scor"
1631 (or small-temporary-file-directory
1632 temporary-file-directory)))
1633 nil)))
1634 (barf-if-buffer-read-only)
1635 (push-mark nil t)
1636 ;; We do not use -f for csh; we will not support broken use of
1637 ;; .cshrcs. Even the BSD csh manual says to use
1638 ;; "if ($?prompt) exit" before things which are not useful
1639 ;; non-interactively. Besides, if someone wants their other
1640 ;; aliases for shell commands then they can still have them.
1641 (call-process shell-file-name nil
1642 (if error-file
1643 (list t error-file)
1645 nil shell-command-switch command)
1646 (when (and error-file (file-exists-p error-file))
1647 (if (< 0 (nth 7 (file-attributes error-file)))
1648 (with-current-buffer (get-buffer-create error-buffer)
1649 (let ((pos-from-end (- (point-max) (point))))
1650 (or (bobp)
1651 (insert "\f\n"))
1652 ;; Do no formatting while reading error file,
1653 ;; because that can run a shell command, and we
1654 ;; don't want that to cause an infinite recursion.
1655 (format-insert-file error-file nil)
1656 ;; Put point after the inserted errors.
1657 (goto-char (- (point-max) pos-from-end)))
1658 (display-buffer (current-buffer))))
1659 (delete-file error-file))
1660 ;; This is like exchange-point-and-mark, but doesn't
1661 ;; activate the mark. It is cleaner to avoid activation,
1662 ;; even though the command loop would deactivate the mark
1663 ;; because we inserted text.
1664 (goto-char (prog1 (mark t)
1665 (set-marker (mark-marker) (point)
1666 (current-buffer)))))
1667 ;; Output goes in a separate buffer.
1668 ;; Preserve the match data in case called from a program.
1669 (save-match-data
1670 (if (string-match "[ \t]*&[ \t]*\\'" command)
1671 ;; Command ending with ampersand means asynchronous.
1672 (let ((buffer (get-buffer-create
1673 (or output-buffer "*Async Shell Command*")))
1674 (directory default-directory)
1675 proc)
1676 ;; Remove the ampersand.
1677 (setq command (substring command 0 (match-beginning 0)))
1678 ;; If will kill a process, query first.
1679 (setq proc (get-buffer-process buffer))
1680 (if proc
1681 (if (yes-or-no-p "A command is running. Kill it? ")
1682 (kill-process proc)
1683 (error "Shell command in progress")))
1684 (with-current-buffer buffer
1685 (setq buffer-read-only nil)
1686 (erase-buffer)
1687 (display-buffer buffer)
1688 (setq default-directory directory)
1689 (setq proc (start-process "Shell" buffer shell-file-name
1690 shell-command-switch command))
1691 (setq mode-line-process '(":%s"))
1692 (require 'shell) (shell-mode)
1693 (set-process-sentinel proc 'shell-command-sentinel)
1695 (shell-command-on-region (point) (point) command
1696 output-buffer nil error-buffer)))))))
1698 (defun display-message-or-buffer (message
1699 &optional buffer-name not-this-window frame)
1700 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1701 MESSAGE may be either a string or a buffer.
1703 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1704 the maximum height of the echo area, as defined by `max-mini-window-height'
1705 if `resize-mini-windows' is non-nil.
1707 Returns either the string shown in the echo area, or when a pop-up
1708 buffer is used, the window used to display it.
1710 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1711 name of the buffer used to display it in the case where a pop-up buffer
1712 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1713 string and it is displayed in the echo area, it is not specified whether
1714 the contents are inserted into the buffer anyway.
1716 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1717 and only used if a buffer is displayed."
1718 (cond ((and (stringp message) (not (string-match "\n" message)))
1719 ;; Trivial case where we can use the echo area
1720 (message "%s" message))
1721 ((and (stringp message)
1722 (= (string-match "\n" message) (1- (length message))))
1723 ;; Trivial case where we can just remove single trailing newline
1724 (message "%s" (substring message 0 (1- (length message)))))
1726 ;; General case
1727 (with-current-buffer
1728 (if (bufferp message)
1729 message
1730 (get-buffer-create (or buffer-name "*Message*")))
1732 (unless (bufferp message)
1733 (erase-buffer)
1734 (insert message))
1736 (let ((lines
1737 (if (= (buffer-size) 0)
1739 (count-lines (point-min) (point-max)))))
1740 (cond ((= lines 0))
1741 ((and (or (<= lines 1)
1742 (<= lines
1743 (if resize-mini-windows
1744 (cond ((floatp max-mini-window-height)
1745 (* (frame-height)
1746 max-mini-window-height))
1747 ((integerp max-mini-window-height)
1748 max-mini-window-height)
1751 1)))
1752 ;; Don't use the echo area if the output buffer is
1753 ;; already dispayed in the selected frame.
1754 (not (get-buffer-window (current-buffer))))
1755 ;; Echo area
1756 (goto-char (point-max))
1757 (when (bolp)
1758 (backward-char 1))
1759 (message "%s" (buffer-substring (point-min) (point))))
1761 ;; Buffer
1762 (goto-char (point-min))
1763 (display-buffer (current-buffer)
1764 not-this-window frame))))))))
1767 ;; We have a sentinel to prevent insertion of a termination message
1768 ;; in the buffer itself.
1769 (defun shell-command-sentinel (process signal)
1770 (if (memq (process-status process) '(exit signal))
1771 (message "%s: %s."
1772 (car (cdr (cdr (process-command process))))
1773 (substring signal 0 -1))))
1775 (defun shell-command-on-region (start end command
1776 &optional output-buffer replace
1777 error-buffer display-error-buffer)
1778 "Execute string COMMAND in inferior shell with region as input.
1779 Normally display output (if any) in temp buffer `*Shell Command Output*';
1780 Prefix arg means replace the region with it. Return the exit code of
1781 COMMAND.
1783 To specify a coding system for converting non-ASCII characters
1784 in the input and output to the shell command, use \\[universal-coding-system-argument]
1785 before this command. By default, the input (from the current buffer)
1786 is encoded in the same coding system that will be used to save the file,
1787 `buffer-file-coding-system'. If the output is going to replace the region,
1788 then it is decoded from that same coding system.
1790 The noninteractive arguments are START, END, COMMAND,
1791 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
1792 Noninteractive callers can specify coding systems by binding
1793 `coding-system-for-read' and `coding-system-for-write'.
1795 If the command generates output, the output may be displayed
1796 in the echo area or in a buffer.
1797 If the output is short enough to display in the echo area
1798 \(determined by the variable `max-mini-window-height' if
1799 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1800 it is displayed in the buffer `*Shell Command Output*'. The output
1801 is available in that buffer in both cases.
1803 If there is output and an error, a message about the error
1804 appears at the end of the output.
1806 If there is no output, or if output is inserted in the current buffer,
1807 then `*Shell Command Output*' is deleted.
1809 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1810 that says to put the output in some other buffer.
1811 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1812 If OUTPUT-BUFFER is not a buffer and not nil,
1813 insert output in the current buffer.
1814 In either case, the output is inserted after point (leaving mark after it).
1816 If REPLACE, the optional fifth argument, is non-nil, that means insert
1817 the output in place of text from START to END, putting point and mark
1818 around it.
1820 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1821 or buffer name to which to direct the command's standard error output.
1822 If it is nil, error output is mingled with regular output.
1823 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
1824 were any errors. (This is always t, interactively.)
1825 In an interactive call, the variable `shell-command-default-error-buffer'
1826 specifies the value of ERROR-BUFFER."
1827 (interactive (let (string)
1828 (unless (mark)
1829 (error "The mark is not set now, so there is no region"))
1830 ;; Do this before calling region-beginning
1831 ;; and region-end, in case subprocess output
1832 ;; relocates them while we are in the minibuffer.
1833 (setq string (read-from-minibuffer "Shell command on region: "
1834 nil nil nil
1835 'shell-command-history))
1836 ;; call-interactively recognizes region-beginning and
1837 ;; region-end specially, leaving them in the history.
1838 (list (region-beginning) (region-end)
1839 string
1840 current-prefix-arg
1841 current-prefix-arg
1842 shell-command-default-error-buffer
1843 t)))
1844 (let ((error-file
1845 (if error-buffer
1846 (make-temp-file
1847 (expand-file-name "scor"
1848 (or small-temporary-file-directory
1849 temporary-file-directory)))
1850 nil))
1851 exit-status)
1852 (if (or replace
1853 (and output-buffer
1854 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1855 ;; Replace specified region with output from command.
1856 (let ((swap (and replace (< start end))))
1857 ;; Don't muck with mark unless REPLACE says we should.
1858 (goto-char start)
1859 (and replace (push-mark (point) 'nomsg))
1860 (setq exit-status
1861 (call-process-region start end shell-file-name t
1862 (if error-file
1863 (list t error-file)
1865 nil shell-command-switch command))
1866 ;; It is rude to delete a buffer which the command is not using.
1867 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1868 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1869 ;; (kill-buffer shell-buffer)))
1870 ;; Don't muck with mark unless REPLACE says we should.
1871 (and replace swap (exchange-point-and-mark)))
1872 ;; No prefix argument: put the output in a temp buffer,
1873 ;; replacing its entire contents.
1874 (let ((buffer (get-buffer-create
1875 (or output-buffer "*Shell Command Output*"))))
1876 (unwind-protect
1877 (if (eq buffer (current-buffer))
1878 ;; If the input is the same buffer as the output,
1879 ;; delete everything but the specified region,
1880 ;; then replace that region with the output.
1881 (progn (setq buffer-read-only nil)
1882 (delete-region (max start end) (point-max))
1883 (delete-region (point-min) (min start end))
1884 (setq exit-status
1885 (call-process-region (point-min) (point-max)
1886 shell-file-name t
1887 (if error-file
1888 (list t error-file)
1890 nil shell-command-switch
1891 command)))
1892 ;; Clear the output buffer, then run the command with
1893 ;; output there.
1894 (let ((directory default-directory))
1895 (save-excursion
1896 (set-buffer buffer)
1897 (setq buffer-read-only nil)
1898 (if (not output-buffer)
1899 (setq default-directory directory))
1900 (erase-buffer)))
1901 (setq exit-status
1902 (call-process-region start end shell-file-name nil
1903 (if error-file
1904 (list buffer error-file)
1905 buffer)
1906 nil shell-command-switch command)))
1907 ;; Report the output.
1908 (with-current-buffer buffer
1909 (setq mode-line-process
1910 (cond ((null exit-status)
1911 " - Error")
1912 ((stringp exit-status)
1913 (format " - Signal [%s]" exit-status))
1914 ((not (equal 0 exit-status))
1915 (format " - Exit [%d]" exit-status)))))
1916 (if (with-current-buffer buffer (> (point-max) (point-min)))
1917 ;; There's some output, display it
1918 (display-message-or-buffer buffer)
1919 ;; No output; error?
1920 (let ((output
1921 (if (and error-file
1922 (< 0 (nth 7 (file-attributes error-file))))
1923 "some error output"
1924 "no output")))
1925 (cond ((null exit-status)
1926 (message "(Shell command failed with error)"))
1927 ((equal 0 exit-status)
1928 (message "(Shell command succeeded with %s)"
1929 output))
1930 ((stringp exit-status)
1931 (message "(Shell command killed by signal %s)"
1932 exit-status))
1934 (message "(Shell command failed with code %d and %s)"
1935 exit-status output))))
1936 ;; Don't kill: there might be useful info in the undo-log.
1937 ;; (kill-buffer buffer)
1938 ))))
1940 (when (and error-file (file-exists-p error-file))
1941 (if (< 0 (nth 7 (file-attributes error-file)))
1942 (with-current-buffer (get-buffer-create error-buffer)
1943 (let ((pos-from-end (- (point-max) (point))))
1944 (or (bobp)
1945 (insert "\f\n"))
1946 ;; Do no formatting while reading error file,
1947 ;; because that can run a shell command, and we
1948 ;; don't want that to cause an infinite recursion.
1949 (format-insert-file error-file nil)
1950 ;; Put point after the inserted errors.
1951 (goto-char (- (point-max) pos-from-end)))
1952 (and display-error-buffer
1953 (display-buffer (current-buffer)))))
1954 (delete-file error-file))
1955 exit-status))
1957 (defun shell-command-to-string (command)
1958 "Execute shell command COMMAND and return its output as a string."
1959 (with-output-to-string
1960 (with-current-buffer
1961 standard-output
1962 (call-process shell-file-name nil t nil shell-command-switch command))))
1964 (defun process-file (program &optional infile buffer display &rest args)
1965 "Process files synchronously in a separate process.
1966 Similar to `call-process', but may invoke a file handler based on
1967 `default-directory'. The current working directory of the
1968 subprocess is `default-directory'.
1970 File names in INFILE and BUFFER are handled normally, but file
1971 names in ARGS should be relative to `default-directory', as they
1972 are passed to the process verbatim. \(This is a difference to
1973 `call-process' which does not support file handlers for INFILE
1974 and BUFFER.\)
1976 Some file handlers might not support all variants, for example
1977 they might behave as if DISPLAY was nil, regardless of the actual
1978 value passed."
1979 (let ((fh (find-file-name-handler default-directory 'process-file))
1980 lc stderr-file)
1981 (unwind-protect
1982 (if fh (apply fh 'process-file program infile buffer display args)
1983 (when infile (setq lc (file-local-copy infile)))
1984 (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer)))
1985 (make-temp-file "emacs")))
1986 (prog1
1987 (apply 'call-process program
1988 (or lc infile)
1989 (if stderr-file (list (car buffer) stderr-file) buffer)
1990 display args)
1991 (when stderr-file (copy-file stderr-file (cadr buffer)))))
1992 (when stderr-file (delete-file stderr-file))
1993 (when lc (delete-file lc)))))
1997 (defvar universal-argument-map
1998 (let ((map (make-sparse-keymap)))
1999 (define-key map [t] 'universal-argument-other-key)
2000 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
2001 (define-key map [switch-frame] nil)
2002 (define-key map [?\C-u] 'universal-argument-more)
2003 (define-key map [?-] 'universal-argument-minus)
2004 (define-key map [?0] 'digit-argument)
2005 (define-key map [?1] 'digit-argument)
2006 (define-key map [?2] 'digit-argument)
2007 (define-key map [?3] 'digit-argument)
2008 (define-key map [?4] 'digit-argument)
2009 (define-key map [?5] 'digit-argument)
2010 (define-key map [?6] 'digit-argument)
2011 (define-key map [?7] 'digit-argument)
2012 (define-key map [?8] 'digit-argument)
2013 (define-key map [?9] 'digit-argument)
2014 (define-key map [kp-0] 'digit-argument)
2015 (define-key map [kp-1] 'digit-argument)
2016 (define-key map [kp-2] 'digit-argument)
2017 (define-key map [kp-3] 'digit-argument)
2018 (define-key map [kp-4] 'digit-argument)
2019 (define-key map [kp-5] 'digit-argument)
2020 (define-key map [kp-6] 'digit-argument)
2021 (define-key map [kp-7] 'digit-argument)
2022 (define-key map [kp-8] 'digit-argument)
2023 (define-key map [kp-9] 'digit-argument)
2024 (define-key map [kp-subtract] 'universal-argument-minus)
2025 map)
2026 "Keymap used while processing \\[universal-argument].")
2028 (defvar universal-argument-num-events nil
2029 "Number of argument-specifying events read by `universal-argument'.
2030 `universal-argument-other-key' uses this to discard those events
2031 from (this-command-keys), and reread only the final command.")
2033 (defvar overriding-map-is-bound nil
2034 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
2036 (defvar saved-overriding-map nil
2037 "The saved value of `overriding-terminal-local-map'.
2038 That variable gets restored to this value on exiting \"universal
2039 argument mode\".")
2041 (defun ensure-overriding-map-is-bound ()
2042 "Check `overriding-terminal-local-map' is `universal-argument-map'."
2043 (unless overriding-map-is-bound
2044 (setq saved-overriding-map overriding-terminal-local-map)
2045 (setq overriding-terminal-local-map universal-argument-map)
2046 (setq overriding-map-is-bound t)))
2048 (defun restore-overriding-map ()
2049 "Restore `overriding-terminal-local-map' to its saved value."
2050 (setq overriding-terminal-local-map saved-overriding-map)
2051 (setq overriding-map-is-bound nil))
2053 (defun universal-argument ()
2054 "Begin a numeric argument for the following command.
2055 Digits or minus sign following \\[universal-argument] make up the numeric argument.
2056 \\[universal-argument] following the digits or minus sign ends the argument.
2057 \\[universal-argument] without digits or minus sign provides 4 as argument.
2058 Repeating \\[universal-argument] without digits or minus sign
2059 multiplies the argument by 4 each time.
2060 For some commands, just \\[universal-argument] by itself serves as a flag
2061 which is different in effect from any particular numeric argument.
2062 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
2063 (interactive)
2064 (setq prefix-arg (list 4))
2065 (setq universal-argument-num-events (length (this-command-keys)))
2066 (ensure-overriding-map-is-bound))
2068 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
2069 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
2070 (defun universal-argument-more (arg)
2071 (interactive "P")
2072 (if (consp arg)
2073 (setq prefix-arg (list (* 4 (car arg))))
2074 (if (eq arg '-)
2075 (setq prefix-arg (list -4))
2076 (setq prefix-arg arg)
2077 (restore-overriding-map)))
2078 (setq universal-argument-num-events (length (this-command-keys))))
2080 (defun negative-argument (arg)
2081 "Begin a negative numeric argument for the next command.
2082 \\[universal-argument] following digits or minus sign ends the argument."
2083 (interactive "P")
2084 (cond ((integerp arg)
2085 (setq prefix-arg (- arg)))
2086 ((eq arg '-)
2087 (setq prefix-arg nil))
2089 (setq prefix-arg '-)))
2090 (setq universal-argument-num-events (length (this-command-keys)))
2091 (ensure-overriding-map-is-bound))
2093 (defun digit-argument (arg)
2094 "Part of the numeric argument for the next command.
2095 \\[universal-argument] following digits or minus sign ends the argument."
2096 (interactive "P")
2097 (let* ((char (if (integerp last-command-char)
2098 last-command-char
2099 (get last-command-char 'ascii-character)))
2100 (digit (- (logand char ?\177) ?0)))
2101 (cond ((integerp arg)
2102 (setq prefix-arg (+ (* arg 10)
2103 (if (< arg 0) (- digit) digit))))
2104 ((eq arg '-)
2105 ;; Treat -0 as just -, so that -01 will work.
2106 (setq prefix-arg (if (zerop digit) '- (- digit))))
2108 (setq prefix-arg digit))))
2109 (setq universal-argument-num-events (length (this-command-keys)))
2110 (ensure-overriding-map-is-bound))
2112 ;; For backward compatibility, minus with no modifiers is an ordinary
2113 ;; command if digits have already been entered.
2114 (defun universal-argument-minus (arg)
2115 (interactive "P")
2116 (if (integerp arg)
2117 (universal-argument-other-key arg)
2118 (negative-argument arg)))
2120 ;; Anything else terminates the argument and is left in the queue to be
2121 ;; executed as a command.
2122 (defun universal-argument-other-key (arg)
2123 (interactive "P")
2124 (setq prefix-arg arg)
2125 (let* ((key (this-command-keys))
2126 (keylist (listify-key-sequence key)))
2127 (setq unread-command-events
2128 (append (nthcdr universal-argument-num-events keylist)
2129 unread-command-events)))
2130 (reset-this-command-lengths)
2131 (restore-overriding-map))
2133 ;;;; Window system cut and paste hooks.
2135 (defvar interprogram-cut-function nil
2136 "Function to call to make a killed region available to other programs.
2138 Most window systems provide some sort of facility for cutting and
2139 pasting text between the windows of different programs.
2140 This variable holds a function that Emacs calls whenever text
2141 is put in the kill ring, to make the new kill available to other
2142 programs.
2144 The function takes one or two arguments.
2145 The first argument, TEXT, is a string containing
2146 the text which should be made available.
2147 The second, optional, argument PUSH, has the same meaning as the
2148 similar argument to `x-set-cut-buffer', which see.")
2150 (defvar interprogram-paste-function nil
2151 "Function to call to get text cut from other programs.
2153 Most window systems provide some sort of facility for cutting and
2154 pasting text between the windows of different programs.
2155 This variable holds a function that Emacs calls to obtain
2156 text that other programs have provided for pasting.
2158 The function should be called with no arguments. If the function
2159 returns nil, then no other program has provided such text, and the top
2160 of the Emacs kill ring should be used. If the function returns a
2161 string, then the caller of the function \(usually `current-kill')
2162 should put this string in the kill ring as the latest kill.
2164 Note that the function should return a string only if a program other
2165 than Emacs has provided a string for pasting; if Emacs provided the
2166 most recent string, the function should return nil. If it is
2167 difficult to tell whether Emacs or some other program provided the
2168 current string, it is probably good enough to return nil if the string
2169 is equal (according to `string=') to the last text Emacs provided.")
2173 ;;;; The kill ring data structure.
2175 (defvar kill-ring nil
2176 "List of killed text sequences.
2177 Since the kill ring is supposed to interact nicely with cut-and-paste
2178 facilities offered by window systems, use of this variable should
2179 interact nicely with `interprogram-cut-function' and
2180 `interprogram-paste-function'. The functions `kill-new',
2181 `kill-append', and `current-kill' are supposed to implement this
2182 interaction; you may want to use them instead of manipulating the kill
2183 ring directly.")
2185 (defcustom kill-ring-max 60
2186 "*Maximum length of kill ring before oldest elements are thrown away."
2187 :type 'integer
2188 :group 'killing)
2190 (defvar kill-ring-yank-pointer nil
2191 "The tail of the kill ring whose car is the last thing yanked.")
2193 (defun kill-new (string &optional replace yank-handler)
2194 "Make STRING the latest kill in the kill ring.
2195 Set `kill-ring-yank-pointer' to point to it.
2196 If `interprogram-cut-function' is non-nil, apply it to STRING.
2197 Optional second argument REPLACE non-nil means that STRING will replace
2198 the front of the kill ring, rather than being added to the list.
2200 Optional third arguments YANK-HANDLER controls how the STRING is later
2201 inserted into a buffer; see `insert-for-yank' for details.
2202 When a yank handler is specified, STRING must be non-empty (the yank
2203 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2205 When the yank handler has a non-nil PARAM element, the original STRING
2206 argument is not used by `insert-for-yank'. However, since Lisp code
2207 may access and use elements from the kill-ring directly, the STRING
2208 argument should still be a \"useful\" string for such uses."
2209 (if (> (length string) 0)
2210 (if yank-handler
2211 (put-text-property 0 (length string)
2212 'yank-handler yank-handler string))
2213 (if yank-handler
2214 (signal 'args-out-of-range
2215 (list string "yank-handler specified for empty string"))))
2216 (if (fboundp 'menu-bar-update-yank-menu)
2217 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2218 (if (and replace kill-ring)
2219 (setcar kill-ring string)
2220 (setq kill-ring (cons string kill-ring))
2221 (if (> (length kill-ring) kill-ring-max)
2222 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2223 (setq kill-ring-yank-pointer kill-ring)
2224 (if interprogram-cut-function
2225 (funcall interprogram-cut-function string (not replace))))
2227 (defun kill-append (string before-p &optional yank-handler)
2228 "Append STRING to the end of the latest kill in the kill ring.
2229 If BEFORE-P is non-nil, prepend STRING to the kill.
2230 Optional third argument YANK-HANDLER, if non-nil, specifies the
2231 yank-handler text property to be set on the combined kill ring
2232 string. If the specified yank-handler arg differs from the
2233 yank-handler property of the latest kill string, this function
2234 adds the combined string to the kill ring as a new element,
2235 instead of replacing the last kill with it.
2236 If `interprogram-cut-function' is set, pass the resulting kill to it."
2237 (let* ((cur (car kill-ring)))
2238 (kill-new (if before-p (concat string cur) (concat cur string))
2239 (or (= (length cur) 0)
2240 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2241 yank-handler)))
2243 (defun current-kill (n &optional do-not-move)
2244 "Rotate the yanking point by N places, and then return that kill.
2245 If N is zero, `interprogram-paste-function' is set, and calling it
2246 returns a string, then that string is added to the front of the
2247 kill ring and returned as the latest kill.
2248 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
2249 yanking point; just return the Nth kill forward."
2250 (let ((interprogram-paste (and (= n 0)
2251 interprogram-paste-function
2252 (funcall interprogram-paste-function))))
2253 (if interprogram-paste
2254 (progn
2255 ;; Disable the interprogram cut function when we add the new
2256 ;; text to the kill ring, so Emacs doesn't try to own the
2257 ;; selection, with identical text.
2258 (let ((interprogram-cut-function nil))
2259 (kill-new interprogram-paste))
2260 interprogram-paste)
2261 (or kill-ring (error "Kill ring is empty"))
2262 (let ((ARGth-kill-element
2263 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2264 (length kill-ring))
2265 kill-ring)))
2266 (or do-not-move
2267 (setq kill-ring-yank-pointer ARGth-kill-element))
2268 (car ARGth-kill-element)))))
2272 ;;;; Commands for manipulating the kill ring.
2274 (defcustom kill-read-only-ok nil
2275 "*Non-nil means don't signal an error for killing read-only text."
2276 :type 'boolean
2277 :group 'killing)
2279 (put 'text-read-only 'error-conditions
2280 '(text-read-only buffer-read-only error))
2281 (put 'text-read-only 'error-message "Text is read-only")
2283 (defun kill-region (beg end &optional yank-handler)
2284 "Kill between point and mark.
2285 The text is deleted but saved in the kill ring.
2286 The command \\[yank] can retrieve it from there.
2287 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2289 If you want to append the killed region to the last killed text,
2290 use \\[append-next-kill] before \\[kill-region].
2292 If the buffer is read-only, Emacs will beep and refrain from deleting
2293 the text, but put the text in the kill ring anyway. This means that
2294 you can use the killing commands to copy text from a read-only buffer.
2296 This is the primitive for programs to kill text (as opposed to deleting it).
2297 Supply two arguments, character positions indicating the stretch of text
2298 to be killed.
2299 Any command that calls this function is a \"kill command\".
2300 If the previous command was also a kill command,
2301 the text killed this time appends to the text killed last time
2302 to make one entry in the kill ring.
2304 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2305 specifies the yank-handler text property to be set on the killed
2306 text. See `insert-for-yank'."
2307 (interactive "r")
2308 (condition-case nil
2309 (let ((string (delete-and-extract-region beg end)))
2310 (when string ;STRING is nil if BEG = END
2311 ;; Add that string to the kill ring, one way or another.
2312 (if (eq last-command 'kill-region)
2313 (kill-append string (< end beg) yank-handler)
2314 (kill-new string nil yank-handler)))
2315 (when (or string (eq last-command 'kill-region))
2316 (setq this-command 'kill-region))
2317 nil)
2318 ((buffer-read-only text-read-only)
2319 ;; The code above failed because the buffer, or some of the characters
2320 ;; in the region, are read-only.
2321 ;; We should beep, in case the user just isn't aware of this.
2322 ;; However, there's no harm in putting
2323 ;; the region's text in the kill ring, anyway.
2324 (copy-region-as-kill beg end)
2325 ;; Set this-command now, so it will be set even if we get an error.
2326 (setq this-command 'kill-region)
2327 ;; This should barf, if appropriate, and give us the correct error.
2328 (if kill-read-only-ok
2329 (progn (message "Read only text copied to kill ring") nil)
2330 ;; Signal an error if the buffer is read-only.
2331 (barf-if-buffer-read-only)
2332 ;; If the buffer isn't read-only, the text is.
2333 (signal 'text-read-only (list (current-buffer)))))))
2335 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2336 ;; to get two copies of the text when the user accidentally types M-w and
2337 ;; then corrects it with the intended C-w.
2338 (defun copy-region-as-kill (beg end)
2339 "Save the region as if killed, but don't kill it.
2340 In Transient Mark mode, deactivate the mark.
2341 If `interprogram-cut-function' is non-nil, also save the text for a window
2342 system cut and paste."
2343 (interactive "r")
2344 (if (eq last-command 'kill-region)
2345 (kill-append (buffer-substring beg end) (< end beg))
2346 (kill-new (buffer-substring beg end)))
2347 (if transient-mark-mode
2348 (setq deactivate-mark t))
2349 nil)
2351 (defun kill-ring-save (beg end)
2352 "Save the region as if killed, but don't kill it.
2353 In Transient Mark mode, deactivate the mark.
2354 If `interprogram-cut-function' is non-nil, also save the text for a window
2355 system cut and paste.
2357 If you want to append the killed line to the last killed text,
2358 use \\[append-next-kill] before \\[kill-ring-save].
2360 This command is similar to `copy-region-as-kill', except that it gives
2361 visual feedback indicating the extent of the region being copied."
2362 (interactive "r")
2363 (copy-region-as-kill beg end)
2364 ;; This use of interactive-p is correct
2365 ;; because the code it controls just gives the user visual feedback.
2366 (if (interactive-p)
2367 (let ((other-end (if (= (point) beg) end beg))
2368 (opoint (point))
2369 ;; Inhibit quitting so we can make a quit here
2370 ;; look like a C-g typed as a command.
2371 (inhibit-quit t))
2372 (if (pos-visible-in-window-p other-end (selected-window))
2373 (unless (and transient-mark-mode
2374 (face-background 'region))
2375 ;; Swap point and mark.
2376 (set-marker (mark-marker) (point) (current-buffer))
2377 (goto-char other-end)
2378 (sit-for blink-matching-delay)
2379 ;; Swap back.
2380 (set-marker (mark-marker) other-end (current-buffer))
2381 (goto-char opoint)
2382 ;; If user quit, deactivate the mark
2383 ;; as C-g would as a command.
2384 (and quit-flag mark-active
2385 (deactivate-mark)))
2386 (let* ((killed-text (current-kill 0))
2387 (message-len (min (length killed-text) 40)))
2388 (if (= (point) beg)
2389 ;; Don't say "killed"; that is misleading.
2390 (message "Saved text until \"%s\""
2391 (substring killed-text (- message-len)))
2392 (message "Saved text from \"%s\""
2393 (substring killed-text 0 message-len))))))))
2395 (defun append-next-kill (&optional interactive)
2396 "Cause following command, if it kills, to append to previous kill.
2397 The argument is used for internal purposes; do not supply one."
2398 (interactive "p")
2399 ;; We don't use (interactive-p), since that breaks kbd macros.
2400 (if interactive
2401 (progn
2402 (setq this-command 'kill-region)
2403 (message "If the next command is a kill, it will append"))
2404 (setq last-command 'kill-region)))
2406 ;; Yanking.
2408 ;; This is actually used in subr.el but defcustom does not work there.
2409 (defcustom yank-excluded-properties
2410 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2411 yank-handler)
2412 "*Text properties to discard when yanking.
2413 The value should be a list of text properties to discard or t,
2414 which means to discard all text properties."
2415 :type '(choice (const :tag "All" t) (repeat symbol))
2416 :group 'killing
2417 :version "21.4")
2419 (defvar yank-window-start nil)
2420 (defvar yank-undo-function nil
2421 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2422 Function is called with two parameters, START and END corresponding to
2423 the value of the mark and point; it is guaranteed that START <= END.
2424 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2426 (defun yank-pop (&optional arg)
2427 "Replace just-yanked stretch of killed text with a different stretch.
2428 This command is allowed only immediately after a `yank' or a `yank-pop'.
2429 At such a time, the region contains a stretch of reinserted
2430 previously-killed text. `yank-pop' deletes that text and inserts in its
2431 place a different stretch of killed text.
2433 With no argument, the previous kill is inserted.
2434 With argument N, insert the Nth previous kill.
2435 If N is negative, this is a more recent kill.
2437 The sequence of kills wraps around, so that after the oldest one
2438 comes the newest one."
2439 (interactive "*p")
2440 (if (not (eq last-command 'yank))
2441 (error "Previous command was not a yank"))
2442 (setq this-command 'yank)
2443 (unless arg (setq arg 1))
2444 (let ((inhibit-read-only t)
2445 (before (< (point) (mark t))))
2446 (if before
2447 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2448 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2449 (setq yank-undo-function nil)
2450 (set-marker (mark-marker) (point) (current-buffer))
2451 (insert-for-yank (current-kill arg))
2452 ;; Set the window start back where it was in the yank command,
2453 ;; if possible.
2454 (set-window-start (selected-window) yank-window-start t)
2455 (if before
2456 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2457 ;; It is cleaner to avoid activation, even though the command
2458 ;; loop would deactivate the mark because we inserted text.
2459 (goto-char (prog1 (mark t)
2460 (set-marker (mark-marker) (point) (current-buffer))))))
2461 nil)
2463 (defun yank (&optional arg)
2464 "Reinsert the last stretch of killed text.
2465 More precisely, reinsert the stretch of killed text most recently
2466 killed OR yanked. Put point at end, and set mark at beginning.
2467 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2468 With argument N, reinsert the Nth most recently killed stretch of killed
2469 text.
2470 See also the command \\[yank-pop]."
2471 (interactive "*P")
2472 (setq yank-window-start (window-start))
2473 ;; If we don't get all the way thru, make last-command indicate that
2474 ;; for the following command.
2475 (setq this-command t)
2476 (push-mark (point))
2477 (insert-for-yank (current-kill (cond
2478 ((listp arg) 0)
2479 ((eq arg '-) -2)
2480 (t (1- arg)))))
2481 (if (consp arg)
2482 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2483 ;; It is cleaner to avoid activation, even though the command
2484 ;; loop would deactivate the mark because we inserted text.
2485 (goto-char (prog1 (mark t)
2486 (set-marker (mark-marker) (point) (current-buffer)))))
2487 ;; If we do get all the way thru, make this-command indicate that.
2488 (if (eq this-command t)
2489 (setq this-command 'yank))
2490 nil)
2492 (defun rotate-yank-pointer (arg)
2493 "Rotate the yanking point in the kill ring.
2494 With argument, rotate that many kills forward (or backward, if negative)."
2495 (interactive "p")
2496 (current-kill arg))
2498 ;; Some kill commands.
2500 ;; Internal subroutine of delete-char
2501 (defun kill-forward-chars (arg)
2502 (if (listp arg) (setq arg (car arg)))
2503 (if (eq arg '-) (setq arg -1))
2504 (kill-region (point) (forward-point arg)))
2506 ;; Internal subroutine of backward-delete-char
2507 (defun kill-backward-chars (arg)
2508 (if (listp arg) (setq arg (car arg)))
2509 (if (eq arg '-) (setq arg -1))
2510 (kill-region (point) (forward-point (- arg))))
2512 (defcustom backward-delete-char-untabify-method 'untabify
2513 "*The method for untabifying when deleting backward.
2514 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2515 `hungry' -- delete all whitespace, both tabs and spaces;
2516 `all' -- delete all whitespace, including tabs, spaces and newlines;
2517 nil -- just delete one character."
2518 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2519 :version "20.3"
2520 :group 'killing)
2522 (defun backward-delete-char-untabify (arg &optional killp)
2523 "Delete characters backward, changing tabs into spaces.
2524 The exact behavior depends on `backward-delete-char-untabify-method'.
2525 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2526 Interactively, ARG is the prefix arg (default 1)
2527 and KILLP is t if a prefix arg was specified."
2528 (interactive "*p\nP")
2529 (when (eq backward-delete-char-untabify-method 'untabify)
2530 (let ((count arg))
2531 (save-excursion
2532 (while (and (> count 0) (not (bobp)))
2533 (if (= (preceding-char) ?\t)
2534 (let ((col (current-column)))
2535 (forward-char -1)
2536 (setq col (- col (current-column)))
2537 (insert-char ?\ col)
2538 (delete-char 1)))
2539 (forward-char -1)
2540 (setq count (1- count))))))
2541 (delete-backward-char
2542 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2543 ((eq backward-delete-char-untabify-method 'all)
2544 " \t\n\r"))))
2545 (if skip
2546 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2547 (point)))))
2548 (+ arg (if (zerop wh) 0 (1- wh))))
2549 arg))
2550 killp))
2552 (defun zap-to-char (arg char)
2553 "Kill up to and including ARG'th occurrence of CHAR.
2554 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2555 Goes backward if ARG is negative; error if CHAR not found."
2556 (interactive "p\ncZap to char: ")
2557 (kill-region (point) (progn
2558 (search-forward (char-to-string char) nil nil arg)
2559 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2560 (point))))
2562 ;; kill-line and its subroutines.
2564 (defcustom kill-whole-line nil
2565 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2566 :type 'boolean
2567 :group 'killing)
2569 (defun kill-line (&optional arg)
2570 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2571 With prefix argument, kill that many lines from point.
2572 Negative arguments kill lines backward.
2573 With zero argument, kills the text before point on the current line.
2575 When calling from a program, nil means \"no arg\",
2576 a number counts as a prefix arg.
2578 To kill a whole line, when point is not at the beginning, type \
2579 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2581 If `kill-whole-line' is non-nil, then this command kills the whole line
2582 including its terminating newline, when used at the beginning of a line
2583 with no argument. As a consequence, you can always kill a whole line
2584 by typing \\[beginning-of-line] \\[kill-line].
2586 If you want to append the killed line to the last killed text,
2587 use \\[append-next-kill] before \\[kill-line].
2589 If the buffer is read-only, Emacs will beep and refrain from deleting
2590 the line, but put the line in the kill ring anyway. This means that
2591 you can use this command to copy text from a read-only buffer.
2592 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2593 even beep.)"
2594 (interactive "P")
2595 (kill-region (point)
2596 ;; It is better to move point to the other end of the kill
2597 ;; before killing. That way, in a read-only buffer, point
2598 ;; moves across the text that is copied to the kill ring.
2599 ;; The choice has no effect on undo now that undo records
2600 ;; the value of point from before the command was run.
2601 (progn
2602 (if arg
2603 (forward-visible-line (prefix-numeric-value arg))
2604 (if (eobp)
2605 (signal 'end-of-buffer nil))
2606 (let ((end
2607 (save-excursion
2608 (end-of-visible-line) (point))))
2609 (if (or (save-excursion
2610 ;; If trailing whitespace is visible,
2611 ;; don't treat it as nothing.
2612 (unless show-trailing-whitespace
2613 (skip-chars-forward " \t" end))
2614 (= (point) end))
2615 (and kill-whole-line (bolp)))
2616 (forward-visible-line 1)
2617 (goto-char end))))
2618 (point))))
2620 (defun kill-whole-line (&optional arg)
2621 "Kill current line.
2622 With prefix arg, kill that many lines starting from the current line.
2623 If arg is negative, kill backward. Also kill the preceding newline.
2624 \(This is meant to make C-x z work well with negative arguments.\)
2625 If arg is zero, kill current line but exclude the trailing newline."
2626 (interactive "p")
2627 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2628 (signal 'end-of-buffer nil))
2629 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2630 (signal 'beginning-of-buffer nil))
2631 (unless (eq last-command 'kill-region)
2632 (kill-new "")
2633 (setq last-command 'kill-region))
2634 (cond ((zerop arg)
2635 ;; We need to kill in two steps, because the previous command
2636 ;; could have been a kill command, in which case the text
2637 ;; before point needs to be prepended to the current kill
2638 ;; ring entry and the text after point appended. Also, we
2639 ;; need to use save-excursion to avoid copying the same text
2640 ;; twice to the kill ring in read-only buffers.
2641 (save-excursion
2642 (kill-region (point) (progn (forward-visible-line 0) (point))))
2643 (kill-region (point) (progn (end-of-visible-line) (point))))
2644 ((< arg 0)
2645 (save-excursion
2646 (kill-region (point) (progn (end-of-visible-line) (point))))
2647 (kill-region (point)
2648 (progn (forward-visible-line (1+ arg))
2649 (unless (bobp) (backward-char))
2650 (point))))
2652 (save-excursion
2653 (kill-region (point) (progn (forward-visible-line 0) (point))))
2654 (kill-region (point)
2655 (progn (forward-visible-line arg) (point))))))
2657 (defun forward-visible-line (arg)
2658 "Move forward by ARG lines, ignoring currently invisible newlines only.
2659 If ARG is negative, move backward -ARG lines.
2660 If ARG is zero, move to the beginning of the current line."
2661 (condition-case nil
2662 (if (> arg 0)
2663 (progn
2664 (while (> arg 0)
2665 (or (zerop (forward-line 1))
2666 (signal 'end-of-buffer nil))
2667 ;; If the newline we just skipped is invisible,
2668 ;; don't count it.
2669 (let ((prop
2670 (get-char-property (1- (point)) 'invisible)))
2671 (if (if (eq buffer-invisibility-spec t)
2672 prop
2673 (or (memq prop buffer-invisibility-spec)
2674 (assq prop buffer-invisibility-spec)))
2675 (setq arg (1+ arg))))
2676 (setq arg (1- arg)))
2677 ;; If invisible text follows, and it is a number of complete lines,
2678 ;; skip it.
2679 (let ((opoint (point)))
2680 (while (and (not (eobp))
2681 (let ((prop
2682 (get-char-property (point) 'invisible)))
2683 (if (eq buffer-invisibility-spec t)
2684 prop
2685 (or (memq prop buffer-invisibility-spec)
2686 (assq prop buffer-invisibility-spec)))))
2687 (goto-char
2688 (if (get-text-property (point) 'invisible)
2689 (or (next-single-property-change (point) 'invisible)
2690 (point-max))
2691 (next-overlay-change (point)))))
2692 (unless (bolp)
2693 (goto-char opoint))))
2694 (let ((first t))
2695 (while (or first (<= arg 0))
2696 (if first
2697 (beginning-of-line)
2698 (or (zerop (forward-line -1))
2699 (signal 'beginning-of-buffer nil)))
2700 ;; If the newline we just moved to is invisible,
2701 ;; don't count it.
2702 (unless (bobp)
2703 (let ((prop
2704 (get-char-property (1- (point)) 'invisible)))
2705 (unless (if (eq buffer-invisibility-spec t)
2706 prop
2707 (or (memq prop buffer-invisibility-spec)
2708 (assq prop buffer-invisibility-spec)))
2709 (setq arg (1+ arg)))))
2710 (setq first nil))
2711 ;; If invisible text follows, and it is a number of complete lines,
2712 ;; skip it.
2713 (let ((opoint (point)))
2714 (while (and (not (bobp))
2715 (let ((prop
2716 (get-char-property (1- (point)) 'invisible)))
2717 (if (eq buffer-invisibility-spec t)
2718 prop
2719 (or (memq prop buffer-invisibility-spec)
2720 (assq prop buffer-invisibility-spec)))))
2721 (goto-char
2722 (if (get-text-property (1- (point)) 'invisible)
2723 (or (previous-single-property-change (point) 'invisible)
2724 (point-min))
2725 (previous-overlay-change (point)))))
2726 (unless (bolp)
2727 (goto-char opoint)))))
2728 ((beginning-of-buffer end-of-buffer)
2729 nil)))
2731 (defun end-of-visible-line ()
2732 "Move to end of current visible line."
2733 (end-of-line)
2734 ;; If the following character is currently invisible,
2735 ;; skip all characters with that same `invisible' property value,
2736 ;; then find the next newline.
2737 (while (and (not (eobp))
2738 (save-excursion
2739 (skip-chars-forward "^\n")
2740 (let ((prop
2741 (get-char-property (point) 'invisible)))
2742 (if (eq buffer-invisibility-spec t)
2743 prop
2744 (or (memq prop buffer-invisibility-spec)
2745 (assq prop buffer-invisibility-spec))))))
2746 (skip-chars-forward "^\n")
2747 (if (get-text-property (point) 'invisible)
2748 (goto-char (next-single-property-change (point) 'invisible))
2749 (goto-char (next-overlay-change (point))))
2750 (end-of-line)))
2752 (defun insert-buffer (buffer)
2753 "Insert after point the contents of BUFFER.
2754 Puts mark after the inserted text.
2755 BUFFER may be a buffer or a buffer name.
2757 This function is meant for the user to run interactively.
2758 Don't call it from programs: use `insert-buffer-substring' instead!"
2759 (interactive
2760 (list
2761 (progn
2762 (barf-if-buffer-read-only)
2763 (read-buffer "Insert buffer: "
2764 (if (eq (selected-window) (next-window (selected-window)))
2765 (other-buffer (current-buffer))
2766 (window-buffer (next-window (selected-window))))
2767 t))))
2768 (push-mark
2769 (save-excursion
2770 (insert-buffer-substring (get-buffer buffer))
2771 (point)))
2772 nil)
2774 (defun append-to-buffer (buffer start end)
2775 "Append to specified buffer the text of the region.
2776 It is inserted into that buffer before its point.
2778 When calling from a program, give three arguments:
2779 BUFFER (or buffer name), START and END.
2780 START and END specify the portion of the current buffer to be copied."
2781 (interactive
2782 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2783 (region-beginning) (region-end)))
2784 (let ((oldbuf (current-buffer)))
2785 (save-excursion
2786 (let* ((append-to (get-buffer-create buffer))
2787 (windows (get-buffer-window-list append-to t t))
2788 point)
2789 (set-buffer append-to)
2790 (setq point (point))
2791 (barf-if-buffer-read-only)
2792 (insert-buffer-substring oldbuf start end)
2793 (dolist (window windows)
2794 (when (= (window-point window) point)
2795 (set-window-point window (point))))))))
2797 (defun prepend-to-buffer (buffer start end)
2798 "Prepend to specified buffer the text of the region.
2799 It is inserted into that buffer after its point.
2801 When calling from a program, give three arguments:
2802 BUFFER (or buffer name), START and END.
2803 START and END specify the portion of the current buffer to be copied."
2804 (interactive "BPrepend to buffer: \nr")
2805 (let ((oldbuf (current-buffer)))
2806 (save-excursion
2807 (set-buffer (get-buffer-create buffer))
2808 (barf-if-buffer-read-only)
2809 (save-excursion
2810 (insert-buffer-substring oldbuf start end)))))
2812 (defun copy-to-buffer (buffer start end)
2813 "Copy to specified buffer the text of the region.
2814 It is inserted into that buffer, replacing existing text there.
2816 When calling from a program, give three arguments:
2817 BUFFER (or buffer name), START and END.
2818 START and END specify the portion of the current buffer to be copied."
2819 (interactive "BCopy to buffer: \nr")
2820 (let ((oldbuf (current-buffer)))
2821 (save-excursion
2822 (set-buffer (get-buffer-create buffer))
2823 (barf-if-buffer-read-only)
2824 (erase-buffer)
2825 (save-excursion
2826 (insert-buffer-substring oldbuf start end)))))
2828 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2829 (put 'mark-inactive 'error-message "The mark is not active now")
2831 (defun mark (&optional force)
2832 "Return this buffer's mark value as integer; error if mark inactive.
2833 If optional argument FORCE is non-nil, access the mark value
2834 even if the mark is not currently active, and return nil
2835 if there is no mark at all.
2837 If you are using this in an editing command, you are most likely making
2838 a mistake; see the documentation of `set-mark'."
2839 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2840 (marker-position (mark-marker))
2841 (signal 'mark-inactive nil)))
2843 ;; Many places set mark-active directly, and several of them failed to also
2844 ;; run deactivate-mark-hook. This shorthand should simplify.
2845 (defsubst deactivate-mark ()
2846 "Deactivate the mark by setting `mark-active' to nil.
2847 \(That makes a difference only in Transient Mark mode.)
2848 Also runs the hook `deactivate-mark-hook'."
2849 (cond
2850 ((eq transient-mark-mode 'lambda)
2851 (setq transient-mark-mode nil))
2852 (transient-mark-mode
2853 (setq mark-active nil)
2854 (run-hooks 'deactivate-mark-hook))))
2856 (defun set-mark (pos)
2857 "Set this buffer's mark to POS. Don't use this function!
2858 That is to say, don't use this function unless you want
2859 the user to see that the mark has moved, and you want the previous
2860 mark position to be lost.
2862 Normally, when a new mark is set, the old one should go on the stack.
2863 This is why most applications should use push-mark, not set-mark.
2865 Novice Emacs Lisp programmers often try to use the mark for the wrong
2866 purposes. The mark saves a location for the user's convenience.
2867 Most editing commands should not alter the mark.
2868 To remember a location for internal use in the Lisp program,
2869 store it in a Lisp variable. Example:
2871 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2873 (if pos
2874 (progn
2875 (setq mark-active t)
2876 (run-hooks 'activate-mark-hook)
2877 (set-marker (mark-marker) pos (current-buffer)))
2878 ;; Normally we never clear mark-active except in Transient Mark mode.
2879 ;; But when we actually clear out the mark value too,
2880 ;; we must clear mark-active in any mode.
2881 (setq mark-active nil)
2882 (run-hooks 'deactivate-mark-hook)
2883 (set-marker (mark-marker) nil)))
2885 (defvar mark-ring nil
2886 "The list of former marks of the current buffer, most recent first.")
2887 (make-variable-buffer-local 'mark-ring)
2888 (put 'mark-ring 'permanent-local t)
2890 (defcustom mark-ring-max 16
2891 "*Maximum size of mark ring. Start discarding off end if gets this big."
2892 :type 'integer
2893 :group 'editing-basics)
2895 (defvar global-mark-ring nil
2896 "The list of saved global marks, most recent first.")
2898 (defcustom global-mark-ring-max 16
2899 "*Maximum size of global mark ring. \
2900 Start discarding off end if gets this big."
2901 :type 'integer
2902 :group 'editing-basics)
2904 (defun pop-to-mark-command ()
2905 "Jump to mark, and pop a new position for mark off the ring
2906 \(does not affect global mark ring\)."
2907 (interactive)
2908 (if (null (mark t))
2909 (error "No mark set in this buffer")
2910 (goto-char (mark t))
2911 (pop-mark)))
2913 (defun push-mark-command (arg &optional nomsg)
2914 "Set mark at where point is.
2915 If no prefix arg and mark is already set there, just activate it.
2916 Display `Mark set' unless the optional second arg NOMSG is non-nil."
2917 (interactive "P")
2918 (let ((mark (marker-position (mark-marker))))
2919 (if (or arg (null mark) (/= mark (point)))
2920 (push-mark nil nomsg t)
2921 (setq mark-active t)
2922 (unless nomsg
2923 (message "Mark activated")))))
2925 (defun set-mark-command (arg)
2926 "Set mark at where point is, or jump to mark.
2927 With no prefix argument, set mark, and push old mark position on local
2928 mark ring; also push mark on global mark ring if last mark was set in
2929 another buffer. Immediately repeating the command activates
2930 `transient-mark-mode' temporarily.
2932 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
2933 jump to mark, and pop a new position
2934 for mark off the local mark ring \(this does not affect the global
2935 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
2936 mark ring \(see `pop-global-mark'\).
2938 Repeating the \\[set-mark-command] command without the prefix jumps to
2939 the next position off the local (or global) mark ring.
2941 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
2942 \\[universal-argument] \\[set-mark-command], unconditionally
2943 set mark where point is.
2945 Novice Emacs Lisp programmers often try to use the mark for the wrong
2946 purposes. See the documentation of `set-mark' for more information."
2947 (interactive "P")
2948 (if (eq transient-mark-mode 'lambda)
2949 (setq transient-mark-mode nil))
2950 (cond
2951 ((and (consp arg) (> (prefix-numeric-value arg) 4))
2952 (push-mark-command nil))
2953 ((not (eq this-command 'set-mark-command))
2954 (if arg
2955 (pop-to-mark-command)
2956 (push-mark-command t)))
2957 ((eq last-command 'pop-to-mark-command)
2958 (setq this-command 'pop-to-mark-command)
2959 (pop-to-mark-command))
2960 ((and (eq last-command 'pop-global-mark) (not arg))
2961 (setq this-command 'pop-global-mark)
2962 (pop-global-mark))
2963 (arg
2964 (setq this-command 'pop-to-mark-command)
2965 (pop-to-mark-command))
2966 ((and (eq last-command 'set-mark-command)
2967 mark-active (null transient-mark-mode))
2968 (setq transient-mark-mode 'lambda)
2969 (message "Transient-mark-mode temporarily enabled"))
2971 (push-mark-command nil))))
2973 (defun push-mark (&optional location nomsg activate)
2974 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2975 If the last global mark pushed was not in the current buffer,
2976 also push LOCATION on the global mark ring.
2977 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2978 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2980 Novice Emacs Lisp programmers often try to use the mark for the wrong
2981 purposes. See the documentation of `set-mark' for more information.
2983 In Transient Mark mode, this does not activate the mark."
2984 (unless (null (mark t))
2985 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2986 (when (> (length mark-ring) mark-ring-max)
2987 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2988 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
2989 (set-marker (mark-marker) (or location (point)) (current-buffer))
2990 ;; Now push the mark on the global mark ring.
2991 (if (and global-mark-ring
2992 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2993 ;; The last global mark pushed was in this same buffer.
2994 ;; Don't push another one.
2996 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2997 (when (> (length global-mark-ring) global-mark-ring-max)
2998 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
2999 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3000 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3001 (message "Mark set"))
3002 (if (or activate (not transient-mark-mode))
3003 (set-mark (mark t)))
3004 nil)
3006 (defun pop-mark ()
3007 "Pop off mark ring into the buffer's actual mark.
3008 Does not set point. Does nothing if mark ring is empty."
3009 (when mark-ring
3010 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3011 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3012 (move-marker (car mark-ring) nil)
3013 (if (null (mark t)) (ding))
3014 (setq mark-ring (cdr mark-ring)))
3015 (deactivate-mark))
3017 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3018 (defun exchange-point-and-mark (&optional arg)
3019 "Put the mark where point is now, and point where the mark is now.
3020 This command works even when the mark is not active,
3021 and it reactivates the mark.
3022 With prefix arg, `transient-mark-mode' is enabled temporarily."
3023 (interactive "P")
3024 (if arg
3025 (if mark-active
3026 (if (null transient-mark-mode)
3027 (setq transient-mark-mode 'lambda))
3028 (setq arg nil)))
3029 (unless arg
3030 (let ((omark (mark t)))
3031 (if (null omark)
3032 (error "No mark set in this buffer"))
3033 (set-mark (point))
3034 (goto-char omark)
3035 nil)))
3037 (define-minor-mode transient-mark-mode
3038 "Toggle Transient Mark mode.
3039 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3041 In Transient Mark mode, when the mark is active, the region is highlighted.
3042 Changing the buffer \"deactivates\" the mark.
3043 So do certain other operations that set the mark
3044 but whose main purpose is something else--for example,
3045 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3047 You can also deactivate the mark by typing \\[keyboard-quit] or
3048 \\[keyboard-escape-quit].
3050 Many commands change their behavior when Transient Mark mode is in effect
3051 and the mark is active, by acting on the region instead of their usual
3052 default part of the buffer's text. Examples of such commands include
3053 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3054 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3055 Invoke \\[apropos-documentation] and type \"transient\" or
3056 \"mark.*active\" at the prompt, to see the documentation of
3057 commands which are sensitive to the Transient Mark mode."
3058 :global t :group 'editing-basics :require nil)
3060 (defun pop-global-mark ()
3061 "Pop off global mark ring and jump to the top location."
3062 (interactive)
3063 ;; Pop entries which refer to non-existent buffers.
3064 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3065 (setq global-mark-ring (cdr global-mark-ring)))
3066 (or global-mark-ring
3067 (error "No global mark set"))
3068 (let* ((marker (car global-mark-ring))
3069 (buffer (marker-buffer marker))
3070 (position (marker-position marker)))
3071 (setq global-mark-ring (nconc (cdr global-mark-ring)
3072 (list (car global-mark-ring))))
3073 (set-buffer buffer)
3074 (or (and (>= position (point-min))
3075 (<= position (point-max)))
3076 (widen))
3077 (goto-char position)
3078 (switch-to-buffer buffer)))
3080 (defcustom next-line-add-newlines nil
3081 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3082 :type 'boolean
3083 :version "21.1"
3084 :group 'editing-basics)
3086 (defun next-line (&optional arg)
3087 "Move cursor vertically down ARG lines.
3088 If there is no character in the target line exactly under the current column,
3089 the cursor is positioned after the character in that line which spans this
3090 column, or at the end of the line if it is not long enough.
3091 If there is no line in the buffer after this one, behavior depends on the
3092 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3093 to create a line, and moves the cursor to that line. Otherwise it moves the
3094 cursor to the end of the buffer.
3096 The command \\[set-goal-column] can be used to create
3097 a semipermanent goal column for this command.
3098 Then instead of trying to move exactly vertically (or as close as possible),
3099 this command moves to the specified goal column (or as close as possible).
3100 The goal column is stored in the variable `goal-column', which is nil
3101 when there is no goal column.
3103 If you are thinking of using this in a Lisp program, consider
3104 using `forward-line' instead. It is usually easier to use
3105 and more reliable (no dependence on goal column, etc.)."
3106 (interactive "p")
3107 (or arg (setq arg 1))
3108 (if (and next-line-add-newlines (= arg 1))
3109 (if (save-excursion (end-of-line) (eobp))
3110 ;; When adding a newline, don't expand an abbrev.
3111 (let ((abbrev-mode nil))
3112 (end-of-line)
3113 (insert "\n"))
3114 (line-move arg))
3115 (if (interactive-p)
3116 (condition-case nil
3117 (line-move arg)
3118 ((beginning-of-buffer end-of-buffer) (ding)))
3119 (line-move arg)))
3120 nil)
3122 (defun previous-line (&optional arg)
3123 "Move cursor vertically up ARG lines.
3124 If there is no character in the target line exactly over the current column,
3125 the cursor is positioned after the character in that line which spans this
3126 column, or at the end of the line if it is not long enough.
3128 The command \\[set-goal-column] can be used to create
3129 a semipermanent goal column for this command.
3130 Then instead of trying to move exactly vertically (or as close as possible),
3131 this command moves to the specified goal column (or as close as possible).
3132 The goal column is stored in the variable `goal-column', which is nil
3133 when there is no goal column.
3135 If you are thinking of using this in a Lisp program, consider using
3136 `forward-line' with a negative argument instead. It is usually easier
3137 to use and more reliable (no dependence on goal column, etc.)."
3138 (interactive "p")
3139 (or arg (setq arg 1))
3140 (if (interactive-p)
3141 (condition-case nil
3142 (line-move (- arg))
3143 ((beginning-of-buffer end-of-buffer) (ding)))
3144 (line-move (- arg)))
3145 nil)
3147 (defcustom track-eol nil
3148 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3149 This means moving to the end of each line moved onto.
3150 The beginning of a blank line does not count as the end of a line."
3151 :type 'boolean
3152 :group 'editing-basics)
3154 (defcustom goal-column nil
3155 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3156 :type '(choice integer
3157 (const :tag "None" nil))
3158 :group 'editing-basics)
3159 (make-variable-buffer-local 'goal-column)
3161 (defvar temporary-goal-column 0
3162 "Current goal column for vertical motion.
3163 It is the column where point was
3164 at the start of current run of vertical motion commands.
3165 When the `track-eol' feature is doing its job, the value is 9999.")
3167 (defcustom line-move-ignore-invisible t
3168 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3169 Outline mode sets this."
3170 :type 'boolean
3171 :group 'editing-basics)
3173 (defun line-move-invisible-p (pos)
3174 "Return non-nil if the character after POS is currently invisible."
3175 (let ((prop
3176 (get-char-property pos 'invisible)))
3177 (if (eq buffer-invisibility-spec t)
3178 prop
3179 (or (memq prop buffer-invisibility-spec)
3180 (assq prop buffer-invisibility-spec)))))
3182 ;; Perform vertical scrolling of tall images if necessary.
3183 (defun line-move (arg &optional noerror to-end)
3184 (if auto-window-vscroll
3185 (let ((forward (> arg 0))
3186 (part (nth 2 (pos-visible-in-window-p (point) nil t))))
3187 (if (and (consp part)
3188 (> (setq part (if forward (cdr part) (car part))) 0))
3189 (set-window-vscroll nil
3190 (if forward
3191 (+ (window-vscroll nil t)
3192 (min part
3193 (* (frame-char-height) arg)))
3194 (max 0
3195 (- (window-vscroll nil t)
3196 (min part
3197 (* (frame-char-height) (- arg))))))
3199 (set-window-vscroll nil 0)
3200 (line-move-1 arg noerror to-end)))
3201 (line-move-1 arg noerror to-end)))
3203 ;; This is the guts of next-line and previous-line.
3204 ;; Arg says how many lines to move.
3205 ;; The value is t if we can move the specified number of lines.
3206 (defun line-move-1 (arg &optional noerror to-end)
3207 ;; Don't run any point-motion hooks, and disregard intangibility,
3208 ;; for intermediate positions.
3209 (let ((inhibit-point-motion-hooks t)
3210 (opoint (point))
3211 (forward (> arg 0)))
3212 (unwind-protect
3213 (progn
3214 (if (not (memq last-command '(next-line previous-line)))
3215 (setq temporary-goal-column
3216 (if (and track-eol (eolp)
3217 ;; Don't count beg of empty line as end of line
3218 ;; unless we just did explicit end-of-line.
3219 (or (not (bolp)) (eq last-command 'end-of-line)))
3220 9999
3221 (current-column))))
3223 (if (and (not (integerp selective-display))
3224 (not line-move-ignore-invisible))
3225 ;; Use just newline characters.
3226 ;; Set ARG to 0 if we move as many lines as requested.
3227 (or (if (> arg 0)
3228 (progn (if (> arg 1) (forward-line (1- arg)))
3229 ;; This way of moving forward ARG lines
3230 ;; verifies that we have a newline after the last one.
3231 ;; It doesn't get confused by intangible text.
3232 (end-of-line)
3233 (if (zerop (forward-line 1))
3234 (setq arg 0)))
3235 (and (zerop (forward-line arg))
3236 (bolp)
3237 (setq arg 0)))
3238 (unless noerror
3239 (signal (if (< arg 0)
3240 'beginning-of-buffer
3241 'end-of-buffer)
3242 nil)))
3243 ;; Move by arg lines, but ignore invisible ones.
3244 (let (done)
3245 (while (and (> arg 0) (not done))
3246 ;; If the following character is currently invisible,
3247 ;; skip all characters with that same `invisible' property value.
3248 (while (and (not (eobp)) (line-move-invisible-p (point)))
3249 (goto-char (next-char-property-change (point))))
3250 ;; Now move a line.
3251 (end-of-line)
3252 (and (zerop (vertical-motion 1))
3253 (if (not noerror)
3254 (signal 'end-of-buffer nil)
3255 (setq done t)))
3256 (unless done
3257 (setq arg (1- arg))))
3258 (while (and (< arg 0) (not done))
3259 (beginning-of-line)
3261 (if (zerop (vertical-motion -1))
3262 (if (not noerror)
3263 (signal 'beginning-of-buffer nil)
3264 (setq done t)))
3265 (unless done
3266 (setq arg (1+ arg))
3267 (while (and ;; Don't move over previous invis lines
3268 ;; if our target is the middle of this line.
3269 (or (zerop (or goal-column temporary-goal-column))
3270 (< arg 0))
3271 (not (bobp)) (line-move-invisible-p (1- (point))))
3272 (goto-char (previous-char-property-change (point))))))))
3273 ;; This is the value the function returns.
3274 (= arg 0))
3276 (cond ((> arg 0)
3277 ;; If we did not move down as far as desired,
3278 ;; at least go to end of line.
3279 (end-of-line))
3280 ((< arg 0)
3281 ;; If we did not move down as far as desired,
3282 ;; at least go to end of line.
3283 (beginning-of-line))
3285 (line-move-finish (or goal-column temporary-goal-column)
3286 opoint forward))))))
3288 (defun line-move-finish (column opoint forward)
3289 (let ((repeat t))
3290 (while repeat
3291 ;; Set REPEAT to t to repeat the whole thing.
3292 (setq repeat nil)
3294 (let (new
3295 (line-beg (save-excursion (beginning-of-line) (point)))
3296 (line-end
3297 ;; Compute the end of the line
3298 ;; ignoring effectively invisible newlines.
3299 (save-excursion
3300 (end-of-line)
3301 (while (and (not (eobp)) (line-move-invisible-p (point)))
3302 (goto-char (next-char-property-change (point)))
3303 (end-of-line))
3304 (point))))
3306 ;; Move to the desired column.
3307 (line-move-to-column column)
3308 (setq new (point))
3310 ;; Process intangibility within a line.
3311 ;; Move to the chosen destination position from above,
3312 ;; with intangibility processing enabled.
3314 (goto-char (point-min))
3315 (let ((inhibit-point-motion-hooks nil))
3316 (goto-char new)
3318 ;; If intangibility moves us to a different (later) place
3319 ;; in the same line, use that as the destination.
3320 (if (<= (point) line-end)
3321 (setq new (point))
3322 ;; If that position is "too late",
3323 ;; try the previous allowable position.
3324 ;; See if it is ok.
3325 (backward-char)
3326 (if (if forward
3327 ;; If going forward, don't accept the previous
3328 ;; allowable position if it is before the target line.
3329 (< line-beg (point))
3330 ;; If going backward, don't accept the previous
3331 ;; allowable position if it is still after the target line.
3332 (<= (point) line-end))
3333 (setq new (point))
3334 ;; As a last resort, use the end of the line.
3335 (setq new line-end))))
3337 ;; Now move to the updated destination, processing fields
3338 ;; as well as intangibility.
3339 (goto-char opoint)
3340 (let ((inhibit-point-motion-hooks nil))
3341 (goto-char
3342 (constrain-to-field new opoint nil t
3343 'inhibit-line-move-field-capture)))
3345 ;; If all this moved us to a different line,
3346 ;; retry everything within that new line.
3347 (when (or (< (point) line-beg) (> (point) line-end))
3348 ;; Repeat the intangibility and field processing.
3349 (setq repeat t))))))
3351 (defun line-move-to-column (col)
3352 "Try to find column COL, considering invisibility.
3353 This function works only in certain cases,
3354 because what we really need is for `move-to-column'
3355 and `current-column' to be able to ignore invisible text."
3356 (if (zerop col)
3357 (beginning-of-line)
3358 (move-to-column col))
3360 (when (and line-move-ignore-invisible
3361 (not (bolp)) (line-move-invisible-p (1- (point))))
3362 (let ((normal-location (point))
3363 (normal-column (current-column)))
3364 ;; If the following character is currently invisible,
3365 ;; skip all characters with that same `invisible' property value.
3366 (while (and (not (eobp))
3367 (line-move-invisible-p (point)))
3368 (goto-char (next-char-property-change (point))))
3369 ;; Have we advanced to a larger column position?
3370 (if (> (current-column) normal-column)
3371 ;; We have made some progress towards the desired column.
3372 ;; See if we can make any further progress.
3373 (line-move-to-column (+ (current-column) (- col normal-column)))
3374 ;; Otherwise, go to the place we originally found
3375 ;; and move back over invisible text.
3376 ;; that will get us to the same place on the screen
3377 ;; but with a more reasonable buffer position.
3378 (goto-char normal-location)
3379 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3380 (while (and (not (bolp)) (line-move-invisible-p (1- (point))))
3381 (goto-char (previous-char-property-change (point) line-beg))))))))
3383 (defun move-end-of-line (arg)
3384 "Move point to end of current line.
3385 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3386 If point reaches the beginning or end of buffer, it stops there.
3387 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3389 This command does not move point across a field boundary unless doing so
3390 would move beyond there to a different line; if ARG is nil or 1, and
3391 point starts at a field boundary, point does not move. To ignore field
3392 boundaries bind `inhibit-field-text-motion' to t."
3393 (interactive "p")
3394 (or arg (setq arg 1))
3395 (let (done)
3396 (while (not done)
3397 (let ((newpos
3398 (save-excursion
3399 (let ((goal-column 0))
3400 (and (line-move arg t)
3401 (not (bobp))
3402 (progn
3403 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
3404 (goto-char (previous-char-property-change (point))))
3405 (backward-char 1)))
3406 (point)))))
3407 (goto-char newpos)
3408 (if (and (> (point) newpos)
3409 (eq (preceding-char) ?\n))
3410 (backward-char 1)
3411 (if (and (> (point) newpos) (not (eobp))
3412 (not (eq (following-char) ?\n)))
3413 ;; If we skipped something intangible
3414 ;; and now we're not really at eol,
3415 ;; keep going.
3416 (setq arg 1)
3417 (setq done t)))))))
3419 ;;; Many people have said they rarely use this feature, and often type
3420 ;;; it by accident. Maybe it shouldn't even be on a key.
3421 (put 'set-goal-column 'disabled t)
3423 (defun set-goal-column (arg)
3424 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3425 Those commands will move to this position in the line moved to
3426 rather than trying to keep the same horizontal position.
3427 With a non-nil argument, clears out the goal column
3428 so that \\[next-line] and \\[previous-line] resume vertical motion.
3429 The goal column is stored in the variable `goal-column'."
3430 (interactive "P")
3431 (if arg
3432 (progn
3433 (setq goal-column nil)
3434 (message "No goal column"))
3435 (setq goal-column (current-column))
3436 (message (substitute-command-keys
3437 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3438 goal-column))
3439 nil)
3442 (defun scroll-other-window-down (lines)
3443 "Scroll the \"other window\" down.
3444 For more details, see the documentation for `scroll-other-window'."
3445 (interactive "P")
3446 (scroll-other-window
3447 ;; Just invert the argument's meaning.
3448 ;; We can do that without knowing which window it will be.
3449 (if (eq lines '-) nil
3450 (if (null lines) '-
3451 (- (prefix-numeric-value lines))))))
3452 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
3454 (defun beginning-of-buffer-other-window (arg)
3455 "Move point to the beginning of the buffer in the other window.
3456 Leave mark at previous position.
3457 With arg N, put point N/10 of the way from the true beginning."
3458 (interactive "P")
3459 (let ((orig-window (selected-window))
3460 (window (other-window-for-scrolling)))
3461 ;; We use unwind-protect rather than save-window-excursion
3462 ;; because the latter would preserve the things we want to change.
3463 (unwind-protect
3464 (progn
3465 (select-window window)
3466 ;; Set point and mark in that window's buffer.
3467 (with-no-warnings
3468 (beginning-of-buffer arg))
3469 ;; Set point accordingly.
3470 (recenter '(t)))
3471 (select-window orig-window))))
3473 (defun end-of-buffer-other-window (arg)
3474 "Move point to the end of the buffer in the other window.
3475 Leave mark at previous position.
3476 With arg N, put point N/10 of the way from the true end."
3477 (interactive "P")
3478 ;; See beginning-of-buffer-other-window for comments.
3479 (let ((orig-window (selected-window))
3480 (window (other-window-for-scrolling)))
3481 (unwind-protect
3482 (progn
3483 (select-window window)
3484 (with-no-warnings
3485 (end-of-buffer arg))
3486 (recenter '(t)))
3487 (select-window orig-window))))
3489 (defun transpose-chars (arg)
3490 "Interchange characters around point, moving forward one character.
3491 With prefix arg ARG, effect is to take character before point
3492 and drag it forward past ARG other characters (backward if ARG negative).
3493 If no argument and at end of line, the previous two chars are exchanged."
3494 (interactive "*P")
3495 (and (null arg) (eolp) (forward-char -1))
3496 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3498 (defun transpose-words (arg)
3499 "Interchange words around point, leaving point at end of them.
3500 With prefix arg ARG, effect is to take word before or around point
3501 and drag it forward past ARG other words (backward if ARG negative).
3502 If ARG is zero, the words around or after point and around or after mark
3503 are interchanged."
3504 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3505 (interactive "*p")
3506 (transpose-subr 'forward-word arg))
3508 (defun transpose-sexps (arg)
3509 "Like \\[transpose-words] but applies to sexps.
3510 Does not work on a sexp that point is in the middle of
3511 if it is a list or string."
3512 (interactive "*p")
3513 (transpose-subr
3514 (lambda (arg)
3515 ;; Here we should try to simulate the behavior of
3516 ;; (cons (progn (forward-sexp x) (point))
3517 ;; (progn (forward-sexp (- x)) (point)))
3518 ;; Except that we don't want to rely on the second forward-sexp
3519 ;; putting us back to where we want to be, since forward-sexp-function
3520 ;; might do funny things like infix-precedence.
3521 (if (if (> arg 0)
3522 (looking-at "\\sw\\|\\s_")
3523 (and (not (bobp))
3524 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3525 ;; Jumping over a symbol. We might be inside it, mind you.
3526 (progn (funcall (if (> arg 0)
3527 'skip-syntax-backward 'skip-syntax-forward)
3528 "w_")
3529 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3530 ;; Otherwise, we're between sexps. Take a step back before jumping
3531 ;; to make sure we'll obey the same precedence no matter which direction
3532 ;; we're going.
3533 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3534 (cons (save-excursion (forward-sexp arg) (point))
3535 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3536 (not (zerop (funcall (if (> arg 0)
3537 'skip-syntax-forward
3538 'skip-syntax-backward)
3539 ".")))))
3540 (point)))))
3541 arg 'special))
3543 (defun transpose-lines (arg)
3544 "Exchange current line and previous line, leaving point after both.
3545 With argument ARG, takes previous line and moves it past ARG lines.
3546 With argument 0, interchanges line point is in with line mark is in."
3547 (interactive "*p")
3548 (transpose-subr (function
3549 (lambda (arg)
3550 (if (> arg 0)
3551 (progn
3552 ;; Move forward over ARG lines,
3553 ;; but create newlines if necessary.
3554 (setq arg (forward-line arg))
3555 (if (/= (preceding-char) ?\n)
3556 (setq arg (1+ arg)))
3557 (if (> arg 0)
3558 (newline arg)))
3559 (forward-line arg))))
3560 arg))
3562 (defun transpose-subr (mover arg &optional special)
3563 (let ((aux (if special mover
3564 (lambda (x)
3565 (cons (progn (funcall mover x) (point))
3566 (progn (funcall mover (- x)) (point))))))
3567 pos1 pos2)
3568 (cond
3569 ((= arg 0)
3570 (save-excursion
3571 (setq pos1 (funcall aux 1))
3572 (goto-char (mark))
3573 (setq pos2 (funcall aux 1))
3574 (transpose-subr-1 pos1 pos2))
3575 (exchange-point-and-mark))
3576 ((> arg 0)
3577 (setq pos1 (funcall aux -1))
3578 (setq pos2 (funcall aux arg))
3579 (transpose-subr-1 pos1 pos2)
3580 (goto-char (car pos2)))
3582 (setq pos1 (funcall aux -1))
3583 (goto-char (car pos1))
3584 (setq pos2 (funcall aux arg))
3585 (transpose-subr-1 pos1 pos2)))))
3587 (defun transpose-subr-1 (pos1 pos2)
3588 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3589 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3590 (when (> (car pos1) (car pos2))
3591 (let ((swap pos1))
3592 (setq pos1 pos2 pos2 swap)))
3593 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3594 (atomic-change-group
3595 (let (word2)
3596 ;; FIXME: We first delete the two pieces of text, so markers that
3597 ;; used to point to after the text end up pointing to before it :-(
3598 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3599 (goto-char (car pos2))
3600 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3601 (goto-char (car pos1))
3602 (insert word2))))
3604 (defun backward-word (&optional arg)
3605 "Move backward until encountering the beginning of a word.
3606 With argument, do this that many times."
3607 (interactive "p")
3608 (forward-word (- (or arg 1))))
3610 (defun mark-word (&optional arg allow-extend)
3611 "Set mark ARG words away from point.
3612 The place mark goes is the same place \\[forward-word] would
3613 move to with the same argument.
3614 Interactively, if this command is repeated
3615 or (in Transient Mark mode) if the mark is active,
3616 it marks the next ARG words after the ones already marked."
3617 (interactive "P\np")
3618 (cond ((and allow-extend
3619 (or (and (eq last-command this-command) (mark t))
3620 (and transient-mark-mode mark-active)))
3621 (setq arg (if arg (prefix-numeric-value arg)
3622 (if (< (mark) (point)) -1 1)))
3623 (set-mark
3624 (save-excursion
3625 (goto-char (mark))
3626 (forward-word arg)
3627 (point))))
3629 (push-mark
3630 (save-excursion
3631 (forward-word (prefix-numeric-value arg))
3632 (point))
3633 nil t))))
3635 (defun kill-word (arg)
3636 "Kill characters forward until encountering the end of a word.
3637 With argument, do this that many times."
3638 (interactive "p")
3639 (kill-region (point) (progn (forward-word arg) (point))))
3641 (defun backward-kill-word (arg)
3642 "Kill characters backward until encountering the end of a word.
3643 With argument, do this that many times."
3644 (interactive "p")
3645 (kill-word (- arg)))
3647 (defun current-word (&optional strict really-word)
3648 "Return the symbol or word that point is on (or a nearby one) as a string.
3649 The return value includes no text properties.
3650 If optional arg STRICT is non-nil, return nil unless point is within
3651 or adjacent to a symbol or word. In all cases the value can be nil
3652 if there is no word nearby.
3653 The function, belying its name, normally finds a symbol.
3654 If optional arg REALLY-WORD is non-nil, it finds just a word."
3655 (save-excursion
3656 (let* ((oldpoint (point)) (start (point)) (end (point))
3657 (syntaxes (if really-word "w" "w_"))
3658 (not-syntaxes (concat "^" syntaxes)))
3659 (skip-syntax-backward syntaxes) (setq start (point))
3660 (goto-char oldpoint)
3661 (skip-syntax-forward syntaxes) (setq end (point))
3662 (when (and (eq start oldpoint) (eq end oldpoint)
3663 ;; Point is neither within nor adjacent to a word.
3664 (not strict))
3665 ;; Look for preceding word in same line.
3666 (skip-syntax-backward not-syntaxes
3667 (save-excursion (beginning-of-line)
3668 (point)))
3669 (if (bolp)
3670 ;; No preceding word in same line.
3671 ;; Look for following word in same line.
3672 (progn
3673 (skip-syntax-forward not-syntaxes
3674 (save-excursion (end-of-line)
3675 (point)))
3676 (setq start (point))
3677 (skip-syntax-forward syntaxes)
3678 (setq end (point)))
3679 (setq end (point))
3680 (skip-syntax-backward syntaxes)
3681 (setq start (point))))
3682 ;; If we found something nonempty, return it as a string.
3683 (unless (= start end)
3684 (buffer-substring-no-properties start end)))))
3686 (defcustom fill-prefix nil
3687 "*String for filling to insert at front of new line, or nil for none."
3688 :type '(choice (const :tag "None" nil)
3689 string)
3690 :group 'fill)
3691 (make-variable-buffer-local 'fill-prefix)
3693 (defcustom auto-fill-inhibit-regexp nil
3694 "*Regexp to match lines which should not be auto-filled."
3695 :type '(choice (const :tag "None" nil)
3696 regexp)
3697 :group 'fill)
3699 (defvar comment-line-break-function 'comment-indent-new-line
3700 "*Mode-specific function which line breaks and continues a comment.
3702 This function is only called during auto-filling of a comment section.
3703 The function should take a single optional argument, which is a flag
3704 indicating whether it should use soft newlines.
3706 Setting this variable automatically makes it local to the current buffer.")
3708 ;; This function is used as the auto-fill-function of a buffer
3709 ;; when Auto-Fill mode is enabled.
3710 ;; It returns t if it really did any work.
3711 ;; (Actually some major modes use a different auto-fill function,
3712 ;; but this one is the default one.)
3713 (defun do-auto-fill ()
3714 (let (fc justify give-up
3715 (fill-prefix fill-prefix))
3716 (if (or (not (setq justify (current-justification)))
3717 (null (setq fc (current-fill-column)))
3718 (and (eq justify 'left)
3719 (<= (current-column) fc))
3720 (and auto-fill-inhibit-regexp
3721 (save-excursion (beginning-of-line)
3722 (looking-at auto-fill-inhibit-regexp))))
3723 nil ;; Auto-filling not required
3724 (if (memq justify '(full center right))
3725 (save-excursion (unjustify-current-line)))
3727 ;; Choose a fill-prefix automatically.
3728 (when (and adaptive-fill-mode
3729 (or (null fill-prefix) (string= fill-prefix "")))
3730 (let ((prefix
3731 (fill-context-prefix
3732 (save-excursion (backward-paragraph 1) (point))
3733 (save-excursion (forward-paragraph 1) (point)))))
3734 (and prefix (not (equal prefix ""))
3735 ;; Use auto-indentation rather than a guessed empty prefix.
3736 (not (and fill-indent-according-to-mode
3737 (string-match "\\`[ \t]*\\'" prefix)))
3738 (setq fill-prefix prefix))))
3740 (while (and (not give-up) (> (current-column) fc))
3741 ;; Determine where to split the line.
3742 (let* (after-prefix
3743 (fill-point
3744 (save-excursion
3745 (beginning-of-line)
3746 (setq after-prefix (point))
3747 (and fill-prefix
3748 (looking-at (regexp-quote fill-prefix))
3749 (setq after-prefix (match-end 0)))
3750 (move-to-column (1+ fc))
3751 (fill-move-to-break-point after-prefix)
3752 (point))))
3754 ;; See whether the place we found is any good.
3755 (if (save-excursion
3756 (goto-char fill-point)
3757 (or (bolp)
3758 ;; There is no use breaking at end of line.
3759 (save-excursion (skip-chars-forward " ") (eolp))
3760 ;; It is futile to split at the end of the prefix
3761 ;; since we would just insert the prefix again.
3762 (and after-prefix (<= (point) after-prefix))
3763 ;; Don't split right after a comment starter
3764 ;; since we would just make another comment starter.
3765 (and comment-start-skip
3766 (let ((limit (point)))
3767 (beginning-of-line)
3768 (and (re-search-forward comment-start-skip
3769 limit t)
3770 (eq (point) limit))))))
3771 ;; No good place to break => stop trying.
3772 (setq give-up t)
3773 ;; Ok, we have a useful place to break the line. Do it.
3774 (let ((prev-column (current-column)))
3775 ;; If point is at the fill-point, do not `save-excursion'.
3776 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3777 ;; point will end up before it rather than after it.
3778 (if (save-excursion
3779 (skip-chars-backward " \t")
3780 (= (point) fill-point))
3781 (funcall comment-line-break-function t)
3782 (save-excursion
3783 (goto-char fill-point)
3784 (funcall comment-line-break-function t)))
3785 ;; Now do justification, if required
3786 (if (not (eq justify 'left))
3787 (save-excursion
3788 (end-of-line 0)
3789 (justify-current-line justify nil t)))
3790 ;; If making the new line didn't reduce the hpos of
3791 ;; the end of the line, then give up now;
3792 ;; trying again will not help.
3793 (if (>= (current-column) prev-column)
3794 (setq give-up t))))))
3795 ;; Justify last line.
3796 (justify-current-line justify t t)
3797 t)))
3799 (defvar normal-auto-fill-function 'do-auto-fill
3800 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3801 Some major modes set this.")
3803 ;; FIXME: turn into a proper minor mode.
3804 ;; Add a global minor mode version of it.
3805 (defun auto-fill-mode (&optional arg)
3806 "Toggle Auto Fill mode.
3807 With arg, turn Auto Fill mode on if and only if arg is positive.
3808 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3809 automatically breaks the line at a previous space.
3811 The value of `normal-auto-fill-function' specifies the function to use
3812 for `auto-fill-function' when turning Auto Fill mode on."
3813 (interactive "P")
3814 (prog1 (setq auto-fill-function
3815 (if (if (null arg)
3816 (not auto-fill-function)
3817 (> (prefix-numeric-value arg) 0))
3818 normal-auto-fill-function
3819 nil))
3820 (force-mode-line-update)))
3822 ;; This holds a document string used to document auto-fill-mode.
3823 (defun auto-fill-function ()
3824 "Automatically break line at a previous space, in insertion of text."
3825 nil)
3827 (defun turn-on-auto-fill ()
3828 "Unconditionally turn on Auto Fill mode."
3829 (auto-fill-mode 1))
3831 (defun turn-off-auto-fill ()
3832 "Unconditionally turn off Auto Fill mode."
3833 (auto-fill-mode -1))
3835 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3837 (defun set-fill-column (arg)
3838 "Set `fill-column' to specified argument.
3839 Use \\[universal-argument] followed by a number to specify a column.
3840 Just \\[universal-argument] as argument means to use the current column."
3841 (interactive "P")
3842 (if (consp arg)
3843 (setq arg (current-column)))
3844 (if (not (integerp arg))
3845 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3846 (error "Set-fill-column requires an explicit argument")
3847 (message "Fill column set to %d (was %d)" arg fill-column)
3848 (setq fill-column arg)))
3850 (defun set-selective-display (arg)
3851 "Set `selective-display' to ARG; clear it if no arg.
3852 When the value of `selective-display' is a number > 0,
3853 lines whose indentation is >= that value are not displayed.
3854 The variable `selective-display' has a separate value for each buffer."
3855 (interactive "P")
3856 (if (eq selective-display t)
3857 (error "selective-display already in use for marked lines"))
3858 (let ((current-vpos
3859 (save-restriction
3860 (narrow-to-region (point-min) (point))
3861 (goto-char (window-start))
3862 (vertical-motion (window-height)))))
3863 (setq selective-display
3864 (and arg (prefix-numeric-value arg)))
3865 (recenter current-vpos))
3866 (set-window-start (selected-window) (window-start (selected-window)))
3867 (princ "selective-display set to " t)
3868 (prin1 selective-display t)
3869 (princ "." t))
3871 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
3872 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
3874 (defun toggle-truncate-lines (arg)
3875 "Toggle whether to fold or truncate long lines on the screen.
3876 With arg, truncate long lines iff arg is positive.
3877 Note that in side-by-side windows, truncation is always enabled."
3878 (interactive "P")
3879 (setq truncate-lines
3880 (if (null arg)
3881 (not truncate-lines)
3882 (> (prefix-numeric-value arg) 0)))
3883 (force-mode-line-update)
3884 (unless truncate-lines
3885 (let ((buffer (current-buffer)))
3886 (walk-windows (lambda (window)
3887 (if (eq buffer (window-buffer window))
3888 (set-window-hscroll window 0)))
3889 nil t)))
3890 (message "Truncate long lines %s"
3891 (if truncate-lines "enabled" "disabled")))
3893 (defvar overwrite-mode-textual " Ovwrt"
3894 "The string displayed in the mode line when in overwrite mode.")
3895 (defvar overwrite-mode-binary " Bin Ovwrt"
3896 "The string displayed in the mode line when in binary overwrite mode.")
3898 (defun overwrite-mode (arg)
3899 "Toggle overwrite mode.
3900 With arg, turn overwrite mode on iff arg is positive.
3901 In overwrite mode, printing characters typed in replace existing text
3902 on a one-for-one basis, rather than pushing it to the right. At the
3903 end of a line, such characters extend the line. Before a tab,
3904 such characters insert until the tab is filled in.
3905 \\[quoted-insert] still inserts characters in overwrite mode; this
3906 is supposed to make it easier to insert characters when necessary."
3907 (interactive "P")
3908 (setq overwrite-mode
3909 (if (if (null arg) (not overwrite-mode)
3910 (> (prefix-numeric-value arg) 0))
3911 'overwrite-mode-textual))
3912 (force-mode-line-update))
3914 (defun binary-overwrite-mode (arg)
3915 "Toggle binary overwrite mode.
3916 With arg, turn binary overwrite mode on iff arg is positive.
3917 In binary overwrite mode, printing characters typed in replace
3918 existing text. Newlines are not treated specially, so typing at the
3919 end of a line joins the line to the next, with the typed character
3920 between them. Typing before a tab character simply replaces the tab
3921 with the character typed.
3922 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3923 typing characters do.
3925 Note that binary overwrite mode is not its own minor mode; it is a
3926 specialization of overwrite-mode, entered by setting the
3927 `overwrite-mode' variable to `overwrite-mode-binary'."
3928 (interactive "P")
3929 (setq overwrite-mode
3930 (if (if (null arg)
3931 (not (eq overwrite-mode 'overwrite-mode-binary))
3932 (> (prefix-numeric-value arg) 0))
3933 'overwrite-mode-binary))
3934 (force-mode-line-update))
3936 (define-minor-mode line-number-mode
3937 "Toggle Line Number mode.
3938 With arg, turn Line Number mode on iff arg is positive.
3939 When Line Number mode is enabled, the line number appears
3940 in the mode line.
3942 Line numbers do not appear for very large buffers and buffers
3943 with very long lines; see variables `line-number-display-limit'
3944 and `line-number-display-limit-width'."
3945 :init-value t :global t :group 'editing-basics :require nil)
3947 (define-minor-mode column-number-mode
3948 "Toggle Column Number mode.
3949 With arg, turn Column Number mode on iff arg is positive.
3950 When Column Number mode is enabled, the column number appears
3951 in the mode line."
3952 :global t :group 'editing-basics :require nil)
3954 (define-minor-mode size-indication-mode
3955 "Toggle Size Indication mode.
3956 With arg, turn Size Indication mode on iff arg is positive. When
3957 Size Indication mode is enabled, the size of the accessible part
3958 of the buffer appears in the mode line."
3959 :global t :group 'editing-basics :require nil)
3961 (defgroup paren-blinking nil
3962 "Blinking matching of parens and expressions."
3963 :prefix "blink-matching-"
3964 :group 'paren-matching)
3966 (defcustom blink-matching-paren t
3967 "*Non-nil means show matching open-paren when close-paren is inserted."
3968 :type 'boolean
3969 :group 'paren-blinking)
3971 (defcustom blink-matching-paren-on-screen t
3972 "*Non-nil means show matching open-paren when it is on screen.
3973 If nil, means don't show it (but the open-paren can still be shown
3974 when it is off screen)."
3975 :type 'boolean
3976 :group 'paren-blinking)
3978 (defcustom blink-matching-paren-distance (* 25 1024)
3979 "*If non-nil, is maximum distance to search for matching open-paren."
3980 :type 'integer
3981 :group 'paren-blinking)
3983 (defcustom blink-matching-delay 1
3984 "*Time in seconds to delay after showing a matching paren."
3985 :type 'number
3986 :group 'paren-blinking)
3988 (defcustom blink-matching-paren-dont-ignore-comments nil
3989 "*Non-nil means `blink-matching-paren' will not ignore comments."
3990 :type 'boolean
3991 :group 'paren-blinking)
3993 (defun blink-matching-open ()
3994 "Move cursor momentarily to the beginning of the sexp before point."
3995 (interactive)
3996 (and (> (point) (1+ (point-min)))
3997 blink-matching-paren
3998 ;; Verify an even number of quoting characters precede the close.
3999 (= 1 (logand 1 (- (point)
4000 (save-excursion
4001 (forward-char -1)
4002 (skip-syntax-backward "/\\")
4003 (point)))))
4004 (let* ((oldpos (point))
4005 (blinkpos)
4006 (mismatch)
4007 matching-paren)
4008 (save-excursion
4009 (save-restriction
4010 (if blink-matching-paren-distance
4011 (narrow-to-region (max (point-min)
4012 (- (point) blink-matching-paren-distance))
4013 oldpos))
4014 (condition-case ()
4015 (let ((parse-sexp-ignore-comments
4016 (and parse-sexp-ignore-comments
4017 (not blink-matching-paren-dont-ignore-comments))))
4018 (setq blinkpos (scan-sexps oldpos -1)))
4019 (error nil)))
4020 (and blinkpos
4021 (not (eq (car (syntax-after blinkpos)) 8)) ;Not syntax '$'.
4022 (setq matching-paren
4023 (let ((syntax (syntax-after blinkpos)))
4024 (and (consp syntax)
4025 (eq (logand (car syntax) 255) 4)
4026 (cdr syntax)))
4027 mismatch
4028 (or (null matching-paren)
4029 (/= (char-after (1- oldpos))
4030 matching-paren))))
4031 (if mismatch (setq blinkpos nil))
4032 (if blinkpos
4033 ;; Don't log messages about paren matching.
4034 (let (message-log-max)
4035 (goto-char blinkpos)
4036 (if (pos-visible-in-window-p)
4037 (and blink-matching-paren-on-screen
4038 (sit-for blink-matching-delay))
4039 (goto-char blinkpos)
4040 (message
4041 "Matches %s"
4042 ;; Show what precedes the open in its line, if anything.
4043 (if (save-excursion
4044 (skip-chars-backward " \t")
4045 (not (bolp)))
4046 (buffer-substring (progn (beginning-of-line) (point))
4047 (1+ blinkpos))
4048 ;; Show what follows the open in its line, if anything.
4049 (if (save-excursion
4050 (forward-char 1)
4051 (skip-chars-forward " \t")
4052 (not (eolp)))
4053 (buffer-substring blinkpos
4054 (progn (end-of-line) (point)))
4055 ;; Otherwise show the previous nonblank line,
4056 ;; if there is one.
4057 (if (save-excursion
4058 (skip-chars-backward "\n \t")
4059 (not (bobp)))
4060 (concat
4061 (buffer-substring (progn
4062 (skip-chars-backward "\n \t")
4063 (beginning-of-line)
4064 (point))
4065 (progn (end-of-line)
4066 (skip-chars-backward " \t")
4067 (point)))
4068 ;; Replace the newline and other whitespace with `...'.
4069 "..."
4070 (buffer-substring blinkpos (1+ blinkpos)))
4071 ;; There is nothing to show except the char itself.
4072 (buffer-substring blinkpos (1+ blinkpos))))))))
4073 (cond (mismatch
4074 (message "Mismatched parentheses"))
4075 ((not blink-matching-paren-distance)
4076 (message "Unmatched parenthesis"))))))))
4078 ;Turned off because it makes dbx bomb out.
4079 (setq blink-paren-function 'blink-matching-open)
4081 ;; This executes C-g typed while Emacs is waiting for a command.
4082 ;; Quitting out of a program does not go through here;
4083 ;; that happens in the QUIT macro at the C code level.
4084 (defun keyboard-quit ()
4085 "Signal a `quit' condition.
4086 During execution of Lisp code, this character causes a quit directly.
4087 At top-level, as an editor command, this simply beeps."
4088 (interactive)
4089 (deactivate-mark)
4090 (if (fboundp 'kmacro-keyboard-quit)
4091 (kmacro-keyboard-quit))
4092 (setq defining-kbd-macro nil)
4093 (signal 'quit nil))
4095 (define-key global-map "\C-g" 'keyboard-quit)
4097 (defvar buffer-quit-function nil
4098 "Function to call to \"quit\" the current buffer, or nil if none.
4099 \\[keyboard-escape-quit] calls this function when its more local actions
4100 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4102 (defun keyboard-escape-quit ()
4103 "Exit the current \"mode\" (in a generalized sense of the word).
4104 This command can exit an interactive command such as `query-replace',
4105 can clear out a prefix argument or a region,
4106 can get out of the minibuffer or other recursive edit,
4107 cancel the use of the current buffer (for special-purpose buffers),
4108 or go back to just one window (by deleting all but the selected window)."
4109 (interactive)
4110 (cond ((eq last-command 'mode-exited) nil)
4111 ((> (minibuffer-depth) 0)
4112 (abort-recursive-edit))
4113 (current-prefix-arg
4114 nil)
4115 ((and transient-mark-mode mark-active)
4116 (deactivate-mark))
4117 ((> (recursion-depth) 0)
4118 (exit-recursive-edit))
4119 (buffer-quit-function
4120 (funcall buffer-quit-function))
4121 ((not (one-window-p t))
4122 (delete-other-windows))
4123 ((string-match "^ \\*" (buffer-name (current-buffer)))
4124 (bury-buffer))))
4126 (defun play-sound-file (file &optional volume device)
4127 "Play sound stored in FILE.
4128 VOLUME and DEVICE correspond to the keywords of the sound
4129 specification for `play-sound'."
4130 (interactive "fPlay sound file: ")
4131 (let ((sound (list :file file)))
4132 (if volume
4133 (plist-put sound :volume volume))
4134 (if device
4135 (plist-put sound :device device))
4136 (push 'sound sound)
4137 (play-sound sound)))
4139 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
4141 (defcustom read-mail-command 'rmail
4142 "*Your preference for a mail reading package.
4143 This is used by some keybindings which support reading mail.
4144 See also `mail-user-agent' concerning sending mail."
4145 :type '(choice (function-item rmail)
4146 (function-item gnus)
4147 (function-item mh-rmail)
4148 (function :tag "Other"))
4149 :version "21.1"
4150 :group 'mail)
4152 (defcustom mail-user-agent 'sendmail-user-agent
4153 "*Your preference for a mail composition package.
4154 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4155 outgoing email message. This variable lets you specify which
4156 mail-sending package you prefer.
4158 Valid values include:
4160 `sendmail-user-agent' -- use the default Emacs Mail package.
4161 See Info node `(emacs)Sending Mail'.
4162 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4163 See Info node `(mh-e)'.
4164 `message-user-agent' -- use the Gnus Message package.
4165 See Info node `(message)'.
4166 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4167 paraphernalia, particularly the Gcc: header for
4168 archiving.
4170 Additional valid symbols may be available; check with the author of
4171 your package for details. The function should return non-nil if it
4172 succeeds.
4174 See also `read-mail-command' concerning reading mail."
4175 :type '(radio (function-item :tag "Default Emacs mail"
4176 :format "%t\n"
4177 sendmail-user-agent)
4178 (function-item :tag "Emacs interface to MH"
4179 :format "%t\n"
4180 mh-e-user-agent)
4181 (function-item :tag "Gnus Message package"
4182 :format "%t\n"
4183 message-user-agent)
4184 (function-item :tag "Gnus Message with full Gnus features"
4185 :format "%t\n"
4186 gnus-user-agent)
4187 (function :tag "Other"))
4188 :group 'mail)
4190 (define-mail-user-agent 'sendmail-user-agent
4191 'sendmail-user-agent-compose
4192 'mail-send-and-exit)
4194 (defun rfc822-goto-eoh ()
4195 ;; Go to header delimiter line in a mail message, following RFC822 rules
4196 (goto-char (point-min))
4197 (when (re-search-forward
4198 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4199 (goto-char (match-beginning 0))))
4201 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4202 switch-function yank-action
4203 send-actions)
4204 (if switch-function
4205 (let ((special-display-buffer-names nil)
4206 (special-display-regexps nil)
4207 (same-window-buffer-names nil)
4208 (same-window-regexps nil))
4209 (funcall switch-function "*mail*")))
4210 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4211 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4212 (body (cdr (assoc-string "body" other-headers t))))
4213 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4214 continue
4215 (error "Message aborted"))
4216 (save-excursion
4217 (rfc822-goto-eoh)
4218 (while other-headers
4219 (unless (member-ignore-case (car (car other-headers))
4220 '("in-reply-to" "cc" "body"))
4221 (insert (car (car other-headers)) ": "
4222 (cdr (car other-headers)) "\n"))
4223 (setq other-headers (cdr other-headers)))
4224 (when body
4225 (forward-line 1)
4226 (insert body))
4227 t)))
4229 (define-mail-user-agent 'mh-e-user-agent
4230 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
4231 'mh-before-send-letter-hook)
4233 (defun compose-mail (&optional to subject other-headers continue
4234 switch-function yank-action send-actions)
4235 "Start composing a mail message to send.
4236 This uses the user's chosen mail composition package
4237 as selected with the variable `mail-user-agent'.
4238 The optional arguments TO and SUBJECT specify recipients
4239 and the initial Subject field, respectively.
4241 OTHER-HEADERS is an alist specifying additional
4242 header fields. Elements look like (HEADER . VALUE) where both
4243 HEADER and VALUE are strings.
4245 CONTINUE, if non-nil, says to continue editing a message already
4246 being composed.
4248 SWITCH-FUNCTION, if non-nil, is a function to use to
4249 switch to and display the buffer used for mail composition.
4251 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4252 to insert the raw text of the message being replied to.
4253 It has the form (FUNCTION . ARGS). The user agent will apply
4254 FUNCTION to ARGS, to insert the raw text of the original message.
4255 \(The user agent will also run `mail-citation-hook', *after* the
4256 original text has been inserted in this way.)
4258 SEND-ACTIONS is a list of actions to call when the message is sent.
4259 Each action has the form (FUNCTION . ARGS)."
4260 (interactive
4261 (list nil nil nil current-prefix-arg))
4262 (let ((function (get mail-user-agent 'composefunc)))
4263 (funcall function to subject other-headers continue
4264 switch-function yank-action send-actions)))
4266 (defun compose-mail-other-window (&optional to subject other-headers continue
4267 yank-action send-actions)
4268 "Like \\[compose-mail], but edit the outgoing message in another window."
4269 (interactive
4270 (list nil nil nil current-prefix-arg))
4271 (compose-mail to subject other-headers continue
4272 'switch-to-buffer-other-window yank-action send-actions))
4275 (defun compose-mail-other-frame (&optional to subject other-headers continue
4276 yank-action send-actions)
4277 "Like \\[compose-mail], but edit the outgoing message in another frame."
4278 (interactive
4279 (list nil nil nil current-prefix-arg))
4280 (compose-mail to subject other-headers continue
4281 'switch-to-buffer-other-frame yank-action send-actions))
4283 (defvar set-variable-value-history nil
4284 "History of values entered with `set-variable'.")
4286 (defun set-variable (var val &optional make-local)
4287 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4288 When using this interactively, enter a Lisp object for VALUE.
4289 If you want VALUE to be a string, you must surround it with doublequotes.
4290 VALUE is used literally, not evaluated.
4292 If VARIABLE has a `variable-interactive' property, that is used as if
4293 it were the arg to `interactive' (which see) to interactively read VALUE.
4295 If VARIABLE has been defined with `defcustom', then the type information
4296 in the definition is used to check that VALUE is valid.
4298 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4299 (interactive
4300 (let* ((default-var (variable-at-point))
4301 (var (if (symbolp default-var)
4302 (read-variable (format "Set variable (default %s): " default-var)
4303 default-var)
4304 (read-variable "Set variable: ")))
4305 (minibuffer-help-form '(describe-variable var))
4306 (prop (get var 'variable-interactive))
4307 (prompt (format "Set %s%s to value: " var
4308 (cond ((local-variable-p var)
4309 " (buffer-local)")
4310 ((or current-prefix-arg
4311 (local-variable-if-set-p var))
4312 " buffer-locally")
4313 (t " globally"))))
4314 (val (if prop
4315 ;; Use VAR's `variable-interactive' property
4316 ;; as an interactive spec for prompting.
4317 (call-interactively `(lambda (arg)
4318 (interactive ,prop)
4319 arg))
4320 (read
4321 (read-string prompt nil
4322 'set-variable-value-history)))))
4323 (list var val current-prefix-arg)))
4325 (and (custom-variable-p var)
4326 (not (get var 'custom-type))
4327 (custom-load-symbol var))
4328 (let ((type (get var 'custom-type)))
4329 (when type
4330 ;; Match with custom type.
4331 (require 'cus-edit)
4332 (setq type (widget-convert type))
4333 (unless (widget-apply type :match val)
4334 (error "Value `%S' does not match type %S of %S"
4335 val (car type) var))))
4337 (if make-local
4338 (make-local-variable var))
4340 (set var val)
4342 ;; Force a thorough redisplay for the case that the variable
4343 ;; has an effect on the display, like `tab-width' has.
4344 (force-mode-line-update))
4346 ;; Define the major mode for lists of completions.
4348 (defvar completion-list-mode-map nil
4349 "Local map for completion list buffers.")
4350 (or completion-list-mode-map
4351 (let ((map (make-sparse-keymap)))
4352 (define-key map [mouse-2] 'mouse-choose-completion)
4353 (define-key map [follow-link] 'mouse-face)
4354 (define-key map [down-mouse-2] nil)
4355 (define-key map "\C-m" 'choose-completion)
4356 (define-key map "\e\e\e" 'delete-completion-window)
4357 (define-key map [left] 'previous-completion)
4358 (define-key map [right] 'next-completion)
4359 (setq completion-list-mode-map map)))
4361 ;; Completion mode is suitable only for specially formatted data.
4362 (put 'completion-list-mode 'mode-class 'special)
4364 (defvar completion-reference-buffer nil
4365 "Record the buffer that was current when the completion list was requested.
4366 This is a local variable in the completion list buffer.
4367 Initial value is nil to avoid some compiler warnings.")
4369 (defvar completion-no-auto-exit nil
4370 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4371 This also applies to other functions such as `choose-completion'
4372 and `mouse-choose-completion'.")
4374 (defvar completion-base-size nil
4375 "Number of chars at beginning of minibuffer not involved in completion.
4376 This is a local variable in the completion list buffer
4377 but it talks about the buffer in `completion-reference-buffer'.
4378 If this is nil, it means to compare text to determine which part
4379 of the tail end of the buffer's text is involved in completion.")
4381 (defun delete-completion-window ()
4382 "Delete the completion list window.
4383 Go to the window from which completion was requested."
4384 (interactive)
4385 (let ((buf completion-reference-buffer))
4386 (if (one-window-p t)
4387 (if (window-dedicated-p (selected-window))
4388 (delete-frame (selected-frame)))
4389 (delete-window (selected-window))
4390 (if (get-buffer-window buf)
4391 (select-window (get-buffer-window buf))))))
4393 (defun previous-completion (n)
4394 "Move to the previous item in the completion list."
4395 (interactive "p")
4396 (next-completion (- n)))
4398 (defun next-completion (n)
4399 "Move to the next item in the completion list.
4400 With prefix argument N, move N items (negative N means move backward)."
4401 (interactive "p")
4402 (let ((beg (point-min)) (end (point-max)))
4403 (while (and (> n 0) (not (eobp)))
4404 ;; If in a completion, move to the end of it.
4405 (when (get-text-property (point) 'mouse-face)
4406 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4407 ;; Move to start of next one.
4408 (unless (get-text-property (point) 'mouse-face)
4409 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4410 (setq n (1- n)))
4411 (while (and (< n 0) (not (bobp)))
4412 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4413 ;; If in a completion, move to the start of it.
4414 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4415 (goto-char (previous-single-property-change
4416 (point) 'mouse-face nil beg)))
4417 ;; Move to end of the previous completion.
4418 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4419 (goto-char (previous-single-property-change
4420 (point) 'mouse-face nil beg)))
4421 ;; Move to the start of that one.
4422 (goto-char (previous-single-property-change
4423 (point) 'mouse-face nil beg))
4424 (setq n (1+ n))))))
4426 (defun choose-completion ()
4427 "Choose the completion that point is in or next to."
4428 (interactive)
4429 (let (beg end completion (buffer completion-reference-buffer)
4430 (base-size completion-base-size))
4431 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4432 (setq end (point) beg (1+ (point))))
4433 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4434 (setq end (1- (point)) beg (point)))
4435 (if (null beg)
4436 (error "No completion here"))
4437 (setq beg (previous-single-property-change beg 'mouse-face))
4438 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4439 (setq completion (buffer-substring beg end))
4440 (let ((owindow (selected-window)))
4441 (if (and (one-window-p t 'selected-frame)
4442 (window-dedicated-p (selected-window)))
4443 ;; This is a special buffer's frame
4444 (iconify-frame (selected-frame))
4445 (or (window-dedicated-p (selected-window))
4446 (bury-buffer)))
4447 (select-window owindow))
4448 (choose-completion-string completion buffer base-size)))
4450 ;; Delete the longest partial match for STRING
4451 ;; that can be found before POINT.
4452 (defun choose-completion-delete-max-match (string)
4453 (let ((opoint (point))
4454 len)
4455 ;; Try moving back by the length of the string.
4456 (goto-char (max (- (point) (length string))
4457 (minibuffer-prompt-end)))
4458 ;; See how far back we were actually able to move. That is the
4459 ;; upper bound on how much we can match and delete.
4460 (setq len (- opoint (point)))
4461 (if completion-ignore-case
4462 (setq string (downcase string)))
4463 (while (and (> len 0)
4464 (let ((tail (buffer-substring (point) opoint)))
4465 (if completion-ignore-case
4466 (setq tail (downcase tail)))
4467 (not (string= tail (substring string 0 len)))))
4468 (setq len (1- len))
4469 (forward-char 1))
4470 (delete-char len)))
4472 (defvar choose-completion-string-functions nil
4473 "Functions that may override the normal insertion of a completion choice.
4474 These functions are called in order with four arguments:
4475 CHOICE - the string to insert in the buffer,
4476 BUFFER - the buffer in which the choice should be inserted,
4477 MINI-P - non-nil iff BUFFER is a minibuffer, and
4478 BASE-SIZE - the number of characters in BUFFER before
4479 the string being completed.
4481 If a function in the list returns non-nil, that function is supposed
4482 to have inserted the CHOICE in the BUFFER, and possibly exited
4483 the minibuffer; no further functions will be called.
4485 If all functions in the list return nil, that means to use
4486 the default method of inserting the completion in BUFFER.")
4488 (defun choose-completion-string (choice &optional buffer base-size)
4489 "Switch to BUFFER and insert the completion choice CHOICE.
4490 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4491 to keep. If it is nil, we call `choose-completion-delete-max-match'
4492 to decide what to delete."
4494 ;; If BUFFER is the minibuffer, exit the minibuffer
4495 ;; unless it is reading a file name and CHOICE is a directory,
4496 ;; or completion-no-auto-exit is non-nil.
4498 (let* ((buffer (or buffer completion-reference-buffer))
4499 (mini-p (minibufferp buffer)))
4500 ;; If BUFFER is a minibuffer, barf unless it's the currently
4501 ;; active minibuffer.
4502 (if (and mini-p
4503 (or (not (active-minibuffer-window))
4504 (not (equal buffer
4505 (window-buffer (active-minibuffer-window))))))
4506 (error "Minibuffer is not active for completion")
4507 ;; Set buffer so buffer-local choose-completion-string-functions works.
4508 (set-buffer buffer)
4509 (unless (run-hook-with-args-until-success
4510 'choose-completion-string-functions
4511 choice buffer mini-p base-size)
4512 ;; Insert the completion into the buffer where it was requested.
4513 (if base-size
4514 (delete-region (+ base-size (if mini-p
4515 (minibuffer-prompt-end)
4516 (point-min)))
4517 (point))
4518 (choose-completion-delete-max-match choice))
4519 (insert choice)
4520 (remove-text-properties (- (point) (length choice)) (point)
4521 '(mouse-face nil))
4522 ;; Update point in the window that BUFFER is showing in.
4523 (let ((window (get-buffer-window buffer t)))
4524 (set-window-point window (point)))
4525 ;; If completing for the minibuffer, exit it with this choice.
4526 (and (not completion-no-auto-exit)
4527 (equal buffer (window-buffer (minibuffer-window)))
4528 minibuffer-completion-table
4529 ;; If this is reading a file name, and the file name chosen
4530 ;; is a directory, don't exit the minibuffer.
4531 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4532 (file-directory-p (field-string (point-max))))
4533 (let ((mini (active-minibuffer-window)))
4534 (select-window mini)
4535 (when minibuffer-auto-raise
4536 (raise-frame (window-frame mini))))
4537 (exit-minibuffer)))))))
4539 (defun completion-list-mode ()
4540 "Major mode for buffers showing lists of possible completions.
4541 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4542 to select the completion near point.
4543 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4544 with the mouse."
4545 (interactive)
4546 (kill-all-local-variables)
4547 (use-local-map completion-list-mode-map)
4548 (setq mode-name "Completion List")
4549 (setq major-mode 'completion-list-mode)
4550 (make-local-variable 'completion-base-size)
4551 (setq completion-base-size nil)
4552 (run-hooks 'completion-list-mode-hook))
4554 (defun completion-list-mode-finish ()
4555 "Finish setup of the completions buffer.
4556 Called from `temp-buffer-show-hook'."
4557 (when (eq major-mode 'completion-list-mode)
4558 (toggle-read-only 1)))
4560 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4562 (defvar completion-setup-hook nil
4563 "Normal hook run at the end of setting up a completion list buffer.
4564 When this hook is run, the current buffer is the one in which the
4565 command to display the completion list buffer was run.
4566 The completion list buffer is available as the value of `standard-output'.")
4568 ;; This function goes in completion-setup-hook, so that it is called
4569 ;; after the text of the completion list buffer is written.
4570 (defface completions-first-difference
4571 '((t (:inherit bold)))
4572 "Face put on the first uncommon character in completions in *Completions* buffer."
4573 :group 'completion)
4575 (defface completions-common-part
4576 '((t (:inherit default)))
4577 "Face put on the common prefix substring in completions in *Completions* buffer.
4578 The idea of `completions-common-part' is that you can use it to
4579 make the common parts less visible than normal, so that the rest
4580 of the differing parts is, by contrast, slightly highlighted."
4581 :group 'completion)
4583 ;; This is for packages that need to bind it to a non-default regexp
4584 ;; in order to make the first-differing character highlight work
4585 ;; to their liking
4586 (defvar completion-root-regexp "^/"
4587 "Regexp to use in `completion-setup-function' to find the root directory.")
4589 (defun completion-setup-function ()
4590 (let ((mainbuf (current-buffer))
4591 (mbuf-contents (minibuffer-contents)))
4592 ;; When reading a file name in the minibuffer,
4593 ;; set default-directory in the minibuffer
4594 ;; so it will get copied into the completion list buffer.
4595 (if minibuffer-completing-file-name
4596 (with-current-buffer mainbuf
4597 (setq default-directory (file-name-directory mbuf-contents))))
4598 ;; If partial-completion-mode is on, point might not be after the
4599 ;; last character in the minibuffer.
4600 ;; FIXME: This still doesn't work if the text to be completed
4601 ;; starts with a `-'.
4602 (when (and partial-completion-mode (not (eobp)))
4603 (setq mbuf-contents
4604 (substring mbuf-contents 0 (- (point) (point-max)))))
4605 (with-current-buffer standard-output
4606 (completion-list-mode)
4607 (make-local-variable 'completion-reference-buffer)
4608 (setq completion-reference-buffer mainbuf)
4609 (if minibuffer-completing-file-name
4610 ;; For file name completion,
4611 ;; use the number of chars before the start of the
4612 ;; last file name component.
4613 (setq completion-base-size
4614 (with-current-buffer mainbuf
4615 (save-excursion
4616 (goto-char (point-max))
4617 (skip-chars-backward completion-root-regexp)
4618 (- (point) (minibuffer-prompt-end)))))
4619 ;; Otherwise, in minibuffer, the whole input is being completed.
4620 (if (minibufferp mainbuf)
4621 (setq completion-base-size 0)))
4622 ;; Put faces on first uncommon characters and common parts.
4623 (when completion-base-size
4624 (let* ((common-string-length
4625 (- (length mbuf-contents) completion-base-size))
4626 (element-start (next-single-property-change
4627 (point-min)
4628 'mouse-face))
4629 (element-common-end
4630 (+ (or element-start nil) common-string-length))
4631 (maxp (point-max)))
4632 (while (and element-start (< element-common-end maxp))
4633 (when (and (get-char-property element-start 'mouse-face)
4634 (get-char-property element-common-end 'mouse-face))
4635 (put-text-property element-start element-common-end
4636 'font-lock-face 'completions-common-part)
4637 (put-text-property element-common-end (1+ element-common-end)
4638 'font-lock-face 'completions-first-difference))
4639 (setq element-start (next-single-property-change
4640 element-start
4641 'mouse-face))
4642 (if element-start
4643 (setq element-common-end (+ element-start common-string-length))))))
4644 ;; Insert help string.
4645 (goto-char (point-min))
4646 (if (display-mouse-p)
4647 (insert (substitute-command-keys
4648 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4649 (insert (substitute-command-keys
4650 "In this buffer, type \\[choose-completion] to \
4651 select the completion near point.\n\n")))))
4653 (add-hook 'completion-setup-hook 'completion-setup-function)
4655 (define-key minibuffer-local-completion-map [prior]
4656 'switch-to-completions)
4657 (define-key minibuffer-local-must-match-map [prior]
4658 'switch-to-completions)
4659 (define-key minibuffer-local-completion-map "\M-v"
4660 'switch-to-completions)
4661 (define-key minibuffer-local-must-match-map "\M-v"
4662 'switch-to-completions)
4664 (defun switch-to-completions ()
4665 "Select the completion list window."
4666 (interactive)
4667 ;; Make sure we have a completions window.
4668 (or (get-buffer-window "*Completions*")
4669 (minibuffer-completion-help))
4670 (let ((window (get-buffer-window "*Completions*")))
4671 (when window
4672 (select-window window)
4673 (goto-char (point-min))
4674 (search-forward "\n\n")
4675 (forward-line 1))))
4677 ;; Support keyboard commands to turn on various modifiers.
4679 ;; These functions -- which are not commands -- each add one modifier
4680 ;; to the following event.
4682 (defun event-apply-alt-modifier (ignore-prompt)
4683 "\\<function-key-map>Add the Alt modifier to the following event.
4684 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
4685 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
4686 (defun event-apply-super-modifier (ignore-prompt)
4687 "\\<function-key-map>Add the Super modifier to the following event.
4688 For example, type \\[event-apply-super-modifier] & to enter Super-&."
4689 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
4690 (defun event-apply-hyper-modifier (ignore-prompt)
4691 "\\<function-key-map>Add the Hyper modifier to the following event.
4692 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
4693 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
4694 (defun event-apply-shift-modifier (ignore-prompt)
4695 "\\<function-key-map>Add the Shift modifier to the following event.
4696 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
4697 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
4698 (defun event-apply-control-modifier (ignore-prompt)
4699 "\\<function-key-map>Add the Ctrl modifier to the following event.
4700 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
4701 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
4702 (defun event-apply-meta-modifier (ignore-prompt)
4703 "\\<function-key-map>Add the Meta modifier to the following event.
4704 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
4705 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
4707 (defun event-apply-modifier (event symbol lshiftby prefix)
4708 "Apply a modifier flag to event EVENT.
4709 SYMBOL is the name of this modifier, as a symbol.
4710 LSHIFTBY is the numeric value of this modifier, in keyboard events.
4711 PREFIX is the string that represents this modifier in an event type symbol."
4712 (if (numberp event)
4713 (cond ((eq symbol 'control)
4714 (if (and (<= (downcase event) ?z)
4715 (>= (downcase event) ?a))
4716 (- (downcase event) ?a -1)
4717 (if (and (<= (downcase event) ?Z)
4718 (>= (downcase event) ?A))
4719 (- (downcase event) ?A -1)
4720 (logior (lsh 1 lshiftby) event))))
4721 ((eq symbol 'shift)
4722 (if (and (<= (downcase event) ?z)
4723 (>= (downcase event) ?a))
4724 (upcase event)
4725 (logior (lsh 1 lshiftby) event)))
4727 (logior (lsh 1 lshiftby) event)))
4728 (if (memq symbol (event-modifiers event))
4729 event
4730 (let ((event-type (if (symbolp event) event (car event))))
4731 (setq event-type (intern (concat prefix (symbol-name event-type))))
4732 (if (symbolp event)
4733 event-type
4734 (cons event-type (cdr event)))))))
4736 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4737 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4738 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4739 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4740 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4741 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4743 ;;;; Keypad support.
4745 ;;; Make the keypad keys act like ordinary typing keys. If people add
4746 ;;; bindings for the function key symbols, then those bindings will
4747 ;;; override these, so this shouldn't interfere with any existing
4748 ;;; bindings.
4750 ;; Also tell read-char how to handle these keys.
4751 (mapc
4752 (lambda (keypad-normal)
4753 (let ((keypad (nth 0 keypad-normal))
4754 (normal (nth 1 keypad-normal)))
4755 (put keypad 'ascii-character normal)
4756 (define-key function-key-map (vector keypad) (vector normal))))
4757 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4758 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4759 (kp-space ?\ )
4760 (kp-tab ?\t)
4761 (kp-enter ?\r)
4762 (kp-multiply ?*)
4763 (kp-add ?+)
4764 (kp-separator ?,)
4765 (kp-subtract ?-)
4766 (kp-decimal ?.)
4767 (kp-divide ?/)
4768 (kp-equal ?=)))
4770 ;;;;
4771 ;;;; forking a twin copy of a buffer.
4772 ;;;;
4774 (defvar clone-buffer-hook nil
4775 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4777 (defun clone-process (process &optional newname)
4778 "Create a twin copy of PROCESS.
4779 If NEWNAME is nil, it defaults to PROCESS' name;
4780 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4781 If PROCESS is associated with a buffer, the new process will be associated
4782 with the current buffer instead.
4783 Returns nil if PROCESS has already terminated."
4784 (setq newname (or newname (process-name process)))
4785 (if (string-match "<[0-9]+>\\'" newname)
4786 (setq newname (substring newname 0 (match-beginning 0))))
4787 (when (memq (process-status process) '(run stop open))
4788 (let* ((process-connection-type (process-tty-name process))
4789 (new-process
4790 (if (memq (process-status process) '(open))
4791 (let ((args (process-contact process t)))
4792 (setq args (plist-put args :name newname))
4793 (setq args (plist-put args :buffer
4794 (if (process-buffer process)
4795 (current-buffer))))
4796 (apply 'make-network-process args))
4797 (apply 'start-process newname
4798 (if (process-buffer process) (current-buffer))
4799 (process-command process)))))
4800 (set-process-query-on-exit-flag
4801 new-process (process-query-on-exit-flag process))
4802 (set-process-inherit-coding-system-flag
4803 new-process (process-inherit-coding-system-flag process))
4804 (set-process-filter new-process (process-filter process))
4805 (set-process-sentinel new-process (process-sentinel process))
4806 (set-process-plist new-process (copy-sequence (process-plist process)))
4807 new-process)))
4809 ;; things to maybe add (currently partly covered by `funcall mode'):
4810 ;; - syntax-table
4811 ;; - overlays
4812 (defun clone-buffer (&optional newname display-flag)
4813 "Create and return a twin copy of the current buffer.
4814 Unlike an indirect buffer, the new buffer can be edited
4815 independently of the old one (if it is not read-only).
4816 NEWNAME is the name of the new buffer. It may be modified by
4817 adding or incrementing <N> at the end as necessary to create a
4818 unique buffer name. If nil, it defaults to the name of the
4819 current buffer, with the proper suffix. If DISPLAY-FLAG is
4820 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
4821 clone a file-visiting buffer, or a buffer whose major mode symbol
4822 has a non-nil `no-clone' property, results in an error.
4824 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
4825 current buffer with appropriate suffix. However, if a prefix
4826 argument is given, then the command prompts for NEWNAME in the
4827 minibuffer.
4829 This runs the normal hook `clone-buffer-hook' in the new buffer
4830 after it has been set up properly in other respects."
4831 (interactive
4832 (progn
4833 (if buffer-file-name
4834 (error "Cannot clone a file-visiting buffer"))
4835 (if (get major-mode 'no-clone)
4836 (error "Cannot clone a buffer in %s mode" mode-name))
4837 (list (if current-prefix-arg (read-string "Name: "))
4838 t)))
4839 (if buffer-file-name
4840 (error "Cannot clone a file-visiting buffer"))
4841 (if (get major-mode 'no-clone)
4842 (error "Cannot clone a buffer in %s mode" mode-name))
4843 (setq newname (or newname (buffer-name)))
4844 (if (string-match "<[0-9]+>\\'" newname)
4845 (setq newname (substring newname 0 (match-beginning 0))))
4846 (let ((buf (current-buffer))
4847 (ptmin (point-min))
4848 (ptmax (point-max))
4849 (pt (point))
4850 (mk (if mark-active (mark t)))
4851 (modified (buffer-modified-p))
4852 (mode major-mode)
4853 (lvars (buffer-local-variables))
4854 (process (get-buffer-process (current-buffer)))
4855 (new (generate-new-buffer (or newname (buffer-name)))))
4856 (save-restriction
4857 (widen)
4858 (with-current-buffer new
4859 (insert-buffer-substring buf)))
4860 (with-current-buffer new
4861 (narrow-to-region ptmin ptmax)
4862 (goto-char pt)
4863 (if mk (set-mark mk))
4864 (set-buffer-modified-p modified)
4866 ;; Clone the old buffer's process, if any.
4867 (when process (clone-process process))
4869 ;; Now set up the major mode.
4870 (funcall mode)
4872 ;; Set up other local variables.
4873 (mapcar (lambda (v)
4874 (condition-case () ;in case var is read-only
4875 (if (symbolp v)
4876 (makunbound v)
4877 (set (make-local-variable (car v)) (cdr v)))
4878 (error nil)))
4879 lvars)
4881 ;; Run any hooks (typically set up by the major mode
4882 ;; for cloning to work properly).
4883 (run-hooks 'clone-buffer-hook))
4884 (if display-flag (pop-to-buffer new))
4885 new))
4888 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4889 "Create an indirect buffer that is a twin copy of the current buffer.
4891 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4892 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4893 or if not called with a prefix arg, NEWNAME defaults to the current
4894 buffer's name. The name is modified by adding a `<N>' suffix to it
4895 or by incrementing the N in an existing suffix.
4897 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4898 This is always done when called interactively.
4900 Optional last arg NORECORD non-nil means do not put this buffer at the
4901 front of the list of recently selected ones."
4902 (interactive
4903 (progn
4904 (if (get major-mode 'no-clone-indirect)
4905 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4906 (list (if current-prefix-arg
4907 (read-string "BName of indirect buffer: "))
4908 t)))
4909 (if (get major-mode 'no-clone-indirect)
4910 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4911 (setq newname (or newname (buffer-name)))
4912 (if (string-match "<[0-9]+>\\'" newname)
4913 (setq newname (substring newname 0 (match-beginning 0))))
4914 (let* ((name (generate-new-buffer-name newname))
4915 (buffer (make-indirect-buffer (current-buffer) name t)))
4916 (when display-flag
4917 (pop-to-buffer buffer norecord))
4918 buffer))
4921 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4922 "Create an indirect buffer that is a twin copy of BUFFER.
4923 Select the new buffer in another window.
4924 Optional second arg NORECORD non-nil means do not put this buffer at
4925 the front of the list of recently selected ones."
4926 (interactive "bClone buffer in other window: ")
4927 (let ((pop-up-windows t))
4928 (set-buffer buffer)
4929 (clone-indirect-buffer nil t norecord)))
4931 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4933 ;;; Handling of Backspace and Delete keys.
4935 (defcustom normal-erase-is-backspace nil
4936 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4938 On window systems, the default value of this option is chosen
4939 according to the keyboard used. If the keyboard has both a Backspace
4940 key and a Delete key, and both are mapped to their usual meanings, the
4941 option's default value is set to t, so that Backspace can be used to
4942 delete backward, and Delete can be used to delete forward.
4944 If not running under a window system, customizing this option accomplishes
4945 a similar effect by mapping C-h, which is usually generated by the
4946 Backspace key, to DEL, and by mapping DEL to C-d via
4947 `keyboard-translate'. The former functionality of C-h is available on
4948 the F1 key. You should probably not use this setting if you don't
4949 have both Backspace, Delete and F1 keys.
4951 Setting this variable with setq doesn't take effect. Programmatically,
4952 call `normal-erase-is-backspace-mode' (which see) instead."
4953 :type 'boolean
4954 :group 'editing-basics
4955 :version "21.1"
4956 :set (lambda (symbol value)
4957 ;; The fboundp is because of a problem with :set when
4958 ;; dumping Emacs. It doesn't really matter.
4959 (if (fboundp 'normal-erase-is-backspace-mode)
4960 (normal-erase-is-backspace-mode (or value 0))
4961 (set-default symbol value))))
4964 (defun normal-erase-is-backspace-mode (&optional arg)
4965 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4967 With numeric arg, turn the mode on if and only if ARG is positive.
4969 On window systems, when this mode is on, Delete is mapped to C-d and
4970 Backspace is mapped to DEL; when this mode is off, both Delete and
4971 Backspace are mapped to DEL. (The remapping goes via
4972 `function-key-map', so binding Delete or Backspace in the global or
4973 local keymap will override that.)
4975 In addition, on window systems, the bindings of C-Delete, M-Delete,
4976 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4977 the global keymap in accordance with the functionality of Delete and
4978 Backspace. For example, if Delete is remapped to C-d, which deletes
4979 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4980 to DEL, which deletes backward, C-Delete is bound to
4981 `backward-kill-word'.
4983 If not running on a window system, a similar effect is accomplished by
4984 remapping C-h (normally produced by the Backspace key) and DEL via
4985 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4986 to C-d; if it's off, the keys are not remapped.
4988 When not running on a window system, and this mode is turned on, the
4989 former functionality of C-h is available on the F1 key. You should
4990 probably not turn on this mode on a text-only terminal if you don't
4991 have both Backspace, Delete and F1 keys.
4993 See also `normal-erase-is-backspace'."
4994 (interactive "P")
4995 (setq normal-erase-is-backspace
4996 (if arg
4997 (> (prefix-numeric-value arg) 0)
4998 (not normal-erase-is-backspace)))
5000 (cond ((or (memq window-system '(x w32 mac pc))
5001 (memq system-type '(ms-dos windows-nt)))
5002 (let ((bindings
5003 `(([C-delete] [C-backspace])
5004 ([M-delete] [M-backspace])
5005 ([C-M-delete] [C-M-backspace])
5006 (,esc-map
5007 [C-delete] [C-backspace])))
5008 (old-state (lookup-key function-key-map [delete])))
5010 (if normal-erase-is-backspace
5011 (progn
5012 (define-key function-key-map [delete] [?\C-d])
5013 (define-key function-key-map [kp-delete] [?\C-d])
5014 (define-key function-key-map [backspace] [?\C-?]))
5015 (define-key function-key-map [delete] [?\C-?])
5016 (define-key function-key-map [kp-delete] [?\C-?])
5017 (define-key function-key-map [backspace] [?\C-?]))
5019 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5020 (unless (equal old-state (lookup-key function-key-map [delete]))
5021 (dolist (binding bindings)
5022 (let ((map global-map))
5023 (when (keymapp (car binding))
5024 (setq map (car binding) binding (cdr binding)))
5025 (let* ((key1 (nth 0 binding))
5026 (key2 (nth 1 binding))
5027 (binding1 (lookup-key map key1))
5028 (binding2 (lookup-key map key2)))
5029 (define-key map key1 binding2)
5030 (define-key map key2 binding1)))))))
5032 (if normal-erase-is-backspace
5033 (progn
5034 (keyboard-translate ?\C-h ?\C-?)
5035 (keyboard-translate ?\C-? ?\C-d))
5036 (keyboard-translate ?\C-h ?\C-h)
5037 (keyboard-translate ?\C-? ?\C-?))))
5039 (run-hooks 'normal-erase-is-backspace-hook)
5040 (if (interactive-p)
5041 (message "Delete key deletes %s"
5042 (if normal-erase-is-backspace "forward" "backward"))))
5044 (defcustom idle-update-delay 0.5
5045 "*Idle time delay before updating various things on the screen.
5046 Various Emacs features that update auxiliary information when point moves
5047 wait this many seconds after Emacs becomes idle before doing an update."
5048 :type 'number
5049 :group 'display
5050 :version "21.4")
5052 (defvar vis-mode-saved-buffer-invisibility-spec nil
5053 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5055 (define-minor-mode visible-mode
5056 "Toggle Visible mode.
5057 With argument ARG turn Visible mode on iff ARG is positive.
5059 Enabling Visible mode makes all invisible text temporarily visible.
5060 Disabling Visible mode turns off that effect. Visible mode
5061 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5062 :lighter " Vis"
5063 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5064 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5065 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5066 (when visible-mode
5067 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5068 buffer-invisibility-spec)
5069 (setq buffer-invisibility-spec nil)))
5071 ;; Minibuffer prompt stuff.
5073 ;(defun minibuffer-prompt-modification (start end)
5074 ; (error "You cannot modify the prompt"))
5077 ;(defun minibuffer-prompt-insertion (start end)
5078 ; (let ((inhibit-modification-hooks t))
5079 ; (delete-region start end)
5080 ; ;; Discard undo information for the text insertion itself
5081 ; ;; and for the text deletion.above.
5082 ; (when (consp buffer-undo-list)
5083 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5084 ; (message "You cannot modify the prompt")))
5087 ;(setq minibuffer-prompt-properties
5088 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5089 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5092 (provide 'simple)
5094 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5095 ;;; simple.el ends here