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, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; A grab-bag of basic Emacs commands not specifically related to some
28 ;; major mode or to file-handling.
32 ;; This is for lexical-let in apply-partially.
33 (eval-when-compile (require 'cl
))
35 (declare-function widget-convert
"wid-edit" (type &rest args
))
36 (declare-function shell-mode
"shell" ())
38 (defvar compilation-current-error
)
40 (defcustom idle-update-delay
0.5
41 "Idle time delay before updating various things on the screen.
42 Various Emacs features that update auxiliary information when point moves
43 wait this many seconds after Emacs becomes idle before doing an update."
49 "Killing and yanking commands."
52 (defgroup paren-matching nil
53 "Highlight (un)matching of parens and expressions."
56 (defun get-next-valid-buffer (list &optional buffer visible-ok frame
)
57 "Search LIST for a valid buffer to display in FRAME.
58 Return nil when all buffers in LIST are undesirable for display,
59 otherwise return the first suitable buffer in LIST.
61 Buffers not visible in windows are preferred to visible buffers,
62 unless VISIBLE-OK is non-nil.
63 If the optional argument FRAME is nil, it defaults to the selected frame.
64 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
65 ;; This logic is more or less copied from other-buffer.
66 (setq frame
(or frame
(selected-frame)))
67 (let ((pred (frame-parameter frame
'buffer-predicate
))
69 (while (and (not found
) list
)
71 (if (and (not (eq buffer buf
))
73 (or (null pred
) (funcall pred buf
))
74 (not (eq (aref (buffer-name buf
) 0) ?\s
))
75 (or visible-ok
(null (get-buffer-window buf
'visible
))))
77 (setq list
(cdr list
))))
80 (defun last-buffer (&optional buffer visible-ok frame
)
81 "Return the last buffer in FRAME's buffer list.
82 If BUFFER is the last buffer, return the preceding buffer instead.
83 Buffers not visible in windows are preferred to visible buffers,
84 unless optional argument VISIBLE-OK is non-nil.
85 Optional third argument FRAME nil or omitted means use the
86 selected frame's buffer list.
87 If no such buffer exists, return the buffer `*scratch*', creating
89 (setq frame
(or frame
(selected-frame)))
90 (or (get-next-valid-buffer (nreverse (buffer-list frame
))
91 buffer visible-ok frame
)
92 (get-buffer "*scratch*")
93 (let ((scratch (get-buffer-create "*scratch*")))
94 (set-buffer-major-mode scratch
)
98 "Switch to the next buffer in cyclic order."
100 (let ((buffer (current-buffer)))
101 (switch-to-buffer (other-buffer buffer t
))
102 (bury-buffer buffer
)))
104 (defun previous-buffer ()
105 "Switch to the previous buffer in cyclic order."
107 (switch-to-buffer (last-buffer (current-buffer) t
)))
110 ;;; next-error support framework
112 (defgroup next-error nil
113 "`next-error' support framework."
118 '((t (:inherit region
)))
119 "Face used to highlight next error locus."
123 (defcustom next-error-highlight
0.5
124 "Highlighting of locations in selected source buffers.
125 If a number, highlight the locus in `next-error' face for the given time
126 in seconds, or until the next command is executed.
127 If t, highlight the locus until the next command is executed, or until
128 some other locus replaces it.
129 If nil, don't highlight the locus in the source buffer.
130 If `fringe-arrow', indicate the locus by the fringe arrow."
131 :type
'(choice (number :tag
"Highlight for specified time")
132 (const :tag
"Semipermanent highlighting" t
)
133 (const :tag
"No highlighting" nil
)
134 (const :tag
"Fringe arrow" fringe-arrow
))
138 (defcustom next-error-highlight-no-select
0.5
139 "Highlighting of locations in `next-error-no-select'.
140 If number, highlight the locus in `next-error' face for given time in seconds.
141 If t, highlight the locus indefinitely until some other locus replaces it.
142 If nil, don't highlight the locus in the source buffer.
143 If `fringe-arrow', indicate the locus by the fringe arrow."
144 :type
'(choice (number :tag
"Highlight for specified time")
145 (const :tag
"Semipermanent highlighting" t
)
146 (const :tag
"No highlighting" nil
)
147 (const :tag
"Fringe arrow" fringe-arrow
))
151 (defcustom next-error-recenter nil
152 "Display the line in the visited source file recentered as specified.
153 If non-nil, the value is passed directly to `recenter'."
154 :type
'(choice (integer :tag
"Line to recenter to")
155 (const :tag
"Center of window" (4))
156 (const :tag
"No recentering" nil
))
160 (defcustom next-error-hook nil
161 "List of hook functions run by `next-error' after visiting source file."
165 (defvar next-error-highlight-timer nil
)
167 (defvar next-error-overlay-arrow-position nil
)
168 (put 'next-error-overlay-arrow-position
'overlay-arrow-string
(purecopy "=>"))
169 (add-to-list 'overlay-arrow-variable-list
'next-error-overlay-arrow-position
)
171 (defvar next-error-last-buffer nil
172 "The most recent `next-error' buffer.
173 A buffer becomes most recent when its compilation, grep, or
174 similar mode is started, or when it is used with \\[next-error]
175 or \\[compile-goto-error].")
177 (defvar next-error-function nil
178 "Function to use to find the next error in the current buffer.
179 The function is called with 2 parameters:
180 ARG is an integer specifying by how many errors to move.
181 RESET is a boolean which, if non-nil, says to go back to the beginning
182 of the errors before moving.
183 Major modes providing compile-like functionality should set this variable
184 to indicate to `next-error' that this is a candidate buffer and how
186 (make-variable-buffer-local 'next-error-function
)
188 (defvar next-error-move-function nil
189 "Function to use to move to an error locus.
190 It takes two arguments, a buffer position in the error buffer
191 and a buffer position in the error locus buffer.
192 The buffer for the error locus should already be current.
193 nil means use goto-char using the second argument position.")
194 (make-variable-buffer-local 'next-error-move-function
)
196 (defsubst next-error-buffer-p
(buffer
197 &optional avoid-current
199 extra-test-exclusive
)
200 "Test if BUFFER is a `next-error' capable buffer.
202 If AVOID-CURRENT is non-nil, treat the current buffer
203 as an absolute last resort only.
205 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
206 that normally would not qualify. If it returns t, the buffer
207 in question is treated as usable.
209 The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
210 that would normally be considered usable. If it returns nil,
211 that buffer is rejected."
212 (and (buffer-name buffer
) ;First make sure it's live.
213 (not (and avoid-current
(eq buffer
(current-buffer))))
214 (with-current-buffer buffer
215 (if next-error-function
; This is the normal test.
216 ;; Optionally reject some buffers.
217 (if extra-test-exclusive
218 (funcall extra-test-exclusive
)
220 ;; Optionally accept some other buffers.
221 (and extra-test-inclusive
222 (funcall extra-test-inclusive
))))))
224 (defun next-error-find-buffer (&optional avoid-current
226 extra-test-exclusive
)
227 "Return a `next-error' capable buffer.
229 If AVOID-CURRENT is non-nil, treat the current buffer
230 as an absolute last resort only.
232 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
233 that normally would not qualify. If it returns t, the buffer
234 in question is treated as usable.
236 The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
237 that would normally be considered usable. If it returns nil,
238 that buffer is rejected."
240 ;; 1. If one window on the selected frame displays such buffer, return it.
241 (let ((window-buffers
243 (delq nil
(mapcar (lambda (w)
244 (if (next-error-buffer-p
247 extra-test-inclusive extra-test-exclusive
)
250 (if (eq (length window-buffers
) 1)
251 (car window-buffers
)))
252 ;; 2. If next-error-last-buffer is an acceptable buffer, use that.
253 (if (and next-error-last-buffer
254 (next-error-buffer-p next-error-last-buffer avoid-current
255 extra-test-inclusive extra-test-exclusive
))
256 next-error-last-buffer
)
257 ;; 3. If the current buffer is acceptable, choose it.
258 (if (next-error-buffer-p (current-buffer) avoid-current
259 extra-test-inclusive extra-test-exclusive
)
261 ;; 4. Look for any acceptable buffer.
262 (let ((buffers (buffer-list)))
264 (not (next-error-buffer-p
265 (car buffers
) avoid-current
266 extra-test-inclusive extra-test-exclusive
)))
267 (setq buffers
(cdr buffers
)))
269 ;; 5. Use the current buffer as a last resort if it qualifies,
270 ;; even despite AVOID-CURRENT.
272 (next-error-buffer-p (current-buffer) nil
273 extra-test-inclusive extra-test-exclusive
)
275 (message "This is the only buffer with error message locations")
278 (error "No buffers contain error message locations")))
280 (defun next-error (&optional arg reset
)
281 "Visit next `next-error' message and corresponding source code.
283 If all the error messages parsed so far have been processed already,
284 the message buffer is checked for new ones.
286 A prefix ARG specifies how many error messages to move;
287 negative means move back to previous error messages.
288 Just \\[universal-argument] as a prefix means reparse the error message buffer
289 and start at the first error.
291 The RESET argument specifies that we should restart from the beginning.
293 \\[next-error] normally uses the most recently started
294 compilation, grep, or occur buffer. It can also operate on any
295 buffer with output from the \\[compile], \\[grep] commands, or,
296 more generally, on any buffer in Compilation mode or with
297 Compilation Minor mode enabled, or any buffer in which
298 `next-error-function' is bound to an appropriate function.
299 To specify use of a particular buffer for error messages, type
300 \\[next-error] in that buffer when it is the only one displayed
301 in the current frame.
303 Once \\[next-error] has chosen the buffer for error messages, it
304 runs `next-error-hook' with `run-hooks', and stays with that buffer
305 until you use it in some other buffer which uses Compilation mode
306 or Compilation Minor mode.
308 See variables `compilation-parse-errors-function' and
309 \`compilation-error-regexp-alist' for customization ideas."
311 (if (consp arg
) (setq reset t arg nil
))
312 (when (setq next-error-last-buffer
(next-error-find-buffer))
313 ;; we know here that next-error-function is a valid symbol we can funcall
314 (with-current-buffer next-error-last-buffer
315 (funcall next-error-function
(prefix-numeric-value arg
) reset
)
316 (when next-error-recenter
317 (recenter next-error-recenter
))
318 (run-hooks 'next-error-hook
))))
320 (defun next-error-internal ()
321 "Visit the source code corresponding to the `next-error' message at point."
322 (setq next-error-last-buffer
(current-buffer))
323 ;; we know here that next-error-function is a valid symbol we can funcall
324 (with-current-buffer next-error-last-buffer
325 (funcall next-error-function
0 nil
)
326 (when next-error-recenter
327 (recenter next-error-recenter
))
328 (run-hooks 'next-error-hook
)))
330 (defalias 'goto-next-locus
'next-error
)
331 (defalias 'next-match
'next-error
)
333 (defun previous-error (&optional n
)
334 "Visit previous `next-error' message and corresponding source code.
336 Prefix arg N says how many error messages to move backwards (or
337 forwards, if negative).
339 This operates on the output from the \\[compile] and \\[grep] commands."
341 (next-error (- (or n
1))))
343 (defun first-error (&optional n
)
344 "Restart at the first error.
345 Visit corresponding source code.
346 With prefix arg N, visit the source code of the Nth error.
347 This operates on the output from the \\[compile] command, for instance."
351 (defun next-error-no-select (&optional n
)
352 "Move point to the next error in the `next-error' buffer and highlight match.
353 Prefix arg N says how many error messages to move forwards (or
354 backwards, if negative).
355 Finds and highlights the source line like \\[next-error], but does not
356 select the source buffer."
358 (let ((next-error-highlight next-error-highlight-no-select
))
360 (pop-to-buffer next-error-last-buffer
))
362 (defun previous-error-no-select (&optional n
)
363 "Move point to the previous error in the `next-error' buffer and highlight match.
364 Prefix arg N says how many error messages to move backwards (or
365 forwards, if negative).
366 Finds and highlights the source line like \\[previous-error], but does not
367 select the source buffer."
369 (next-error-no-select (- (or n
1))))
371 ;; Internal variable for `next-error-follow-mode-post-command-hook'.
372 (defvar next-error-follow-last-line nil
)
374 (define-minor-mode next-error-follow-minor-mode
375 "Minor mode for compilation, occur and diff modes.
376 When turned on, cursor motion in the compilation, grep, occur or diff
377 buffer causes automatic display of the corresponding source code
379 :group
'next-error
:init-value nil
:lighter
" Fol"
380 (if (not next-error-follow-minor-mode
)
381 (remove-hook 'post-command-hook
'next-error-follow-mode-post-command-hook t
)
382 (add-hook 'post-command-hook
'next-error-follow-mode-post-command-hook nil t
)
383 (make-local-variable 'next-error-follow-last-line
)))
385 ;; Used as a `post-command-hook' by `next-error-follow-mode'
386 ;; for the *Compilation* *grep* and *Occur* buffers.
387 (defun next-error-follow-mode-post-command-hook ()
388 (unless (equal next-error-follow-last-line
(line-number-at-pos))
389 (setq next-error-follow-last-line
(line-number-at-pos))
391 (let ((compilation-context-lines nil
))
392 (setq compilation-current-error
(point))
393 (next-error-no-select 0))
399 (defun fundamental-mode ()
400 "Major mode not specialized for anything in particular.
401 Other major modes are defined by comparison with this one."
403 (kill-all-local-variables)
404 (unless delay-mode-hooks
405 (run-hooks 'after-change-major-mode-hook
)))
407 ;; Special major modes to view specially formatted data rather than files.
409 (defvar special-mode-map
410 (let ((map (make-sparse-keymap)))
411 (suppress-keymap map
)
412 (define-key map
"q" 'quit-window
)
413 (define-key map
" " 'scroll-up
)
414 (define-key map
"\C-?" 'scroll-down
)
415 (define-key map
"?" 'describe-mode
)
416 (define-key map
">" 'end-of-buffer
)
417 (define-key map
"<" 'beginning-of-buffer
)
418 (define-key map
"g" 'revert-buffer
)
421 (put 'special-mode
'mode-class
'special
)
422 (define-derived-mode special-mode nil
"Special"
423 "Parent major mode from which special major modes should inherit."
424 (setq buffer-read-only t
))
426 ;; Making and deleting lines.
428 (defvar hard-newline
(propertize "\n" 'hard t
'rear-nonsticky
'(hard))
429 "Propertized string representing a hard newline character.")
431 (defun newline (&optional arg
)
432 "Insert a newline, and move to left margin of the new line if it's blank.
433 If `use-hard-newlines' is non-nil, the newline is marked with the
434 text-property `hard'.
435 With ARG, insert that many newlines.
436 Call `auto-fill-function' if the current column number is greater
437 than the value of `fill-column' and ARG is nil."
439 (barf-if-buffer-read-only)
440 ;; Inserting a newline at the end of a line produces better redisplay in
441 ;; try_window_id than inserting at the beginning of a line, and the textual
442 ;; result is the same. So, if we're at beginning of line, pretend to be at
443 ;; the end of the previous line.
444 (let ((flag (and (not (bobp))
446 ;; Make sure no functions want to be told about
447 ;; the range of the changes.
448 (not after-change-functions
)
449 (not before-change-functions
)
450 ;; Make sure there are no markers here.
451 (not (buffer-has-markers-at (1- (point))))
452 (not (buffer-has-markers-at (point)))
453 ;; Make sure no text properties want to know
454 ;; where the change was.
455 (not (get-char-property (1- (point)) 'modification-hooks
))
456 (not (get-char-property (1- (point)) 'insert-behind-hooks
))
458 (not (get-char-property (point) 'insert-in-front-hooks
)))
459 ;; Make sure the newline before point isn't intangible.
460 (not (get-char-property (1- (point)) 'intangible
))
461 ;; Make sure the newline before point isn't read-only.
462 (not (get-char-property (1- (point)) 'read-only
))
463 ;; Make sure the newline before point isn't invisible.
464 (not (get-char-property (1- (point)) 'invisible
))
465 ;; Make sure the newline before point has the same
466 ;; properties as the char before it (if any).
467 (< (or (previous-property-change (point)) -
2)
469 (was-page-start (and (bolp)
470 (looking-at page-delimiter
)))
472 (if flag
(backward-char 1))
473 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
474 ;; Set last-command-event to tell self-insert what to insert.
475 (let ((last-command-event ?
\n)
476 ;; Don't auto-fill if we have a numeric argument.
477 ;; Also not if flag is true (it would fill wrong line);
478 ;; there is no need to since we're at BOL.
479 (auto-fill-function (if (or arg flag
) nil auto-fill-function
)))
481 (self-insert-command (prefix-numeric-value arg
))
482 ;; If we get an error in self-insert-command, put point at right place.
483 (if flag
(forward-char 1))))
484 ;; Even if we did *not* get an error, keep that forward-char;
485 ;; all further processing should apply to the newline that the user
486 ;; thinks he inserted.
488 ;; Mark the newline(s) `hard'.
489 (if use-hard-newlines
490 (set-hard-newline-properties
491 (- (point) (prefix-numeric-value arg
)) (point)))
492 ;; If the newline leaves the previous line blank,
493 ;; and we have a left margin, delete that from the blank line.
496 (goto-char beforepos
)
498 (and (looking-at "[ \t]$")
499 (> (current-left-margin) 0)
500 (delete-region (point) (progn (end-of-line) (point))))))
501 ;; Indent the line after the newline, except in one case:
502 ;; when we added the newline at the beginning of a line
503 ;; which starts a page.
505 (move-to-left-margin nil t
)))
508 (defun set-hard-newline-properties (from to
)
509 (let ((sticky (get-text-property from
'rear-nonsticky
)))
510 (put-text-property from to
'hard
't
)
511 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
512 (if (and (listp sticky
) (not (memq 'hard sticky
)))
513 (put-text-property from
(point) 'rear-nonsticky
514 (cons 'hard sticky
)))))
517 "Insert a newline and leave point before it.
518 If there is a fill prefix and/or a `left-margin', insert them
519 on the new line if the line would have been blank.
520 With arg N, insert N newlines."
522 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
523 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
525 ;; Don't expand an abbrev before point.
531 (if do-left-margin
(indent-to (current-left-margin)))
532 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
538 (defun split-line (&optional arg
)
539 "Split current line, moving portion beyond point vertically down.
540 If the current line starts with `fill-prefix', insert it on the new
541 line as well. With prefix ARG, don't insert `fill-prefix' on new line.
543 When called from Lisp code, ARG may be a prefix string to copy."
545 (skip-chars-forward " \t")
546 (let* ((col (current-column))
548 ;; What prefix should we check for (nil means don't).
549 (prefix (cond ((stringp arg
) arg
)
552 ;; Does this line start with it?
553 (have-prfx (and prefix
556 (looking-at (regexp-quote prefix
))))))
558 (if have-prfx
(insert-and-inherit prefix
))
562 (defun delete-indentation (&optional arg
)
563 "Join this line to previous and fix up whitespace at join.
564 If there is a fill prefix, delete it from the beginning of this line.
565 With argument, join this line to following line."
568 (if arg
(forward-line 1))
569 (if (eq (preceding-char) ?
\n)
571 (delete-region (point) (1- (point)))
572 ;; If the second line started with the fill prefix,
573 ;; delete the prefix.
575 (<= (+ (point) (length fill-prefix
)) (point-max))
577 (buffer-substring (point)
578 (+ (point) (length fill-prefix
)))))
579 (delete-region (point) (+ (point) (length fill-prefix
))))
580 (fixup-whitespace))))
582 (defalias 'join-line
#'delete-indentation
) ; easier to find
584 (defun delete-blank-lines ()
585 "On blank line, delete all surrounding blank lines, leaving just one.
586 On isolated blank line, delete that one.
587 On nonblank line, delete any immediately following blank lines."
589 (let (thisblank singleblank
)
592 (setq thisblank
(looking-at "[ \t]*$"))
593 ;; Set singleblank if there is just one blank line here.
596 (not (looking-at "[ \t]*\n[ \t]*$"))
598 (progn (forward-line -
1)
599 (not (looking-at "[ \t]*$")))))))
600 ;; Delete preceding blank lines, and this one too if it's the only one.
604 (if singleblank
(forward-line 1))
605 (delete-region (point)
606 (if (re-search-backward "[^ \t\n]" nil t
)
607 (progn (forward-line 1) (point))
609 ;; Delete following blank lines, unless the current line is blank
610 ;; and there are no following blank lines.
611 (if (not (and thisblank singleblank
))
615 (delete-region (point)
616 (if (re-search-forward "[^ \t\n]" nil t
)
617 (progn (beginning-of-line) (point))
619 ;; Handle the special case where point is followed by newline and eob.
620 ;; Delete the line, leaving point at eob.
621 (if (looking-at "^[ \t]*\n\\'")
622 (delete-region (point) (point-max)))))
624 (defun delete-trailing-whitespace ()
625 "Delete all the trailing whitespace across the current buffer.
626 All whitespace after the last non-whitespace character in a line is deleted.
627 This respects narrowing, created by \\[narrow-to-region] and friends.
628 A formfeed is not considered whitespace by this function."
632 (goto-char (point-min))
633 (while (re-search-forward "\\s-$" nil t
)
634 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
635 ;; Don't delete formfeeds, even if they are considered whitespace.
637 (if (looking-at ".*\f")
638 (goto-char (match-end 0))))
639 (delete-region (point) (match-end 0))))))
641 (defun newline-and-indent ()
642 "Insert a newline, then indent according to major mode.
643 Indentation is done using the value of `indent-line-function'.
644 In programming language modes, this is the same as TAB.
645 In some text modes, where TAB inserts a tab, this command indents to the
646 column specified by the function `current-left-margin'."
648 (delete-horizontal-space t
)
650 (indent-according-to-mode))
652 (defun reindent-then-newline-and-indent ()
653 "Reindent current line, insert newline, then indent the new line.
654 Indentation of both lines is done according to the current major mode,
655 which means calling the current value of `indent-line-function'.
656 In programming language modes, this is the same as TAB.
657 In some text modes, where TAB inserts a tab, this indents to the
658 column specified by the function `current-left-margin'."
661 ;; Be careful to insert the newline before indenting the line.
662 ;; Otherwise, the indentation might be wrong.
666 ;; We are at EOL before the call to indent-according-to-mode, and
667 ;; after it we usually are as well, but not always. We tried to
668 ;; address it with `save-excursion' but that uses a normal marker
669 ;; whereas we need `move after insertion', so we do the save/restore
671 (setq pos
(copy-marker pos t
))
672 (indent-according-to-mode)
674 ;; Remove the trailing white-space after indentation because
675 ;; indentation may introduce the whitespace.
676 (delete-horizontal-space t
))
677 (indent-according-to-mode)))
679 (defun quoted-insert (arg)
680 "Read next input character and insert it.
681 This is useful for inserting control characters.
682 With argument, insert ARG copies of the character.
684 If the first character you type after this command is an octal digit,
685 you should type a sequence of octal digits which specify a character code.
686 Any nondigit terminates the sequence. If the terminator is a RET,
687 it is discarded; any other terminator is used itself as input.
688 The variable `read-quoted-char-radix' specifies the radix for this feature;
689 set it to 10 or 16 to use decimal or hex instead of octal.
691 In overwrite mode, this function inserts the character anyway, and
692 does not handle octal digits specially. This means that if you use
693 overwrite as your normal editing mode, you can use this function to
694 insert characters when necessary.
696 In binary overwrite mode, this function does overwrite, and octal
697 digits are interpreted as a character code. This is intended to be
698 useful for editing binary files."
701 ;; Avoid "obsolete" warnings for translation-table-for-input.
703 (let (translation-table-for-input input-method-function
)
704 (if (or (not overwrite-mode
)
705 (eq overwrite-mode
'overwrite-mode-binary
))
708 ;; This used to assume character codes 0240 - 0377 stand for
709 ;; characters in some single-byte character set, and converted them
710 ;; to Emacs characters. But in 23.1 this feature is deprecated
711 ;; in favor of inserting the corresponding Unicode characters.
712 ;; (if (and enable-multibyte-characters
715 ;; (setq char (unibyte-char-to-multibyte char)))
717 (if (eq overwrite-mode
'overwrite-mode-binary
)
720 (insert-and-inherit char
)
721 (setq arg
(1- arg
)))))
723 (defun forward-to-indentation (&optional arg
)
724 "Move forward ARG lines and position at first nonblank character."
726 (forward-line (or arg
1))
727 (skip-chars-forward " \t"))
729 (defun backward-to-indentation (&optional arg
)
730 "Move backward ARG lines and position at first nonblank character."
732 (forward-line (- (or arg
1)))
733 (skip-chars-forward " \t"))
735 (defun back-to-indentation ()
736 "Move point to the first non-whitespace character on this line."
738 (beginning-of-line 1)
739 (skip-syntax-forward " " (line-end-position))
740 ;; Move back over chars that have whitespace syntax but have the p flag.
741 (backward-prefix-chars))
743 (defun fixup-whitespace ()
744 "Fixup white space between objects around point.
745 Leave one space or none, according to the context."
748 (delete-horizontal-space)
749 (if (or (looking-at "^\\|\\s)")
750 (save-excursion (forward-char -
1)
751 (looking-at "$\\|\\s(\\|\\s'")))
755 (defun delete-horizontal-space (&optional backward-only
)
756 "Delete all spaces and tabs around point.
757 If BACKWARD-ONLY is non-nil, only delete them before point."
759 (let ((orig-pos (point)))
764 (skip-chars-forward " \t")
765 (constrain-to-field nil orig-pos t
)))
767 (skip-chars-backward " \t")
768 (constrain-to-field nil orig-pos
)))))
770 (defun just-one-space (&optional n
)
771 "Delete all spaces and tabs around point, leaving one space (or N spaces)."
773 (let ((orig-pos (point)))
774 (skip-chars-backward " \t")
775 (constrain-to-field nil orig-pos
)
776 (dotimes (i (or n
1))
777 (if (= (following-char) ?\s
)
783 (skip-chars-forward " \t")
784 (constrain-to-field nil orig-pos t
)))))
786 (defun beginning-of-buffer (&optional arg
)
787 "Move point to the beginning of the buffer; leave mark at previous position.
788 With \\[universal-argument] prefix, do not set mark at previous position.
789 With numeric arg N, put point N/10 of the way from the beginning.
791 If the buffer is narrowed, this command uses the beginning and size
792 of the accessible part of the buffer.
794 Don't use this command in Lisp programs!
795 \(goto-char (point-min)) is faster and avoids clobbering the mark."
800 (let ((size (- (point-max) (point-min))))
801 (goto-char (if (and arg
(not (consp arg
)))
804 ;; Avoid overflow for large buffer sizes!
805 (* (prefix-numeric-value arg
)
807 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
809 (if (and arg
(not (consp arg
))) (forward-line 1)))
811 (defun end-of-buffer (&optional arg
)
812 "Move point to the end of the buffer; leave mark at previous position.
813 With \\[universal-argument] prefix, do not set mark at previous position.
814 With numeric arg N, put point N/10 of the way from the end.
816 If the buffer is narrowed, this command uses the beginning and size
817 of the accessible part of the buffer.
819 Don't use this command in Lisp programs!
820 \(goto-char (point-max)) is faster and avoids clobbering the mark."
822 (or (consp arg
) (region-active-p) (push-mark))
823 (let ((size (- (point-max) (point-min))))
824 (goto-char (if (and arg
(not (consp arg
)))
827 ;; Avoid overflow for large buffer sizes!
828 (* (prefix-numeric-value arg
)
830 (/ (* size
(prefix-numeric-value arg
)) 10)))
832 ;; If we went to a place in the middle of the buffer,
833 ;; adjust it to the beginning of a line.
834 (cond ((and arg
(not (consp arg
))) (forward-line 1))
835 ((> (point) (window-end nil t
))
836 ;; If the end of the buffer is not already on the screen,
837 ;; then scroll specially to put it near, but not at, the bottom.
838 (overlay-recenter (point))
841 (defun mark-whole-buffer ()
842 "Put point at beginning and mark at end of buffer.
843 You probably should not use this function in Lisp programs;
844 it is usually a mistake for a Lisp function to use any subroutine
845 that uses or sets the mark."
848 (push-mark (point-max) nil t
)
849 (goto-char (point-min)))
852 ;; Counting lines, one way or another.
854 (defun goto-line (line &optional buffer
)
855 "Goto LINE, counting from line 1 at beginning of buffer.
856 Normally, move point in the current buffer, and leave mark at the
857 previous position. With just \\[universal-argument] as argument,
858 move point in the most recently selected other buffer, and switch to it.
860 If there's a number in the buffer at point, it is the default for LINE.
862 This function is usually the wrong thing to use in a Lisp program.
863 What you probably want instead is something like:
864 (goto-char (point-min)) (forward-line (1- N))
865 If at all possible, an even better solution is to use char counts
866 rather than line counts."
868 (if (and current-prefix-arg
(not (consp current-prefix-arg
)))
869 (list (prefix-numeric-value current-prefix-arg
))
870 ;; Look for a default, a number in the buffer at point.
873 (skip-chars-backward "0-9")
874 (if (looking-at "[0-9]")
875 (buffer-substring-no-properties
877 (progn (skip-chars-forward "0-9")
879 ;; Decide if we're switching buffers.
881 (if (consp current-prefix-arg
)
882 (other-buffer (current-buffer) t
)))
885 (concat " in " (buffer-name buffer
))
887 ;; Read the argument, offering that number (if any) as default.
888 (list (read-from-minibuffer (format (if default
"Goto line%s (%s): "
896 ;; Switch to the desired buffer, one way or another.
898 (let ((window (get-buffer-window buffer
)))
899 (if window
(select-window window
)
900 (switch-to-buffer-other-window buffer
))))
901 ;; Leave mark at previous position
902 (or (region-active-p) (push-mark))
903 ;; Move to the specified line number in that buffer.
906 (goto-char (point-min))
907 (if (eq selective-display t
)
908 (re-search-forward "[\n\C-m]" nil
'end
(1- line
))
909 (forward-line (1- line
)))))
911 (defun count-lines-region (start end
)
912 "Print number of lines and characters in the region."
914 (message "Region has %d lines, %d characters"
915 (count-lines start end
) (- end start
)))
918 "Print the current buffer line number and narrowed line number of point."
920 (let ((start (point-min))
921 (n (line-number-at-pos)))
923 (message "Line %d" n
)
927 (message "line %d (narrowed line %d)"
928 (+ n
(line-number-at-pos start
) -
1) n
))))))
930 (defun count-lines (start end
)
931 "Return number of lines between START and END.
932 This is usually the number of newlines between them,
933 but can be one more if START is not equal to END
934 and the greater of them is not at the start of a line."
937 (narrow-to-region start end
)
938 (goto-char (point-min))
939 (if (eq selective-display t
)
942 (while (re-search-forward "[\n\C-m]" nil t
40)
943 (setq done
(+ 40 done
)))
944 (while (re-search-forward "[\n\C-m]" nil t
1)
945 (setq done
(+ 1 done
)))
946 (goto-char (point-max))
947 (if (and (/= start end
)
951 (- (buffer-size) (forward-line (buffer-size)))))))
953 (defun line-number-at-pos (&optional pos
)
954 "Return (narrowed) buffer line number at position POS.
955 If POS is nil, use current buffer location.
956 Counting starts at (point-min), so the value refers
957 to the contents of the accessible portion of the buffer."
958 (let ((opoint (or pos
(point))) start
)
960 (goto-char (point-min))
964 (1+ (count-lines start
(point))))))
966 (defun what-cursor-position (&optional detail
)
967 "Print info on cursor position (on screen and within buffer).
968 Also describe the character after point, and give its character code
969 in octal, decimal and hex.
971 For a non-ASCII multibyte character, also give its encoding in the
972 buffer's selected coding system if the coding system encodes the
973 character safely. If the character is encoded into one byte, that
974 code is shown in hex. If the character is encoded into more than one
975 byte, just \"...\" is shown.
977 In addition, with prefix argument, show details about that character
978 in *Help* buffer. See also the command `describe-char'."
980 (let* ((char (following-char))
984 (total (buffer-size))
985 (percent (if (> total
50000)
986 ;; Avoid overflow from multiplying by 100!
987 (/ (+ (/ total
200) (1- pos
)) (max (/ total
100) 1))
988 (/ (+ (/ total
2) (* 100 (1- pos
))) (max total
1))))
989 (hscroll (if (= (window-hscroll) 0)
991 (format " Hscroll=%d" (window-hscroll))))
992 (col (current-column)))
994 (if (or (/= beg
1) (/= end
(1+ total
)))
995 (message "point=%d of %d (%d%%) <%d-%d> column=%d%s"
996 pos total percent beg end col hscroll
)
997 (message "point=%d of %d (EOB) column=%d%s"
998 pos total col hscroll
))
999 (let ((coding buffer-file-coding-system
)
1000 encoded encoding-msg display-prop under-display
)
1001 (if (or (not coding
)
1002 (eq (coding-system-type coding
) t
))
1003 (setq coding
(default-value 'buffer-file-coding-system
)))
1004 (if (eq (char-charset char
) 'eight-bit
)
1006 (format "(%d, #o%o, #x%x, raw-byte)" char char char
))
1007 ;; Check if the character is displayed with some `display'
1008 ;; text property. In that case, set under-display to the
1009 ;; buffer substring covered by that property.
1010 (setq display-prop
(get-text-property pos
'display
))
1012 (let ((to (or (next-single-property-change pos
'display
)
1014 (if (< to
(+ pos
4))
1015 (setq under-display
"")
1016 (setq under-display
"..."
1019 (concat (buffer-substring-no-properties pos to
)
1021 (setq encoded
(and (>= char
128) (encode-coding-char char coding
))))
1024 (if (not (stringp display-prop
))
1025 (format "(%d, #o%o, #x%x, part of display \"%s\")"
1026 char char char under-display
)
1027 (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")"
1028 char char char under-display display-prop
))
1030 (format "(%d, #o%o, #x%x, file %s)"
1032 (if (> (length encoded
) 1)
1034 (encoded-string-description encoded coding
)))
1035 (format "(%d, #o%o, #x%x)" char char char
)))))
1037 ;; We show the detailed information about CHAR.
1038 (describe-char (point)))
1039 (if (or (/= beg
1) (/= end
(1+ total
)))
1040 (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
1042 (single-key-description char
)
1043 (buffer-substring-no-properties (point) (1+ (point))))
1044 encoding-msg pos total percent beg end col hscroll
)
1045 (message "Char: %s %s point=%d of %d (%d%%) column=%d%s"
1046 (if enable-multibyte-characters
1048 (single-key-description char
)
1049 (buffer-substring-no-properties (point) (1+ (point))))
1050 (single-key-description char
))
1051 encoding-msg pos total percent col hscroll
))))))
1053 ;; Initialize read-expression-map. It is defined at C level.
1054 (let ((m (make-sparse-keymap)))
1055 (define-key m
"\M-\t" 'lisp-complete-symbol
)
1056 (set-keymap-parent m minibuffer-local-map
)
1057 (setq read-expression-map m
))
1059 (defvar read-expression-history nil
)
1061 (defvar minibuffer-completing-symbol nil
1062 "Non-nil means completing a Lisp symbol in the minibuffer.")
1064 (defvar minibuffer-default nil
1065 "The current default value or list of default values in the minibuffer.
1066 The functions `read-from-minibuffer' and `completing-read' bind
1067 this variable locally.")
1069 (defcustom eval-expression-print-level
4
1070 "Value for `print-level' while printing value in `eval-expression'.
1071 A value of nil means no limit."
1073 :type
'(choice (const :tag
"No Limit" nil
) integer
)
1076 (defcustom eval-expression-print-length
12
1077 "Value for `print-length' while printing value in `eval-expression'.
1078 A value of nil means no limit."
1080 :type
'(choice (const :tag
"No Limit" nil
) integer
)
1083 (defcustom eval-expression-debug-on-error t
1084 "If non-nil set `debug-on-error' to t in `eval-expression'.
1085 If nil, don't change the value of `debug-on-error'."
1090 (defun eval-expression-print-format (value)
1091 "Format VALUE as a result of evaluated expression.
1092 Return a formatted string which is displayed in the echo area
1093 in addition to the value printed by prin1 in functions which
1094 display the result of expression evaluation."
1095 (if (and (integerp value
)
1096 (or (not (memq this-command
'(eval-last-sexp eval-print-last-sexp
)))
1097 (eq this-command last-command
)
1098 (if (boundp 'edebug-active
) edebug-active
)))
1100 (if (or (if (boundp 'edebug-active
) edebug-active
)
1101 (memq this-command
'(eval-last-sexp eval-print-last-sexp
)))
1102 (prin1-char value
))))
1104 (format " (#o%o, #x%x, %s)" value value char-string
)
1105 (format " (#o%o, #x%x)" value value
)))))
1107 ;; We define this, rather than making `eval' interactive,
1108 ;; for the sake of completion of names like eval-region, eval-buffer.
1109 (defun eval-expression (eval-expression-arg
1110 &optional eval-expression-insert-value
)
1111 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
1112 Value is also consed on to front of the variable `values'.
1113 Optional argument EVAL-EXPRESSION-INSERT-VALUE non-nil (interactively,
1114 with prefix argument) means insert the result into the current buffer
1115 instead of printing it in the echo area. Truncates long output
1116 according to the value of the variables `eval-expression-print-length'
1117 and `eval-expression-print-level'.
1119 If `eval-expression-debug-on-error' is non-nil, which is the default,
1120 this command arranges for all errors to enter the debugger."
1122 (list (let ((minibuffer-completing-symbol t
))
1123 (read-from-minibuffer "Eval: "
1124 nil read-expression-map t
1125 'read-expression-history
))
1126 current-prefix-arg
))
1128 (if (null eval-expression-debug-on-error
)
1129 (setq values
(cons (eval eval-expression-arg
) values
))
1130 (let ((old-value (make-symbol "t")) new-value
)
1131 ;; Bind debug-on-error to something unique so that we can
1132 ;; detect when evaled code changes it.
1133 (let ((debug-on-error old-value
))
1134 (setq values
(cons (eval eval-expression-arg
) values
))
1135 (setq new-value debug-on-error
))
1136 ;; If evaled code has changed the value of debug-on-error,
1137 ;; propagate that change to the global binding.
1138 (unless (eq old-value new-value
)
1139 (setq debug-on-error new-value
))))
1141 (let ((print-length eval-expression-print-length
)
1142 (print-level eval-expression-print-level
))
1143 (if eval-expression-insert-value
1145 (let ((standard-output (current-buffer)))
1146 (prin1 (car values
))))
1148 (prin1 (car values
) t
)
1149 (let ((str (eval-expression-print-format (car values
))))
1150 (if str
(princ str t
)))))))
1152 (defun edit-and-eval-command (prompt command
)
1153 "Prompting with PROMPT, let user edit COMMAND and eval result.
1154 COMMAND is a Lisp expression. Let user edit that expression in
1155 the minibuffer, then read and evaluate the result."
1157 (let ((print-level nil
)
1158 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1160 (read-from-minibuffer prompt
1161 (prin1-to-string command
)
1162 read-expression-map t
1164 ;; If command was added to command-history as a string,
1165 ;; get rid of that. We want only evaluable expressions there.
1166 (if (stringp (car command-history
))
1167 (setq command-history
(cdr command-history
)))))))
1169 ;; If command to be redone does not match front of history,
1170 ;; add it to the history.
1171 (or (equal command
(car command-history
))
1172 (setq command-history
(cons command command-history
)))
1175 (defun repeat-complex-command (arg)
1176 "Edit and re-evaluate last complex command, or ARGth from last.
1177 A complex command is one which used the minibuffer.
1178 The command is placed in the minibuffer as a Lisp form for editing.
1179 The result is executed, repeating the command as changed.
1180 If the command has been changed or is not the most recent previous
1181 command it is added to the front of the command history.
1182 You can use the minibuffer history commands \
1183 \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
1184 to get different commands to edit and resubmit."
1186 (let ((elt (nth (1- arg
) command-history
))
1191 (let ((print-level nil
)
1192 (minibuffer-history-position arg
)
1193 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1195 (read-from-minibuffer
1196 "Redo: " (prin1-to-string elt
) read-expression-map t
1197 (cons 'command-history arg
))
1199 ;; If command was added to command-history as a
1200 ;; string, get rid of that. We want only
1201 ;; evaluable expressions there.
1202 (if (stringp (car command-history
))
1203 (setq command-history
(cdr command-history
))))))
1205 ;; If command to be redone does not match front of history,
1206 ;; add it to the history.
1207 (or (equal newcmd
(car command-history
))
1208 (setq command-history
(cons newcmd command-history
)))
1211 (error "Argument %d is beyond length of command history" arg
)
1212 (error "There are no previous complex commands to repeat")))))
1214 (defvar minibuffer-history nil
1215 "Default minibuffer history list.
1216 This is used for all minibuffer input
1217 except when an alternate history list is specified.
1219 Maximum length of the history list is determined by the value
1220 of `history-length', which see.")
1221 (defvar minibuffer-history-sexp-flag nil
1222 "Control whether history list elements are expressions or strings.
1223 If the value of this variable equals current minibuffer depth,
1224 they are expressions; otherwise they are strings.
1225 \(That convention is designed to do the right thing for
1226 recursive uses of the minibuffer.)")
1227 (setq minibuffer-history-variable
'minibuffer-history
)
1228 (setq minibuffer-history-position nil
) ;; Defvar is in C code.
1229 (defvar minibuffer-history-search-history nil
)
1231 (defvar minibuffer-text-before-history nil
1232 "Text that was in this minibuffer before any history commands.
1233 This is nil if there have not yet been any history commands
1234 in this use of the minibuffer.")
1236 (add-hook 'minibuffer-setup-hook
'minibuffer-history-initialize
)
1238 (defun minibuffer-history-initialize ()
1239 (setq minibuffer-text-before-history nil
))
1241 (defun minibuffer-avoid-prompt (new old
)
1242 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1243 (constrain-to-field nil
(point-max)))
1245 (defcustom minibuffer-history-case-insensitive-variables nil
1246 "Minibuffer history variables for which matching should ignore case.
1247 If a history variable is a member of this list, then the
1248 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1249 commands ignore case when searching it, regardless of `case-fold-search'."
1250 :type
'(repeat variable
)
1253 (defun previous-matching-history-element (regexp n
)
1254 "Find the previous history element that matches REGEXP.
1255 \(Previous history elements refer to earlier actions.)
1256 With prefix argument N, search for Nth previous match.
1257 If N is negative, find the next or Nth next match.
1258 Normally, history elements are matched case-insensitively if
1259 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1260 makes the search case-sensitive.
1261 See also `minibuffer-history-case-insensitive-variables'."
1263 (let* ((enable-recursive-minibuffers t
)
1264 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1266 minibuffer-local-map
1268 'minibuffer-history-search-history
1269 (car minibuffer-history-search-history
))))
1270 ;; Use the last regexp specified, by default, if input is empty.
1271 (list (if (string= regexp
"")
1272 (if minibuffer-history-search-history
1273 (car minibuffer-history-search-history
)
1274 (error "No previous history search regexp"))
1276 (prefix-numeric-value current-prefix-arg
))))
1278 (if (and (zerop minibuffer-history-position
)
1279 (null minibuffer-text-before-history
))
1280 (setq minibuffer-text-before-history
1281 (minibuffer-contents-no-properties)))
1282 (let ((history (symbol-value minibuffer-history-variable
))
1284 (if (isearch-no-upper-case-p regexp t
) ; assume isearch.el is dumped
1285 ;; On some systems, ignore case for file names.
1286 (if (memq minibuffer-history-variable
1287 minibuffer-history-case-insensitive-variables
)
1289 ;; Respect the user's setting for case-fold-search:
1295 (pos minibuffer-history-position
))
1298 (setq pos
(min (max 1 (+ pos
(if (< n
0) -
1 1))) (length history
)))
1299 (when (= pos prevpos
)
1300 (error (if (= pos
1)
1301 "No later matching history item"
1302 "No earlier matching history item")))
1304 (if (eq minibuffer-history-sexp-flag
(minibuffer-depth))
1305 (let ((print-level nil
))
1306 (prin1-to-string (nth (1- pos
) history
)))
1307 (nth (1- pos
) history
)))
1310 (and (string-match regexp match-string
)
1312 (and (string-match (concat ".*\\(" regexp
"\\)") match-string
)
1313 (match-beginning 1))))
1315 (setq n
(+ n
(if (< n
0) 1 -
1)))))
1316 (setq minibuffer-history-position pos
)
1317 (goto-char (point-max))
1318 (delete-minibuffer-contents)
1319 (insert match-string
)
1320 (goto-char (+ (minibuffer-prompt-end) match-offset
))))
1321 (if (memq (car (car command-history
)) '(previous-matching-history-element
1322 next-matching-history-element
))
1323 (setq command-history
(cdr command-history
))))
1325 (defun next-matching-history-element (regexp n
)
1326 "Find the next history element that matches REGEXP.
1327 \(The next history element refers to a more recent action.)
1328 With prefix argument N, search for Nth next match.
1329 If N is negative, find the previous or Nth previous match.
1330 Normally, history elements are matched case-insensitively if
1331 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1332 makes the search case-sensitive."
1334 (let* ((enable-recursive-minibuffers t
)
1335 (regexp (read-from-minibuffer "Next element matching (regexp): "
1337 minibuffer-local-map
1339 'minibuffer-history-search-history
1340 (car minibuffer-history-search-history
))))
1341 ;; Use the last regexp specified, by default, if input is empty.
1342 (list (if (string= regexp
"")
1343 (if minibuffer-history-search-history
1344 (car minibuffer-history-search-history
)
1345 (error "No previous history search regexp"))
1347 (prefix-numeric-value current-prefix-arg
))))
1348 (previous-matching-history-element regexp
(- n
)))
1350 (defvar minibuffer-temporary-goal-position nil
)
1352 (defvar minibuffer-default-add-function
'minibuffer-default-add-completions
1353 "Function run by `goto-history-element' before consuming default values.
1354 This is useful to dynamically add more elements to the list of default values
1355 when `goto-history-element' reaches the end of this list.
1356 Before calling this function `goto-history-element' sets the variable
1357 `minibuffer-default-add-done' to t, so it will call this function only
1358 once. In special cases, when this function needs to be called more
1359 than once, it can set `minibuffer-default-add-done' to nil explicitly,
1360 overriding the setting of this variable to t in `goto-history-element'.")
1362 (defvar minibuffer-default-add-done nil
1363 "When nil, add more elements to the end of the list of default values.
1364 The value nil causes `goto-history-element' to add more elements to
1365 the list of defaults when it reaches the end of this list. It does
1366 this by calling a function defined by `minibuffer-default-add-function'.")
1368 (make-variable-buffer-local 'minibuffer-default-add-done
)
1370 (defun minibuffer-default-add-completions ()
1371 "Return a list of all completions without the default value.
1372 This function is used to add all elements of the completion table to
1373 the end of the list of defaults just after the default value."
1374 (let ((def minibuffer-default
)
1375 (all (all-completions ""
1376 minibuffer-completion-table
1377 minibuffer-completion-predicate
)))
1380 (cons def
(delete def all
)))))
1382 (defun goto-history-element (nabs)
1383 "Puts element of the minibuffer history in the minibuffer.
1384 The argument NABS specifies the absolute history position."
1386 (when (and (not minibuffer-default-add-done
)
1387 (functionp minibuffer-default-add-function
)
1388 (< nabs
(- (if (listp minibuffer-default
)
1389 (length minibuffer-default
)
1391 (setq minibuffer-default-add-done t
1392 minibuffer-default
(funcall minibuffer-default-add-function
)))
1393 (let ((minimum (if minibuffer-default
1394 (- (if (listp minibuffer-default
)
1395 (length minibuffer-default
)
1398 elt minibuffer-returned-to-present
)
1399 (if (and (zerop minibuffer-history-position
)
1400 (null minibuffer-text-before-history
))
1401 (setq minibuffer-text-before-history
1402 (minibuffer-contents-no-properties)))
1403 (if (< nabs minimum
)
1404 (if minibuffer-default
1405 (error "End of defaults; no next item")
1406 (error "End of history; no default available")))
1407 (if (> nabs
(length (symbol-value minibuffer-history-variable
)))
1408 (error "Beginning of history; no preceding item"))
1409 (unless (memq last-command
'(next-history-element
1410 previous-history-element
))
1411 (let ((prompt-end (minibuffer-prompt-end)))
1412 (set (make-local-variable 'minibuffer-temporary-goal-position
)
1413 (cond ((<= (point) prompt-end
) prompt-end
)
1416 (goto-char (point-max))
1417 (delete-minibuffer-contents)
1418 (setq minibuffer-history-position nabs
)
1420 (setq elt
(if (listp minibuffer-default
)
1421 (nth (1- (abs nabs
)) minibuffer-default
)
1422 minibuffer-default
)))
1424 (setq elt
(or minibuffer-text-before-history
""))
1425 (setq minibuffer-returned-to-present t
)
1426 (setq minibuffer-text-before-history nil
))
1427 (t (setq elt
(nth (1- minibuffer-history-position
)
1428 (symbol-value minibuffer-history-variable
)))))
1430 (if (and (eq minibuffer-history-sexp-flag
(minibuffer-depth))
1431 (not minibuffer-returned-to-present
))
1432 (let ((print-level nil
))
1433 (prin1-to-string elt
))
1435 (goto-char (or minibuffer-temporary-goal-position
(point-max)))))
1437 (defun next-history-element (n)
1438 "Puts next element of the minibuffer history in the minibuffer.
1439 With argument N, it uses the Nth following element."
1442 (goto-history-element (- minibuffer-history-position n
))))
1444 (defun previous-history-element (n)
1445 "Puts previous element of the minibuffer history in the minibuffer.
1446 With argument N, it uses the Nth previous element."
1449 (goto-history-element (+ minibuffer-history-position n
))))
1451 (defun next-complete-history-element (n)
1452 "Get next history element which completes the minibuffer before the point.
1453 The contents of the minibuffer after the point are deleted, and replaced
1454 by the new completion."
1456 (let ((point-at-start (point)))
1457 (next-matching-history-element
1459 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1461 ;; next-matching-history-element always puts us at (point-min).
1462 ;; Move to the position we were at before changing the buffer contents.
1463 ;; This is still sensical, because the text before point has not changed.
1464 (goto-char point-at-start
)))
1466 (defun previous-complete-history-element (n)
1468 Get previous history element which completes the minibuffer before the point.
1469 The contents of the minibuffer after the point are deleted, and replaced
1470 by the new completion."
1472 (next-complete-history-element (- n
)))
1474 ;; For compatibility with the old subr of the same name.
1475 (defun minibuffer-prompt-width ()
1476 "Return the display width of the minibuffer prompt.
1477 Return 0 if current buffer is not a minibuffer."
1478 ;; Return the width of everything before the field at the end of
1479 ;; the buffer; this should be 0 for normal buffers.
1480 (1- (minibuffer-prompt-end)))
1482 ;; isearch minibuffer history
1483 (add-hook 'minibuffer-setup-hook
'minibuffer-history-isearch-setup
)
1485 (defvar minibuffer-history-isearch-message-overlay
)
1486 (make-variable-buffer-local 'minibuffer-history-isearch-message-overlay
)
1488 (defun minibuffer-history-isearch-setup ()
1489 "Set up a minibuffer for using isearch to search the minibuffer history.
1490 Intended to be added to `minibuffer-setup-hook'."
1491 (set (make-local-variable 'isearch-search-fun-function
)
1492 'minibuffer-history-isearch-search
)
1493 (set (make-local-variable 'isearch-message-function
)
1494 'minibuffer-history-isearch-message
)
1495 (set (make-local-variable 'isearch-wrap-function
)
1496 'minibuffer-history-isearch-wrap
)
1497 (set (make-local-variable 'isearch-push-state-function
)
1498 'minibuffer-history-isearch-push-state
)
1499 (add-hook 'isearch-mode-end-hook
'minibuffer-history-isearch-end nil t
))
1501 (defun minibuffer-history-isearch-end ()
1502 "Clean up the minibuffer after terminating isearch in the minibuffer."
1503 (if minibuffer-history-isearch-message-overlay
1504 (delete-overlay minibuffer-history-isearch-message-overlay
)))
1506 (defun minibuffer-history-isearch-search ()
1507 "Return the proper search function, for isearch in minibuffer history."
1510 (if isearch-forward
'word-search-forward
'word-search-backward
))
1512 (lambda (string bound noerror
)
1514 ;; Use standard functions to search within minibuffer text
1517 (if isearch-forward
're-search-forward
're-search-backward
))
1519 (if isearch-forward
'search-forward
'search-backward
))))
1521 ;; Avoid lazy-highlighting matches in the minibuffer prompt when
1522 ;; searching forward. Lazy-highlight calls this lambda with the
1523 ;; bound arg, so skip the minibuffer prompt.
1524 (if (and bound isearch-forward
(< (point) (minibuffer-prompt-end)))
1525 (goto-char (minibuffer-prompt-end)))
1527 ;; 1. First try searching in the initial minibuffer text
1528 (funcall search-fun string
1529 (if isearch-forward bound
(minibuffer-prompt-end))
1531 ;; 2. If the above search fails, start putting next/prev history
1532 ;; elements in the minibuffer successively, and search the string
1533 ;; in them. Do this only when bound is nil (i.e. not while
1534 ;; lazy-highlighting search strings in the current minibuffer text).
1539 (cond (isearch-forward
1540 (next-history-element 1)
1541 (goto-char (minibuffer-prompt-end)))
1543 (previous-history-element 1)
1544 (goto-char (point-max))))
1545 (setq isearch-barrier
(point) isearch-opoint
(point))
1546 ;; After putting the next/prev history element, search
1547 ;; the string in them again, until next-history-element
1548 ;; or previous-history-element raises an error at the
1549 ;; beginning/end of history.
1550 (setq found
(funcall search-fun string
1551 (unless isearch-forward
1552 ;; For backward search, don't search
1553 ;; in the minibuffer prompt
1554 (minibuffer-prompt-end))
1556 ;; Return point of the new search result
1558 ;; Return nil when next(prev)-history-element fails
1561 (defun minibuffer-history-isearch-message (&optional c-q-hack ellipsis
)
1562 "Display the minibuffer history search prompt.
1563 If there are no search errors, this function displays an overlay with
1564 the isearch prompt which replaces the original minibuffer prompt.
1565 Otherwise, it displays the standard isearch message returned from
1567 (if (not (and (minibufferp) isearch-success
(not isearch-error
)))
1568 ;; Use standard function `isearch-message' when not in the minibuffer,
1569 ;; or search fails, or has an error (like incomplete regexp).
1570 ;; This function overwrites minibuffer text with isearch message,
1571 ;; so it's possible to see what is wrong in the search string.
1572 (isearch-message c-q-hack ellipsis
)
1573 ;; Otherwise, put the overlay with the standard isearch prompt over
1574 ;; the initial minibuffer prompt.
1575 (if (overlayp minibuffer-history-isearch-message-overlay
)
1576 (move-overlay minibuffer-history-isearch-message-overlay
1577 (point-min) (minibuffer-prompt-end))
1578 (setq minibuffer-history-isearch-message-overlay
1579 (make-overlay (point-min) (minibuffer-prompt-end)))
1580 (overlay-put minibuffer-history-isearch-message-overlay
'evaporate t
))
1581 (overlay-put minibuffer-history-isearch-message-overlay
1582 'display
(isearch-message-prefix c-q-hack ellipsis
))
1583 ;; And clear any previous isearch message.
1586 (defun minibuffer-history-isearch-wrap ()
1587 "Wrap the minibuffer history search when search fails.
1588 Move point to the first history element for a forward search,
1589 or to the last history element for a backward search."
1590 (unless isearch-word
1591 ;; When `minibuffer-history-isearch-search' fails on reaching the
1592 ;; beginning/end of the history, wrap the search to the first/last
1593 ;; minibuffer history element.
1595 (goto-history-element (length (symbol-value minibuffer-history-variable
)))
1596 (goto-history-element 0))
1597 (setq isearch-success t
))
1598 (goto-char (if isearch-forward
(minibuffer-prompt-end) (point-max))))
1600 (defun minibuffer-history-isearch-push-state ()
1601 "Save a function restoring the state of minibuffer history search.
1602 Save `minibuffer-history-position' to the additional state parameter
1603 in the search status stack."
1605 (minibuffer-history-isearch-pop-state cmd
,minibuffer-history-position
)))
1607 (defun minibuffer-history-isearch-pop-state (cmd hist-pos
)
1608 "Restore the minibuffer history search state.
1609 Go to the history element by the absolute history position HIST-POS."
1610 (goto-history-element hist-pos
))
1613 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1614 (define-obsolete-function-alias 'advertised-undo
'undo
"23.2")
1616 (defconst undo-equiv-table
(make-hash-table :test
'eq
:weakness t
)
1617 "Table mapping redo records to the corresponding undo one.
1618 A redo record for undo-in-region maps to t.
1619 A redo record for ordinary undo maps to the following (earlier) undo.")
1621 (defvar undo-in-region nil
1622 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1624 (defvar undo-no-redo nil
1625 "If t, `undo' doesn't go through redo entries.")
1627 (defvar pending-undo-list nil
1628 "Within a run of consecutive undo commands, list remaining to be undone.
1629 If t, we undid all the way to the end of it.")
1631 (defun undo (&optional arg
)
1632 "Undo some previous changes.
1633 Repeat this command to undo more changes.
1634 A numeric ARG serves as a repeat count.
1636 In Transient Mark mode when the mark is active, only undo changes within
1637 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1638 as an argument limits undo to changes within the current region."
1640 ;; Make last-command indicate for the next command that this was an undo.
1641 ;; That way, another undo will undo more.
1642 ;; If we get to the end of the undo history and get an error,
1643 ;; another undo command will find the undo history empty
1644 ;; and will get another error. To begin undoing the undos,
1645 ;; you must type some other command.
1646 (let ((modified (buffer-modified-p))
1647 (recent-save (recent-auto-save-p))
1649 ;; If we get an error in undo-start,
1650 ;; the next command should not be a "consecutive undo".
1651 ;; So set `this-command' to something other than `undo'.
1652 (setq this-command
'undo-start
)
1654 (unless (and (eq last-command
'undo
)
1655 (or (eq pending-undo-list t
)
1656 ;; If something (a timer or filter?) changed the buffer
1657 ;; since the previous command, don't continue the undo seq.
1658 (let ((list buffer-undo-list
))
1659 (while (eq (car list
) nil
)
1660 (setq list
(cdr list
)))
1661 ;; If the last undo record made was made by undo
1662 ;; it shows nothing else happened in between.
1663 (gethash list undo-equiv-table
))))
1664 (setq undo-in-region
1665 (or (region-active-p) (and arg
(not (numberp arg
)))))
1667 (undo-start (region-beginning) (region-end))
1669 ;; get rid of initial undo boundary
1671 ;; If we got this far, the next command should be a consecutive undo.
1672 (setq this-command
'undo
)
1673 ;; Check to see whether we're hitting a redo record, and if
1674 ;; so, ask the user whether she wants to skip the redo/undo pair.
1675 (let ((equiv (gethash pending-undo-list undo-equiv-table
)))
1676 (or (eq (selected-window) (minibuffer-window))
1677 (setq message
(if undo-in-region
1678 (if equiv
"Redo in region!" "Undo in region!")
1679 (if equiv
"Redo!" "Undo!"))))
1680 (when (and (consp equiv
) undo-no-redo
)
1681 ;; The equiv entry might point to another redo record if we have done
1682 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1683 (while (let ((next (gethash equiv undo-equiv-table
)))
1684 (if next
(setq equiv next
))))
1685 (setq pending-undo-list equiv
)))
1688 (prefix-numeric-value arg
)
1690 ;; Record the fact that the just-generated undo records come from an
1691 ;; undo operation--that is, they are redo records.
1692 ;; In the ordinary case (not within a region), map the redo
1693 ;; record to the following undos.
1694 ;; I don't know how to do that in the undo-in-region case.
1695 (let ((list buffer-undo-list
))
1696 ;; Strip any leading undo boundaries there might be, like we do
1697 ;; above when checking.
1698 (while (eq (car list
) nil
)
1699 (setq list
(cdr list
)))
1700 (puthash list
(if undo-in-region t pending-undo-list
)
1702 ;; Don't specify a position in the undo record for the undo command.
1703 ;; Instead, undoing this should move point to where the change is.
1704 (let ((tail buffer-undo-list
)
1707 (when (integerp (car tail
))
1708 (let ((pos (car tail
)))
1710 (setcdr prev
(cdr tail
))
1711 (setq buffer-undo-list
(cdr tail
)))
1712 (setq tail
(cdr tail
))
1714 (if (eq pos
(car tail
))
1716 (setcdr prev
(cdr tail
))
1717 (setq buffer-undo-list
(cdr tail
)))
1719 (setq tail
(cdr tail
)))
1721 (setq prev tail tail
(cdr tail
))))
1722 ;; Record what the current undo list says,
1723 ;; so the next command can tell if the buffer was modified in between.
1724 (and modified
(not (buffer-modified-p))
1725 (delete-auto-save-file-if-necessary recent-save
))
1726 ;; Display a message announcing success.
1728 (message "%s" message
))))
1730 (defun buffer-disable-undo (&optional buffer
)
1731 "Make BUFFER stop keeping undo information.
1732 No argument or nil as argument means do this for the current buffer."
1734 (with-current-buffer (if buffer
(get-buffer buffer
) (current-buffer))
1735 (setq buffer-undo-list t
)))
1737 (defun undo-only (&optional arg
)
1738 "Undo some previous changes.
1739 Repeat this command to undo more changes.
1740 A numeric ARG serves as a repeat count.
1741 Contrary to `undo', this will not redo a previous undo."
1743 (let ((undo-no-redo t
)) (undo arg
)))
1745 (defvar undo-in-progress nil
1746 "Non-nil while performing an undo.
1747 Some change-hooks test this variable to do something different.")
1749 (defun undo-more (n)
1750 "Undo back N undo-boundaries beyond what was already undone recently.
1751 Call `undo-start' to get ready to undo recent changes,
1752 then call `undo-more' one or more times to undo them."
1753 (or (listp pending-undo-list
)
1754 (error (concat "No further undo information"
1755 (and undo-in-region
" for region"))))
1756 (let ((undo-in-progress t
))
1757 ;; Note: The following, while pulling elements off
1758 ;; `pending-undo-list' will call primitive change functions which
1759 ;; will push more elements onto `buffer-undo-list'.
1760 (setq pending-undo-list
(primitive-undo n pending-undo-list
))
1761 (if (null pending-undo-list
)
1762 (setq pending-undo-list t
))))
1764 ;; Deep copy of a list
1765 (defun undo-copy-list (list)
1766 "Make a copy of undo list LIST."
1767 (mapcar 'undo-copy-list-1 list
))
1769 (defun undo-copy-list-1 (elt)
1771 (cons (car elt
) (undo-copy-list-1 (cdr elt
)))
1774 (defun undo-start (&optional beg end
)
1775 "Set `pending-undo-list' to the front of the undo list.
1776 The next call to `undo-more' will undo the most recently made change.
1777 If BEG and END are specified, then only undo elements
1778 that apply to text between BEG and END are used; other undo elements
1779 are ignored. If BEG and END are nil, all undo elements are used."
1780 (if (eq buffer-undo-list t
)
1781 (error "No undo information in this buffer"))
1782 (setq pending-undo-list
1783 (if (and beg end
(not (= beg end
)))
1784 (undo-make-selective-list (min beg end
) (max beg end
))
1787 (defvar undo-adjusted-markers
)
1789 (defun undo-make-selective-list (start end
)
1790 "Return a list of undo elements for the region START to END.
1791 The elements come from `buffer-undo-list', but we keep only
1792 the elements inside this region, and discard those outside this region.
1793 If we find an element that crosses an edge of this region,
1794 we stop and ignore all further elements."
1795 (let ((undo-list-copy (undo-copy-list buffer-undo-list
))
1796 (undo-list (list nil
))
1797 undo-adjusted-markers
1799 undo-elt undo-elt temp-undo-list delta
)
1800 (while undo-list-copy
1801 (setq undo-elt
(car undo-list-copy
))
1803 (cond ((and (consp undo-elt
) (eq (car undo-elt
) t
))
1804 ;; This is a "was unmodified" element.
1805 ;; Keep it if we have kept everything thus far.
1806 (not some-rejected
))
1808 (undo-elt-in-region undo-elt start end
)))))
1811 (setq end
(+ end
(cdr (undo-delta undo-elt
))))
1812 ;; Don't put two nils together in the list
1813 (if (not (and (eq (car undo-list
) nil
)
1815 (setq undo-list
(cons undo-elt undo-list
))))
1816 (if (undo-elt-crosses-region undo-elt start end
)
1817 (setq undo-list-copy nil
)
1818 (setq some-rejected t
)
1819 (setq temp-undo-list
(cdr undo-list-copy
))
1820 (setq delta
(undo-delta undo-elt
))
1822 (when (/= (cdr delta
) 0)
1823 (let ((position (car delta
))
1824 (offset (cdr delta
)))
1826 ;; Loop down the earlier events adjusting their buffer
1827 ;; positions to reflect the fact that a change to the buffer
1828 ;; isn't being undone. We only need to process those element
1829 ;; types which undo-elt-in-region will return as being in
1830 ;; the region since only those types can ever get into the
1833 (while temp-undo-list
1834 (setq undo-elt
(car temp-undo-list
))
1835 (cond ((integerp undo-elt
)
1836 (if (>= undo-elt position
)
1837 (setcar temp-undo-list
(- undo-elt offset
))))
1838 ((atom undo-elt
) nil
)
1839 ((stringp (car undo-elt
))
1840 ;; (TEXT . POSITION)
1841 (let ((text-pos (abs (cdr undo-elt
)))
1842 (point-at-end (< (cdr undo-elt
) 0 )))
1843 (if (>= text-pos position
)
1844 (setcdr undo-elt
(* (if point-at-end -
1 1)
1845 (- text-pos offset
))))))
1846 ((integerp (car undo-elt
))
1848 (when (>= (car undo-elt
) position
)
1849 (setcar undo-elt
(- (car undo-elt
) offset
))
1850 (setcdr undo-elt
(- (cdr undo-elt
) offset
))))
1851 ((null (car undo-elt
))
1852 ;; (nil PROPERTY VALUE BEG . END)
1853 (let ((tail (nthcdr 3 undo-elt
)))
1854 (when (>= (car tail
) position
)
1855 (setcar tail
(- (car tail
) offset
))
1856 (setcdr tail
(- (cdr tail
) offset
))))))
1857 (setq temp-undo-list
(cdr temp-undo-list
))))))))
1858 (setq undo-list-copy
(cdr undo-list-copy
)))
1859 (nreverse undo-list
)))
1861 (defun undo-elt-in-region (undo-elt start end
)
1862 "Determine whether UNDO-ELT falls inside the region START ... END.
1863 If it crosses the edge, we return nil."
1864 (cond ((integerp undo-elt
)
1865 (and (>= undo-elt start
)
1871 ((stringp (car undo-elt
))
1872 ;; (TEXT . POSITION)
1873 (and (>= (abs (cdr undo-elt
)) start
)
1874 (< (abs (cdr undo-elt
)) end
)))
1875 ((and (consp undo-elt
) (markerp (car undo-elt
)))
1876 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1877 ;; See if MARKER is inside the region.
1878 (let ((alist-elt (assq (car undo-elt
) undo-adjusted-markers
)))
1880 (setq alist-elt
(cons (car undo-elt
)
1881 (marker-position (car undo-elt
))))
1882 (setq undo-adjusted-markers
1883 (cons alist-elt undo-adjusted-markers
)))
1884 (and (cdr alist-elt
)
1885 (>= (cdr alist-elt
) start
)
1886 (<= (cdr alist-elt
) end
))))
1887 ((null (car undo-elt
))
1888 ;; (nil PROPERTY VALUE BEG . END)
1889 (let ((tail (nthcdr 3 undo-elt
)))
1890 (and (>= (car tail
) start
)
1891 (<= (cdr tail
) end
))))
1892 ((integerp (car undo-elt
))
1894 (and (>= (car undo-elt
) start
)
1895 (<= (cdr undo-elt
) end
)))))
1897 (defun undo-elt-crosses-region (undo-elt start end
)
1898 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1899 This assumes we have already decided that UNDO-ELT
1900 is not *inside* the region START...END."
1901 (cond ((atom undo-elt
) nil
)
1902 ((null (car undo-elt
))
1903 ;; (nil PROPERTY VALUE BEG . END)
1904 (let ((tail (nthcdr 3 undo-elt
)))
1905 (and (< (car tail
) end
)
1906 (> (cdr tail
) start
))))
1907 ((integerp (car undo-elt
))
1909 (and (< (car undo-elt
) end
)
1910 (> (cdr undo-elt
) start
)))))
1912 ;; Return the first affected buffer position and the delta for an undo element
1913 ;; delta is defined as the change in subsequent buffer positions if we *did*
1915 (defun undo-delta (undo-elt)
1916 (if (consp undo-elt
)
1917 (cond ((stringp (car undo-elt
))
1918 ;; (TEXT . POSITION)
1919 (cons (abs (cdr undo-elt
)) (length (car undo-elt
))))
1920 ((integerp (car undo-elt
))
1922 (cons (car undo-elt
) (- (car undo-elt
) (cdr undo-elt
))))
1927 (defcustom undo-ask-before-discard nil
1928 "If non-nil ask about discarding undo info for the current command.
1929 Normally, Emacs discards the undo info for the current command if
1930 it exceeds `undo-outer-limit'. But if you set this option
1931 non-nil, it asks in the echo area whether to discard the info.
1932 If you answer no, there is a slight risk that Emacs might crash, so
1933 only do it if you really want to undo the command.
1935 This option is mainly intended for debugging. You have to be
1936 careful if you use it for other purposes. Garbage collection is
1937 inhibited while the question is asked, meaning that Emacs might
1938 leak memory. So you should make sure that you do not wait
1939 excessively long before answering the question."
1944 (defvar undo-extra-outer-limit nil
1945 "If non-nil, an extra level of size that's ok in an undo item.
1946 We don't ask the user about truncating the undo list until the
1947 current item gets bigger than this amount.
1949 This variable only matters if `undo-ask-before-discard' is non-nil.")
1950 (make-variable-buffer-local 'undo-extra-outer-limit
)
1952 ;; When the first undo batch in an undo list is longer than
1953 ;; undo-outer-limit, this function gets called to warn the user that
1954 ;; the undo info for the current command was discarded. Garbage
1955 ;; collection is inhibited around the call, so it had better not do a
1957 (setq undo-outer-limit-function
'undo-outer-limit-truncate
)
1958 (defun undo-outer-limit-truncate (size)
1959 (if undo-ask-before-discard
1960 (when (or (null undo-extra-outer-limit
)
1961 (> size undo-extra-outer-limit
))
1962 ;; Don't ask the question again unless it gets even bigger.
1963 ;; This applies, in particular, if the user quits from the question.
1964 ;; Such a quit quits out of GC, but something else will call GC
1965 ;; again momentarily. It will call this function again,
1966 ;; but we don't want to ask the question again.
1967 (setq undo-extra-outer-limit
(+ size
50000))
1968 (if (let (use-dialog-box track-mouse executing-kbd-macro
)
1969 (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? "
1970 (buffer-name) size
)))
1971 (progn (setq buffer-undo-list nil
)
1972 (setq undo-extra-outer-limit nil
)
1975 (display-warning '(undo discard-info
)
1977 (format "Buffer `%s' undo info was %d bytes long.\n"
1979 "The undo info was discarded because it exceeded \
1982 This is normal if you executed a command that made a huge change
1983 to the buffer. In that case, to prevent similar problems in the
1984 future, set `undo-outer-limit' to a value that is large enough to
1985 cover the maximum size of normal changes you expect a single
1986 command to make, but not so large that it might exceed the
1987 maximum memory allotted to Emacs.
1989 If you did not execute any such command, the situation is
1990 probably due to a bug and you should report it.
1992 You can disable the popping up of this buffer by adding the entry
1993 \(undo discard-info) to the user option `warning-suppress-types',
1994 which is defined in the `warnings' library.\n")
1996 (setq buffer-undo-list nil
)
1999 (defvar shell-command-history nil
2000 "History list for some commands that read shell commands.
2002 Maximum length of the history list is determined by the value
2003 of `history-length', which see.")
2005 (defvar shell-command-switch
(purecopy "-c")
2006 "Switch used to have the shell execute its command line argument.")
2008 (defvar shell-command-default-error-buffer nil
2009 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
2010 This buffer is used when `shell-command' or `shell-command-on-region'
2011 is run interactively. A value of nil means that output to stderr and
2012 stdout will be intermixed in the output stream.")
2014 (declare-function mailcap-file-default-commands
"mailcap" (files))
2015 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2017 (defun minibuffer-default-add-shell-commands ()
2018 "Return a list of all commands associated with the current file.
2019 This function is used to add all related commands retrieved by `mailcap'
2020 to the end of the list of defaults just after the default value."
2022 (let* ((filename (if (listp minibuffer-default
)
2023 (car minibuffer-default
)
2024 minibuffer-default
))
2025 (commands (and filename
(require 'mailcap nil t
)
2026 (mailcap-file-default-commands (list filename
)))))
2027 (setq commands
(mapcar (lambda (command)
2028 (concat command
" " filename
))
2030 (if (listp minibuffer-default
)
2031 (append minibuffer-default commands
)
2032 (cons minibuffer-default commands
))))
2034 (defvar shell-delimiter-argument-list
)
2035 (defvar shell-file-name-chars
)
2036 (defvar shell-file-name-quote-list
)
2038 (defun minibuffer-complete-shell-command ()
2039 "Dynamically complete shell command at point."
2042 (let ((comint-delimiter-argument-list shell-delimiter-argument-list
)
2043 (comint-file-name-chars shell-file-name-chars
)
2044 (comint-file-name-quote-list shell-file-name-quote-list
))
2045 (run-hook-with-args-until-success 'shell-dynamic-complete-functions
)))
2047 (defvar minibuffer-local-shell-command-map
2048 (let ((map (make-sparse-keymap)))
2049 (set-keymap-parent map minibuffer-local-map
)
2050 (define-key map
"\t" 'minibuffer-complete-shell-command
)
2052 "Keymap used for completing shell commands in minibuffer.")
2054 (defun read-shell-command (prompt &optional initial-contents hist
&rest args
)
2055 "Read a shell command from the minibuffer.
2056 The arguments are the same as the ones of `read-from-minibuffer',
2057 except READ and KEYMAP are missing and HIST defaults
2058 to `shell-command-history'."
2059 (minibuffer-with-setup-hook
2061 (set (make-local-variable 'minibuffer-default-add-function
)
2062 'minibuffer-default-add-shell-commands
))
2063 (apply 'read-from-minibuffer prompt initial-contents
2064 minibuffer-local-shell-command-map
2066 (or hist
'shell-command-history
)
2069 (defun async-shell-command (command &optional output-buffer error-buffer
)
2070 "Execute string COMMAND asynchronously in background.
2072 Like `shell-command' but if COMMAND doesn't end in ampersand, adds `&'
2073 surrounded by whitespace and executes the command asynchronously.
2074 The output appears in the buffer `*Async Shell Command*'."
2077 (read-shell-command "Async shell command: " nil nil
2078 (and buffer-file-name
2079 (file-relative-name buffer-file-name
)))
2081 shell-command-default-error-buffer
))
2082 (unless (string-match "&[ \t]*\\'" command
)
2083 (setq command
(concat command
" &")))
2084 (shell-command command output-buffer error-buffer
))
2086 (defun shell-command (command &optional output-buffer error-buffer
)
2087 "Execute string COMMAND in inferior shell; display output, if any.
2088 With prefix argument, insert the COMMAND's output at point.
2090 If COMMAND ends in ampersand, execute it asynchronously.
2091 The output appears in the buffer `*Async Shell Command*'.
2092 That buffer is in shell mode.
2094 Otherwise, COMMAND is executed synchronously. The output appears in
2095 the buffer `*Shell Command Output*'. If the output is short enough to
2096 display in the echo area (which is determined by the variables
2097 `resize-mini-windows' and `max-mini-window-height'), it is shown
2098 there, but it is nonetheless available in buffer `*Shell Command
2099 Output*' even though that buffer is not automatically displayed.
2101 To specify a coding system for converting non-ASCII characters
2102 in the shell command output, use \\[universal-coding-system-argument] \
2103 before this command.
2105 Noninteractive callers can specify coding systems by binding
2106 `coding-system-for-read' and `coding-system-for-write'.
2108 The optional second argument OUTPUT-BUFFER, if non-nil,
2109 says to put the output in some other buffer.
2110 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
2111 If OUTPUT-BUFFER is not a buffer and not nil,
2112 insert output in current buffer. (This cannot be done asynchronously.)
2113 In either case, the buffer is first erased, and the output is
2114 inserted after point (leaving mark after it).
2116 If the command terminates without error, but generates output,
2117 and you did not specify \"insert it in the current buffer\",
2118 the output can be displayed in the echo area or in its buffer.
2119 If the output is short enough to display in the echo area
2120 \(determined by the variable `max-mini-window-height' if
2121 `resize-mini-windows' is non-nil), it is shown there.
2122 Otherwise,the buffer containing the output is displayed.
2124 If there is output and an error, and you did not specify \"insert it
2125 in the current buffer\", a message about the error goes at the end
2128 If there is no output, or if output is inserted in the current buffer,
2129 then `*Shell Command Output*' is deleted.
2131 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
2132 or buffer name to which to direct the command's standard error output.
2133 If it is nil, error output is mingled with regular output.
2134 In an interactive call, the variable `shell-command-default-error-buffer'
2135 specifies the value of ERROR-BUFFER."
2139 (read-shell-command "Shell command: " nil nil
2143 ((eq major-mode
'dired-mode
)
2144 (dired-get-filename nil t
)))))
2145 (and filename
(file-relative-name filename
))))
2147 shell-command-default-error-buffer
))
2148 ;; Look for a handler in case default-directory is a remote file name.
2150 (find-file-name-handler (directory-file-name default-directory
)
2153 (funcall handler
'shell-command command output-buffer error-buffer
)
2154 (if (and output-buffer
2155 (not (or (bufferp output-buffer
) (stringp output-buffer
))))
2156 ;; Output goes in current buffer.
2160 (expand-file-name "scor"
2161 (or small-temporary-file-directory
2162 temporary-file-directory
)))
2164 (barf-if-buffer-read-only)
2166 ;; We do not use -f for csh; we will not support broken use of
2167 ;; .cshrcs. Even the BSD csh manual says to use
2168 ;; "if ($?prompt) exit" before things which are not useful
2169 ;; non-interactively. Besides, if someone wants their other
2170 ;; aliases for shell commands then they can still have them.
2171 (call-process shell-file-name nil
2175 nil shell-command-switch command
)
2176 (when (and error-file
(file-exists-p error-file
))
2177 (if (< 0 (nth 7 (file-attributes error-file
)))
2178 (with-current-buffer (get-buffer-create error-buffer
)
2179 (let ((pos-from-end (- (point-max) (point))))
2182 ;; Do no formatting while reading error file,
2183 ;; because that can run a shell command, and we
2184 ;; don't want that to cause an infinite recursion.
2185 (format-insert-file error-file nil
)
2186 ;; Put point after the inserted errors.
2187 (goto-char (- (point-max) pos-from-end
)))
2188 (display-buffer (current-buffer))))
2189 (delete-file error-file
))
2190 ;; This is like exchange-point-and-mark, but doesn't
2191 ;; activate the mark. It is cleaner to avoid activation,
2192 ;; even though the command loop would deactivate the mark
2193 ;; because we inserted text.
2194 (goto-char (prog1 (mark t
)
2195 (set-marker (mark-marker) (point)
2196 (current-buffer)))))
2197 ;; Output goes in a separate buffer.
2198 ;; Preserve the match data in case called from a program.
2200 (if (string-match "[ \t]*&[ \t]*\\'" command
)
2201 ;; Command ending with ampersand means asynchronous.
2202 (let ((buffer (get-buffer-create
2203 (or output-buffer
"*Async Shell Command*")))
2204 (directory default-directory
)
2206 ;; Remove the ampersand.
2207 (setq command
(substring command
0 (match-beginning 0)))
2208 ;; If will kill a process, query first.
2209 (setq proc
(get-buffer-process buffer
))
2211 (if (yes-or-no-p "A command is running. Kill it? ")
2213 (error "Shell command in progress")))
2214 (with-current-buffer buffer
2215 (setq buffer-read-only nil
)
2217 (display-buffer buffer
)
2218 (setq default-directory directory
)
2219 (setq proc
(start-process "Shell" buffer shell-file-name
2220 shell-command-switch command
))
2221 (setq mode-line-process
'(":%s"))
2222 (require 'shell
) (shell-mode)
2223 (set-process-sentinel proc
'shell-command-sentinel
)
2224 ;; Use the comint filter for proper handling of carriage motion
2225 ;; (see `comint-inhibit-carriage-motion'),.
2226 (set-process-filter proc
'comint-output-filter
)
2228 ;; Otherwise, command is executed synchronously.
2229 (shell-command-on-region (point) (point) command
2230 output-buffer nil error-buffer
)))))))
2232 (defun display-message-or-buffer (message
2233 &optional buffer-name not-this-window frame
)
2234 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
2235 MESSAGE may be either a string or a buffer.
2237 A buffer is displayed using `display-buffer' if MESSAGE is too long for
2238 the maximum height of the echo area, as defined by `max-mini-window-height'
2239 if `resize-mini-windows' is non-nil.
2241 Returns either the string shown in the echo area, or when a pop-up
2242 buffer is used, the window used to display it.
2244 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
2245 name of the buffer used to display it in the case where a pop-up buffer
2246 is used, defaulting to `*Message*'. In the case where MESSAGE is a
2247 string and it is displayed in the echo area, it is not specified whether
2248 the contents are inserted into the buffer anyway.
2250 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
2251 and only used if a buffer is displayed."
2252 (cond ((and (stringp message
) (not (string-match "\n" message
)))
2253 ;; Trivial case where we can use the echo area
2254 (message "%s" message
))
2255 ((and (stringp message
)
2256 (= (string-match "\n" message
) (1- (length message
))))
2257 ;; Trivial case where we can just remove single trailing newline
2258 (message "%s" (substring message
0 (1- (length message
)))))
2261 (with-current-buffer
2262 (if (bufferp message
)
2264 (get-buffer-create (or buffer-name
"*Message*")))
2266 (unless (bufferp message
)
2271 (if (= (buffer-size) 0)
2273 (count-screen-lines nil nil nil
(minibuffer-window)))))
2275 ((and (or (<= lines
1)
2277 (if resize-mini-windows
2278 (cond ((floatp max-mini-window-height
)
2280 max-mini-window-height
))
2281 ((integerp max-mini-window-height
)
2282 max-mini-window-height
)
2286 ;; Don't use the echo area if the output buffer is
2287 ;; already dispayed in the selected frame.
2288 (not (get-buffer-window (current-buffer))))
2290 (goto-char (point-max))
2293 (message "%s" (buffer-substring (point-min) (point))))
2296 (goto-char (point-min))
2297 (display-buffer (current-buffer)
2298 not-this-window frame
))))))))
2301 ;; We have a sentinel to prevent insertion of a termination message
2302 ;; in the buffer itself.
2303 (defun shell-command-sentinel (process signal
)
2304 (if (memq (process-status process
) '(exit signal
))
2306 (car (cdr (cdr (process-command process
))))
2307 (substring signal
0 -
1))))
2309 (defun shell-command-on-region (start end command
2310 &optional output-buffer replace
2311 error-buffer display-error-buffer
)
2312 "Execute string COMMAND in inferior shell with region as input.
2313 Normally display output (if any) in temp buffer `*Shell Command Output*';
2314 Prefix arg means replace the region with it. Return the exit code of
2317 To specify a coding system for converting non-ASCII characters
2318 in the input and output to the shell command, use \\[universal-coding-system-argument]
2319 before this command. By default, the input (from the current buffer)
2320 is encoded in the same coding system that will be used to save the file,
2321 `buffer-file-coding-system'. If the output is going to replace the region,
2322 then it is decoded from that same coding system.
2324 The noninteractive arguments are START, END, COMMAND,
2325 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
2326 Noninteractive callers can specify coding systems by binding
2327 `coding-system-for-read' and `coding-system-for-write'.
2329 If the command generates output, the output may be displayed
2330 in the echo area or in a buffer.
2331 If the output is short enough to display in the echo area
2332 \(determined by the variable `max-mini-window-height' if
2333 `resize-mini-windows' is non-nil), it is shown there. Otherwise
2334 it is displayed in the buffer `*Shell Command Output*'. The output
2335 is available in that buffer in both cases.
2337 If there is output and an error, a message about the error
2338 appears at the end of the output.
2340 If there is no output, or if output is inserted in the current buffer,
2341 then `*Shell Command Output*' is deleted.
2343 If the optional fourth argument OUTPUT-BUFFER is non-nil,
2344 that says to put the output in some other buffer.
2345 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
2346 If OUTPUT-BUFFER is not a buffer and not nil,
2347 insert output in the current buffer.
2348 In either case, the output is inserted after point (leaving mark after it).
2350 If REPLACE, the optional fifth argument, is non-nil, that means insert
2351 the output in place of text from START to END, putting point and mark
2354 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
2355 or buffer name to which to direct the command's standard error output.
2356 If it is nil, error output is mingled with regular output.
2357 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
2358 were any errors. (This is always t, interactively.)
2359 In an interactive call, the variable `shell-command-default-error-buffer'
2360 specifies the value of ERROR-BUFFER."
2361 (interactive (let (string)
2363 (error "The mark is not set now, so there is no region"))
2364 ;; Do this before calling region-beginning
2365 ;; and region-end, in case subprocess output
2366 ;; relocates them while we are in the minibuffer.
2367 (setq string
(read-shell-command "Shell command on region: "))
2368 ;; call-interactively recognizes region-beginning and
2369 ;; region-end specially, leaving them in the history.
2370 (list (region-beginning) (region-end)
2374 shell-command-default-error-buffer
2379 (expand-file-name "scor"
2380 (or small-temporary-file-directory
2381 temporary-file-directory
)))
2386 (not (or (bufferp output-buffer
) (stringp output-buffer
)))))
2387 ;; Replace specified region with output from command.
2388 (let ((swap (and replace
(< start end
))))
2389 ;; Don't muck with mark unless REPLACE says we should.
2391 (and replace
(push-mark (point) 'nomsg
))
2393 (call-process-region start end shell-file-name t
2397 nil shell-command-switch command
))
2398 ;; It is rude to delete a buffer which the command is not using.
2399 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
2400 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
2401 ;; (kill-buffer shell-buffer)))
2402 ;; Don't muck with mark unless REPLACE says we should.
2403 (and replace swap
(exchange-point-and-mark)))
2404 ;; No prefix argument: put the output in a temp buffer,
2405 ;; replacing its entire contents.
2406 (let ((buffer (get-buffer-create
2407 (or output-buffer
"*Shell Command Output*"))))
2409 (if (eq buffer
(current-buffer))
2410 ;; If the input is the same buffer as the output,
2411 ;; delete everything but the specified region,
2412 ;; then replace that region with the output.
2413 (progn (setq buffer-read-only nil
)
2414 (delete-region (max start end
) (point-max))
2415 (delete-region (point-min) (min start end
))
2417 (call-process-region (point-min) (point-max)
2422 nil shell-command-switch
2424 ;; Clear the output buffer, then run the command with
2426 (let ((directory default-directory
))
2427 (with-current-buffer buffer
2428 (setq buffer-read-only nil
)
2429 (if (not output-buffer
)
2430 (setq default-directory directory
))
2433 (call-process-region start end shell-file-name nil
2435 (list buffer error-file
)
2437 nil shell-command-switch command
)))
2438 ;; Report the output.
2439 (with-current-buffer buffer
2440 (setq mode-line-process
2441 (cond ((null exit-status
)
2443 ((stringp exit-status
)
2444 (format " - Signal [%s]" exit-status
))
2445 ((not (equal 0 exit-status
))
2446 (format " - Exit [%d]" exit-status
)))))
2447 (if (with-current-buffer buffer
(> (point-max) (point-min)))
2448 ;; There's some output, display it
2449 (display-message-or-buffer buffer
)
2450 ;; No output; error?
2453 (< 0 (nth 7 (file-attributes error-file
))))
2456 (cond ((null exit-status
)
2457 (message "(Shell command failed with error)"))
2458 ((equal 0 exit-status
)
2459 (message "(Shell command succeeded with %s)"
2461 ((stringp exit-status
)
2462 (message "(Shell command killed by signal %s)"
2465 (message "(Shell command failed with code %d and %s)"
2466 exit-status output
))))
2467 ;; Don't kill: there might be useful info in the undo-log.
2468 ;; (kill-buffer buffer)
2471 (when (and error-file
(file-exists-p error-file
))
2472 (if (< 0 (nth 7 (file-attributes error-file
)))
2473 (with-current-buffer (get-buffer-create error-buffer
)
2474 (let ((pos-from-end (- (point-max) (point))))
2477 ;; Do no formatting while reading error file,
2478 ;; because that can run a shell command, and we
2479 ;; don't want that to cause an infinite recursion.
2480 (format-insert-file error-file nil
)
2481 ;; Put point after the inserted errors.
2482 (goto-char (- (point-max) pos-from-end
)))
2483 (and display-error-buffer
2484 (display-buffer (current-buffer)))))
2485 (delete-file error-file
))
2488 (defun shell-command-to-string (command)
2489 "Execute shell command COMMAND and return its output as a string."
2490 (with-output-to-string
2491 (with-current-buffer
2493 (call-process shell-file-name nil t nil shell-command-switch command
))))
2495 (defun process-file (program &optional infile buffer display
&rest args
)
2496 "Process files synchronously in a separate process.
2497 Similar to `call-process', but may invoke a file handler based on
2498 `default-directory'. The current working directory of the
2499 subprocess is `default-directory'.
2501 File names in INFILE and BUFFER are handled normally, but file
2502 names in ARGS should be relative to `default-directory', as they
2503 are passed to the process verbatim. \(This is a difference to
2504 `call-process' which does not support file handlers for INFILE
2507 Some file handlers might not support all variants, for example
2508 they might behave as if DISPLAY was nil, regardless of the actual
2510 (let ((fh (find-file-name-handler default-directory
'process-file
))
2513 (if fh
(apply fh
'process-file program infile buffer display args
)
2514 (when infile
(setq lc
(file-local-copy infile
)))
2515 (setq stderr-file
(when (and (consp buffer
) (stringp (cadr buffer
)))
2516 (make-temp-file "emacs")))
2518 (apply 'call-process program
2520 (if stderr-file
(list (car buffer
) stderr-file
) buffer
)
2522 (when stderr-file
(copy-file stderr-file
(cadr buffer
)))))
2523 (when stderr-file
(delete-file stderr-file
))
2524 (when lc
(delete-file lc
)))))
2526 (defvar process-file-side-effects t
2527 "Whether a call of `process-file' changes remote files.
2529 Per default, this variable is always set to `t', meaning that a
2530 call of `process-file' could potentially change any file on a
2531 remote host. When set to `nil', a file handler could optimize
2532 its behaviour with respect to remote file attributes caching.
2534 This variable should never be changed by `setq'. Instead of, it
2535 shall be set only by let-binding.")
2537 (defun start-file-process (name buffer program
&rest program-args
)
2538 "Start a program in a subprocess. Return the process object for it.
2540 Similar to `start-process', but may invoke a file handler based on
2541 `default-directory'. See Info node `(elisp)Magic File Names'.
2543 This handler ought to run PROGRAM, perhaps on the local host,
2544 perhaps on a remote host that corresponds to `default-directory'.
2545 In the latter case, the local part of `default-directory' becomes
2546 the working directory of the process.
2548 PROGRAM and PROGRAM-ARGS might be file names. They are not
2549 objects of file handler invocation. File handlers might not
2550 support pty association, if PROGRAM is nil."
2551 (let ((fh (find-file-name-handler default-directory
'start-file-process
)))
2552 (if fh
(apply fh
'start-file-process name buffer program program-args
)
2553 (apply 'start-process name buffer program program-args
))))
2556 (defvar universal-argument-map
2557 (let ((map (make-sparse-keymap)))
2558 (define-key map
[t] 'universal-argument-other-key)
2559 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
2560 (define-key map [switch-frame] nil)
2561 (define-key map [?\C-u] 'universal-argument-more)
2562 (define-key map [?-] 'universal-argument-minus)
2563 (define-key map [?0] 'digit-argument)
2564 (define-key map [?1] 'digit-argument)
2565 (define-key map [?2] 'digit-argument)
2566 (define-key map [?3] 'digit-argument)
2567 (define-key map [?4] 'digit-argument)
2568 (define-key map [?5] 'digit-argument)
2569 (define-key map [?6] 'digit-argument)
2570 (define-key map [?7] 'digit-argument)
2571 (define-key map [?8] 'digit-argument)
2572 (define-key map [?9] 'digit-argument)
2573 (define-key map [kp-0] 'digit-argument)
2574 (define-key map [kp-1] 'digit-argument)
2575 (define-key map [kp-2] 'digit-argument)
2576 (define-key map [kp-3] 'digit-argument)
2577 (define-key map [kp-4] 'digit-argument)
2578 (define-key map [kp-5] 'digit-argument)
2579 (define-key map [kp-6] 'digit-argument)
2580 (define-key map [kp-7] 'digit-argument)
2581 (define-key map [kp-8] 'digit-argument)
2582 (define-key map [kp-9] 'digit-argument)
2583 (define-key map [kp-subtract] 'universal-argument-minus)
2585 "Keymap used while processing \\[universal-argument].")
2587 (defvar universal-argument-num-events nil
2588 "Number of argument-specifying events read by `universal-argument'.
2589 `universal-argument-other-key' uses this to discard those events
2590 from (this-command-keys), and reread only the final command.")
2592 (defvar overriding-map-is-bound nil
2593 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
2595 (defvar saved-overriding-map nil
2596 "The saved value of `overriding-terminal-local-map'.
2597 That variable gets restored to this value on exiting \"universal
2600 (defun ensure-overriding-map-is-bound ()
2601 "Check `overriding-terminal-local-map' is `universal-argument-map'."
2602 (unless overriding-map-is-bound
2603 (setq saved-overriding-map overriding-terminal-local-map)
2604 (setq overriding-terminal-local-map universal-argument-map)
2605 (setq overriding-map-is-bound t)))
2607 (defun restore-overriding-map ()
2608 "Restore `overriding-terminal-local-map' to its saved value."
2609 (setq overriding-terminal-local-map saved-overriding-map)
2610 (setq overriding-map-is-bound nil))
2612 (defun universal-argument ()
2613 "Begin a numeric argument for the following command.
2614 Digits or minus sign following \\[universal-argument] make up the numeric argument.
2615 \\[universal-argument] following the digits or minus sign ends the argument.
2616 \\[universal-argument] without digits or minus sign provides 4 as argument.
2617 Repeating \\[universal-argument] without digits or minus sign
2618 multiplies the argument by 4 each time.
2619 For some commands, just \\[universal-argument] by itself serves as a flag
2620 which is different in effect from any particular numeric argument.
2621 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
2623 (setq prefix-arg (list 4))
2624 (setq universal-argument-num-events (length (this-command-keys)))
2625 (ensure-overriding-map-is-bound))
2627 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
2628 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
2629 (defun universal-argument-more (arg)
2632 (setq prefix-arg (list (* 4 (car arg))))
2634 (setq prefix-arg (list -4))
2635 (setq prefix-arg arg)
2636 (restore-overriding-map)))
2637 (setq universal-argument-num-events (length (this-command-keys))))
2639 (defun negative-argument (arg)
2640 "Begin a negative numeric argument for the next command.
2641 \\[universal-argument] following digits or minus sign ends the argument."
2643 (cond ((integerp arg)
2644 (setq prefix-arg (- arg)))
2646 (setq prefix-arg nil))
2648 (setq prefix-arg '-)))
2649 (setq universal-argument-num-events (length (this-command-keys)))
2650 (ensure-overriding-map-is-bound))
2652 (defun digit-argument (arg)
2653 "Part of the numeric argument for the next command.
2654 \\[universal-argument] following digits or minus sign ends the argument."
2656 (let* ((char (if (integerp last-command-event)
2658 (get last-command-event 'ascii-character)))
2659 (digit (- (logand char ?\177) ?0)))
2660 (cond ((integerp arg)
2661 (setq prefix-arg (+ (* arg 10)
2662 (if (< arg 0) (- digit) digit))))
2664 ;; Treat -0 as just -, so that -01 will work.
2665 (setq prefix-arg (if (zerop digit) '- (- digit))))
2667 (setq prefix-arg digit))))
2668 (setq universal-argument-num-events (length (this-command-keys)))
2669 (ensure-overriding-map-is-bound))
2671 ;; For backward compatibility, minus with no modifiers is an ordinary
2672 ;; command if digits have already been entered.
2673 (defun universal-argument-minus (arg)
2676 (universal-argument-other-key arg)
2677 (negative-argument arg)))
2679 ;; Anything else terminates the argument and is left in the queue to be
2680 ;; executed as a command.
2681 (defun universal-argument-other-key (arg)
2683 (setq prefix-arg arg)
2684 (let* ((key (this-command-keys))
2685 (keylist (listify-key-sequence key)))
2686 (setq unread-command-events
2687 (append (nthcdr universal-argument-num-events keylist)
2688 unread-command-events)))
2689 (reset-this-command-lengths)
2690 (restore-overriding-map))
2692 (defvar buffer-substring-filters nil
2693 "List of filter functions for `filter-buffer-substring'.
2694 Each function must accept a single argument, a string, and return
2695 a string. The buffer substring is passed to the first function
2696 in the list, and the return value of each function is passed to
2697 the next. The return value of the last function is used as the
2698 return value of `filter-buffer-substring'.
2700 If this variable is nil, no filtering is performed.")
2702 (defun filter-buffer-substring (beg end &optional delete noprops)
2703 "Return the buffer substring between BEG and END, after filtering.
2704 The buffer substring is passed through each of the filter
2705 functions in `buffer-substring-filters', and the value from the
2706 last filter function is returned. If `buffer-substring-filters'
2707 is nil, the buffer substring is returned unaltered.
2709 If DELETE is non-nil, the text between BEG and END is deleted
2712 If NOPROPS is non-nil, final string returned does not include
2713 text properties, while the string passed to the filters still
2714 includes text properties from the buffer text.
2716 Point is temporarily set to BEG before calling
2717 `buffer-substring-filters', in case the functions need to know
2718 where the text came from.
2720 This function should be used instead of `buffer-substring',
2721 `buffer-substring-no-properties', or `delete-and-extract-region'
2722 when you want to allow filtering to take place. For example,
2723 major or minor modes can use `buffer-substring-filters' to
2724 extract characters that are special to a buffer, and should not
2725 be copied into other buffers."
2727 ((or delete buffer-substring-filters)
2730 (let ((string (if delete (delete-and-extract-region beg end)
2731 (buffer-substring beg end))))
2732 (dolist (filter buffer-substring-filters)
2733 (setq string (funcall filter string)))
2735 (set-text-properties 0 (length string) nil string))
2738 (buffer-substring-no-properties beg end))
2740 (buffer-substring beg end))))
2743 ;;;; Window system cut and paste hooks.
2745 (defvar interprogram-cut-function nil
2746 "Function to call to make a killed region available to other programs.
2748 Most window systems provide some sort of facility for cutting and
2749 pasting text between the windows of different programs.
2750 This variable holds a function that Emacs calls whenever text
2751 is put in the kill ring, to make the new kill available to other
2754 The function takes one or two arguments.
2755 The first argument, TEXT, is a string containing
2756 the text which should be made available.
2757 The second, optional, argument PUSH, has the same meaning as the
2758 similar argument to `x-set-cut-buffer', which see.")
2760 (defvar interprogram-paste-function nil
2761 "Function to call to get text cut from other programs.
2763 Most window systems provide some sort of facility for cutting and
2764 pasting text between the windows of different programs.
2765 This variable holds a function that Emacs calls to obtain
2766 text that other programs have provided for pasting.
2768 The function should be called with no arguments. If the function
2769 returns nil, then no other program has provided such text, and the top
2770 of the Emacs kill ring should be used. If the function returns a
2771 string, then the caller of the function \(usually `current-kill')
2772 should put this string in the kill ring as the latest kill.
2774 This function may also return a list of strings if the window
2775 system supports multiple selections. The first string will be
2776 used as the pasted text, but the other will be placed in the
2777 kill ring for easy access via `yank-pop'.
2779 Note that the function should return a string only if a program other
2780 than Emacs has provided a string for pasting; if Emacs provided the
2781 most recent string, the function should return nil. If it is
2782 difficult to tell whether Emacs or some other program provided the
2783 current string, it is probably good enough to return nil if the string
2784 is equal (according to `string=') to the last text Emacs provided.")
2788 ;;;; The kill ring data structure.
2790 (defvar kill-ring nil
2791 "List of killed text sequences.
2792 Since the kill ring is supposed to interact nicely with cut-and-paste
2793 facilities offered by window systems, use of this variable should
2794 interact nicely with `interprogram-cut-function' and
2795 `interprogram-paste-function'. The functions `kill-new',
2796 `kill-append', and `current-kill' are supposed to implement this
2797 interaction; you may want to use them instead of manipulating the kill
2800 (defcustom kill-ring-max 60
2801 "Maximum length of kill ring before oldest elements are thrown away."
2805 (defvar kill-ring-yank-pointer nil
2806 "The tail of the kill ring whose car is the last thing yanked.")
2808 (defcustom save-interprogram-paste-before-kill nil
2809 "Save the paste strings into `kill-ring' before replacing it with emacs strings.
2810 When one selects something in another program to paste it into Emacs,
2811 but kills something in Emacs before actually pasting it,
2812 this selection is gone unless this variable is non-nil,
2813 in which case the other program's selection is saved in the `kill-ring'
2814 before the Emacs kill and one can still paste it using \\[yank] \\[yank-pop]."
2819 (defcustom kill-do-not-save-duplicates nil
2820 "Do not add a new string to `kill-ring' when it is the same as the last one."
2825 (defun kill-new (string &optional replace yank-handler)
2826 "Make STRING the latest kill in the kill ring.
2827 Set `kill-ring-yank-pointer' to point to it.
2828 If `interprogram-cut-function' is non-nil, apply it to STRING.
2829 Optional second argument REPLACE non-nil means that STRING will replace
2830 the front of the kill ring, rather than being added to the list.
2832 Optional third arguments YANK-HANDLER controls how the STRING is later
2833 inserted into a buffer; see `insert-for-yank' for details.
2834 When a yank handler is specified, STRING must be non-empty (the yank
2835 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2837 When `save-interprogram-paste-before-kill' and `interprogram-paste-function'
2838 are non-nil, saves the interprogram paste string(s) into `kill-ring' before
2841 When the yank handler has a non-nil PARAM element, the original STRING
2842 argument is not used by `insert-for-yank'. However, since Lisp code
2843 may access and use elements from the kill ring directly, the STRING
2844 argument should still be a \"useful\" string for such uses."
2845 (if (> (length string) 0)
2847 (put-text-property 0 (length string)
2848 'yank-handler yank-handler string))
2850 (signal 'args-out-of-range
2851 (list string "yank-handler specified for empty string"))))
2852 (when (and kill-do-not-save-duplicates
2853 (equal string (car kill-ring)))
2855 (if (fboundp 'menu-bar-update-yank-menu)
2856 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2857 (when save-interprogram-paste-before-kill
2858 (let ((interprogram-paste (and interprogram-paste-function
2859 (funcall interprogram-paste-function))))
2860 (when interprogram-paste
2861 (if (listp interprogram-paste)
2862 (dolist (s (nreverse interprogram-paste))
2864 (push interprogram-paste kill-ring)))))
2865 (if (and replace kill-ring)
2866 (setcar kill-ring string)
2867 (push string kill-ring)
2868 (if (> (length kill-ring) kill-ring-max)
2869 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2870 (setq kill-ring-yank-pointer kill-ring)
2871 (if interprogram-cut-function
2872 (funcall interprogram-cut-function string (not replace))))
2874 (defun kill-append (string before-p &optional yank-handler)
2875 "Append STRING to the end of the latest kill in the kill ring.
2876 If BEFORE-P is non-nil, prepend STRING to the kill.
2877 Optional third argument YANK-HANDLER, if non-nil, specifies the
2878 yank-handler text property to be set on the combined kill ring
2879 string. If the specified yank-handler arg differs from the
2880 yank-handler property of the latest kill string, this function
2881 adds the combined string to the kill ring as a new element,
2882 instead of replacing the last kill with it.
2883 If `interprogram-cut-function' is set, pass the resulting kill to it."
2884 (let* ((cur (car kill-ring)))
2885 (kill-new (if before-p (concat string cur) (concat cur string))
2886 (or (= (length cur) 0)
2887 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2890 (defcustom yank-pop-change-selection nil
2891 "If non-nil, rotating the kill ring changes the window system selection."
2896 (defun current-kill (n &optional do-not-move)
2897 "Rotate the yanking point by N places, and then return that kill.
2898 If N is zero, `interprogram-paste-function' is set, and calling
2899 it returns a string or list of strings, then that string (or
2900 list) is added to the front of the kill ring and the string (or
2901 first string in the list) is returned as the latest kill.
2903 If N is not zero, and if `yank-pop-change-selection' is
2904 non-nil, use `interprogram-cut-function' to transfer the
2905 kill at the new yank point into the window system selection.
2907 If optional arg DO-NOT-MOVE is non-nil, then don't actually
2908 move the yanking point; just return the Nth kill forward."
2910 (let ((interprogram-paste (and (= n 0)
2911 interprogram-paste-function
2912 (funcall interprogram-paste-function))))
2913 (if interprogram-paste
2915 ;; Disable the interprogram cut function when we add the new
2916 ;; text to the kill ring, so Emacs doesn't try to own the
2917 ;; selection, with identical text.
2918 (let ((interprogram-cut-function nil))
2919 (if (listp interprogram-paste)
2920 (mapc 'kill-new (nreverse interprogram-paste))
2921 (kill-new interprogram-paste)))
2923 (or kill-ring (error "Kill ring is empty"))
2924 (let ((ARGth-kill-element
2925 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2929 (setq kill-ring-yank-pointer ARGth-kill-element)
2930 (when (and yank-pop-change-selection
2932 interprogram-cut-function)
2933 (funcall interprogram-cut-function (car ARGth-kill-element))))
2934 (car ARGth-kill-element)))))
2938 ;;;; Commands for manipulating the kill ring.
2940 (defcustom kill-read-only-ok nil
2941 "Non-nil means don't signal an error for killing read-only text."
2945 (put 'text-read-only 'error-conditions
2946 '(text-read-only buffer-read-only error))
2947 (put 'text-read-only 'error-message (purecopy "Text is read-only"))
2949 (defun kill-region (beg end &optional yank-handler)
2950 "Kill (\"cut\") text between point and mark.
2951 This deletes the text from the buffer and saves it in the kill ring.
2952 The command \\[yank] can retrieve it from there.
2953 \(If you want to save the region without killing it, use \\[kill-ring-save].)
2955 If you want to append the killed region to the last killed text,
2956 use \\[append-next-kill] before \\[kill-region].
2958 If the buffer is read-only, Emacs will beep and refrain from deleting
2959 the text, but put the text in the kill ring anyway. This means that
2960 you can use the killing commands to copy text from a read-only buffer.
2962 This is the primitive for programs to kill text (as opposed to deleting it).
2963 Supply two arguments, character positions indicating the stretch of text
2965 Any command that calls this function is a \"kill command\".
2966 If the previous command was also a kill command,
2967 the text killed this time appends to the text killed last time
2968 to make one entry in the kill ring.
2970 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2971 specifies the yank-handler text property to be set on the killed
2972 text. See `insert-for-yank'."
2973 ;; Pass point first, then mark, because the order matters
2974 ;; when calling kill-append.
2975 (interactive (list (point) (mark)))
2976 (unless (and beg end)
2977 (error "The mark is not set now, so there is no region"))
2979 (let ((string (filter-buffer-substring beg end t)))
2980 (when string ;STRING is nil if BEG = END
2981 ;; Add that string to the kill ring, one way or another.
2982 (if (eq last-command 'kill-region)
2983 (kill-append string (< end beg) yank-handler)
2984 (kill-new string nil yank-handler)))
2985 (when (or string (eq last-command 'kill-region))
2986 (setq this-command 'kill-region))
2988 ((buffer-read-only text-read-only)
2989 ;; The code above failed because the buffer, or some of the characters
2990 ;; in the region, are read-only.
2991 ;; We should beep, in case the user just isn't aware of this.
2992 ;; However, there's no harm in putting
2993 ;; the region's text in the kill ring, anyway.
2994 (copy-region-as-kill beg end)
2995 ;; Set this-command now, so it will be set even if we get an error.
2996 (setq this-command 'kill-region)
2997 ;; This should barf, if appropriate, and give us the correct error.
2998 (if kill-read-only-ok
2999 (progn (message "Read only text copied to kill ring") nil)
3000 ;; Signal an error if the buffer is read-only.
3001 (barf-if-buffer-read-only)
3002 ;; If the buffer isn't read-only, the text is.
3003 (signal 'text-read-only (list (current-buffer)))))))
3005 ;; copy-region-as-kill no longer sets this-command, because it's confusing
3006 ;; to get two copies of the text when the user accidentally types M-w and
3007 ;; then corrects it with the intended C-w.
3008 (defun copy-region-as-kill (beg end)
3009 "Save the region as if killed, but don't kill it.
3010 In Transient Mark mode, deactivate the mark.
3011 If `interprogram-cut-function' is non-nil, also save the text for a window
3012 system cut and paste.
3014 This command's old key binding has been given to `kill-ring-save'."
3016 (if (eq last-command 'kill-region)
3017 (kill-append (filter-buffer-substring beg end) (< end beg))
3018 (kill-new (filter-buffer-substring beg end)))
3019 (setq deactivate-mark t)
3022 (defun kill-ring-save (beg end)
3023 "Save the region as if killed, but don't kill it.
3024 In Transient Mark mode, deactivate the mark.
3025 If `interprogram-cut-function' is non-nil, also save the text for a window
3026 system cut and paste.
3028 If you want to append the killed line to the last killed text,
3029 use \\[append-next-kill] before \\[kill-ring-save].
3031 This command is similar to `copy-region-as-kill', except that it gives
3032 visual feedback indicating the extent of the region being copied."
3034 (copy-region-as-kill beg end)
3035 ;; This use of called-interactively-p is correct
3036 ;; because the code it controls just gives the user visual feedback.
3037 (if (called-interactively-p 'interactive)
3038 (let ((other-end (if (= (point) beg) end beg))
3040 ;; Inhibit quitting so we can make a quit here
3041 ;; look like a C-g typed as a command.
3043 (if (pos-visible-in-window-p other-end (selected-window))
3044 ;; Swap point-and-mark quickly so as to show the region that
3045 ;; was selected. Don't do it if the region is highlighted.
3046 (unless (and (region-active-p)
3047 (face-background 'region))
3048 ;; Swap point and mark.
3049 (set-marker (mark-marker) (point) (current-buffer))
3050 (goto-char other-end)
3051 (sit-for blink-matching-delay)
3053 (set-marker (mark-marker) other-end (current-buffer))
3055 ;; If user quit, deactivate the mark
3056 ;; as C-g would as a command.
3057 (and quit-flag mark-active
3059 (let* ((killed-text (current-kill 0))
3060 (message-len (min (length killed-text) 40)))
3062 ;; Don't say "killed"; that is misleading.
3063 (message "Saved text until \"%s\""
3064 (substring killed-text (- message-len)))
3065 (message "Saved text from \"%s\""
3066 (substring killed-text 0 message-len))))))))
3068 (defun append-next-kill (&optional interactive)
3069 "Cause following command, if it kills, to append to previous kill.
3070 The argument is used for internal purposes; do not supply one."
3072 ;; We don't use (interactive-p), since that breaks kbd macros.
3075 (setq this-command 'kill-region)
3076 (message "If the next command is a kill, it will append"))
3077 (setq last-command 'kill-region)))
3081 ;; This is actually used in subr.el but defcustom does not work there.
3082 (defcustom yank-excluded-properties
3083 '(read-only invisible intangible field mouse-face help-echo local-map keymap
3084 yank-handler follow-link fontified)
3085 "Text properties to discard when yanking.
3086 The value should be a list of text properties to discard or t,
3087 which means to discard all text properties."
3088 :type '(choice (const :tag "All" t) (repeat symbol))
3092 (defvar yank-window-start nil)
3093 (defvar yank-undo-function nil
3094 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
3095 Function is called with two parameters, START and END corresponding to
3096 the value of the mark and point; it is guaranteed that START <= END.
3097 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
3099 (defun yank-pop (&optional arg)
3100 "Replace just-yanked stretch of killed text with a different stretch.
3101 This command is allowed only immediately after a `yank' or a `yank-pop'.
3102 At such a time, the region contains a stretch of reinserted
3103 previously-killed text. `yank-pop' deletes that text and inserts in its
3104 place a different stretch of killed text.
3106 With no argument, the previous kill is inserted.
3107 With argument N, insert the Nth previous kill.
3108 If N is negative, this is a more recent kill.
3110 The sequence of kills wraps around, so that after the oldest one
3111 comes the newest one.
3113 When this command inserts killed text into the buffer, it honors
3114 `yank-excluded-properties' and `yank-handler' as described in the
3115 doc string for `insert-for-yank-1', which see."
3117 (if (not (eq last-command 'yank))
3118 (error "Previous command was not a yank"))
3119 (setq this-command 'yank)
3120 (unless arg (setq arg 1))
3121 (let ((inhibit-read-only t)
3122 (before (< (point) (mark t))))
3124 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
3125 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
3126 (setq yank-undo-function nil)
3127 (set-marker (mark-marker) (point) (current-buffer))
3128 (insert-for-yank (current-kill arg))
3129 ;; Set the window start back where it was in the yank command,
3131 (set-window-start (selected-window) yank-window-start t)
3133 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
3134 ;; It is cleaner to avoid activation, even though the command
3135 ;; loop would deactivate the mark because we inserted text.
3136 (goto-char (prog1 (mark t)
3137 (set-marker (mark-marker) (point) (current-buffer))))))
3140 (defun yank (&optional arg)
3141 "Reinsert (\"paste\") the last stretch of killed text.
3142 More precisely, reinsert the stretch of killed text most recently
3143 killed OR yanked. Put point at end, and set mark at beginning.
3144 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
3145 With argument N, reinsert the Nth most recently killed stretch of killed
3148 When this command inserts killed text into the buffer, it honors
3149 `yank-excluded-properties' and `yank-handler' as described in the
3150 doc string for `insert-for-yank-1', which see.
3152 See also the command `yank-pop' (\\[yank-pop])."
3154 (setq yank-window-start (window-start))
3155 ;; If we don't get all the way thru, make last-command indicate that
3156 ;; for the following command.
3157 (setq this-command t)
3159 (insert-for-yank (current-kill (cond
3164 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
3165 ;; It is cleaner to avoid activation, even though the command
3166 ;; loop would deactivate the mark because we inserted text.
3167 (goto-char (prog1 (mark t)
3168 (set-marker (mark-marker) (point) (current-buffer)))))
3169 ;; If we do get all the way thru, make this-command indicate that.
3170 (if (eq this-command t)
3171 (setq this-command 'yank))
3174 (defun rotate-yank-pointer (arg)
3175 "Rotate the yanking point in the kill ring.
3176 With ARG, rotate that many kills forward (or backward, if negative)."
3180 ;; Some kill commands.
3182 ;; Internal subroutine of delete-char
3183 (defun kill-forward-chars (arg)
3184 (if (listp arg) (setq arg (car arg)))
3185 (if (eq arg '-) (setq arg -1))
3186 (kill-region (point) (+ (point) arg)))
3188 ;; Internal subroutine of backward-delete-char
3189 (defun kill-backward-chars (arg)
3190 (if (listp arg) (setq arg (car arg)))
3191 (if (eq arg '-) (setq arg -1))
3192 (kill-region (point) (- (point) arg)))
3194 (defcustom backward-delete-char-untabify-method 'untabify
3195 "The method for untabifying when deleting backward.
3196 Can be `untabify' -- turn a tab to many spaces, then delete one space;
3197 `hungry' -- delete all whitespace, both tabs and spaces;
3198 `all' -- delete all whitespace, including tabs, spaces and newlines;
3199 nil -- just delete one character."
3200 :type '(choice (const untabify) (const hungry) (const all) (const nil))
3204 (defun backward-delete-char-untabify (arg &optional killp)
3205 "Delete characters backward, changing tabs into spaces.
3206 The exact behavior depends on `backward-delete-char-untabify-method'.
3207 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
3208 Interactively, ARG is the prefix arg (default 1)
3209 and KILLP is t if a prefix arg was specified."
3210 (interactive "*p\nP")
3211 (when (eq backward-delete-char-untabify-method 'untabify)
3214 (while (and (> count 0) (not (bobp)))
3215 (if (= (preceding-char) ?\t)
3216 (let ((col (current-column)))
3218 (setq col (- col (current-column)))
3219 (insert-char ?\s col)
3222 (setq count (1- count))))))
3223 (delete-backward-char
3224 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
3225 ((eq backward-delete-char-untabify-method 'all)
3228 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
3230 (+ arg (if (zerop wh) 0 (1- wh))))
3234 (defun zap-to-char (arg char)
3235 "Kill up to and including ARGth occurrence of CHAR.
3236 Case is ignored if `case-fold-search' is non-nil in the current buffer.
3237 Goes backward if ARG is negative; error if CHAR not found."
3238 (interactive "p\ncZap to char: ")
3239 ;; Avoid "obsolete" warnings for translation-table-for-input.
3241 (if (char-table-p translation-table-for-input)
3242 (setq char (or (aref translation-table-for-input char) char))))
3243 (kill-region (point) (progn
3244 (search-forward (char-to-string char) nil nil arg)
3245 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
3248 ;; kill-line and its subroutines.
3250 (defcustom kill-whole-line nil
3251 "If non-nil, `kill-line' with no arg at beg of line kills the whole line."
3255 (defun kill-line (&optional arg)
3256 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
3257 With prefix argument ARG, kill that many lines from point.
3258 Negative arguments kill lines backward.
3259 With zero argument, kills the text before point on the current line.
3261 When calling from a program, nil means \"no arg\",
3262 a number counts as a prefix arg.
3264 To kill a whole line, when point is not at the beginning, type \
3265 \\[move-beginning-of-line] \\[kill-line] \\[kill-line].
3267 If `kill-whole-line' is non-nil, then this command kills the whole line
3268 including its terminating newline, when used at the beginning of a line
3269 with no argument. As a consequence, you can always kill a whole line
3270 by typing \\[move-beginning-of-line] \\[kill-line].
3272 If you want to append the killed line to the last killed text,
3273 use \\[append-next-kill] before \\[kill-line].
3275 If the buffer is read-only, Emacs will beep and refrain from deleting
3276 the line, but put the line in the kill ring anyway. This means that
3277 you can use this command to copy text from a read-only buffer.
3278 \(If the variable `kill-read-only-ok' is non-nil, then this won't
3281 (kill-region (point)
3282 ;; It is better to move point to the other end of the kill
3283 ;; before killing. That way, in a read-only buffer, point
3284 ;; moves across the text that is copied to the kill ring.
3285 ;; The choice has no effect on undo now that undo records
3286 ;; the value of point from before the command was run.
3289 (forward-visible-line (prefix-numeric-value arg))
3291 (signal 'end-of-buffer nil))
3294 (end-of-visible-line) (point))))
3295 (if (or (save-excursion
3296 ;; If trailing whitespace is visible,
3297 ;; don't treat it as nothing.
3298 (unless show-trailing-whitespace
3299 (skip-chars-forward " \t" end))
3301 (and kill-whole-line (bolp)))
3302 (forward-visible-line 1)
3306 (defun kill-whole-line (&optional arg)
3308 With prefix ARG, kill that many lines starting from the current line.
3309 If ARG is negative, kill backward. Also kill the preceding newline.
3310 \(This is meant to make \\[repeat] work well with negative arguments.\)
3311 If ARG is zero, kill current line but exclude the trailing newline."
3313 (or arg (setq arg 1))
3314 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
3315 (signal 'end-of-buffer nil))
3316 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
3317 (signal 'beginning-of-buffer nil))
3318 (unless (eq last-command 'kill-region)
3320 (setq last-command 'kill-region))
3322 ;; We need to kill in two steps, because the previous command
3323 ;; could have been a kill command, in which case the text
3324 ;; before point needs to be prepended to the current kill
3325 ;; ring entry and the text after point appended. Also, we
3326 ;; need to use save-excursion to avoid copying the same text
3327 ;; twice to the kill ring in read-only buffers.
3329 (kill-region (point) (progn (forward-visible-line 0) (point))))
3330 (kill-region (point) (progn (end-of-visible-line) (point))))
3333 (kill-region (point) (progn (end-of-visible-line) (point))))
3334 (kill-region (point)
3335 (progn (forward-visible-line (1+ arg))
3336 (unless (bobp) (backward-char))
3340 (kill-region (point) (progn (forward-visible-line 0) (point))))
3341 (kill-region (point)
3342 (progn (forward-visible-line arg) (point))))))
3344 (defun forward-visible-line (arg)
3345 "Move forward by ARG lines, ignoring currently invisible newlines only.
3346 If ARG is negative, move backward -ARG lines.
3347 If ARG is zero, move to the beginning of the current line."
3352 (or (zerop (forward-line 1))
3353 (signal 'end-of-buffer nil))
3354 ;; If the newline we just skipped is invisible,
3357 (get-char-property (1- (point)) 'invisible)))
3358 (if (if (eq buffer-invisibility-spec t)
3360 (or (memq prop buffer-invisibility-spec)
3361 (assq prop buffer-invisibility-spec)))
3362 (setq arg (1+ arg))))
3363 (setq arg (1- arg)))
3364 ;; If invisible text follows, and it is a number of complete lines,
3366 (let ((opoint (point)))
3367 (while (and (not (eobp))
3369 (get-char-property (point) 'invisible)))
3370 (if (eq buffer-invisibility-spec t)
3372 (or (memq prop buffer-invisibility-spec)
3373 (assq prop buffer-invisibility-spec)))))
3375 (if (get-text-property (point) 'invisible)
3376 (or (next-single-property-change (point) 'invisible)
3378 (next-overlay-change (point)))))
3380 (goto-char opoint))))
3382 (while (or first (<= arg 0))
3385 (or (zerop (forward-line -1))
3386 (signal 'beginning-of-buffer nil)))
3387 ;; If the newline we just moved to is invisible,
3391 (get-char-property (1- (point)) 'invisible)))
3392 (unless (if (eq buffer-invisibility-spec t)
3394 (or (memq prop buffer-invisibility-spec)
3395 (assq prop buffer-invisibility-spec)))
3396 (setq arg (1+ arg)))))
3398 ;; If invisible text follows, and it is a number of complete lines,
3400 (let ((opoint (point)))
3401 (while (and (not (bobp))
3403 (get-char-property (1- (point)) 'invisible)))
3404 (if (eq buffer-invisibility-spec t)
3406 (or (memq prop buffer-invisibility-spec)
3407 (assq prop buffer-invisibility-spec)))))
3409 (if (get-text-property (1- (point)) 'invisible)
3410 (or (previous-single-property-change (point) 'invisible)
3412 (previous-overlay-change (point)))))
3414 (goto-char opoint)))))
3415 ((beginning-of-buffer end-of-buffer)
3418 (defun end-of-visible-line ()
3419 "Move to end of current visible line."
3421 ;; If the following character is currently invisible,
3422 ;; skip all characters with that same `invisible' property value,
3423 ;; then find the next newline.
3424 (while (and (not (eobp))
3426 (skip-chars-forward "^\n")
3428 (get-char-property (point) 'invisible)))
3429 (if (eq buffer-invisibility-spec t)
3431 (or (memq prop buffer-invisibility-spec)
3432 (assq prop buffer-invisibility-spec))))))
3433 (skip-chars-forward "^\n")
3434 (if (get-text-property (point) 'invisible)
3435 (goto-char (next-single-property-change (point) 'invisible))
3436 (goto-char (next-overlay-change (point))))
3439 (defun insert-buffer (buffer)
3440 "Insert after point the contents of BUFFER.
3441 Puts mark after the inserted text.
3442 BUFFER may be a buffer or a buffer name.
3444 This function is meant for the user to run interactively.
3445 Don't call it from programs: use `insert-buffer-substring' instead!"
3449 (barf-if-buffer-read-only)
3450 (read-buffer "Insert buffer: "
3451 (if (eq (selected-window) (next-window (selected-window)))
3452 (other-buffer (current-buffer))
3453 (window-buffer (next-window (selected-window))))
3457 (insert-buffer-substring (get-buffer buffer))
3461 (defun append-to-buffer (buffer start end)
3462 "Append to specified buffer the text of the region.
3463 It is inserted into that buffer before its point.
3465 When calling from a program, give three arguments:
3466 BUFFER (or buffer name), START and END.
3467 START and END specify the portion of the current buffer to be copied."
3469 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
3470 (region-beginning) (region-end)))
3471 (let ((oldbuf (current-buffer)))
3472 (let* ((append-to (get-buffer-create buffer))
3473 (windows (get-buffer-window-list append-to t t))
3475 (with-current-buffer append-to
3476 (setq point (point))
3477 (barf-if-buffer-read-only)
3478 (insert-buffer-substring oldbuf start end)
3479 (dolist (window windows)
3480 (when (= (window-point window) point)
3481 (set-window-point window (point))))))))
3483 (defun prepend-to-buffer (buffer start end)
3484 "Prepend to specified buffer the text of the region.
3485 It is inserted into that buffer after its point.
3487 When calling from a program, give three arguments:
3488 BUFFER (or buffer name), START and END.
3489 START and END specify the portion of the current buffer to be copied."
3490 (interactive "BPrepend to buffer: \nr")
3491 (let ((oldbuf (current-buffer)))
3492 (with-current-buffer (get-buffer-create buffer)
3493 (barf-if-buffer-read-only)
3495 (insert-buffer-substring oldbuf start end)))))
3497 (defun copy-to-buffer (buffer start end)
3498 "Copy to specified buffer the text of the region.
3499 It is inserted into that buffer, replacing existing text there.
3501 When calling from a program, give three arguments:
3502 BUFFER (or buffer name), START and END.
3503 START and END specify the portion of the current buffer to be copied."
3504 (interactive "BCopy to buffer: \nr")
3505 (let ((oldbuf (current-buffer)))
3506 (with-current-buffer (get-buffer-create buffer)
3507 (barf-if-buffer-read-only)
3510 (insert-buffer-substring oldbuf start end)))))
3512 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3513 (put 'mark-inactive 'error-message (purecopy "The mark is not active now"))
3515 (defvar activate-mark-hook nil
3516 "Hook run when the mark becomes active.
3517 It is also run at the end of a command, if the mark is active and
3518 it is possible that the region may have changed.")
3520 (defvar deactivate-mark-hook nil
3521 "Hook run when the mark becomes inactive.")
3523 (defun mark (&optional force)
3524 "Return this buffer's mark value as integer, or nil if never set.
3526 In Transient Mark mode, this function signals an error if
3527 the mark is not active. However, if `mark-even-if-inactive' is non-nil,
3528 or the argument FORCE is non-nil, it disregards whether the mark
3529 is active, and returns an integer or nil in the usual way.
3531 If you are using this in an editing command, you are most likely making
3532 a mistake; see the documentation of `set-mark'."
3533 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3534 (marker-position (mark-marker))
3535 (signal 'mark-inactive nil)))
3537 (defcustom select-active-regions nil
3538 "If non-nil, an active region automatically becomes the window selection."
3543 (declare-function x-selection-owner-p "xselect.c" (&optional selection))
3545 ;; Many places set mark-active directly, and several of them failed to also
3546 ;; run deactivate-mark-hook. This shorthand should simplify.
3547 (defsubst deactivate-mark (&optional force)
3548 "Deactivate the mark by setting `mark-active' to nil.
3549 Unless FORCE is non-nil, this function does nothing if Transient
3550 Mark mode is disabled.
3551 This function also runs `deactivate-mark-hook'."
3552 (when (or transient-mark-mode force)
3553 ;; Copy the latest region into the primary selection, if desired.
3554 (and select-active-regions
3556 (display-selections-p)
3557 (x-selection-owner-p 'PRIMARY)
3558 (x-set-selection 'PRIMARY (buffer-substring-no-properties
3559 (region-beginning) (region-end))))
3560 (if (and (null force)
3561 (or (eq transient-mark-mode 'lambda)
3562 (and (eq (car-safe transient-mark-mode) 'only)
3563 (null (cdr transient-mark-mode)))))
3564 ;; When deactivating a temporary region, don't change
3565 ;; `mark-active' or run `deactivate-mark-hook'.
3566 (setq transient-mark-mode nil)
3567 (if (eq (car-safe transient-mark-mode) 'only)
3568 (setq transient-mark-mode (cdr transient-mark-mode)))
3569 (setq mark-active nil)
3570 (run-hooks 'deactivate-mark-hook))))
3572 (defun activate-mark ()
3573 "Activate the mark."
3575 (setq mark-active t)
3576 (unless transient-mark-mode
3577 (setq transient-mark-mode 'lambda))
3578 (when (and select-active-regions
3579 (display-selections-p))
3580 (x-set-selection 'PRIMARY (current-buffer)))))
3582 (defun set-mark (pos)
3583 "Set this buffer's mark to POS. Don't use this function!
3584 That is to say, don't use this function unless you want
3585 the user to see that the mark has moved, and you want the previous
3586 mark position to be lost.
3588 Normally, when a new mark is set, the old one should go on the stack.
3589 This is why most applications should use `push-mark', not `set-mark'.
3591 Novice Emacs Lisp programmers often try to use the mark for the wrong
3592 purposes. The mark saves a location for the user's convenience.
3593 Most editing commands should not alter the mark.
3594 To remember a location for internal use in the Lisp program,
3595 store it in a Lisp variable. Example:
3597 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3601 (setq mark-active t)
3602 (run-hooks 'activate-mark-hook)
3603 (when (and select-active-regions
3604 (display-selections-p))
3605 (x-set-selection 'PRIMARY (current-buffer)))
3606 (set-marker (mark-marker) pos (current-buffer)))
3607 ;; Normally we never clear mark-active except in Transient Mark mode.
3608 ;; But when we actually clear out the mark value too, we must
3609 ;; clear mark-active in any mode.
3611 (set-marker (mark-marker) nil)))
3613 (defcustom use-empty-active-region nil
3614 "Whether \"region-aware\" commands should act on empty regions.
3615 If nil, region-aware commands treat empty regions as inactive.
3616 If non-nil, region-aware commands treat the region as active as
3617 long as the mark is active, even if the region is empty.
3619 Region-aware commands are those that act on the region if it is
3620 active and Transient Mark mode is enabled, and on the text near
3624 :group 'editing-basics)
3626 (defun use-region-p ()
3627 "Return t if the region is active and it is appropriate to act on it.
3628 This is used by commands that act specially on the region under
3629 Transient Mark mode.
3631 The return value is t provided Transient Mark mode is enabled and
3632 the mark is active; and, when `use-empty-active-region' is
3633 non-nil, provided the region is empty. Otherwise, the return
3636 For some commands, it may be appropriate to ignore the value of
3637 `use-empty-active-region'; in that case, use `region-active-p'."
3638 (and (region-active-p)
3639 (or use-empty-active-region (> (region-end) (region-beginning)))))
3641 (defun region-active-p ()
3642 "Return t if Transient Mark mode is enabled and the mark is active.
3644 Some commands act specially on the region when Transient Mark
3645 mode is enabled. Usually, such commands should use
3646 `use-region-p' instead of this function, because `use-region-p'
3647 also checks the value of `use-empty-active-region'."
3648 (and transient-mark-mode mark-active))
3650 (defvar mark-ring nil
3651 "The list of former marks of the current buffer, most recent first.")
3652 (make-variable-buffer-local 'mark-ring)
3653 (put 'mark-ring 'permanent-local t)
3655 (defcustom mark-ring-max 16
3656 "Maximum size of mark ring. Start discarding off end if gets this big."
3658 :group 'editing-basics)
3660 (defvar global-mark-ring nil
3661 "The list of saved global marks, most recent first.")
3663 (defcustom global-mark-ring-max 16
3664 "Maximum size of global mark ring. \
3665 Start discarding off end if gets this big."
3667 :group 'editing-basics)
3669 (defun pop-to-mark-command ()
3670 "Jump to mark, and pop a new position for mark off the ring.
3671 \(Does not affect global mark ring\)."
3674 (error "No mark set in this buffer")
3675 (if (= (point) (mark t))
3676 (message "Mark popped"))
3677 (goto-char (mark t))
3680 (defun push-mark-command (arg &optional nomsg)
3681 "Set mark at where point is.
3682 If no prefix ARG and mark is already set there, just activate it.
3683 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3685 (let ((mark (marker-position (mark-marker))))
3686 (if (or arg (null mark) (/= mark (point)))
3687 (push-mark nil nomsg t)
3688 (setq mark-active t)
3689 (run-hooks 'activate-mark-hook)
3691 (message "Mark activated")))))
3693 (defcustom set-mark-command-repeat-pop nil
3694 "Non-nil means repeating \\[set-mark-command] after popping mark pops it again.
3695 That means that C-u \\[set-mark-command] \\[set-mark-command]
3696 will pop the mark twice, and
3697 C-u \\[set-mark-command] \\[set-mark-command] \\[set-mark-command]
3698 will pop the mark three times.
3700 A value of nil means \\[set-mark-command]'s behavior does not change
3701 after C-u \\[set-mark-command]."
3703 :group 'editing-basics)
3705 (defcustom set-mark-default-inactive nil
3706 "If non-nil, setting the mark does not activate it.
3707 This causes \\[set-mark-command] and \\[exchange-point-and-mark] to
3708 behave the same whether or not `transient-mark-mode' is enabled."
3710 :group 'editing-basics
3713 (defun set-mark-command (arg)
3714 "Set the mark where point is, or jump to the mark.
3715 Setting the mark also alters the region, which is the text
3716 between point and mark; this is the closest equivalent in
3717 Emacs to what some editors call the \"selection\".
3719 With no prefix argument, set the mark at point, and push the
3720 old mark position on local mark ring. Also push the old mark on
3721 global mark ring, if the previous mark was set in another buffer.
3723 When Transient Mark Mode is off, immediately repeating this
3724 command activates `transient-mark-mode' temporarily.
3726 With prefix argument \(e.g., \\[universal-argument] \\[set-mark-command]\), \
3727 jump to the mark, and set the mark from
3728 position popped off the local mark ring \(this does not affect the global
3729 mark ring\). Use \\[pop-global-mark] to jump to a mark popped off the global
3730 mark ring \(see `pop-global-mark'\).
3732 If `set-mark-command-repeat-pop' is non-nil, repeating
3733 the \\[set-mark-command] command with no prefix argument pops the next position
3734 off the local (or global) mark ring and jumps there.
3736 With \\[universal-argument] \\[universal-argument] as prefix
3737 argument, unconditionally set mark where point is, even if
3738 `set-mark-command-repeat-pop' is non-nil.
3740 Novice Emacs Lisp programmers often try to use the mark for the wrong
3741 purposes. See the documentation of `set-mark' for more information."
3743 (cond ((eq transient-mark-mode 'lambda)
3744 (setq transient-mark-mode nil))
3745 ((eq (car-safe transient-mark-mode) 'only)
3748 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3749 (push-mark-command nil))
3750 ((not (eq this-command 'set-mark-command))
3752 (pop-to-mark-command)
3753 (push-mark-command t)))
3754 ((and set-mark-command-repeat-pop
3755 (eq last-command 'pop-to-mark-command))
3756 (setq this-command 'pop-to-mark-command)
3757 (pop-to-mark-command))
3758 ((and set-mark-command-repeat-pop
3759 (eq last-command 'pop-global-mark)
3761 (setq this-command 'pop-global-mark)
3764 (setq this-command 'pop-to-mark-command)
3765 (pop-to-mark-command))
3766 ((eq last-command 'set-mark-command)
3767 (if (region-active-p)
3770 (message "Mark deactivated"))
3772 (message "Mark activated")))
3774 (push-mark-command nil)
3775 (if set-mark-default-inactive (deactivate-mark)))))
3777 (defun push-mark (&optional location nomsg activate)
3778 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3779 If the last global mark pushed was not in the current buffer,
3780 also push LOCATION on the global mark ring.
3781 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3783 Novice Emacs Lisp programmers often try to use the mark for the wrong
3784 purposes. See the documentation of `set-mark' for more information.
3786 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil."
3787 (unless (null (mark t))
3788 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3789 (when (> (length mark-ring) mark-ring-max)
3790 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3791 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3792 (set-marker (mark-marker) (or location (point)) (current-buffer))
3793 ;; Now push the mark on the global mark ring.
3794 (if (and global-mark-ring
3795 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3796 ;; The last global mark pushed was in this same buffer.
3797 ;; Don't push another one.
3799 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3800 (when (> (length global-mark-ring) global-mark-ring-max)
3801 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3802 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3803 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3804 (message "Mark set"))
3805 (if (or activate (not transient-mark-mode))
3806 (set-mark (mark t)))
3810 "Pop off mark ring into the buffer's actual mark.
3811 Does not set point. Does nothing if mark ring is empty."
3813 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3814 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3815 (move-marker (car mark-ring) nil)
3816 (if (null (mark t)) (ding))
3817 (setq mark-ring (cdr mark-ring)))
3820 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3821 (defun exchange-point-and-mark (&optional arg)
3822 "Put the mark where point is now, and point where the mark is now.
3823 This command works even when the mark is not active,
3824 and it reactivates the mark.
3826 If Transient Mark mode is on, a prefix ARG deactivates the mark
3827 if it is active, and otherwise avoids reactivating it. If
3828 Transient Mark mode is off, a prefix ARG enables Transient Mark
3831 (let ((omark (mark t))
3832 (temp-highlight (eq (car-safe transient-mark-mode) 'only)))
3834 (error "No mark set in this buffer"))
3838 (if set-mark-default-inactive (deactivate-mark))
3839 (cond (temp-highlight
3840 (setq transient-mark-mode (cons 'only transient-mark-mode)))
3841 ((or (and arg (region-active-p)) ; (xor arg (not (region-active-p)))
3842 (not (or arg (region-active-p))))
3844 (t (activate-mark)))
3847 (defcustom shift-select-mode t
3848 "When non-nil, shifted motion keys activate the mark momentarily.
3850 While the mark is activated in this way, any shift-translated point
3851 motion key extends the region, and if Transient Mark mode was off, it
3852 is temporarily turned on. Furthermore, the mark will be deactivated
3853 by any subsequent point motion key that was not shift-translated, or
3854 by any action that normally deactivates the mark in Transient Mark mode.
3856 See `this-command-keys-shift-translated' for the meaning of
3859 :group 'editing-basics)
3861 (defun handle-shift-selection ()
3862 "Activate/deactivate mark depending on invocation thru shift translation.
3863 This function is called by `call-interactively' when a command
3864 with a `^' character in its `interactive' spec is invoked, before
3865 running the command itself.
3867 If `shift-select-mode' is enabled and the command was invoked
3868 through shift translation, set the mark and activate the region
3869 temporarily, unless it was already set in this way. See
3870 `this-command-keys-shift-translated' for the meaning of shift
3873 Otherwise, if the region has been activated temporarily,
3874 deactivate it, and restore the variable `transient-mark-mode' to
3876 (cond ((and shift-select-mode this-command-keys-shift-translated)
3877 (unless (and mark-active
3878 (eq (car-safe transient-mark-mode) 'only))
3879 (setq transient-mark-mode
3881 (unless (eq transient-mark-mode 'lambda)
3882 transient-mark-mode)))
3883 (push-mark nil nil t)))
3884 ((eq (car-safe transient-mark-mode) 'only)
3885 (setq transient-mark-mode (cdr transient-mark-mode))
3886 (deactivate-mark))))
3888 (define-minor-mode transient-mark-mode
3889 "Toggle Transient Mark mode.
3890 With ARG, turn Transient Mark mode on if ARG is positive, off otherwise.
3892 In Transient Mark mode, when the mark is active, the region is highlighted.
3893 Changing the buffer \"deactivates\" the mark.
3894 So do certain other operations that set the mark
3895 but whose main purpose is something else--for example,
3896 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3898 You can also deactivate the mark by typing \\[keyboard-quit] or
3899 \\[keyboard-escape-quit].
3901 Many commands change their behavior when Transient Mark mode is in effect
3902 and the mark is active, by acting on the region instead of their usual
3903 default part of the buffer's text. Examples of such commands include
3904 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3905 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3906 Invoke \\[apropos-documentation] and type \"transient\" or
3907 \"mark.*active\" at the prompt, to see the documentation of
3908 commands which are sensitive to the Transient Mark mode."
3910 :init-value (not noninteractive)
3911 :initialize 'custom-initialize-delay
3912 :group 'editing-basics)
3914 ;; The variable transient-mark-mode is ugly: it can take on special
3915 ;; values. Document these here.
3916 (defvar transient-mark-mode t
3917 "*Non-nil if Transient Mark mode is enabled.
3918 See the command `transient-mark-mode' for a description of this minor mode.
3920 Non-nil also enables highlighting of the region whenever the mark is active.
3921 The variable `highlight-nonselected-windows' controls whether to highlight
3922 all windows or just the selected window.
3924 If the value is `lambda', that enables Transient Mark mode temporarily.
3925 After any subsequent action that would normally deactivate the mark
3926 \(such as buffer modification), Transient Mark mode is turned off.
3928 If the value is (only . OLDVAL), that enables Transient Mark mode
3929 temporarily. After any subsequent point motion command that is not
3930 shift-translated, or any other action that would normally deactivate
3931 the mark (such as buffer modification), the value of
3932 `transient-mark-mode' is set to OLDVAL.")
3934 (defvar widen-automatically t
3935 "Non-nil means it is ok for commands to call `widen' when they want to.
3936 Some commands will do this in order to go to positions outside
3937 the current accessible part of the buffer.
3939 If `widen-automatically' is nil, these commands will do something else
3940 as a fallback, and won't change the buffer bounds.")
3942 (defun pop-global-mark ()
3943 "Pop off global mark ring and jump to the top location."
3945 ;; Pop entries which refer to non-existent buffers.
3946 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3947 (setq global-mark-ring (cdr global-mark-ring)))
3948 (or global-mark-ring
3949 (error "No global mark set"))
3950 (let* ((marker (car global-mark-ring))
3951 (buffer (marker-buffer marker))
3952 (position (marker-position marker)))
3953 (setq global-mark-ring (nconc (cdr global-mark-ring)
3954 (list (car global-mark-ring))))
3956 (or (and (>= position (point-min))
3957 (<= position (point-max)))
3958 (if widen-automatically
3960 (error "Global mark position is outside accessible part of buffer")))
3961 (goto-char position)
3962 (switch-to-buffer buffer)))
3964 (defcustom next-line-add-newlines nil
3965 "If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3968 :group 'editing-basics)
3970 (defun next-line (&optional arg try-vscroll)
3971 "Move cursor vertically down ARG lines.
3972 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3973 If there is no character in the target line exactly under the current column,
3974 the cursor is positioned after the character in that line which spans this
3975 column, or at the end of the line if it is not long enough.
3976 If there is no line in the buffer after this one, behavior depends on the
3977 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3978 to create a line, and moves the cursor to that line. Otherwise it moves the
3979 cursor to the end of the buffer.
3981 If the variable `line-move-visual' is non-nil, this command moves
3982 by display lines. Otherwise, it moves by buffer lines, without
3983 taking variable-width characters or continued lines into account.
3985 The command \\[set-goal-column] can be used to create
3986 a semipermanent goal column for this command.
3987 Then instead of trying to move exactly vertically (or as close as possible),
3988 this command moves to the specified goal column (or as close as possible).
3989 The goal column is stored in the variable `goal-column', which is nil
3990 when there is no goal column.
3992 If you are thinking of using this in a Lisp program, consider
3993 using `forward-line' instead. It is usually easier to use
3994 and more reliable (no dependence on goal column, etc.)."
3995 (interactive "^p\np")
3996 (or arg (setq arg 1))
3997 (if (and next-line-add-newlines (= arg 1))
3998 (if (save-excursion (end-of-line) (eobp))
3999 ;; When adding a newline, don't expand an abbrev.
4000 (let ((abbrev-mode nil))
4002 (insert (if use-hard-newlines hard-newline "\n")))
4003 (line-move arg nil nil try-vscroll))
4004 (if (called-interactively-p 'interactive)
4006 (line-move arg nil nil try-vscroll)
4007 ((beginning-of-buffer end-of-buffer) (ding)))
4008 (line-move arg nil nil try-vscroll)))
4011 (defun previous-line (&optional arg try-vscroll)
4012 "Move cursor vertically up ARG lines.
4013 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
4014 If there is no character in the target line exactly over the current column,
4015 the cursor is positioned after the character in that line which spans this
4016 column, or at the end of the line if it is not long enough.
4018 If the variable `line-move-visual' is non-nil, this command moves
4019 by display lines. Otherwise, it moves by buffer lines, without
4020 taking variable-width characters or continued lines into account.
4022 The command \\[set-goal-column] can be used to create
4023 a semipermanent goal column for this command.
4024 Then instead of trying to move exactly vertically (or as close as possible),
4025 this command moves to the specified goal column (or as close as possible).
4026 The goal column is stored in the variable `goal-column', which is nil
4027 when there is no goal column.
4029 If you are thinking of using this in a Lisp program, consider using
4030 `forward-line' with a negative argument instead. It is usually easier
4031 to use and more reliable (no dependence on goal column, etc.)."
4032 (interactive "^p\np")
4033 (or arg (setq arg 1))
4034 (if (called-interactively-p 'interactive)
4036 (line-move (- arg) nil nil try-vscroll)
4037 ((beginning-of-buffer end-of-buffer) (ding)))
4038 (line-move (- arg) nil nil try-vscroll))
4041 (defcustom track-eol nil
4042 "Non-nil means vertical motion starting at end of line keeps to ends of lines.
4043 This means moving to the end of each line moved onto.
4044 The beginning of a blank line does not count as the end of a line.
4045 This has no effect when `line-move-visual' is non-nil."
4047 :group 'editing-basics)
4049 (defcustom goal-column nil
4050 "Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
4051 :type '(choice integer
4052 (const :tag "None" nil))
4053 :group 'editing-basics)
4054 (make-variable-buffer-local 'goal-column)
4056 (defvar temporary-goal-column 0
4057 "Current goal column for vertical motion.
4058 It is the column where point was at the start of the current run
4059 of vertical motion commands.
4061 When moving by visual lines via `line-move-visual', it is a cons
4062 cell (COL . HSCROLL), where COL is the x-position, in pixels,
4063 divided by the default column width, and HSCROLL is the number of
4064 columns by which window is scrolled from left margin.
4066 When the `track-eol' feature is doing its job, the value is
4067 `most-positive-fixnum'.")
4069 (defcustom line-move-ignore-invisible t
4070 "Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
4071 Outline mode sets this."
4073 :group 'editing-basics)
4075 (defcustom line-move-visual t
4076 "When non-nil, `line-move' moves point by visual lines.
4077 This movement is based on where the cursor is displayed on the
4078 screen, instead of relying on buffer contents alone. It takes
4079 into account variable-width characters and line continuation."
4081 :group 'editing-basics)
4083 ;; Returns non-nil if partial move was done.
4084 (defun line-move-partial (arg noerror to-end)
4086 ;; Move backward (up).
4087 ;; If already vscrolled, reduce vscroll
4088 (let ((vs (window-vscroll nil t)))
4089 (when (> vs (frame-char-height))
4090 (set-window-vscroll nil (- vs (frame-char-height)) t)))
4092 ;; Move forward (down).
4093 (let* ((lh (window-line-height -1))
4099 (>= rbot (frame-char-height))
4100 (<= ypos (- (frame-char-height))))
4102 (let ((wend (pos-visible-in-window-p t nil t)))
4103 (setq rbot (nth 3 wend)
4104 vpos (nth 5 wend))))
4106 ;; If last line of window is fully visible, move forward.
4107 ((or (null rbot) (= rbot 0))
4109 ;; If cursor is not in the bottom scroll margin, move forward.
4112 (or (nth 1 (window-line-height))
4113 (let ((ppos (posn-at-point)))
4114 (cdr (or (posn-actual-col-row ppos)
4115 (posn-col-row ppos))))))
4116 (min (- (window-text-height) scroll-margin 1) (1- vpos))))
4118 ;; When already vscrolled, we vscroll some more if we can,
4119 ;; or clear vscroll and move forward at end of tall image.
4120 ((> (setq vs (window-vscroll nil t)) 0)
4122 (set-window-vscroll nil (+ vs (min rbot (frame-char-height))) t)))
4123 ;; If cursor just entered the bottom scroll margin, move forward,
4124 ;; but also vscroll one line so redisplay wont recenter.
4126 (= py (min (- (window-text-height) scroll-margin 1)
4128 (set-window-vscroll nil (frame-char-height) t)
4129 (line-move-1 arg noerror to-end)
4131 ;; If there are lines above the last line, scroll-up one line.
4135 ;; Finally, start vscroll.
4137 (set-window-vscroll nil (frame-char-height) t)))))))
4140 ;; This is like line-move-1 except that it also performs
4141 ;; vertical scrolling of tall images if appropriate.
4142 ;; That is not really a clean thing to do, since it mixes
4143 ;; scrolling with cursor motion. But so far we don't have
4144 ;; a cleaner solution to the problem of making C-n do something
4145 ;; useful given a tall image.
4146 (defun line-move (arg &optional noerror to-end try-vscroll)
4147 (unless (and auto-window-vscroll try-vscroll
4148 ;; Only vscroll for single line moves
4150 ;; But don't vscroll in a keyboard macro.
4151 (not defining-kbd-macro)
4152 (not executing-kbd-macro)
4153 (line-move-partial arg noerror to-end))
4154 (set-window-vscroll nil 0 t)
4155 (if line-move-visual
4156 (line-move-visual arg noerror)
4157 (line-move-1 arg noerror to-end))))
4159 ;; Display-based alternative to line-move-1.
4160 ;; Arg says how many lines to move. The value is t if we can move the
4161 ;; specified number of lines.
4162 (defun line-move-visual (arg &optional noerror)
4163 (let ((opoint (point))
4164 (hscroll (window-hscroll))
4166 ;; Check if the previous command was a line-motion command, or if
4167 ;; we were called from some other command.
4168 (if (and (consp temporary-goal-column)
4169 (memq last-command `(next-line previous-line ,this-command)))
4170 ;; If so, there's no need to reset `temporary-goal-column',
4171 ;; but we may need to hscroll.
4172 (if (or (/= (cdr temporary-goal-column) hscroll)
4173 (> (cdr temporary-goal-column) 0))
4174 (setq target-hscroll (cdr temporary-goal-column)))
4175 ;; Otherwise, we should reset `temporary-goal-column'.
4176 (let ((posn (posn-at-point)))
4178 ;; Handle the `overflow-newline-into-fringe' case:
4179 ((eq (nth 1 posn) 'right-fringe)
4180 (setq temporary-goal-column (cons (- (window-width) 1) hscroll)))
4181 ((car (posn-x-y posn))
4182 (setq temporary-goal-column
4183 (cons (/ (float (car (posn-x-y posn)))
4184 (frame-char-width)) hscroll))))))
4186 (set-window-hscroll (selected-window) target-hscroll))
4187 (or (and (= (vertical-motion
4188 (cons (or goal-column
4189 (if (consp temporary-goal-column)
4190 (truncate (car temporary-goal-column))
4191 temporary-goal-column))
4196 ;; If the goal column lies on a display string,
4197 ;; `vertical-motion' advances the cursor to the end
4198 ;; of the string. For arg < 0, this can cause the
4199 ;; cursor to get stuck. (Bug#3020).
4200 (= (vertical-motion arg) arg)))
4202 (signal (if (< arg 0) 'beginning-of-buffer 'end-of-buffer)
4205 ;; This is the guts of next-line and previous-line.
4206 ;; Arg says how many lines to move.
4207 ;; The value is t if we can move the specified number of lines.
4208 (defun line-move-1 (arg &optional noerror to-end)
4209 ;; Don't run any point-motion hooks, and disregard intangibility,
4210 ;; for intermediate positions.
4211 (let ((inhibit-point-motion-hooks t)
4214 (if (consp temporary-goal-column)
4215 (setq temporary-goal-column (+ (car temporary-goal-column)
4216 (cdr temporary-goal-column))))
4219 (if (not (memq last-command '(next-line previous-line)))
4220 (setq temporary-goal-column
4221 (if (and track-eol (eolp)
4222 ;; Don't count beg of empty line as end of line
4223 ;; unless we just did explicit end-of-line.
4224 (or (not (bolp)) (eq last-command 'move-end-of-line)))
4225 most-positive-fixnum
4228 (if (not (or (integerp selective-display)
4229 line-move-ignore-invisible))
4230 ;; Use just newline characters.
4231 ;; Set ARG to 0 if we move as many lines as requested.
4233 (progn (if (> arg 1) (forward-line (1- arg)))
4234 ;; This way of moving forward ARG lines
4235 ;; verifies that we have a newline after the last one.
4236 ;; It doesn't get confused by intangible text.
4238 (if (zerop (forward-line 1))
4240 (and (zerop (forward-line arg))
4244 (signal (if (< arg 0)
4245 'beginning-of-buffer
4248 ;; Move by arg lines, but ignore invisible ones.
4250 (while (and (> arg 0) (not done))
4251 ;; If the following character is currently invisible,
4252 ;; skip all characters with that same `invisible' property value.
4253 (while (and (not (eobp)) (invisible-p (point)))
4254 (goto-char (next-char-property-change (point))))
4256 ;; We don't use `end-of-line', since we want to escape
4257 ;; from field boundaries ocurring exactly at point.
4258 (goto-char (constrain-to-field
4259 (let ((inhibit-field-text-motion t))
4260 (line-end-position))
4262 'inhibit-line-move-field-capture))
4263 ;; If there's no invisibility here, move over the newline.
4267 (signal 'end-of-buffer nil)
4269 ((and (> arg 1) ;; Use vertical-motion for last move
4270 (not (integerp selective-display))
4271 (not (invisible-p (point))))
4272 ;; We avoid vertical-motion when possible
4273 ;; because that has to fontify.
4275 ;; Otherwise move a more sophisticated way.
4276 ((zerop (vertical-motion 1))
4278 (signal 'end-of-buffer nil)
4281 (setq arg (1- arg))))
4282 ;; The logic of this is the same as the loop above,
4283 ;; it just goes in the other direction.
4284 (while (and (< arg 0) (not done))
4285 ;; For completely consistency with the forward-motion
4286 ;; case, we should call beginning-of-line here.
4287 ;; However, if point is inside a field and on a
4288 ;; continued line, the call to (vertical-motion -1)
4289 ;; below won't move us back far enough; then we return
4290 ;; to the same column in line-move-finish, and point
4291 ;; gets stuck -- cyd
4296 (signal 'beginning-of-buffer nil)
4298 ((and (< arg -1) ;; Use vertical-motion for last move
4299 (not (integerp selective-display))
4300 (not (invisible-p (1- (point)))))
4302 ((zerop (vertical-motion -1))
4304 (signal 'beginning-of-buffer nil)
4308 (while (and ;; Don't move over previous invis lines
4309 ;; if our target is the middle of this line.
4310 (or (zerop (or goal-column temporary-goal-column))
4312 (not (bobp)) (invisible-p (1- (point))))
4313 (goto-char (previous-char-property-change (point))))))))
4314 ;; This is the value the function returns.
4318 ;; If we did not move down as far as desired, at least go
4319 ;; to end of line. Be sure to call point-entered and
4320 ;; point-left-hooks.
4321 (let* ((npoint (prog1 (line-end-position)
4322 (goto-char opoint)))
4323 (inhibit-point-motion-hooks nil))
4324 (goto-char npoint)))
4326 ;; If we did not move up as far as desired,
4327 ;; at least go to beginning of line.
4328 (let* ((npoint (prog1 (line-beginning-position)
4329 (goto-char opoint)))
4330 (inhibit-point-motion-hooks nil))
4331 (goto-char npoint)))
4333 (line-move-finish (or goal-column temporary-goal-column)
4334 opoint (> orig-arg 0)))))))
4336 (defun line-move-finish (column opoint forward)
4339 ;; Set REPEAT to t to repeat the whole thing.
4344 (line-beg (save-excursion (beginning-of-line) (point)))
4346 ;; Compute the end of the line
4347 ;; ignoring effectively invisible newlines.
4349 ;; Like end-of-line but ignores fields.
4350 (skip-chars-forward "^\n")
4351 (while (and (not (eobp)) (invisible-p (point)))
4352 (goto-char (next-char-property-change (point)))
4353 (skip-chars-forward "^\n"))
4356 ;; Move to the desired column.
4357 (line-move-to-column (truncate column))
4359 ;; Corner case: suppose we start out in a field boundary in
4360 ;; the middle of a continued line. When we get to
4361 ;; line-move-finish, point is at the start of a new *screen*
4362 ;; line but the same text line; then line-move-to-column would
4363 ;; move us backwards. Test using C-n with point on the "x" in
4364 ;; (insert "a" (propertize "x" 'field t) (make-string 89 ?y))
4371 ;; Process intangibility within a line.
4372 ;; With inhibit-point-motion-hooks bound to nil, a call to
4373 ;; goto-char moves point past intangible text.
4375 ;; However, inhibit-point-motion-hooks controls both the
4376 ;; intangibility and the point-entered/point-left hooks. The
4377 ;; following hack avoids calling the point-* hooks
4378 ;; unnecessarily. Note that we move *forward* past intangible
4379 ;; text when the initial and final points are the same.
4381 (let ((inhibit-point-motion-hooks nil))
4384 ;; If intangibility moves us to a different (later) place
4385 ;; in the same line, use that as the destination.
4386 (if (<= (point) line-end)
4388 ;; If that position is "too late",
4389 ;; try the previous allowable position.
4393 ;; If going forward, don't accept the previous
4394 ;; allowable position if it is before the target line.
4395 (< line-beg (point))
4396 ;; If going backward, don't accept the previous
4397 ;; allowable position if it is still after the target line.
4398 (<= (point) line-end))
4400 ;; As a last resort, use the end of the line.
4401 (setq new line-end))))
4403 ;; Now move to the updated destination, processing fields
4404 ;; as well as intangibility.
4406 (let ((inhibit-point-motion-hooks nil))
4408 ;; Ignore field boundaries if the initial and final
4409 ;; positions have the same `field' property, even if the
4410 ;; fields are non-contiguous. This seems to be "nicer"
4411 ;; behavior in many situations.
4412 (if (eq (get-char-property new 'field)
4413 (get-char-property opoint 'field))
4415 (constrain-to-field new opoint t t
4416 'inhibit-line-move-field-capture))))
4418 ;; If all this moved us to a different line,
4419 ;; retry everything within that new line.
4420 (when (or (< (point) line-beg) (> (point) line-end))
4421 ;; Repeat the intangibility and field processing.
4422 (setq repeat t))))))
4424 (defun line-move-to-column (col)
4425 "Try to find column COL, considering invisibility.
4426 This function works only in certain cases,
4427 because what we really need is for `move-to-column'
4428 and `current-column' to be able to ignore invisible text."
4431 (move-to-column col))
4433 (when (and line-move-ignore-invisible
4434 (not (bolp)) (invisible-p (1- (point))))
4435 (let ((normal-location (point))
4436 (normal-column (current-column)))
4437 ;; If the following character is currently invisible,
4438 ;; skip all characters with that same `invisible' property value.
4439 (while (and (not (eobp))
4440 (invisible-p (point)))
4441 (goto-char (next-char-property-change (point))))
4442 ;; Have we advanced to a larger column position?
4443 (if (> (current-column) normal-column)
4444 ;; We have made some progress towards the desired column.
4445 ;; See if we can make any further progress.
4446 (line-move-to-column (+ (current-column) (- col normal-column)))
4447 ;; Otherwise, go to the place we originally found
4448 ;; and move back over invisible text.
4449 ;; that will get us to the same place on the screen
4450 ;; but with a more reasonable buffer position.
4451 (goto-char normal-location)
4452 (let ((line-beg (save-excursion (beginning-of-line) (point))))
4453 (while (and (not (bolp)) (invisible-p (1- (point))))
4454 (goto-char (previous-char-property-change (point) line-beg))))))))
4456 (defun move-end-of-line (arg)
4457 "Move point to end of current line as displayed.
4458 With argument ARG not nil or 1, move forward ARG - 1 lines first.
4459 If point reaches the beginning or end of buffer, it stops there.
4461 To ignore the effects of the `intangible' text or overlay
4462 property, bind `inhibit-point-motion-hooks' to t.
4463 If there is an image in the current line, this function
4464 disregards newlines that are part of the text on which the image
4467 (or arg (setq arg 1))
4472 (let ((goal-column 0)
4473 (line-move-visual nil))
4474 (and (line-move arg t)
4477 (while (and (not (bobp)) (invisible-p (1- (point))))
4478 (goto-char (previous-single-char-property-change
4479 (point) 'invisible)))
4483 (if (and (> (point) newpos)
4484 (eq (preceding-char) ?\n))
4486 (if (and (> (point) newpos) (not (eobp))
4487 (not (eq (following-char) ?\n)))
4488 ;; If we skipped something intangible and now we're not
4489 ;; really at eol, keep going.
4493 (defun move-beginning-of-line (arg)
4494 "Move point to beginning of current line as displayed.
4495 \(If there's an image in the line, this disregards newlines
4496 which are part of the text that the image rests on.)
4498 With argument ARG not nil or 1, move forward ARG - 1 lines first.
4499 If point reaches the beginning or end of buffer, it stops there.
4500 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4502 (or arg (setq arg 1))
4504 (let ((orig (point))
4505 first-vis first-vis-field-value)
4507 ;; Move by lines, if ARG is not 1 (the default).
4509 (let ((line-move-visual nil))
4510 (line-move (1- arg) t)))
4512 ;; Move to beginning-of-line, ignoring fields and invisibles.
4513 (skip-chars-backward "^\n")
4514 (while (and (not (bobp)) (invisible-p (1- (point))))
4515 (goto-char (previous-char-property-change (point)))
4516 (skip-chars-backward "^\n"))
4518 ;; Now find first visible char in the line
4519 (while (and (not (eobp)) (invisible-p (point)))
4520 (goto-char (next-char-property-change (point))))
4521 (setq first-vis (point))
4523 ;; See if fields would stop us from reaching FIRST-VIS.
4524 (setq first-vis-field-value
4525 (constrain-to-field first-vis orig (/= arg 1) t nil))
4527 (goto-char (if (/= first-vis-field-value first-vis)
4528 ;; If yes, obey them.
4529 first-vis-field-value
4530 ;; Otherwise, move to START with attention to fields.
4531 ;; (It is possible that fields never matter in this case.)
4532 (constrain-to-field (point) orig
4533 (/= arg 1) t nil)))))
4536 ;; Many people have said they rarely use this feature, and often type
4537 ;; it by accident. Maybe it shouldn't even be on a key.
4538 (put 'set-goal-column 'disabled t)
4540 (defun set-goal-column (arg)
4541 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
4542 Those commands will move to this position in the line moved to
4543 rather than trying to keep the same horizontal position.
4544 With a non-nil argument ARG, clears out the goal column
4545 so that \\[next-line] and \\[previous-line] resume vertical motion.
4546 The goal column is stored in the variable `goal-column'."
4550 (setq goal-column nil)
4551 (message "No goal column"))
4552 (setq goal-column (current-column))
4553 ;; The older method below can be erroneous if `set-goal-column' is bound
4554 ;; to a sequence containing %
4555 ;;(message (substitute-command-keys
4556 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
4560 (format "Goal column %d " goal-column)
4561 (substitute-command-keys
4562 "(use \\[set-goal-column] with an arg to unset it)")))
4567 ;;; Editing based on visual lines, as opposed to logical lines.
4569 (defun end-of-visual-line (&optional n)
4570 "Move point to end of current visual line.
4571 With argument N not nil or 1, move forward N - 1 visual lines first.
4572 If point reaches the beginning or end of buffer, it stops there.
4573 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4577 (let ((line-move-visual t))
4578 (line-move (1- n) t)))
4579 ;; Unlike `move-beginning-of-line', `move-end-of-line' doesn't
4580 ;; constrain to field boundaries, so we don't either.
4581 (vertical-motion (cons (window-width) 0)))
4583 (defun beginning-of-visual-line (&optional n)
4584 "Move point to beginning of current visual line.
4585 With argument N not nil or 1, move forward N - 1 visual lines first.
4586 If point reaches the beginning or end of buffer, it stops there.
4587 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4590 (let ((opoint (point)))
4592 (let ((line-move-visual t))
4593 (line-move (1- n) t)))
4595 ;; Constrain to field boundaries, like `move-beginning-of-line'.
4596 (goto-char (constrain-to-field (point) opoint (/= n 1)))))
4598 (defun kill-visual-line (&optional arg)
4599 "Kill the rest of the visual line.
4600 With prefix argument ARG, kill that many visual lines from point.
4601 If ARG is negative, kill visual lines backward.
4602 If ARG is zero, kill the text before point on the current visual
4605 If you want to append the killed line to the last killed text,
4606 use \\[append-next-kill] before \\[kill-line].
4608 If the buffer is read-only, Emacs will beep and refrain from deleting
4609 the line, but put the line in the kill ring anyway. This means that
4610 you can use this command to copy text from a read-only buffer.
4611 \(If the variable `kill-read-only-ok' is non-nil, then this won't
4614 ;; Like in `kill-line', it's better to move point to the other end
4615 ;; of the kill before killing.
4616 (let ((opoint (point))
4617 (kill-whole-line (and kill-whole-line (bolp))))
4619 (vertical-motion (prefix-numeric-value arg))
4620 (end-of-visual-line 1)
4621 (if (= (point) opoint)
4623 ;; Skip any trailing whitespace at the end of the visual line.
4624 ;; We used to do this only if `show-trailing-whitespace' is
4625 ;; nil, but that's wrong; the correct thing would be to check
4626 ;; whether the trailing whitespace is highlighted. But, it's
4627 ;; OK to just do this unconditionally.
4628 (skip-chars-forward " \t")))
4629 (kill-region opoint (if (and kill-whole-line (looking-at "\n"))
4633 (defun next-logical-line (&optional arg try-vscroll)
4634 "Move cursor vertically down ARG lines.
4635 This is identical to `next-line', except that it always moves
4636 by logical lines instead of visual lines, ignoring the value of
4637 the variable `line-move-visual'."
4638 (interactive "^p\np")
4639 (let ((line-move-visual nil))
4641 (next-line arg try-vscroll))))
4643 (defun previous-logical-line (&optional arg try-vscroll)
4644 "Move cursor vertically up ARG lines.
4645 This is identical to `previous-line', except that it always moves
4646 by logical lines instead of visual lines, ignoring the value of
4647 the variable `line-move-visual'."
4648 (interactive "^p\np")
4649 (let ((line-move-visual nil))
4651 (previous-line arg try-vscroll))))
4653 (defgroup visual-line nil
4654 "Editing based on visual lines."
4658 (defvar visual-line-mode-map
4659 (let ((map (make-sparse-keymap)))
4660 (define-key map [remap kill-line] 'kill-visual-line)
4661 (define-key map [remap move-beginning-of-line] 'beginning-of-visual-line)
4662 (define-key map [remap move-end-of-line] 'end-of-visual-line)
4663 ;; These keybindings interfere with xterm function keys. Are
4664 ;; there any other suitable bindings?
4665 ;; (define-key map "\M-[" 'previous-logical-line)
4666 ;; (define-key map "\M-]" 'next-logical-line)
4669 (defcustom visual-line-fringe-indicators '(nil nil)
4670 "How fringe indicators are shown for wrapped lines in `visual-line-mode'.
4671 The value should be a list of the form (LEFT RIGHT), where LEFT
4672 and RIGHT are symbols representing the bitmaps to display, to
4673 indicate wrapped lines, in the left and right fringes respectively.
4674 See also `fringe-indicator-alist'.
4675 The default is not to display fringe indicators for wrapped lines.
4676 This variable does not affect fringe indicators displayed for
4678 :type '(list (choice (const :tag "Hide left indicator" nil)
4679 (const :tag "Left curly arrow" left-curly-arrow)
4680 (symbol :tag "Other bitmap"))
4681 (choice (const :tag "Hide right indicator" nil)
4682 (const :tag "Right curly arrow" right-curly-arrow)
4683 (symbol :tag "Other bitmap")))
4684 :set (lambda (symbol value)
4685 (dolist (buf (buffer-list))
4686 (with-current-buffer buf
4687 (when (and (boundp 'visual-line-mode)
4688 (symbol-value 'visual-line-mode))
4689 (setq fringe-indicator-alist
4690 (cons (cons 'continuation value)
4693 (copy-tree fringe-indicator-alist)))))))
4694 (set-default symbol value)))
4696 (defvar visual-line--saved-state nil)
4698 (define-minor-mode visual-line-mode
4699 "Redefine simple editing commands to act on visual lines, not logical lines.
4700 This also turns on `word-wrap' in the buffer."
4701 :keymap visual-line-mode-map
4704 (if visual-line-mode
4706 (set (make-local-variable 'visual-line--saved-state) nil)
4707 ;; Save the local values of some variables, to be restored if
4708 ;; visual-line-mode is turned off.
4709 (dolist (var '(line-move-visual truncate-lines
4710 truncate-partial-width-windows
4711 word-wrap fringe-indicator-alist))
4712 (if (local-variable-p var)
4713 (push (cons var (symbol-value var))
4714 visual-line--saved-state)))
4715 (set (make-local-variable 'line-move-visual) t)
4716 (set (make-local-variable 'truncate-partial-width-windows) nil)
4717 (setq truncate-lines nil
4719 fringe-indicator-alist
4720 (cons (cons 'continuation visual-line-fringe-indicators)
4721 fringe-indicator-alist)))
4722 (kill-local-variable 'line-move-visual)
4723 (kill-local-variable 'word-wrap)
4724 (kill-local-variable 'truncate-lines)
4725 (kill-local-variable 'truncate-partial-width-windows)
4726 (kill-local-variable 'fringe-indicator-alist)
4727 (dolist (saved visual-line--saved-state)
4728 (set (make-local-variable (car saved)) (cdr saved)))
4729 (kill-local-variable 'visual-line--saved-state)))
4731 (defun turn-on-visual-line-mode ()
4732 (visual-line-mode 1))
4734 (define-globalized-minor-mode global-visual-line-mode
4735 visual-line-mode turn-on-visual-line-mode
4738 (defun scroll-other-window-down (lines)
4739 "Scroll the \"other window\" down.
4740 For more details, see the documentation for `scroll-other-window'."
4742 (scroll-other-window
4743 ;; Just invert the argument's meaning.
4744 ;; We can do that without knowing which window it will be.
4745 (if (eq lines '-) nil
4747 (- (prefix-numeric-value lines))))))
4749 (defun beginning-of-buffer-other-window (arg)
4750 "Move point to the beginning of the buffer in the other window.
4751 Leave mark at previous position.
4752 With arg N, put point N/10 of the way from the true beginning."
4754 (let ((orig-window (selected-window))
4755 (window (other-window-for-scrolling)))
4756 ;; We use unwind-protect rather than save-window-excursion
4757 ;; because the latter would preserve the things we want to change.
4760 (select-window window)
4761 ;; Set point and mark in that window's buffer.
4763 (beginning-of-buffer arg))
4764 ;; Set point accordingly.
4766 (select-window orig-window))))
4768 (defun end-of-buffer-other-window (arg)
4769 "Move point to the end of the buffer in the other window.
4770 Leave mark at previous position.
4771 With arg N, put point N/10 of the way from the true end."
4773 ;; See beginning-of-buffer-other-window for comments.
4774 (let ((orig-window (selected-window))
4775 (window (other-window-for-scrolling)))
4778 (select-window window)
4780 (end-of-buffer arg))
4782 (select-window orig-window))))
4784 (defun transpose-chars (arg)
4785 "Interchange characters around point, moving forward one character.
4786 With prefix arg ARG, effect is to take character before point
4787 and drag it forward past ARG other characters (backward if ARG negative).
4788 If no argument and at end of line, the previous two chars are exchanged."
4790 (and (null arg) (eolp) (forward-char -1))
4791 (transpose-subr 'forward-char (prefix-numeric-value arg)))
4793 (defun transpose-words (arg)
4794 "Interchange words around point, leaving point at end of them.
4795 With prefix arg ARG, effect is to take word before or around point
4796 and drag it forward past ARG other words (backward if ARG negative).
4797 If ARG is zero, the words around or after point and around or after mark
4799 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
4801 (transpose-subr 'forward-word arg))
4803 (defun transpose-sexps (arg)
4804 "Like \\[transpose-words] but applies to sexps.
4805 Does not work on a sexp that point is in the middle of
4806 if it is a list or string."
4810 ;; Here we should try to simulate the behavior of
4811 ;; (cons (progn (forward-sexp x) (point))
4812 ;; (progn (forward-sexp (- x)) (point)))
4813 ;; Except that we don't want to rely on the second forward-sexp
4814 ;; putting us back to where we want to be, since forward-sexp-function
4815 ;; might do funny things like infix-precedence.
4817 (looking-at "\\sw\\|\\s_")
4819 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
4820 ;; Jumping over a symbol. We might be inside it, mind you.
4821 (progn (funcall (if (> arg 0)
4822 'skip-syntax-backward 'skip-syntax-forward)
4824 (cons (save-excursion (forward-sexp arg) (point)) (point)))
4825 ;; Otherwise, we're between sexps. Take a step back before jumping
4826 ;; to make sure we'll obey the same precedence no matter which direction
4828 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
4829 (cons (save-excursion (forward-sexp arg) (point))
4830 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
4831 (not (zerop (funcall (if (> arg 0)
4832 'skip-syntax-forward
4833 'skip-syntax-backward)
4838 (defun transpose-lines (arg)
4839 "Exchange current line and previous line, leaving point after both.
4840 With argument ARG, takes previous line and moves it past ARG lines.
4841 With argument 0, interchanges line point is in with line mark is in."
4843 (transpose-subr (function
4847 ;; Move forward over ARG lines,
4848 ;; but create newlines if necessary.
4849 (setq arg (forward-line arg))
4850 (if (/= (preceding-char) ?\n)
4851 (setq arg (1+ arg)))
4854 (forward-line arg))))
4857 (defun transpose-subr (mover arg &optional special)
4858 (let ((aux (if special mover
4860 (cons (progn (funcall mover x) (point))
4861 (progn (funcall mover (- x)) (point))))))
4866 (setq pos1 (funcall aux 1))
4867 (goto-char (or (mark) (error "No mark set in this buffer")))
4868 (setq pos2 (funcall aux 1))
4869 (transpose-subr-1 pos1 pos2))
4870 (exchange-point-and-mark))
4872 (setq pos1 (funcall aux -1))
4873 (setq pos2 (funcall aux arg))
4874 (transpose-subr-1 pos1 pos2)
4875 (goto-char (car pos2)))
4877 (setq pos1 (funcall aux -1))
4878 (goto-char (car pos1))
4879 (setq pos2 (funcall aux arg))
4880 (transpose-subr-1 pos1 pos2)))))
4882 (defun transpose-subr-1 (pos1 pos2)
4883 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
4884 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
4885 (when (> (car pos1) (car pos2))
4887 (setq pos1 pos2 pos2 swap)))
4888 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
4889 (atomic-change-group
4891 ;; FIXME: We first delete the two pieces of text, so markers that
4892 ;; used to point to after the text end up pointing to before it :-(
4893 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
4894 (goto-char (car pos2))
4895 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
4896 (goto-char (car pos1))
4899 (defun backward-word (&optional arg)
4900 "Move backward until encountering the beginning of a word.
4901 With argument ARG, do this that many times."
4903 (forward-word (- (or arg 1))))
4905 (defun mark-word (&optional arg allow-extend)
4906 "Set mark ARG words away from point.
4907 The place mark goes is the same place \\[forward-word] would
4908 move to with the same argument.
4909 Interactively, if this command is repeated
4910 or (in Transient Mark mode) if the mark is active,
4911 it marks the next ARG words after the ones already marked."
4912 (interactive "P\np")
4913 (cond ((and allow-extend
4914 (or (and (eq last-command this-command) (mark t))
4916 (setq arg (if arg (prefix-numeric-value arg)
4917 (if (< (mark) (point)) -1 1)))
4926 (forward-word (prefix-numeric-value arg))
4930 (defun kill-word (arg)
4931 "Kill characters forward until encountering the end of a word.
4932 With argument ARG, do this that many times."
4934 (kill-region (point) (progn (forward-word arg) (point))))
4936 (defun backward-kill-word (arg)
4937 "Kill characters backward until encountering the beginning of a word.
4938 With argument ARG, do this that many times."
4940 (kill-word (- arg)))
4942 (defun current-word (&optional strict really-word)
4943 "Return the symbol or word that point is on (or a nearby one) as a string.
4944 The return value includes no text properties.
4945 If optional arg STRICT is non-nil, return nil unless point is within
4946 or adjacent to a symbol or word. In all cases the value can be nil
4947 if there is no word nearby.
4948 The function, belying its name, normally finds a symbol.
4949 If optional arg REALLY-WORD is non-nil, it finds just a word."
4951 (let* ((oldpoint (point)) (start (point)) (end (point))
4952 (syntaxes (if really-word "w" "w_"))
4953 (not-syntaxes (concat "^" syntaxes)))
4954 (skip-syntax-backward syntaxes) (setq start (point))
4955 (goto-char oldpoint)
4956 (skip-syntax-forward syntaxes) (setq end (point))
4957 (when (and (eq start oldpoint) (eq end oldpoint)
4958 ;; Point is neither within nor adjacent to a word.
4960 ;; Look for preceding word in same line.
4961 (skip-syntax-backward not-syntaxes
4962 (save-excursion (beginning-of-line)
4965 ;; No preceding word in same line.
4966 ;; Look for following word in same line.
4968 (skip-syntax-forward not-syntaxes
4969 (save-excursion (end-of-line)
4971 (setq start (point))
4972 (skip-syntax-forward syntaxes)
4975 (skip-syntax-backward syntaxes)
4976 (setq start (point))))
4977 ;; If we found something nonempty, return it as a string.
4978 (unless (= start end)
4979 (buffer-substring-no-properties start end)))))
4981 (defcustom fill-prefix nil
4982 "String for filling to insert at front of new line, or nil for none."
4983 :type '(choice (const :tag "None" nil)
4986 (make-variable-buffer-local 'fill-prefix)
4987 (put 'fill-prefix 'safe-local-variable 'string-or-null-p)
4989 (defcustom auto-fill-inhibit-regexp nil
4990 "Regexp to match lines which should not be auto-filled."
4991 :type '(choice (const :tag "None" nil)
4995 ;; This function is used as the auto-fill-function of a buffer
4996 ;; when Auto-Fill mode is enabled.
4997 ;; It returns t if it really did any work.
4998 ;; (Actually some major modes use a different auto-fill function,
4999 ;; but this one is the default one.)
5000 (defun do-auto-fill ()
5001 (let (fc justify give-up
5002 (fill-prefix fill-prefix))
5003 (if (or (not (setq justify (current-justification)))
5004 (null (setq fc (current-fill-column)))
5005 (and (eq justify 'left)
5006 (<= (current-column) fc))
5007 (and auto-fill-inhibit-regexp
5008 (save-excursion (beginning-of-line)
5009 (looking-at auto-fill-inhibit-regexp))))
5010 nil ;; Auto-filling not required
5011 (if (memq justify '(full center right))
5012 (save-excursion (unjustify-current-line)))
5014 ;; Choose a fill-prefix automatically.
5015 (when (and adaptive-fill-mode
5016 (or (null fill-prefix) (string= fill-prefix "")))
5018 (fill-context-prefix
5019 (save-excursion (backward-paragraph 1) (point))
5020 (save-excursion (forward-paragraph 1) (point)))))
5021 (and prefix (not (equal prefix ""))
5022 ;; Use auto-indentation rather than a guessed empty prefix.
5023 (not (and fill-indent-according-to-mode
5024 (string-match "\\`[ \t]*\\'" prefix)))
5025 (setq fill-prefix prefix))))
5027 (while (and (not give-up) (> (current-column) fc))
5028 ;; Determine where to split the line.
5033 (setq after-prefix (point))
5035 (looking-at (regexp-quote fill-prefix))
5036 (setq after-prefix (match-end 0)))
5037 (move-to-column (1+ fc))
5038 (fill-move-to-break-point after-prefix)
5041 ;; See whether the place we found is any good.
5043 (goto-char fill-point)
5045 ;; There is no use breaking at end of line.
5046 (save-excursion (skip-chars-forward " ") (eolp))
5047 ;; It is futile to split at the end of the prefix
5048 ;; since we would just insert the prefix again.
5049 (and after-prefix (<= (point) after-prefix))
5050 ;; Don't split right after a comment starter
5051 ;; since we would just make another comment starter.
5052 (and comment-start-skip
5053 (let ((limit (point)))
5055 (and (re-search-forward comment-start-skip
5057 (eq (point) limit))))))
5058 ;; No good place to break => stop trying.
5060 ;; Ok, we have a useful place to break the line. Do it.
5061 (let ((prev-column (current-column)))
5062 ;; If point is at the fill-point, do not `save-excursion'.
5063 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
5064 ;; point will end up before it rather than after it.
5066 (skip-chars-backward " \t")
5067 (= (point) fill-point))
5068 (default-indent-new-line t)
5070 (goto-char fill-point)
5071 (default-indent-new-line t)))
5072 ;; Now do justification, if required
5073 (if (not (eq justify 'left))
5076 (justify-current-line justify nil t)))
5077 ;; If making the new line didn't reduce the hpos of
5078 ;; the end of the line, then give up now;
5079 ;; trying again will not help.
5080 (if (>= (current-column) prev-column)
5081 (setq give-up t))))))
5082 ;; Justify last line.
5083 (justify-current-line justify t t)
5086 (defvar comment-line-break-function 'comment-indent-new-line
5087 "*Mode-specific function which line breaks and continues a comment.
5088 This function is called during auto-filling when a comment syntax
5090 The function should take a single optional argument, which is a flag
5091 indicating whether it should use soft newlines.")
5093 (defun default-indent-new-line (&optional soft)
5094 "Break line at point and indent.
5095 If a comment syntax is defined, call `comment-indent-new-line'.
5097 The inserted newline is marked hard if variable `use-hard-newlines' is true,
5098 unless optional argument SOFT is non-nil."
5101 (funcall comment-line-break-function soft)
5102 ;; Insert the newline before removing empty space so that markers
5103 ;; get preserved better.
5104 (if soft (insert-and-inherit ?\n) (newline 1))
5105 (save-excursion (forward-char -1) (delete-horizontal-space))
5106 (delete-horizontal-space)
5108 (if (and fill-prefix (not adaptive-fill-mode))
5109 ;; Blindly trust a non-adaptive fill-prefix.
5111 (indent-to-left-margin)
5112 (insert-before-markers-and-inherit fill-prefix))
5115 ;; If there's an adaptive prefix, use it unless we're inside
5116 ;; a comment and the prefix is not a comment starter.
5118 (indent-to-left-margin)
5119 (insert-and-inherit fill-prefix))
5120 ;; If we're not inside a comment, just try to indent.
5121 (t (indent-according-to-mode))))))
5123 (defvar normal-auto-fill-function 'do-auto-fill
5124 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
5125 Some major modes set this.")
5127 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
5128 ;; `functions' and `hooks' are usually unsafe to set, but setting
5129 ;; auto-fill-function to nil in a file-local setting is safe and
5130 ;; can be useful to prevent auto-filling.
5131 (put 'auto-fill-function 'safe-local-variable 'null)
5132 ;; FIXME: turn into a proper minor mode.
5133 ;; Add a global minor mode version of it.
5134 (defun auto-fill-mode (&optional arg)
5135 "Toggle Auto Fill mode.
5136 With ARG, turn Auto Fill mode on if and only if ARG is positive.
5137 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
5138 automatically breaks the line at a previous space.
5140 The value of `normal-auto-fill-function' specifies the function to use
5141 for `auto-fill-function' when turning Auto Fill mode on."
5143 (prog1 (setq auto-fill-function
5145 (not auto-fill-function)
5146 (> (prefix-numeric-value arg) 0))
5147 normal-auto-fill-function
5149 (force-mode-line-update)))
5151 ;; This holds a document string used to document auto-fill-mode.
5152 (defun auto-fill-function ()
5153 "Automatically break line at a previous space, in insertion of text."
5156 (defun turn-on-auto-fill ()
5157 "Unconditionally turn on Auto Fill mode."
5160 (defun turn-off-auto-fill ()
5161 "Unconditionally turn off Auto Fill mode."
5162 (auto-fill-mode -1))
5164 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
5166 (defun set-fill-column (arg)
5167 "Set `fill-column' to specified argument.
5168 Use \\[universal-argument] followed by a number to specify a column.
5169 Just \\[universal-argument] as argument means to use the current column."
5171 (list (or current-prefix-arg
5172 ;; We used to use current-column silently, but C-x f is too easily
5173 ;; typed as a typo for C-x C-f, so we turned it into an error and
5174 ;; now an interactive prompt.
5175 (read-number "Set fill-column to: " (current-column)))))
5177 (setq arg (current-column)))
5178 (if (not (integerp arg))
5179 ;; Disallow missing argument; it's probably a typo for C-x C-f.
5180 (error "set-fill-column requires an explicit argument")
5181 (message "Fill column set to %d (was %d)" arg fill-column)
5182 (setq fill-column arg)))
5184 (defun set-selective-display (arg)
5185 "Set `selective-display' to ARG; clear it if no arg.
5186 When the value of `selective-display' is a number > 0,
5187 lines whose indentation is >= that value are not displayed.
5188 The variable `selective-display' has a separate value for each buffer."
5190 (if (eq selective-display t)
5191 (error "selective-display already in use for marked lines"))
5194 (narrow-to-region (point-min) (point))
5195 (goto-char (window-start))
5196 (vertical-motion (window-height)))))
5197 (setq selective-display
5198 (and arg (prefix-numeric-value arg)))
5199 (recenter current-vpos))
5200 (set-window-start (selected-window) (window-start (selected-window)))
5201 (princ "selective-display set to " t)
5202 (prin1 selective-display t)
5205 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
5207 (defun toggle-truncate-lines (&optional arg)
5208 "Toggle whether to fold or truncate long lines for the current buffer.
5209 With prefix argument ARG, truncate long lines if ARG is positive,
5210 otherwise don't truncate them. Note that in side-by-side windows,
5211 this command has no effect if `truncate-partial-width-windows'
5214 (setq truncate-lines
5216 (not truncate-lines)
5217 (> (prefix-numeric-value arg) 0)))
5218 (force-mode-line-update)
5219 (unless truncate-lines
5220 (let ((buffer (current-buffer)))
5221 (walk-windows (lambda (window)
5222 (if (eq buffer (window-buffer window))
5223 (set-window-hscroll window 0)))
5225 (message "Truncate long lines %s"
5226 (if truncate-lines "enabled" "disabled")))
5228 (defun toggle-word-wrap (&optional arg)
5229 "Toggle whether to use word-wrapping for continuation lines.
5230 With prefix argument ARG, wrap continuation lines at word boundaries
5231 if ARG is positive, otherwise wrap them at the right screen edge.
5232 This command toggles the value of `word-wrap'. It has no effect
5233 if long lines are truncated."
5238 (> (prefix-numeric-value arg) 0)))
5239 (force-mode-line-update)
5240 (message "Word wrapping %s"
5241 (if word-wrap "enabled" "disabled")))
5243 (defvar overwrite-mode-textual (purecopy " Ovwrt")
5244 "The string displayed in the mode line when in overwrite mode.")
5245 (defvar overwrite-mode-binary (purecopy " Bin Ovwrt")
5246 "The string displayed in the mode line when in binary overwrite mode.")
5248 (defun overwrite-mode (arg)
5249 "Toggle overwrite mode.
5250 With prefix argument ARG, turn overwrite mode on if ARG is positive,
5251 otherwise turn it off. In overwrite mode, printing characters typed
5252 in replace existing text on a one-for-one basis, rather than pushing
5253 it to the right. At the end of a line, such characters extend the line.
5254 Before a tab, such characters insert until the tab is filled in.
5255 \\[quoted-insert] still inserts characters in overwrite mode; this
5256 is supposed to make it easier to insert characters when necessary."
5258 (setq overwrite-mode
5259 (if (if (null arg) (not overwrite-mode)
5260 (> (prefix-numeric-value arg) 0))
5261 'overwrite-mode-textual))
5262 (force-mode-line-update))
5264 (defun binary-overwrite-mode (arg)
5265 "Toggle binary overwrite mode.
5266 With prefix argument ARG, turn binary overwrite mode on if ARG is
5267 positive, otherwise turn it off. In binary overwrite mode, printing
5268 characters typed in replace existing text. Newlines are not treated
5269 specially, so typing at the end of a line joins the line to the next,
5270 with the typed character between them. Typing before a tab character
5271 simply replaces the tab with the character typed. \\[quoted-insert]
5272 replaces the text at the cursor, just as ordinary typing characters do.
5274 Note that binary overwrite mode is not its own minor mode; it is a
5275 specialization of overwrite mode, entered by setting the
5276 `overwrite-mode' variable to `overwrite-mode-binary'."
5278 (setq overwrite-mode
5280 (not (eq overwrite-mode 'overwrite-mode-binary))
5281 (> (prefix-numeric-value arg) 0))
5282 'overwrite-mode-binary))
5283 (force-mode-line-update))
5285 (define-minor-mode line-number-mode
5286 "Toggle Line Number mode.
5287 With ARG, turn Line Number mode on if ARG is positive, otherwise
5288 turn it off. When Line Number mode is enabled, the line number
5289 appears in the mode line.
5291 Line numbers do not appear for very large buffers and buffers
5292 with very long lines; see variables `line-number-display-limit'
5293 and `line-number-display-limit-width'."
5294 :init-value t :global t :group 'mode-line)
5296 (define-minor-mode column-number-mode
5297 "Toggle Column Number mode.
5298 With ARG, turn Column Number mode on if ARG is positive,
5299 otherwise turn it off. When Column Number mode is enabled, the
5300 column number appears in the mode line."
5301 :global t :group 'mode-line)
5303 (define-minor-mode size-indication-mode
5304 "Toggle Size Indication mode.
5305 With ARG, turn Size Indication mode on if ARG is positive,
5306 otherwise turn it off. When Size Indication mode is enabled, the
5307 size of the accessible part of the buffer appears in the mode line."
5308 :global t :group 'mode-line)
5310 (defgroup paren-blinking nil
5311 "Blinking matching of parens and expressions."
5312 :prefix "blink-matching-"
5313 :group 'paren-matching)
5315 (defcustom blink-matching-paren t
5316 "Non-nil means show matching open-paren when close-paren is inserted."
5318 :group 'paren-blinking)
5320 (defcustom blink-matching-paren-on-screen t
5321 "Non-nil means show matching open-paren when it is on screen.
5322 If nil, don't show it (but the open-paren can still be shown
5323 when it is off screen).
5325 This variable has no effect if `blink-matching-paren' is nil.
5326 \(In that case, the open-paren is never shown.)
5327 It is also ignored if `show-paren-mode' is enabled."
5329 :group 'paren-blinking)
5331 (defcustom blink-matching-paren-distance (* 100 1024)
5332 "If non-nil, maximum distance to search backwards for matching open-paren.
5333 If nil, search stops at the beginning of the accessible portion of the buffer."
5334 :version "23.2" ; 25->100k
5335 :type '(choice (const nil) integer)
5336 :group 'paren-blinking)
5338 (defcustom blink-matching-delay 1
5339 "Time in seconds to delay after showing a matching paren."
5341 :group 'paren-blinking)
5343 (defcustom blink-matching-paren-dont-ignore-comments nil
5344 "If nil, `blink-matching-paren' ignores comments.
5345 More precisely, when looking for the matching parenthesis,
5346 it skips the contents of comments that end before point."
5348 :group 'paren-blinking)
5350 (defun blink-matching-open ()
5351 "Move cursor momentarily to the beginning of the sexp before point."
5353 (when (and (> (point) (point-min))
5354 blink-matching-paren
5355 ;; Verify an even number of quoting characters precede the close.
5356 (= 1 (logand 1 (- (point)
5359 (skip-syntax-backward "/\\")
5361 (let* ((oldpos (point))
5362 (message-log-max nil) ; Don't log messages about paren matching.
5363 (atdollar (eq (syntax-class (syntax-after (1- oldpos))) 8))
5368 (if blink-matching-paren-distance
5370 (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
5371 (- (point) blink-matching-paren-distance))
5373 (let ((parse-sexp-ignore-comments
5374 (and parse-sexp-ignore-comments
5375 (not blink-matching-paren-dont-ignore-comments))))
5377 (scan-sexps oldpos -1)
5383 (eq (syntax-class (syntax-after blinkpos)) 8)))
5384 (let ((syntax (syntax-after blinkpos)))
5386 (eq (syntax-class syntax) 4)
5390 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00871.html
5391 ((not (or (and isdollar blinkpos)
5392 (and atdollar (not blinkpos)) ; see below
5393 (eq matching-paren (char-before oldpos))
5394 ;; The cdr might hold a new paren-class info rather than
5395 ;; a matching-char info, in which case the two CDRs
5397 (eq matching-paren (cdr (syntax-after (1- oldpos))))))
5399 (minibuffer-message " [Mismatched parentheses]")
5400 (message "Mismatched parentheses")))
5402 (or blink-matching-paren-distance
5403 ;; Don't complain when `$' with no blinkpos, because it
5404 ;; could just be the first one typed in the buffer.
5407 (minibuffer-message " [Unmatched parenthesis]")
5408 (message "Unmatched parenthesis"))))
5409 ((pos-visible-in-window-p blinkpos)
5410 ;; Matching open within window, temporarily move to blinkpos but only
5411 ;; if `blink-matching-paren-on-screen' is non-nil.
5412 (and blink-matching-paren-on-screen
5413 (not show-paren-mode)
5415 (goto-char blinkpos)
5416 (sit-for blink-matching-delay))))
5419 (goto-char blinkpos)
5420 (let ((open-paren-line-string
5421 ;; Show what precedes the open in its line, if anything.
5423 ((save-excursion (skip-chars-backward " \t") (not (bolp)))
5424 (buffer-substring (line-beginning-position)
5426 ;; Show what follows the open in its line, if anything.
5429 (skip-chars-forward " \t")
5431 (buffer-substring blinkpos
5432 (line-end-position)))
5433 ;; Otherwise show the previous nonblank line,
5435 ((save-excursion (skip-chars-backward "\n \t") (not (bobp)))
5437 (buffer-substring (progn
5438 (skip-chars-backward "\n \t")
5439 (line-beginning-position))
5440 (progn (end-of-line)
5441 (skip-chars-backward " \t")
5443 ;; Replace the newline and other whitespace with `...'.
5445 (buffer-substring blinkpos (1+ blinkpos))))
5446 ;; There is nothing to show except the char itself.
5447 (t (buffer-substring blinkpos (1+ blinkpos))))))
5448 (message "Matches %s"
5449 (substring-no-properties open-paren-line-string)))))))))
5451 (setq blink-paren-function 'blink-matching-open)
5453 ;; This executes C-g typed while Emacs is waiting for a command.
5454 ;; Quitting out of a program does not go through here;
5455 ;; that happens in the QUIT macro at the C code level.
5456 (defun keyboard-quit ()
5457 "Signal a `quit' condition.
5458 During execution of Lisp code, this character causes a quit directly.
5459 At top-level, as an editor command, this simply beeps."
5462 (if (fboundp 'kmacro-keyboard-quit)
5463 (kmacro-keyboard-quit))
5464 (setq defining-kbd-macro nil)
5467 (defvar buffer-quit-function nil
5468 "Function to call to \"quit\" the current buffer, or nil if none.
5469 \\[keyboard-escape-quit] calls this function when its more local actions
5470 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
5472 (defun keyboard-escape-quit ()
5473 "Exit the current \"mode\" (in a generalized sense of the word).
5474 This command can exit an interactive command such as `query-replace',
5475 can clear out a prefix argument or a region,
5476 can get out of the minibuffer or other recursive edit,
5477 cancel the use of the current buffer (for special-purpose buffers),
5478 or go back to just one window (by deleting all but the selected window)."
5480 (cond ((eq last-command 'mode-exited) nil)
5481 ((> (minibuffer-depth) 0)
5482 (abort-recursive-edit))
5487 ((> (recursion-depth) 0)
5488 (exit-recursive-edit))
5489 (buffer-quit-function
5490 (funcall buffer-quit-function))
5491 ((not (one-window-p t))
5492 (delete-other-windows))
5493 ((string-match "^ \\*" (buffer-name (current-buffer)))
5496 (defun play-sound-file (file &optional volume device)
5497 "Play sound stored in FILE.
5498 VOLUME and DEVICE correspond to the keywords of the sound
5499 specification for `play-sound'."
5500 (interactive "fPlay sound file: ")
5501 (let ((sound (list :file file)))
5503 (plist-put sound :volume volume))
5505 (plist-put sound :device device))
5507 (play-sound sound)))
5510 (defcustom read-mail-command 'rmail
5511 "Your preference for a mail reading package.
5512 This is used by some keybindings which support reading mail.
5513 See also `mail-user-agent' concerning sending mail."
5514 :type '(radio (function-item :tag "Rmail" :format "%t\n" rmail)
5515 (function-item :tag "Gnus" :format "%t\n" gnus)
5516 (function-item :tag "Emacs interface to MH"
5517 :format "%t\n" mh-rmail)
5518 (function :tag "Other"))
5522 (defcustom mail-user-agent 'message-user-agent
5523 "Your preference for a mail composition package.
5524 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
5525 outgoing email message. This variable lets you specify which
5526 mail-sending package you prefer.
5528 Valid values include:
5530 `message-user-agent' -- use the Message package.
5531 See Info node `(message)'.
5532 `sendmail-user-agent' -- use the Mail package.
5533 See Info node `(emacs)Sending Mail'.
5534 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
5535 See Info node `(mh-e)'.
5536 `gnus-user-agent' -- like `message-user-agent', but with Gnus
5537 paraphernalia, particularly the Gcc: header for
5540 Additional valid symbols may be available; check with the author of
5541 your package for details. The function should return non-nil if it
5544 See also `read-mail-command' concerning reading mail."
5545 :type '(radio (function-item :tag "Message package"
5548 (function-item :tag "Mail package"
5550 sendmail-user-agent)
5551 (function-item :tag "Emacs interface to MH"
5554 (function-item :tag "Message with full Gnus features"
5557 (function :tag "Other"))
5558 :version "23.2" ; sendmail->message
5561 (defcustom compose-mail-user-agent-warnings t
5562 "If non-nil, `compose-mail' warns about changes in `mail-user-agent'.
5563 If the value of `mail-user-agent' is the default, and the user
5564 appears to have customizations applying to the old default,
5565 `compose-mail' issues a warning."
5570 (define-mail-user-agent 'sendmail-user-agent
5571 'sendmail-user-agent-compose
5572 'mail-send-and-exit)
5574 (defun rfc822-goto-eoh ()
5575 ;; Go to header delimiter line in a mail message, following RFC822 rules
5576 (goto-char (point-min))
5577 (when (re-search-forward
5578 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
5579 (goto-char (match-beginning 0))))
5581 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
5582 switch-function yank-action
5585 (let ((special-display-buffer-names nil)
5586 (special-display-regexps nil)
5587 (same-window-buffer-names nil)
5588 (same-window-regexps nil))
5589 (funcall switch-function "*mail*")))
5590 (let ((cc (cdr (assoc-string "cc" other-headers t)))
5591 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
5592 (body (cdr (assoc-string "body" other-headers t))))
5593 (or (mail continue to subject in-reply-to cc yank-action send-actions)
5595 (error "Message aborted"))
5598 (while other-headers
5599 (unless (member-ignore-case (car (car other-headers))
5600 '("in-reply-to" "cc" "body"))
5601 (insert (car (car other-headers)) ": "
5602 (cdr (car other-headers))
5603 (if use-hard-newlines hard-newline "\n")))
5604 (setq other-headers (cdr other-headers)))
5610 (defun compose-mail (&optional to subject other-headers continue
5611 switch-function yank-action send-actions)
5612 "Start composing a mail message to send.
5613 This uses the user's chosen mail composition package
5614 as selected with the variable `mail-user-agent'.
5615 The optional arguments TO and SUBJECT specify recipients
5616 and the initial Subject field, respectively.
5618 OTHER-HEADERS is an alist specifying additional
5619 header fields. Elements look like (HEADER . VALUE) where both
5620 HEADER and VALUE are strings.
5622 CONTINUE, if non-nil, says to continue editing a message already
5623 being composed. Interactively, CONTINUE is the prefix argument.
5625 SWITCH-FUNCTION, if non-nil, is a function to use to
5626 switch to and display the buffer used for mail composition.
5628 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
5629 to insert the raw text of the message being replied to.
5630 It has the form (FUNCTION . ARGS). The user agent will apply
5631 FUNCTION to ARGS, to insert the raw text of the original message.
5632 \(The user agent will also run `mail-citation-hook', *after* the
5633 original text has been inserted in this way.)
5635 SEND-ACTIONS is a list of actions to call when the message is sent.
5636 Each action has the form (FUNCTION . ARGS)."
5638 (list nil nil nil current-prefix-arg))
5640 ;; In Emacs 23.2, the default value of `mail-user-agent' changed
5641 ;; from sendmail-user-agent to message-user-agent. Some users may
5642 ;; encounter incompatibilities. This hack tries to detect problems
5643 ;; and warn about them.
5644 (and compose-mail-user-agent-warnings
5645 (eq mail-user-agent 'message-user-agent)
5647 (dolist (var '(mail-mode-hook mail-send-hook mail-setup-hook
5648 mail-yank-hooks mail-archive-file-name
5649 mail-default-reply-to mail-mailing-lists
5653 (push var warn-vars)))
5655 (display-warning 'mail
5657 The default mail mode is now Message mode.
5658 You have the following Mail mode variable%s customized:
5659 \n %s\n\nTo use Mail mode, set `mail-user-agent' to sendmail-user-agent.
5660 To disable this warning, set `compose-mail-check-user-agent' to nil."
5661 (if (> (length warn-vars) 1) "s" "")
5662 (mapconcat 'symbol-name
5665 (let ((function (get mail-user-agent 'composefunc)))
5666 (funcall function to subject other-headers continue
5667 switch-function yank-action send-actions)))
5669 (defun compose-mail-other-window (&optional to subject other-headers continue
5670 yank-action send-actions)
5671 "Like \\[compose-mail], but edit the outgoing message in another window."
5673 (list nil nil nil current-prefix-arg))
5674 (compose-mail to subject other-headers continue
5675 'switch-to-buffer-other-window yank-action send-actions))
5678 (defun compose-mail-other-frame (&optional to subject other-headers continue
5679 yank-action send-actions)
5680 "Like \\[compose-mail], but edit the outgoing message in another frame."
5682 (list nil nil nil current-prefix-arg))
5683 (compose-mail to subject other-headers continue
5684 'switch-to-buffer-other-frame yank-action send-actions))
5686 (defvar set-variable-value-history nil
5687 "History of values entered with `set-variable'.
5689 Maximum length of the history list is determined by the value
5690 of `history-length', which see.")
5692 (defun set-variable (variable value &optional make-local)
5693 "Set VARIABLE to VALUE. VALUE is a Lisp object.
5694 VARIABLE should be a user option variable name, a Lisp variable
5695 meant to be customized by users. You should enter VALUE in Lisp syntax,
5696 so if you want VALUE to be a string, you must surround it with doublequotes.
5697 VALUE is used literally, not evaluated.
5699 If VARIABLE has a `variable-interactive' property, that is used as if
5700 it were the arg to `interactive' (which see) to interactively read VALUE.
5702 If VARIABLE has been defined with `defcustom', then the type information
5703 in the definition is used to check that VALUE is valid.
5705 With a prefix argument, set VARIABLE to VALUE buffer-locally."
5707 (let* ((default-var (variable-at-point))
5708 (var (if (user-variable-p default-var)
5709 (read-variable (format "Set variable (default %s): " default-var)
5711 (read-variable "Set variable: ")))
5712 (minibuffer-help-form '(describe-variable var))
5713 (prop (get var 'variable-interactive))
5714 (obsolete (car (get var 'byte-obsolete-variable)))
5715 (prompt (format "Set %s %s to value: " var
5716 (cond ((local-variable-p var)
5718 ((or current-prefix-arg
5719 (local-variable-if-set-p var))
5724 (message (concat "`%S' is obsolete; "
5725 (if (symbolp obsolete) "use `%S' instead" "%s"))
5729 ;; Use VAR's `variable-interactive' property
5730 ;; as an interactive spec for prompting.
5731 (call-interactively `(lambda (arg)
5735 (read-string prompt nil
5736 'set-variable-value-history
5737 (format "%S" (symbol-value var))))))))
5738 (list var val current-prefix-arg)))
5740 (and (custom-variable-p variable)
5741 (not (get variable 'custom-type))
5742 (custom-load-symbol variable))
5743 (let ((type (get variable 'custom-type)))
5745 ;; Match with custom type.
5747 (setq type (widget-convert type))
5748 (unless (widget-apply type :match value)
5749 (error "Value `%S' does not match type %S of %S"
5750 value (car type) variable))))
5753 (make-local-variable variable))
5755 (set variable value)
5757 ;; Force a thorough redisplay for the case that the variable
5758 ;; has an effect on the display, like `tab-width' has.
5759 (force-mode-line-update))
5761 ;; Define the major mode for lists of completions.
5763 (defvar completion-list-mode-map
5764 (let ((map (make-sparse-keymap)))
5765 (define-key map [mouse-2] 'mouse-choose-completion)
5766 (define-key map [follow-link] 'mouse-face)
5767 (define-key map [down-mouse-2] nil)
5768 (define-key map "\C-m" 'choose-completion)
5769 (define-key map "\e\e\e" 'delete-completion-window)
5770 (define-key map [left] 'previous-completion)
5771 (define-key map [right] 'next-completion)
5772 (define-key map "q" 'quit-window)
5774 "Local map for completion list buffers.")
5776 ;; Completion mode is suitable only for specially formatted data.
5777 (put 'completion-list-mode 'mode-class 'special)
5779 (defvar completion-reference-buffer nil
5780 "Record the buffer that was current when the completion list was requested.
5781 This is a local variable in the completion list buffer.
5782 Initial value is nil to avoid some compiler warnings.")
5784 (defvar completion-no-auto-exit nil
5785 "Non-nil means `choose-completion-string' should never exit the minibuffer.
5786 This also applies to other functions such as `choose-completion'.")
5788 (defvar completion-base-position nil
5789 "Position of the base of the text corresponding to the shown completions.
5790 This variable is used in the *Completions* buffers.
5791 Its value is a list of the form (START END) where START is the place
5792 where the completion should be inserted and END (if non-nil) is the end
5793 of the text to replace. If END is nil, point is used instead.")
5795 (defvar completion-base-size nil
5796 "Number of chars before point not involved in completion.
5797 This is a local variable in the completion list buffer.
5798 It refers to the chars in the minibuffer if completing in the
5799 minibuffer, or in `completion-reference-buffer' otherwise.
5800 Only characters in the field at point are included.
5802 If nil, Emacs determines which part of the tail end of the
5803 buffer's text is involved in completion by comparing the text
5805 (make-obsolete-variable 'completion-base-size 'completion-base-position "23.2")
5807 (defun delete-completion-window ()
5808 "Delete the completion list window.
5809 Go to the window from which completion was requested."
5811 (let ((buf completion-reference-buffer))
5812 (if (one-window-p t)
5813 (if (window-dedicated-p (selected-window))
5814 (delete-frame (selected-frame)))
5815 (delete-window (selected-window))
5816 (if (get-buffer-window buf)
5817 (select-window (get-buffer-window buf))))))
5819 (defun previous-completion (n)
5820 "Move to the previous item in the completion list."
5822 (next-completion (- n)))
5824 (defun next-completion (n)
5825 "Move to the next item in the completion list.
5826 With prefix argument N, move N items (negative N means move backward)."
5828 (let ((beg (point-min)) (end (point-max)))
5829 (while (and (> n 0) (not (eobp)))
5830 ;; If in a completion, move to the end of it.
5831 (when (get-text-property (point) 'mouse-face)
5832 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
5833 ;; Move to start of next one.
5834 (unless (get-text-property (point) 'mouse-face)
5835 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
5837 (while (and (< n 0) (not (bobp)))
5838 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
5839 ;; If in a completion, move to the start of it.
5840 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
5841 (goto-char (previous-single-property-change
5842 (point) 'mouse-face nil beg)))
5843 ;; Move to end of the previous completion.
5844 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
5845 (goto-char (previous-single-property-change
5846 (point) 'mouse-face nil beg)))
5847 ;; Move to the start of that one.
5848 (goto-char (previous-single-property-change
5849 (point) 'mouse-face nil beg))
5852 (defun choose-completion (&optional event)
5853 "Choose the completion at point."
5854 (interactive (list last-nonmenu-event))
5855 ;; In case this is run via the mouse, give temporary modes such as
5856 ;; isearch a chance to turn off.
5857 (run-hooks 'mouse-leave-buffer-hook)
5858 (let (buffer base-size base-position choice)
5859 (with-current-buffer (window-buffer (posn-window (event-start event)))
5860 (setq buffer completion-reference-buffer)
5861 (setq base-size completion-base-size)
5862 (setq base-position completion-base-position)
5864 (goto-char (posn-point (event-start event)))
5866 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
5867 (setq end (point) beg (1+ (point))))
5868 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
5869 (setq end (1- (point)) beg (point)))
5871 (error "No completion here"))
5872 (setq beg (previous-single-property-change beg 'mouse-face))
5873 (setq end (or (next-single-property-change end 'mouse-face)
5875 (setq choice (buffer-substring-no-properties beg end)))))
5877 (let ((owindow (selected-window)))
5878 (select-window (posn-window (event-start event)))
5879 (if (and (one-window-p t 'selected-frame)
5880 (window-dedicated-p (selected-window)))
5881 ;; This is a special buffer's frame
5882 (iconify-frame (selected-frame))
5883 (or (window-dedicated-p (selected-window))
5886 (or (and (buffer-live-p buffer)
5887 (get-buffer-window buffer 0))
5890 (choose-completion-string
5894 ;; Someone's using old completion code that doesn't know
5895 ;; about base-position yet.
5896 (list (+ base-size (with-current-buffer buffer (field-beginning)))))
5897 ;; If all else fails, just guess.
5898 (with-current-buffer buffer
5899 (list (choose-completion-guess-base-position choice)))))))
5901 ;; Delete the longest partial match for STRING
5902 ;; that can be found before POINT.
5903 (defun choose-completion-guess-base-position (string)
5905 (let ((opoint (point))
5907 ;; Try moving back by the length of the string.
5908 (goto-char (max (- (point) (length string))
5909 (minibuffer-prompt-end)))
5910 ;; See how far back we were actually able to move. That is the
5911 ;; upper bound on how much we can match and delete.
5912 (setq len (- opoint (point)))
5913 (if completion-ignore-case
5914 (setq string (downcase string)))
5915 (while (and (> len 0)
5916 (let ((tail (buffer-substring (point) opoint)))
5917 (if completion-ignore-case
5918 (setq tail (downcase tail)))
5919 (not (string= tail (substring string 0 len)))))
5924 (defun choose-completion-delete-max-match (string)
5925 (delete-region (choose-completion-guess-base-position string) (point)))
5926 (make-obsolete 'choose-completion-delete-max-match
5927 'choose-completion-guess-base-position "23.2")
5929 (defvar choose-completion-string-functions nil
5930 "Functions that may override the normal insertion of a completion choice.
5931 These functions are called in order with four arguments:
5932 CHOICE - the string to insert in the buffer,
5933 BUFFER - the buffer in which the choice should be inserted,
5934 MINI-P - non-nil if BUFFER is a minibuffer, and
5935 BASE-SIZE - the number of characters in BUFFER before
5936 the string being completed.
5938 If a function in the list returns non-nil, that function is supposed
5939 to have inserted the CHOICE in the BUFFER, and possibly exited
5940 the minibuffer; no further functions will be called.
5942 If all functions in the list return nil, that means to use
5943 the default method of inserting the completion in BUFFER.")
5945 (defun choose-completion-string (choice &optional buffer base-position)
5946 "Switch to BUFFER and insert the completion choice CHOICE.
5947 BASE-POSITION, says where to insert the completion."
5949 ;; If BUFFER is the minibuffer, exit the minibuffer
5950 ;; unless it is reading a file name and CHOICE is a directory,
5951 ;; or completion-no-auto-exit is non-nil.
5953 ;; Some older code may call us passing `base-size' instead of
5954 ;; `base-position'. It's difficult to make any use of `base-size',
5955 ;; so we just ignore it.
5956 (unless (consp base-position)
5957 (message "Obsolete `base-size' passed to choose-completion-string")
5958 (setq base-position nil))
5960 (let* ((buffer (or buffer completion-reference-buffer))
5961 (mini-p (minibufferp buffer)))
5962 ;; If BUFFER is a minibuffer, barf unless it's the currently
5963 ;; active minibuffer.
5965 (or (not (active-minibuffer-window))
5967 (window-buffer (active-minibuffer-window))))))
5968 (error "Minibuffer is not active for completion")
5969 ;; Set buffer so buffer-local choose-completion-string-functions works.
5971 (unless (run-hook-with-args-until-success
5972 'choose-completion-string-functions
5973 ;; The fourth arg used to be `mini-p' but was useless
5974 ;; (since minibufferp can be used on the `buffer' arg)
5975 ;; and indeed unused. The last used to be `base-size', so we
5976 ;; keep it to try and avoid breaking old code.
5977 choice buffer base-position nil)
5978 ;; Insert the completion into the buffer where it was requested.
5979 (delete-region (or (car base-position) (point))
5980 (or (cadr base-position) (point)))
5982 (remove-text-properties (- (point) (length choice)) (point)
5984 ;; Update point in the window that BUFFER is showing in.
5985 (let ((window (get-buffer-window buffer t)))
5986 (set-window-point window (point)))
5987 ;; If completing for the minibuffer, exit it with this choice.
5988 (and (not completion-no-auto-exit)
5989 (minibufferp buffer)
5990 minibuffer-completion-table
5991 ;; If this is reading a file name, and the file name chosen
5992 ;; is a directory, don't exit the minibuffer.
5993 (let* ((result (buffer-substring (field-beginning) (point)))
5995 (completion-boundaries result minibuffer-completion-table
5996 minibuffer-completion-predicate
5998 (if (eq (car bounds) (length result))
5999 ;; The completion chosen leads to a new set of completions
6000 ;; (e.g. it's a directory): don't exit the minibuffer yet.
6001 (let ((mini (active-minibuffer-window)))
6002 (select-window mini)
6003 (when minibuffer-auto-raise
6004 (raise-frame (window-frame mini))))
6005 (exit-minibuffer))))))))
6007 (define-derived-mode completion-list-mode nil "Completion List"
6008 "Major mode for buffers showing lists of possible completions.
6009 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
6010 to select the completion near point.
6011 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
6014 \\{completion-list-mode-map}"
6015 (set (make-local-variable 'completion-base-size) nil))
6017 (defun completion-list-mode-finish ()
6018 "Finish setup of the completions buffer.
6019 Called from `temp-buffer-show-hook'."
6020 (when (eq major-mode 'completion-list-mode)
6021 (toggle-read-only 1)))
6023 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
6026 ;; Variables and faces used in `completion-setup-function'.
6028 (defcustom completion-show-help t
6029 "Non-nil means show help message in *Completions* buffer."
6034 ;; This function goes in completion-setup-hook, so that it is called
6035 ;; after the text of the completion list buffer is written.
6036 (defun completion-setup-function ()
6037 (let* ((mainbuf (current-buffer))
6039 ;; When reading a file name in the minibuffer,
6040 ;; try and find the right default-directory to set in the
6041 ;; completion list buffer.
6042 ;; FIXME: Why do we do that, actually? --Stef
6043 (if minibuffer-completing-file-name
6044 (file-name-as-directory
6046 (substring (minibuffer-completion-contents)
6047 0 (or completion-base-size 0)))))))
6048 (with-current-buffer standard-output
6049 (let ((base-size completion-base-size) ;Read before killing localvars.
6050 (base-position completion-base-position))
6051 (completion-list-mode)
6052 (set (make-local-variable 'completion-base-size) base-size)
6053 (set (make-local-variable 'completion-base-position) base-position))
6054 (set (make-local-variable 'completion-reference-buffer) mainbuf)
6055 (if base-dir (setq default-directory base-dir))
6056 ;; Maybe insert help string.
6057 (when completion-show-help
6058 (goto-char (point-min))
6059 (if (display-mouse-p)
6060 (insert (substitute-command-keys
6061 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
6062 (insert (substitute-command-keys
6063 "In this buffer, type \\[choose-completion] to \
6064 select the completion near point.\n\n"))))))
6066 (add-hook 'completion-setup-hook 'completion-setup-function)
6068 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
6069 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
6071 (defun switch-to-completions ()
6072 "Select the completion list window."
6074 (let ((window (or (get-buffer-window "*Completions*" 0)
6075 ;; Make sure we have a completions window.
6076 (progn (minibuffer-completion-help)
6077 (get-buffer-window "*Completions*" 0)))))
6079 (select-window window)
6080 ;; In the new buffer, go to the first completion.
6081 ;; FIXME: Perhaps this should be done in `minibuffer-completion-help'.
6083 (next-completion 1)))))
6085 ;;; Support keyboard commands to turn on various modifiers.
6087 ;; These functions -- which are not commands -- each add one modifier
6088 ;; to the following event.
6090 (defun event-apply-alt-modifier (ignore-prompt)
6091 "\\<function-key-map>Add the Alt modifier to the following event.
6092 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
6093 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
6094 (defun event-apply-super-modifier (ignore-prompt)
6095 "\\<function-key-map>Add the Super modifier to the following event.
6096 For example, type \\[event-apply-super-modifier] & to enter Super-&."
6097 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
6098 (defun event-apply-hyper-modifier (ignore-prompt)
6099 "\\<function-key-map>Add the Hyper modifier to the following event.
6100 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
6101 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
6102 (defun event-apply-shift-modifier (ignore-prompt)
6103 "\\<function-key-map>Add the Shift modifier to the following event.
6104 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
6105 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
6106 (defun event-apply-control-modifier (ignore-prompt)
6107 "\\<function-key-map>Add the Ctrl modifier to the following event.
6108 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
6109 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
6110 (defun event-apply-meta-modifier (ignore-prompt)
6111 "\\<function-key-map>Add the Meta modifier to the following event.
6112 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
6113 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
6115 (defun event-apply-modifier (event symbol lshiftby prefix)
6116 "Apply a modifier flag to event EVENT.
6117 SYMBOL is the name of this modifier, as a symbol.
6118 LSHIFTBY is the numeric value of this modifier, in keyboard events.
6119 PREFIX is the string that represents this modifier in an event type symbol."
6121 (cond ((eq symbol 'control)
6122 (if (and (<= (downcase event) ?z)
6123 (>= (downcase event) ?a))
6124 (- (downcase event) ?a -1)
6125 (if (and (<= (downcase event) ?Z)
6126 (>= (downcase event) ?A))
6127 (- (downcase event) ?A -1)
6128 (logior (lsh 1 lshiftby) event))))
6130 (if (and (<= (downcase event) ?z)
6131 (>= (downcase event) ?a))
6133 (logior (lsh 1 lshiftby) event)))
6135 (logior (lsh 1 lshiftby) event)))
6136 (if (memq symbol (event-modifiers event))
6138 (let ((event-type (if (symbolp event) event (car event))))
6139 (setq event-type (intern (concat prefix (symbol-name event-type))))
6142 (cons event-type (cdr event)))))))
6144 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
6145 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
6146 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
6147 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
6148 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
6149 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
6151 ;;;; Keypad support.
6153 ;; Make the keypad keys act like ordinary typing keys. If people add
6154 ;; bindings for the function key symbols, then those bindings will
6155 ;; override these, so this shouldn't interfere with any existing
6158 ;; Also tell read-char how to handle these keys.
6160 (lambda (keypad-normal)
6161 (let ((keypad (nth 0 keypad-normal))
6162 (normal (nth 1 keypad-normal)))
6163 (put keypad 'ascii-character normal)
6164 (define-key function-key-map (vector keypad) (vector normal))))
6165 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
6166 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
6177 ;; Do the same for various keys that are represented as symbols under
6178 ;; GUIs but naturally correspond to characters.
6189 ;;;; forking a twin copy of a buffer.
6192 (defvar clone-buffer-hook nil
6193 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
6195 (defvar clone-indirect-buffer-hook nil
6196 "Normal hook to run in the new buffer at the end of `clone-indirect-buffer'.")
6198 (defun clone-process (process &optional newname)
6199 "Create a twin copy of PROCESS.
6200 If NEWNAME is nil, it defaults to PROCESS' name;
6201 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
6202 If PROCESS is associated with a buffer, the new process will be associated
6203 with the current buffer instead.
6204 Returns nil if PROCESS has already terminated."
6205 (setq newname (or newname (process-name process)))
6206 (if (string-match "<[0-9]+>\\'" newname)
6207 (setq newname (substring newname 0 (match-beginning 0))))
6208 (when (memq (process-status process) '(run stop open))
6209 (let* ((process-connection-type (process-tty-name process))
6211 (if (memq (process-status process) '(open))
6212 (let ((args (process-contact process t)))
6213 (setq args (plist-put args :name newname))
6214 (setq args (plist-put args :buffer
6215 (if (process-buffer process)
6217 (apply 'make-network-process args))
6218 (apply 'start-process newname
6219 (if (process-buffer process) (current-buffer))
6220 (process-command process)))))
6221 (set-process-query-on-exit-flag
6222 new-process (process-query-on-exit-flag process))
6223 (set-process-inherit-coding-system-flag
6224 new-process (process-inherit-coding-system-flag process))
6225 (set-process-filter new-process (process-filter process))
6226 (set-process-sentinel new-process (process-sentinel process))
6227 (set-process-plist new-process (copy-sequence (process-plist process)))
6230 ;; things to maybe add (currently partly covered by `funcall mode'):
6233 (defun clone-buffer (&optional newname display-flag)
6234 "Create and return a twin copy of the current buffer.
6235 Unlike an indirect buffer, the new buffer can be edited
6236 independently of the old one (if it is not read-only).
6237 NEWNAME is the name of the new buffer. It may be modified by
6238 adding or incrementing <N> at the end as necessary to create a
6239 unique buffer name. If nil, it defaults to the name of the
6240 current buffer, with the proper suffix. If DISPLAY-FLAG is
6241 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
6242 clone a file-visiting buffer, or a buffer whose major mode symbol
6243 has a non-nil `no-clone' property, results in an error.
6245 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
6246 current buffer with appropriate suffix. However, if a prefix
6247 argument is given, then the command prompts for NEWNAME in the
6250 This runs the normal hook `clone-buffer-hook' in the new buffer
6251 after it has been set up properly in other respects."
6254 (if buffer-file-name
6255 (error "Cannot clone a file-visiting buffer"))
6256 (if (get major-mode 'no-clone)
6257 (error "Cannot clone a buffer in %s mode" mode-name))
6258 (list (if current-prefix-arg
6259 (read-buffer "Name of new cloned buffer: " (current-buffer)))
6261 (if buffer-file-name
6262 (error "Cannot clone a file-visiting buffer"))
6263 (if (get major-mode 'no-clone)
6264 (error "Cannot clone a buffer in %s mode" mode-name))
6265 (setq newname (or newname (buffer-name)))
6266 (if (string-match "<[0-9]+>\\'" newname)
6267 (setq newname (substring newname 0 (match-beginning 0))))
6268 (let ((buf (current-buffer))
6272 (mk (if mark-active (mark t)))
6273 (modified (buffer-modified-p))
6275 (lvars (buffer-local-variables))
6276 (process (get-buffer-process (current-buffer)))
6277 (new (generate-new-buffer (or newname (buffer-name)))))
6280 (with-current-buffer new
6281 (insert-buffer-substring buf)))
6282 (with-current-buffer new
6283 (narrow-to-region ptmin ptmax)
6285 (if mk (set-mark mk))
6286 (set-buffer-modified-p modified)
6288 ;; Clone the old buffer's process, if any.
6289 (when process (clone-process process))
6291 ;; Now set up the major mode.
6294 ;; Set up other local variables.
6296 (condition-case () ;in case var is read-only
6299 (set (make-local-variable (car v)) (cdr v)))
6303 ;; Run any hooks (typically set up by the major mode
6304 ;; for cloning to work properly).
6305 (run-hooks 'clone-buffer-hook))
6307 ;; Presumably the current buffer is shown in the selected frame, so
6308 ;; we want to display the clone elsewhere.
6309 (let ((same-window-regexps nil)
6310 (same-window-buffer-names))
6311 (pop-to-buffer new)))
6315 (defun clone-indirect-buffer (newname display-flag &optional norecord)
6316 "Create an indirect buffer that is a twin copy of the current buffer.
6318 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
6319 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
6320 or if not called with a prefix arg, NEWNAME defaults to the current
6321 buffer's name. The name is modified by adding a `<N>' suffix to it
6322 or by incrementing the N in an existing suffix. Trying to clone a
6323 buffer whose major mode symbol has a non-nil `no-clone-indirect'
6324 property results in an error.
6326 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
6327 This is always done when called interactively.
6329 Optional third arg NORECORD non-nil means do not put this buffer at the
6330 front of the list of recently selected ones."
6333 (if (get major-mode 'no-clone-indirect)
6334 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
6335 (list (if current-prefix-arg
6336 (read-buffer "Name of indirect buffer: " (current-buffer)))
6338 (if (get major-mode 'no-clone-indirect)
6339 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
6340 (setq newname (or newname (buffer-name)))
6341 (if (string-match "<[0-9]+>\\'" newname)
6342 (setq newname (substring newname 0 (match-beginning 0))))
6343 (let* ((name (generate-new-buffer-name newname))
6344 (buffer (make-indirect-buffer (current-buffer) name t)))
6345 (with-current-buffer buffer
6346 (run-hooks 'clone-indirect-buffer-hook))
6348 (pop-to-buffer buffer norecord))
6352 (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
6353 "Like `clone-indirect-buffer' but display in another window."
6356 (if (get major-mode 'no-clone-indirect)
6357 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
6358 (list (if current-prefix-arg
6359 (read-buffer "Name of indirect buffer: " (current-buffer)))
6361 (let ((pop-up-windows t))
6362 (clone-indirect-buffer newname display-flag norecord)))
6365 ;;; Handling of Backspace and Delete keys.
6367 (defcustom normal-erase-is-backspace 'maybe
6368 "Set the default behavior of the Delete and Backspace keys.
6370 If set to t, Delete key deletes forward and Backspace key deletes
6373 If set to nil, both Delete and Backspace keys delete backward.
6375 If set to 'maybe (which is the default), Emacs automatically
6376 selects a behavior. On window systems, the behavior depends on
6377 the keyboard used. If the keyboard has both a Backspace key and
6378 a Delete key, and both are mapped to their usual meanings, the
6379 option's default value is set to t, so that Backspace can be used
6380 to delete backward, and Delete can be used to delete forward.
6382 If not running under a window system, customizing this option
6383 accomplishes a similar effect by mapping C-h, which is usually
6384 generated by the Backspace key, to DEL, and by mapping DEL to C-d
6385 via `keyboard-translate'. The former functionality of C-h is
6386 available on the F1 key. You should probably not use this
6387 setting if you don't have both Backspace, Delete and F1 keys.
6389 Setting this variable with setq doesn't take effect. Programmatically,
6390 call `normal-erase-is-backspace-mode' (which see) instead."
6391 :type '(choice (const :tag "Off" nil)
6392 (const :tag "Maybe" maybe)
6393 (other :tag "On" t))
6394 :group 'editing-basics
6396 :set (lambda (symbol value)
6397 ;; The fboundp is because of a problem with :set when
6398 ;; dumping Emacs. It doesn't really matter.
6399 (if (fboundp 'normal-erase-is-backspace-mode)
6400 (normal-erase-is-backspace-mode (or value 0))
6401 (set-default symbol value))))
6403 (defun normal-erase-is-backspace-setup-frame (&optional frame)
6404 "Set up `normal-erase-is-backspace-mode' on FRAME, if necessary."
6405 (unless frame (setq frame (selected-frame)))
6406 (with-selected-frame frame
6407 (unless (terminal-parameter nil 'normal-erase-is-backspace)
6408 (normal-erase-is-backspace-mode
6409 (if (if (eq normal-erase-is-backspace 'maybe)
6410 (and (not noninteractive)
6411 (or (memq system-type '(ms-dos windows-nt))
6412 (and (memq window-system '(x))
6413 (fboundp 'x-backspace-delete-keys-p)
6414 (x-backspace-delete-keys-p))
6415 ;; If the terminal Emacs is running on has erase char
6416 ;; set to ^H, use the Backspace key for deleting
6417 ;; backward, and the Delete key for deleting forward.
6418 (and (null window-system)
6419 (eq tty-erase-char ?\^H))))
6420 normal-erase-is-backspace)
6423 (defun normal-erase-is-backspace-mode (&optional arg)
6424 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
6426 With numeric ARG, turn the mode on if and only if ARG is positive.
6428 On window systems, when this mode is on, Delete is mapped to C-d
6429 and Backspace is mapped to DEL; when this mode is off, both
6430 Delete and Backspace are mapped to DEL. (The remapping goes via
6431 `local-function-key-map', so binding Delete or Backspace in the
6432 global or local keymap will override that.)
6434 In addition, on window systems, the bindings of C-Delete, M-Delete,
6435 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
6436 the global keymap in accordance with the functionality of Delete and
6437 Backspace. For example, if Delete is remapped to C-d, which deletes
6438 forward, C-Delete is bound to `kill-word', but if Delete is remapped
6439 to DEL, which deletes backward, C-Delete is bound to
6440 `backward-kill-word'.
6442 If not running on a window system, a similar effect is accomplished by
6443 remapping C-h (normally produced by the Backspace key) and DEL via
6444 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
6445 to C-d; if it's off, the keys are not remapped.
6447 When not running on a window system, and this mode is turned on, the
6448 former functionality of C-h is available on the F1 key. You should
6449 probably not turn on this mode on a text-only terminal if you don't
6450 have both Backspace, Delete and F1 keys.
6452 See also `normal-erase-is-backspace'."
6454 (let ((enabled (or (and arg (> (prefix-numeric-value arg) 0))
6456 (eq 1 (terminal-parameter
6457 nil 'normal-erase-is-backspace)))))))
6458 (set-terminal-parameter nil 'normal-erase-is-backspace
6461 (cond ((or (memq window-system '(x w32 ns pc))
6462 (memq system-type '(ms-dos windows-nt)))
6464 `(([M-delete] [M-backspace])
6465 ([C-M-delete] [C-M-backspace])
6466 ([?\e C-delete] [?\e C-backspace])))
6467 (old-state (lookup-key local-function-key-map [delete])))
6471 (define-key local-function-key-map [delete] [?\C-d])
6472 (define-key local-function-key-map [kp-delete] [?\C-d])
6473 (define-key local-function-key-map [backspace] [?\C-?])
6474 (dolist (b bindings)
6475 ;; Not sure if input-decode-map is really right, but
6476 ;; keyboard-translate-table (used below) only works
6477 ;; for integer events, and key-translation-table is
6478 ;; global (like the global-map, used earlier).
6479 (define-key input-decode-map (car b) nil)
6480 (define-key input-decode-map (cadr b) nil)))
6481 (define-key local-function-key-map [delete] [?\C-?])
6482 (define-key local-function-key-map [kp-delete] [?\C-?])
6483 (define-key local-function-key-map [backspace] [?\C-?])
6484 (dolist (b bindings)
6485 (define-key input-decode-map (car b) (cadr b))
6486 (define-key input-decode-map (cadr b) (car b))))))
6490 (keyboard-translate ?\C-h ?\C-?)
6491 (keyboard-translate ?\C-? ?\C-d))
6492 (keyboard-translate ?\C-h ?\C-h)
6493 (keyboard-translate ?\C-? ?\C-?))))
6495 (run-hooks 'normal-erase-is-backspace-hook)
6496 (if (called-interactively-p 'interactive)
6497 (message "Delete key deletes %s"
6498 (if (eq 1 (terminal-parameter nil 'normal-erase-is-backspace))
6499 "forward" "backward")))))
6501 (defvar vis-mode-saved-buffer-invisibility-spec nil
6502 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
6504 (define-minor-mode visible-mode
6505 "Toggle Visible mode.
6506 With argument ARG turn Visible mode on if ARG is positive, otherwise
6509 Enabling Visible mode makes all invisible text temporarily visible.
6510 Disabling Visible mode turns off that effect. Visible mode works by
6511 saving the value of `buffer-invisibility-spec' and setting it to nil."
6513 :group 'editing-basics
6514 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
6515 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
6516 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
6518 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
6519 buffer-invisibility-spec)
6520 (setq buffer-invisibility-spec nil)))
6522 ;; Partial application of functions (similar to "currying").
6523 ;; This function is here rather than in subr.el because it uses CL.
6524 (defun apply-partially (fun &rest args)
6525 "Return a function that is a partial application of FUN to ARGS.
6526 ARGS is a list of the first N arguments to pass to FUN.
6527 The result is a new function which does the same as FUN, except that
6528 the first N arguments are fixed at the values with which this function
6530 (lexical-let ((fun fun) (args1 args))
6531 (lambda (&rest args2) (apply fun (append args1 args2)))))
6533 ;; This function is here rather than in subr.el because it uses CL.
6534 (defmacro with-wrapper-hook (var args &rest body)
6535 "Run BODY wrapped with the VAR hook.
6536 VAR is a special hook: its functions are called with a first argument
6537 which is the \"original\" code (the BODY), so the hook function can wrap
6538 the original function, or call it any number of times (including not calling
6539 it at all). This is similar to an `around' advice.
6540 VAR is normally a symbol (a variable) in which case it is treated like
6541 a hook, with a buffer-local and a global part. But it can also be an
6542 arbitrary expression.
6543 ARGS is a list of variables which will be passed as additional arguments
6544 to each function, after the inital argument, and which the first argument
6545 expects to receive when called."
6546 (declare (indent 2) (debug t))
6547 ;; We need those two gensyms because CL's lexical scoping is not available
6548 ;; for function arguments :-(
6549 (let ((funs (make-symbol "funs"))
6550 (global (make-symbol "global"))
6551 (argssym (make-symbol "args")))
6552 ;; Since the hook is a wrapper, the loop has to be done via
6553 ;; recursion: a given hook function will call its parameter in order to
6554 ;; continue looping.
6555 `(labels ((runrestofhook (,funs ,global ,argssym)
6556 ;; `funs' holds the functions left on the hook and `global'
6557 ;; holds the functions left on the global part of the hook
6558 ;; (in case the hook is local).
6559 (lexical-let ((funs ,funs)
6562 (if (eq t (car funs))
6564 (append global (cdr funs)) nil ,argssym)
6566 (lambda (&rest ,argssym)
6567 (runrestofhook (cdr funs) global ,argssym))
6569 ;; Once there are no more functions on the hook, run
6570 ;; the original body.
6571 (apply (lambda ,args ,@body) ,argssym)))))
6573 ;; The global part of the hook, if any.
6575 `(if (local-variable-p ',var)
6576 (default-value ',var)))
6579 ;; Minibuffer prompt stuff.
6581 ;(defun minibuffer-prompt-modification (start end)
6582 ; (error "You cannot modify the prompt"))
6585 ;(defun minibuffer-prompt-insertion (start end)
6586 ; (let ((inhibit-modification-hooks t))
6587 ; (delete-region start end)
6588 ; ;; Discard undo information for the text insertion itself
6589 ; ;; and for the text deletion.above.
6590 ; (when (consp buffer-undo-list)
6591 ; (setq buffer-undo-list (cddr buffer-undo-list)))
6592 ; (message "You cannot modify the prompt")))
6595 ;(setq minibuffer-prompt-properties
6596 ; (list 'modification-hooks '(minibuffer-prompt-modification)
6597 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
6601 ;;;; Problematic external packages.
6603 ;; rms says this should be done by specifying symbols that define
6604 ;; versions together with bad values. This is therefore not as
6605 ;; flexible as it could be. See the thread:
6606 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00300.html
6607 (defconst bad-packages-alist
6608 ;; Not sure exactly which semantic versions have problems.
6609 ;; Definitely 2.0pre3, probably all 2.0pre's before this.
6610 '((semantic semantic-version "\\`2\\.0pre[1-3]\\'"
6611 "The version of `semantic' loaded does not work in Emacs 22.
6612 It can cause constant high CPU load.
6613 Upgrade to at least Semantic 2.0pre4 (distributed with CEDET 1.0pre4).")
6614 ;; CUA-mode does not work with GNU Emacs version 22.1 and newer.
6615 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
6616 ;; provided the `CUA-mode' feature. Since this is no longer true,
6617 ;; we can warn the user if the `CUA-mode' feature is ever provided.
6619 "CUA-mode is now part of the standard GNU Emacs distribution,
6620 so you can now enable CUA via the Options menu or by customizing `cua-mode'.
6622 You have loaded an older version of CUA-mode which does not work
6623 correctly with this version of Emacs. You should remove the old
6624 version and use the one distributed with Emacs."))
6625 "Alist of packages known to cause problems in this version of Emacs.
6626 Each element has the form (PACKAGE SYMBOL REGEXP STRING).
6627 PACKAGE is either a regular expression to match file names, or a
6628 symbol (a feature name); see the documentation of
6629 `after-load-alist', to which this variable adds functions.
6630 SYMBOL is either the name of a string variable, or `t'. Upon
6631 loading PACKAGE, if SYMBOL is t or matches REGEXP, display a
6632 warning using STRING as the message.")
6634 (defun bad-package-check (package)
6635 "Run a check using the element from `bad-packages-alist' matching PACKAGE."
6637 (let* ((list (assoc package bad-packages-alist))
6638 (symbol (nth 1 list)))
6642 (and (stringp (setq symbol (eval symbol)))
6643 (string-match-p (nth 2 list) symbol)))
6644 (display-warning package (nth 3 list) :warning)))
6647 (mapc (lambda (elem)
6648 (eval-after-load (car elem) `(bad-package-check ',(car elem))))
6654 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
6655 ;;; simple.el ends here