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, 2006 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; A grab-bag of basic Emacs commands not specifically related to some
29 ;; major mode or to file-handling.
34 (autoload 'widget-convert
"wid-edit")
35 (autoload 'shell-mode
"shell"))
37 (defvar compilation-current-error
)
39 (defcustom idle-update-delay
0.5
40 "*Idle time delay before updating various things on the screen.
41 Various Emacs features that update auxiliary information when point moves
42 wait this many seconds after Emacs becomes idle before doing an update."
48 "Killing and yanking commands."
51 (defgroup paren-matching nil
52 "Highlight (un)matching of parens and expressions."
55 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
56 "Search LIST for a valid buffer to display in FRAME.
57 Return nil when all buffers in LIST are undesirable for display,
58 otherwise return the first suitable buffer in LIST.
60 Buffers not visible in windows are preferred to visible buffers,
61 unless VISIBLE-OK is non-nil.
62 If the optional argument FRAME is nil, it defaults to the selected frame.
63 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
64 ;; This logic is more or less copied from other-buffer.
65 (setq frame
(or frame
(selected-frame)))
66 (let ((pred (frame-parameter frame
'buffer-predicate
))
68 (while (and (not found
) list
)
70 (if (and (not (eq buffer buf
))
72 (or (null pred
) (funcall pred buf
))
73 (not (eq (aref (buffer-name buf
) 0) ?\s
))
74 (or visible-ok
(null (get-buffer-window buf
'visible
))))
76 (setq list
(cdr list
))))
79 (defun last-buffer (&optional buffer visible-ok frame
)
80 "Return the last non-hidden displayable buffer in the buffer list.
81 If BUFFER is non-nil, last-buffer will ignore that buffer.
82 Buffers not visible in windows are preferred to visible buffers,
83 unless optional argument VISIBLE-OK is non-nil.
84 If the optional third argument FRAME is non-nil, use that frame's
85 buffer list instead of the selected frame's buffer list.
86 If no other buffer exists, the buffer `*scratch*' is returned."
87 (setq frame
(or frame
(selected-frame)))
88 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
89 buffer visible-ok frame
)
91 (set-buffer-major-mode (get-buffer-create "*scratch*"))
92 (get-buffer "*scratch*"))))
94 "Switch to the next buffer in cyclic order."
96 (let ((buffer (current-buffer)))
97 (switch-to-buffer (other-buffer buffer t
))
98 (bury-buffer buffer
)))
100 (defun previous-buffer ()
101 "Switch to the previous buffer in cyclic order."
103 (switch-to-buffer (last-buffer (current-buffer) t
)))
106 ;;; next-error support framework
108 (defgroup next-error nil
109 "`next-error' support framework."
114 '((t (:inherit region
)))
115 "Face used to highlight next error locus."
119 (defcustom next-error-highlight
0.5
120 "*Highlighting of locations in selected source buffers.
121 If number, highlight the locus in `next-error' face for given time in seconds.
122 If t, highlight the locus indefinitely until some other locus replaces it.
123 If nil, don't highlight the locus in the source buffer.
124 If `fringe-arrow', indicate the locus by the fringe arrow."
125 :type
'(choice (number :tag
"Highlight for specified time")
126 (const :tag
"Semipermanent highlighting" t
)
127 (const :tag
"No highlighting" nil
)
128 (const :tag
"Fringe arrow" fringe-arrow
))
132 (defcustom next-error-highlight-no-select
0.5
133 "*Highlighting of locations in `next-error-no-select'.
134 If number, highlight the locus in `next-error' face for given time in seconds.
135 If t, highlight the locus indefinitely until some other locus replaces it.
136 If nil, don't highlight the locus in the source buffer.
137 If `fringe-arrow', indicate the locus by the fringe arrow."
138 :type
'(choice (number :tag
"Highlight for specified time")
139 (const :tag
"Semipermanent highlighting" t
)
140 (const :tag
"No highlighting" nil
)
141 (const :tag
"Fringe arrow" fringe-arrow
))
145 (defcustom next-error-hook nil
146 "*List of hook functions run by `next-error' after visiting source file."
150 (defvar next-error-highlight-timer nil
)
152 (defvar next-error-overlay-arrow-position nil
)
153 (put 'next-error-overlay-arrow-position
'overlay-arrow-string
"=>")
154 (add-to-list 'overlay-arrow-variable-list
'next-error-overlay-arrow-position
)
156 (defvar next-error-last-buffer nil
157 "The most recent `next-error' buffer.
158 A buffer becomes most recent when its compilation, grep, or
159 similar mode is started, or when it is used with \\[next-error]
160 or \\[compile-goto-error].")
162 (defvar next-error-function nil
163 "Function to use to find the next error in the current buffer.
164 The function is called with 2 parameters:
165 ARG is an integer specifying by how many errors to move.
166 RESET is a boolean which, if non-nil, says to go back to the beginning
167 of the errors before moving.
168 Major modes providing compile-like functionality should set this variable
169 to indicate to `next-error' that this is a candidate buffer and how
172 (make-variable-buffer-local 'next-error-function
)
174 (defsubst next-error-buffer-p
(buffer
175 &optional avoid-current
177 extra-test-exclusive
)
178 "Test if BUFFER is a `next-error' capable buffer.
180 If AVOID-CURRENT is non-nil, treat the current buffer
181 as an absolute last resort only.
183 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
184 that normally would not qualify. If it returns t, the buffer
185 in question is treated as usable.
187 The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
188 that would normally be considered usable. If it returns nil,
189 that buffer is rejected."
190 (and (buffer-name buffer
) ;First make sure it's live.
191 (not (and avoid-current
(eq buffer
(current-buffer))))
192 (with-current-buffer buffer
193 (if next-error-function
; This is the normal test.
194 ;; Optionally reject some buffers.
195 (if extra-test-exclusive
196 (funcall extra-test-exclusive
)
198 ;; Optionally accept some other buffers.
199 (and extra-test-inclusive
200 (funcall extra-test-inclusive
))))))
202 (defun next-error-find-buffer (&optional avoid-current
204 extra-test-exclusive
)
205 "Return a `next-error' capable buffer.
207 If AVOID-CURRENT is non-nil, treat the current buffer
208 as an absolute last resort only.
210 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
211 that normally would not qualify. If it returns t, the buffer
212 in question is treated as usable.
214 The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
215 that would normally be considered usable. If it returns nil,
216 that buffer is rejected."
218 ;; 1. If one window on the selected frame displays such buffer, return it.
219 (let ((window-buffers
221 (delq nil
(mapcar (lambda (w)
222 (if (next-error-buffer-p
225 extra-test-inclusive extra-test-exclusive
)
228 (if (eq (length window-buffers
) 1)
229 (car window-buffers
)))
230 ;; 2. If next-error-last-buffer is an acceptable buffer, use that.
231 (if (and next-error-last-buffer
232 (next-error-buffer-p next-error-last-buffer avoid-current
233 extra-test-inclusive extra-test-exclusive
))
234 next-error-last-buffer
)
235 ;; 3. If the current buffer is acceptable, choose it.
236 (if (next-error-buffer-p (current-buffer) avoid-current
237 extra-test-inclusive extra-test-exclusive
)
239 ;; 4. Look for any acceptable buffer.
240 (let ((buffers (buffer-list)))
242 (not (next-error-buffer-p
243 (car buffers
) avoid-current
244 extra-test-inclusive extra-test-exclusive
)))
245 (setq buffers
(cdr buffers
)))
247 ;; 5. Use the current buffer as a last resort if it qualifies,
248 ;; even despite AVOID-CURRENT.
250 (next-error-buffer-p (current-buffer) nil
251 extra-test-inclusive extra-test-exclusive
)
253 (message "This is the only buffer with error message locations")
256 (error "No buffers contain error message locations")))
258 (defun next-error (&optional arg reset
)
259 "Visit next `next-error' message and corresponding source code.
261 If all the error messages parsed so far have been processed already,
262 the message buffer is checked for new ones.
264 A prefix ARG specifies how many error messages to move;
265 negative means move back to previous error messages.
266 Just \\[universal-argument] as a prefix means reparse the error message buffer
267 and start at the first error.
269 The RESET argument specifies that we should restart from the beginning.
271 \\[next-error] normally uses the most recently started
272 compilation, grep, or occur buffer. It can also operate on any
273 buffer with output from the \\[compile], \\[grep] commands, or,
274 more generally, on any buffer in Compilation mode or with
275 Compilation Minor mode enabled, or any buffer in which
276 `next-error-function' is bound to an appropriate function.
277 To specify use of a particular buffer for error messages, type
278 \\[next-error] in that buffer when it is the only one displayed
279 in the current frame.
281 Once \\[next-error] has chosen the buffer for error messages, it
282 runs `next-error-hook' with `run-hooks', and stays with that buffer
283 until you use it in some other buffer which uses Compilation mode
284 or Compilation Minor mode.
286 See variables `compilation-parse-errors-function' and
287 \`compilation-error-regexp-alist' for customization ideas."
289 (if (consp arg
) (setq reset t arg nil
))
290 (when (setq next-error-last-buffer
(next-error-find-buffer))
291 ;; we know here that next-error-function is a valid symbol we can funcall
292 (with-current-buffer next-error-last-buffer
293 (funcall next-error-function
(prefix-numeric-value arg
) reset
)
294 (run-hooks 'next-error-hook
))))
296 (defun next-error-internal ()
297 "Visit the source code corresponding to the `next-error' message at point."
298 (setq next-error-last-buffer
(current-buffer))
299 ;; we know here that next-error-function is a valid symbol we can funcall
300 (with-current-buffer next-error-last-buffer
301 (funcall next-error-function
0 nil
)
302 (run-hooks 'next-error-hook
)))
304 (defalias 'goto-next-locus
'next-error
)
305 (defalias 'next-match
'next-error
)
307 (defun previous-error (&optional n
)
308 "Visit previous `next-error' message and corresponding source code.
310 Prefix arg N says how many error messages to move backwards (or
311 forwards, if negative).
313 This operates on the output from the \\[compile] and \\[grep] commands."
315 (next-error (- (or n
1))))
317 (defun first-error (&optional n
)
318 "Restart at the first error.
319 Visit corresponding source code.
320 With prefix arg N, visit the source code of the Nth error.
321 This operates on the output from the \\[compile] command, for instance."
325 (defun next-error-no-select (&optional n
)
326 "Move point to the next error in the `next-error' buffer and highlight match.
327 Prefix arg N says how many error messages to move forwards (or
328 backwards, if negative).
329 Finds and highlights the source line like \\[next-error], but does not
330 select the source buffer."
332 (let ((next-error-highlight next-error-highlight-no-select
))
334 (pop-to-buffer next-error-last-buffer
))
336 (defun previous-error-no-select (&optional n
)
337 "Move point to the previous error in the `next-error' buffer and highlight match.
338 Prefix arg N says how many error messages to move backwards (or
339 forwards, if negative).
340 Finds and highlights the source line like \\[previous-error], but does not
341 select the source buffer."
343 (next-error-no-select (- (or n
1))))
345 ;;; Internal variable for `next-error-follow-mode-post-command-hook'.
346 (defvar next-error-follow-last-line nil
)
348 (define-minor-mode next-error-follow-minor-mode
349 "Minor mode for compilation, occur and diff modes.
350 When turned on, cursor motion in the compilation, grep, occur or diff
351 buffer causes automatic display of the corresponding source code
353 :group
'next-error
:init-value nil
:lighter
" Fol"
354 (if (not next-error-follow-minor-mode
)
355 (remove-hook 'post-command-hook
'next-error-follow-mode-post-command-hook t
)
356 (add-hook 'post-command-hook
'next-error-follow-mode-post-command-hook nil t
)
357 (make-local-variable 'next-error-follow-last-line
)))
359 ;;; Used as a `post-command-hook' by `next-error-follow-mode'
360 ;;; for the *Compilation* *grep* and *Occur* buffers.
361 (defun next-error-follow-mode-post-command-hook ()
362 (unless (equal next-error-follow-last-line
(line-number-at-pos))
363 (setq next-error-follow-last-line
(line-number-at-pos))
365 (let ((compilation-context-lines nil
))
366 (setq compilation-current-error
(point))
367 (next-error-no-select 0))
373 (defun fundamental-mode ()
374 "Major mode not specialized for anything in particular.
375 Other major modes are defined by comparison with this one."
377 (kill-all-local-variables)
378 (unless delay-mode-hooks
379 (run-hooks 'after-change-major-mode-hook
)))
381 ;; Making and deleting lines.
383 (defvar hard-newline
(propertize "\n" 'hard t
'rear-nonsticky
'(hard)))
385 (defun newline (&optional arg
)
386 "Insert a newline, and move to left margin of the new line if it's blank.
387 If `use-hard-newlines' is non-nil, the newline is marked with the
388 text-property `hard'.
389 With ARG, insert that many newlines.
390 Call `auto-fill-function' if the current column number is greater
391 than the value of `fill-column' and ARG is nil."
393 (barf-if-buffer-read-only)
394 ;; Inserting a newline at the end of a line produces better redisplay in
395 ;; try_window_id than inserting at the beginning of a line, and the textual
396 ;; result is the same. So, if we're at beginning of line, pretend to be at
397 ;; the end of the previous line.
398 (let ((flag (and (not (bobp))
400 ;; Make sure no functions want to be told about
401 ;; the range of the changes.
402 (not after-change-functions
)
403 (not before-change-functions
)
404 ;; Make sure there are no markers here.
405 (not (buffer-has-markers-at (1- (point))))
406 (not (buffer-has-markers-at (point)))
407 ;; Make sure no text properties want to know
408 ;; where the change was.
409 (not (get-char-property (1- (point)) 'modification-hooks
))
410 (not (get-char-property (1- (point)) 'insert-behind-hooks
))
412 (not (get-char-property (point) 'insert-in-front-hooks
)))
413 ;; Make sure the newline before point isn't intangible.
414 (not (get-char-property (1- (point)) 'intangible
))
415 ;; Make sure the newline before point isn't read-only.
416 (not (get-char-property (1- (point)) 'read-only
))
417 ;; Make sure the newline before point isn't invisible.
418 (not (get-char-property (1- (point)) 'invisible
))
419 ;; Make sure the newline before point has the same
420 ;; properties as the char before it (if any).
421 (< (or (previous-property-change (point)) -
2)
423 (was-page-start (and (bolp)
424 (looking-at page-delimiter
)))
426 (if flag
(backward-char 1))
427 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
428 ;; Set last-command-char to tell self-insert what to insert.
429 (let ((last-command-char ?
\n)
430 ;; Don't auto-fill if we have a numeric argument.
431 ;; Also not if flag is true (it would fill wrong line);
432 ;; there is no need to since we're at BOL.
433 (auto-fill-function (if (or arg flag
) nil auto-fill-function
)))
435 (self-insert-command (prefix-numeric-value arg
))
436 ;; If we get an error in self-insert-command, put point at right place.
437 (if flag
(forward-char 1))))
438 ;; Even if we did *not* get an error, keep that forward-char;
439 ;; all further processing should apply to the newline that the user
440 ;; thinks he inserted.
442 ;; Mark the newline(s) `hard'.
443 (if use-hard-newlines
444 (set-hard-newline-properties
445 (- (point) (if arg
(prefix-numeric-value arg
) 1)) (point)))
446 ;; If the newline leaves the previous line blank,
447 ;; and we have a left margin, delete that from the blank line.
450 (goto-char beforepos
)
452 (and (looking-at "[ \t]$")
453 (> (current-left-margin) 0)
454 (delete-region (point) (progn (end-of-line) (point))))))
455 ;; Indent the line after the newline, except in one case:
456 ;; when we added the newline at the beginning of a line
457 ;; which starts a page.
459 (move-to-left-margin nil t
)))
462 (defun set-hard-newline-properties (from to
)
463 (let ((sticky (get-text-property from
'rear-nonsticky
)))
464 (put-text-property from to
'hard
't
)
465 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
466 (if (and (listp sticky
) (not (memq 'hard sticky
)))
467 (put-text-property from
(point) 'rear-nonsticky
468 (cons 'hard sticky
)))))
471 "Insert a newline and leave point before it.
472 If there is a fill prefix and/or a `left-margin', insert them
473 on the new line if the line would have been blank.
474 With arg N, insert N newlines."
476 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
477 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
479 ;; Don't expand an abbrev before point.
485 (if do-left-margin
(indent-to (current-left-margin)))
486 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
492 (defun split-line (&optional arg
)
493 "Split current line, moving portion beyond point vertically down.
494 If the current line starts with `fill-prefix', insert it on the new
495 line as well. With prefix ARG, don't insert `fill-prefix' on new line.
497 When called from Lisp code, ARG may be a prefix string to copy."
499 (skip-chars-forward " \t")
500 (let* ((col (current-column))
502 ;; What prefix should we check for (nil means don't).
503 (prefix (cond ((stringp arg
) arg
)
506 ;; Does this line start with it?
507 (have-prfx (and prefix
510 (looking-at (regexp-quote prefix
))))))
512 (if have-prfx
(insert-and-inherit prefix
))
516 (defun delete-indentation (&optional arg
)
517 "Join this line to previous and fix up whitespace at join.
518 If there is a fill prefix, delete it from the beginning of this line.
519 With argument, join this line to following line."
522 (if arg
(forward-line 1))
523 (if (eq (preceding-char) ?
\n)
525 (delete-region (point) (1- (point)))
526 ;; If the second line started with the fill prefix,
527 ;; delete the prefix.
529 (<= (+ (point) (length fill-prefix
)) (point-max))
531 (buffer-substring (point)
532 (+ (point) (length fill-prefix
)))))
533 (delete-region (point) (+ (point) (length fill-prefix
))))
534 (fixup-whitespace))))
536 (defalias 'join-line
#'delete-indentation
) ; easier to find
538 (defun delete-blank-lines ()
539 "On blank line, delete all surrounding blank lines, leaving just one.
540 On isolated blank line, delete that one.
541 On nonblank line, delete any immediately following blank lines."
543 (let (thisblank singleblank
)
546 (setq thisblank
(looking-at "[ \t]*$"))
547 ;; Set singleblank if there is just one blank line here.
550 (not (looking-at "[ \t]*\n[ \t]*$"))
552 (progn (forward-line -
1)
553 (not (looking-at "[ \t]*$")))))))
554 ;; Delete preceding blank lines, and this one too if it's the only one.
558 (if singleblank
(forward-line 1))
559 (delete-region (point)
560 (if (re-search-backward "[^ \t\n]" nil t
)
561 (progn (forward-line 1) (point))
563 ;; Delete following blank lines, unless the current line is blank
564 ;; and there are no following blank lines.
565 (if (not (and thisblank singleblank
))
569 (delete-region (point)
570 (if (re-search-forward "[^ \t\n]" nil t
)
571 (progn (beginning-of-line) (point))
573 ;; Handle the special case where point is followed by newline and eob.
574 ;; Delete the line, leaving point at eob.
575 (if (looking-at "^[ \t]*\n\\'")
576 (delete-region (point) (point-max)))))
578 (defun delete-trailing-whitespace ()
579 "Delete all the trailing whitespace across the current buffer.
580 All whitespace after the last non-whitespace character in a line is deleted.
581 This respects narrowing, created by \\[narrow-to-region] and friends.
582 A formfeed is not considered whitespace by this function."
586 (goto-char (point-min))
587 (while (re-search-forward "\\s-$" nil t
)
588 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
589 ;; Don't delete formfeeds, even if they are considered whitespace.
591 (if (looking-at ".*\f")
592 (goto-char (match-end 0))))
593 (delete-region (point) (match-end 0))))))
595 (defun newline-and-indent ()
596 "Insert a newline, then indent according to major mode.
597 Indentation is done using the value of `indent-line-function'.
598 In programming language modes, this is the same as TAB.
599 In some text modes, where TAB inserts a tab, this command indents to the
600 column specified by the function `current-left-margin'."
602 (delete-horizontal-space t
)
604 (indent-according-to-mode))
606 (defun reindent-then-newline-and-indent ()
607 "Reindent current line, insert newline, then indent the new line.
608 Indentation of both lines is done according to the current major mode,
609 which means calling the current value of `indent-line-function'.
610 In programming language modes, this is the same as TAB.
611 In some text modes, where TAB inserts a tab, this indents to the
612 column specified by the function `current-left-margin'."
615 ;; Be careful to insert the newline before indenting the line.
616 ;; Otherwise, the indentation might be wrong.
620 (indent-according-to-mode)
621 (delete-horizontal-space t
))
622 (indent-according-to-mode)))
624 (defun quoted-insert (arg)
625 "Read next input character and insert it.
626 This is useful for inserting control characters.
628 If the first character you type after this command is an octal digit,
629 you should type a sequence of octal digits which specify a character code.
630 Any nondigit terminates the sequence. If the terminator is a RET,
631 it is discarded; any other terminator is used itself as input.
632 The variable `read-quoted-char-radix' specifies the radix for this feature;
633 set it to 10 or 16 to use decimal or hex instead of octal.
635 In overwrite mode, this function inserts the character anyway, and
636 does not handle octal digits specially. This means that if you use
637 overwrite as your normal editing mode, you can use this function to
638 insert characters when necessary.
640 In binary overwrite mode, this function does overwrite, and octal
641 digits are interpreted as a character code. This is intended to be
642 useful for editing binary files."
644 (let* ((char (let (translation-table-for-input input-method-function
)
645 (if (or (not overwrite-mode
)
646 (eq overwrite-mode
'overwrite-mode-binary
))
649 ;; Assume character codes 0240 - 0377 stand for characters in some
650 ;; single-byte character set, and convert them to Emacs
652 (if (and enable-multibyte-characters
655 (setq char
(unibyte-char-to-multibyte char
)))
657 (if (eq overwrite-mode
'overwrite-mode-binary
)
660 (insert-and-inherit char
)
661 (setq arg
(1- arg
)))))
663 (defun forward-to-indentation (&optional arg
)
664 "Move forward ARG lines and position at first nonblank character."
666 (forward-line (or arg
1))
667 (skip-chars-forward " \t"))
669 (defun backward-to-indentation (&optional arg
)
670 "Move backward ARG lines and position at first nonblank character."
672 (forward-line (- (or arg
1)))
673 (skip-chars-forward " \t"))
675 (defun back-to-indentation ()
676 "Move point to the first non-whitespace character on this line."
678 (beginning-of-line 1)
679 (skip-syntax-forward " " (line-end-position))
680 ;; Move back over chars that have whitespace syntax but have the p flag.
681 (backward-prefix-chars))
683 (defun fixup-whitespace ()
684 "Fixup white space between objects around point.
685 Leave one space or none, according to the context."
688 (delete-horizontal-space)
689 (if (or (looking-at "^\\|\\s)")
690 (save-excursion (forward-char -
1)
691 (looking-at "$\\|\\s(\\|\\s'")))
695 (defun delete-horizontal-space (&optional backward-only
)
696 "Delete all spaces and tabs around point.
697 If BACKWARD-ONLY is non-nil, only delete spaces before point."
699 (let ((orig-pos (point)))
704 (skip-chars-forward " \t")
705 (constrain-to-field nil orig-pos t
)))
707 (skip-chars-backward " \t")
708 (constrain-to-field nil orig-pos
)))))
710 (defun just-one-space (&optional n
)
711 "Delete all spaces and tabs around point, leaving one space (or N spaces)."
713 (let ((orig-pos (point)))
714 (skip-chars-backward " \t")
715 (constrain-to-field nil orig-pos
)
716 (dotimes (i (or n
1))
717 (if (= (following-char) ?\s
)
723 (skip-chars-forward " \t")
724 (constrain-to-field nil orig-pos t
)))))
726 (defun beginning-of-buffer (&optional arg
)
727 "Move point to the beginning of the buffer; leave mark at previous position.
728 With \\[universal-argument] prefix, do not set mark at previous position.
729 With numeric arg N, put point N/10 of the way from the beginning.
731 If the buffer is narrowed, this command uses the beginning and size
732 of the accessible part of the buffer.
734 Don't use this command in Lisp programs!
735 \(goto-char (point-min)) is faster and avoids clobbering the mark."
738 (and transient-mark-mode mark-active
)
740 (let ((size (- (point-max) (point-min))))
741 (goto-char (if (and arg
(not (consp arg
)))
744 ;; Avoid overflow for large buffer sizes!
745 (* (prefix-numeric-value arg
)
747 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
749 (if arg
(forward-line 1)))
751 (defun end-of-buffer (&optional arg
)
752 "Move point to the end of the buffer; leave mark at previous position.
753 With \\[universal-argument] prefix, do not set mark at previous position.
754 With numeric arg N, put point N/10 of the way from the end.
756 If the buffer is narrowed, this command uses the beginning and size
757 of the accessible part of the buffer.
759 Don't use this command in Lisp programs!
760 \(goto-char (point-max)) is faster and avoids clobbering the mark."
763 (and transient-mark-mode mark-active
)
765 (let ((size (- (point-max) (point-min))))
766 (goto-char (if (and arg
(not (consp arg
)))
769 ;; Avoid overflow for large buffer sizes!
770 (* (prefix-numeric-value arg
)
772 (/ (* size
(prefix-numeric-value arg
)) 10)))
774 ;; If we went to a place in the middle of the buffer,
775 ;; adjust it to the beginning of a line.
776 (cond (arg (forward-line 1))
777 ((> (point) (window-end nil t
))
778 ;; If the end of the buffer is not already on the screen,
779 ;; then scroll specially to put it near, but not at, the bottom.
780 (overlay-recenter (point))
783 (defun mark-whole-buffer ()
784 "Put point at beginning and mark at end of buffer.
785 You probably should not use this function in Lisp programs;
786 it is usually a mistake for a Lisp function to use any subroutine
787 that uses or sets the mark."
790 (push-mark (point-max) nil t
)
791 (goto-char (point-min)))
794 ;; Counting lines, one way or another.
796 (defun goto-line (arg &optional buffer
)
797 "Goto line ARG, counting from line 1 at beginning of buffer.
798 Normally, move point in the current buffer.
799 With just \\[universal-argument] as argument, move point in the most recently
800 displayed other buffer, and switch to it. When called from Lisp code,
801 the optional argument BUFFER specifies a buffer to switch to.
803 If there's a number in the buffer at point, it is the default for ARG."
805 (if (and current-prefix-arg
(not (consp current-prefix-arg
)))
806 (list (prefix-numeric-value current-prefix-arg
))
807 ;; Look for a default, a number in the buffer at point.
810 (skip-chars-backward "0-9")
811 (if (looking-at "[0-9]")
812 (buffer-substring-no-properties
814 (progn (skip-chars-forward "0-9")
816 ;; Decide if we're switching buffers.
818 (if (consp current-prefix-arg
)
819 (other-buffer (current-buffer) t
)))
822 (concat " in " (buffer-name buffer
))
824 ;; Read the argument, offering that number (if any) as default.
825 (list (read-from-minibuffer (format (if default
"Goto line%s (%s): "
833 ;; Switch to the desired buffer, one way or another.
835 (let ((window (get-buffer-window buffer
)))
836 (if window
(select-window window
)
837 (switch-to-buffer-other-window buffer
))))
838 ;; Move to the specified line number in that buffer.
842 (if (eq selective-display t
)
843 (re-search-forward "[\n\C-m]" nil
'end
(1- arg
))
844 (forward-line (1- arg
)))))
846 (defun count-lines-region (start end
)
847 "Print number of lines and characters in the region."
849 (message "Region has %d lines, %d characters"
850 (count-lines start end
) (- end start
)))
853 "Print the current buffer line number and narrowed line number of point."
855 (let ((start (point-min))
856 (n (line-number-at-pos)))
858 (message "Line %d" n
)
862 (message "line %d (narrowed line %d)"
863 (+ n
(line-number-at-pos start
) -
1) n
))))))
865 (defun count-lines (start end
)
866 "Return number of lines between START and END.
867 This is usually the number of newlines between them,
868 but can be one more if START is not equal to END
869 and the greater of them is not at the start of a line."
872 (narrow-to-region start end
)
873 (goto-char (point-min))
874 (if (eq selective-display t
)
877 (while (re-search-forward "[\n\C-m]" nil t
40)
878 (setq done
(+ 40 done
)))
879 (while (re-search-forward "[\n\C-m]" nil t
1)
880 (setq done
(+ 1 done
)))
881 (goto-char (point-max))
882 (if (and (/= start end
)
886 (- (buffer-size) (forward-line (buffer-size)))))))
888 (defun line-number-at-pos (&optional pos
)
889 "Return (narrowed) buffer line number at position POS.
890 If POS is nil, use current buffer location.
891 Counting starts at (point-min), so the value refers
892 to the contents of the accessible portion of the buffer."
893 (let ((opoint (or pos
(point))) start
)
895 (goto-char (point-min))
899 (1+ (count-lines start
(point))))))
901 (defun what-cursor-position (&optional detail
)
902 "Print info on cursor position (on screen and within buffer).
903 Also describe the character after point, and give its character code
904 in octal, decimal and hex.
906 For a non-ASCII multibyte character, also give its encoding in the
907 buffer's selected coding system if the coding system encodes the
908 character safely. If the character is encoded into one byte, that
909 code is shown in hex. If the character is encoded into more than one
910 byte, just \"...\" is shown.
912 In addition, with prefix argument, show details about that character
913 in *Help* buffer. See also the command `describe-char'."
915 (let* ((char (following-char))
919 (total (buffer-size))
920 (percent (if (> total
50000)
921 ;; Avoid overflow from multiplying by 100!
922 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
923 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
924 (hscroll (if (= (window-hscroll) 0)
926 (format " Hscroll=%d" (window-hscroll))))
927 (col (current-column)))
929 (if (or (/= beg
1) (/= end
(1+ total
)))
930 (message "point=%d of %d (%d%%) <%d-%d> column=%d%s"
931 pos total percent beg end col hscroll
)
932 (message "point=%d of %d (EOB) column=%d%s"
933 pos total col hscroll
))
934 (let ((coding buffer-file-coding-system
)
935 encoded encoding-msg display-prop under-display
)
937 (eq (coding-system-type coding
) t
))
938 (setq coding default-buffer-file-coding-system
))
939 (if (not (char-valid-p char
))
941 (format "(%d, #o%o, #x%x, invalid)" char char char
))
942 ;; Check if the character is displayed with some `display'
943 ;; text property. In that case, set under-display to the
944 ;; buffer substring covered by that property.
945 (setq display-prop
(get-text-property pos
'display
))
947 (let ((to (or (next-single-property-change pos
'display
)
950 (setq under-display
"")
951 (setq under-display
"..."
954 (concat (buffer-substring-no-properties pos to
)
956 (setq encoded
(and (>= char
128) (encode-coding-char char coding
))))
959 (if (not (stringp display-prop
))
960 (format "(%d, #o%o, #x%x, part of display \"%s\")"
961 char char char under-display
)
962 (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")"
963 char char char under-display display-prop
))
965 (format "(%d, #o%o, #x%x, file %s)"
967 (if (> (length encoded
) 1)
969 (encoded-string-description encoded coding
)))
970 (format "(%d, #o%o, #x%x)" char char char
)))))
972 ;; We show the detailed information about CHAR.
973 (describe-char (point)))
974 (if (or (/= beg
1) (/= end
(1+ total
)))
975 (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
977 (single-key-description char
)
978 (buffer-substring-no-properties (point) (1+ (point))))
979 encoding-msg pos total percent beg end col hscroll
)
980 (message "Char: %s %s point=%d of %d (%d%%) column=%d%s"
981 (if enable-multibyte-characters
983 (single-key-description char
)
984 (buffer-substring-no-properties (point) (1+ (point))))
985 (single-key-description char
))
986 encoding-msg pos total percent col hscroll
))))))
988 ;; Initialize read-expression-map. It is defined at C level.
989 (let ((m (make-sparse-keymap)))
990 (define-key m
"\M-\t" 'lisp-complete-symbol
)
991 (set-keymap-parent m minibuffer-local-map
)
992 (setq read-expression-map m
))
994 (defvar read-expression-history nil
)
996 (defcustom eval-expression-print-level
4
997 "Value for `print-level' while printing value in `eval-expression'.
998 A value of nil means no limit."
1000 :type
'(choice (const :tag
"No Limit" nil
) integer
)
1003 (defcustom eval-expression-print-length
12
1004 "Value for `print-length' while printing value in `eval-expression'.
1005 A value of nil means no limit."
1007 :type
'(choice (const :tag
"No Limit" nil
) integer
)
1010 (defcustom eval-expression-debug-on-error t
1011 "If non-nil set `debug-on-error' to t in `eval-expression'.
1012 If nil, don't change the value of `debug-on-error'."
1017 (defun eval-expression-print-format (value)
1018 "Format VALUE as a result of evaluated expression.
1019 Return a formatted string which is displayed in the echo area
1020 in addition to the value printed by prin1 in functions which
1021 display the result of expression evaluation."
1022 (if (and (integerp value
)
1023 (or (not (memq this-command
'(eval-last-sexp eval-print-last-sexp
)))
1024 (eq this-command last-command
)
1025 (if (boundp 'edebug-active
) edebug-active
)))
1027 (if (or (if (boundp 'edebug-active
) edebug-active
)
1028 (memq this-command
'(eval-last-sexp eval-print-last-sexp
)))
1029 (prin1-char value
))))
1031 (format " (#o%o, #x%x, %s)" value value char-string
)
1032 (format " (#o%o, #x%x)" value value
)))))
1034 ;; We define this, rather than making `eval' interactive,
1035 ;; for the sake of completion of names like eval-region, eval-buffer.
1036 (defun eval-expression (eval-expression-arg
1037 &optional eval-expression-insert-value
)
1038 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
1039 Value is also consed on to front of the variable `values'.
1040 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
1041 insert the result into the current buffer instead of printing it in
1044 If `eval-expression-debug-on-error' is non-nil, which is the default,
1045 this command arranges for all errors to enter the debugger."
1047 (list (read-from-minibuffer "Eval: "
1048 nil read-expression-map t
1049 'read-expression-history
)
1050 current-prefix-arg
))
1052 (if (null eval-expression-debug-on-error
)
1053 (setq values
(cons (eval eval-expression-arg
) values
))
1054 (let ((old-value (make-symbol "t")) new-value
)
1055 ;; Bind debug-on-error to something unique so that we can
1056 ;; detect when evaled code changes it.
1057 (let ((debug-on-error old-value
))
1058 (setq values
(cons (eval eval-expression-arg
) values
))
1059 (setq new-value debug-on-error
))
1060 ;; If evaled code has changed the value of debug-on-error,
1061 ;; propagate that change to the global binding.
1062 (unless (eq old-value new-value
)
1063 (setq debug-on-error new-value
))))
1065 (let ((print-length eval-expression-print-length
)
1066 (print-level eval-expression-print-level
))
1067 (if eval-expression-insert-value
1069 (let ((standard-output (current-buffer)))
1070 (eval-last-sexp-print-value (car values
))))
1072 (prin1 (car values
) t
)
1073 (let ((str (eval-expression-print-format (car values
))))
1074 (if str
(princ str t
)))))))
1076 (defun edit-and-eval-command (prompt command
)
1077 "Prompting with PROMPT, let user edit COMMAND and eval result.
1078 COMMAND is a Lisp expression. Let user edit that expression in
1079 the minibuffer, then read and evaluate the result."
1081 (let ((print-level nil
)
1082 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1084 (read-from-minibuffer prompt
1085 (prin1-to-string command
)
1086 read-expression-map t
1088 ;; If command was added to command-history as a string,
1089 ;; get rid of that. We want only evaluable expressions there.
1090 (if (stringp (car command-history
))
1091 (setq command-history
(cdr command-history
)))))))
1093 ;; If command to be redone does not match front of history,
1094 ;; add it to the history.
1095 (or (equal command
(car command-history
))
1096 (setq command-history
(cons command command-history
)))
1099 (defun repeat-complex-command (arg)
1100 "Edit and re-evaluate last complex command, or ARGth from last.
1101 A complex command is one which used the minibuffer.
1102 The command is placed in the minibuffer as a Lisp form for editing.
1103 The result is executed, repeating the command as changed.
1104 If the command has been changed or is not the most recent previous command
1105 it is added to the front of the command history.
1106 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
1107 to get different commands to edit and resubmit."
1109 (let ((elt (nth (1- arg
) command-history
))
1114 (let ((print-level nil
)
1115 (minibuffer-history-position arg
)
1116 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1118 (read-from-minibuffer
1119 "Redo: " (prin1-to-string elt
) read-expression-map t
1120 (cons 'command-history arg
))
1122 ;; If command was added to command-history as a
1123 ;; string, get rid of that. We want only
1124 ;; evaluable expressions there.
1125 (if (stringp (car command-history
))
1126 (setq command-history
(cdr command-history
))))))
1128 ;; If command to be redone does not match front of history,
1129 ;; add it to the history.
1130 (or (equal newcmd
(car command-history
))
1131 (setq command-history
(cons newcmd command-history
)))
1134 (error "Argument %d is beyond length of command history" arg
)
1135 (error "There are no previous complex commands to repeat")))))
1137 (defvar minibuffer-history nil
1138 "Default minibuffer history list.
1139 This is used for all minibuffer input
1140 except when an alternate history list is specified.")
1141 (defvar minibuffer-history-sexp-flag nil
1142 "Control whether history list elements are expressions or strings.
1143 If the value of this variable equals current minibuffer depth,
1144 they are expressions; otherwise they are strings.
1145 \(That convention is designed to do the right thing for
1146 recursive uses of the minibuffer.)")
1147 (setq minibuffer-history-variable
'minibuffer-history
)
1148 (setq minibuffer-history-position nil
) ;; Defvar is in C code.
1149 (defvar minibuffer-history-search-history nil
)
1151 (defvar minibuffer-text-before-history nil
1152 "Text that was in this minibuffer before any history commands.
1153 This is nil if there have not yet been any history commands
1154 in this use of the minibuffer.")
1156 (add-hook 'minibuffer-setup-hook
'minibuffer-history-initialize
)
1158 (defun minibuffer-history-initialize ()
1159 (setq minibuffer-text-before-history nil
))
1161 (defun minibuffer-avoid-prompt (new old
)
1162 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1163 (constrain-to-field nil
(point-max)))
1165 (defcustom minibuffer-history-case-insensitive-variables nil
1166 "*Minibuffer history variables for which matching should ignore case.
1167 If a history variable is a member of this list, then the
1168 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1169 commands ignore case when searching it, regardless of `case-fold-search'."
1170 :type
'(repeat variable
)
1173 (defun previous-matching-history-element (regexp n
)
1174 "Find the previous history element that matches REGEXP.
1175 \(Previous history elements refer to earlier actions.)
1176 With prefix argument N, search for Nth previous match.
1177 If N is negative, find the next or Nth next match.
1178 Normally, history elements are matched case-insensitively if
1179 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1180 makes the search case-sensitive.
1181 See also `minibuffer-history-case-insensitive-variables'."
1183 (let* ((enable-recursive-minibuffers t
)
1184 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1186 minibuffer-local-map
1188 'minibuffer-history-search-history
1189 (car minibuffer-history-search-history
))))
1190 ;; Use the last regexp specified, by default, if input is empty.
1191 (list (if (string= regexp
"")
1192 (if minibuffer-history-search-history
1193 (car minibuffer-history-search-history
)
1194 (error "No previous history search regexp"))
1196 (prefix-numeric-value current-prefix-arg
))))
1198 (if (and (zerop minibuffer-history-position
)
1199 (null minibuffer-text-before-history
))
1200 (setq minibuffer-text-before-history
1201 (minibuffer-contents-no-properties)))
1202 (let ((history (symbol-value minibuffer-history-variable
))
1204 (if (isearch-no-upper-case-p regexp t
) ; assume isearch.el is dumped
1205 ;; On some systems, ignore case for file names.
1206 (if (memq minibuffer-history-variable
1207 minibuffer-history-case-insensitive-variables
)
1209 ;; Respect the user's setting for case-fold-search:
1215 (pos minibuffer-history-position
))
1218 (setq pos
(min (max 1 (+ pos
(if (< n
0) -
1 1))) (length history
)))
1219 (when (= pos prevpos
)
1220 (error (if (= pos
1)
1221 "No later matching history item"
1222 "No earlier matching history item")))
1224 (if (eq minibuffer-history-sexp-flag
(minibuffer-depth))
1225 (let ((print-level nil
))
1226 (prin1-to-string (nth (1- pos
) history
)))
1227 (nth (1- pos
) history
)))
1230 (and (string-match regexp match-string
)
1232 (and (string-match (concat ".*\\(" regexp
"\\)") match-string
)
1233 (match-beginning 1))))
1235 (setq n
(+ n
(if (< n
0) 1 -
1)))))
1236 (setq minibuffer-history-position pos
)
1237 (goto-char (point-max))
1238 (delete-minibuffer-contents)
1239 (insert match-string
)
1240 (goto-char (+ (minibuffer-prompt-end) match-offset
))))
1241 (if (memq (car (car command-history
)) '(previous-matching-history-element
1242 next-matching-history-element
))
1243 (setq command-history
(cdr command-history
))))
1245 (defun next-matching-history-element (regexp n
)
1246 "Find the next history element that matches REGEXP.
1247 \(The next history element refers to a more recent action.)
1248 With prefix argument N, search for Nth next match.
1249 If N is negative, find the previous or Nth previous match.
1250 Normally, history elements are matched case-insensitively if
1251 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1252 makes the search case-sensitive."
1254 (let* ((enable-recursive-minibuffers t
)
1255 (regexp (read-from-minibuffer "Next element matching (regexp): "
1257 minibuffer-local-map
1259 'minibuffer-history-search-history
1260 (car minibuffer-history-search-history
))))
1261 ;; Use the last regexp specified, by default, if input is empty.
1262 (list (if (string= regexp
"")
1263 (if minibuffer-history-search-history
1264 (car minibuffer-history-search-history
)
1265 (error "No previous history search regexp"))
1267 (prefix-numeric-value current-prefix-arg
))))
1268 (previous-matching-history-element regexp
(- n
)))
1270 (defvar minibuffer-temporary-goal-position nil
)
1272 (defun next-history-element (n)
1273 "Puts next element of the minibuffer history in the minibuffer.
1274 With argument N, it uses the Nth following element."
1277 (let ((narg (- minibuffer-history-position n
))
1278 (minimum (if minibuffer-default -
1 0))
1279 elt minibuffer-returned-to-present
)
1280 (if (and (zerop minibuffer-history-position
)
1281 (null minibuffer-text-before-history
))
1282 (setq minibuffer-text-before-history
1283 (minibuffer-contents-no-properties)))
1284 (if (< narg minimum
)
1285 (if minibuffer-default
1286 (error "End of history; no next item")
1287 (error "End of history; no default available")))
1288 (if (> narg
(length (symbol-value minibuffer-history-variable
)))
1289 (error "Beginning of history; no preceding item"))
1290 (unless (memq last-command
'(next-history-element
1291 previous-history-element
))
1292 (let ((prompt-end (minibuffer-prompt-end)))
1293 (set (make-local-variable 'minibuffer-temporary-goal-position
)
1294 (cond ((<= (point) prompt-end
) prompt-end
)
1297 (goto-char (point-max))
1298 (delete-minibuffer-contents)
1299 (setq minibuffer-history-position narg
)
1301 (setq elt minibuffer-default
))
1303 (setq elt
(or minibuffer-text-before-history
""))
1304 (setq minibuffer-returned-to-present t
)
1305 (setq minibuffer-text-before-history nil
))
1306 (t (setq elt
(nth (1- minibuffer-history-position
)
1307 (symbol-value minibuffer-history-variable
)))))
1309 (if (and (eq minibuffer-history-sexp-flag
(minibuffer-depth))
1310 (not minibuffer-returned-to-present
))
1311 (let ((print-level nil
))
1312 (prin1-to-string elt
))
1314 (goto-char (or minibuffer-temporary-goal-position
(point-max))))))
1316 (defun previous-history-element (n)
1317 "Puts previous element of the minibuffer history in the minibuffer.
1318 With argument N, it uses the Nth previous element."
1320 (next-history-element (- n
)))
1322 (defun next-complete-history-element (n)
1323 "Get next history element which completes the minibuffer before the point.
1324 The contents of the minibuffer after the point are deleted, and replaced
1325 by the new completion."
1327 (let ((point-at-start (point)))
1328 (next-matching-history-element
1330 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1332 ;; next-matching-history-element always puts us at (point-min).
1333 ;; Move to the position we were at before changing the buffer contents.
1334 ;; This is still sensical, because the text before point has not changed.
1335 (goto-char point-at-start
)))
1337 (defun previous-complete-history-element (n)
1339 Get previous history element which completes the minibuffer before the point.
1340 The contents of the minibuffer after the point are deleted, and replaced
1341 by the new completion."
1343 (next-complete-history-element (- n
)))
1345 ;; For compatibility with the old subr of the same name.
1346 (defun minibuffer-prompt-width ()
1347 "Return the display width of the minibuffer prompt.
1348 Return 0 if current buffer is not a minibuffer."
1349 ;; Return the width of everything before the field at the end of
1350 ;; the buffer; this should be 0 for normal buffers.
1351 (1- (minibuffer-prompt-end)))
1353 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1354 (defalias 'advertised-undo
'undo
)
1356 (defconst undo-equiv-table
(make-hash-table :test
'eq
:weakness t
)
1357 "Table mapping redo records to the corresponding undo one.
1358 A redo record for undo-in-region maps to t.
1359 A redo record for ordinary undo maps to the following (earlier) undo.")
1361 (defvar undo-in-region nil
1362 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1364 (defvar undo-no-redo nil
1365 "If t, `undo' doesn't go through redo entries.")
1367 (defvar pending-undo-list nil
1368 "Within a run of consecutive undo commands, list remaining to be undone.
1369 If t, we undid all the way to the end of it.")
1371 (defun undo (&optional arg
)
1372 "Undo some previous changes.
1373 Repeat this command to undo more changes.
1374 A numeric argument serves as a repeat count.
1376 In Transient Mark mode when the mark is active, only undo changes within
1377 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1378 as an argument limits undo to changes within the current region."
1380 ;; Make last-command indicate for the next command that this was an undo.
1381 ;; That way, another undo will undo more.
1382 ;; If we get to the end of the undo history and get an error,
1383 ;; another undo command will find the undo history empty
1384 ;; and will get another error. To begin undoing the undos,
1385 ;; you must type some other command.
1386 (let ((modified (buffer-modified-p))
1387 (recent-save (recent-auto-save-p))
1389 ;; If we get an error in undo-start,
1390 ;; the next command should not be a "consecutive undo".
1391 ;; So set `this-command' to something other than `undo'.
1392 (setq this-command
'undo-start
)
1394 (unless (and (eq last-command
'undo
)
1395 (or (eq pending-undo-list t
)
1396 ;; If something (a timer or filter?) changed the buffer
1397 ;; since the previous command, don't continue the undo seq.
1398 (let ((list buffer-undo-list
))
1399 (while (eq (car list
) nil
)
1400 (setq list
(cdr list
)))
1401 ;; If the last undo record made was made by undo
1402 ;; it shows nothing else happened in between.
1403 (gethash list undo-equiv-table
))))
1404 (setq undo-in-region
1405 (if transient-mark-mode mark-active
(and arg
(not (numberp arg
)))))
1407 (undo-start (region-beginning) (region-end))
1409 ;; get rid of initial undo boundary
1411 ;; If we got this far, the next command should be a consecutive undo.
1412 (setq this-command
'undo
)
1413 ;; Check to see whether we're hitting a redo record, and if
1414 ;; so, ask the user whether she wants to skip the redo/undo pair.
1415 (let ((equiv (gethash pending-undo-list undo-equiv-table
)))
1416 (or (eq (selected-window) (minibuffer-window))
1417 (setq message
(if undo-in-region
1418 (if equiv
"Redo in region!" "Undo in region!")
1419 (if equiv
"Redo!" "Undo!"))))
1420 (when (and (consp equiv
) undo-no-redo
)
1421 ;; The equiv entry might point to another redo record if we have done
1422 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1423 (while (let ((next (gethash equiv undo-equiv-table
)))
1424 (if next
(setq equiv next
))))
1425 (setq pending-undo-list equiv
)))
1427 (if (or transient-mark-mode
(numberp arg
))
1428 (prefix-numeric-value arg
)
1430 ;; Record the fact that the just-generated undo records come from an
1431 ;; undo operation--that is, they are redo records.
1432 ;; In the ordinary case (not within a region), map the redo
1433 ;; record to the following undos.
1434 ;; I don't know how to do that in the undo-in-region case.
1435 (puthash buffer-undo-list
1436 (if undo-in-region t pending-undo-list
)
1438 ;; Don't specify a position in the undo record for the undo command.
1439 ;; Instead, undoing this should move point to where the change is.
1440 (let ((tail buffer-undo-list
)
1443 (when (integerp (car tail
))
1444 (let ((pos (car tail
)))
1446 (setcdr prev
(cdr tail
))
1447 (setq buffer-undo-list
(cdr tail
)))
1448 (setq tail
(cdr tail
))
1450 (if (eq pos
(car tail
))
1452 (setcdr prev
(cdr tail
))
1453 (setq buffer-undo-list
(cdr tail
)))
1455 (setq tail
(cdr tail
)))
1457 (setq prev tail tail
(cdr tail
))))
1458 ;; Record what the current undo list says,
1459 ;; so the next command can tell if the buffer was modified in between.
1460 (and modified
(not (buffer-modified-p))
1461 (delete-auto-save-file-if-necessary recent-save
))
1462 ;; Display a message announcing success.
1464 (message message
))))
1466 (defun buffer-disable-undo (&optional buffer
)
1467 "Make BUFFER stop keeping undo information.
1468 No argument or nil as argument means do this for the current buffer."
1470 (with-current-buffer (if buffer
(get-buffer buffer
) (current-buffer))
1471 (setq buffer-undo-list t
)))
1473 (defun undo-only (&optional arg
)
1474 "Undo some previous changes.
1475 Repeat this command to undo more changes.
1476 A numeric argument serves as a repeat count.
1477 Contrary to `undo', this will not redo a previous undo."
1479 (let ((undo-no-redo t
)) (undo arg
)))
1481 (defvar undo-in-progress nil
1482 "Non-nil while performing an undo.
1483 Some change-hooks test this variable to do something different.")
1485 (defun undo-more (n)
1486 "Undo back N undo-boundaries beyond what was already undone recently.
1487 Call `undo-start' to get ready to undo recent changes,
1488 then call `undo-more' one or more times to undo them."
1489 (or (listp pending-undo-list
)
1490 (error (concat "No further undo information"
1491 (and undo-in-region
" for region"))))
1492 (let ((undo-in-progress t
))
1493 (setq pending-undo-list
(primitive-undo n pending-undo-list
))
1494 (if (null pending-undo-list
)
1495 (setq pending-undo-list t
))))
1497 ;; Deep copy of a list
1498 (defun undo-copy-list (list)
1499 "Make a copy of undo list LIST."
1500 (mapcar 'undo-copy-list-1 list
))
1502 (defun undo-copy-list-1 (elt)
1504 (cons (car elt
) (undo-copy-list-1 (cdr elt
)))
1507 (defun undo-start (&optional beg end
)
1508 "Set `pending-undo-list' to the front of the undo list.
1509 The next call to `undo-more' will undo the most recently made change.
1510 If BEG and END are specified, then only undo elements
1511 that apply to text between BEG and END are used; other undo elements
1512 are ignored. If BEG and END are nil, all undo elements are used."
1513 (if (eq buffer-undo-list t
)
1514 (error "No undo information in this buffer"))
1515 (setq pending-undo-list
1516 (if (and beg end
(not (= beg end
)))
1517 (undo-make-selective-list (min beg end
) (max beg end
))
1520 (defvar undo-adjusted-markers
)
1522 (defun undo-make-selective-list (start end
)
1523 "Return a list of undo elements for the region START to END.
1524 The elements come from `buffer-undo-list', but we keep only
1525 the elements inside this region, and discard those outside this region.
1526 If we find an element that crosses an edge of this region,
1527 we stop and ignore all further elements."
1528 (let ((undo-list-copy (undo-copy-list buffer-undo-list
))
1529 (undo-list (list nil
))
1530 undo-adjusted-markers
1532 undo-elt undo-elt temp-undo-list delta
)
1533 (while undo-list-copy
1534 (setq undo-elt
(car undo-list-copy
))
1536 (cond ((and (consp undo-elt
) (eq (car undo-elt
) t
))
1537 ;; This is a "was unmodified" element.
1538 ;; Keep it if we have kept everything thus far.
1539 (not some-rejected
))
1541 (undo-elt-in-region undo-elt start end
)))))
1544 (setq end
(+ end
(cdr (undo-delta undo-elt
))))
1545 ;; Don't put two nils together in the list
1546 (if (not (and (eq (car undo-list
) nil
)
1548 (setq undo-list
(cons undo-elt undo-list
))))
1549 (if (undo-elt-crosses-region undo-elt start end
)
1550 (setq undo-list-copy nil
)
1551 (setq some-rejected t
)
1552 (setq temp-undo-list
(cdr undo-list-copy
))
1553 (setq delta
(undo-delta undo-elt
))
1555 (when (/= (cdr delta
) 0)
1556 (let ((position (car delta
))
1557 (offset (cdr delta
)))
1559 ;; Loop down the earlier events adjusting their buffer
1560 ;; positions to reflect the fact that a change to the buffer
1561 ;; isn't being undone. We only need to process those element
1562 ;; types which undo-elt-in-region will return as being in
1563 ;; the region since only those types can ever get into the
1566 (while temp-undo-list
1567 (setq undo-elt
(car temp-undo-list
))
1568 (cond ((integerp undo-elt
)
1569 (if (>= undo-elt position
)
1570 (setcar temp-undo-list
(- undo-elt offset
))))
1571 ((atom undo-elt
) nil
)
1572 ((stringp (car undo-elt
))
1573 ;; (TEXT . POSITION)
1574 (let ((text-pos (abs (cdr undo-elt
)))
1575 (point-at-end (< (cdr undo-elt
) 0 )))
1576 (if (>= text-pos position
)
1577 (setcdr undo-elt
(* (if point-at-end -
1 1)
1578 (- text-pos offset
))))))
1579 ((integerp (car undo-elt
))
1581 (when (>= (car undo-elt
) position
)
1582 (setcar undo-elt
(- (car undo-elt
) offset
))
1583 (setcdr undo-elt
(- (cdr undo-elt
) offset
))))
1584 ((null (car undo-elt
))
1585 ;; (nil PROPERTY VALUE BEG . END)
1586 (let ((tail (nthcdr 3 undo-elt
)))
1587 (when (>= (car tail
) position
)
1588 (setcar tail
(- (car tail
) offset
))
1589 (setcdr tail
(- (cdr tail
) offset
))))))
1590 (setq temp-undo-list
(cdr temp-undo-list
))))))))
1591 (setq undo-list-copy
(cdr undo-list-copy
)))
1592 (nreverse undo-list
)))
1594 (defun undo-elt-in-region (undo-elt start end
)
1595 "Determine whether UNDO-ELT falls inside the region START ... END.
1596 If it crosses the edge, we return nil."
1597 (cond ((integerp undo-elt
)
1598 (and (>= undo-elt start
)
1604 ((stringp (car undo-elt
))
1605 ;; (TEXT . POSITION)
1606 (and (>= (abs (cdr undo-elt
)) start
)
1607 (< (abs (cdr undo-elt
)) end
)))
1608 ((and (consp undo-elt
) (markerp (car undo-elt
)))
1609 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1610 ;; See if MARKER is inside the region.
1611 (let ((alist-elt (assq (car undo-elt
) undo-adjusted-markers
)))
1613 (setq alist-elt
(cons (car undo-elt
)
1614 (marker-position (car undo-elt
))))
1615 (setq undo-adjusted-markers
1616 (cons alist-elt undo-adjusted-markers
)))
1617 (and (cdr alist-elt
)
1618 (>= (cdr alist-elt
) start
)
1619 (<= (cdr alist-elt
) end
))))
1620 ((null (car undo-elt
))
1621 ;; (nil PROPERTY VALUE BEG . END)
1622 (let ((tail (nthcdr 3 undo-elt
)))
1623 (and (>= (car tail
) start
)
1624 (<= (cdr tail
) end
))))
1625 ((integerp (car undo-elt
))
1627 (and (>= (car undo-elt
) start
)
1628 (<= (cdr undo-elt
) end
)))))
1630 (defun undo-elt-crosses-region (undo-elt start end
)
1631 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1632 This assumes we have already decided that UNDO-ELT
1633 is not *inside* the region START...END."
1634 (cond ((atom undo-elt
) nil
)
1635 ((null (car undo-elt
))
1636 ;; (nil PROPERTY VALUE BEG . END)
1637 (let ((tail (nthcdr 3 undo-elt
)))
1638 (and (< (car tail
) end
)
1639 (> (cdr tail
) start
))))
1640 ((integerp (car undo-elt
))
1642 (and (< (car undo-elt
) end
)
1643 (> (cdr undo-elt
) start
)))))
1645 ;; Return the first affected buffer position and the delta for an undo element
1646 ;; delta is defined as the change in subsequent buffer positions if we *did*
1648 (defun undo-delta (undo-elt)
1649 (if (consp undo-elt
)
1650 (cond ((stringp (car undo-elt
))
1651 ;; (TEXT . POSITION)
1652 (cons (abs (cdr undo-elt
)) (length (car undo-elt
))))
1653 ((integerp (car undo-elt
))
1655 (cons (car undo-elt
) (- (car undo-elt
) (cdr undo-elt
))))
1660 (defcustom undo-ask-before-discard nil
1661 "If non-nil ask about discarding undo info for the current command.
1662 Normally, Emacs discards the undo info for the current command if
1663 it exceeds `undo-outer-limit'. But if you set this option
1664 non-nil, it asks in the echo area whether to discard the info.
1665 If you answer no, there is a slight risk that Emacs might crash, so
1666 only do it if you really want to undo the command.
1668 This option is mainly intended for debugging. You have to be
1669 careful if you use it for other purposes. Garbage collection is
1670 inhibited while the question is asked, meaning that Emacs might
1671 leak memory. So you should make sure that you do not wait
1672 excessively long before answering the question."
1677 (defvar undo-extra-outer-limit nil
1678 "If non-nil, an extra level of size that's ok in an undo item.
1679 We don't ask the user about truncating the undo list until the
1680 current item gets bigger than this amount.
1682 This variable only matters if `undo-ask-before-discard' is non-nil.")
1683 (make-variable-buffer-local 'undo-extra-outer-limit
)
1685 ;; When the first undo batch in an undo list is longer than
1686 ;; undo-outer-limit, this function gets called to warn the user that
1687 ;; the undo info for the current command was discarded. Garbage
1688 ;; collection is inhibited around the call, so it had better not do a
1690 (setq undo-outer-limit-function
'undo-outer-limit-truncate
)
1691 (defun undo-outer-limit-truncate (size)
1692 (if undo-ask-before-discard
1693 (when (or (null undo-extra-outer-limit
)
1694 (> size undo-extra-outer-limit
))
1695 ;; Don't ask the question again unless it gets even bigger.
1696 ;; This applies, in particular, if the user quits from the question.
1697 ;; Such a quit quits out of GC, but something else will call GC
1698 ;; again momentarily. It will call this function again,
1699 ;; but we don't want to ask the question again.
1700 (setq undo-extra-outer-limit
(+ size
50000))
1701 (if (let (use-dialog-box track-mouse executing-kbd-macro
)
1702 (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? "
1703 (buffer-name) size
)))
1704 (progn (setq buffer-undo-list nil
)
1705 (setq undo-extra-outer-limit nil
)
1708 (display-warning '(undo discard-info
)
1710 (format "Buffer `%s' undo info was %d bytes long.\n"
1712 "The undo info was discarded because it exceeded \
1715 This is normal if you executed a command that made a huge change
1716 to the buffer. In that case, to prevent similar problems in the
1717 future, set `undo-outer-limit' to a value that is large enough to
1718 cover the maximum size of normal changes you expect a single
1719 command to make, but not so large that it might exceed the
1720 maximum memory allotted to Emacs.
1722 If you did not execute any such command, the situation is
1723 probably due to a bug and you should report it.
1725 You can disable the popping up of this buffer by adding the entry
1726 \(undo discard-info) to the user option `warning-suppress-types'.\n")
1728 (setq buffer-undo-list nil
)
1731 (defvar shell-command-history nil
1732 "History list for some commands that read shell commands.")
1734 (defvar shell-command-switch
"-c"
1735 "Switch used to have the shell execute its command line argument.")
1737 (defvar shell-command-default-error-buffer nil
1738 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1739 This buffer is used when `shell-command' or `shell-command-on-region'
1740 is run interactively. A value of nil means that output to stderr and
1741 stdout will be intermixed in the output stream.")
1743 (defun shell-command (command &optional output-buffer error-buffer
)
1744 "Execute string COMMAND in inferior shell; display output, if any.
1745 With prefix argument, insert the COMMAND's output at point.
1747 If COMMAND ends in ampersand, execute it asynchronously.
1748 The output appears in the buffer `*Async Shell Command*'.
1749 That buffer is in shell mode.
1751 Otherwise, COMMAND is executed synchronously. The output appears in
1752 the buffer `*Shell Command Output*'. If the output is short enough to
1753 display in the echo area (which is determined by the variables
1754 `resize-mini-windows' and `max-mini-window-height'), it is shown
1755 there, but it is nonetheless available in buffer `*Shell Command
1756 Output*' even though that buffer is not automatically displayed.
1758 To specify a coding system for converting non-ASCII characters
1759 in the shell command output, use \\[universal-coding-system-argument]
1760 before this command.
1762 Noninteractive callers can specify coding systems by binding
1763 `coding-system-for-read' and `coding-system-for-write'.
1765 The optional second argument OUTPUT-BUFFER, if non-nil,
1766 says to put the output in some other buffer.
1767 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1768 If OUTPUT-BUFFER is not a buffer and not nil,
1769 insert output in current buffer. (This cannot be done asynchronously.)
1770 In either case, the output is inserted after point (leaving mark after it).
1772 If the command terminates without error, but generates output,
1773 and you did not specify \"insert it in the current buffer\",
1774 the output can be displayed in the echo area or in its buffer.
1775 If the output is short enough to display in the echo area
1776 \(determined by the variable `max-mini-window-height' if
1777 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1778 the buffer containing the output is displayed.
1780 If there is output and an error, and you did not specify \"insert it
1781 in the current buffer\", a message about the error goes at the end
1784 If there is no output, or if output is inserted in the current buffer,
1785 then `*Shell Command Output*' is deleted.
1787 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1788 or buffer name to which to direct the command's standard error output.
1789 If it is nil, error output is mingled with regular output.
1790 In an interactive call, the variable `shell-command-default-error-buffer'
1791 specifies the value of ERROR-BUFFER."
1793 (interactive (list (read-from-minibuffer "Shell command: "
1794 nil nil nil
'shell-command-history
)
1796 shell-command-default-error-buffer
))
1797 ;; Look for a handler in case default-directory is a remote file name.
1799 (find-file-name-handler (directory-file-name default-directory
)
1802 (funcall handler
'shell-command command output-buffer error-buffer
)
1803 (if (and output-buffer
1804 (not (or (bufferp output-buffer
) (stringp output-buffer
))))
1805 ;; Output goes in current buffer.
1809 (expand-file-name "scor"
1810 (or small-temporary-file-directory
1811 temporary-file-directory
)))
1813 (barf-if-buffer-read-only)
1815 ;; We do not use -f for csh; we will not support broken use of
1816 ;; .cshrcs. Even the BSD csh manual says to use
1817 ;; "if ($?prompt) exit" before things which are not useful
1818 ;; non-interactively. Besides, if someone wants their other
1819 ;; aliases for shell commands then they can still have them.
1820 (call-process shell-file-name nil
1824 nil shell-command-switch command
)
1825 (when (and error-file
(file-exists-p error-file
))
1826 (if (< 0 (nth 7 (file-attributes error-file
)))
1827 (with-current-buffer (get-buffer-create error-buffer
)
1828 (let ((pos-from-end (- (point-max) (point))))
1831 ;; Do no formatting while reading error file,
1832 ;; because that can run a shell command, and we
1833 ;; don't want that to cause an infinite recursion.
1834 (format-insert-file error-file nil
)
1835 ;; Put point after the inserted errors.
1836 (goto-char (- (point-max) pos-from-end
)))
1837 (display-buffer (current-buffer))))
1838 (delete-file error-file
))
1839 ;; This is like exchange-point-and-mark, but doesn't
1840 ;; activate the mark. It is cleaner to avoid activation,
1841 ;; even though the command loop would deactivate the mark
1842 ;; because we inserted text.
1843 (goto-char (prog1 (mark t
)
1844 (set-marker (mark-marker) (point)
1845 (current-buffer)))))
1846 ;; Output goes in a separate buffer.
1847 ;; Preserve the match data in case called from a program.
1849 (if (string-match "[ \t]*&[ \t]*\\'" command
)
1850 ;; Command ending with ampersand means asynchronous.
1851 (let ((buffer (get-buffer-create
1852 (or output-buffer
"*Async Shell Command*")))
1853 (directory default-directory
)
1855 ;; Remove the ampersand.
1856 (setq command
(substring command
0 (match-beginning 0)))
1857 ;; If will kill a process, query first.
1858 (setq proc
(get-buffer-process buffer
))
1860 (if (yes-or-no-p "A command is running. Kill it? ")
1862 (error "Shell command in progress")))
1863 (with-current-buffer buffer
1864 (setq buffer-read-only nil
)
1866 (display-buffer buffer
)
1867 (setq default-directory directory
)
1868 (setq proc
(start-process "Shell" buffer shell-file-name
1869 shell-command-switch command
))
1870 (setq mode-line-process
'(":%s"))
1871 (require 'shell
) (shell-mode)
1872 (set-process-sentinel proc
'shell-command-sentinel
)
1874 (shell-command-on-region (point) (point) command
1875 output-buffer nil error-buffer
)))))))
1877 (defun display-message-or-buffer (message
1878 &optional buffer-name not-this-window frame
)
1879 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1880 MESSAGE may be either a string or a buffer.
1882 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1883 the maximum height of the echo area, as defined by `max-mini-window-height'
1884 if `resize-mini-windows' is non-nil.
1886 Returns either the string shown in the echo area, or when a pop-up
1887 buffer is used, the window used to display it.
1889 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1890 name of the buffer used to display it in the case where a pop-up buffer
1891 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1892 string and it is displayed in the echo area, it is not specified whether
1893 the contents are inserted into the buffer anyway.
1895 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1896 and only used if a buffer is displayed."
1897 (cond ((and (stringp message
)
1898 (not (string-match "\n" message
))
1899 (<= (length message
) (frame-width)))
1900 ;; Trivial case where we can use the echo area
1901 (message "%s" message
))
1902 ((and (stringp message
)
1903 (= (string-match "\n" message
) (1- (length message
)))
1904 (<= (1- (length message
)) (frame-width)))
1905 ;; Trivial case where we can just remove single trailing newline
1906 (message "%s" (substring message
0 (1- (length message
)))))
1909 (with-current-buffer
1910 (if (bufferp message
)
1912 (get-buffer-create (or buffer-name
"*Message*")))
1914 (unless (bufferp message
)
1919 (if (= (buffer-size) 0)
1921 (count-screen-lines nil nil nil
(minibuffer-window)))))
1923 ((and (or (<= lines
1)
1925 (if resize-mini-windows
1926 (cond ((floatp max-mini-window-height
)
1928 max-mini-window-height
))
1929 ((integerp max-mini-window-height
)
1930 max-mini-window-height
)
1934 ;; Don't use the echo area if the output buffer is
1935 ;; already dispayed in the selected frame.
1936 (not (get-buffer-window (current-buffer))))
1938 (goto-char (point-max))
1941 (message "%s" (buffer-substring (point-min) (point))))
1944 (goto-char (point-min))
1945 (display-buffer (current-buffer)
1946 not-this-window frame
))))))))
1949 ;; We have a sentinel to prevent insertion of a termination message
1950 ;; in the buffer itself.
1951 (defun shell-command-sentinel (process signal
)
1952 (if (memq (process-status process
) '(exit signal
))
1954 (car (cdr (cdr (process-command process
))))
1955 (substring signal
0 -
1))))
1957 (defun shell-command-on-region (start end command
1958 &optional output-buffer replace
1959 error-buffer display-error-buffer
)
1960 "Execute string COMMAND in inferior shell with region as input.
1961 Normally display output (if any) in temp buffer `*Shell Command Output*';
1962 Prefix arg means replace the region with it. Return the exit code of
1965 To specify a coding system for converting non-ASCII characters
1966 in the input and output to the shell command, use \\[universal-coding-system-argument]
1967 before this command. By default, the input (from the current buffer)
1968 is encoded in the same coding system that will be used to save the file,
1969 `buffer-file-coding-system'. If the output is going to replace the region,
1970 then it is decoded from that same coding system.
1972 The noninteractive arguments are START, END, COMMAND,
1973 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
1974 Noninteractive callers can specify coding systems by binding
1975 `coding-system-for-read' and `coding-system-for-write'.
1977 If the command generates output, the output may be displayed
1978 in the echo area or in a buffer.
1979 If the output is short enough to display in the echo area
1980 \(determined by the variable `max-mini-window-height' if
1981 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1982 it is displayed in the buffer `*Shell Command Output*'. The output
1983 is available in that buffer in both cases.
1985 If there is output and an error, a message about the error
1986 appears at the end of the output.
1988 If there is no output, or if output is inserted in the current buffer,
1989 then `*Shell Command Output*' is deleted.
1991 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1992 that says to put the output in some other buffer.
1993 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1994 If OUTPUT-BUFFER is not a buffer and not nil,
1995 insert output in the current buffer.
1996 In either case, the output is inserted after point (leaving mark after it).
1998 If REPLACE, the optional fifth argument, is non-nil, that means insert
1999 the output in place of text from START to END, putting point and mark
2002 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
2003 or buffer name to which to direct the command's standard error output.
2004 If it is nil, error output is mingled with regular output.
2005 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
2006 were any errors. (This is always t, interactively.)
2007 In an interactive call, the variable `shell-command-default-error-buffer'
2008 specifies the value of ERROR-BUFFER."
2009 (interactive (let (string)
2011 (error "The mark is not set now, so there is no region"))
2012 ;; Do this before calling region-beginning
2013 ;; and region-end, in case subprocess output
2014 ;; relocates them while we are in the minibuffer.
2015 (setq string
(read-from-minibuffer "Shell command on region: "
2017 'shell-command-history
))
2018 ;; call-interactively recognizes region-beginning and
2019 ;; region-end specially, leaving them in the history.
2020 (list (region-beginning) (region-end)
2024 shell-command-default-error-buffer
2029 (expand-file-name "scor"
2030 (or small-temporary-file-directory
2031 temporary-file-directory
)))
2036 (not (or (bufferp output-buffer
) (stringp output-buffer
)))))
2037 ;; Replace specified region with output from command.
2038 (let ((swap (and replace
(< start end
))))
2039 ;; Don't muck with mark unless REPLACE says we should.
2041 (and replace
(push-mark (point) 'nomsg
))
2043 (call-process-region start end shell-file-name t
2047 nil shell-command-switch command
))
2048 ;; It is rude to delete a buffer which the command is not using.
2049 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
2050 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
2051 ;; (kill-buffer shell-buffer)))
2052 ;; Don't muck with mark unless REPLACE says we should.
2053 (and replace swap
(exchange-point-and-mark)))
2054 ;; No prefix argument: put the output in a temp buffer,
2055 ;; replacing its entire contents.
2056 (let ((buffer (get-buffer-create
2057 (or output-buffer
"*Shell Command Output*"))))
2059 (if (eq buffer
(current-buffer))
2060 ;; If the input is the same buffer as the output,
2061 ;; delete everything but the specified region,
2062 ;; then replace that region with the output.
2063 (progn (setq buffer-read-only nil
)
2064 (delete-region (max start end
) (point-max))
2065 (delete-region (point-min) (min start end
))
2067 (call-process-region (point-min) (point-max)
2072 nil shell-command-switch
2074 ;; Clear the output buffer, then run the command with
2076 (let ((directory default-directory
))
2079 (setq buffer-read-only nil
)
2080 (if (not output-buffer
)
2081 (setq default-directory directory
))
2084 (call-process-region start end shell-file-name nil
2086 (list buffer error-file
)
2088 nil shell-command-switch command
)))
2089 ;; Report the output.
2090 (with-current-buffer buffer
2091 (setq mode-line-process
2092 (cond ((null exit-status
)
2094 ((stringp exit-status
)
2095 (format " - Signal [%s]" exit-status
))
2096 ((not (equal 0 exit-status
))
2097 (format " - Exit [%d]" exit-status
)))))
2098 (if (with-current-buffer buffer
(> (point-max) (point-min)))
2099 ;; There's some output, display it
2100 (display-message-or-buffer buffer
)
2101 ;; No output; error?
2104 (< 0 (nth 7 (file-attributes error-file
))))
2107 (cond ((null exit-status
)
2108 (message "(Shell command failed with error)"))
2109 ((equal 0 exit-status
)
2110 (message "(Shell command succeeded with %s)"
2112 ((stringp exit-status
)
2113 (message "(Shell command killed by signal %s)"
2116 (message "(Shell command failed with code %d and %s)"
2117 exit-status output
))))
2118 ;; Don't kill: there might be useful info in the undo-log.
2119 ;; (kill-buffer buffer)
2122 (when (and error-file
(file-exists-p error-file
))
2123 (if (< 0 (nth 7 (file-attributes error-file
)))
2124 (with-current-buffer (get-buffer-create error-buffer
)
2125 (let ((pos-from-end (- (point-max) (point))))
2128 ;; Do no formatting while reading error file,
2129 ;; because that can run a shell command, and we
2130 ;; don't want that to cause an infinite recursion.
2131 (format-insert-file error-file nil
)
2132 ;; Put point after the inserted errors.
2133 (goto-char (- (point-max) pos-from-end
)))
2134 (and display-error-buffer
2135 (display-buffer (current-buffer)))))
2136 (delete-file error-file
))
2139 (defun shell-command-to-string (command)
2140 "Execute shell command COMMAND and return its output as a string."
2141 (with-output-to-string
2142 (with-current-buffer
2144 (call-process shell-file-name nil t nil shell-command-switch command
))))
2146 (defun process-file (program &optional infile buffer display
&rest args
)
2147 "Process files synchronously in a separate process.
2148 Similar to `call-process', but may invoke a file handler based on
2149 `default-directory'. The current working directory of the
2150 subprocess is `default-directory'.
2152 File names in INFILE and BUFFER are handled normally, but file
2153 names in ARGS should be relative to `default-directory', as they
2154 are passed to the process verbatim. \(This is a difference to
2155 `call-process' which does not support file handlers for INFILE
2158 Some file handlers might not support all variants, for example
2159 they might behave as if DISPLAY was nil, regardless of the actual
2161 (let ((fh (find-file-name-handler default-directory
'process-file
))
2164 (if fh
(apply fh
'process-file program infile buffer display args
)
2165 (when infile
(setq lc
(file-local-copy infile
)))
2166 (setq stderr-file
(when (and (consp buffer
) (stringp (cadr buffer
)))
2167 (make-temp-file "emacs")))
2169 (apply 'call-process program
2171 (if stderr-file
(list (car buffer
) stderr-file
) buffer
)
2173 (when stderr-file
(copy-file stderr-file
(cadr buffer
)))))
2174 (when stderr-file
(delete-file stderr-file
))
2175 (when lc
(delete-file lc
)))))
2179 (defvar universal-argument-map
2180 (let ((map (make-sparse-keymap)))
2181 (define-key map
[t] 'universal-argument-other-key)
2182 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
2183 (define-key map [switch-frame] nil)
2184 (define-key map [?\C-u] 'universal-argument-more)
2185 (define-key map [?-] 'universal-argument-minus)
2186 (define-key map [?0] 'digit-argument)
2187 (define-key map [?1] 'digit-argument)
2188 (define-key map [?2] 'digit-argument)
2189 (define-key map [?3] 'digit-argument)
2190 (define-key map [?4] 'digit-argument)
2191 (define-key map [?5] 'digit-argument)
2192 (define-key map [?6] 'digit-argument)
2193 (define-key map [?7] 'digit-argument)
2194 (define-key map [?8] 'digit-argument)
2195 (define-key map [?9] 'digit-argument)
2196 (define-key map [kp-0] 'digit-argument)
2197 (define-key map [kp-1] 'digit-argument)
2198 (define-key map [kp-2] 'digit-argument)
2199 (define-key map [kp-3] 'digit-argument)
2200 (define-key map [kp-4] 'digit-argument)
2201 (define-key map [kp-5] 'digit-argument)
2202 (define-key map [kp-6] 'digit-argument)
2203 (define-key map [kp-7] 'digit-argument)
2204 (define-key map [kp-8] 'digit-argument)
2205 (define-key map [kp-9] 'digit-argument)
2206 (define-key map [kp-subtract] 'universal-argument-minus)
2208 "Keymap used while processing \\[universal-argument].")
2210 (defvar universal-argument-num-events nil
2211 "Number of argument-specifying events read by `universal-argument'.
2212 `universal-argument-other-key' uses this to discard those events
2213 from (this-command-keys), and reread only the final command.")
2215 (defvar overriding-map-is-bound nil
2216 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
2218 (defvar saved-overriding-map nil
2219 "The saved value of `overriding-terminal-local-map'.
2220 That variable gets restored to this value on exiting \"universal
2223 (defun ensure-overriding-map-is-bound ()
2224 "Check `overriding-terminal-local-map' is `universal-argument-map'."
2225 (unless overriding-map-is-bound
2226 (setq saved-overriding-map overriding-terminal-local-map)
2227 (setq overriding-terminal-local-map universal-argument-map)
2228 (setq overriding-map-is-bound t)))
2230 (defun restore-overriding-map ()
2231 "Restore `overriding-terminal-local-map' to its saved value."
2232 (setq overriding-terminal-local-map saved-overriding-map)
2233 (setq overriding-map-is-bound nil))
2235 (defun universal-argument ()
2236 "Begin a numeric argument for the following command.
2237 Digits or minus sign following \\[universal-argument] make up the numeric argument.
2238 \\[universal-argument] following the digits or minus sign ends the argument.
2239 \\[universal-argument] without digits or minus sign provides 4 as argument.
2240 Repeating \\[universal-argument] without digits or minus sign
2241 multiplies the argument by 4 each time.
2242 For some commands, just \\[universal-argument] by itself serves as a flag
2243 which is different in effect from any particular numeric argument.
2244 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
2246 (setq prefix-arg (list 4))
2247 (setq universal-argument-num-events (length (this-command-keys)))
2248 (ensure-overriding-map-is-bound))
2250 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
2251 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
2252 (defun universal-argument-more (arg)
2255 (setq prefix-arg (list (* 4 (car arg))))
2257 (setq prefix-arg (list -4))
2258 (setq prefix-arg arg)
2259 (restore-overriding-map)))
2260 (setq universal-argument-num-events (length (this-command-keys))))
2262 (defun negative-argument (arg)
2263 "Begin a negative numeric argument for the next command.
2264 \\[universal-argument] following digits or minus sign ends the argument."
2266 (cond ((integerp arg)
2267 (setq prefix-arg (- arg)))
2269 (setq prefix-arg nil))
2271 (setq prefix-arg '-)))
2272 (setq universal-argument-num-events (length (this-command-keys)))
2273 (ensure-overriding-map-is-bound))
2275 (defun digit-argument (arg)
2276 "Part of the numeric argument for the next command.
2277 \\[universal-argument] following digits or minus sign ends the argument."
2279 (let* ((char (if (integerp last-command-char)
2281 (get last-command-char 'ascii-character)))
2282 (digit (- (logand char ?\177) ?0)))
2283 (cond ((integerp arg)
2284 (setq prefix-arg (+ (* arg 10)
2285 (if (< arg 0) (- digit) digit))))
2287 ;; Treat -0 as just -, so that -01 will work.
2288 (setq prefix-arg (if (zerop digit) '- (- digit))))
2290 (setq prefix-arg digit))))
2291 (setq universal-argument-num-events (length (this-command-keys)))
2292 (ensure-overriding-map-is-bound))
2294 ;; For backward compatibility, minus with no modifiers is an ordinary
2295 ;; command if digits have already been entered.
2296 (defun universal-argument-minus (arg)
2299 (universal-argument-other-key arg)
2300 (negative-argument arg)))
2302 ;; Anything else terminates the argument and is left in the queue to be
2303 ;; executed as a command.
2304 (defun universal-argument-other-key (arg)
2306 (setq prefix-arg arg)
2307 (let* ((key (this-command-keys))
2308 (keylist (listify-key-sequence key)))
2309 (setq unread-command-events
2310 (append (nthcdr universal-argument-num-events keylist)
2311 unread-command-events)))
2312 (reset-this-command-lengths)
2313 (restore-overriding-map))
2315 (defvar buffer-substring-filters nil
2316 "List of filter functions for `filter-buffer-substring'.
2317 Each function must accept a single argument, a string, and return
2318 a string. The buffer substring is passed to the first function
2319 in the list, and the return value of each function is passed to
2320 the next. The return value of the last function is used as the
2321 return value of `filter-buffer-substring'.
2323 If this variable is nil, no filtering is performed.")
2325 (defun filter-buffer-substring (beg end &optional delete noprops)
2326 "Return the buffer substring between BEG and END, after filtering.
2327 The buffer substring is passed through each of the filter
2328 functions in `buffer-substring-filters', and the value from the
2329 last filter function is returned. If `buffer-substring-filters'
2330 is nil, the buffer substring is returned unaltered.
2332 If DELETE is non-nil, the text between BEG and END is deleted
2335 If NOPROPS is non-nil, final string returned does not include
2336 text properties, while the string passed to the filters still
2337 includes text properties from the buffer text.
2339 Point is temporarily set to BEG before calling
2340 `buffer-substring-filters', in case the functions need to know
2341 where the text came from.
2343 This function should be used instead of `buffer-substring',
2344 `buffer-substring-no-properties', or `delete-and-extract-region'
2345 when you want to allow filtering to take place. For example,
2346 major or minor modes can use `buffer-substring-filters' to
2347 extract characters that are special to a buffer, and should not
2348 be copied into other buffers."
2350 ((or delete buffer-substring-filters)
2353 (let ((string (if delete (delete-and-extract-region beg end)
2354 (buffer-substring beg end))))
2355 (dolist (filter buffer-substring-filters)
2356 (setq string (funcall filter string)))
2358 (set-text-properties 0 (length string) nil string))
2361 (buffer-substring-no-properties beg end))
2363 (buffer-substring beg end))))
2366 ;;;; Window system cut and paste hooks.
2368 (defvar interprogram-cut-function nil
2369 "Function to call to make a killed region available to other programs.
2371 Most window systems provide some sort of facility for cutting and
2372 pasting text between the windows of different programs.
2373 This variable holds a function that Emacs calls whenever text
2374 is put in the kill ring, to make the new kill available to other
2377 The function takes one or two arguments.
2378 The first argument, TEXT, is a string containing
2379 the text which should be made available.
2380 The second, optional, argument PUSH, has the same meaning as the
2381 similar argument to `x-set-cut-buffer', which see.")
2383 (make-variable-frame-local 'interprogram-cut-function)
2385 (defvar interprogram-paste-function nil
2386 "Function to call to get text cut from other programs.
2388 Most window systems provide some sort of facility for cutting and
2389 pasting text between the windows of different programs.
2390 This variable holds a function that Emacs calls to obtain
2391 text that other programs have provided for pasting.
2393 The function should be called with no arguments. If the function
2394 returns nil, then no other program has provided such text, and the top
2395 of the Emacs kill ring should be used. If the function returns a
2396 string, then the caller of the function \(usually `current-kill')
2397 should put this string in the kill ring as the latest kill.
2399 Note that the function should return a string only if a program other
2400 than Emacs has provided a string for pasting; if Emacs provided the
2401 most recent string, the function should return nil. If it is
2402 difficult to tell whether Emacs or some other program provided the
2403 current string, it is probably good enough to return nil if the string
2404 is equal (according to `string=') to the last text Emacs provided.")
2406 (make-variable-frame-local 'interprogram-paste-function)
2410 ;;;; The kill ring data structure.
2412 (defvar kill-ring nil
2413 "List of killed text sequences.
2414 Since the kill ring is supposed to interact nicely with cut-and-paste
2415 facilities offered by window systems, use of this variable should
2416 interact nicely with `interprogram-cut-function' and
2417 `interprogram-paste-function'. The functions `kill-new',
2418 `kill-append', and `current-kill' are supposed to implement this
2419 interaction; you may want to use them instead of manipulating the kill
2422 (defcustom kill-ring-max 60
2423 "*Maximum length of kill ring before oldest elements are thrown away."
2427 (defvar kill-ring-yank-pointer nil
2428 "The tail of the kill ring whose car is the last thing yanked.")
2430 (defun kill-new (string &optional replace yank-handler)
2431 "Make STRING the latest kill in the kill ring.
2432 Set `kill-ring-yank-pointer' to point to it.
2433 If `interprogram-cut-function' is non-nil, apply it to STRING.
2434 Optional second argument REPLACE non-nil means that STRING will replace
2435 the front of the kill ring, rather than being added to the list.
2437 Optional third arguments YANK-HANDLER controls how the STRING is later
2438 inserted into a buffer; see `insert-for-yank' for details.
2439 When a yank handler is specified, STRING must be non-empty (the yank
2440 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2442 When the yank handler has a non-nil PARAM element, the original STRING
2443 argument is not used by `insert-for-yank'. However, since Lisp code
2444 may access and use elements from the kill ring directly, the STRING
2445 argument should still be a \"useful\" string for such uses."
2446 (if (> (length string) 0)
2448 (put-text-property 0 (length string)
2449 'yank-handler yank-handler string))
2451 (signal 'args-out-of-range
2452 (list string "yank-handler specified for empty string"))))
2453 (if (fboundp 'menu-bar-update-yank-menu)
2454 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2455 (if (and replace kill-ring)
2456 (setcar kill-ring string)
2457 (push string kill-ring)
2458 (if (> (length kill-ring) kill-ring-max)
2459 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2460 (setq kill-ring-yank-pointer kill-ring)
2461 (if interprogram-cut-function
2462 (funcall interprogram-cut-function string (not replace))))
2464 (defun kill-append (string before-p &optional yank-handler)
2465 "Append STRING to the end of the latest kill in the kill ring.
2466 If BEFORE-P is non-nil, prepend STRING to the kill.
2467 Optional third argument YANK-HANDLER, if non-nil, specifies the
2468 yank-handler text property to be set on the combined kill ring
2469 string. If the specified yank-handler arg differs from the
2470 yank-handler property of the latest kill string, this function
2471 adds the combined string to the kill ring as a new element,
2472 instead of replacing the last kill with it.
2473 If `interprogram-cut-function' is set, pass the resulting kill to it."
2474 (let* ((cur (car kill-ring)))
2475 (kill-new (if before-p (concat string cur) (concat cur string))
2476 (or (= (length cur) 0)
2477 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2480 (defun current-kill (n &optional do-not-move)
2481 "Rotate the yanking point by N places, and then return that kill.
2482 If N is zero, `interprogram-paste-function' is set, and calling it
2483 returns a string, then that string is added to the front of the
2484 kill ring and returned as the latest kill.
2485 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
2486 yanking point; just return the Nth kill forward."
2487 (let ((interprogram-paste (and (= n 0)
2488 interprogram-paste-function
2489 (funcall interprogram-paste-function))))
2490 (if interprogram-paste
2492 ;; Disable the interprogram cut function when we add the new
2493 ;; text to the kill ring, so Emacs doesn't try to own the
2494 ;; selection, with identical text.
2495 (let ((interprogram-cut-function nil))
2496 (kill-new interprogram-paste))
2498 (or kill-ring (error "Kill ring is empty"))
2499 (let ((ARGth-kill-element
2500 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2504 (setq kill-ring-yank-pointer ARGth-kill-element))
2505 (car ARGth-kill-element)))))
2509 ;;;; Commands for manipulating the kill ring.
2511 (defcustom kill-read-only-ok nil
2512 "*Non-nil means don't signal an error for killing read-only text."
2516 (put 'text-read-only 'error-conditions
2517 '(text-read-only buffer-read-only error))
2518 (put 'text-read-only 'error-message "Text is read-only")
2520 (defun kill-region (beg end &optional yank-handler)
2521 "Kill (\"cut\") text between point and mark.
2522 This deletes the text from the buffer and saves it in the kill ring.
2523 The command \\[yank] can retrieve it from there.
2524 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2526 If you want to append the killed region to the last killed text,
2527 use \\[append-next-kill] before \\[kill-region].
2529 If the buffer is read-only, Emacs will beep and refrain from deleting
2530 the text, but put the text in the kill ring anyway. This means that
2531 you can use the killing commands to copy text from a read-only buffer.
2533 This is the primitive for programs to kill text (as opposed to deleting it).
2534 Supply two arguments, character positions indicating the stretch of text
2536 Any command that calls this function is a \"kill command\".
2537 If the previous command was also a kill command,
2538 the text killed this time appends to the text killed last time
2539 to make one entry in the kill ring.
2541 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2542 specifies the yank-handler text property to be set on the killed
2543 text. See `insert-for-yank'."
2544 ;; Pass point first, then mark, because the order matters
2545 ;; when calling kill-append.
2546 (interactive (list (point) (mark)))
2547 (unless (and beg end)
2548 (error "The mark is not set now, so there is no region"))
2550 (let ((string (filter-buffer-substring beg end t)))
2551 (when string ;STRING is nil if BEG = END
2552 ;; Add that string to the kill ring, one way or another.
2553 (if (eq last-command 'kill-region)
2554 (kill-append string (< end beg) yank-handler)
2555 (kill-new string nil yank-handler)))
2556 (when (or string (eq last-command 'kill-region))
2557 (setq this-command 'kill-region))
2559 ((buffer-read-only text-read-only)
2560 ;; The code above failed because the buffer, or some of the characters
2561 ;; in the region, are read-only.
2562 ;; We should beep, in case the user just isn't aware of this.
2563 ;; However, there's no harm in putting
2564 ;; the region's text in the kill ring, anyway.
2565 (copy-region-as-kill beg end)
2566 ;; Set this-command now, so it will be set even if we get an error.
2567 (setq this-command 'kill-region)
2568 ;; This should barf, if appropriate, and give us the correct error.
2569 (if kill-read-only-ok
2570 (progn (message "Read only text copied to kill ring") nil)
2571 ;; Signal an error if the buffer is read-only.
2572 (barf-if-buffer-read-only)
2573 ;; If the buffer isn't read-only, the text is.
2574 (signal 'text-read-only (list (current-buffer)))))))
2576 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2577 ;; to get two copies of the text when the user accidentally types M-w and
2578 ;; then corrects it with the intended C-w.
2579 (defun copy-region-as-kill (beg end)
2580 "Save the region as if killed, but don't kill it.
2581 In Transient Mark mode, deactivate the mark.
2582 If `interprogram-cut-function' is non-nil, also save the text for a window
2583 system cut and paste."
2585 (if (eq last-command 'kill-region)
2586 (kill-append (filter-buffer-substring beg end) (< end beg))
2587 (kill-new (filter-buffer-substring beg end)))
2588 (if transient-mark-mode
2589 (setq deactivate-mark t))
2592 (defun kill-ring-save (beg end)
2593 "Save the region as if killed, but don't kill it.
2594 In Transient Mark mode, deactivate the mark.
2595 If `interprogram-cut-function' is non-nil, also save the text for a window
2596 system cut and paste.
2598 If you want to append the killed line to the last killed text,
2599 use \\[append-next-kill] before \\[kill-ring-save].
2601 This command is similar to `copy-region-as-kill', except that it gives
2602 visual feedback indicating the extent of the region being copied."
2604 (copy-region-as-kill beg end)
2605 ;; This use of interactive-p is correct
2606 ;; because the code it controls just gives the user visual feedback.
2608 (let ((other-end (if (= (point) beg) end beg))
2610 ;; Inhibit quitting so we can make a quit here
2611 ;; look like a C-g typed as a command.
2613 (if (pos-visible-in-window-p other-end (selected-window))
2614 (unless (and transient-mark-mode
2615 (face-background 'region))
2616 ;; Swap point and mark.
2617 (set-marker (mark-marker) (point) (current-buffer))
2618 (goto-char other-end)
2619 (sit-for blink-matching-delay)
2621 (set-marker (mark-marker) other-end (current-buffer))
2623 ;; If user quit, deactivate the mark
2624 ;; as C-g would as a command.
2625 (and quit-flag mark-active
2627 (let* ((killed-text (current-kill 0))
2628 (message-len (min (length killed-text) 40)))
2630 ;; Don't say "killed"; that is misleading.
2631 (message "Saved text until \"%s\""
2632 (substring killed-text (- message-len)))
2633 (message "Saved text from \"%s\""
2634 (substring killed-text 0 message-len))))))))
2636 (defun append-next-kill (&optional interactive)
2637 "Cause following command, if it kills, to append to previous kill.
2638 The argument is used for internal purposes; do not supply one."
2640 ;; We don't use (interactive-p), since that breaks kbd macros.
2643 (setq this-command 'kill-region)
2644 (message "If the next command is a kill, it will append"))
2645 (setq last-command 'kill-region)))
2649 ;; This is actually used in subr.el but defcustom does not work there.
2650 (defcustom yank-excluded-properties
2651 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2652 yank-handler follow-link fontified)
2653 "*Text properties to discard when yanking.
2654 The value should be a list of text properties to discard or t,
2655 which means to discard all text properties."
2656 :type '(choice (const :tag "All" t) (repeat symbol))
2660 (defvar yank-window-start nil)
2661 (defvar yank-undo-function nil
2662 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2663 Function is called with two parameters, START and END corresponding to
2664 the value of the mark and point; it is guaranteed that START <= END.
2665 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2667 (defun yank-pop (&optional arg)
2668 "Replace just-yanked stretch of killed text with a different stretch.
2669 This command is allowed only immediately after a `yank' or a `yank-pop'.
2670 At such a time, the region contains a stretch of reinserted
2671 previously-killed text. `yank-pop' deletes that text and inserts in its
2672 place a different stretch of killed text.
2674 With no argument, the previous kill is inserted.
2675 With argument N, insert the Nth previous kill.
2676 If N is negative, this is a more recent kill.
2678 The sequence of kills wraps around, so that after the oldest one
2679 comes the newest one.
2681 When this command inserts killed text into the buffer, it honors
2682 `yank-excluded-properties' and `yank-handler' as described in the
2683 doc string for `insert-for-yank-1', which see."
2685 (if (not (eq last-command 'yank))
2686 (error "Previous command was not a yank"))
2687 (setq this-command 'yank)
2688 (unless arg (setq arg 1))
2689 (let ((inhibit-read-only t)
2690 (before (< (point) (mark t))))
2692 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2693 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2694 (setq yank-undo-function nil)
2695 (set-marker (mark-marker) (point) (current-buffer))
2696 (insert-for-yank (current-kill arg))
2697 ;; Set the window start back where it was in the yank command,
2699 (set-window-start (selected-window) yank-window-start t)
2701 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2702 ;; It is cleaner to avoid activation, even though the command
2703 ;; loop would deactivate the mark because we inserted text.
2704 (goto-char (prog1 (mark t)
2705 (set-marker (mark-marker) (point) (current-buffer))))))
2708 (defun yank (&optional arg)
2709 "Reinsert (\"paste\") the last stretch of killed text.
2710 More precisely, reinsert the stretch of killed text most recently
2711 killed OR yanked. Put point at end, and set mark at beginning.
2712 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2713 With argument N, reinsert the Nth most recently killed stretch of killed
2716 When this command inserts killed text into the buffer, it honors
2717 `yank-excluded-properties' and `yank-handler' as described in the
2718 doc string for `insert-for-yank-1', which see.
2720 See also the command `yank-pop' (\\[yank-pop])."
2722 (setq yank-window-start (window-start))
2723 ;; If we don't get all the way thru, make last-command indicate that
2724 ;; for the following command.
2725 (setq this-command t)
2727 (insert-for-yank (current-kill (cond
2732 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2733 ;; It is cleaner to avoid activation, even though the command
2734 ;; loop would deactivate the mark because we inserted text.
2735 (goto-char (prog1 (mark t)
2736 (set-marker (mark-marker) (point) (current-buffer)))))
2737 ;; If we do get all the way thru, make this-command indicate that.
2738 (if (eq this-command t)
2739 (setq this-command 'yank))
2742 (defun rotate-yank-pointer (arg)
2743 "Rotate the yanking point in the kill ring.
2744 With argument, rotate that many kills forward (or backward, if negative)."
2748 ;; Some kill commands.
2750 ;; Internal subroutine of delete-char
2751 (defun kill-forward-chars (arg)
2752 (if (listp arg) (setq arg (car arg)))
2753 (if (eq arg '-) (setq arg -1))
2754 (kill-region (point) (forward-point arg)))
2756 ;; Internal subroutine of backward-delete-char
2757 (defun kill-backward-chars (arg)
2758 (if (listp arg) (setq arg (car arg)))
2759 (if (eq arg '-) (setq arg -1))
2760 (kill-region (point) (forward-point (- arg))))
2762 (defcustom backward-delete-char-untabify-method 'untabify
2763 "*The method for untabifying when deleting backward.
2764 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2765 `hungry' -- delete all whitespace, both tabs and spaces;
2766 `all' -- delete all whitespace, including tabs, spaces and newlines;
2767 nil -- just delete one character."
2768 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2772 (defun backward-delete-char-untabify (arg &optional killp)
2773 "Delete characters backward, changing tabs into spaces.
2774 The exact behavior depends on `backward-delete-char-untabify-method'.
2775 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2776 Interactively, ARG is the prefix arg (default 1)
2777 and KILLP is t if a prefix arg was specified."
2778 (interactive "*p\nP")
2779 (when (eq backward-delete-char-untabify-method 'untabify)
2782 (while (and (> count 0) (not (bobp)))
2783 (if (= (preceding-char) ?\t)
2784 (let ((col (current-column)))
2786 (setq col (- col (current-column)))
2787 (insert-char ?\s col)
2790 (setq count (1- count))))))
2791 (delete-backward-char
2792 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2793 ((eq backward-delete-char-untabify-method 'all)
2796 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2798 (+ arg (if (zerop wh) 0 (1- wh))))
2802 (defun zap-to-char (arg char)
2803 "Kill up to and including ARG'th occurrence of CHAR.
2804 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2805 Goes backward if ARG is negative; error if CHAR not found."
2806 (interactive "p\ncZap to char: ")
2807 (if (char-table-p translation-table-for-input)
2808 (setq char (or (aref translation-table-for-input char) char)))
2809 (kill-region (point) (progn
2810 (search-forward (char-to-string char) nil nil arg)
2811 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2814 ;; kill-line and its subroutines.
2816 (defcustom kill-whole-line nil
2817 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2821 (defun kill-line (&optional arg)
2822 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2823 With prefix argument, kill that many lines from point.
2824 Negative arguments kill lines backward.
2825 With zero argument, kills the text before point on the current line.
2827 When calling from a program, nil means \"no arg\",
2828 a number counts as a prefix arg.
2830 To kill a whole line, when point is not at the beginning, type \
2831 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2833 If `kill-whole-line' is non-nil, then this command kills the whole line
2834 including its terminating newline, when used at the beginning of a line
2835 with no argument. As a consequence, you can always kill a whole line
2836 by typing \\[beginning-of-line] \\[kill-line].
2838 If you want to append the killed line to the last killed text,
2839 use \\[append-next-kill] before \\[kill-line].
2841 If the buffer is read-only, Emacs will beep and refrain from deleting
2842 the line, but put the line in the kill ring anyway. This means that
2843 you can use this command to copy text from a read-only buffer.
2844 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2847 (kill-region (point)
2848 ;; It is better to move point to the other end of the kill
2849 ;; before killing. That way, in a read-only buffer, point
2850 ;; moves across the text that is copied to the kill ring.
2851 ;; The choice has no effect on undo now that undo records
2852 ;; the value of point from before the command was run.
2855 (forward-visible-line (prefix-numeric-value arg))
2857 (signal 'end-of-buffer nil))
2860 (end-of-visible-line) (point))))
2861 (if (or (save-excursion
2862 ;; If trailing whitespace is visible,
2863 ;; don't treat it as nothing.
2864 (unless show-trailing-whitespace
2865 (skip-chars-forward " \t" end))
2867 (and kill-whole-line (bolp)))
2868 (forward-visible-line 1)
2872 (defun kill-whole-line (&optional arg)
2874 With prefix arg, kill that many lines starting from the current line.
2875 If arg is negative, kill backward. Also kill the preceding newline.
2876 \(This is meant to make \\[repeat] work well with negative arguments.\)
2877 If arg is zero, kill current line but exclude the trailing newline."
2879 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2880 (signal 'end-of-buffer nil))
2881 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2882 (signal 'beginning-of-buffer nil))
2883 (unless (eq last-command 'kill-region)
2885 (setq last-command 'kill-region))
2887 ;; We need to kill in two steps, because the previous command
2888 ;; could have been a kill command, in which case the text
2889 ;; before point needs to be prepended to the current kill
2890 ;; ring entry and the text after point appended. Also, we
2891 ;; need to use save-excursion to avoid copying the same text
2892 ;; twice to the kill ring in read-only buffers.
2894 (kill-region (point) (progn (forward-visible-line 0) (point))))
2895 (kill-region (point) (progn (end-of-visible-line) (point))))
2898 (kill-region (point) (progn (end-of-visible-line) (point))))
2899 (kill-region (point)
2900 (progn (forward-visible-line (1+ arg))
2901 (unless (bobp) (backward-char))
2905 (kill-region (point) (progn (forward-visible-line 0) (point))))
2906 (kill-region (point)
2907 (progn (forward-visible-line arg) (point))))))
2909 (defun forward-visible-line (arg)
2910 "Move forward by ARG lines, ignoring currently invisible newlines only.
2911 If ARG is negative, move backward -ARG lines.
2912 If ARG is zero, move to the beginning of the current line."
2917 (or (zerop (forward-line 1))
2918 (signal 'end-of-buffer nil))
2919 ;; If the newline we just skipped is invisible,
2922 (get-char-property (1- (point)) 'invisible)))
2923 (if (if (eq buffer-invisibility-spec t)
2925 (or (memq prop buffer-invisibility-spec)
2926 (assq prop buffer-invisibility-spec)))
2927 (setq arg (1+ arg))))
2928 (setq arg (1- arg)))
2929 ;; If invisible text follows, and it is a number of complete lines,
2931 (let ((opoint (point)))
2932 (while (and (not (eobp))
2934 (get-char-property (point) 'invisible)))
2935 (if (eq buffer-invisibility-spec t)
2937 (or (memq prop buffer-invisibility-spec)
2938 (assq prop buffer-invisibility-spec)))))
2940 (if (get-text-property (point) 'invisible)
2941 (or (next-single-property-change (point) 'invisible)
2943 (next-overlay-change (point)))))
2945 (goto-char opoint))))
2947 (while (or first (<= arg 0))
2950 (or (zerop (forward-line -1))
2951 (signal 'beginning-of-buffer nil)))
2952 ;; If the newline we just moved to is invisible,
2956 (get-char-property (1- (point)) 'invisible)))
2957 (unless (if (eq buffer-invisibility-spec t)
2959 (or (memq prop buffer-invisibility-spec)
2960 (assq prop buffer-invisibility-spec)))
2961 (setq arg (1+ arg)))))
2963 ;; If invisible text follows, and it is a number of complete lines,
2965 (let ((opoint (point)))
2966 (while (and (not (bobp))
2968 (get-char-property (1- (point)) 'invisible)))
2969 (if (eq buffer-invisibility-spec t)
2971 (or (memq prop buffer-invisibility-spec)
2972 (assq prop buffer-invisibility-spec)))))
2974 (if (get-text-property (1- (point)) 'invisible)
2975 (or (previous-single-property-change (point) 'invisible)
2977 (previous-overlay-change (point)))))
2979 (goto-char opoint)))))
2980 ((beginning-of-buffer end-of-buffer)
2983 (defun end-of-visible-line ()
2984 "Move to end of current visible line."
2986 ;; If the following character is currently invisible,
2987 ;; skip all characters with that same `invisible' property value,
2988 ;; then find the next newline.
2989 (while (and (not (eobp))
2991 (skip-chars-forward "^\n")
2993 (get-char-property (point) 'invisible)))
2994 (if (eq buffer-invisibility-spec t)
2996 (or (memq prop buffer-invisibility-spec)
2997 (assq prop buffer-invisibility-spec))))))
2998 (skip-chars-forward "^\n")
2999 (if (get-text-property (point) 'invisible)
3000 (goto-char (next-single-property-change (point) 'invisible))
3001 (goto-char (next-overlay-change (point))))
3004 (defun insert-buffer (buffer)
3005 "Insert after point the contents of BUFFER.
3006 Puts mark after the inserted text.
3007 BUFFER may be a buffer or a buffer name.
3009 This function is meant for the user to run interactively.
3010 Don't call it from programs: use `insert-buffer-substring' instead!"
3014 (barf-if-buffer-read-only)
3015 (read-buffer "Insert buffer: "
3016 (if (eq (selected-window) (next-window (selected-window)))
3017 (other-buffer (current-buffer))
3018 (window-buffer (next-window (selected-window))))
3022 (insert-buffer-substring (get-buffer buffer))
3026 (defun append-to-buffer (buffer start end)
3027 "Append to specified buffer the text of the region.
3028 It is inserted into that buffer before its point.
3030 When calling from a program, give three arguments:
3031 BUFFER (or buffer name), START and END.
3032 START and END specify the portion of the current buffer to be copied."
3034 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
3035 (region-beginning) (region-end)))
3036 (let ((oldbuf (current-buffer)))
3038 (let* ((append-to (get-buffer-create buffer))
3039 (windows (get-buffer-window-list append-to t t))
3041 (set-buffer append-to)
3042 (setq point (point))
3043 (barf-if-buffer-read-only)
3044 (insert-buffer-substring oldbuf start end)
3045 (dolist (window windows)
3046 (when (= (window-point window) point)
3047 (set-window-point window (point))))))))
3049 (defun prepend-to-buffer (buffer start end)
3050 "Prepend to specified buffer the text of the region.
3051 It is inserted into that buffer after its point.
3053 When calling from a program, give three arguments:
3054 BUFFER (or buffer name), START and END.
3055 START and END specify the portion of the current buffer to be copied."
3056 (interactive "BPrepend to buffer: \nr")
3057 (let ((oldbuf (current-buffer)))
3059 (set-buffer (get-buffer-create buffer))
3060 (barf-if-buffer-read-only)
3062 (insert-buffer-substring oldbuf start end)))))
3064 (defun copy-to-buffer (buffer start end)
3065 "Copy to specified buffer the text of the region.
3066 It is inserted into that buffer, replacing existing text there.
3068 When calling from a program, give three arguments:
3069 BUFFER (or buffer name), START and END.
3070 START and END specify the portion of the current buffer to be copied."
3071 (interactive "BCopy to buffer: \nr")
3072 (let ((oldbuf (current-buffer)))
3073 (with-current-buffer (get-buffer-create buffer)
3074 (barf-if-buffer-read-only)
3077 (insert-buffer-substring oldbuf start end)))))
3079 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3080 (put 'mark-inactive 'error-message "The mark is not active now")
3082 (defvar activate-mark-hook nil
3083 "Hook run when the mark becomes active.
3084 It is also run at the end of a command, if the mark is active and
3085 it is possible that the region may have changed")
3087 (defvar deactivate-mark-hook nil
3088 "Hook run when the mark becomes inactive.")
3090 (defun mark (&optional force)
3091 "Return this buffer's mark value as integer, or nil if never set.
3093 In Transient Mark mode, this function signals an error if
3094 the mark is not active. However, if `mark-even-if-inactive' is non-nil,
3095 or the argument FORCE is non-nil, it disregards whether the mark
3096 is active, and returns an integer or nil in the usual way.
3098 If you are using this in an editing command, you are most likely making
3099 a mistake; see the documentation of `set-mark'."
3100 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3101 (marker-position (mark-marker))
3102 (signal 'mark-inactive nil)))
3104 ;; Many places set mark-active directly, and several of them failed to also
3105 ;; run deactivate-mark-hook. This shorthand should simplify.
3106 (defsubst deactivate-mark ()
3107 "Deactivate the mark by setting `mark-active' to nil.
3108 \(That makes a difference only in Transient Mark mode.)
3109 Also runs the hook `deactivate-mark-hook'."
3111 ((eq transient-mark-mode 'lambda)
3112 (setq transient-mark-mode nil))
3113 (transient-mark-mode
3114 (setq mark-active nil)
3115 (run-hooks 'deactivate-mark-hook))))
3117 (defun set-mark (pos)
3118 "Set this buffer's mark to POS. Don't use this function!
3119 That is to say, don't use this function unless you want
3120 the user to see that the mark has moved, and you want the previous
3121 mark position to be lost.
3123 Normally, when a new mark is set, the old one should go on the stack.
3124 This is why most applications should use `push-mark', not `set-mark'.
3126 Novice Emacs Lisp programmers often try to use the mark for the wrong
3127 purposes. The mark saves a location for the user's convenience.
3128 Most editing commands should not alter the mark.
3129 To remember a location for internal use in the Lisp program,
3130 store it in a Lisp variable. Example:
3132 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3136 (setq mark-active t)
3137 (run-hooks 'activate-mark-hook)
3138 (set-marker (mark-marker) pos (current-buffer)))
3139 ;; Normally we never clear mark-active except in Transient Mark mode.
3140 ;; But when we actually clear out the mark value too,
3141 ;; we must clear mark-active in any mode.
3142 (setq mark-active nil)
3143 (run-hooks 'deactivate-mark-hook)
3144 (set-marker (mark-marker) nil)))
3146 (defvar mark-ring nil
3147 "The list of former marks of the current buffer, most recent first.")
3148 (make-variable-buffer-local 'mark-ring)
3149 (put 'mark-ring 'permanent-local t)
3151 (defcustom mark-ring-max 16
3152 "*Maximum size of mark ring. Start discarding off end if gets this big."
3154 :group 'editing-basics)
3156 (defvar global-mark-ring nil
3157 "The list of saved global marks, most recent first.")
3159 (defcustom global-mark-ring-max 16
3160 "*Maximum size of global mark ring. \
3161 Start discarding off end if gets this big."
3163 :group 'editing-basics)
3165 (defun pop-to-mark-command ()
3166 "Jump to mark, and pop a new position for mark off the ring
3167 \(does not affect global mark ring\)."
3170 (error "No mark set in this buffer")
3171 (goto-char (mark t))
3174 (defun push-mark-command (arg &optional nomsg)
3175 "Set mark at where point is.
3176 If no prefix arg and mark is already set there, just activate it.
3177 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3179 (let ((mark (marker-position (mark-marker))))
3180 (if (or arg (null mark) (/= mark (point)))
3181 (push-mark nil nomsg t)
3182 (setq mark-active t)
3183 (run-hooks 'activate-mark-hook)
3185 (message "Mark activated")))))
3187 (defcustom set-mark-command-repeat-pop nil
3188 "*Non-nil means that repeating \\[set-mark-command] after popping will pop.
3189 This means that if you type C-u \\[set-mark-command] \\[set-mark-command]
3194 (defun set-mark-command (arg)
3195 "Set mark at where point is, or jump to mark.
3196 With no prefix argument, set mark, and push old mark position on local
3197 mark ring; also push mark on global mark ring if last mark was set in
3198 another buffer. Immediately repeating the command activates
3199 `transient-mark-mode' temporarily.
3201 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
3202 jump to mark, and pop a new position
3203 for mark off the local mark ring \(this does not affect the global
3204 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
3205 mark ring \(see `pop-global-mark'\).
3207 If `set-mark-command-repeat-pop' is non-nil, repeating
3208 the \\[set-mark-command] command with no prefix pops the next position
3209 off the local (or global) mark ring and jumps there.
3211 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
3212 \\[universal-argument] \\[set-mark-command], unconditionally
3213 set mark where point is.
3215 Setting the mark also sets the \"region\", which is the closest
3216 equivalent in Emacs to what some editors call the \"selection\".
3218 Novice Emacs Lisp programmers often try to use the mark for the wrong
3219 purposes. See the documentation of `set-mark' for more information."
3221 (if (eq transient-mark-mode 'lambda)
3222 (setq transient-mark-mode nil))
3224 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3225 (push-mark-command nil))
3226 ((not (eq this-command 'set-mark-command))
3228 (pop-to-mark-command)
3229 (push-mark-command t)))
3230 ((and set-mark-command-repeat-pop
3231 (eq last-command 'pop-to-mark-command))
3232 (setq this-command 'pop-to-mark-command)
3233 (pop-to-mark-command))
3234 ((and set-mark-command-repeat-pop
3235 (eq last-command 'pop-global-mark)
3237 (setq this-command 'pop-global-mark)
3240 (setq this-command 'pop-to-mark-command)
3241 (pop-to-mark-command))
3242 ((and (eq last-command 'set-mark-command)
3243 mark-active (null transient-mark-mode))
3244 (setq transient-mark-mode 'lambda)
3245 (message "Transient-mark-mode temporarily enabled"))
3247 (push-mark-command nil))))
3249 (defun push-mark (&optional location nomsg activate)
3250 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3251 If the last global mark pushed was not in the current buffer,
3252 also push LOCATION on the global mark ring.
3253 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3254 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
3256 Novice Emacs Lisp programmers often try to use the mark for the wrong
3257 purposes. See the documentation of `set-mark' for more information.
3259 In Transient Mark mode, this does not activate the mark."
3260 (unless (null (mark t))
3261 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3262 (when (> (length mark-ring) mark-ring-max)
3263 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3264 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3265 (set-marker (mark-marker) (or location (point)) (current-buffer))
3266 ;; Now push the mark on the global mark ring.
3267 (if (and global-mark-ring
3268 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3269 ;; The last global mark pushed was in this same buffer.
3270 ;; Don't push another one.
3272 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3273 (when (> (length global-mark-ring) global-mark-ring-max)
3274 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3275 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3276 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3277 (message "Mark set"))
3278 (if (or activate (not transient-mark-mode))
3279 (set-mark (mark t)))
3283 "Pop off mark ring into the buffer's actual mark.
3284 Does not set point. Does nothing if mark ring is empty."
3286 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3287 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3288 (move-marker (car mark-ring) nil)
3289 (if (null (mark t)) (ding))
3290 (setq mark-ring (cdr mark-ring)))
3293 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3294 (defun exchange-point-and-mark (&optional arg)
3295 "Put the mark where point is now, and point where the mark is now.
3296 This command works even when the mark is not active,
3297 and it reactivates the mark.
3298 With prefix arg, `transient-mark-mode' is enabled temporarily."
3302 (if (null transient-mark-mode)
3303 (setq transient-mark-mode 'lambda))
3306 (let ((omark (mark t)))
3308 (error "No mark set in this buffer"))
3313 (define-minor-mode transient-mark-mode
3314 "Toggle Transient Mark mode.
3315 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3317 In Transient Mark mode, when the mark is active, the region is highlighted.
3318 Changing the buffer \"deactivates\" the mark.
3319 So do certain other operations that set the mark
3320 but whose main purpose is something else--for example,
3321 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3323 You can also deactivate the mark by typing \\[keyboard-quit] or
3324 \\[keyboard-escape-quit].
3326 Many commands change their behavior when Transient Mark mode is in effect
3327 and the mark is active, by acting on the region instead of their usual
3328 default part of the buffer's text. Examples of such commands include
3329 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3330 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3331 Invoke \\[apropos-documentation] and type \"transient\" or
3332 \"mark.*active\" at the prompt, to see the documentation of
3333 commands which are sensitive to the Transient Mark mode."
3334 :global t :group 'editing-basics)
3336 (defvar widen-automatically t
3337 "Non-nil means it is ok for commands to call `widen' when they want to.
3338 Some commands will do this in order to go to positions outside
3339 the current accessible part of the buffer.
3341 If `widen-automatically' is nil, these commands will do something else
3342 as a fallback, and won't change the buffer bounds.")
3344 (defun pop-global-mark ()
3345 "Pop off global mark ring and jump to the top location."
3347 ;; Pop entries which refer to non-existent buffers.
3348 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3349 (setq global-mark-ring (cdr global-mark-ring)))
3350 (or global-mark-ring
3351 (error "No global mark set"))
3352 (let* ((marker (car global-mark-ring))
3353 (buffer (marker-buffer marker))
3354 (position (marker-position marker)))
3355 (setq global-mark-ring (nconc (cdr global-mark-ring)
3356 (list (car global-mark-ring))))
3358 (or (and (>= position (point-min))
3359 (<= position (point-max)))
3360 (if widen-automatically
3362 (error "Global mark position is outside accessible part of buffer")))
3363 (goto-char position)
3364 (switch-to-buffer buffer)))
3366 (defcustom next-line-add-newlines nil
3367 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3370 :group 'editing-basics)
3372 (defun next-line (&optional arg try-vscroll)
3373 "Move cursor vertically down ARG lines.
3374 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3375 If there is no character in the target line exactly under the current column,
3376 the cursor is positioned after the character in that line which spans this
3377 column, or at the end of the line if it is not long enough.
3378 If there is no line in the buffer after this one, behavior depends on the
3379 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3380 to create a line, and moves the cursor to that line. Otherwise it moves the
3381 cursor to the end of the buffer.
3383 The command \\[set-goal-column] can be used to create
3384 a semipermanent goal column for this command.
3385 Then instead of trying to move exactly vertically (or as close as possible),
3386 this command moves to the specified goal column (or as close as possible).
3387 The goal column is stored in the variable `goal-column', which is nil
3388 when there is no goal column.
3390 If you are thinking of using this in a Lisp program, consider
3391 using `forward-line' instead. It is usually easier to use
3392 and more reliable (no dependence on goal column, etc.)."
3393 (interactive "p\np")
3394 (or arg (setq arg 1))
3395 (if (and next-line-add-newlines (= arg 1))
3396 (if (save-excursion (end-of-line) (eobp))
3397 ;; When adding a newline, don't expand an abbrev.
3398 (let ((abbrev-mode nil))
3400 (insert (if use-hard-newlines hard-newline "\n")))
3401 (line-move arg nil nil try-vscroll))
3404 (line-move arg nil nil try-vscroll)
3405 ((beginning-of-buffer end-of-buffer) (ding)))
3406 (line-move arg nil nil try-vscroll)))
3409 (defun previous-line (&optional arg try-vscroll)
3410 "Move cursor vertically up ARG lines.
3411 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3412 If there is no character in the target line exactly over the current column,
3413 the cursor is positioned after the character in that line which spans this
3414 column, or at the end of the line if it is not long enough.
3416 The command \\[set-goal-column] can be used to create
3417 a semipermanent goal column for this command.
3418 Then instead of trying to move exactly vertically (or as close as possible),
3419 this command moves to the specified goal column (or as close as possible).
3420 The goal column is stored in the variable `goal-column', which is nil
3421 when there is no goal column.
3423 If you are thinking of using this in a Lisp program, consider using
3424 `forward-line' with a negative argument instead. It is usually easier
3425 to use and more reliable (no dependence on goal column, etc.)."
3426 (interactive "p\np")
3427 (or arg (setq arg 1))
3430 (line-move (- arg) nil nil try-vscroll)
3431 ((beginning-of-buffer end-of-buffer) (ding)))
3432 (line-move (- arg) nil nil try-vscroll))
3435 (defcustom track-eol nil
3436 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3437 This means moving to the end of each line moved onto.
3438 The beginning of a blank line does not count as the end of a line."
3440 :group 'editing-basics)
3442 (defcustom goal-column nil
3443 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3444 :type '(choice integer
3445 (const :tag "None" nil))
3446 :group 'editing-basics)
3447 (make-variable-buffer-local 'goal-column)
3449 (defvar temporary-goal-column 0
3450 "Current goal column for vertical motion.
3451 It is the column where point was
3452 at the start of current run of vertical motion commands.
3453 When the `track-eol' feature is doing its job, the value is 9999.")
3455 (defcustom line-move-ignore-invisible t
3456 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3457 Outline mode sets this."
3459 :group 'editing-basics)
3461 (defun line-move-invisible-p (pos)
3462 "Return non-nil if the character after POS is currently invisible."
3464 (get-char-property pos 'invisible)))
3465 (if (eq buffer-invisibility-spec t)
3467 (or (memq prop buffer-invisibility-spec)
3468 (assq prop buffer-invisibility-spec)))))
3470 ;; Returns non-nil if partial move was done.
3471 (defun line-move-partial (arg noerror to-end)
3473 ;; Move backward (up).
3474 ;; If already vscrolled, reduce vscroll
3475 (let ((vs (window-vscroll nil t)))
3476 (when (> vs (frame-char-height))
3477 (set-window-vscroll nil (- vs (frame-char-height)) t)))
3479 ;; Move forward (down).
3480 (let* ((lh (window-line-height -1))
3486 (>= rbot (frame-char-height))
3487 (<= ypos (- (frame-char-height))))
3489 (let ((wend (pos-visible-in-window-p t nil t)))
3490 (setq rbot (nth 3 wend)
3491 vpos (nth 5 wend))))
3493 ;; If last line of window is fully visible, move forward.
3494 ((or (null rbot) (= rbot 0))
3496 ;; If cursor is not in the bottom scroll margin, move forward.
3499 (or (nth 1 (window-line-height))
3500 (let ((ppos (posn-at-point)))
3501 (cdr (or (posn-actual-col-row ppos)
3502 (posn-col-row ppos))))))
3503 (min (- (window-text-height) scroll-margin 1) (1- vpos))))
3505 ;; When already vscrolled, we vscroll some more if we can,
3506 ;; or clear vscroll and move forward at end of tall image.
3507 ((> (setq vs (window-vscroll nil t)) 0)
3509 (set-window-vscroll nil (+ vs (min rbot (frame-char-height))) t)))
3510 ;; If cursor just entered the bottom scroll margin, move forward,
3511 ;; but also vscroll one line so redisplay wont recenter.
3513 (= py (min (- (window-text-height) scroll-margin 1)
3515 (set-window-vscroll nil (frame-char-height) t)
3516 (line-move-1 arg noerror to-end)
3518 ;; If there are lines above the last line, scroll-up one line.
3522 ;; Finally, start vscroll.
3524 (set-window-vscroll nil (frame-char-height) t)))))))
3527 ;; This is like line-move-1 except that it also performs
3528 ;; vertical scrolling of tall images if appropriate.
3529 ;; That is not really a clean thing to do, since it mixes
3530 ;; scrolling with cursor motion. But so far we don't have
3531 ;; a cleaner solution to the problem of making C-n do something
3532 ;; useful given a tall image.
3533 (defun line-move (arg &optional noerror to-end try-vscroll)
3534 (unless (and auto-window-vscroll try-vscroll
3535 ;; Only vscroll for single line moves
3537 ;; But don't vscroll in a keyboard macro.
3538 (not defining-kbd-macro)
3539 (not executing-kbd-macro)
3540 (line-move-partial arg noerror to-end))
3541 (set-window-vscroll nil 0 t)
3542 (line-move-1 arg noerror to-end)))
3544 ;; This is the guts of next-line and previous-line.
3545 ;; Arg says how many lines to move.
3546 ;; The value is t if we can move the specified number of lines.
3547 (defun line-move-1 (arg &optional noerror to-end)
3548 ;; Don't run any point-motion hooks, and disregard intangibility,
3549 ;; for intermediate positions.
3550 (let ((inhibit-point-motion-hooks t)
3555 (if (not (memq last-command '(next-line previous-line)))
3556 (setq temporary-goal-column
3557 (if (and track-eol (eolp)
3558 ;; Don't count beg of empty line as end of line
3559 ;; unless we just did explicit end-of-line.
3560 (or (not (bolp)) (eq last-command 'move-end-of-line)))
3564 (if (and (not (integerp selective-display))
3565 (not line-move-ignore-invisible))
3566 ;; Use just newline characters.
3567 ;; Set ARG to 0 if we move as many lines as requested.
3569 (progn (if (> arg 1) (forward-line (1- arg)))
3570 ;; This way of moving forward ARG lines
3571 ;; verifies that we have a newline after the last one.
3572 ;; It doesn't get confused by intangible text.
3574 (if (zerop (forward-line 1))
3576 (and (zerop (forward-line arg))
3580 (signal (if (< arg 0)
3581 'beginning-of-buffer
3584 ;; Move by arg lines, but ignore invisible ones.
3585 (let (done line-end)
3586 (while (and (> arg 0) (not done))
3587 ;; If the following character is currently invisible,
3588 ;; skip all characters with that same `invisible' property value.
3589 (while (and (not (eobp)) (line-move-invisible-p (point)))
3590 (goto-char (next-char-property-change (point))))
3592 ;; We don't use `end-of-line', since we want to escape
3593 ;; from field boundaries ocurring exactly at point.
3594 (let ((inhibit-field-text-motion t))
3595 (setq line-end (line-end-position)))
3596 (goto-char (constrain-to-field line-end (point) t t))
3597 ;; If there's no invisibility here, move over the newline.
3601 (signal 'end-of-buffer nil)
3603 ((and (> arg 1) ;; Use vertical-motion for last move
3604 (not (integerp selective-display))
3605 (not (line-move-invisible-p (point))))
3606 ;; We avoid vertical-motion when possible
3607 ;; because that has to fontify.
3609 ;; Otherwise move a more sophisticated way.
3610 ((zerop (vertical-motion 1))
3612 (signal 'end-of-buffer nil)
3615 (setq arg (1- arg))))
3616 ;; The logic of this is the same as the loop above,
3617 ;; it just goes in the other direction.
3618 (while (and (< arg 0) (not done))
3619 ;; For completely consistency with the forward-motion
3620 ;; case, we should call beginning-of-line here.
3621 ;; However, if point is inside a field and on a
3622 ;; continued line, the call to (vertical-motion -1)
3623 ;; below won't move us back far enough; then we return
3624 ;; to the same column in line-move-finish, and point
3625 ;; gets stuck -- cyd
3630 (signal 'beginning-of-buffer nil)
3632 ((and (< arg -1) ;; Use vertical-motion for last move
3633 (not (integerp selective-display))
3634 (not (line-move-invisible-p (1- (point)))))
3636 ((zerop (vertical-motion -1))
3638 (signal 'beginning-of-buffer nil)
3642 (while (and ;; Don't move over previous invis lines
3643 ;; if our target is the middle of this line.
3644 (or (zerop (or goal-column temporary-goal-column))
3646 (not (bobp)) (line-move-invisible-p (1- (point))))
3647 (goto-char (previous-char-property-change (point))))))))
3648 ;; This is the value the function returns.
3652 ;; If we did not move down as far as desired,
3653 ;; at least go to end of line.
3656 ;; If we did not move up as far as desired,
3657 ;; at least go to beginning of line.
3658 (beginning-of-line))
3660 (line-move-finish (or goal-column temporary-goal-column)
3661 opoint (> orig-arg 0)))))))
3663 (defun line-move-finish (column opoint forward)
3666 ;; Set REPEAT to t to repeat the whole thing.
3671 (line-beg (save-excursion (beginning-of-line) (point)))
3673 ;; Compute the end of the line
3674 ;; ignoring effectively invisible newlines.
3676 ;; Like end-of-line but ignores fields.
3677 (skip-chars-forward "^\n")
3678 (while (and (not (eobp)) (line-move-invisible-p (point)))
3679 (goto-char (next-char-property-change (point)))
3680 (skip-chars-forward "^\n"))
3683 ;; Move to the desired column.
3684 (line-move-to-column column)
3686 ;; Corner case: suppose we start out in a field boundary in
3687 ;; the middle of a continued line. When we get to
3688 ;; line-move-finish, point is at the start of a new *screen*
3689 ;; line but the same text line; then line-move-to-column would
3690 ;; move us backwards. Test using C-n with point on the "x" in
3691 ;; (insert "a" (propertize "x" 'field t) (make-string 89 ?y))
3698 ;; Process intangibility within a line.
3699 ;; With inhibit-point-motion-hooks bound to nil, a call to
3700 ;; goto-char moves point past intangible text.
3702 ;; However, inhibit-point-motion-hooks controls both the
3703 ;; intangibility and the point-entered/point-left hooks. The
3704 ;; following hack avoids calling the point-* hooks
3705 ;; unnecessarily. Note that we move *forward* past intangible
3706 ;; text when the initial and final points are the same.
3708 (let ((inhibit-point-motion-hooks nil))
3711 ;; If intangibility moves us to a different (later) place
3712 ;; in the same line, use that as the destination.
3713 (if (<= (point) line-end)
3715 ;; If that position is "too late",
3716 ;; try the previous allowable position.
3720 ;; If going forward, don't accept the previous
3721 ;; allowable position if it is before the target line.
3722 (< line-beg (point))
3723 ;; If going backward, don't accept the previous
3724 ;; allowable position if it is still after the target line.
3725 (<= (point) line-end))
3727 ;; As a last resort, use the end of the line.
3728 (setq new line-end))))
3730 ;; Now move to the updated destination, processing fields
3731 ;; as well as intangibility.
3733 (let ((inhibit-point-motion-hooks nil))
3735 ;; Ignore field boundaries if the initial and final
3736 ;; positions have the same `field' property, even if the
3737 ;; fields are non-contiguous. This seems to be "nicer"
3738 ;; behavior in many situations.
3739 (if (eq (get-char-property new 'field)
3740 (get-char-property opoint 'field))
3742 (constrain-to-field new opoint t t
3743 'inhibit-line-move-field-capture))))
3745 ;; If all this moved us to a different line,
3746 ;; retry everything within that new line.
3747 (when (or (< (point) line-beg) (> (point) line-end))
3748 ;; Repeat the intangibility and field processing.
3749 (setq repeat t))))))
3751 (defun line-move-to-column (col)
3752 "Try to find column COL, considering invisibility.
3753 This function works only in certain cases,
3754 because what we really need is for `move-to-column'
3755 and `current-column' to be able to ignore invisible text."
3758 (move-to-column col))
3760 (when (and line-move-ignore-invisible
3761 (not (bolp)) (line-move-invisible-p (1- (point))))
3762 (let ((normal-location (point))
3763 (normal-column (current-column)))
3764 ;; If the following character is currently invisible,
3765 ;; skip all characters with that same `invisible' property value.
3766 (while (and (not (eobp))
3767 (line-move-invisible-p (point)))
3768 (goto-char (next-char-property-change (point))))
3769 ;; Have we advanced to a larger column position?
3770 (if (> (current-column) normal-column)
3771 ;; We have made some progress towards the desired column.
3772 ;; See if we can make any further progress.
3773 (line-move-to-column (+ (current-column) (- col normal-column)))
3774 ;; Otherwise, go to the place we originally found
3775 ;; and move back over invisible text.
3776 ;; that will get us to the same place on the screen
3777 ;; but with a more reasonable buffer position.
3778 (goto-char normal-location)
3779 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3780 (while (and (not (bolp)) (line-move-invisible-p (1- (point))))
3781 (goto-char (previous-char-property-change (point) line-beg))))))))
3783 (defun move-end-of-line (arg)
3784 "Move point to end of current line as displayed.
3785 \(If there's an image in the line, this disregards newlines
3786 which are part of the text that the image rests on.)
3788 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3789 If point reaches the beginning or end of buffer, it stops there.
3790 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
3792 (or arg (setq arg 1))
3797 (let ((goal-column 0))
3798 (and (line-move arg t)
3801 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
3802 (goto-char (previous-char-property-change (point))))
3806 (if (and (> (point) newpos)
3807 (eq (preceding-char) ?\n))
3809 (if (and (> (point) newpos) (not (eobp))
3810 (not (eq (following-char) ?\n)))
3811 ;; If we skipped something intangible
3812 ;; and now we're not really at eol,
3817 (defun move-beginning-of-line (arg)
3818 "Move point to beginning of current line as displayed.
3819 \(If there's an image in the line, this disregards newlines
3820 which are part of the text that the image rests on.)
3822 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3823 If point reaches the beginning or end of buffer, it stops there.
3824 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
3826 (or arg (setq arg 1))
3828 (let ((orig (point))
3829 start first-vis first-vis-field-value)
3831 ;; Move by lines, if ARG is not 1 (the default).
3833 (line-move (1- arg) t))
3835 ;; Move to beginning-of-line, ignoring fields and invisibles.
3836 (skip-chars-backward "^\n")
3837 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
3838 (goto-char (previous-char-property-change (point)))
3839 (skip-chars-backward "^\n"))
3840 (setq start (point))
3842 ;; Now find first visible char in the line
3843 (while (and (not (eobp)) (line-move-invisible-p (point)))
3844 (goto-char (next-char-property-change (point))))
3845 (setq first-vis (point))
3847 ;; See if fields would stop us from reaching FIRST-VIS.
3848 (setq first-vis-field-value
3849 (constrain-to-field first-vis orig (/= arg 1) t nil))
3851 (goto-char (if (/= first-vis-field-value first-vis)
3852 ;; If yes, obey them.
3853 first-vis-field-value
3854 ;; Otherwise, move to START with attention to fields.
3855 ;; (It is possible that fields never matter in this case.)
3856 (constrain-to-field (point) orig
3857 (/= arg 1) t nil)))))
3860 ;;; Many people have said they rarely use this feature, and often type
3861 ;;; it by accident. Maybe it shouldn't even be on a key.
3862 (put 'set-goal-column 'disabled t)
3864 (defun set-goal-column (arg)
3865 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3866 Those commands will move to this position in the line moved to
3867 rather than trying to keep the same horizontal position.
3868 With a non-nil argument, clears out the goal column
3869 so that \\[next-line] and \\[previous-line] resume vertical motion.
3870 The goal column is stored in the variable `goal-column'."
3874 (setq goal-column nil)
3875 (message "No goal column"))
3876 (setq goal-column (current-column))
3877 ;; The older method below can be erroneous if `set-goal-column' is bound
3878 ;; to a sequence containing %
3879 ;;(message (substitute-command-keys
3880 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3884 (format "Goal column %d " goal-column)
3885 (substitute-command-keys
3886 "(use \\[set-goal-column] with an arg to unset it)")))
3892 (defun scroll-other-window-down (lines)
3893 "Scroll the \"other window\" down.
3894 For more details, see the documentation for `scroll-other-window'."
3896 (scroll-other-window
3897 ;; Just invert the argument's meaning.
3898 ;; We can do that without knowing which window it will be.
3899 (if (eq lines '-) nil
3901 (- (prefix-numeric-value lines))))))
3903 (defun beginning-of-buffer-other-window (arg)
3904 "Move point to the beginning of the buffer in the other window.
3905 Leave mark at previous position.
3906 With arg N, put point N/10 of the way from the true beginning."
3908 (let ((orig-window (selected-window))
3909 (window (other-window-for-scrolling)))
3910 ;; We use unwind-protect rather than save-window-excursion
3911 ;; because the latter would preserve the things we want to change.
3914 (select-window window)
3915 ;; Set point and mark in that window's buffer.
3917 (beginning-of-buffer arg))
3918 ;; Set point accordingly.
3920 (select-window orig-window))))
3922 (defun end-of-buffer-other-window (arg)
3923 "Move point to the end of the buffer in the other window.
3924 Leave mark at previous position.
3925 With arg N, put point N/10 of the way from the true end."
3927 ;; See beginning-of-buffer-other-window for comments.
3928 (let ((orig-window (selected-window))
3929 (window (other-window-for-scrolling)))
3932 (select-window window)
3934 (end-of-buffer arg))
3936 (select-window orig-window))))
3938 (defun transpose-chars (arg)
3939 "Interchange characters around point, moving forward one character.
3940 With prefix arg ARG, effect is to take character before point
3941 and drag it forward past ARG other characters (backward if ARG negative).
3942 If no argument and at end of line, the previous two chars are exchanged."
3944 (and (null arg) (eolp) (forward-char -1))
3945 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3947 (defun transpose-words (arg)
3948 "Interchange words around point, leaving point at end of them.
3949 With prefix arg ARG, effect is to take word before or around point
3950 and drag it forward past ARG other words (backward if ARG negative).
3951 If ARG is zero, the words around or after point and around or after mark
3953 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3955 (transpose-subr 'forward-word arg))
3957 (defun transpose-sexps (arg)
3958 "Like \\[transpose-words] but applies to sexps.
3959 Does not work on a sexp that point is in the middle of
3960 if it is a list or string."
3964 ;; Here we should try to simulate the behavior of
3965 ;; (cons (progn (forward-sexp x) (point))
3966 ;; (progn (forward-sexp (- x)) (point)))
3967 ;; Except that we don't want to rely on the second forward-sexp
3968 ;; putting us back to where we want to be, since forward-sexp-function
3969 ;; might do funny things like infix-precedence.
3971 (looking-at "\\sw\\|\\s_")
3973 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3974 ;; Jumping over a symbol. We might be inside it, mind you.
3975 (progn (funcall (if (> arg 0)
3976 'skip-syntax-backward 'skip-syntax-forward)
3978 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3979 ;; Otherwise, we're between sexps. Take a step back before jumping
3980 ;; to make sure we'll obey the same precedence no matter which direction
3982 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3983 (cons (save-excursion (forward-sexp arg) (point))
3984 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3985 (not (zerop (funcall (if (> arg 0)
3986 'skip-syntax-forward
3987 'skip-syntax-backward)
3992 (defun transpose-lines (arg)
3993 "Exchange current line and previous line, leaving point after both.
3994 With argument ARG, takes previous line and moves it past ARG lines.
3995 With argument 0, interchanges line point is in with line mark is in."
3997 (transpose-subr (function
4001 ;; Move forward over ARG lines,
4002 ;; but create newlines if necessary.
4003 (setq arg (forward-line arg))
4004 (if (/= (preceding-char) ?\n)
4005 (setq arg (1+ arg)))
4008 (forward-line arg))))
4011 (defun transpose-subr (mover arg &optional special)
4012 (let ((aux (if special mover
4014 (cons (progn (funcall mover x) (point))
4015 (progn (funcall mover (- x)) (point))))))
4020 (setq pos1 (funcall aux 1))
4022 (setq pos2 (funcall aux 1))
4023 (transpose-subr-1 pos1 pos2))
4024 (exchange-point-and-mark))
4026 (setq pos1 (funcall aux -1))
4027 (setq pos2 (funcall aux arg))
4028 (transpose-subr-1 pos1 pos2)
4029 (goto-char (car pos2)))
4031 (setq pos1 (funcall aux -1))
4032 (goto-char (car pos1))
4033 (setq pos2 (funcall aux arg))
4034 (transpose-subr-1 pos1 pos2)))))
4036 (defun transpose-subr-1 (pos1 pos2)
4037 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
4038 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
4039 (when (> (car pos1) (car pos2))
4041 (setq pos1 pos2 pos2 swap)))
4042 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
4043 (atomic-change-group
4045 ;; FIXME: We first delete the two pieces of text, so markers that
4046 ;; used to point to after the text end up pointing to before it :-(
4047 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
4048 (goto-char (car pos2))
4049 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
4050 (goto-char (car pos1))
4053 (defun backward-word (&optional arg)
4054 "Move backward until encountering the beginning of a word.
4055 With argument, do this that many times."
4057 (forward-word (- (or arg 1))))
4059 (defun mark-word (&optional arg allow-extend)
4060 "Set mark ARG words away from point.
4061 The place mark goes is the same place \\[forward-word] would
4062 move to with the same argument.
4063 Interactively, if this command is repeated
4064 or (in Transient Mark mode) if the mark is active,
4065 it marks the next ARG words after the ones already marked."
4066 (interactive "P\np")
4067 (cond ((and allow-extend
4068 (or (and (eq last-command this-command) (mark t))
4069 (and transient-mark-mode mark-active)))
4070 (setq arg (if arg (prefix-numeric-value arg)
4071 (if (< (mark) (point)) -1 1)))
4080 (forward-word (prefix-numeric-value arg))
4084 (defun kill-word (arg)
4085 "Kill characters forward until encountering the end of a word.
4086 With argument, do this that many times."
4088 (kill-region (point) (progn (forward-word arg) (point))))
4090 (defun backward-kill-word (arg)
4091 "Kill characters backward until encountering the end of a word.
4092 With argument, do this that many times."
4094 (kill-word (- arg)))
4096 (defun current-word (&optional strict really-word)
4097 "Return the symbol or word that point is on (or a nearby one) as a string.
4098 The return value includes no text properties.
4099 If optional arg STRICT is non-nil, return nil unless point is within
4100 or adjacent to a symbol or word. In all cases the value can be nil
4101 if there is no word nearby.
4102 The function, belying its name, normally finds a symbol.
4103 If optional arg REALLY-WORD is non-nil, it finds just a word."
4105 (let* ((oldpoint (point)) (start (point)) (end (point))
4106 (syntaxes (if really-word "w" "w_"))
4107 (not-syntaxes (concat "^" syntaxes)))
4108 (skip-syntax-backward syntaxes) (setq start (point))
4109 (goto-char oldpoint)
4110 (skip-syntax-forward syntaxes) (setq end (point))
4111 (when (and (eq start oldpoint) (eq end oldpoint)
4112 ;; Point is neither within nor adjacent to a word.
4114 ;; Look for preceding word in same line.
4115 (skip-syntax-backward not-syntaxes
4116 (save-excursion (beginning-of-line)
4119 ;; No preceding word in same line.
4120 ;; Look for following word in same line.
4122 (skip-syntax-forward not-syntaxes
4123 (save-excursion (end-of-line)
4125 (setq start (point))
4126 (skip-syntax-forward syntaxes)
4129 (skip-syntax-backward syntaxes)
4130 (setq start (point))))
4131 ;; If we found something nonempty, return it as a string.
4132 (unless (= start end)
4133 (buffer-substring-no-properties start end)))))
4135 (defcustom fill-prefix nil
4136 "*String for filling to insert at front of new line, or nil for none."
4137 :type '(choice (const :tag "None" nil)
4140 (make-variable-buffer-local 'fill-prefix)
4141 ;;;###autoload(put 'fill-prefix 'safe-local-variable 'string-or-null-p)
4143 (defcustom auto-fill-inhibit-regexp nil
4144 "*Regexp to match lines which should not be auto-filled."
4145 :type '(choice (const :tag "None" nil)
4149 (defvar comment-line-break-function 'comment-indent-new-line
4150 "*Mode-specific function which line breaks and continues a comment.
4152 This function is only called during auto-filling of a comment section.
4153 The function should take a single optional argument, which is a flag
4154 indicating whether it should use soft newlines.")
4156 ;; This function is used as the auto-fill-function of a buffer
4157 ;; when Auto-Fill mode is enabled.
4158 ;; It returns t if it really did any work.
4159 ;; (Actually some major modes use a different auto-fill function,
4160 ;; but this one is the default one.)
4161 (defun do-auto-fill ()
4162 (let (fc justify give-up
4163 (fill-prefix fill-prefix))
4164 (if (or (not (setq justify (current-justification)))
4165 (null (setq fc (current-fill-column)))
4166 (and (eq justify 'left)
4167 (<= (current-column) fc))
4168 (and auto-fill-inhibit-regexp
4169 (save-excursion (beginning-of-line)
4170 (looking-at auto-fill-inhibit-regexp))))
4171 nil ;; Auto-filling not required
4172 (if (memq justify '(full center right))
4173 (save-excursion (unjustify-current-line)))
4175 ;; Choose a fill-prefix automatically.
4176 (when (and adaptive-fill-mode
4177 (or (null fill-prefix) (string= fill-prefix "")))
4179 (fill-context-prefix
4180 (save-excursion (backward-paragraph 1) (point))
4181 (save-excursion (forward-paragraph 1) (point)))))
4182 (and prefix (not (equal prefix ""))
4183 ;; Use auto-indentation rather than a guessed empty prefix.
4184 (not (and fill-indent-according-to-mode
4185 (string-match "\\`[ \t]*\\'" prefix)))
4186 (setq fill-prefix prefix))))
4188 (while (and (not give-up) (> (current-column) fc))
4189 ;; Determine where to split the line.
4194 (setq after-prefix (point))
4196 (looking-at (regexp-quote fill-prefix))
4197 (setq after-prefix (match-end 0)))
4198 (move-to-column (1+ fc))
4199 (fill-move-to-break-point after-prefix)
4202 ;; See whether the place we found is any good.
4204 (goto-char fill-point)
4206 ;; There is no use breaking at end of line.
4207 (save-excursion (skip-chars-forward " ") (eolp))
4208 ;; It is futile to split at the end of the prefix
4209 ;; since we would just insert the prefix again.
4210 (and after-prefix (<= (point) after-prefix))
4211 ;; Don't split right after a comment starter
4212 ;; since we would just make another comment starter.
4213 (and comment-start-skip
4214 (let ((limit (point)))
4216 (and (re-search-forward comment-start-skip
4218 (eq (point) limit))))))
4219 ;; No good place to break => stop trying.
4221 ;; Ok, we have a useful place to break the line. Do it.
4222 (let ((prev-column (current-column)))
4223 ;; If point is at the fill-point, do not `save-excursion'.
4224 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
4225 ;; point will end up before it rather than after it.
4227 (skip-chars-backward " \t")
4228 (= (point) fill-point))
4229 (funcall comment-line-break-function t)
4231 (goto-char fill-point)
4232 (funcall comment-line-break-function t)))
4233 ;; Now do justification, if required
4234 (if (not (eq justify 'left))
4237 (justify-current-line justify nil t)))
4238 ;; If making the new line didn't reduce the hpos of
4239 ;; the end of the line, then give up now;
4240 ;; trying again will not help.
4241 (if (>= (current-column) prev-column)
4242 (setq give-up t))))))
4243 ;; Justify last line.
4244 (justify-current-line justify t t)
4247 (defvar normal-auto-fill-function 'do-auto-fill
4248 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
4249 Some major modes set this.")
4251 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
4252 ;; FIXME: turn into a proper minor mode.
4253 ;; Add a global minor mode version of it.
4254 (defun auto-fill-mode (&optional arg)
4255 "Toggle Auto Fill mode.
4256 With arg, turn Auto Fill mode on if and only if arg is positive.
4257 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
4258 automatically breaks the line at a previous space.
4260 The value of `normal-auto-fill-function' specifies the function to use
4261 for `auto-fill-function' when turning Auto Fill mode on."
4263 (prog1 (setq auto-fill-function
4265 (not auto-fill-function)
4266 (> (prefix-numeric-value arg) 0))
4267 normal-auto-fill-function
4269 (force-mode-line-update)))
4271 ;; This holds a document string used to document auto-fill-mode.
4272 (defun auto-fill-function ()
4273 "Automatically break line at a previous space, in insertion of text."
4276 (defun turn-on-auto-fill ()
4277 "Unconditionally turn on Auto Fill mode."
4280 (defun turn-off-auto-fill ()
4281 "Unconditionally turn off Auto Fill mode."
4282 (auto-fill-mode -1))
4284 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
4286 (defun set-fill-column (arg)
4287 "Set `fill-column' to specified argument.
4288 Use \\[universal-argument] followed by a number to specify a column.
4289 Just \\[universal-argument] as argument means to use the current column."
4292 (setq arg (current-column)))
4293 (if (not (integerp arg))
4294 ;; Disallow missing argument; it's probably a typo for C-x C-f.
4295 (error "set-fill-column requires an explicit argument")
4296 (message "Fill column set to %d (was %d)" arg fill-column)
4297 (setq fill-column arg)))
4299 (defun set-selective-display (arg)
4300 "Set `selective-display' to ARG; clear it if no arg.
4301 When the value of `selective-display' is a number > 0,
4302 lines whose indentation is >= that value are not displayed.
4303 The variable `selective-display' has a separate value for each buffer."
4305 (if (eq selective-display t)
4306 (error "selective-display already in use for marked lines"))
4309 (narrow-to-region (point-min) (point))
4310 (goto-char (window-start))
4311 (vertical-motion (window-height)))))
4312 (setq selective-display
4313 (and arg (prefix-numeric-value arg)))
4314 (recenter current-vpos))
4315 (set-window-start (selected-window) (window-start (selected-window)))
4316 (princ "selective-display set to " t)
4317 (prin1 selective-display t)
4320 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
4321 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
4323 (defun toggle-truncate-lines (&optional arg)
4324 "Toggle whether to fold or truncate long lines on the screen.
4325 With arg, truncate long lines iff arg is positive.
4326 Note that in side-by-side windows, truncation is always enabled."
4328 (setq truncate-lines
4330 (not truncate-lines)
4331 (> (prefix-numeric-value arg) 0)))
4332 (force-mode-line-update)
4333 (unless truncate-lines
4334 (let ((buffer (current-buffer)))
4335 (walk-windows (lambda (window)
4336 (if (eq buffer (window-buffer window))
4337 (set-window-hscroll window 0)))
4339 (message "Truncate long lines %s"
4340 (if truncate-lines "enabled" "disabled")))
4342 (defvar overwrite-mode-textual " Ovwrt"
4343 "The string displayed in the mode line when in overwrite mode.")
4344 (defvar overwrite-mode-binary " Bin Ovwrt"
4345 "The string displayed in the mode line when in binary overwrite mode.")
4347 (defun overwrite-mode (arg)
4348 "Toggle overwrite mode.
4349 With arg, turn overwrite mode on iff arg is positive.
4350 In overwrite mode, printing characters typed in replace existing text
4351 on a one-for-one basis, rather than pushing it to the right. At the
4352 end of a line, such characters extend the line. Before a tab,
4353 such characters insert until the tab is filled in.
4354 \\[quoted-insert] still inserts characters in overwrite mode; this
4355 is supposed to make it easier to insert characters when necessary."
4357 (setq overwrite-mode
4358 (if (if (null arg) (not overwrite-mode)
4359 (> (prefix-numeric-value arg) 0))
4360 'overwrite-mode-textual))
4361 (force-mode-line-update))
4363 (defun binary-overwrite-mode (arg)
4364 "Toggle binary overwrite mode.
4365 With arg, turn binary overwrite mode on iff arg is positive.
4366 In binary overwrite mode, printing characters typed in replace
4367 existing text. Newlines are not treated specially, so typing at the
4368 end of a line joins the line to the next, with the typed character
4369 between them. Typing before a tab character simply replaces the tab
4370 with the character typed.
4371 \\[quoted-insert] replaces the text at the cursor, just as ordinary
4372 typing characters do.
4374 Note that binary overwrite mode is not its own minor mode; it is a
4375 specialization of overwrite mode, entered by setting the
4376 `overwrite-mode' variable to `overwrite-mode-binary'."
4378 (setq overwrite-mode
4380 (not (eq overwrite-mode 'overwrite-mode-binary))
4381 (> (prefix-numeric-value arg) 0))
4382 'overwrite-mode-binary))
4383 (force-mode-line-update))
4385 (define-minor-mode line-number-mode
4386 "Toggle Line Number mode.
4387 With arg, turn Line Number mode on iff arg is positive.
4388 When Line Number mode is enabled, the line number appears
4391 Line numbers do not appear for very large buffers and buffers
4392 with very long lines; see variables `line-number-display-limit'
4393 and `line-number-display-limit-width'."
4394 :init-value t :global t :group 'mode-line)
4396 (define-minor-mode column-number-mode
4397 "Toggle Column Number mode.
4398 With arg, turn Column Number mode on iff arg is positive.
4399 When Column Number mode is enabled, the column number appears
4401 :global t :group 'mode-line)
4403 (define-minor-mode size-indication-mode
4404 "Toggle Size Indication mode.
4405 With arg, turn Size Indication mode on iff arg is positive. When
4406 Size Indication mode is enabled, the size of the accessible part
4407 of the buffer appears in the mode line."
4408 :global t :group 'mode-line)
4410 (defgroup paren-blinking nil
4411 "Blinking matching of parens and expressions."
4412 :prefix "blink-matching-"
4413 :group 'paren-matching)
4415 (defcustom blink-matching-paren t
4416 "*Non-nil means show matching open-paren when close-paren is inserted."
4418 :group 'paren-blinking)
4420 (defcustom blink-matching-paren-on-screen t
4421 "*Non-nil means show matching open-paren when it is on screen.
4422 If nil, means don't show it (but the open-paren can still be shown
4423 when it is off screen).
4425 This variable has no effect if `blink-matching-paren' is nil.
4426 \(In that case, the open-paren is never shown.)
4427 It is also ignored if `show-paren-mode' is enabled."
4429 :group 'paren-blinking)
4431 (defcustom blink-matching-paren-distance (* 25 1024)
4432 "*If non-nil, maximum distance to search backwards for matching open-paren.
4433 If nil, search stops at the beginning of the accessible portion of the buffer."
4434 :type '(choice (const nil) integer)
4435 :group 'paren-blinking)
4437 (defcustom blink-matching-delay 1
4438 "*Time in seconds to delay after showing a matching paren."
4440 :group 'paren-blinking)
4442 (defcustom blink-matching-paren-dont-ignore-comments nil
4443 "*nil means `blink-matching-paren' ignores comments.
4444 More precisely, when looking for the matching parenthesis,
4445 it skips the contents of comments that end before point."
4447 :group 'paren-blinking)
4449 (defun blink-matching-open ()
4450 "Move cursor momentarily to the beginning of the sexp before point."
4452 (when (and (> (point) (point-min))
4453 blink-matching-paren
4454 ;; Verify an even number of quoting characters precede the close.
4455 (= 1 (logand 1 (- (point)
4458 (skip-syntax-backward "/\\")
4460 (let* ((oldpos (point))
4462 message-log-max ; Don't log messages about paren matching.
4464 open-paren-line-string)
4467 (if blink-matching-paren-distance
4468 (narrow-to-region (max (point-min)
4469 (- (point) blink-matching-paren-distance))
4472 (let ((parse-sexp-ignore-comments
4473 (and parse-sexp-ignore-comments
4474 (not blink-matching-paren-dont-ignore-comments))))
4475 (setq blinkpos (scan-sexps oldpos -1)))
4479 (not (eq (syntax-class (syntax-after blinkpos)) 8))
4480 (setq matching-paren
4481 (let ((syntax (syntax-after blinkpos)))
4483 (eq (syntax-class syntax) 4)
4486 ((not (or (eq matching-paren (char-before oldpos))
4487 ;; The cdr might hold a new paren-class info rather than
4488 ;; a matching-char info, in which case the two CDRs
4490 (eq matching-paren (cdr (syntax-after (1- oldpos))))))
4491 (message "Mismatched parentheses"))
4493 (if (not blink-matching-paren-distance)
4494 (message "Unmatched parenthesis")))
4495 ((pos-visible-in-window-p blinkpos)
4496 ;; Matching open within window, temporarily move to blinkpos but only
4497 ;; if `blink-matching-paren-on-screen' is non-nil.
4498 (and blink-matching-paren-on-screen
4499 (not show-paren-mode)
4501 (goto-char blinkpos)
4502 (sit-for blink-matching-delay))))
4505 (goto-char blinkpos)
4506 (setq open-paren-line-string
4507 ;; Show what precedes the open in its line, if anything.
4509 (skip-chars-backward " \t")
4511 (buffer-substring (line-beginning-position)
4513 ;; Show what follows the open in its line, if anything.
4516 (skip-chars-forward " \t")
4518 (buffer-substring blinkpos
4519 (line-end-position))
4520 ;; Otherwise show the previous nonblank line,
4523 (skip-chars-backward "\n \t")
4526 (buffer-substring (progn
4527 (skip-chars-backward "\n \t")
4528 (line-beginning-position))
4529 (progn (end-of-line)
4530 (skip-chars-backward " \t")
4532 ;; Replace the newline and other whitespace with `...'.
4534 (buffer-substring blinkpos (1+ blinkpos)))
4535 ;; There is nothing to show except the char itself.
4536 (buffer-substring blinkpos (1+ blinkpos)))))))
4537 (message "Matches %s"
4538 (substring-no-properties open-paren-line-string))))))))
4540 ;Turned off because it makes dbx bomb out.
4541 (setq blink-paren-function 'blink-matching-open)
4543 ;; This executes C-g typed while Emacs is waiting for a command.
4544 ;; Quitting out of a program does not go through here;
4545 ;; that happens in the QUIT macro at the C code level.
4546 (defun keyboard-quit ()
4547 "Signal a `quit' condition.
4548 During execution of Lisp code, this character causes a quit directly.
4549 At top-level, as an editor command, this simply beeps."
4552 (if (fboundp 'kmacro-keyboard-quit)
4553 (kmacro-keyboard-quit))
4554 (setq defining-kbd-macro nil)
4557 (defvar buffer-quit-function nil
4558 "Function to call to \"quit\" the current buffer, or nil if none.
4559 \\[keyboard-escape-quit] calls this function when its more local actions
4560 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4562 (defun keyboard-escape-quit ()
4563 "Exit the current \"mode\" (in a generalized sense of the word).
4564 This command can exit an interactive command such as `query-replace',
4565 can clear out a prefix argument or a region,
4566 can get out of the minibuffer or other recursive edit,
4567 cancel the use of the current buffer (for special-purpose buffers),
4568 or go back to just one window (by deleting all but the selected window)."
4570 (cond ((eq last-command 'mode-exited) nil)
4571 ((> (minibuffer-depth) 0)
4572 (abort-recursive-edit))
4575 ((and transient-mark-mode mark-active)
4577 ((> (recursion-depth) 0)
4578 (exit-recursive-edit))
4579 (buffer-quit-function
4580 (funcall buffer-quit-function))
4581 ((not (one-window-p t))
4582 (delete-other-windows))
4583 ((string-match "^ \\*" (buffer-name (current-buffer)))
4586 (defun play-sound-file (file &optional volume device)
4587 "Play sound stored in FILE.
4588 VOLUME and DEVICE correspond to the keywords of the sound
4589 specification for `play-sound'."
4590 (interactive "fPlay sound file: ")
4591 (let ((sound (list :file file)))
4593 (plist-put sound :volume volume))
4595 (plist-put sound :device device))
4597 (play-sound sound)))
4600 (defcustom read-mail-command 'rmail
4601 "*Your preference for a mail reading package.
4602 This is used by some keybindings which support reading mail.
4603 See also `mail-user-agent' concerning sending mail."
4604 :type '(choice (function-item rmail)
4605 (function-item gnus)
4606 (function-item mh-rmail)
4607 (function :tag "Other"))
4611 (defcustom mail-user-agent 'sendmail-user-agent
4612 "*Your preference for a mail composition package.
4613 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4614 outgoing email message. This variable lets you specify which
4615 mail-sending package you prefer.
4617 Valid values include:
4619 `sendmail-user-agent' -- use the default Emacs Mail package.
4620 See Info node `(emacs)Sending Mail'.
4621 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4622 See Info node `(mh-e)'.
4623 `message-user-agent' -- use the Gnus Message package.
4624 See Info node `(message)'.
4625 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4626 paraphernalia, particularly the Gcc: header for
4629 Additional valid symbols may be available; check with the author of
4630 your package for details. The function should return non-nil if it
4633 See also `read-mail-command' concerning reading mail."
4634 :type '(radio (function-item :tag "Default Emacs mail"
4636 sendmail-user-agent)
4637 (function-item :tag "Emacs interface to MH"
4640 (function-item :tag "Gnus Message package"
4643 (function-item :tag "Gnus Message with full Gnus features"
4646 (function :tag "Other"))
4649 (define-mail-user-agent 'sendmail-user-agent
4650 'sendmail-user-agent-compose
4651 'mail-send-and-exit)
4653 (defun rfc822-goto-eoh ()
4654 ;; Go to header delimiter line in a mail message, following RFC822 rules
4655 (goto-char (point-min))
4656 (when (re-search-forward
4657 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4658 (goto-char (match-beginning 0))))
4660 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4661 switch-function yank-action
4664 (let ((special-display-buffer-names nil)
4665 (special-display-regexps nil)
4666 (same-window-buffer-names nil)
4667 (same-window-regexps nil))
4668 (funcall switch-function "*mail*")))
4669 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4670 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4671 (body (cdr (assoc-string "body" other-headers t))))
4672 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4674 (error "Message aborted"))
4677 (while other-headers
4678 (unless (member-ignore-case (car (car other-headers))
4679 '("in-reply-to" "cc" "body"))
4680 (insert (car (car other-headers)) ": "
4681 (cdr (car other-headers))
4682 (if use-hard-newlines hard-newline "\n")))
4683 (setq other-headers (cdr other-headers)))
4689 (defun compose-mail (&optional to subject other-headers continue
4690 switch-function yank-action send-actions)
4691 "Start composing a mail message to send.
4692 This uses the user's chosen mail composition package
4693 as selected with the variable `mail-user-agent'.
4694 The optional arguments TO and SUBJECT specify recipients
4695 and the initial Subject field, respectively.
4697 OTHER-HEADERS is an alist specifying additional
4698 header fields. Elements look like (HEADER . VALUE) where both
4699 HEADER and VALUE are strings.
4701 CONTINUE, if non-nil, says to continue editing a message already
4704 SWITCH-FUNCTION, if non-nil, is a function to use to
4705 switch to and display the buffer used for mail composition.
4707 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4708 to insert the raw text of the message being replied to.
4709 It has the form (FUNCTION . ARGS). The user agent will apply
4710 FUNCTION to ARGS, to insert the raw text of the original message.
4711 \(The user agent will also run `mail-citation-hook', *after* the
4712 original text has been inserted in this way.)
4714 SEND-ACTIONS is a list of actions to call when the message is sent.
4715 Each action has the form (FUNCTION . ARGS)."
4717 (list nil nil nil current-prefix-arg))
4718 (let ((function (get mail-user-agent 'composefunc)))
4719 (funcall function to subject other-headers continue
4720 switch-function yank-action send-actions)))
4722 (defun compose-mail-other-window (&optional to subject other-headers continue
4723 yank-action send-actions)
4724 "Like \\[compose-mail], but edit the outgoing message in another window."
4726 (list nil nil nil current-prefix-arg))
4727 (compose-mail to subject other-headers continue
4728 'switch-to-buffer-other-window yank-action send-actions))
4731 (defun compose-mail-other-frame (&optional to subject other-headers continue
4732 yank-action send-actions)
4733 "Like \\[compose-mail], but edit the outgoing message in another frame."
4735 (list nil nil nil current-prefix-arg))
4736 (compose-mail to subject other-headers continue
4737 'switch-to-buffer-other-frame yank-action send-actions))
4739 (defvar set-variable-value-history nil
4740 "History of values entered with `set-variable'.")
4742 (defun set-variable (variable value &optional make-local)
4743 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4744 VARIABLE should be a user option variable name, a Lisp variable
4745 meant to be customized by users. You should enter VALUE in Lisp syntax,
4746 so if you want VALUE to be a string, you must surround it with doublequotes.
4747 VALUE is used literally, not evaluated.
4749 If VARIABLE has a `variable-interactive' property, that is used as if
4750 it were the arg to `interactive' (which see) to interactively read VALUE.
4752 If VARIABLE has been defined with `defcustom', then the type information
4753 in the definition is used to check that VALUE is valid.
4755 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4757 (let* ((default-var (variable-at-point))
4758 (var (if (user-variable-p default-var)
4759 (read-variable (format "Set variable (default %s): " default-var)
4761 (read-variable "Set variable: ")))
4762 (minibuffer-help-form '(describe-variable var))
4763 (prop (get var 'variable-interactive))
4764 (obsolete (car (get var 'byte-obsolete-variable)))
4765 (prompt (format "Set %s %s to value: " var
4766 (cond ((local-variable-p var)
4768 ((or current-prefix-arg
4769 (local-variable-if-set-p var))
4774 (message (concat "`%S' is obsolete; "
4775 (if (symbolp obsolete) "use `%S' instead" "%s"))
4779 ;; Use VAR's `variable-interactive' property
4780 ;; as an interactive spec for prompting.
4781 (call-interactively `(lambda (arg)
4785 (read-string prompt nil
4786 'set-variable-value-history
4787 (format "%S" (symbol-value var))))))))
4788 (list var val current-prefix-arg)))
4790 (and (custom-variable-p variable)
4791 (not (get variable 'custom-type))
4792 (custom-load-symbol variable))
4793 (let ((type (get variable 'custom-type)))
4795 ;; Match with custom type.
4797 (setq type (widget-convert type))
4798 (unless (widget-apply type :match value)
4799 (error "Value `%S' does not match type %S of %S"
4800 value (car type) variable))))
4803 (make-local-variable variable))
4805 (set variable value)
4807 ;; Force a thorough redisplay for the case that the variable
4808 ;; has an effect on the display, like `tab-width' has.
4809 (force-mode-line-update))
4811 ;; Define the major mode for lists of completions.
4813 (defvar completion-list-mode-map nil
4814 "Local map for completion list buffers.")
4815 (or completion-list-mode-map
4816 (let ((map (make-sparse-keymap)))
4817 (define-key map [mouse-2] 'mouse-choose-completion)
4818 (define-key map [follow-link] 'mouse-face)
4819 (define-key map [down-mouse-2] nil)
4820 (define-key map "\C-m" 'choose-completion)
4821 (define-key map "\e\e\e" 'delete-completion-window)
4822 (define-key map [left] 'previous-completion)
4823 (define-key map [right] 'next-completion)
4824 (setq completion-list-mode-map map)))
4826 ;; Completion mode is suitable only for specially formatted data.
4827 (put 'completion-list-mode 'mode-class 'special)
4829 (defvar completion-reference-buffer nil
4830 "Record the buffer that was current when the completion list was requested.
4831 This is a local variable in the completion list buffer.
4832 Initial value is nil to avoid some compiler warnings.")
4834 (defvar completion-no-auto-exit nil
4835 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4836 This also applies to other functions such as `choose-completion'
4837 and `mouse-choose-completion'.")
4839 (defvar completion-base-size nil
4840 "Number of chars at beginning of minibuffer not involved in completion.
4841 This is a local variable in the completion list buffer
4842 but it talks about the buffer in `completion-reference-buffer'.
4843 If this is nil, it means to compare text to determine which part
4844 of the tail end of the buffer's text is involved in completion.")
4846 (defun delete-completion-window ()
4847 "Delete the completion list window.
4848 Go to the window from which completion was requested."
4850 (let ((buf completion-reference-buffer))
4851 (if (one-window-p t)
4852 (if (window-dedicated-p (selected-window))
4853 (delete-frame (selected-frame)))
4854 (delete-window (selected-window))
4855 (if (get-buffer-window buf)
4856 (select-window (get-buffer-window buf))))))
4858 (defun previous-completion (n)
4859 "Move to the previous item in the completion list."
4861 (next-completion (- n)))
4863 (defun next-completion (n)
4864 "Move to the next item in the completion list.
4865 With prefix argument N, move N items (negative N means move backward)."
4867 (let ((beg (point-min)) (end (point-max)))
4868 (while (and (> n 0) (not (eobp)))
4869 ;; If in a completion, move to the end of it.
4870 (when (get-text-property (point) 'mouse-face)
4871 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4872 ;; Move to start of next one.
4873 (unless (get-text-property (point) 'mouse-face)
4874 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4876 (while (and (< n 0) (not (bobp)))
4877 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4878 ;; If in a completion, move to the start of it.
4879 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4880 (goto-char (previous-single-property-change
4881 (point) 'mouse-face nil beg)))
4882 ;; Move to end of the previous completion.
4883 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4884 (goto-char (previous-single-property-change
4885 (point) 'mouse-face nil beg)))
4886 ;; Move to the start of that one.
4887 (goto-char (previous-single-property-change
4888 (point) 'mouse-face nil beg))
4891 (defun choose-completion ()
4892 "Choose the completion that point is in or next to."
4894 (let (beg end completion (buffer completion-reference-buffer)
4895 (base-size completion-base-size))
4896 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4897 (setq end (point) beg (1+ (point))))
4898 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4899 (setq end (1- (point)) beg (point)))
4901 (error "No completion here"))
4902 (setq beg (previous-single-property-change beg 'mouse-face))
4903 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4904 (setq completion (buffer-substring-no-properties beg end))
4905 (let ((owindow (selected-window)))
4906 (if (and (one-window-p t 'selected-frame)
4907 (window-dedicated-p (selected-window)))
4908 ;; This is a special buffer's frame
4909 (iconify-frame (selected-frame))
4910 (or (window-dedicated-p (selected-window))
4912 (select-window owindow))
4913 (choose-completion-string completion buffer base-size)))
4915 ;; Delete the longest partial match for STRING
4916 ;; that can be found before POINT.
4917 (defun choose-completion-delete-max-match (string)
4918 (let ((opoint (point))
4920 ;; Try moving back by the length of the string.
4921 (goto-char (max (- (point) (length string))
4922 (minibuffer-prompt-end)))
4923 ;; See how far back we were actually able to move. That is the
4924 ;; upper bound on how much we can match and delete.
4925 (setq len (- opoint (point)))
4926 (if completion-ignore-case
4927 (setq string (downcase string)))
4928 (while (and (> len 0)
4929 (let ((tail (buffer-substring (point) opoint)))
4930 (if completion-ignore-case
4931 (setq tail (downcase tail)))
4932 (not (string= tail (substring string 0 len)))))
4937 (defvar choose-completion-string-functions nil
4938 "Functions that may override the normal insertion of a completion choice.
4939 These functions are called in order with four arguments:
4940 CHOICE - the string to insert in the buffer,
4941 BUFFER - the buffer in which the choice should be inserted,
4942 MINI-P - non-nil iff BUFFER is a minibuffer, and
4943 BASE-SIZE - the number of characters in BUFFER before
4944 the string being completed.
4946 If a function in the list returns non-nil, that function is supposed
4947 to have inserted the CHOICE in the BUFFER, and possibly exited
4948 the minibuffer; no further functions will be called.
4950 If all functions in the list return nil, that means to use
4951 the default method of inserting the completion in BUFFER.")
4953 (defun choose-completion-string (choice &optional buffer base-size)
4954 "Switch to BUFFER and insert the completion choice CHOICE.
4955 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4956 to keep. If it is nil, we call `choose-completion-delete-max-match'
4957 to decide what to delete."
4959 ;; If BUFFER is the minibuffer, exit the minibuffer
4960 ;; unless it is reading a file name and CHOICE is a directory,
4961 ;; or completion-no-auto-exit is non-nil.
4963 (let* ((buffer (or buffer completion-reference-buffer))
4964 (mini-p (minibufferp buffer)))
4965 ;; If BUFFER is a minibuffer, barf unless it's the currently
4966 ;; active minibuffer.
4968 (or (not (active-minibuffer-window))
4970 (window-buffer (active-minibuffer-window))))))
4971 (error "Minibuffer is not active for completion")
4972 ;; Set buffer so buffer-local choose-completion-string-functions works.
4974 (unless (run-hook-with-args-until-success
4975 'choose-completion-string-functions
4976 choice buffer mini-p base-size)
4977 ;; Insert the completion into the buffer where it was requested.
4979 (delete-region (+ base-size (if mini-p
4980 (minibuffer-prompt-end)
4983 (choose-completion-delete-max-match choice))
4985 (remove-text-properties (- (point) (length choice)) (point)
4987 ;; Update point in the window that BUFFER is showing in.
4988 (let ((window (get-buffer-window buffer t)))
4989 (set-window-point window (point)))
4990 ;; If completing for the minibuffer, exit it with this choice.
4991 (and (not completion-no-auto-exit)
4992 (equal buffer (window-buffer (minibuffer-window)))
4993 minibuffer-completion-table
4994 ;; If this is reading a file name, and the file name chosen
4995 ;; is a directory, don't exit the minibuffer.
4996 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4997 (file-directory-p (field-string (point-max))))
4998 (let ((mini (active-minibuffer-window)))
4999 (select-window mini)
5000 (when minibuffer-auto-raise
5001 (raise-frame (window-frame mini))))
5002 (exit-minibuffer)))))))
5004 (defun completion-list-mode ()
5005 "Major mode for buffers showing lists of possible completions.
5006 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
5007 to select the completion near point.
5008 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
5011 (kill-all-local-variables)
5012 (use-local-map completion-list-mode-map)
5013 (setq mode-name "Completion List")
5014 (setq major-mode 'completion-list-mode)
5015 (make-local-variable 'completion-base-size)
5016 (setq completion-base-size nil)
5017 (run-mode-hooks 'completion-list-mode-hook))
5019 (defun completion-list-mode-finish ()
5020 "Finish setup of the completions buffer.
5021 Called from `temp-buffer-show-hook'."
5022 (when (eq major-mode 'completion-list-mode)
5023 (toggle-read-only 1)))
5025 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
5027 (defvar completion-setup-hook nil
5028 "Normal hook run at the end of setting up a completion list buffer.
5029 When this hook is run, the current buffer is the one in which the
5030 command to display the completion list buffer was run.
5031 The completion list buffer is available as the value of `standard-output'.
5032 The common prefix substring for completion may be available as the
5033 value of `completion-common-substring'. See also `display-completion-list'.")
5036 ;; Variables and faces used in `completion-setup-function'.
5038 (defcustom completion-show-help t
5039 "Non-nil means show help message in *Completions* buffer."
5044 (defface completions-first-difference
5045 '((t (:inherit bold)))
5046 "Face put on the first uncommon character in completions in *Completions* buffer."
5049 (defface completions-common-part
5050 '((t (:inherit default)))
5051 "Face put on the common prefix substring in completions in *Completions* buffer.
5052 The idea of `completions-common-part' is that you can use it to
5053 make the common parts less visible than normal, so that the rest
5054 of the differing parts is, by contrast, slightly highlighted."
5057 ;; This is for packages that need to bind it to a non-default regexp
5058 ;; in order to make the first-differing character highlight work
5060 (defvar completion-root-regexp "^/"
5061 "Regexp to use in `completion-setup-function' to find the root directory.")
5063 (defvar completion-common-substring nil
5064 "Common prefix substring to use in `completion-setup-function' to put faces.
5065 The value is set by `display-completion-list' during running `completion-setup-hook'.
5067 To put faces `completions-first-difference' and `completions-common-part'
5068 in the `*Completions*' buffer, the common prefix substring in completions
5069 is needed as a hint. (The minibuffer is a special case. The content
5070 of the minibuffer before point is always the common substring.)")
5072 ;; This function goes in completion-setup-hook, so that it is called
5073 ;; after the text of the completion list buffer is written.
5074 (defun completion-setup-function ()
5075 (let* ((mainbuf (current-buffer))
5076 (mbuf-contents (minibuffer-completion-contents))
5077 common-string-length)
5078 ;; When reading a file name in the minibuffer,
5079 ;; set default-directory in the minibuffer
5080 ;; so it will get copied into the completion list buffer.
5081 (if minibuffer-completing-file-name
5082 (with-current-buffer mainbuf
5083 (setq default-directory (file-name-directory mbuf-contents))))
5084 (with-current-buffer standard-output
5085 (completion-list-mode)
5086 (set (make-local-variable 'completion-reference-buffer) mainbuf)
5087 (setq completion-base-size
5089 ((and (symbolp minibuffer-completion-table)
5090 (get minibuffer-completion-table 'completion-base-size-function))
5091 ;; To compute base size, a function can use the global value of
5092 ;; completion-common-substring or minibuffer-completion-contents.
5093 (with-current-buffer mainbuf
5094 (funcall (get minibuffer-completion-table
5095 'completion-base-size-function))))
5096 (minibuffer-completing-file-name
5097 ;; For file name completion, use the number of chars before
5098 ;; the start of the file name component at point.
5099 (with-current-buffer mainbuf
5101 (skip-chars-backward completion-root-regexp)
5102 (- (point) (minibuffer-prompt-end)))))
5103 ;; Otherwise, in minibuffer, the base size is 0.
5104 ((minibufferp mainbuf) 0)))
5105 (setq common-string-length
5107 (completion-common-substring
5108 (length completion-common-substring))
5109 (completion-base-size
5110 (- (length mbuf-contents) completion-base-size))))
5111 ;; Put faces on first uncommon characters and common parts.
5112 (when (and (integerp common-string-length) (>= common-string-length 0))
5113 (let ((element-start (point-min))
5116 (while (and (setq element-start
5117 (next-single-property-change
5118 element-start 'mouse-face))
5119 (< (setq element-common-end
5120 (+ element-start common-string-length))
5122 (when (get-char-property element-start 'mouse-face)
5123 (if (and (> common-string-length 0)
5124 (get-char-property (1- element-common-end) 'mouse-face))
5125 (put-text-property element-start element-common-end
5126 'font-lock-face 'completions-common-part))
5127 (if (get-char-property element-common-end 'mouse-face)
5128 (put-text-property element-common-end (1+ element-common-end)
5129 'font-lock-face 'completions-first-difference))))))
5130 ;; Maybe insert help string.
5131 (when completion-show-help
5132 (goto-char (point-min))
5133 (if (display-mouse-p)
5134 (insert (substitute-command-keys
5135 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
5136 (insert (substitute-command-keys
5137 "In this buffer, type \\[choose-completion] to \
5138 select the completion near point.\n\n"))))))
5140 (add-hook 'completion-setup-hook 'completion-setup-function)
5142 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
5143 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
5145 (defun switch-to-completions ()
5146 "Select the completion list window."
5148 ;; Make sure we have a completions window.
5149 (or (get-buffer-window "*Completions*")
5150 (minibuffer-completion-help))
5151 (let ((window (get-buffer-window "*Completions*")))
5153 (select-window window)
5154 (goto-char (point-min))
5155 (search-forward "\n\n")
5158 ;;; Support keyboard commands to turn on various modifiers.
5160 ;; These functions -- which are not commands -- each add one modifier
5161 ;; to the following event.
5163 (defun event-apply-alt-modifier (ignore-prompt)
5164 "\\<function-key-map>Add the Alt modifier to the following event.
5165 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
5166 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
5167 (defun event-apply-super-modifier (ignore-prompt)
5168 "\\<function-key-map>Add the Super modifier to the following event.
5169 For example, type \\[event-apply-super-modifier] & to enter Super-&."
5170 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
5171 (defun event-apply-hyper-modifier (ignore-prompt)
5172 "\\<function-key-map>Add the Hyper modifier to the following event.
5173 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
5174 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
5175 (defun event-apply-shift-modifier (ignore-prompt)
5176 "\\<function-key-map>Add the Shift modifier to the following event.
5177 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
5178 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
5179 (defun event-apply-control-modifier (ignore-prompt)
5180 "\\<function-key-map>Add the Ctrl modifier to the following event.
5181 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
5182 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
5183 (defun event-apply-meta-modifier (ignore-prompt)
5184 "\\<function-key-map>Add the Meta modifier to the following event.
5185 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
5186 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
5188 (defun event-apply-modifier (event symbol lshiftby prefix)
5189 "Apply a modifier flag to event EVENT.
5190 SYMBOL is the name of this modifier, as a symbol.
5191 LSHIFTBY is the numeric value of this modifier, in keyboard events.
5192 PREFIX is the string that represents this modifier in an event type symbol."
5194 (cond ((eq symbol 'control)
5195 (if (and (<= (downcase event) ?z)
5196 (>= (downcase event) ?a))
5197 (- (downcase event) ?a -1)
5198 (if (and (<= (downcase event) ?Z)
5199 (>= (downcase event) ?A))
5200 (- (downcase event) ?A -1)
5201 (logior (lsh 1 lshiftby) event))))
5203 (if (and (<= (downcase event) ?z)
5204 (>= (downcase event) ?a))
5206 (logior (lsh 1 lshiftby) event)))
5208 (logior (lsh 1 lshiftby) event)))
5209 (if (memq symbol (event-modifiers event))
5211 (let ((event-type (if (symbolp event) event (car event))))
5212 (setq event-type (intern (concat prefix (symbol-name event-type))))
5215 (cons event-type (cdr event)))))))
5217 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
5218 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
5219 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
5220 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
5221 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
5222 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
5224 ;;;; Keypad support.
5226 ;;; Make the keypad keys act like ordinary typing keys. If people add
5227 ;;; bindings for the function key symbols, then those bindings will
5228 ;;; override these, so this shouldn't interfere with any existing
5231 ;; Also tell read-char how to handle these keys.
5233 (lambda (keypad-normal)
5234 (let ((keypad (nth 0 keypad-normal))
5235 (normal (nth 1 keypad-normal)))
5236 (put keypad 'ascii-character normal)
5237 (define-key function-key-map (vector keypad) (vector normal))))
5238 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
5239 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
5252 ;;;; forking a twin copy of a buffer.
5255 (defvar clone-buffer-hook nil
5256 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
5258 (defun clone-process (process &optional newname)
5259 "Create a twin copy of PROCESS.
5260 If NEWNAME is nil, it defaults to PROCESS' name;
5261 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
5262 If PROCESS is associated with a buffer, the new process will be associated
5263 with the current buffer instead.
5264 Returns nil if PROCESS has already terminated."
5265 (setq newname (or newname (process-name process)))
5266 (if (string-match "<[0-9]+>\\'" newname)
5267 (setq newname (substring newname 0 (match-beginning 0))))
5268 (when (memq (process-status process) '(run stop open))
5269 (let* ((process-connection-type (process-tty-name process))
5271 (if (memq (process-status process) '(open))
5272 (let ((args (process-contact process t)))
5273 (setq args (plist-put args :name newname))
5274 (setq args (plist-put args :buffer
5275 (if (process-buffer process)
5277 (apply 'make-network-process args))
5278 (apply 'start-process newname
5279 (if (process-buffer process) (current-buffer))
5280 (process-command process)))))
5281 (set-process-query-on-exit-flag
5282 new-process (process-query-on-exit-flag process))
5283 (set-process-inherit-coding-system-flag
5284 new-process (process-inherit-coding-system-flag process))
5285 (set-process-filter new-process (process-filter process))
5286 (set-process-sentinel new-process (process-sentinel process))
5287 (set-process-plist new-process (copy-sequence (process-plist process)))
5290 ;; things to maybe add (currently partly covered by `funcall mode'):
5293 (defun clone-buffer (&optional newname display-flag)
5294 "Create and return a twin copy of the current buffer.
5295 Unlike an indirect buffer, the new buffer can be edited
5296 independently of the old one (if it is not read-only).
5297 NEWNAME is the name of the new buffer. It may be modified by
5298 adding or incrementing <N> at the end as necessary to create a
5299 unique buffer name. If nil, it defaults to the name of the
5300 current buffer, with the proper suffix. If DISPLAY-FLAG is
5301 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
5302 clone a file-visiting buffer, or a buffer whose major mode symbol
5303 has a non-nil `no-clone' property, results in an error.
5305 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
5306 current buffer with appropriate suffix. However, if a prefix
5307 argument is given, then the command prompts for NEWNAME in the
5310 This runs the normal hook `clone-buffer-hook' in the new buffer
5311 after it has been set up properly in other respects."
5314 (if buffer-file-name
5315 (error "Cannot clone a file-visiting buffer"))
5316 (if (get major-mode 'no-clone)
5317 (error "Cannot clone a buffer in %s mode" mode-name))
5318 (list (if current-prefix-arg
5319 (read-buffer "Name of new cloned buffer: " (current-buffer)))
5321 (if buffer-file-name
5322 (error "Cannot clone a file-visiting buffer"))
5323 (if (get major-mode 'no-clone)
5324 (error "Cannot clone a buffer in %s mode" mode-name))
5325 (setq newname (or newname (buffer-name)))
5326 (if (string-match "<[0-9]+>\\'" newname)
5327 (setq newname (substring newname 0 (match-beginning 0))))
5328 (let ((buf (current-buffer))
5332 (mk (if mark-active (mark t)))
5333 (modified (buffer-modified-p))
5335 (lvars (buffer-local-variables))
5336 (process (get-buffer-process (current-buffer)))
5337 (new (generate-new-buffer (or newname (buffer-name)))))
5340 (with-current-buffer new
5341 (insert-buffer-substring buf)))
5342 (with-current-buffer new
5343 (narrow-to-region ptmin ptmax)
5345 (if mk (set-mark mk))
5346 (set-buffer-modified-p modified)
5348 ;; Clone the old buffer's process, if any.
5349 (when process (clone-process process))
5351 ;; Now set up the major mode.
5354 ;; Set up other local variables.
5356 (condition-case () ;in case var is read-only
5359 (set (make-local-variable (car v)) (cdr v)))
5363 ;; Run any hooks (typically set up by the major mode
5364 ;; for cloning to work properly).
5365 (run-hooks 'clone-buffer-hook))
5367 ;; Presumably the current buffer is shown in the selected frame, so
5368 ;; we want to display the clone elsewhere.
5369 (let ((same-window-regexps nil)
5370 (same-window-buffer-names))
5371 (pop-to-buffer new)))
5375 (defun clone-indirect-buffer (newname display-flag &optional norecord)
5376 "Create an indirect buffer that is a twin copy of the current buffer.
5378 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
5379 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
5380 or if not called with a prefix arg, NEWNAME defaults to the current
5381 buffer's name. The name is modified by adding a `<N>' suffix to it
5382 or by incrementing the N in an existing suffix.
5384 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
5385 This is always done when called interactively.
5387 Optional third arg NORECORD non-nil means do not put this buffer at the
5388 front of the list of recently selected ones."
5391 (if (get major-mode 'no-clone-indirect)
5392 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5393 (list (if current-prefix-arg
5394 (read-buffer "Name of indirect buffer: " (current-buffer)))
5396 (if (get major-mode 'no-clone-indirect)
5397 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5398 (setq newname (or newname (buffer-name)))
5399 (if (string-match "<[0-9]+>\\'" newname)
5400 (setq newname (substring newname 0 (match-beginning 0))))
5401 (let* ((name (generate-new-buffer-name newname))
5402 (buffer (make-indirect-buffer (current-buffer) name t)))
5404 (pop-to-buffer buffer norecord))
5408 (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
5409 "Like `clone-indirect-buffer' but display in another window."
5412 (if (get major-mode 'no-clone-indirect)
5413 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5414 (list (if current-prefix-arg
5415 (read-buffer "Name of indirect buffer: " (current-buffer)))
5417 (let ((pop-up-windows t))
5418 (clone-indirect-buffer newname display-flag norecord)))
5421 ;;; Handling of Backspace and Delete keys.
5423 (defcustom normal-erase-is-backspace 'maybe
5424 "Set the default behaviour of the Delete and Backspace keys.
5426 If set to t, Delete key deletes forward and Backspace key deletes
5429 If set to nil, both Delete and Backspace keys delete backward.
5431 If set to 'maybe (which is the default), Emacs automatically
5432 selects a behaviour. On window systems, the behaviour depends on
5433 the keyboard used. If the keyboard has both a Backspace key and
5434 a Delete key, and both are mapped to their usual meanings, the
5435 option's default value is set to t, so that Backspace can be used
5436 to delete backward, and Delete can be used to delete forward.
5438 If not running under a window system, customizing this option
5439 accomplishes a similar effect by mapping C-h, which is usually
5440 generated by the Backspace key, to DEL, and by mapping DEL to C-d
5441 via `keyboard-translate'. The former functionality of C-h is
5442 available on the F1 key. You should probably not use this
5443 setting if you don't have both Backspace, Delete and F1 keys.
5445 Setting this variable with setq doesn't take effect. Programmatically,
5446 call `normal-erase-is-backspace-mode' (which see) instead."
5447 :type '(choice (const :tag "Off" nil)
5448 (const :tag "Maybe" maybe)
5449 (other :tag "On" t))
5450 :group 'editing-basics
5452 :set (lambda (symbol value)
5453 ;; The fboundp is because of a problem with :set when
5454 ;; dumping Emacs. It doesn't really matter.
5455 (if (fboundp 'normal-erase-is-backspace-mode)
5456 (normal-erase-is-backspace-mode (or value 0))
5457 (set-default symbol value))))
5459 (defun normal-erase-is-backspace-setup-frame (&optional frame)
5460 "Set up `normal-erase-is-backspace-mode' on FRAME, if necessary."
5461 (unless frame (setq frame (selected-frame)))
5462 (with-selected-frame frame
5463 (unless (terminal-parameter nil 'normal-erase-is-backspace)
5464 (if (cond ((eq normal-erase-is-backspace 'maybe)
5465 (and (not noninteractive)
5466 (or (memq system-type '(ms-dos windows-nt))
5467 (eq window-system 'mac)
5468 (and (memq window-system '(x))
5469 (fboundp 'x-backspace-delete-keys-p)
5470 (x-backspace-delete-keys-p))
5471 ;; If the terminal Emacs is running on has erase char
5472 ;; set to ^H, use the Backspace key for deleting
5473 ;; backward and, and the Delete key for deleting forward.
5474 (and (null window-system)
5475 (eq tty-erase-char ?\^H)))))
5477 normal-erase-is-backspace))
5478 (normal-erase-is-backspace-mode 1)
5479 (normal-erase-is-backspace-mode 0)))))
5481 (defun normal-erase-is-backspace-mode (&optional arg)
5482 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
5484 With numeric arg, turn the mode on if and only if ARG is positive.
5486 On window systems, when this mode is on, Delete is mapped to C-d
5487 and Backspace is mapped to DEL; when this mode is off, both
5488 Delete and Backspace are mapped to DEL. (The remapping goes via
5489 `local-function-key-map', so binding Delete or Backspace in the
5490 global or local keymap will override that.)
5492 In addition, on window systems, the bindings of C-Delete, M-Delete,
5493 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
5494 the global keymap in accordance with the functionality of Delete and
5495 Backspace. For example, if Delete is remapped to C-d, which deletes
5496 forward, C-Delete is bound to `kill-word', but if Delete is remapped
5497 to DEL, which deletes backward, C-Delete is bound to
5498 `backward-kill-word'.
5500 If not running on a window system, a similar effect is accomplished by
5501 remapping C-h (normally produced by the Backspace key) and DEL via
5502 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
5503 to C-d; if it's off, the keys are not remapped.
5505 When not running on a window system, and this mode is turned on, the
5506 former functionality of C-h is available on the F1 key. You should
5507 probably not turn on this mode on a text-only terminal if you don't
5508 have both Backspace, Delete and F1 keys.
5510 See also `normal-erase-is-backspace'."
5512 (let ((enabled (or (and arg (> (prefix-numeric-value arg) 0))
5514 (not (eq 1 (terminal-parameter
5515 nil 'normal-erase-is-backspace)))))))
5516 (set-terminal-parameter nil 'normal-erase-is-backspace
5519 (cond ((or (memq window-system '(x w32 mac pc))
5520 (memq system-type '(ms-dos windows-nt)))
5522 `(([C-delete] [C-backspace])
5523 ([M-delete] [M-backspace])
5524 ([C-M-delete] [C-M-backspace])
5526 [C-delete] [C-backspace])))
5527 (old-state (lookup-key local-function-key-map [delete])))
5531 (define-key local-function-key-map [delete] [?\C-d])
5532 (define-key local-function-key-map [kp-delete] [?\C-d])
5533 (define-key local-function-key-map [backspace] [?\C-?]))
5534 (define-key local-function-key-map [delete] [?\C-?])
5535 (define-key local-function-key-map [kp-delete] [?\C-?])
5536 (define-key local-function-key-map [backspace] [?\C-?]))
5538 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5539 (unless (equal old-state (lookup-key local-function-key-map [delete]))
5540 (dolist (binding bindings)
5541 (let ((map global-map))
5542 (when (keymapp (car binding))
5543 (setq map (car binding) binding (cdr binding)))
5544 (let* ((key1 (nth 0 binding))
5545 (key2 (nth 1 binding))
5546 (binding1 (lookup-key map key1))
5547 (binding2 (lookup-key map key2)))
5548 (define-key map key1 binding2)
5549 (define-key map key2 binding1)))))))
5553 (keyboard-translate ?\C-h ?\C-?)
5554 (keyboard-translate ?\C-? ?\C-d))
5555 (keyboard-translate ?\C-h ?\C-h)
5556 (keyboard-translate ?\C-? ?\C-?))))
5558 (run-hooks 'normal-erase-is-backspace-hook)
5560 (message "Delete key deletes %s"
5561 (if (terminal-parameter nil 'normal-erase-is-backspace)
5562 "forward" "backward")))))
5564 (defvar vis-mode-saved-buffer-invisibility-spec nil
5565 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5567 (define-minor-mode visible-mode
5568 "Toggle Visible mode.
5569 With argument ARG turn Visible mode on iff ARG is positive.
5571 Enabling Visible mode makes all invisible text temporarily visible.
5572 Disabling Visible mode turns off that effect. Visible mode
5573 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5575 :group 'editing-basics
5576 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5577 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5578 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5580 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5581 buffer-invisibility-spec)
5582 (setq buffer-invisibility-spec nil)))
5584 ;; Minibuffer prompt stuff.
5586 ;(defun minibuffer-prompt-modification (start end)
5587 ; (error "You cannot modify the prompt"))
5590 ;(defun minibuffer-prompt-insertion (start end)
5591 ; (let ((inhibit-modification-hooks t))
5592 ; (delete-region start end)
5593 ; ;; Discard undo information for the text insertion itself
5594 ; ;; and for the text deletion.above.
5595 ; (when (consp buffer-undo-list)
5596 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5597 ; (message "You cannot modify the prompt")))
5600 ;(setq minibuffer-prompt-properties
5601 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5602 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5607 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5608 ;;; simple.el ends here