(vc-annotate-background): Fix custom type.
[emacs.git] / lisp / simple.el
blobd7cea7d3194f6328863d08caa2beb19a39ea5828
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
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; A grab-bag of basic Emacs commands not specifically related to some
30 ;; major mode or to file-handling.
32 ;;; Code:
34 (declare-function widget-convert "wid-edit" (type &rest args))
35 (declare-function shell-mode "shell" ())
37 (defvar compilation-current-error)
39 (defcustom idle-update-delay 0.5
40 "*Idle time delay before updating various things on the screen.
41 Various Emacs features that update auxiliary information when point moves
42 wait this many seconds after Emacs becomes idle before doing an update."
43 :type 'number
44 :group 'display
45 :version "22.1")
47 (defgroup killing nil
48 "Killing and yanking commands."
49 :group 'editing)
51 (defgroup paren-matching nil
52 "Highlight (un)matching of parens and expressions."
53 :group 'matching)
55 (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
56 "Search LIST for a valid buffer to display in FRAME.
57 Return nil when all buffers in LIST are undesirable for display,
58 otherwise return the first suitable buffer in LIST.
60 Buffers not visible in windows are preferred to visible buffers,
61 unless VISIBLE-OK is non-nil.
62 If the optional argument FRAME is nil, it defaults to the selected frame.
63 If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
64 ;; This logic is more or less copied from other-buffer.
65 (setq frame (or frame (selected-frame)))
66 (let ((pred (frame-parameter frame 'buffer-predicate))
67 found buf)
68 (while (and (not found) list)
69 (setq buf (car list))
70 (if (and (not (eq buffer buf))
71 (buffer-live-p buf)
72 (or (null pred) (funcall pred buf))
73 (not (eq (aref (buffer-name buf) 0) ?\s))
74 (or visible-ok (null (get-buffer-window buf 'visible))))
75 (setq found buf)
76 (setq list (cdr list))))
77 (car list)))
79 (defun last-buffer (&optional buffer visible-ok frame)
80 "Return the last non-hidden displayable buffer in the buffer list.
81 If BUFFER is non-nil, last-buffer will ignore that buffer.
82 Buffers not visible in windows are preferred to visible buffers,
83 unless optional argument VISIBLE-OK is non-nil.
84 If the optional third argument FRAME is non-nil, use that frame's
85 buffer list instead of the selected frame's buffer list.
86 If no other buffer exists, the buffer `*scratch*' is returned."
87 (setq frame (or frame (selected-frame)))
88 (or (get-next-valid-buffer (nreverse (buffer-list frame))
89 buffer visible-ok frame)
90 (progn
91 (set-buffer-major-mode (get-buffer-create "*scratch*"))
92 (get-buffer "*scratch*"))))
93 (defun next-buffer ()
94 "Switch to the next buffer in cyclic order."
95 (interactive)
96 (let ((buffer (current-buffer)))
97 (switch-to-buffer (other-buffer buffer t))
98 (bury-buffer buffer)))
100 (defun previous-buffer ()
101 "Switch to the previous buffer in cyclic order."
102 (interactive)
103 (switch-to-buffer (last-buffer (current-buffer) t)))
106 ;;; next-error support framework
108 (defgroup next-error nil
109 "`next-error' support framework."
110 :group 'compilation
111 :version "22.1")
113 (defface next-error
114 '((t (:inherit region)))
115 "Face used to highlight next error locus."
116 :group 'next-error
117 :version "22.1")
119 (defcustom next-error-highlight 0.5
120 "*Highlighting of locations in selected source buffers.
121 If a number, highlight the locus in `next-error' face for the given time
122 in seconds, or until the next command is executed.
123 If t, highlight the locus until the next command is executed, or until
124 some other locus replaces it.
125 If nil, don't highlight the locus in the source buffer.
126 If `fringe-arrow', indicate the locus by the fringe arrow."
127 :type '(choice (number :tag "Highlight for specified time")
128 (const :tag "Semipermanent highlighting" t)
129 (const :tag "No highlighting" nil)
130 (const :tag "Fringe arrow" fringe-arrow))
131 :group 'next-error
132 :version "22.1")
134 (defcustom next-error-highlight-no-select 0.5
135 "*Highlighting of locations in `next-error-no-select'.
136 If number, highlight the locus in `next-error' face for given time in seconds.
137 If t, highlight the locus indefinitely until some other locus replaces it.
138 If nil, don't highlight the locus in the source buffer.
139 If `fringe-arrow', indicate the locus by the fringe arrow."
140 :type '(choice (number :tag "Highlight for specified time")
141 (const :tag "Semipermanent highlighting" t)
142 (const :tag "No highlighting" nil)
143 (const :tag "Fringe arrow" fringe-arrow))
144 :group 'next-error
145 :version "22.1")
147 (defcustom next-error-recenter nil
148 "*Display the line in the visited source file recentered as specified.
149 If non-nil, the value is passed directly to `recenter'."
150 :type '(choice (integer :tag "Line to recenter to")
151 (const :tag "Center of window" (4))
152 (const :tag "No recentering" nil))
153 :group 'next-error
154 :version "23.1")
156 (defcustom next-error-hook nil
157 "*List of hook functions run by `next-error' after visiting source file."
158 :type 'hook
159 :group 'next-error)
161 (defvar next-error-highlight-timer nil)
163 (defvar next-error-overlay-arrow-position nil)
164 (put 'next-error-overlay-arrow-position 'overlay-arrow-string "=>")
165 (add-to-list 'overlay-arrow-variable-list 'next-error-overlay-arrow-position)
167 (defvar next-error-last-buffer nil
168 "The most recent `next-error' buffer.
169 A buffer becomes most recent when its compilation, grep, or
170 similar mode is started, or when it is used with \\[next-error]
171 or \\[compile-goto-error].")
173 (defvar next-error-function nil
174 "Function to use to find the next error in the current buffer.
175 The function is called with 2 parameters:
176 ARG is an integer specifying by how many errors to move.
177 RESET is a boolean which, if non-nil, says to go back to the beginning
178 of the errors before moving.
179 Major modes providing compile-like functionality should set this variable
180 to indicate to `next-error' that this is a candidate buffer and how
181 to navigate in it.")
183 (make-variable-buffer-local 'next-error-function)
185 (defsubst next-error-buffer-p (buffer
186 &optional avoid-current
187 extra-test-inclusive
188 extra-test-exclusive)
189 "Test if BUFFER is a `next-error' capable buffer.
191 If AVOID-CURRENT is non-nil, treat the current buffer
192 as an absolute last resort only.
194 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
195 that normally would not qualify. If it returns t, the buffer
196 in question is treated as usable.
198 The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
199 that would normally be considered usable. If it returns nil,
200 that buffer is rejected."
201 (and (buffer-name buffer) ;First make sure it's live.
202 (not (and avoid-current (eq buffer (current-buffer))))
203 (with-current-buffer buffer
204 (if next-error-function ; This is the normal test.
205 ;; Optionally reject some buffers.
206 (if extra-test-exclusive
207 (funcall extra-test-exclusive)
209 ;; Optionally accept some other buffers.
210 (and extra-test-inclusive
211 (funcall extra-test-inclusive))))))
213 (defun next-error-find-buffer (&optional avoid-current
214 extra-test-inclusive
215 extra-test-exclusive)
216 "Return a `next-error' capable buffer.
218 If AVOID-CURRENT is non-nil, treat the current buffer
219 as an absolute last resort only.
221 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
222 that normally would not qualify. If it returns t, the buffer
223 in question is treated as usable.
225 The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
226 that would normally be considered usable. If it returns nil,
227 that buffer is rejected."
229 ;; 1. If one window on the selected frame displays such buffer, return it.
230 (let ((window-buffers
231 (delete-dups
232 (delq nil (mapcar (lambda (w)
233 (if (next-error-buffer-p
234 (window-buffer w)
235 avoid-current
236 extra-test-inclusive extra-test-exclusive)
237 (window-buffer w)))
238 (window-list))))))
239 (if (eq (length window-buffers) 1)
240 (car window-buffers)))
241 ;; 2. If next-error-last-buffer is an acceptable buffer, use that.
242 (if (and next-error-last-buffer
243 (next-error-buffer-p next-error-last-buffer avoid-current
244 extra-test-inclusive extra-test-exclusive))
245 next-error-last-buffer)
246 ;; 3. If the current buffer is acceptable, choose it.
247 (if (next-error-buffer-p (current-buffer) avoid-current
248 extra-test-inclusive extra-test-exclusive)
249 (current-buffer))
250 ;; 4. Look for any acceptable buffer.
251 (let ((buffers (buffer-list)))
252 (while (and buffers
253 (not (next-error-buffer-p
254 (car buffers) avoid-current
255 extra-test-inclusive extra-test-exclusive)))
256 (setq buffers (cdr buffers)))
257 (car buffers))
258 ;; 5. Use the current buffer as a last resort if it qualifies,
259 ;; even despite AVOID-CURRENT.
260 (and avoid-current
261 (next-error-buffer-p (current-buffer) nil
262 extra-test-inclusive extra-test-exclusive)
263 (progn
264 (message "This is the only buffer with error message locations")
265 (current-buffer)))
266 ;; 6. Give up.
267 (error "No buffers contain error message locations")))
269 (defun next-error (&optional arg reset)
270 "Visit next `next-error' message and corresponding source code.
272 If all the error messages parsed so far have been processed already,
273 the message buffer is checked for new ones.
275 A prefix ARG specifies how many error messages to move;
276 negative means move back to previous error messages.
277 Just \\[universal-argument] as a prefix means reparse the error message buffer
278 and start at the first error.
280 The RESET argument specifies that we should restart from the beginning.
282 \\[next-error] normally uses the most recently started
283 compilation, grep, or occur buffer. It can also operate on any
284 buffer with output from the \\[compile], \\[grep] commands, or,
285 more generally, on any buffer in Compilation mode or with
286 Compilation Minor mode enabled, or any buffer in which
287 `next-error-function' is bound to an appropriate function.
288 To specify use of a particular buffer for error messages, type
289 \\[next-error] in that buffer when it is the only one displayed
290 in the current frame.
292 Once \\[next-error] has chosen the buffer for error messages, it
293 runs `next-error-hook' with `run-hooks', and stays with that buffer
294 until you use it in some other buffer which uses Compilation mode
295 or Compilation Minor mode.
297 See variables `compilation-parse-errors-function' and
298 \`compilation-error-regexp-alist' for customization ideas."
299 (interactive "P")
300 (if (consp arg) (setq reset t arg nil))
301 (when (setq next-error-last-buffer (next-error-find-buffer))
302 ;; we know here that next-error-function is a valid symbol we can funcall
303 (with-current-buffer next-error-last-buffer
304 (funcall next-error-function (prefix-numeric-value arg) reset)
305 (when next-error-recenter
306 (recenter next-error-recenter))
307 (run-hooks 'next-error-hook))))
309 (defun next-error-internal ()
310 "Visit the source code corresponding to the `next-error' message at point."
311 (setq next-error-last-buffer (current-buffer))
312 ;; we know here that next-error-function is a valid symbol we can funcall
313 (with-current-buffer next-error-last-buffer
314 (funcall next-error-function 0 nil)
315 (when next-error-recenter
316 (recenter next-error-recenter))
317 (run-hooks 'next-error-hook)))
319 (defalias 'goto-next-locus 'next-error)
320 (defalias 'next-match 'next-error)
322 (defun previous-error (&optional n)
323 "Visit previous `next-error' message and corresponding source code.
325 Prefix arg N says how many error messages to move backwards (or
326 forwards, if negative).
328 This operates on the output from the \\[compile] and \\[grep] commands."
329 (interactive "p")
330 (next-error (- (or n 1))))
332 (defun first-error (&optional n)
333 "Restart at the first error.
334 Visit corresponding source code.
335 With prefix arg N, visit the source code of the Nth error.
336 This operates on the output from the \\[compile] command, for instance."
337 (interactive "p")
338 (next-error n t))
340 (defun next-error-no-select (&optional n)
341 "Move point to the next error in the `next-error' buffer and highlight match.
342 Prefix arg N says how many error messages to move forwards (or
343 backwards, if negative).
344 Finds and highlights the source line like \\[next-error], but does not
345 select the source buffer."
346 (interactive "p")
347 (let ((next-error-highlight next-error-highlight-no-select))
348 (next-error n))
349 (pop-to-buffer next-error-last-buffer))
351 (defun previous-error-no-select (&optional n)
352 "Move point to the previous error in the `next-error' buffer and highlight match.
353 Prefix arg N says how many error messages to move backwards (or
354 forwards, if negative).
355 Finds and highlights the source line like \\[previous-error], but does not
356 select the source buffer."
357 (interactive "p")
358 (next-error-no-select (- (or n 1))))
360 ;;; Internal variable for `next-error-follow-mode-post-command-hook'.
361 (defvar next-error-follow-last-line nil)
363 (define-minor-mode next-error-follow-minor-mode
364 "Minor mode for compilation, occur and diff modes.
365 When turned on, cursor motion in the compilation, grep, occur or diff
366 buffer causes automatic display of the corresponding source code
367 location."
368 :group 'next-error :init-value nil :lighter " Fol"
369 (if (not next-error-follow-minor-mode)
370 (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t)
371 (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t)
372 (make-local-variable 'next-error-follow-last-line)))
374 ;;; Used as a `post-command-hook' by `next-error-follow-mode'
375 ;;; for the *Compilation* *grep* and *Occur* buffers.
376 (defun next-error-follow-mode-post-command-hook ()
377 (unless (equal next-error-follow-last-line (line-number-at-pos))
378 (setq next-error-follow-last-line (line-number-at-pos))
379 (condition-case nil
380 (let ((compilation-context-lines nil))
381 (setq compilation-current-error (point))
382 (next-error-no-select 0))
383 (error t))))
388 (defun fundamental-mode ()
389 "Major mode not specialized for anything in particular.
390 Other major modes are defined by comparison with this one."
391 (interactive)
392 (kill-all-local-variables)
393 (unless delay-mode-hooks
394 (run-hooks 'after-change-major-mode-hook)))
396 ;; Making and deleting lines.
398 (defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard)))
400 (defun newline (&optional arg)
401 "Insert a newline, and move to left margin of the new line if it's blank.
402 If `use-hard-newlines' is non-nil, the newline is marked with the
403 text-property `hard'.
404 With ARG, insert that many newlines.
405 Call `auto-fill-function' if the current column number is greater
406 than the value of `fill-column' and ARG is nil."
407 (interactive "*P")
408 (barf-if-buffer-read-only)
409 ;; Inserting a newline at the end of a line produces better redisplay in
410 ;; try_window_id than inserting at the beginning of a line, and the textual
411 ;; result is the same. So, if we're at beginning of line, pretend to be at
412 ;; the end of the previous line.
413 (let ((flag (and (not (bobp))
414 (bolp)
415 ;; Make sure no functions want to be told about
416 ;; the range of the changes.
417 (not after-change-functions)
418 (not before-change-functions)
419 ;; Make sure there are no markers here.
420 (not (buffer-has-markers-at (1- (point))))
421 (not (buffer-has-markers-at (point)))
422 ;; Make sure no text properties want to know
423 ;; where the change was.
424 (not (get-char-property (1- (point)) 'modification-hooks))
425 (not (get-char-property (1- (point)) 'insert-behind-hooks))
426 (or (eobp)
427 (not (get-char-property (point) 'insert-in-front-hooks)))
428 ;; Make sure the newline before point isn't intangible.
429 (not (get-char-property (1- (point)) 'intangible))
430 ;; Make sure the newline before point isn't read-only.
431 (not (get-char-property (1- (point)) 'read-only))
432 ;; Make sure the newline before point isn't invisible.
433 (not (get-char-property (1- (point)) 'invisible))
434 ;; Make sure the newline before point has the same
435 ;; properties as the char before it (if any).
436 (< (or (previous-property-change (point)) -2)
437 (- (point) 2))))
438 (was-page-start (and (bolp)
439 (looking-at page-delimiter)))
440 (beforepos (point)))
441 (if flag (backward-char 1))
442 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
443 ;; Set last-command-char to tell self-insert what to insert.
444 (let ((last-command-char ?\n)
445 ;; Don't auto-fill if we have a numeric argument.
446 ;; Also not if flag is true (it would fill wrong line);
447 ;; there is no need to since we're at BOL.
448 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
449 (unwind-protect
450 (self-insert-command (prefix-numeric-value arg))
451 ;; If we get an error in self-insert-command, put point at right place.
452 (if flag (forward-char 1))))
453 ;; Even if we did *not* get an error, keep that forward-char;
454 ;; all further processing should apply to the newline that the user
455 ;; thinks he inserted.
457 ;; Mark the newline(s) `hard'.
458 (if use-hard-newlines
459 (set-hard-newline-properties
460 (- (point) (prefix-numeric-value arg)) (point)))
461 ;; If the newline leaves the previous line blank,
462 ;; and we have a left margin, delete that from the blank line.
463 (or flag
464 (save-excursion
465 (goto-char beforepos)
466 (beginning-of-line)
467 (and (looking-at "[ \t]$")
468 (> (current-left-margin) 0)
469 (delete-region (point) (progn (end-of-line) (point))))))
470 ;; Indent the line after the newline, except in one case:
471 ;; when we added the newline at the beginning of a line
472 ;; which starts a page.
473 (or was-page-start
474 (move-to-left-margin nil t)))
475 nil)
477 (defun set-hard-newline-properties (from to)
478 (let ((sticky (get-text-property from 'rear-nonsticky)))
479 (put-text-property from to 'hard 't)
480 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
481 (if (and (listp sticky) (not (memq 'hard sticky)))
482 (put-text-property from (point) 'rear-nonsticky
483 (cons 'hard sticky)))))
485 (defun open-line (n)
486 "Insert a newline and leave point before it.
487 If there is a fill prefix and/or a `left-margin', insert them
488 on the new line if the line would have been blank.
489 With arg N, insert N newlines."
490 (interactive "*p")
491 (let* ((do-fill-prefix (and fill-prefix (bolp)))
492 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
493 (loc (point))
494 ;; Don't expand an abbrev before point.
495 (abbrev-mode nil))
496 (newline n)
497 (goto-char loc)
498 (while (> n 0)
499 (cond ((bolp)
500 (if do-left-margin (indent-to (current-left-margin)))
501 (if do-fill-prefix (insert-and-inherit fill-prefix))))
502 (forward-line 1)
503 (setq n (1- n)))
504 (goto-char loc)
505 (end-of-line)))
507 (defun split-line (&optional arg)
508 "Split current line, moving portion beyond point vertically down.
509 If the current line starts with `fill-prefix', insert it on the new
510 line as well. With prefix ARG, don't insert `fill-prefix' on new line.
512 When called from Lisp code, ARG may be a prefix string to copy."
513 (interactive "*P")
514 (skip-chars-forward " \t")
515 (let* ((col (current-column))
516 (pos (point))
517 ;; What prefix should we check for (nil means don't).
518 (prefix (cond ((stringp arg) arg)
519 (arg nil)
520 (t fill-prefix)))
521 ;; Does this line start with it?
522 (have-prfx (and prefix
523 (save-excursion
524 (beginning-of-line)
525 (looking-at (regexp-quote prefix))))))
526 (newline 1)
527 (if have-prfx (insert-and-inherit prefix))
528 (indent-to col 0)
529 (goto-char pos)))
531 (defun delete-indentation (&optional arg)
532 "Join this line to previous and fix up whitespace at join.
533 If there is a fill prefix, delete it from the beginning of this line.
534 With argument, join this line to following line."
535 (interactive "*P")
536 (beginning-of-line)
537 (if arg (forward-line 1))
538 (if (eq (preceding-char) ?\n)
539 (progn
540 (delete-region (point) (1- (point)))
541 ;; If the second line started with the fill prefix,
542 ;; delete the prefix.
543 (if (and fill-prefix
544 (<= (+ (point) (length fill-prefix)) (point-max))
545 (string= fill-prefix
546 (buffer-substring (point)
547 (+ (point) (length fill-prefix)))))
548 (delete-region (point) (+ (point) (length fill-prefix))))
549 (fixup-whitespace))))
551 (defalias 'join-line #'delete-indentation) ; easier to find
553 (defun delete-blank-lines ()
554 "On blank line, delete all surrounding blank lines, leaving just one.
555 On isolated blank line, delete that one.
556 On nonblank line, delete any immediately following blank lines."
557 (interactive "*")
558 (let (thisblank singleblank)
559 (save-excursion
560 (beginning-of-line)
561 (setq thisblank (looking-at "[ \t]*$"))
562 ;; Set singleblank if there is just one blank line here.
563 (setq singleblank
564 (and thisblank
565 (not (looking-at "[ \t]*\n[ \t]*$"))
566 (or (bobp)
567 (progn (forward-line -1)
568 (not (looking-at "[ \t]*$")))))))
569 ;; Delete preceding blank lines, and this one too if it's the only one.
570 (if thisblank
571 (progn
572 (beginning-of-line)
573 (if singleblank (forward-line 1))
574 (delete-region (point)
575 (if (re-search-backward "[^ \t\n]" nil t)
576 (progn (forward-line 1) (point))
577 (point-min)))))
578 ;; Delete following blank lines, unless the current line is blank
579 ;; and there are no following blank lines.
580 (if (not (and thisblank singleblank))
581 (save-excursion
582 (end-of-line)
583 (forward-line 1)
584 (delete-region (point)
585 (if (re-search-forward "[^ \t\n]" nil t)
586 (progn (beginning-of-line) (point))
587 (point-max)))))
588 ;; Handle the special case where point is followed by newline and eob.
589 ;; Delete the line, leaving point at eob.
590 (if (looking-at "^[ \t]*\n\\'")
591 (delete-region (point) (point-max)))))
593 (defun delete-trailing-whitespace ()
594 "Delete all the trailing whitespace across the current buffer.
595 All whitespace after the last non-whitespace character in a line is deleted.
596 This respects narrowing, created by \\[narrow-to-region] and friends.
597 A formfeed is not considered whitespace by this function."
598 (interactive "*")
599 (save-match-data
600 (save-excursion
601 (goto-char (point-min))
602 (while (re-search-forward "\\s-$" nil t)
603 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
604 ;; Don't delete formfeeds, even if they are considered whitespace.
605 (save-match-data
606 (if (looking-at ".*\f")
607 (goto-char (match-end 0))))
608 (delete-region (point) (match-end 0))))))
610 (defun newline-and-indent ()
611 "Insert a newline, then indent according to major mode.
612 Indentation is done using the value of `indent-line-function'.
613 In programming language modes, this is the same as TAB.
614 In some text modes, where TAB inserts a tab, this command indents to the
615 column specified by the function `current-left-margin'."
616 (interactive "*")
617 (delete-horizontal-space t)
618 (newline)
619 (indent-according-to-mode))
621 (defun reindent-then-newline-and-indent ()
622 "Reindent current line, insert newline, then indent the new line.
623 Indentation of both lines is done according to the current major mode,
624 which means calling the current value of `indent-line-function'.
625 In programming language modes, this is the same as TAB.
626 In some text modes, where TAB inserts a tab, this indents to the
627 column specified by the function `current-left-margin'."
628 (interactive "*")
629 (let ((pos (point)))
630 ;; Be careful to insert the newline before indenting the line.
631 ;; Otherwise, the indentation might be wrong.
632 (newline)
633 (save-excursion
634 (goto-char pos)
635 ;; We are at EOL before the call to indent-according-to-mode, and
636 ;; after it we usually are as well, but not always. We tried to
637 ;; address it with `save-excursion' but that uses a normal marker
638 ;; whereas we need `move after insertion', so we do the save/restore
639 ;; by hand.
640 (setq pos (copy-marker pos t))
641 (indent-according-to-mode)
642 (goto-char pos)
643 ;; Remove the trailing white-space after indentation because
644 ;; indentation may introduce the whitespace.
645 (delete-horizontal-space t))
646 (indent-according-to-mode)))
648 (defun quoted-insert (arg)
649 "Read next input character and insert it.
650 This is useful for inserting control characters.
652 If the first character you type after this command is an octal digit,
653 you should type a sequence of octal digits which specify a character code.
654 Any nondigit terminates the sequence. If the terminator is a RET,
655 it is discarded; any other terminator is used itself as input.
656 The variable `read-quoted-char-radix' specifies the radix for this feature;
657 set it to 10 or 16 to use decimal or hex instead of octal.
659 In overwrite mode, this function inserts the character anyway, and
660 does not handle octal digits specially. This means that if you use
661 overwrite as your normal editing mode, you can use this function to
662 insert characters when necessary.
664 In binary overwrite mode, this function does overwrite, and octal
665 digits are interpreted as a character code. This is intended to be
666 useful for editing binary files."
667 (interactive "*p")
668 (let* ((char (let (translation-table-for-input input-method-function)
669 (if (or (not overwrite-mode)
670 (eq overwrite-mode 'overwrite-mode-binary))
671 (read-quoted-char)
672 (read-char)))))
673 ;; Assume character codes 0240 - 0377 stand for characters in some
674 ;; single-byte character set, and convert them to Emacs
675 ;; characters.
676 (if (and enable-multibyte-characters
677 (>= char ?\240)
678 (<= char ?\377))
679 (setq char (unibyte-char-to-multibyte char)))
680 (if (> arg 0)
681 (if (eq overwrite-mode 'overwrite-mode-binary)
682 (delete-char arg)))
683 (while (> arg 0)
684 (insert-and-inherit char)
685 (setq arg (1- arg)))))
687 (defun forward-to-indentation (&optional arg)
688 "Move forward ARG lines and position at first nonblank character."
689 (interactive "p")
690 (forward-line (or arg 1))
691 (skip-chars-forward " \t"))
693 (defun backward-to-indentation (&optional arg)
694 "Move backward ARG lines and position at first nonblank character."
695 (interactive "p")
696 (forward-line (- (or arg 1)))
697 (skip-chars-forward " \t"))
699 (defun back-to-indentation ()
700 "Move point to the first non-whitespace character on this line."
701 (interactive)
702 (beginning-of-line 1)
703 (skip-syntax-forward " " (line-end-position))
704 ;; Move back over chars that have whitespace syntax but have the p flag.
705 (backward-prefix-chars))
707 (defun fixup-whitespace ()
708 "Fixup white space between objects around point.
709 Leave one space or none, according to the context."
710 (interactive "*")
711 (save-excursion
712 (delete-horizontal-space)
713 (if (or (looking-at "^\\|\\s)")
714 (save-excursion (forward-char -1)
715 (looking-at "$\\|\\s(\\|\\s'")))
717 (insert ?\s))))
719 (defun delete-horizontal-space (&optional backward-only)
720 "Delete all spaces and tabs around point.
721 If BACKWARD-ONLY is non-nil, only delete them before point."
722 (interactive "*P")
723 (let ((orig-pos (point)))
724 (delete-region
725 (if backward-only
726 orig-pos
727 (progn
728 (skip-chars-forward " \t")
729 (constrain-to-field nil orig-pos t)))
730 (progn
731 (skip-chars-backward " \t")
732 (constrain-to-field nil orig-pos)))))
734 (defun just-one-space (&optional n)
735 "Delete all spaces and tabs around point, leaving one space (or N spaces)."
736 (interactive "*p")
737 (let ((orig-pos (point)))
738 (skip-chars-backward " \t")
739 (constrain-to-field nil orig-pos)
740 (dotimes (i (or n 1))
741 (if (= (following-char) ?\s)
742 (forward-char 1)
743 (insert ?\s)))
744 (delete-region
745 (point)
746 (progn
747 (skip-chars-forward " \t")
748 (constrain-to-field nil orig-pos t)))))
750 (defun beginning-of-buffer (&optional arg)
751 "Move point to the beginning of the buffer; leave mark at previous position.
752 With \\[universal-argument] prefix, do not set mark at previous position.
753 With numeric arg N, put point N/10 of the way from the beginning.
755 If the buffer is narrowed, this command uses the beginning and size
756 of the accessible part of the buffer.
758 Don't use this command in Lisp programs!
759 \(goto-char (point-min)) is faster and avoids clobbering the mark."
760 (interactive "P")
761 (or (consp arg)
762 (and transient-mark-mode mark-active)
763 (push-mark))
764 (let ((size (- (point-max) (point-min))))
765 (goto-char (if (and arg (not (consp arg)))
766 (+ (point-min)
767 (if (> size 10000)
768 ;; Avoid overflow for large buffer sizes!
769 (* (prefix-numeric-value arg)
770 (/ size 10))
771 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
772 (point-min))))
773 (if (and arg (not (consp arg))) (forward-line 1)))
775 (defun end-of-buffer (&optional arg)
776 "Move point to the end of the buffer; leave mark at previous position.
777 With \\[universal-argument] prefix, do not set mark at previous position.
778 With numeric arg N, put point N/10 of the way from the end.
780 If the buffer is narrowed, this command uses the beginning and size
781 of the accessible part of the buffer.
783 Don't use this command in Lisp programs!
784 \(goto-char (point-max)) is faster and avoids clobbering the mark."
785 (interactive "P")
786 (or (consp arg)
787 (and transient-mark-mode mark-active)
788 (push-mark))
789 (let ((size (- (point-max) (point-min))))
790 (goto-char (if (and arg (not (consp arg)))
791 (- (point-max)
792 (if (> size 10000)
793 ;; Avoid overflow for large buffer sizes!
794 (* (prefix-numeric-value arg)
795 (/ size 10))
796 (/ (* size (prefix-numeric-value arg)) 10)))
797 (point-max))))
798 ;; If we went to a place in the middle of the buffer,
799 ;; adjust it to the beginning of a line.
800 (cond ((and arg (not (consp arg))) (forward-line 1))
801 ((> (point) (window-end nil t))
802 ;; If the end of the buffer is not already on the screen,
803 ;; then scroll specially to put it near, but not at, the bottom.
804 (overlay-recenter (point))
805 (recenter -3))))
807 (defun mark-whole-buffer ()
808 "Put point at beginning and mark at end of buffer.
809 You probably should not use this function in Lisp programs;
810 it is usually a mistake for a Lisp function to use any subroutine
811 that uses or sets the mark."
812 (interactive)
813 (push-mark (point))
814 (push-mark (point-max) nil t)
815 (goto-char (point-min)))
818 ;; Counting lines, one way or another.
820 (defun goto-line (arg &optional buffer)
821 "Goto line ARG, counting from line 1 at beginning of buffer.
822 Normally, move point in the current buffer, and leave mark at previous
823 position. With just \\[universal-argument] as argument, move point
824 in the most recently displayed other buffer, and switch to it.
825 When called from Lisp code, the optional argument BUFFER specifies
826 a buffer to switch to.
828 If there's a number in the buffer at point, it is the default for ARG."
829 (interactive
830 (if (and current-prefix-arg (not (consp current-prefix-arg)))
831 (list (prefix-numeric-value current-prefix-arg))
832 ;; Look for a default, a number in the buffer at point.
833 (let* ((default
834 (save-excursion
835 (skip-chars-backward "0-9")
836 (if (looking-at "[0-9]")
837 (buffer-substring-no-properties
838 (point)
839 (progn (skip-chars-forward "0-9")
840 (point))))))
841 ;; Decide if we're switching buffers.
842 (buffer
843 (if (consp current-prefix-arg)
844 (other-buffer (current-buffer) t)))
845 (buffer-prompt
846 (if buffer
847 (concat " in " (buffer-name buffer))
848 "")))
849 ;; Read the argument, offering that number (if any) as default.
850 (list (read-from-minibuffer (format (if default "Goto line%s (%s): "
851 "Goto line%s: ")
852 buffer-prompt
853 default)
854 nil nil t
855 'minibuffer-history
856 default)
857 buffer))))
858 ;; Switch to the desired buffer, one way or another.
859 (if buffer
860 (let ((window (get-buffer-window buffer)))
861 (if window (select-window window)
862 (switch-to-buffer-other-window buffer))))
863 ;; Leave mark at previous position
864 (or (and transient-mark-mode mark-active)
865 (push-mark))
866 ;; Move to the specified line number in that buffer.
867 (save-restriction
868 (widen)
869 (goto-char 1)
870 (if (eq selective-display t)
871 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
872 (forward-line (1- arg)))))
874 (defun count-lines-region (start end)
875 "Print number of lines and characters in the region."
876 (interactive "r")
877 (message "Region has %d lines, %d characters"
878 (count-lines start end) (- end start)))
880 (defun what-line ()
881 "Print the current buffer line number and narrowed line number of point."
882 (interactive)
883 (let ((start (point-min))
884 (n (line-number-at-pos)))
885 (if (= start 1)
886 (message "Line %d" n)
887 (save-excursion
888 (save-restriction
889 (widen)
890 (message "line %d (narrowed line %d)"
891 (+ n (line-number-at-pos start) -1) n))))))
893 (defun count-lines (start end)
894 "Return number of lines between START and END.
895 This is usually the number of newlines between them,
896 but can be one more if START is not equal to END
897 and the greater of them is not at the start of a line."
898 (save-excursion
899 (save-restriction
900 (narrow-to-region start end)
901 (goto-char (point-min))
902 (if (eq selective-display t)
903 (save-match-data
904 (let ((done 0))
905 (while (re-search-forward "[\n\C-m]" nil t 40)
906 (setq done (+ 40 done)))
907 (while (re-search-forward "[\n\C-m]" nil t 1)
908 (setq done (+ 1 done)))
909 (goto-char (point-max))
910 (if (and (/= start end)
911 (not (bolp)))
912 (1+ done)
913 done)))
914 (- (buffer-size) (forward-line (buffer-size)))))))
916 (defun line-number-at-pos (&optional pos)
917 "Return (narrowed) buffer line number at position POS.
918 If POS is nil, use current buffer location.
919 Counting starts at (point-min), so the value refers
920 to the contents of the accessible portion of the buffer."
921 (let ((opoint (or pos (point))) start)
922 (save-excursion
923 (goto-char (point-min))
924 (setq start (point))
925 (goto-char opoint)
926 (forward-line 0)
927 (1+ (count-lines start (point))))))
929 (defun what-cursor-position (&optional detail)
930 "Print info on cursor position (on screen and within buffer).
931 Also describe the character after point, and give its character code
932 in octal, decimal and hex.
934 For a non-ASCII multibyte character, also give its encoding in the
935 buffer's selected coding system if the coding system encodes the
936 character safely. If the character is encoded into one byte, that
937 code is shown in hex. If the character is encoded into more than one
938 byte, just \"...\" is shown.
940 In addition, with prefix argument, show details about that character
941 in *Help* buffer. See also the command `describe-char'."
942 (interactive "P")
943 (let* ((char (following-char))
944 (beg (point-min))
945 (end (point-max))
946 (pos (point))
947 (total (buffer-size))
948 (percent (if (> total 50000)
949 ;; Avoid overflow from multiplying by 100!
950 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
951 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
952 (hscroll (if (= (window-hscroll) 0)
954 (format " Hscroll=%d" (window-hscroll))))
955 (col (current-column)))
956 (if (= pos end)
957 (if (or (/= beg 1) (/= end (1+ total)))
958 (message "point=%d of %d (%d%%) <%d-%d> column=%d%s"
959 pos total percent beg end col hscroll)
960 (message "point=%d of %d (EOB) column=%d%s"
961 pos total col hscroll))
962 (let ((coding buffer-file-coding-system)
963 encoded encoding-msg display-prop under-display)
964 (if (or (not coding)
965 (eq (coding-system-type coding) t))
966 (setq coding default-buffer-file-coding-system))
967 (if (eq (char-charset char) 'eight-bit)
968 (setq encoding-msg
969 (format "(%d, #o%o, #x%x, raw-byte)" char char char))
970 ;; Check if the character is displayed with some `display'
971 ;; text property. In that case, set under-display to the
972 ;; buffer substring covered by that property.
973 (setq display-prop (get-text-property pos 'display))
974 (if display-prop
975 (let ((to (or (next-single-property-change pos 'display)
976 (point-max))))
977 (if (< to (+ pos 4))
978 (setq under-display "")
979 (setq under-display "..."
980 to (+ pos 4)))
981 (setq under-display
982 (concat (buffer-substring-no-properties pos to)
983 under-display)))
984 (setq encoded (and (>= char 128) (encode-coding-char char coding))))
985 (setq encoding-msg
986 (if display-prop
987 (if (not (stringp display-prop))
988 (format "(%d, #o%o, #x%x, part of display \"%s\")"
989 char char char under-display)
990 (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")"
991 char char char under-display display-prop))
992 (if encoded
993 (format "(%d, #o%o, #x%x, file %s)"
994 char char char
995 (if (> (length encoded) 1)
996 "..."
997 (encoded-string-description encoded coding)))
998 (format "(%d, #o%o, #x%x)" char char char)))))
999 (if detail
1000 ;; We show the detailed information about CHAR.
1001 (describe-char (point)))
1002 (if (or (/= beg 1) (/= end (1+ total)))
1003 (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
1004 (if (< char 256)
1005 (single-key-description char)
1006 (buffer-substring-no-properties (point) (1+ (point))))
1007 encoding-msg pos total percent beg end col hscroll)
1008 (message "Char: %s %s point=%d of %d (%d%%) column=%d%s"
1009 (if enable-multibyte-characters
1010 (if (< char 128)
1011 (single-key-description char)
1012 (buffer-substring-no-properties (point) (1+ (point))))
1013 (single-key-description char))
1014 encoding-msg pos total percent col hscroll))))))
1016 ;; Initialize read-expression-map. It is defined at C level.
1017 (let ((m (make-sparse-keymap)))
1018 (define-key m "\M-\t" 'lisp-complete-symbol)
1019 (set-keymap-parent m minibuffer-local-map)
1020 (setq read-expression-map m))
1022 (defvar read-expression-history nil)
1024 (defvar minibuffer-completing-symbol nil
1025 "Non-nil means completing a Lisp symbol in the minibuffer.")
1027 (defcustom eval-expression-print-level 4
1028 "Value for `print-level' while printing value in `eval-expression'.
1029 A value of nil means no limit."
1030 :group 'lisp
1031 :type '(choice (const :tag "No Limit" nil) integer)
1032 :version "21.1")
1034 (defcustom eval-expression-print-length 12
1035 "Value for `print-length' while printing value in `eval-expression'.
1036 A value of nil means no limit."
1037 :group 'lisp
1038 :type '(choice (const :tag "No Limit" nil) integer)
1039 :version "21.1")
1041 (defcustom eval-expression-debug-on-error t
1042 "If non-nil set `debug-on-error' to t in `eval-expression'.
1043 If nil, don't change the value of `debug-on-error'."
1044 :group 'lisp
1045 :type 'boolean
1046 :version "21.1")
1048 (defun eval-expression-print-format (value)
1049 "Format VALUE as a result of evaluated expression.
1050 Return a formatted string which is displayed in the echo area
1051 in addition to the value printed by prin1 in functions which
1052 display the result of expression evaluation."
1053 (if (and (integerp value)
1054 (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
1055 (eq this-command last-command)
1056 (if (boundp 'edebug-active) edebug-active)))
1057 (let ((char-string
1058 (if (or (if (boundp 'edebug-active) edebug-active)
1059 (memq this-command '(eval-last-sexp eval-print-last-sexp)))
1060 (prin1-char value))))
1061 (if char-string
1062 (format " (#o%o, #x%x, %s)" value value char-string)
1063 (format " (#o%o, #x%x)" value value)))))
1065 ;; We define this, rather than making `eval' interactive,
1066 ;; for the sake of completion of names like eval-region, eval-buffer.
1067 (defun eval-expression (eval-expression-arg
1068 &optional eval-expression-insert-value)
1069 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
1070 Value is also consed on to front of the variable `values'.
1071 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
1072 insert the result into the current buffer instead of printing it in
1073 the echo area.
1075 If `eval-expression-debug-on-error' is non-nil, which is the default,
1076 this command arranges for all errors to enter the debugger."
1077 (interactive
1078 (list (let ((minibuffer-completing-symbol t))
1079 (read-from-minibuffer "Eval: "
1080 nil read-expression-map t
1081 'read-expression-history))
1082 current-prefix-arg))
1084 (if (null eval-expression-debug-on-error)
1085 (setq values (cons (eval eval-expression-arg) values))
1086 (let ((old-value (make-symbol "t")) new-value)
1087 ;; Bind debug-on-error to something unique so that we can
1088 ;; detect when evaled code changes it.
1089 (let ((debug-on-error old-value))
1090 (setq values (cons (eval eval-expression-arg) values))
1091 (setq new-value debug-on-error))
1092 ;; If evaled code has changed the value of debug-on-error,
1093 ;; propagate that change to the global binding.
1094 (unless (eq old-value new-value)
1095 (setq debug-on-error new-value))))
1097 (let ((print-length eval-expression-print-length)
1098 (print-level eval-expression-print-level))
1099 (if eval-expression-insert-value
1100 (with-no-warnings
1101 (let ((standard-output (current-buffer)))
1102 (prin1 (car values))))
1103 (prog1
1104 (prin1 (car values) t)
1105 (let ((str (eval-expression-print-format (car values))))
1106 (if str (princ str t)))))))
1108 (defun edit-and-eval-command (prompt command)
1109 "Prompting with PROMPT, let user edit COMMAND and eval result.
1110 COMMAND is a Lisp expression. Let user edit that expression in
1111 the minibuffer, then read and evaluate the result."
1112 (let ((command
1113 (let ((print-level nil)
1114 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1115 (unwind-protect
1116 (read-from-minibuffer prompt
1117 (prin1-to-string command)
1118 read-expression-map t
1119 'command-history)
1120 ;; If command was added to command-history as a string,
1121 ;; get rid of that. We want only evaluable expressions there.
1122 (if (stringp (car command-history))
1123 (setq command-history (cdr command-history)))))))
1125 ;; If command to be redone does not match front of history,
1126 ;; add it to the history.
1127 (or (equal command (car command-history))
1128 (setq command-history (cons command command-history)))
1129 (eval command)))
1131 (defun repeat-complex-command (arg)
1132 "Edit and re-evaluate last complex command, or ARGth from last.
1133 A complex command is one which used the minibuffer.
1134 The command is placed in the minibuffer as a Lisp form for editing.
1135 The result is executed, repeating the command as changed.
1136 If the command has been changed or is not the most recent previous command
1137 it is added to the front of the command history.
1138 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
1139 to get different commands to edit and resubmit."
1140 (interactive "p")
1141 (let ((elt (nth (1- arg) command-history))
1142 newcmd)
1143 (if elt
1144 (progn
1145 (setq newcmd
1146 (let ((print-level nil)
1147 (minibuffer-history-position arg)
1148 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1149 (unwind-protect
1150 (read-from-minibuffer
1151 "Redo: " (prin1-to-string elt) read-expression-map t
1152 (cons 'command-history arg))
1154 ;; If command was added to command-history as a
1155 ;; string, get rid of that. We want only
1156 ;; evaluable expressions there.
1157 (if (stringp (car command-history))
1158 (setq command-history (cdr command-history))))))
1160 ;; If command to be redone does not match front of history,
1161 ;; add it to the history.
1162 (or (equal newcmd (car command-history))
1163 (setq command-history (cons newcmd command-history)))
1164 (eval newcmd))
1165 (if command-history
1166 (error "Argument %d is beyond length of command history" arg)
1167 (error "There are no previous complex commands to repeat")))))
1169 (defvar minibuffer-history nil
1170 "Default minibuffer history list.
1171 This is used for all minibuffer input
1172 except when an alternate history list is specified.
1174 Maximum length of the history list is determined by the value
1175 of `history-length', which see.")
1176 (defvar minibuffer-history-sexp-flag nil
1177 "Control whether history list elements are expressions or strings.
1178 If the value of this variable equals current minibuffer depth,
1179 they are expressions; otherwise they are strings.
1180 \(That convention is designed to do the right thing for
1181 recursive uses of the minibuffer.)")
1182 (setq minibuffer-history-variable 'minibuffer-history)
1183 (setq minibuffer-history-position nil) ;; Defvar is in C code.
1184 (defvar minibuffer-history-search-history nil)
1186 (defvar minibuffer-text-before-history nil
1187 "Text that was in this minibuffer before any history commands.
1188 This is nil if there have not yet been any history commands
1189 in this use of the minibuffer.")
1191 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
1193 (defun minibuffer-history-initialize ()
1194 (setq minibuffer-text-before-history nil))
1196 (defun minibuffer-avoid-prompt (new old)
1197 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1198 (constrain-to-field nil (point-max)))
1200 (defcustom minibuffer-history-case-insensitive-variables nil
1201 "*Minibuffer history variables for which matching should ignore case.
1202 If a history variable is a member of this list, then the
1203 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1204 commands ignore case when searching it, regardless of `case-fold-search'."
1205 :type '(repeat variable)
1206 :group 'minibuffer)
1208 (defun previous-matching-history-element (regexp n)
1209 "Find the previous history element that matches REGEXP.
1210 \(Previous history elements refer to earlier actions.)
1211 With prefix argument N, search for Nth previous match.
1212 If N is negative, find the next or Nth next match.
1213 Normally, history elements are matched case-insensitively if
1214 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1215 makes the search case-sensitive.
1216 See also `minibuffer-history-case-insensitive-variables'."
1217 (interactive
1218 (let* ((enable-recursive-minibuffers t)
1219 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1221 minibuffer-local-map
1223 'minibuffer-history-search-history
1224 (car minibuffer-history-search-history))))
1225 ;; Use the last regexp specified, by default, if input is empty.
1226 (list (if (string= regexp "")
1227 (if minibuffer-history-search-history
1228 (car minibuffer-history-search-history)
1229 (error "No previous history search regexp"))
1230 regexp)
1231 (prefix-numeric-value current-prefix-arg))))
1232 (unless (zerop n)
1233 (if (and (zerop minibuffer-history-position)
1234 (null minibuffer-text-before-history))
1235 (setq minibuffer-text-before-history
1236 (minibuffer-contents-no-properties)))
1237 (let ((history (symbol-value minibuffer-history-variable))
1238 (case-fold-search
1239 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
1240 ;; On some systems, ignore case for file names.
1241 (if (memq minibuffer-history-variable
1242 minibuffer-history-case-insensitive-variables)
1244 ;; Respect the user's setting for case-fold-search:
1245 case-fold-search)
1246 nil))
1247 prevpos
1248 match-string
1249 match-offset
1250 (pos minibuffer-history-position))
1251 (while (/= n 0)
1252 (setq prevpos pos)
1253 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1254 (when (= pos prevpos)
1255 (error (if (= pos 1)
1256 "No later matching history item"
1257 "No earlier matching history item")))
1258 (setq match-string
1259 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
1260 (let ((print-level nil))
1261 (prin1-to-string (nth (1- pos) history)))
1262 (nth (1- pos) history)))
1263 (setq match-offset
1264 (if (< n 0)
1265 (and (string-match regexp match-string)
1266 (match-end 0))
1267 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
1268 (match-beginning 1))))
1269 (when match-offset
1270 (setq n (+ n (if (< n 0) 1 -1)))))
1271 (setq minibuffer-history-position pos)
1272 (goto-char (point-max))
1273 (delete-minibuffer-contents)
1274 (insert match-string)
1275 (goto-char (+ (minibuffer-prompt-end) match-offset))))
1276 (if (memq (car (car command-history)) '(previous-matching-history-element
1277 next-matching-history-element))
1278 (setq command-history (cdr command-history))))
1280 (defun next-matching-history-element (regexp n)
1281 "Find the next history element that matches REGEXP.
1282 \(The next history element refers to a more recent action.)
1283 With prefix argument N, search for Nth next match.
1284 If N is negative, find the previous or Nth previous match.
1285 Normally, history elements are matched case-insensitively if
1286 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1287 makes the search case-sensitive."
1288 (interactive
1289 (let* ((enable-recursive-minibuffers t)
1290 (regexp (read-from-minibuffer "Next element matching (regexp): "
1292 minibuffer-local-map
1294 'minibuffer-history-search-history
1295 (car minibuffer-history-search-history))))
1296 ;; Use the last regexp specified, by default, if input is empty.
1297 (list (if (string= regexp "")
1298 (if minibuffer-history-search-history
1299 (car minibuffer-history-search-history)
1300 (error "No previous history search regexp"))
1301 regexp)
1302 (prefix-numeric-value current-prefix-arg))))
1303 (previous-matching-history-element regexp (- n)))
1305 (defvar minibuffer-temporary-goal-position nil)
1307 (defun goto-history-element (nabs)
1308 "Puts element of the minibuffer history in the minibuffer.
1309 The argument NABS specifies the absolute history position."
1310 (interactive "p")
1311 (let ((minimum (if minibuffer-default
1312 (- (if (listp minibuffer-default)
1313 (length minibuffer-default)
1316 elt minibuffer-returned-to-present)
1317 (if (and (zerop minibuffer-history-position)
1318 (null minibuffer-text-before-history))
1319 (setq minibuffer-text-before-history
1320 (minibuffer-contents-no-properties)))
1321 (if (< nabs minimum)
1322 (if minibuffer-default
1323 (error "End of history; no next item")
1324 (error "End of history; no default available")))
1325 (if (> nabs (length (symbol-value minibuffer-history-variable)))
1326 (error "Beginning of history; no preceding item"))
1327 (unless (memq last-command '(next-history-element
1328 previous-history-element))
1329 (let ((prompt-end (minibuffer-prompt-end)))
1330 (set (make-local-variable 'minibuffer-temporary-goal-position)
1331 (cond ((<= (point) prompt-end) prompt-end)
1332 ((eobp) nil)
1333 (t (point))))))
1334 (goto-char (point-max))
1335 (delete-minibuffer-contents)
1336 (setq minibuffer-history-position nabs)
1337 (cond ((< nabs 0)
1338 (setq elt (if (listp minibuffer-default)
1339 (nth (1- (abs nabs)) minibuffer-default)
1340 minibuffer-default)))
1341 ((= nabs 0)
1342 (setq elt (or minibuffer-text-before-history ""))
1343 (setq minibuffer-returned-to-present t)
1344 (setq minibuffer-text-before-history nil))
1345 (t (setq elt (nth (1- minibuffer-history-position)
1346 (symbol-value minibuffer-history-variable)))))
1347 (insert
1348 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
1349 (not minibuffer-returned-to-present))
1350 (let ((print-level nil))
1351 (prin1-to-string elt))
1352 elt))
1353 (goto-char (or minibuffer-temporary-goal-position (point-max)))))
1355 (defun next-history-element (n)
1356 "Puts next element of the minibuffer history in the minibuffer.
1357 With argument N, it uses the Nth following element."
1358 (interactive "p")
1359 (or (zerop n)
1360 (goto-history-element (- minibuffer-history-position n))))
1362 (defun previous-history-element (n)
1363 "Puts previous element of the minibuffer history in the minibuffer.
1364 With argument N, it uses the Nth previous element."
1365 (interactive "p")
1366 (or (zerop n)
1367 (goto-history-element (+ minibuffer-history-position n))))
1369 (defun next-complete-history-element (n)
1370 "Get next history element which completes the minibuffer before the point.
1371 The contents of the minibuffer after the point are deleted, and replaced
1372 by the new completion."
1373 (interactive "p")
1374 (let ((point-at-start (point)))
1375 (next-matching-history-element
1376 (concat
1377 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1379 ;; next-matching-history-element always puts us at (point-min).
1380 ;; Move to the position we were at before changing the buffer contents.
1381 ;; This is still sensical, because the text before point has not changed.
1382 (goto-char point-at-start)))
1384 (defun previous-complete-history-element (n)
1386 Get previous history element which completes the minibuffer before the point.
1387 The contents of the minibuffer after the point are deleted, and replaced
1388 by the new completion."
1389 (interactive "p")
1390 (next-complete-history-element (- n)))
1392 ;; For compatibility with the old subr of the same name.
1393 (defun minibuffer-prompt-width ()
1394 "Return the display width of the minibuffer prompt.
1395 Return 0 if current buffer is not a minibuffer."
1396 ;; Return the width of everything before the field at the end of
1397 ;; the buffer; this should be 0 for normal buffers.
1398 (1- (minibuffer-prompt-end)))
1400 ;; isearch minibuffer history
1401 (add-hook 'minibuffer-setup-hook 'minibuffer-history-isearch-setup)
1403 (defvar minibuffer-history-isearch-message-overlay)
1404 (make-variable-buffer-local 'minibuffer-history-isearch-message-overlay)
1406 (defun minibuffer-history-isearch-setup ()
1407 "Set up a minibuffer for using isearch to search the minibuffer history.
1408 Intended to be added to `minibuffer-setup-hook'."
1409 (set (make-local-variable 'isearch-search-fun-function)
1410 'minibuffer-history-isearch-search)
1411 (set (make-local-variable 'isearch-message-function)
1412 'minibuffer-history-isearch-message)
1413 (set (make-local-variable 'isearch-wrap-function)
1414 'minibuffer-history-isearch-wrap)
1415 (set (make-local-variable 'isearch-push-state-function)
1416 'minibuffer-history-isearch-push-state)
1417 (add-hook 'isearch-mode-end-hook 'minibuffer-history-isearch-end nil t))
1419 (defun minibuffer-history-isearch-end ()
1420 "Clean up the minibuffer after terminating isearch in the minibuffer."
1421 (if minibuffer-history-isearch-message-overlay
1422 (delete-overlay minibuffer-history-isearch-message-overlay)))
1424 (defun minibuffer-history-isearch-search ()
1425 "Return the proper search function, for isearch in minibuffer history."
1426 (cond
1427 (isearch-word
1428 (if isearch-forward 'word-search-forward 'word-search-backward))
1430 (lambda (string bound noerror)
1431 (let ((search-fun
1432 ;; Use standard functions to search within minibuffer text
1433 (cond
1434 (isearch-regexp
1435 (if isearch-forward 're-search-forward 're-search-backward))
1437 (if isearch-forward 'search-forward 'search-backward))))
1438 found)
1439 ;; Avoid lazy-highlighting matches in the minibuffer prompt when
1440 ;; searching forward. Lazy-highlight calls this lambda with the
1441 ;; bound arg, so skip the minibuffer prompt.
1442 (if (and bound isearch-forward (< (point) (minibuffer-prompt-end)))
1443 (goto-char (minibuffer-prompt-end)))
1445 ;; 1. First try searching in the initial minibuffer text
1446 (funcall search-fun string
1447 (if isearch-forward bound (minibuffer-prompt-end))
1448 noerror)
1449 ;; 2. If the above search fails, start putting next/prev history
1450 ;; elements in the minibuffer successively, and search the string
1451 ;; in them. Do this only when bound is nil (i.e. not while
1452 ;; lazy-highlighting search strings in the current minibuffer text).
1453 (unless bound
1454 (condition-case nil
1455 (progn
1456 (while (not found)
1457 (cond (isearch-forward
1458 (next-history-element 1)
1459 (goto-char (minibuffer-prompt-end)))
1461 (previous-history-element 1)
1462 (goto-char (point-max))))
1463 (setq isearch-barrier (point) isearch-opoint (point))
1464 ;; After putting the next/prev history element, search
1465 ;; the string in them again, until next-history-element
1466 ;; or previous-history-element raises an error at the
1467 ;; beginning/end of history.
1468 (setq found (funcall search-fun string
1469 (unless isearch-forward
1470 ;; For backward search, don't search
1471 ;; in the minibuffer prompt
1472 (minibuffer-prompt-end))
1473 noerror)))
1474 ;; Return point of the new search result
1475 (point))
1476 ;; Return nil when next(prev)-history-element fails
1477 (error nil)))))))))
1479 (defun minibuffer-history-isearch-message (&optional c-q-hack ellipsis)
1480 "Display the minibuffer history search prompt.
1481 If there are no search errors, this function displays an overlay with
1482 the isearch prompt which replaces the original minibuffer prompt.
1483 Otherwise, it displays the standard isearch message returned from
1484 `isearch-message'."
1485 (if (not (and (minibufferp) isearch-success (not isearch-error)))
1486 ;; Use standard function `isearch-message' when not in the minibuffer,
1487 ;; or search fails, or has an error (like incomplete regexp).
1488 ;; This function overwrites minibuffer text with isearch message,
1489 ;; so it's possible to see what is wrong in the search string.
1490 (isearch-message c-q-hack ellipsis)
1491 ;; Otherwise, put the overlay with the standard isearch prompt over
1492 ;; the initial minibuffer prompt.
1493 (if (overlayp minibuffer-history-isearch-message-overlay)
1494 (move-overlay minibuffer-history-isearch-message-overlay
1495 (point-min) (minibuffer-prompt-end))
1496 (setq minibuffer-history-isearch-message-overlay
1497 (make-overlay (point-min) (minibuffer-prompt-end)))
1498 (overlay-put minibuffer-history-isearch-message-overlay 'evaporate t))
1499 (overlay-put minibuffer-history-isearch-message-overlay
1500 'display (isearch-message-prefix c-q-hack ellipsis))
1501 ;; And clear any previous isearch message.
1502 (message "")))
1504 (defun minibuffer-history-isearch-wrap ()
1505 "Wrap the minibuffer history search when search is failed.
1506 Move point to the first history element for a forward search,
1507 or to the last history element for a backward search."
1508 (unless isearch-word
1509 ;; When `minibuffer-history-isearch-search' fails on reaching the
1510 ;; beginning/end of the history, wrap the search to the first/last
1511 ;; minibuffer history element.
1512 (if isearch-forward
1513 (goto-history-element (length (symbol-value minibuffer-history-variable)))
1514 (goto-history-element 0))
1515 (setq isearch-success t))
1516 (goto-char (if isearch-forward (minibuffer-prompt-end) (point-max))))
1518 (defun minibuffer-history-isearch-push-state ()
1519 "Save a function restoring the state of minibuffer history search.
1520 Save `minibuffer-history-position' to the additional state parameter
1521 in the search status stack."
1522 `(lambda (cmd)
1523 (minibuffer-history-isearch-pop-state cmd ,minibuffer-history-position)))
1525 (defun minibuffer-history-isearch-pop-state (cmd hist-pos)
1526 "Restore the minibuffer history search state.
1527 Go to the history element by the absolute history position `hist-pos'."
1528 (goto-history-element hist-pos))
1531 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1532 (defalias 'advertised-undo 'undo)
1534 (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t)
1535 "Table mapping redo records to the corresponding undo one.
1536 A redo record for undo-in-region maps to t.
1537 A redo record for ordinary undo maps to the following (earlier) undo.")
1539 (defvar undo-in-region nil
1540 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1542 (defvar undo-no-redo nil
1543 "If t, `undo' doesn't go through redo entries.")
1545 (defvar pending-undo-list nil
1546 "Within a run of consecutive undo commands, list remaining to be undone.
1547 If t, we undid all the way to the end of it.")
1549 (defun undo (&optional arg)
1550 "Undo some previous changes.
1551 Repeat this command to undo more changes.
1552 A numeric argument serves as a repeat count.
1554 In Transient Mark mode when the mark is active, only undo changes within
1555 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1556 as an argument limits undo to changes within the current region."
1557 (interactive "*P")
1558 ;; Make last-command indicate for the next command that this was an undo.
1559 ;; That way, another undo will undo more.
1560 ;; If we get to the end of the undo history and get an error,
1561 ;; another undo command will find the undo history empty
1562 ;; and will get another error. To begin undoing the undos,
1563 ;; you must type some other command.
1564 (let ((modified (buffer-modified-p))
1565 (recent-save (recent-auto-save-p))
1566 message)
1567 ;; If we get an error in undo-start,
1568 ;; the next command should not be a "consecutive undo".
1569 ;; So set `this-command' to something other than `undo'.
1570 (setq this-command 'undo-start)
1572 (unless (and (eq last-command 'undo)
1573 (or (eq pending-undo-list t)
1574 ;; If something (a timer or filter?) changed the buffer
1575 ;; since the previous command, don't continue the undo seq.
1576 (let ((list buffer-undo-list))
1577 (while (eq (car list) nil)
1578 (setq list (cdr list)))
1579 ;; If the last undo record made was made by undo
1580 ;; it shows nothing else happened in between.
1581 (gethash list undo-equiv-table))))
1582 (setq undo-in-region
1583 (if transient-mark-mode mark-active (and arg (not (numberp arg)))))
1584 (if undo-in-region
1585 (undo-start (region-beginning) (region-end))
1586 (undo-start))
1587 ;; get rid of initial undo boundary
1588 (undo-more 1))
1589 ;; If we got this far, the next command should be a consecutive undo.
1590 (setq this-command 'undo)
1591 ;; Check to see whether we're hitting a redo record, and if
1592 ;; so, ask the user whether she wants to skip the redo/undo pair.
1593 (let ((equiv (gethash pending-undo-list undo-equiv-table)))
1594 (or (eq (selected-window) (minibuffer-window))
1595 (setq message (if undo-in-region
1596 (if equiv "Redo in region!" "Undo in region!")
1597 (if equiv "Redo!" "Undo!"))))
1598 (when (and (consp equiv) undo-no-redo)
1599 ;; The equiv entry might point to another redo record if we have done
1600 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1601 (while (let ((next (gethash equiv undo-equiv-table)))
1602 (if next (setq equiv next))))
1603 (setq pending-undo-list equiv)))
1604 (undo-more
1605 (if (or transient-mark-mode (numberp arg))
1606 (prefix-numeric-value arg)
1608 ;; Record the fact that the just-generated undo records come from an
1609 ;; undo operation--that is, they are redo records.
1610 ;; In the ordinary case (not within a region), map the redo
1611 ;; record to the following undos.
1612 ;; I don't know how to do that in the undo-in-region case.
1613 (puthash buffer-undo-list
1614 (if undo-in-region t pending-undo-list)
1615 undo-equiv-table)
1616 ;; Don't specify a position in the undo record for the undo command.
1617 ;; Instead, undoing this should move point to where the change is.
1618 (let ((tail buffer-undo-list)
1619 (prev nil))
1620 (while (car tail)
1621 (when (integerp (car tail))
1622 (let ((pos (car tail)))
1623 (if prev
1624 (setcdr prev (cdr tail))
1625 (setq buffer-undo-list (cdr tail)))
1626 (setq tail (cdr tail))
1627 (while (car tail)
1628 (if (eq pos (car tail))
1629 (if prev
1630 (setcdr prev (cdr tail))
1631 (setq buffer-undo-list (cdr tail)))
1632 (setq prev tail))
1633 (setq tail (cdr tail)))
1634 (setq tail nil)))
1635 (setq prev tail tail (cdr tail))))
1636 ;; Record what the current undo list says,
1637 ;; so the next command can tell if the buffer was modified in between.
1638 (and modified (not (buffer-modified-p))
1639 (delete-auto-save-file-if-necessary recent-save))
1640 ;; Display a message announcing success.
1641 (if message
1642 (message "%s" message))))
1644 (defun buffer-disable-undo (&optional buffer)
1645 "Make BUFFER stop keeping undo information.
1646 No argument or nil as argument means do this for the current buffer."
1647 (interactive)
1648 (with-current-buffer (if buffer (get-buffer buffer) (current-buffer))
1649 (setq buffer-undo-list t)))
1651 (defun undo-only (&optional arg)
1652 "Undo some previous changes.
1653 Repeat this command to undo more changes.
1654 A numeric argument serves as a repeat count.
1655 Contrary to `undo', this will not redo a previous undo."
1656 (interactive "*p")
1657 (let ((undo-no-redo t)) (undo arg)))
1659 (defvar undo-in-progress nil
1660 "Non-nil while performing an undo.
1661 Some change-hooks test this variable to do something different.")
1663 (defun undo-more (n)
1664 "Undo back N undo-boundaries beyond what was already undone recently.
1665 Call `undo-start' to get ready to undo recent changes,
1666 then call `undo-more' one or more times to undo them."
1667 (or (listp pending-undo-list)
1668 (error (concat "No further undo information"
1669 (and undo-in-region " for region"))))
1670 (let ((undo-in-progress t))
1671 (setq pending-undo-list (primitive-undo n pending-undo-list))
1672 (if (null pending-undo-list)
1673 (setq pending-undo-list t))))
1675 ;; Deep copy of a list
1676 (defun undo-copy-list (list)
1677 "Make a copy of undo list LIST."
1678 (mapcar 'undo-copy-list-1 list))
1680 (defun undo-copy-list-1 (elt)
1681 (if (consp elt)
1682 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1683 elt))
1685 (defun undo-start (&optional beg end)
1686 "Set `pending-undo-list' to the front of the undo list.
1687 The next call to `undo-more' will undo the most recently made change.
1688 If BEG and END are specified, then only undo elements
1689 that apply to text between BEG and END are used; other undo elements
1690 are ignored. If BEG and END are nil, all undo elements are used."
1691 (if (eq buffer-undo-list t)
1692 (error "No undo information in this buffer"))
1693 (setq pending-undo-list
1694 (if (and beg end (not (= beg end)))
1695 (undo-make-selective-list (min beg end) (max beg end))
1696 buffer-undo-list)))
1698 (defvar undo-adjusted-markers)
1700 (defun undo-make-selective-list (start end)
1701 "Return a list of undo elements for the region START to END.
1702 The elements come from `buffer-undo-list', but we keep only
1703 the elements inside this region, and discard those outside this region.
1704 If we find an element that crosses an edge of this region,
1705 we stop and ignore all further elements."
1706 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1707 (undo-list (list nil))
1708 undo-adjusted-markers
1709 some-rejected
1710 undo-elt undo-elt temp-undo-list delta)
1711 (while undo-list-copy
1712 (setq undo-elt (car undo-list-copy))
1713 (let ((keep-this
1714 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1715 ;; This is a "was unmodified" element.
1716 ;; Keep it if we have kept everything thus far.
1717 (not some-rejected))
1719 (undo-elt-in-region undo-elt start end)))))
1720 (if keep-this
1721 (progn
1722 (setq end (+ end (cdr (undo-delta undo-elt))))
1723 ;; Don't put two nils together in the list
1724 (if (not (and (eq (car undo-list) nil)
1725 (eq undo-elt nil)))
1726 (setq undo-list (cons undo-elt undo-list))))
1727 (if (undo-elt-crosses-region undo-elt start end)
1728 (setq undo-list-copy nil)
1729 (setq some-rejected t)
1730 (setq temp-undo-list (cdr undo-list-copy))
1731 (setq delta (undo-delta undo-elt))
1733 (when (/= (cdr delta) 0)
1734 (let ((position (car delta))
1735 (offset (cdr delta)))
1737 ;; Loop down the earlier events adjusting their buffer
1738 ;; positions to reflect the fact that a change to the buffer
1739 ;; isn't being undone. We only need to process those element
1740 ;; types which undo-elt-in-region will return as being in
1741 ;; the region since only those types can ever get into the
1742 ;; output
1744 (while temp-undo-list
1745 (setq undo-elt (car temp-undo-list))
1746 (cond ((integerp undo-elt)
1747 (if (>= undo-elt position)
1748 (setcar temp-undo-list (- undo-elt offset))))
1749 ((atom undo-elt) nil)
1750 ((stringp (car undo-elt))
1751 ;; (TEXT . POSITION)
1752 (let ((text-pos (abs (cdr undo-elt)))
1753 (point-at-end (< (cdr undo-elt) 0 )))
1754 (if (>= text-pos position)
1755 (setcdr undo-elt (* (if point-at-end -1 1)
1756 (- text-pos offset))))))
1757 ((integerp (car undo-elt))
1758 ;; (BEGIN . END)
1759 (when (>= (car undo-elt) position)
1760 (setcar undo-elt (- (car undo-elt) offset))
1761 (setcdr undo-elt (- (cdr undo-elt) offset))))
1762 ((null (car undo-elt))
1763 ;; (nil PROPERTY VALUE BEG . END)
1764 (let ((tail (nthcdr 3 undo-elt)))
1765 (when (>= (car tail) position)
1766 (setcar tail (- (car tail) offset))
1767 (setcdr tail (- (cdr tail) offset))))))
1768 (setq temp-undo-list (cdr temp-undo-list))))))))
1769 (setq undo-list-copy (cdr undo-list-copy)))
1770 (nreverse undo-list)))
1772 (defun undo-elt-in-region (undo-elt start end)
1773 "Determine whether UNDO-ELT falls inside the region START ... END.
1774 If it crosses the edge, we return nil."
1775 (cond ((integerp undo-elt)
1776 (and (>= undo-elt start)
1777 (<= undo-elt end)))
1778 ((eq undo-elt nil)
1780 ((atom undo-elt)
1781 nil)
1782 ((stringp (car undo-elt))
1783 ;; (TEXT . POSITION)
1784 (and (>= (abs (cdr undo-elt)) start)
1785 (< (abs (cdr undo-elt)) end)))
1786 ((and (consp undo-elt) (markerp (car undo-elt)))
1787 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1788 ;; See if MARKER is inside the region.
1789 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1790 (unless alist-elt
1791 (setq alist-elt (cons (car undo-elt)
1792 (marker-position (car undo-elt))))
1793 (setq undo-adjusted-markers
1794 (cons alist-elt undo-adjusted-markers)))
1795 (and (cdr alist-elt)
1796 (>= (cdr alist-elt) start)
1797 (<= (cdr alist-elt) end))))
1798 ((null (car undo-elt))
1799 ;; (nil PROPERTY VALUE BEG . END)
1800 (let ((tail (nthcdr 3 undo-elt)))
1801 (and (>= (car tail) start)
1802 (<= (cdr tail) end))))
1803 ((integerp (car undo-elt))
1804 ;; (BEGIN . END)
1805 (and (>= (car undo-elt) start)
1806 (<= (cdr undo-elt) end)))))
1808 (defun undo-elt-crosses-region (undo-elt start end)
1809 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1810 This assumes we have already decided that UNDO-ELT
1811 is not *inside* the region START...END."
1812 (cond ((atom undo-elt) nil)
1813 ((null (car undo-elt))
1814 ;; (nil PROPERTY VALUE BEG . END)
1815 (let ((tail (nthcdr 3 undo-elt)))
1816 (and (< (car tail) end)
1817 (> (cdr tail) start))))
1818 ((integerp (car undo-elt))
1819 ;; (BEGIN . END)
1820 (and (< (car undo-elt) end)
1821 (> (cdr undo-elt) start)))))
1823 ;; Return the first affected buffer position and the delta for an undo element
1824 ;; delta is defined as the change in subsequent buffer positions if we *did*
1825 ;; the undo.
1826 (defun undo-delta (undo-elt)
1827 (if (consp undo-elt)
1828 (cond ((stringp (car undo-elt))
1829 ;; (TEXT . POSITION)
1830 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1831 ((integerp (car undo-elt))
1832 ;; (BEGIN . END)
1833 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1835 '(0 . 0)))
1836 '(0 . 0)))
1838 (defcustom undo-ask-before-discard nil
1839 "If non-nil ask about discarding undo info for the current command.
1840 Normally, Emacs discards the undo info for the current command if
1841 it exceeds `undo-outer-limit'. But if you set this option
1842 non-nil, it asks in the echo area whether to discard the info.
1843 If you answer no, there is a slight risk that Emacs might crash, so
1844 only do it if you really want to undo the command.
1846 This option is mainly intended for debugging. You have to be
1847 careful if you use it for other purposes. Garbage collection is
1848 inhibited while the question is asked, meaning that Emacs might
1849 leak memory. So you should make sure that you do not wait
1850 excessively long before answering the question."
1851 :type 'boolean
1852 :group 'undo
1853 :version "22.1")
1855 (defvar undo-extra-outer-limit nil
1856 "If non-nil, an extra level of size that's ok in an undo item.
1857 We don't ask the user about truncating the undo list until the
1858 current item gets bigger than this amount.
1860 This variable only matters if `undo-ask-before-discard' is non-nil.")
1861 (make-variable-buffer-local 'undo-extra-outer-limit)
1863 ;; When the first undo batch in an undo list is longer than
1864 ;; undo-outer-limit, this function gets called to warn the user that
1865 ;; the undo info for the current command was discarded. Garbage
1866 ;; collection is inhibited around the call, so it had better not do a
1867 ;; lot of consing.
1868 (setq undo-outer-limit-function 'undo-outer-limit-truncate)
1869 (defun undo-outer-limit-truncate (size)
1870 (if undo-ask-before-discard
1871 (when (or (null undo-extra-outer-limit)
1872 (> size undo-extra-outer-limit))
1873 ;; Don't ask the question again unless it gets even bigger.
1874 ;; This applies, in particular, if the user quits from the question.
1875 ;; Such a quit quits out of GC, but something else will call GC
1876 ;; again momentarily. It will call this function again,
1877 ;; but we don't want to ask the question again.
1878 (setq undo-extra-outer-limit (+ size 50000))
1879 (if (let (use-dialog-box track-mouse executing-kbd-macro )
1880 (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? "
1881 (buffer-name) size)))
1882 (progn (setq buffer-undo-list nil)
1883 (setq undo-extra-outer-limit nil)
1885 nil))
1886 (display-warning '(undo discard-info)
1887 (concat
1888 (format "Buffer `%s' undo info was %d bytes long.\n"
1889 (buffer-name) size)
1890 "The undo info was discarded because it exceeded \
1891 `undo-outer-limit'.
1893 This is normal if you executed a command that made a huge change
1894 to the buffer. In that case, to prevent similar problems in the
1895 future, set `undo-outer-limit' to a value that is large enough to
1896 cover the maximum size of normal changes you expect a single
1897 command to make, but not so large that it might exceed the
1898 maximum memory allotted to Emacs.
1900 If you did not execute any such command, the situation is
1901 probably due to a bug and you should report it.
1903 You can disable the popping up of this buffer by adding the entry
1904 \(undo discard-info) to the user option `warning-suppress-types'.\n")
1905 :warning)
1906 (setq buffer-undo-list nil)
1909 (defvar shell-command-history nil
1910 "History list for some commands that read shell commands.
1912 Maximum length of the history list is determined by the value
1913 of `history-length', which see.")
1915 (defvar shell-command-switch "-c"
1916 "Switch used to have the shell execute its command line argument.")
1918 (defvar shell-command-default-error-buffer nil
1919 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1920 This buffer is used when `shell-command' or `shell-command-on-region'
1921 is run interactively. A value of nil means that output to stderr and
1922 stdout will be intermixed in the output stream.")
1924 (defun shell-command (command &optional output-buffer error-buffer)
1925 "Execute string COMMAND in inferior shell; display output, if any.
1926 With prefix argument, insert the COMMAND's output at point.
1928 If COMMAND ends in ampersand, execute it asynchronously.
1929 The output appears in the buffer `*Async Shell Command*'.
1930 That buffer is in shell mode.
1932 Otherwise, COMMAND is executed synchronously. The output appears in
1933 the buffer `*Shell Command Output*'. If the output is short enough to
1934 display in the echo area (which is determined by the variables
1935 `resize-mini-windows' and `max-mini-window-height'), it is shown
1936 there, but it is nonetheless available in buffer `*Shell Command
1937 Output*' even though that buffer is not automatically displayed.
1939 To specify a coding system for converting non-ASCII characters
1940 in the shell command output, use \\[universal-coding-system-argument]
1941 before this command.
1943 Noninteractive callers can specify coding systems by binding
1944 `coding-system-for-read' and `coding-system-for-write'.
1946 The optional second argument OUTPUT-BUFFER, if non-nil,
1947 says to put the output in some other buffer.
1948 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1949 If OUTPUT-BUFFER is not a buffer and not nil,
1950 insert output in current buffer. (This cannot be done asynchronously.)
1951 In either case, the output is inserted after point (leaving mark after it).
1953 If the command terminates without error, but generates output,
1954 and you did not specify \"insert it in the current buffer\",
1955 the output can be displayed in the echo area or in its buffer.
1956 If the output is short enough to display in the echo area
1957 \(determined by the variable `max-mini-window-height' if
1958 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1959 the buffer containing the output is displayed.
1961 If there is output and an error, and you did not specify \"insert it
1962 in the current buffer\", a message about the error goes at the end
1963 of the output.
1965 If there is no output, or if output is inserted in the current buffer,
1966 then `*Shell Command Output*' is deleted.
1968 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1969 or buffer name to which to direct the command's standard error output.
1970 If it is nil, error output is mingled with regular output.
1971 In an interactive call, the variable `shell-command-default-error-buffer'
1972 specifies the value of ERROR-BUFFER."
1974 (interactive (list (read-from-minibuffer "Shell command: "
1975 nil nil nil 'shell-command-history)
1976 current-prefix-arg
1977 shell-command-default-error-buffer))
1978 ;; Look for a handler in case default-directory is a remote file name.
1979 (let ((handler
1980 (find-file-name-handler (directory-file-name default-directory)
1981 'shell-command)))
1982 (if handler
1983 (funcall handler 'shell-command command output-buffer error-buffer)
1984 (if (and output-buffer
1985 (not (or (bufferp output-buffer) (stringp output-buffer))))
1986 ;; Output goes in current buffer.
1987 (let ((error-file
1988 (if error-buffer
1989 (make-temp-file
1990 (expand-file-name "scor"
1991 (or small-temporary-file-directory
1992 temporary-file-directory)))
1993 nil)))
1994 (barf-if-buffer-read-only)
1995 (push-mark nil t)
1996 ;; We do not use -f for csh; we will not support broken use of
1997 ;; .cshrcs. Even the BSD csh manual says to use
1998 ;; "if ($?prompt) exit" before things which are not useful
1999 ;; non-interactively. Besides, if someone wants their other
2000 ;; aliases for shell commands then they can still have them.
2001 (call-process shell-file-name nil
2002 (if error-file
2003 (list t error-file)
2005 nil shell-command-switch command)
2006 (when (and error-file (file-exists-p error-file))
2007 (if (< 0 (nth 7 (file-attributes error-file)))
2008 (with-current-buffer (get-buffer-create error-buffer)
2009 (let ((pos-from-end (- (point-max) (point))))
2010 (or (bobp)
2011 (insert "\f\n"))
2012 ;; Do no formatting while reading error file,
2013 ;; because that can run a shell command, and we
2014 ;; don't want that to cause an infinite recursion.
2015 (format-insert-file error-file nil)
2016 ;; Put point after the inserted errors.
2017 (goto-char (- (point-max) pos-from-end)))
2018 (display-buffer (current-buffer))))
2019 (delete-file error-file))
2020 ;; This is like exchange-point-and-mark, but doesn't
2021 ;; activate the mark. It is cleaner to avoid activation,
2022 ;; even though the command loop would deactivate the mark
2023 ;; because we inserted text.
2024 (goto-char (prog1 (mark t)
2025 (set-marker (mark-marker) (point)
2026 (current-buffer)))))
2027 ;; Output goes in a separate buffer.
2028 ;; Preserve the match data in case called from a program.
2029 (save-match-data
2030 (if (string-match "[ \t]*&[ \t]*\\'" command)
2031 ;; Command ending with ampersand means asynchronous.
2032 (let ((buffer (get-buffer-create
2033 (or output-buffer "*Async Shell Command*")))
2034 (directory default-directory)
2035 proc)
2036 ;; Remove the ampersand.
2037 (setq command (substring command 0 (match-beginning 0)))
2038 ;; If will kill a process, query first.
2039 (setq proc (get-buffer-process buffer))
2040 (if proc
2041 (if (yes-or-no-p "A command is running. Kill it? ")
2042 (kill-process proc)
2043 (error "Shell command in progress")))
2044 (with-current-buffer buffer
2045 (setq buffer-read-only nil)
2046 (erase-buffer)
2047 (display-buffer buffer)
2048 (setq default-directory directory)
2049 (setq proc (start-process "Shell" buffer shell-file-name
2050 shell-command-switch command))
2051 (setq mode-line-process '(":%s"))
2052 (require 'shell) (shell-mode)
2053 (set-process-sentinel proc 'shell-command-sentinel)
2055 (shell-command-on-region (point) (point) command
2056 output-buffer nil error-buffer)))))))
2058 (defun display-message-or-buffer (message
2059 &optional buffer-name not-this-window frame)
2060 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
2061 MESSAGE may be either a string or a buffer.
2063 A buffer is displayed using `display-buffer' if MESSAGE is too long for
2064 the maximum height of the echo area, as defined by `max-mini-window-height'
2065 if `resize-mini-windows' is non-nil.
2067 Returns either the string shown in the echo area, or when a pop-up
2068 buffer is used, the window used to display it.
2070 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
2071 name of the buffer used to display it in the case where a pop-up buffer
2072 is used, defaulting to `*Message*'. In the case where MESSAGE is a
2073 string and it is displayed in the echo area, it is not specified whether
2074 the contents are inserted into the buffer anyway.
2076 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
2077 and only used if a buffer is displayed."
2078 (cond ((and (stringp message) (not (string-match "\n" message)))
2079 ;; Trivial case where we can use the echo area
2080 (message "%s" message))
2081 ((and (stringp message)
2082 (= (string-match "\n" message) (1- (length message))))
2083 ;; Trivial case where we can just remove single trailing newline
2084 (message "%s" (substring message 0 (1- (length message)))))
2086 ;; General case
2087 (with-current-buffer
2088 (if (bufferp message)
2089 message
2090 (get-buffer-create (or buffer-name "*Message*")))
2092 (unless (bufferp message)
2093 (erase-buffer)
2094 (insert message))
2096 (let ((lines
2097 (if (= (buffer-size) 0)
2099 (count-screen-lines nil nil nil (minibuffer-window)))))
2100 (cond ((= lines 0))
2101 ((and (or (<= lines 1)
2102 (<= lines
2103 (if resize-mini-windows
2104 (cond ((floatp max-mini-window-height)
2105 (* (frame-height)
2106 max-mini-window-height))
2107 ((integerp max-mini-window-height)
2108 max-mini-window-height)
2111 1)))
2112 ;; Don't use the echo area if the output buffer is
2113 ;; already dispayed in the selected frame.
2114 (not (get-buffer-window (current-buffer))))
2115 ;; Echo area
2116 (goto-char (point-max))
2117 (when (bolp)
2118 (backward-char 1))
2119 (message "%s" (buffer-substring (point-min) (point))))
2121 ;; Buffer
2122 (goto-char (point-min))
2123 (display-buffer (current-buffer)
2124 not-this-window frame))))))))
2127 ;; We have a sentinel to prevent insertion of a termination message
2128 ;; in the buffer itself.
2129 (defun shell-command-sentinel (process signal)
2130 (if (memq (process-status process) '(exit signal))
2131 (message "%s: %s."
2132 (car (cdr (cdr (process-command process))))
2133 (substring signal 0 -1))))
2135 (defun shell-command-on-region (start end command
2136 &optional output-buffer replace
2137 error-buffer display-error-buffer)
2138 "Execute string COMMAND in inferior shell with region as input.
2139 Normally display output (if any) in temp buffer `*Shell Command Output*';
2140 Prefix arg means replace the region with it. Return the exit code of
2141 COMMAND.
2143 To specify a coding system for converting non-ASCII characters
2144 in the input and output to the shell command, use \\[universal-coding-system-argument]
2145 before this command. By default, the input (from the current buffer)
2146 is encoded in the same coding system that will be used to save the file,
2147 `buffer-file-coding-system'. If the output is going to replace the region,
2148 then it is decoded from that same coding system.
2150 The noninteractive arguments are START, END, COMMAND,
2151 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
2152 Noninteractive callers can specify coding systems by binding
2153 `coding-system-for-read' and `coding-system-for-write'.
2155 If the command generates output, the output may be displayed
2156 in the echo area or in a buffer.
2157 If the output is short enough to display in the echo area
2158 \(determined by the variable `max-mini-window-height' if
2159 `resize-mini-windows' is non-nil), it is shown there. Otherwise
2160 it is displayed in the buffer `*Shell Command Output*'. The output
2161 is available in that buffer in both cases.
2163 If there is output and an error, a message about the error
2164 appears at the end of the output.
2166 If there is no output, or if output is inserted in the current buffer,
2167 then `*Shell Command Output*' is deleted.
2169 If the optional fourth argument OUTPUT-BUFFER is non-nil,
2170 that says to put the output in some other buffer.
2171 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
2172 If OUTPUT-BUFFER is not a buffer and not nil,
2173 insert output in the current buffer.
2174 In either case, the output is inserted after point (leaving mark after it).
2176 If REPLACE, the optional fifth argument, is non-nil, that means insert
2177 the output in place of text from START to END, putting point and mark
2178 around it.
2180 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
2181 or buffer name to which to direct the command's standard error output.
2182 If it is nil, error output is mingled with regular output.
2183 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
2184 were any errors. (This is always t, interactively.)
2185 In an interactive call, the variable `shell-command-default-error-buffer'
2186 specifies the value of ERROR-BUFFER."
2187 (interactive (let (string)
2188 (unless (mark)
2189 (error "The mark is not set now, so there is no region"))
2190 ;; Do this before calling region-beginning
2191 ;; and region-end, in case subprocess output
2192 ;; relocates them while we are in the minibuffer.
2193 (setq string (read-from-minibuffer "Shell command on region: "
2194 nil nil nil
2195 'shell-command-history))
2196 ;; call-interactively recognizes region-beginning and
2197 ;; region-end specially, leaving them in the history.
2198 (list (region-beginning) (region-end)
2199 string
2200 current-prefix-arg
2201 current-prefix-arg
2202 shell-command-default-error-buffer
2203 t)))
2204 (let ((error-file
2205 (if error-buffer
2206 (make-temp-file
2207 (expand-file-name "scor"
2208 (or small-temporary-file-directory
2209 temporary-file-directory)))
2210 nil))
2211 exit-status)
2212 (if (or replace
2213 (and output-buffer
2214 (not (or (bufferp output-buffer) (stringp output-buffer)))))
2215 ;; Replace specified region with output from command.
2216 (let ((swap (and replace (< start end))))
2217 ;; Don't muck with mark unless REPLACE says we should.
2218 (goto-char start)
2219 (and replace (push-mark (point) 'nomsg))
2220 (setq exit-status
2221 (call-process-region start end shell-file-name t
2222 (if error-file
2223 (list t error-file)
2225 nil shell-command-switch command))
2226 ;; It is rude to delete a buffer which the command is not using.
2227 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
2228 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
2229 ;; (kill-buffer shell-buffer)))
2230 ;; Don't muck with mark unless REPLACE says we should.
2231 (and replace swap (exchange-point-and-mark)))
2232 ;; No prefix argument: put the output in a temp buffer,
2233 ;; replacing its entire contents.
2234 (let ((buffer (get-buffer-create
2235 (or output-buffer "*Shell Command Output*"))))
2236 (unwind-protect
2237 (if (eq buffer (current-buffer))
2238 ;; If the input is the same buffer as the output,
2239 ;; delete everything but the specified region,
2240 ;; then replace that region with the output.
2241 (progn (setq buffer-read-only nil)
2242 (delete-region (max start end) (point-max))
2243 (delete-region (point-min) (min start end))
2244 (setq exit-status
2245 (call-process-region (point-min) (point-max)
2246 shell-file-name t
2247 (if error-file
2248 (list t error-file)
2250 nil shell-command-switch
2251 command)))
2252 ;; Clear the output buffer, then run the command with
2253 ;; output there.
2254 (let ((directory default-directory))
2255 (save-excursion
2256 (set-buffer buffer)
2257 (setq buffer-read-only nil)
2258 (if (not output-buffer)
2259 (setq default-directory directory))
2260 (erase-buffer)))
2261 (setq exit-status
2262 (call-process-region start end shell-file-name nil
2263 (if error-file
2264 (list buffer error-file)
2265 buffer)
2266 nil shell-command-switch command)))
2267 ;; Report the output.
2268 (with-current-buffer buffer
2269 (setq mode-line-process
2270 (cond ((null exit-status)
2271 " - Error")
2272 ((stringp exit-status)
2273 (format " - Signal [%s]" exit-status))
2274 ((not (equal 0 exit-status))
2275 (format " - Exit [%d]" exit-status)))))
2276 (if (with-current-buffer buffer (> (point-max) (point-min)))
2277 ;; There's some output, display it
2278 (display-message-or-buffer buffer)
2279 ;; No output; error?
2280 (let ((output
2281 (if (and error-file
2282 (< 0 (nth 7 (file-attributes error-file))))
2283 "some error output"
2284 "no output")))
2285 (cond ((null exit-status)
2286 (message "(Shell command failed with error)"))
2287 ((equal 0 exit-status)
2288 (message "(Shell command succeeded with %s)"
2289 output))
2290 ((stringp exit-status)
2291 (message "(Shell command killed by signal %s)"
2292 exit-status))
2294 (message "(Shell command failed with code %d and %s)"
2295 exit-status output))))
2296 ;; Don't kill: there might be useful info in the undo-log.
2297 ;; (kill-buffer buffer)
2298 ))))
2300 (when (and error-file (file-exists-p error-file))
2301 (if (< 0 (nth 7 (file-attributes error-file)))
2302 (with-current-buffer (get-buffer-create error-buffer)
2303 (let ((pos-from-end (- (point-max) (point))))
2304 (or (bobp)
2305 (insert "\f\n"))
2306 ;; Do no formatting while reading error file,
2307 ;; because that can run a shell command, and we
2308 ;; don't want that to cause an infinite recursion.
2309 (format-insert-file error-file nil)
2310 ;; Put point after the inserted errors.
2311 (goto-char (- (point-max) pos-from-end)))
2312 (and display-error-buffer
2313 (display-buffer (current-buffer)))))
2314 (delete-file error-file))
2315 exit-status))
2317 (defun shell-command-to-string (command)
2318 "Execute shell command COMMAND and return its output as a string."
2319 (with-output-to-string
2320 (with-current-buffer
2321 standard-output
2322 (call-process shell-file-name nil t nil shell-command-switch command))))
2324 (defun process-file (program &optional infile buffer display &rest args)
2325 "Process files synchronously in a separate process.
2326 Similar to `call-process', but may invoke a file handler based on
2327 `default-directory'. The current working directory of the
2328 subprocess is `default-directory'.
2330 File names in INFILE and BUFFER are handled normally, but file
2331 names in ARGS should be relative to `default-directory', as they
2332 are passed to the process verbatim. \(This is a difference to
2333 `call-process' which does not support file handlers for INFILE
2334 and BUFFER.\)
2336 Some file handlers might not support all variants, for example
2337 they might behave as if DISPLAY was nil, regardless of the actual
2338 value passed."
2339 (let ((fh (find-file-name-handler default-directory 'process-file))
2340 lc stderr-file)
2341 (unwind-protect
2342 (if fh (apply fh 'process-file program infile buffer display args)
2343 (when infile (setq lc (file-local-copy infile)))
2344 (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer)))
2345 (make-temp-file "emacs")))
2346 (prog1
2347 (apply 'call-process program
2348 (or lc infile)
2349 (if stderr-file (list (car buffer) stderr-file) buffer)
2350 display args)
2351 (when stderr-file (copy-file stderr-file (cadr buffer)))))
2352 (when stderr-file (delete-file stderr-file))
2353 (when lc (delete-file lc)))))
2355 (defun start-file-process (name buffer program &rest program-args)
2356 "Start a program in a subprocess. Return the process object for it.
2357 Similar to `start-process', but may invoke a file handler based on
2358 `default-directory'. The current working directory of the
2359 subprocess is `default-directory'.
2361 PROGRAM and PROGRAM-ARGS might be file names. They are not
2362 objects of file handler invocation."
2363 (let ((fh (find-file-name-handler default-directory 'start-file-process)))
2364 (if fh (apply fh 'start-file-process name buffer program program-args)
2365 (apply 'start-process name buffer program program-args))))
2369 (defvar universal-argument-map
2370 (let ((map (make-sparse-keymap)))
2371 (define-key map [t] 'universal-argument-other-key)
2372 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
2373 (define-key map [switch-frame] nil)
2374 (define-key map [?\C-u] 'universal-argument-more)
2375 (define-key map [?-] 'universal-argument-minus)
2376 (define-key map [?0] 'digit-argument)
2377 (define-key map [?1] 'digit-argument)
2378 (define-key map [?2] 'digit-argument)
2379 (define-key map [?3] 'digit-argument)
2380 (define-key map [?4] 'digit-argument)
2381 (define-key map [?5] 'digit-argument)
2382 (define-key map [?6] 'digit-argument)
2383 (define-key map [?7] 'digit-argument)
2384 (define-key map [?8] 'digit-argument)
2385 (define-key map [?9] 'digit-argument)
2386 (define-key map [kp-0] 'digit-argument)
2387 (define-key map [kp-1] 'digit-argument)
2388 (define-key map [kp-2] 'digit-argument)
2389 (define-key map [kp-3] 'digit-argument)
2390 (define-key map [kp-4] 'digit-argument)
2391 (define-key map [kp-5] 'digit-argument)
2392 (define-key map [kp-6] 'digit-argument)
2393 (define-key map [kp-7] 'digit-argument)
2394 (define-key map [kp-8] 'digit-argument)
2395 (define-key map [kp-9] 'digit-argument)
2396 (define-key map [kp-subtract] 'universal-argument-minus)
2397 map)
2398 "Keymap used while processing \\[universal-argument].")
2400 (defvar universal-argument-num-events nil
2401 "Number of argument-specifying events read by `universal-argument'.
2402 `universal-argument-other-key' uses this to discard those events
2403 from (this-command-keys), and reread only the final command.")
2405 (defvar overriding-map-is-bound nil
2406 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
2408 (defvar saved-overriding-map nil
2409 "The saved value of `overriding-terminal-local-map'.
2410 That variable gets restored to this value on exiting \"universal
2411 argument mode\".")
2413 (defun ensure-overriding-map-is-bound ()
2414 "Check `overriding-terminal-local-map' is `universal-argument-map'."
2415 (unless overriding-map-is-bound
2416 (setq saved-overriding-map overriding-terminal-local-map)
2417 (setq overriding-terminal-local-map universal-argument-map)
2418 (setq overriding-map-is-bound t)))
2420 (defun restore-overriding-map ()
2421 "Restore `overriding-terminal-local-map' to its saved value."
2422 (setq overriding-terminal-local-map saved-overriding-map)
2423 (setq overriding-map-is-bound nil))
2425 (defun universal-argument ()
2426 "Begin a numeric argument for the following command.
2427 Digits or minus sign following \\[universal-argument] make up the numeric argument.
2428 \\[universal-argument] following the digits or minus sign ends the argument.
2429 \\[universal-argument] without digits or minus sign provides 4 as argument.
2430 Repeating \\[universal-argument] without digits or minus sign
2431 multiplies the argument by 4 each time.
2432 For some commands, just \\[universal-argument] by itself serves as a flag
2433 which is different in effect from any particular numeric argument.
2434 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
2435 (interactive)
2436 (setq prefix-arg (list 4))
2437 (setq universal-argument-num-events (length (this-command-keys)))
2438 (ensure-overriding-map-is-bound))
2440 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
2441 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
2442 (defun universal-argument-more (arg)
2443 (interactive "P")
2444 (if (consp arg)
2445 (setq prefix-arg (list (* 4 (car arg))))
2446 (if (eq arg '-)
2447 (setq prefix-arg (list -4))
2448 (setq prefix-arg arg)
2449 (restore-overriding-map)))
2450 (setq universal-argument-num-events (length (this-command-keys))))
2452 (defun negative-argument (arg)
2453 "Begin a negative numeric argument for the next command.
2454 \\[universal-argument] following digits or minus sign ends the argument."
2455 (interactive "P")
2456 (cond ((integerp arg)
2457 (setq prefix-arg (- arg)))
2458 ((eq arg '-)
2459 (setq prefix-arg nil))
2461 (setq prefix-arg '-)))
2462 (setq universal-argument-num-events (length (this-command-keys)))
2463 (ensure-overriding-map-is-bound))
2465 (defun digit-argument (arg)
2466 "Part of the numeric argument for the next command.
2467 \\[universal-argument] following digits or minus sign ends the argument."
2468 (interactive "P")
2469 (let* ((char (if (integerp last-command-char)
2470 last-command-char
2471 (get last-command-char 'ascii-character)))
2472 (digit (- (logand char ?\177) ?0)))
2473 (cond ((integerp arg)
2474 (setq prefix-arg (+ (* arg 10)
2475 (if (< arg 0) (- digit) digit))))
2476 ((eq arg '-)
2477 ;; Treat -0 as just -, so that -01 will work.
2478 (setq prefix-arg (if (zerop digit) '- (- digit))))
2480 (setq prefix-arg digit))))
2481 (setq universal-argument-num-events (length (this-command-keys)))
2482 (ensure-overriding-map-is-bound))
2484 ;; For backward compatibility, minus with no modifiers is an ordinary
2485 ;; command if digits have already been entered.
2486 (defun universal-argument-minus (arg)
2487 (interactive "P")
2488 (if (integerp arg)
2489 (universal-argument-other-key arg)
2490 (negative-argument arg)))
2492 ;; Anything else terminates the argument and is left in the queue to be
2493 ;; executed as a command.
2494 (defun universal-argument-other-key (arg)
2495 (interactive "P")
2496 (setq prefix-arg arg)
2497 (let* ((key (this-command-keys))
2498 (keylist (listify-key-sequence key)))
2499 (setq unread-command-events
2500 (append (nthcdr universal-argument-num-events keylist)
2501 unread-command-events)))
2502 (reset-this-command-lengths)
2503 (restore-overriding-map))
2505 (defvar buffer-substring-filters nil
2506 "List of filter functions for `filter-buffer-substring'.
2507 Each function must accept a single argument, a string, and return
2508 a string. The buffer substring is passed to the first function
2509 in the list, and the return value of each function is passed to
2510 the next. The return value of the last function is used as the
2511 return value of `filter-buffer-substring'.
2513 If this variable is nil, no filtering is performed.")
2515 (defun filter-buffer-substring (beg end &optional delete noprops)
2516 "Return the buffer substring between BEG and END, after filtering.
2517 The buffer substring is passed through each of the filter
2518 functions in `buffer-substring-filters', and the value from the
2519 last filter function is returned. If `buffer-substring-filters'
2520 is nil, the buffer substring is returned unaltered.
2522 If DELETE is non-nil, the text between BEG and END is deleted
2523 from the buffer.
2525 If NOPROPS is non-nil, final string returned does not include
2526 text properties, while the string passed to the filters still
2527 includes text properties from the buffer text.
2529 Point is temporarily set to BEG before calling
2530 `buffer-substring-filters', in case the functions need to know
2531 where the text came from.
2533 This function should be used instead of `buffer-substring',
2534 `buffer-substring-no-properties', or `delete-and-extract-region'
2535 when you want to allow filtering to take place. For example,
2536 major or minor modes can use `buffer-substring-filters' to
2537 extract characters that are special to a buffer, and should not
2538 be copied into other buffers."
2539 (cond
2540 ((or delete buffer-substring-filters)
2541 (save-excursion
2542 (goto-char beg)
2543 (let ((string (if delete (delete-and-extract-region beg end)
2544 (buffer-substring beg end))))
2545 (dolist (filter buffer-substring-filters)
2546 (setq string (funcall filter string)))
2547 (if noprops
2548 (set-text-properties 0 (length string) nil string))
2549 string)))
2550 (noprops
2551 (buffer-substring-no-properties beg end))
2553 (buffer-substring beg end))))
2556 ;;;; Window system cut and paste hooks.
2558 (defvar interprogram-cut-function nil
2559 "Function to call to make a killed region available to other programs.
2561 Most window systems provide some sort of facility for cutting and
2562 pasting text between the windows of different programs.
2563 This variable holds a function that Emacs calls whenever text
2564 is put in the kill ring, to make the new kill available to other
2565 programs.
2567 The function takes one or two arguments.
2568 The first argument, TEXT, is a string containing
2569 the text which should be made available.
2570 The second, optional, argument PUSH, has the same meaning as the
2571 similar argument to `x-set-cut-buffer', which see.")
2573 (defvar interprogram-paste-function nil
2574 "Function to call to get text cut from other programs.
2576 Most window systems provide some sort of facility for cutting and
2577 pasting text between the windows of different programs.
2578 This variable holds a function that Emacs calls to obtain
2579 text that other programs have provided for pasting.
2581 The function should be called with no arguments. If the function
2582 returns nil, then no other program has provided such text, and the top
2583 of the Emacs kill ring should be used. If the function returns a
2584 string, then the caller of the function \(usually `current-kill')
2585 should put this string in the kill ring as the latest kill.
2587 This function may also return a list of strings if the window
2588 system supports multiple selections. The first string will be
2589 used as the pasted text, but the other will be placed in the
2590 kill ring for easy access via `yank-pop'.
2592 Note that the function should return a string only if a program other
2593 than Emacs has provided a string for pasting; if Emacs provided the
2594 most recent string, the function should return nil. If it is
2595 difficult to tell whether Emacs or some other program provided the
2596 current string, it is probably good enough to return nil if the string
2597 is equal (according to `string=') to the last text Emacs provided.")
2601 ;;;; The kill ring data structure.
2603 (defvar kill-ring nil
2604 "List of killed text sequences.
2605 Since the kill ring is supposed to interact nicely with cut-and-paste
2606 facilities offered by window systems, use of this variable should
2607 interact nicely with `interprogram-cut-function' and
2608 `interprogram-paste-function'. The functions `kill-new',
2609 `kill-append', and `current-kill' are supposed to implement this
2610 interaction; you may want to use them instead of manipulating the kill
2611 ring directly.")
2613 (defcustom kill-ring-max 60
2614 "*Maximum length of kill ring before oldest elements are thrown away."
2615 :type 'integer
2616 :group 'killing)
2618 (defvar kill-ring-yank-pointer nil
2619 "The tail of the kill ring whose car is the last thing yanked.")
2621 (defun kill-new (string &optional replace yank-handler)
2622 "Make STRING the latest kill in the kill ring.
2623 Set `kill-ring-yank-pointer' to point to it.
2624 If `interprogram-cut-function' is non-nil, apply it to STRING.
2625 Optional second argument REPLACE non-nil means that STRING will replace
2626 the front of the kill ring, rather than being added to the list.
2628 Optional third arguments YANK-HANDLER controls how the STRING is later
2629 inserted into a buffer; see `insert-for-yank' for details.
2630 When a yank handler is specified, STRING must be non-empty (the yank
2631 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2633 When the yank handler has a non-nil PARAM element, the original STRING
2634 argument is not used by `insert-for-yank'. However, since Lisp code
2635 may access and use elements from the kill ring directly, the STRING
2636 argument should still be a \"useful\" string for such uses."
2637 (if (> (length string) 0)
2638 (if yank-handler
2639 (put-text-property 0 (length string)
2640 'yank-handler yank-handler string))
2641 (if yank-handler
2642 (signal 'args-out-of-range
2643 (list string "yank-handler specified for empty string"))))
2644 (if (fboundp 'menu-bar-update-yank-menu)
2645 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2646 (if (and replace kill-ring)
2647 (setcar kill-ring string)
2648 (push string kill-ring)
2649 (if (> (length kill-ring) kill-ring-max)
2650 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2651 (setq kill-ring-yank-pointer kill-ring)
2652 (if interprogram-cut-function
2653 (funcall interprogram-cut-function string (not replace))))
2655 (defun kill-append (string before-p &optional yank-handler)
2656 "Append STRING to the end of the latest kill in the kill ring.
2657 If BEFORE-P is non-nil, prepend STRING to the kill.
2658 Optional third argument YANK-HANDLER, if non-nil, specifies the
2659 yank-handler text property to be set on the combined kill ring
2660 string. If the specified yank-handler arg differs from the
2661 yank-handler property of the latest kill string, this function
2662 adds the combined string to the kill ring as a new element,
2663 instead of replacing the last kill with it.
2664 If `interprogram-cut-function' is set, pass the resulting kill to it."
2665 (let* ((cur (car kill-ring)))
2666 (kill-new (if before-p (concat string cur) (concat cur string))
2667 (or (= (length cur) 0)
2668 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2669 yank-handler)))
2671 (defcustom yank-pop-change-selection nil
2672 "If non-nil, rotating the kill ring changes the window system selection."
2673 :type 'boolean
2674 :group 'killing
2675 :version "23.1")
2677 (defun current-kill (n &optional do-not-move)
2678 "Rotate the yanking point by N places, and then return that kill.
2679 If N is zero, `interprogram-paste-function' is set, and calling it returns a
2680 string or list of strings, then that string (or list) is added to the front
2681 of the kill ring and the string (or first string in the list) is returned as
2682 the latest kill.
2684 If N is not zero, and if `yank-pop-change-selection' is
2685 non-nil, use `interprogram-cut-function' to transfer the
2686 kill at the new yank point into the window system selection.
2688 If optional arg DO-NOT-MOVE is non-nil, then don't actually
2689 move the yanking point; just return the Nth kill forward."
2691 (let ((interprogram-paste (and (= n 0)
2692 interprogram-paste-function
2693 (funcall interprogram-paste-function))))
2694 (if interprogram-paste
2695 (progn
2696 ;; Disable the interprogram cut function when we add the new
2697 ;; text to the kill ring, so Emacs doesn't try to own the
2698 ;; selection, with identical text.
2699 (let ((interprogram-cut-function nil))
2700 (if (listp interprogram-paste)
2701 (mapc 'kill-new (nreverse interprogram-paste))
2702 (kill-new interprogram-paste)))
2703 (car kill-ring))
2704 (or kill-ring (error "Kill ring is empty"))
2705 (let ((ARGth-kill-element
2706 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2707 (length kill-ring))
2708 kill-ring)))
2709 (unless do-not-move
2710 (setq kill-ring-yank-pointer ARGth-kill-element)
2711 (when (and yank-pop-change-selection
2712 (> n 0)
2713 interprogram-cut-function)
2714 (funcall interprogram-cut-function (car ARGth-kill-element))))
2715 (car ARGth-kill-element)))))
2719 ;;;; Commands for manipulating the kill ring.
2721 (defcustom kill-read-only-ok nil
2722 "*Non-nil means don't signal an error for killing read-only text."
2723 :type 'boolean
2724 :group 'killing)
2726 (put 'text-read-only 'error-conditions
2727 '(text-read-only buffer-read-only error))
2728 (put 'text-read-only 'error-message "Text is read-only")
2730 (defun kill-region (beg end &optional yank-handler)
2731 "Kill (\"cut\") text between point and mark.
2732 This deletes the text from the buffer and saves it in the kill ring.
2733 The command \\[yank] can retrieve it from there.
2734 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2736 If you want to append the killed region to the last killed text,
2737 use \\[append-next-kill] before \\[kill-region].
2739 If the buffer is read-only, Emacs will beep and refrain from deleting
2740 the text, but put the text in the kill ring anyway. This means that
2741 you can use the killing commands to copy text from a read-only buffer.
2743 This is the primitive for programs to kill text (as opposed to deleting it).
2744 Supply two arguments, character positions indicating the stretch of text
2745 to be killed.
2746 Any command that calls this function is a \"kill command\".
2747 If the previous command was also a kill command,
2748 the text killed this time appends to the text killed last time
2749 to make one entry in the kill ring.
2751 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2752 specifies the yank-handler text property to be set on the killed
2753 text. See `insert-for-yank'."
2754 ;; Pass point first, then mark, because the order matters
2755 ;; when calling kill-append.
2756 (interactive (list (point) (mark)))
2757 (unless (and beg end)
2758 (error "The mark is not set now, so there is no region"))
2759 (condition-case nil
2760 (let ((string (filter-buffer-substring beg end t)))
2761 (when string ;STRING is nil if BEG = END
2762 ;; Add that string to the kill ring, one way or another.
2763 (if (eq last-command 'kill-region)
2764 (kill-append string (< end beg) yank-handler)
2765 (kill-new string nil yank-handler)))
2766 (when (or string (eq last-command 'kill-region))
2767 (setq this-command 'kill-region))
2768 nil)
2769 ((buffer-read-only text-read-only)
2770 ;; The code above failed because the buffer, or some of the characters
2771 ;; in the region, are read-only.
2772 ;; We should beep, in case the user just isn't aware of this.
2773 ;; However, there's no harm in putting
2774 ;; the region's text in the kill ring, anyway.
2775 (copy-region-as-kill beg end)
2776 ;; Set this-command now, so it will be set even if we get an error.
2777 (setq this-command 'kill-region)
2778 ;; This should barf, if appropriate, and give us the correct error.
2779 (if kill-read-only-ok
2780 (progn (message "Read only text copied to kill ring") nil)
2781 ;; Signal an error if the buffer is read-only.
2782 (barf-if-buffer-read-only)
2783 ;; If the buffer isn't read-only, the text is.
2784 (signal 'text-read-only (list (current-buffer)))))))
2786 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2787 ;; to get two copies of the text when the user accidentally types M-w and
2788 ;; then corrects it with the intended C-w.
2789 (defun copy-region-as-kill (beg end)
2790 "Save the region as if killed, but don't kill it.
2791 In Transient Mark mode, deactivate the mark.
2792 If `interprogram-cut-function' is non-nil, also save the text for a window
2793 system cut and paste.
2795 This command's old key binding has been given to `kill-ring-save'."
2796 (interactive "r")
2797 (if (eq last-command 'kill-region)
2798 (kill-append (filter-buffer-substring beg end) (< end beg))
2799 (kill-new (filter-buffer-substring beg end)))
2800 (if transient-mark-mode
2801 (setq deactivate-mark t))
2802 nil)
2804 (defun kill-ring-save (beg end)
2805 "Save the region as if killed, but don't kill it.
2806 In Transient Mark mode, deactivate the mark.
2807 If `interprogram-cut-function' is non-nil, also save the text for a window
2808 system cut and paste.
2810 If you want to append the killed line to the last killed text,
2811 use \\[append-next-kill] before \\[kill-ring-save].
2813 This command is similar to `copy-region-as-kill', except that it gives
2814 visual feedback indicating the extent of the region being copied."
2815 (interactive "r")
2816 (copy-region-as-kill beg end)
2817 ;; This use of interactive-p is correct
2818 ;; because the code it controls just gives the user visual feedback.
2819 (if (interactive-p)
2820 (let ((other-end (if (= (point) beg) end beg))
2821 (opoint (point))
2822 ;; Inhibit quitting so we can make a quit here
2823 ;; look like a C-g typed as a command.
2824 (inhibit-quit t))
2825 (if (pos-visible-in-window-p other-end (selected-window))
2826 (unless (and transient-mark-mode
2827 (face-background 'region))
2828 ;; Swap point and mark.
2829 (set-marker (mark-marker) (point) (current-buffer))
2830 (goto-char other-end)
2831 (sit-for blink-matching-delay)
2832 ;; Swap back.
2833 (set-marker (mark-marker) other-end (current-buffer))
2834 (goto-char opoint)
2835 ;; If user quit, deactivate the mark
2836 ;; as C-g would as a command.
2837 (and quit-flag mark-active
2838 (deactivate-mark)))
2839 (let* ((killed-text (current-kill 0))
2840 (message-len (min (length killed-text) 40)))
2841 (if (= (point) beg)
2842 ;; Don't say "killed"; that is misleading.
2843 (message "Saved text until \"%s\""
2844 (substring killed-text (- message-len)))
2845 (message "Saved text from \"%s\""
2846 (substring killed-text 0 message-len))))))))
2848 (defun append-next-kill (&optional interactive)
2849 "Cause following command, if it kills, to append to previous kill.
2850 The argument is used for internal purposes; do not supply one."
2851 (interactive "p")
2852 ;; We don't use (interactive-p), since that breaks kbd macros.
2853 (if interactive
2854 (progn
2855 (setq this-command 'kill-region)
2856 (message "If the next command is a kill, it will append"))
2857 (setq last-command 'kill-region)))
2859 ;; Yanking.
2861 ;; This is actually used in subr.el but defcustom does not work there.
2862 (defcustom yank-excluded-properties
2863 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2864 yank-handler follow-link fontified)
2865 "Text properties to discard when yanking.
2866 The value should be a list of text properties to discard or t,
2867 which means to discard all text properties."
2868 :type '(choice (const :tag "All" t) (repeat symbol))
2869 :group 'killing
2870 :version "22.1")
2872 (defvar yank-window-start nil)
2873 (defvar yank-undo-function nil
2874 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2875 Function is called with two parameters, START and END corresponding to
2876 the value of the mark and point; it is guaranteed that START <= END.
2877 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2879 (defun yank-pop (&optional arg)
2880 "Replace just-yanked stretch of killed text with a different stretch.
2881 This command is allowed only immediately after a `yank' or a `yank-pop'.
2882 At such a time, the region contains a stretch of reinserted
2883 previously-killed text. `yank-pop' deletes that text and inserts in its
2884 place a different stretch of killed text.
2886 With no argument, the previous kill is inserted.
2887 With argument N, insert the Nth previous kill.
2888 If N is negative, this is a more recent kill.
2890 The sequence of kills wraps around, so that after the oldest one
2891 comes the newest one.
2893 When this command inserts killed text into the buffer, it honors
2894 `yank-excluded-properties' and `yank-handler' as described in the
2895 doc string for `insert-for-yank-1', which see."
2896 (interactive "*p")
2897 (if (not (eq last-command 'yank))
2898 (error "Previous command was not a yank"))
2899 (setq this-command 'yank)
2900 (unless arg (setq arg 1))
2901 (let ((inhibit-read-only t)
2902 (before (< (point) (mark t))))
2903 (if before
2904 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2905 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2906 (setq yank-undo-function nil)
2907 (set-marker (mark-marker) (point) (current-buffer))
2908 (insert-for-yank (current-kill arg))
2909 ;; Set the window start back where it was in the yank command,
2910 ;; if possible.
2911 (set-window-start (selected-window) yank-window-start t)
2912 (if before
2913 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2914 ;; It is cleaner to avoid activation, even though the command
2915 ;; loop would deactivate the mark because we inserted text.
2916 (goto-char (prog1 (mark t)
2917 (set-marker (mark-marker) (point) (current-buffer))))))
2918 nil)
2920 (defun yank (&optional arg)
2921 "Reinsert (\"paste\") the last stretch of killed text.
2922 More precisely, reinsert the stretch of killed text most recently
2923 killed OR yanked. Put point at end, and set mark at beginning.
2924 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2925 With argument N, reinsert the Nth most recently killed stretch of killed
2926 text.
2928 When this command inserts killed text into the buffer, it honors
2929 `yank-excluded-properties' and `yank-handler' as described in the
2930 doc string for `insert-for-yank-1', which see.
2932 See also the command `yank-pop' (\\[yank-pop])."
2933 (interactive "*P")
2934 (setq yank-window-start (window-start))
2935 ;; If we don't get all the way thru, make last-command indicate that
2936 ;; for the following command.
2937 (setq this-command t)
2938 (push-mark (point))
2939 (insert-for-yank (current-kill (cond
2940 ((listp arg) 0)
2941 ((eq arg '-) -2)
2942 (t (1- arg)))))
2943 (if (consp arg)
2944 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2945 ;; It is cleaner to avoid activation, even though the command
2946 ;; loop would deactivate the mark because we inserted text.
2947 (goto-char (prog1 (mark t)
2948 (set-marker (mark-marker) (point) (current-buffer)))))
2949 ;; If we do get all the way thru, make this-command indicate that.
2950 (if (eq this-command t)
2951 (setq this-command 'yank))
2952 nil)
2954 (defun rotate-yank-pointer (arg)
2955 "Rotate the yanking point in the kill ring.
2956 With argument, rotate that many kills forward (or backward, if negative)."
2957 (interactive "p")
2958 (current-kill arg))
2960 ;; Some kill commands.
2962 ;; Internal subroutine of delete-char
2963 (defun kill-forward-chars (arg)
2964 (if (listp arg) (setq arg (car arg)))
2965 (if (eq arg '-) (setq arg -1))
2966 (kill-region (point) (forward-point arg)))
2968 ;; Internal subroutine of backward-delete-char
2969 (defun kill-backward-chars (arg)
2970 (if (listp arg) (setq arg (car arg)))
2971 (if (eq arg '-) (setq arg -1))
2972 (kill-region (point) (forward-point (- arg))))
2974 (defcustom backward-delete-char-untabify-method 'untabify
2975 "*The method for untabifying when deleting backward.
2976 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2977 `hungry' -- delete all whitespace, both tabs and spaces;
2978 `all' -- delete all whitespace, including tabs, spaces and newlines;
2979 nil -- just delete one character."
2980 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2981 :version "20.3"
2982 :group 'killing)
2984 (defun backward-delete-char-untabify (arg &optional killp)
2985 "Delete characters backward, changing tabs into spaces.
2986 The exact behavior depends on `backward-delete-char-untabify-method'.
2987 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2988 Interactively, ARG is the prefix arg (default 1)
2989 and KILLP is t if a prefix arg was specified."
2990 (interactive "*p\nP")
2991 (when (eq backward-delete-char-untabify-method 'untabify)
2992 (let ((count arg))
2993 (save-excursion
2994 (while (and (> count 0) (not (bobp)))
2995 (if (= (preceding-char) ?\t)
2996 (let ((col (current-column)))
2997 (forward-char -1)
2998 (setq col (- col (current-column)))
2999 (insert-char ?\s col)
3000 (delete-char 1)))
3001 (forward-char -1)
3002 (setq count (1- count))))))
3003 (delete-backward-char
3004 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
3005 ((eq backward-delete-char-untabify-method 'all)
3006 " \t\n\r"))))
3007 (if skip
3008 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
3009 (point)))))
3010 (+ arg (if (zerop wh) 0 (1- wh))))
3011 arg))
3012 killp))
3014 (defun zap-to-char (arg char)
3015 "Kill up to and including ARG'th occurrence of CHAR.
3016 Case is ignored if `case-fold-search' is non-nil in the current buffer.
3017 Goes backward if ARG is negative; error if CHAR not found."
3018 (interactive "p\ncZap to char: ")
3019 (if (char-table-p translation-table-for-input)
3020 (setq char (or (aref translation-table-for-input char) char)))
3021 (kill-region (point) (progn
3022 (search-forward (char-to-string char) nil nil arg)
3023 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
3024 (point))))
3026 ;; kill-line and its subroutines.
3028 (defcustom kill-whole-line nil
3029 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
3030 :type 'boolean
3031 :group 'killing)
3033 (defun kill-line (&optional arg)
3034 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
3035 With prefix argument, kill that many lines from point.
3036 Negative arguments kill lines backward.
3037 With zero argument, kills the text before point on the current line.
3039 When calling from a program, nil means \"no arg\",
3040 a number counts as a prefix arg.
3042 To kill a whole line, when point is not at the beginning, type \
3043 \\[move-beginning-of-line] \\[kill-line] \\[kill-line].
3045 If `kill-whole-line' is non-nil, then this command kills the whole line
3046 including its terminating newline, when used at the beginning of a line
3047 with no argument. As a consequence, you can always kill a whole line
3048 by typing \\[move-beginning-of-line] \\[kill-line].
3050 If you want to append the killed line to the last killed text,
3051 use \\[append-next-kill] before \\[kill-line].
3053 If the buffer is read-only, Emacs will beep and refrain from deleting
3054 the line, but put the line in the kill ring anyway. This means that
3055 you can use this command to copy text from a read-only buffer.
3056 \(If the variable `kill-read-only-ok' is non-nil, then this won't
3057 even beep.)"
3058 (interactive "P")
3059 (kill-region (point)
3060 ;; It is better to move point to the other end of the kill
3061 ;; before killing. That way, in a read-only buffer, point
3062 ;; moves across the text that is copied to the kill ring.
3063 ;; The choice has no effect on undo now that undo records
3064 ;; the value of point from before the command was run.
3065 (progn
3066 (if arg
3067 (forward-visible-line (prefix-numeric-value arg))
3068 (if (eobp)
3069 (signal 'end-of-buffer nil))
3070 (let ((end
3071 (save-excursion
3072 (end-of-visible-line) (point))))
3073 (if (or (save-excursion
3074 ;; If trailing whitespace is visible,
3075 ;; don't treat it as nothing.
3076 (unless show-trailing-whitespace
3077 (skip-chars-forward " \t" end))
3078 (= (point) end))
3079 (and kill-whole-line (bolp)))
3080 (forward-visible-line 1)
3081 (goto-char end))))
3082 (point))))
3084 (defun kill-whole-line (&optional arg)
3085 "Kill current line.
3086 With prefix arg, kill that many lines starting from the current line.
3087 If arg is negative, kill backward. Also kill the preceding newline.
3088 \(This is meant to make \\[repeat] work well with negative arguments.\)
3089 If arg is zero, kill current line but exclude the trailing newline."
3090 (interactive "p")
3091 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
3092 (signal 'end-of-buffer nil))
3093 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
3094 (signal 'beginning-of-buffer nil))
3095 (unless (eq last-command 'kill-region)
3096 (kill-new "")
3097 (setq last-command 'kill-region))
3098 (cond ((zerop arg)
3099 ;; We need to kill in two steps, because the previous command
3100 ;; could have been a kill command, in which case the text
3101 ;; before point needs to be prepended to the current kill
3102 ;; ring entry and the text after point appended. Also, we
3103 ;; need to use save-excursion to avoid copying the same text
3104 ;; twice to the kill ring in read-only buffers.
3105 (save-excursion
3106 (kill-region (point) (progn (forward-visible-line 0) (point))))
3107 (kill-region (point) (progn (end-of-visible-line) (point))))
3108 ((< arg 0)
3109 (save-excursion
3110 (kill-region (point) (progn (end-of-visible-line) (point))))
3111 (kill-region (point)
3112 (progn (forward-visible-line (1+ arg))
3113 (unless (bobp) (backward-char))
3114 (point))))
3116 (save-excursion
3117 (kill-region (point) (progn (forward-visible-line 0) (point))))
3118 (kill-region (point)
3119 (progn (forward-visible-line arg) (point))))))
3121 (defun forward-visible-line (arg)
3122 "Move forward by ARG lines, ignoring currently invisible newlines only.
3123 If ARG is negative, move backward -ARG lines.
3124 If ARG is zero, move to the beginning of the current line."
3125 (condition-case nil
3126 (if (> arg 0)
3127 (progn
3128 (while (> arg 0)
3129 (or (zerop (forward-line 1))
3130 (signal 'end-of-buffer nil))
3131 ;; If the newline we just skipped is invisible,
3132 ;; don't count it.
3133 (let ((prop
3134 (get-char-property (1- (point)) 'invisible)))
3135 (if (if (eq buffer-invisibility-spec t)
3136 prop
3137 (or (memq prop buffer-invisibility-spec)
3138 (assq prop buffer-invisibility-spec)))
3139 (setq arg (1+ arg))))
3140 (setq arg (1- arg)))
3141 ;; If invisible text follows, and it is a number of complete lines,
3142 ;; skip it.
3143 (let ((opoint (point)))
3144 (while (and (not (eobp))
3145 (let ((prop
3146 (get-char-property (point) 'invisible)))
3147 (if (eq buffer-invisibility-spec t)
3148 prop
3149 (or (memq prop buffer-invisibility-spec)
3150 (assq prop buffer-invisibility-spec)))))
3151 (goto-char
3152 (if (get-text-property (point) 'invisible)
3153 (or (next-single-property-change (point) 'invisible)
3154 (point-max))
3155 (next-overlay-change (point)))))
3156 (unless (bolp)
3157 (goto-char opoint))))
3158 (let ((first t))
3159 (while (or first (<= arg 0))
3160 (if first
3161 (beginning-of-line)
3162 (or (zerop (forward-line -1))
3163 (signal 'beginning-of-buffer nil)))
3164 ;; If the newline we just moved to is invisible,
3165 ;; don't count it.
3166 (unless (bobp)
3167 (let ((prop
3168 (get-char-property (1- (point)) 'invisible)))
3169 (unless (if (eq buffer-invisibility-spec t)
3170 prop
3171 (or (memq prop buffer-invisibility-spec)
3172 (assq prop buffer-invisibility-spec)))
3173 (setq arg (1+ arg)))))
3174 (setq first nil))
3175 ;; If invisible text follows, and it is a number of complete lines,
3176 ;; skip it.
3177 (let ((opoint (point)))
3178 (while (and (not (bobp))
3179 (let ((prop
3180 (get-char-property (1- (point)) 'invisible)))
3181 (if (eq buffer-invisibility-spec t)
3182 prop
3183 (or (memq prop buffer-invisibility-spec)
3184 (assq prop buffer-invisibility-spec)))))
3185 (goto-char
3186 (if (get-text-property (1- (point)) 'invisible)
3187 (or (previous-single-property-change (point) 'invisible)
3188 (point-min))
3189 (previous-overlay-change (point)))))
3190 (unless (bolp)
3191 (goto-char opoint)))))
3192 ((beginning-of-buffer end-of-buffer)
3193 nil)))
3195 (defun end-of-visible-line ()
3196 "Move to end of current visible line."
3197 (end-of-line)
3198 ;; If the following character is currently invisible,
3199 ;; skip all characters with that same `invisible' property value,
3200 ;; then find the next newline.
3201 (while (and (not (eobp))
3202 (save-excursion
3203 (skip-chars-forward "^\n")
3204 (let ((prop
3205 (get-char-property (point) 'invisible)))
3206 (if (eq buffer-invisibility-spec t)
3207 prop
3208 (or (memq prop buffer-invisibility-spec)
3209 (assq prop buffer-invisibility-spec))))))
3210 (skip-chars-forward "^\n")
3211 (if (get-text-property (point) 'invisible)
3212 (goto-char (next-single-property-change (point) 'invisible))
3213 (goto-char (next-overlay-change (point))))
3214 (end-of-line)))
3216 (defun insert-buffer (buffer)
3217 "Insert after point the contents of BUFFER.
3218 Puts mark after the inserted text.
3219 BUFFER may be a buffer or a buffer name.
3221 This function is meant for the user to run interactively.
3222 Don't call it from programs: use `insert-buffer-substring' instead!"
3223 (interactive
3224 (list
3225 (progn
3226 (barf-if-buffer-read-only)
3227 (read-buffer "Insert buffer: "
3228 (if (eq (selected-window) (next-window (selected-window)))
3229 (other-buffer (current-buffer))
3230 (window-buffer (next-window (selected-window))))
3231 t))))
3232 (push-mark
3233 (save-excursion
3234 (insert-buffer-substring (get-buffer buffer))
3235 (point)))
3236 nil)
3238 (defun append-to-buffer (buffer start end)
3239 "Append to specified buffer the text of the region.
3240 It is inserted into that buffer before its point.
3242 When calling from a program, give three arguments:
3243 BUFFER (or buffer name), START and END.
3244 START and END specify the portion of the current buffer to be copied."
3245 (interactive
3246 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
3247 (region-beginning) (region-end)))
3248 (let ((oldbuf (current-buffer)))
3249 (save-excursion
3250 (let* ((append-to (get-buffer-create buffer))
3251 (windows (get-buffer-window-list append-to t t))
3252 point)
3253 (set-buffer append-to)
3254 (setq point (point))
3255 (barf-if-buffer-read-only)
3256 (insert-buffer-substring oldbuf start end)
3257 (dolist (window windows)
3258 (when (= (window-point window) point)
3259 (set-window-point window (point))))))))
3261 (defun prepend-to-buffer (buffer start end)
3262 "Prepend to specified buffer the text of the region.
3263 It is inserted into that buffer after its point.
3265 When calling from a program, give three arguments:
3266 BUFFER (or buffer name), START and END.
3267 START and END specify the portion of the current buffer to be copied."
3268 (interactive "BPrepend to buffer: \nr")
3269 (let ((oldbuf (current-buffer)))
3270 (save-excursion
3271 (set-buffer (get-buffer-create buffer))
3272 (barf-if-buffer-read-only)
3273 (save-excursion
3274 (insert-buffer-substring oldbuf start end)))))
3276 (defun copy-to-buffer (buffer start end)
3277 "Copy to specified buffer the text of the region.
3278 It is inserted into that buffer, replacing existing text there.
3280 When calling from a program, give three arguments:
3281 BUFFER (or buffer name), START and END.
3282 START and END specify the portion of the current buffer to be copied."
3283 (interactive "BCopy to buffer: \nr")
3284 (let ((oldbuf (current-buffer)))
3285 (with-current-buffer (get-buffer-create buffer)
3286 (barf-if-buffer-read-only)
3287 (erase-buffer)
3288 (save-excursion
3289 (insert-buffer-substring oldbuf start end)))))
3291 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3292 (put 'mark-inactive 'error-message "The mark is not active now")
3294 (defvar activate-mark-hook nil
3295 "Hook run when the mark becomes active.
3296 It is also run at the end of a command, if the mark is active and
3297 it is possible that the region may have changed.")
3299 (defvar deactivate-mark-hook nil
3300 "Hook run when the mark becomes inactive.")
3302 (defun mark (&optional force)
3303 "Return this buffer's mark value as integer, or nil if never set.
3305 In Transient Mark mode, this function signals an error if
3306 the mark is not active. However, if `mark-even-if-inactive' is non-nil,
3307 or the argument FORCE is non-nil, it disregards whether the mark
3308 is active, and returns an integer or nil in the usual way.
3310 If you are using this in an editing command, you are most likely making
3311 a mistake; see the documentation of `set-mark'."
3312 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3313 (marker-position (mark-marker))
3314 (signal 'mark-inactive nil)))
3316 ;; Many places set mark-active directly, and several of them failed to also
3317 ;; run deactivate-mark-hook. This shorthand should simplify.
3318 (defsubst deactivate-mark ()
3319 "Deactivate the mark by setting `mark-active' to nil.
3320 \(That makes a difference only in Transient Mark mode.)
3321 Also runs the hook `deactivate-mark-hook'."
3322 (cond
3323 ((eq transient-mark-mode 'lambda)
3324 (setq transient-mark-mode nil))
3325 (transient-mark-mode
3326 (setq mark-active nil)
3327 (run-hooks 'deactivate-mark-hook))))
3329 (defcustom select-active-regions nil
3330 "If non-nil, an active region automatically becomes the window selection."
3331 :type 'boolean
3332 :group 'killing
3333 :version "23.1")
3335 (defun set-mark (pos)
3336 "Set this buffer's mark to POS. Don't use this function!
3337 That is to say, don't use this function unless you want
3338 the user to see that the mark has moved, and you want the previous
3339 mark position to be lost.
3341 Normally, when a new mark is set, the old one should go on the stack.
3342 This is why most applications should use `push-mark', not `set-mark'.
3344 Novice Emacs Lisp programmers often try to use the mark for the wrong
3345 purposes. The mark saves a location for the user's convenience.
3346 Most editing commands should not alter the mark.
3347 To remember a location for internal use in the Lisp program,
3348 store it in a Lisp variable. Example:
3350 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3352 (if pos
3353 (progn
3354 (setq mark-active t)
3355 (run-hooks 'activate-mark-hook)
3356 (and select-active-regions
3357 (x-set-selection
3358 nil (buffer-substring (region-beginning) (region-end))))
3359 (set-marker (mark-marker) pos (current-buffer)))
3360 ;; Normally we never clear mark-active except in Transient Mark mode.
3361 ;; But when we actually clear out the mark value too,
3362 ;; we must clear mark-active in any mode.
3363 (setq mark-active nil)
3364 (run-hooks 'deactivate-mark-hook)
3365 (set-marker (mark-marker) nil)))
3367 (defcustom use-empty-active-region nil
3368 "If non-nil, an active region takes control even if empty.
3369 This applies to certain commands which, in Transient Mark mode,
3370 apply to the active region if there is one. If the setting is t,
3371 these commands apply to an empty active region if there is one.
3372 If the setting is nil, these commands treat an empty active
3373 region as if it were not active."
3374 :type 'boolean
3375 :version "23.1"
3376 :group 'editing-basics)
3378 (defun use-region-p ()
3379 "Return t if certain commands should apply to the region.
3380 Certain commands normally apply to text near point,
3381 but in Transient Mark mode when the mark is active they apply
3382 to the region instead. Such commands should use this subroutine to
3383 test whether to do that.
3385 This function also obeys `use-empty-active-region'."
3386 (and transient-mark-mode mark-active
3387 (or use-empty-active-region (> (region-end) (region-beginning)))))
3389 (defun region-active-p ()
3390 "Return t if Transient Mark mode is enabled and the mark is active.
3391 This is NOT the best function to use to test whether a command should
3392 operate on the region instead of the usual behavior -- for that,
3393 use `use-region-p'."
3394 (and transient-mark-mode mark-active))
3396 (defvar mark-ring nil
3397 "The list of former marks of the current buffer, most recent first.")
3398 (make-variable-buffer-local 'mark-ring)
3399 (put 'mark-ring 'permanent-local t)
3401 (defcustom mark-ring-max 16
3402 "*Maximum size of mark ring. Start discarding off end if gets this big."
3403 :type 'integer
3404 :group 'editing-basics)
3406 (defvar global-mark-ring nil
3407 "The list of saved global marks, most recent first.")
3409 (defcustom global-mark-ring-max 16
3410 "*Maximum size of global mark ring. \
3411 Start discarding off end if gets this big."
3412 :type 'integer
3413 :group 'editing-basics)
3415 (defun pop-to-mark-command ()
3416 "Jump to mark, and pop a new position for mark off the ring
3417 \(does not affect global mark ring\)."
3418 (interactive)
3419 (if (null (mark t))
3420 (error "No mark set in this buffer")
3421 (if (= (point) (mark t))
3422 (message "Mark popped"))
3423 (goto-char (mark t))
3424 (pop-mark)))
3426 (defun push-mark-command (arg &optional nomsg)
3427 "Set mark at where point is.
3428 If no prefix arg and mark is already set there, just activate it.
3429 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3430 (interactive "P")
3431 (let ((mark (marker-position (mark-marker))))
3432 (if (or arg (null mark) (/= mark (point)))
3433 (push-mark nil nomsg t)
3434 (setq mark-active t)
3435 (run-hooks 'activate-mark-hook)
3436 (unless nomsg
3437 (message "Mark activated")))))
3439 (defcustom set-mark-command-repeat-pop nil
3440 "*Non-nil means repeating \\[set-mark-command] after popping mark pops it again.
3441 That means that C-u \\[set-mark-command] \\[set-mark-command]
3442 will pop the mark twice, and
3443 C-u \\[set-mark-command] \\[set-mark-command] \\[set-mark-command]
3444 will pop the mark three times.
3446 A value of nil means \\[set-mark-command]'s behavior does not change
3447 after C-u \\[set-mark-command]."
3448 :type 'boolean
3449 :group 'editing-basics)
3451 (defun set-mark-command (arg)
3452 "Set the mark where point is, or jump to the mark.
3453 Setting the mark also alters the region, which is the text
3454 between point and mark; this is the closest equivalent in
3455 Emacs to what some editors call the \"selection\".
3457 With no prefix argument, set the mark at point, and push the
3458 old mark position on local mark ring. Also push the old mark on
3459 global mark ring, if the previous mark was set in another buffer.
3461 When Transient Mark Mode is off, immediately repeating this
3462 command activates `transient-mark-mode' temporarily.
3464 With prefix argument \(e.g., \\[universal-argument] \\[set-mark-command]\), \
3465 jump to the mark, and set the mark from
3466 position popped off the local mark ring \(this does not affect the global
3467 mark ring\). Use \\[pop-global-mark] to jump to a mark popped off the global
3468 mark ring \(see `pop-global-mark'\).
3470 If `set-mark-command-repeat-pop' is non-nil, repeating
3471 the \\[set-mark-command] command with no prefix argument pops the next position
3472 off the local (or global) mark ring and jumps there.
3474 With \\[universal-argument] \\[universal-argument] as prefix
3475 argument, unconditionally set mark where point is, even if
3476 `set-mark-command-repeat-pop' is non-nil.
3478 Novice Emacs Lisp programmers often try to use the mark for the wrong
3479 purposes. See the documentation of `set-mark' for more information."
3480 (interactive "P")
3481 (if (eq transient-mark-mode 'lambda)
3482 (setq transient-mark-mode nil))
3483 (cond
3484 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3485 (push-mark-command nil))
3486 ((not (eq this-command 'set-mark-command))
3487 (if arg
3488 (pop-to-mark-command)
3489 (push-mark-command t)))
3490 ((and set-mark-command-repeat-pop
3491 (eq last-command 'pop-to-mark-command))
3492 (setq this-command 'pop-to-mark-command)
3493 (pop-to-mark-command))
3494 ((and set-mark-command-repeat-pop
3495 (eq last-command 'pop-global-mark)
3496 (not arg))
3497 (setq this-command 'pop-global-mark)
3498 (pop-global-mark))
3499 (arg
3500 (setq this-command 'pop-to-mark-command)
3501 (pop-to-mark-command))
3502 ((and (eq last-command 'set-mark-command)
3503 mark-active (null transient-mark-mode))
3504 (setq transient-mark-mode 'lambda)
3505 (message "Transient-mark-mode temporarily enabled"))
3506 ((and (eq last-command 'set-mark-command)
3507 transient-mark-mode)
3508 (deactivate-mark))
3510 (push-mark-command nil))))
3512 (defun push-mark (&optional location nomsg activate)
3513 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3514 If the last global mark pushed was not in the current buffer,
3515 also push LOCATION on the global mark ring.
3516 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3518 Novice Emacs Lisp programmers often try to use the mark for the wrong
3519 purposes. See the documentation of `set-mark' for more information.
3521 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil."
3522 (unless (null (mark t))
3523 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3524 (when (> (length mark-ring) mark-ring-max)
3525 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3526 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3527 (set-marker (mark-marker) (or location (point)) (current-buffer))
3528 ;; Now push the mark on the global mark ring.
3529 (if (and global-mark-ring
3530 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3531 ;; The last global mark pushed was in this same buffer.
3532 ;; Don't push another one.
3534 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3535 (when (> (length global-mark-ring) global-mark-ring-max)
3536 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3537 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3538 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3539 (message "Mark set"))
3540 (if (or activate (not transient-mark-mode))
3541 (set-mark (mark t)))
3542 nil)
3544 (defun pop-mark ()
3545 "Pop off mark ring into the buffer's actual mark.
3546 Does not set point. Does nothing if mark ring is empty."
3547 (when mark-ring
3548 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3549 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3550 (move-marker (car mark-ring) nil)
3551 (if (null (mark t)) (ding))
3552 (setq mark-ring (cdr mark-ring)))
3553 (deactivate-mark))
3555 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3556 (defun exchange-point-and-mark (&optional arg)
3557 "Put the mark where point is now, and point where the mark is now.
3558 This command works even when the mark is not active,
3559 and it reactivates the mark.
3560 With prefix arg, `transient-mark-mode' is enabled temporarily."
3561 (interactive "P")
3562 (if arg
3563 (if mark-active
3564 (if (null transient-mark-mode)
3565 (setq transient-mark-mode 'lambda))
3566 (setq arg nil)))
3567 (unless arg
3568 (let ((omark (mark t)))
3569 (if (null omark)
3570 (error "No mark set in this buffer"))
3571 (set-mark (point))
3572 (goto-char omark)
3573 nil)))
3575 (define-minor-mode transient-mark-mode
3576 "Toggle Transient Mark mode.
3577 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3579 In Transient Mark mode, when the mark is active, the region is highlighted.
3580 Changing the buffer \"deactivates\" the mark.
3581 So do certain other operations that set the mark
3582 but whose main purpose is something else--for example,
3583 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3585 You can also deactivate the mark by typing \\[keyboard-quit] or
3586 \\[keyboard-escape-quit].
3588 Many commands change their behavior when Transient Mark mode is in effect
3589 and the mark is active, by acting on the region instead of their usual
3590 default part of the buffer's text. Examples of such commands include
3591 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3592 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3593 Invoke \\[apropos-documentation] and type \"transient\" or
3594 \"mark.*active\" at the prompt, to see the documentation of
3595 commands which are sensitive to the Transient Mark mode."
3596 :global t
3597 ;;; :init-value (not noninteractive)
3598 :group 'editing-basics)
3600 (defvar widen-automatically t
3601 "Non-nil means it is ok for commands to call `widen' when they want to.
3602 Some commands will do this in order to go to positions outside
3603 the current accessible part of the buffer.
3605 If `widen-automatically' is nil, these commands will do something else
3606 as a fallback, and won't change the buffer bounds.")
3608 (defun pop-global-mark ()
3609 "Pop off global mark ring and jump to the top location."
3610 (interactive)
3611 ;; Pop entries which refer to non-existent buffers.
3612 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3613 (setq global-mark-ring (cdr global-mark-ring)))
3614 (or global-mark-ring
3615 (error "No global mark set"))
3616 (let* ((marker (car global-mark-ring))
3617 (buffer (marker-buffer marker))
3618 (position (marker-position marker)))
3619 (setq global-mark-ring (nconc (cdr global-mark-ring)
3620 (list (car global-mark-ring))))
3621 (set-buffer buffer)
3622 (or (and (>= position (point-min))
3623 (<= position (point-max)))
3624 (if widen-automatically
3625 (widen)
3626 (error "Global mark position is outside accessible part of buffer")))
3627 (goto-char position)
3628 (switch-to-buffer buffer)))
3630 (defcustom next-line-add-newlines nil
3631 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3632 :type 'boolean
3633 :version "21.1"
3634 :group 'editing-basics)
3636 (defun next-line (&optional arg try-vscroll)
3637 "Move cursor vertically down ARG lines.
3638 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3639 If there is no character in the target line exactly under the current column,
3640 the cursor is positioned after the character in that line which spans this
3641 column, or at the end of the line if it is not long enough.
3642 If there is no line in the buffer after this one, behavior depends on the
3643 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3644 to create a line, and moves the cursor to that line. Otherwise it moves the
3645 cursor to the end of the buffer.
3647 The command \\[set-goal-column] can be used to create
3648 a semipermanent goal column for this command.
3649 Then instead of trying to move exactly vertically (or as close as possible),
3650 this command moves to the specified goal column (or as close as possible).
3651 The goal column is stored in the variable `goal-column', which is nil
3652 when there is no goal column.
3654 If you are thinking of using this in a Lisp program, consider
3655 using `forward-line' instead. It is usually easier to use
3656 and more reliable (no dependence on goal column, etc.)."
3657 (interactive "p\np")
3658 (or arg (setq arg 1))
3659 (if (and next-line-add-newlines (= arg 1))
3660 (if (save-excursion (end-of-line) (eobp))
3661 ;; When adding a newline, don't expand an abbrev.
3662 (let ((abbrev-mode nil))
3663 (end-of-line)
3664 (insert (if use-hard-newlines hard-newline "\n")))
3665 (line-move arg nil nil try-vscroll))
3666 (if (interactive-p)
3667 (condition-case nil
3668 (line-move arg nil nil try-vscroll)
3669 ((beginning-of-buffer end-of-buffer) (ding)))
3670 (line-move arg nil nil try-vscroll)))
3671 nil)
3673 (defun previous-line (&optional arg try-vscroll)
3674 "Move cursor vertically up ARG lines.
3675 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3676 If there is no character in the target line exactly over the current column,
3677 the cursor is positioned after the character in that line which spans this
3678 column, or at the end of the line if it is not long enough.
3680 The command \\[set-goal-column] can be used to create
3681 a semipermanent goal column for this command.
3682 Then instead of trying to move exactly vertically (or as close as possible),
3683 this command moves to the specified goal column (or as close as possible).
3684 The goal column is stored in the variable `goal-column', which is nil
3685 when there is no goal column.
3687 If you are thinking of using this in a Lisp program, consider using
3688 `forward-line' with a negative argument instead. It is usually easier
3689 to use and more reliable (no dependence on goal column, etc.)."
3690 (interactive "p\np")
3691 (or arg (setq arg 1))
3692 (if (interactive-p)
3693 (condition-case nil
3694 (line-move (- arg) nil nil try-vscroll)
3695 ((beginning-of-buffer end-of-buffer) (ding)))
3696 (line-move (- arg) nil nil try-vscroll))
3697 nil)
3699 (defcustom track-eol nil
3700 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3701 This means moving to the end of each line moved onto.
3702 The beginning of a blank line does not count as the end of a line."
3703 :type 'boolean
3704 :group 'editing-basics)
3706 (defcustom goal-column nil
3707 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3708 :type '(choice integer
3709 (const :tag "None" nil))
3710 :group 'editing-basics)
3711 (make-variable-buffer-local 'goal-column)
3713 (defvar temporary-goal-column 0
3714 "Current goal column for vertical motion.
3715 It is the column where point was
3716 at the start of current run of vertical motion commands.
3717 When the `track-eol' feature is doing its job, the value is `most-positive-fixnum'.")
3719 (defcustom line-move-ignore-invisible t
3720 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3721 Outline mode sets this."
3722 :type 'boolean
3723 :group 'editing-basics)
3725 ;; Returns non-nil if partial move was done.
3726 (defun line-move-partial (arg noerror to-end)
3727 (if (< arg 0)
3728 ;; Move backward (up).
3729 ;; If already vscrolled, reduce vscroll
3730 (let ((vs (window-vscroll nil t)))
3731 (when (> vs (frame-char-height))
3732 (set-window-vscroll nil (- vs (frame-char-height)) t)))
3734 ;; Move forward (down).
3735 (let* ((lh (window-line-height -1))
3736 (vpos (nth 1 lh))
3737 (ypos (nth 2 lh))
3738 (rbot (nth 3 lh))
3739 py vs)
3740 (when (or (null lh)
3741 (>= rbot (frame-char-height))
3742 (<= ypos (- (frame-char-height))))
3743 (unless lh
3744 (let ((wend (pos-visible-in-window-p t nil t)))
3745 (setq rbot (nth 3 wend)
3746 vpos (nth 5 wend))))
3747 (cond
3748 ;; If last line of window is fully visible, move forward.
3749 ((or (null rbot) (= rbot 0))
3750 nil)
3751 ;; If cursor is not in the bottom scroll margin, move forward.
3752 ((and (> vpos 0)
3753 (< (setq py
3754 (or (nth 1 (window-line-height))
3755 (let ((ppos (posn-at-point)))
3756 (cdr (or (posn-actual-col-row ppos)
3757 (posn-col-row ppos))))))
3758 (min (- (window-text-height) scroll-margin 1) (1- vpos))))
3759 nil)
3760 ;; When already vscrolled, we vscroll some more if we can,
3761 ;; or clear vscroll and move forward at end of tall image.
3762 ((> (setq vs (window-vscroll nil t)) 0)
3763 (when (> rbot 0)
3764 (set-window-vscroll nil (+ vs (min rbot (frame-char-height))) t)))
3765 ;; If cursor just entered the bottom scroll margin, move forward,
3766 ;; but also vscroll one line so redisplay wont recenter.
3767 ((and (> vpos 0)
3768 (= py (min (- (window-text-height) scroll-margin 1)
3769 (1- vpos))))
3770 (set-window-vscroll nil (frame-char-height) t)
3771 (line-move-1 arg noerror to-end)
3773 ;; If there are lines above the last line, scroll-up one line.
3774 ((> vpos 0)
3775 (scroll-up 1)
3777 ;; Finally, start vscroll.
3779 (set-window-vscroll nil (frame-char-height) t)))))))
3782 ;; This is like line-move-1 except that it also performs
3783 ;; vertical scrolling of tall images if appropriate.
3784 ;; That is not really a clean thing to do, since it mixes
3785 ;; scrolling with cursor motion. But so far we don't have
3786 ;; a cleaner solution to the problem of making C-n do something
3787 ;; useful given a tall image.
3788 (defun line-move (arg &optional noerror to-end try-vscroll)
3789 (unless (and auto-window-vscroll try-vscroll
3790 ;; Only vscroll for single line moves
3791 (= (abs arg) 1)
3792 ;; But don't vscroll in a keyboard macro.
3793 (not defining-kbd-macro)
3794 (not executing-kbd-macro)
3795 (line-move-partial arg noerror to-end))
3796 (set-window-vscroll nil 0 t)
3797 (line-move-1 arg noerror to-end)))
3799 ;; This is the guts of next-line and previous-line.
3800 ;; Arg says how many lines to move.
3801 ;; The value is t if we can move the specified number of lines.
3802 (defun line-move-1 (arg &optional noerror to-end)
3803 ;; Don't run any point-motion hooks, and disregard intangibility,
3804 ;; for intermediate positions.
3805 (let ((inhibit-point-motion-hooks t)
3806 (opoint (point))
3807 (orig-arg arg))
3808 (unwind-protect
3809 (progn
3810 (if (not (memq last-command '(next-line previous-line)))
3811 (setq temporary-goal-column
3812 (if (and track-eol (eolp)
3813 ;; Don't count beg of empty line as end of line
3814 ;; unless we just did explicit end-of-line.
3815 (or (not (bolp)) (eq last-command 'move-end-of-line)))
3816 most-positive-fixnum
3817 (current-column))))
3819 (if (not (or (integerp selective-display)
3820 line-move-ignore-invisible))
3821 ;; Use just newline characters.
3822 ;; Set ARG to 0 if we move as many lines as requested.
3823 (or (if (> arg 0)
3824 (progn (if (> arg 1) (forward-line (1- arg)))
3825 ;; This way of moving forward ARG lines
3826 ;; verifies that we have a newline after the last one.
3827 ;; It doesn't get confused by intangible text.
3828 (end-of-line)
3829 (if (zerop (forward-line 1))
3830 (setq arg 0)))
3831 (and (zerop (forward-line arg))
3832 (bolp)
3833 (setq arg 0)))
3834 (unless noerror
3835 (signal (if (< arg 0)
3836 'beginning-of-buffer
3837 'end-of-buffer)
3838 nil)))
3839 ;; Move by arg lines, but ignore invisible ones.
3840 (let (done)
3841 (while (and (> arg 0) (not done))
3842 ;; If the following character is currently invisible,
3843 ;; skip all characters with that same `invisible' property value.
3844 (while (and (not (eobp)) (invisible-p (point)))
3845 (goto-char (next-char-property-change (point))))
3846 ;; Move a line.
3847 ;; We don't use `end-of-line', since we want to escape
3848 ;; from field boundaries ocurring exactly at point.
3849 (goto-char (constrain-to-field
3850 (let ((inhibit-field-text-motion t))
3851 (line-end-position))
3852 (point) t t
3853 'inhibit-line-move-field-capture))
3854 ;; If there's no invisibility here, move over the newline.
3855 (cond
3856 ((eobp)
3857 (if (not noerror)
3858 (signal 'end-of-buffer nil)
3859 (setq done t)))
3860 ((and (> arg 1) ;; Use vertical-motion for last move
3861 (not (integerp selective-display))
3862 (not (invisible-p (point))))
3863 ;; We avoid vertical-motion when possible
3864 ;; because that has to fontify.
3865 (forward-line 1))
3866 ;; Otherwise move a more sophisticated way.
3867 ((zerop (vertical-motion 1))
3868 (if (not noerror)
3869 (signal 'end-of-buffer nil)
3870 (setq done t))))
3871 (unless done
3872 (setq arg (1- arg))))
3873 ;; The logic of this is the same as the loop above,
3874 ;; it just goes in the other direction.
3875 (while (and (< arg 0) (not done))
3876 ;; For completely consistency with the forward-motion
3877 ;; case, we should call beginning-of-line here.
3878 ;; However, if point is inside a field and on a
3879 ;; continued line, the call to (vertical-motion -1)
3880 ;; below won't move us back far enough; then we return
3881 ;; to the same column in line-move-finish, and point
3882 ;; gets stuck -- cyd
3883 (forward-line 0)
3884 (cond
3885 ((bobp)
3886 (if (not noerror)
3887 (signal 'beginning-of-buffer nil)
3888 (setq done t)))
3889 ((and (< arg -1) ;; Use vertical-motion for last move
3890 (not (integerp selective-display))
3891 (not (invisible-p (1- (point)))))
3892 (forward-line -1))
3893 ((zerop (vertical-motion -1))
3894 (if (not noerror)
3895 (signal 'beginning-of-buffer nil)
3896 (setq done t))))
3897 (unless done
3898 (setq arg (1+ arg))
3899 (while (and ;; Don't move over previous invis lines
3900 ;; if our target is the middle of this line.
3901 (or (zerop (or goal-column temporary-goal-column))
3902 (< arg 0))
3903 (not (bobp)) (invisible-p (1- (point))))
3904 (goto-char (previous-char-property-change (point))))))))
3905 ;; This is the value the function returns.
3906 (= arg 0))
3908 (cond ((> arg 0)
3909 ;; If we did not move down as far as desired,
3910 ;; at least go to end of line.
3911 (end-of-line))
3912 ((< arg 0)
3913 ;; If we did not move up as far as desired,
3914 ;; at least go to beginning of line.
3915 (beginning-of-line))
3917 (line-move-finish (or goal-column temporary-goal-column)
3918 opoint (> orig-arg 0)))))))
3920 (defun line-move-finish (column opoint forward)
3921 (let ((repeat t))
3922 (while repeat
3923 ;; Set REPEAT to t to repeat the whole thing.
3924 (setq repeat nil)
3926 (let (new
3927 (old (point))
3928 (line-beg (save-excursion (beginning-of-line) (point)))
3929 (line-end
3930 ;; Compute the end of the line
3931 ;; ignoring effectively invisible newlines.
3932 (save-excursion
3933 ;; Like end-of-line but ignores fields.
3934 (skip-chars-forward "^\n")
3935 (while (and (not (eobp)) (invisible-p (point)))
3936 (goto-char (next-char-property-change (point)))
3937 (skip-chars-forward "^\n"))
3938 (point))))
3940 ;; Move to the desired column.
3941 (line-move-to-column column)
3943 ;; Corner case: suppose we start out in a field boundary in
3944 ;; the middle of a continued line. When we get to
3945 ;; line-move-finish, point is at the start of a new *screen*
3946 ;; line but the same text line; then line-move-to-column would
3947 ;; move us backwards. Test using C-n with point on the "x" in
3948 ;; (insert "a" (propertize "x" 'field t) (make-string 89 ?y))
3949 (and forward
3950 (< (point) old)
3951 (goto-char old))
3953 (setq new (point))
3955 ;; Process intangibility within a line.
3956 ;; With inhibit-point-motion-hooks bound to nil, a call to
3957 ;; goto-char moves point past intangible text.
3959 ;; However, inhibit-point-motion-hooks controls both the
3960 ;; intangibility and the point-entered/point-left hooks. The
3961 ;; following hack avoids calling the point-* hooks
3962 ;; unnecessarily. Note that we move *forward* past intangible
3963 ;; text when the initial and final points are the same.
3964 (goto-char new)
3965 (let ((inhibit-point-motion-hooks nil))
3966 (goto-char new)
3968 ;; If intangibility moves us to a different (later) place
3969 ;; in the same line, use that as the destination.
3970 (if (<= (point) line-end)
3971 (setq new (point))
3972 ;; If that position is "too late",
3973 ;; try the previous allowable position.
3974 ;; See if it is ok.
3975 (backward-char)
3976 (if (if forward
3977 ;; If going forward, don't accept the previous
3978 ;; allowable position if it is before the target line.
3979 (< line-beg (point))
3980 ;; If going backward, don't accept the previous
3981 ;; allowable position if it is still after the target line.
3982 (<= (point) line-end))
3983 (setq new (point))
3984 ;; As a last resort, use the end of the line.
3985 (setq new line-end))))
3987 ;; Now move to the updated destination, processing fields
3988 ;; as well as intangibility.
3989 (goto-char opoint)
3990 (let ((inhibit-point-motion-hooks nil))
3991 (goto-char
3992 ;; Ignore field boundaries if the initial and final
3993 ;; positions have the same `field' property, even if the
3994 ;; fields are non-contiguous. This seems to be "nicer"
3995 ;; behavior in many situations.
3996 (if (eq (get-char-property new 'field)
3997 (get-char-property opoint 'field))
3999 (constrain-to-field new opoint t t
4000 'inhibit-line-move-field-capture))))
4002 ;; If all this moved us to a different line,
4003 ;; retry everything within that new line.
4004 (when (or (< (point) line-beg) (> (point) line-end))
4005 ;; Repeat the intangibility and field processing.
4006 (setq repeat t))))))
4008 (defun line-move-to-column (col)
4009 "Try to find column COL, considering invisibility.
4010 This function works only in certain cases,
4011 because what we really need is for `move-to-column'
4012 and `current-column' to be able to ignore invisible text."
4013 (if (zerop col)
4014 (beginning-of-line)
4015 (move-to-column col))
4017 (when (and line-move-ignore-invisible
4018 (not (bolp)) (invisible-p (1- (point))))
4019 (let ((normal-location (point))
4020 (normal-column (current-column)))
4021 ;; If the following character is currently invisible,
4022 ;; skip all characters with that same `invisible' property value.
4023 (while (and (not (eobp))
4024 (invisible-p (point)))
4025 (goto-char (next-char-property-change (point))))
4026 ;; Have we advanced to a larger column position?
4027 (if (> (current-column) normal-column)
4028 ;; We have made some progress towards the desired column.
4029 ;; See if we can make any further progress.
4030 (line-move-to-column (+ (current-column) (- col normal-column)))
4031 ;; Otherwise, go to the place we originally found
4032 ;; and move back over invisible text.
4033 ;; that will get us to the same place on the screen
4034 ;; but with a more reasonable buffer position.
4035 (goto-char normal-location)
4036 (let ((line-beg (save-excursion (beginning-of-line) (point))))
4037 (while (and (not (bolp)) (invisible-p (1- (point))))
4038 (goto-char (previous-char-property-change (point) line-beg))))))))
4040 (defun move-end-of-line (arg)
4041 "Move point to end of current line as displayed.
4042 \(If there's an image in the line, this disregards newlines
4043 which are part of the text that the image rests on.)
4045 With argument ARG not nil or 1, move forward ARG - 1 lines first.
4046 If point reaches the beginning or end of buffer, it stops there.
4047 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4048 (interactive "p")
4049 (or arg (setq arg 1))
4050 (let (done)
4051 (while (not done)
4052 (let ((newpos
4053 (save-excursion
4054 (let ((goal-column 0))
4055 (and (line-move arg t)
4056 (not (bobp))
4057 (progn
4058 (while (and (not (bobp)) (invisible-p (1- (point))))
4059 (goto-char (previous-single-char-property-change
4060 (point) 'invisible)))
4061 (backward-char 1)))
4062 (point)))))
4063 (goto-char newpos)
4064 (if (and (> (point) newpos)
4065 (eq (preceding-char) ?\n))
4066 (backward-char 1)
4067 (if (and (> (point) newpos) (not (eobp))
4068 (not (eq (following-char) ?\n)))
4069 ;; If we skipped something intangible
4070 ;; and now we're not really at eol,
4071 ;; keep going.
4072 (setq arg 1)
4073 (setq done t)))))))
4075 (defun move-beginning-of-line (arg)
4076 "Move point to beginning of current line as displayed.
4077 \(If there's an image in the line, this disregards newlines
4078 which are part of the text that the image rests on.)
4080 With argument ARG not nil or 1, move forward ARG - 1 lines first.
4081 If point reaches the beginning or end of buffer, it stops there.
4082 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4083 (interactive "p")
4084 (or arg (setq arg 1))
4086 (let ((orig (point))
4087 first-vis first-vis-field-value)
4089 ;; Move by lines, if ARG is not 1 (the default).
4090 (if (/= arg 1)
4091 (line-move (1- arg) t))
4093 ;; Move to beginning-of-line, ignoring fields and invisibles.
4094 (skip-chars-backward "^\n")
4095 (while (and (not (bobp)) (invisible-p (1- (point))))
4096 (goto-char (previous-char-property-change (point)))
4097 (skip-chars-backward "^\n"))
4099 ;; Now find first visible char in the line
4100 (while (and (not (eobp)) (invisible-p (point)))
4101 (goto-char (next-char-property-change (point))))
4102 (setq first-vis (point))
4104 ;; See if fields would stop us from reaching FIRST-VIS.
4105 (setq first-vis-field-value
4106 (constrain-to-field first-vis orig (/= arg 1) t nil))
4108 (goto-char (if (/= first-vis-field-value first-vis)
4109 ;; If yes, obey them.
4110 first-vis-field-value
4111 ;; Otherwise, move to START with attention to fields.
4112 ;; (It is possible that fields never matter in this case.)
4113 (constrain-to-field (point) orig
4114 (/= arg 1) t nil)))))
4117 ;;; Many people have said they rarely use this feature, and often type
4118 ;;; it by accident. Maybe it shouldn't even be on a key.
4119 (put 'set-goal-column 'disabled t)
4121 (defun set-goal-column (arg)
4122 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
4123 Those commands will move to this position in the line moved to
4124 rather than trying to keep the same horizontal position.
4125 With a non-nil argument, clears out the goal column
4126 so that \\[next-line] and \\[previous-line] resume vertical motion.
4127 The goal column is stored in the variable `goal-column'."
4128 (interactive "P")
4129 (if arg
4130 (progn
4131 (setq goal-column nil)
4132 (message "No goal column"))
4133 (setq goal-column (current-column))
4134 ;; The older method below can be erroneous if `set-goal-column' is bound
4135 ;; to a sequence containing %
4136 ;;(message (substitute-command-keys
4137 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
4138 ;;goal-column)
4139 (message "%s"
4140 (concat
4141 (format "Goal column %d " goal-column)
4142 (substitute-command-keys
4143 "(use \\[set-goal-column] with an arg to unset it)")))
4146 nil)
4149 (defun scroll-other-window-down (lines)
4150 "Scroll the \"other window\" down.
4151 For more details, see the documentation for `scroll-other-window'."
4152 (interactive "P")
4153 (scroll-other-window
4154 ;; Just invert the argument's meaning.
4155 ;; We can do that without knowing which window it will be.
4156 (if (eq lines '-) nil
4157 (if (null lines) '-
4158 (- (prefix-numeric-value lines))))))
4160 (defun beginning-of-buffer-other-window (arg)
4161 "Move point to the beginning of the buffer in the other window.
4162 Leave mark at previous position.
4163 With arg N, put point N/10 of the way from the true beginning."
4164 (interactive "P")
4165 (let ((orig-window (selected-window))
4166 (window (other-window-for-scrolling)))
4167 ;; We use unwind-protect rather than save-window-excursion
4168 ;; because the latter would preserve the things we want to change.
4169 (unwind-protect
4170 (progn
4171 (select-window window)
4172 ;; Set point and mark in that window's buffer.
4173 (with-no-warnings
4174 (beginning-of-buffer arg))
4175 ;; Set point accordingly.
4176 (recenter '(t)))
4177 (select-window orig-window))))
4179 (defun end-of-buffer-other-window (arg)
4180 "Move point to the end of the buffer in the other window.
4181 Leave mark at previous position.
4182 With arg N, put point N/10 of the way from the true end."
4183 (interactive "P")
4184 ;; See beginning-of-buffer-other-window for comments.
4185 (let ((orig-window (selected-window))
4186 (window (other-window-for-scrolling)))
4187 (unwind-protect
4188 (progn
4189 (select-window window)
4190 (with-no-warnings
4191 (end-of-buffer arg))
4192 (recenter '(t)))
4193 (select-window orig-window))))
4195 (defun transpose-chars (arg)
4196 "Interchange characters around point, moving forward one character.
4197 With prefix arg ARG, effect is to take character before point
4198 and drag it forward past ARG other characters (backward if ARG negative).
4199 If no argument and at end of line, the previous two chars are exchanged."
4200 (interactive "*P")
4201 (and (null arg) (eolp) (forward-char -1))
4202 (transpose-subr 'forward-char (prefix-numeric-value arg)))
4204 (defun transpose-words (arg)
4205 "Interchange words around point, leaving point at end of them.
4206 With prefix arg ARG, effect is to take word before or around point
4207 and drag it forward past ARG other words (backward if ARG negative).
4208 If ARG is zero, the words around or after point and around or after mark
4209 are interchanged."
4210 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
4211 (interactive "*p")
4212 (transpose-subr 'forward-word arg))
4214 (defun transpose-sexps (arg)
4215 "Like \\[transpose-words] but applies to sexps.
4216 Does not work on a sexp that point is in the middle of
4217 if it is a list or string."
4218 (interactive "*p")
4219 (transpose-subr
4220 (lambda (arg)
4221 ;; Here we should try to simulate the behavior of
4222 ;; (cons (progn (forward-sexp x) (point))
4223 ;; (progn (forward-sexp (- x)) (point)))
4224 ;; Except that we don't want to rely on the second forward-sexp
4225 ;; putting us back to where we want to be, since forward-sexp-function
4226 ;; might do funny things like infix-precedence.
4227 (if (if (> arg 0)
4228 (looking-at "\\sw\\|\\s_")
4229 (and (not (bobp))
4230 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
4231 ;; Jumping over a symbol. We might be inside it, mind you.
4232 (progn (funcall (if (> arg 0)
4233 'skip-syntax-backward 'skip-syntax-forward)
4234 "w_")
4235 (cons (save-excursion (forward-sexp arg) (point)) (point)))
4236 ;; Otherwise, we're between sexps. Take a step back before jumping
4237 ;; to make sure we'll obey the same precedence no matter which direction
4238 ;; we're going.
4239 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
4240 (cons (save-excursion (forward-sexp arg) (point))
4241 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
4242 (not (zerop (funcall (if (> arg 0)
4243 'skip-syntax-forward
4244 'skip-syntax-backward)
4245 ".")))))
4246 (point)))))
4247 arg 'special))
4249 (defun transpose-lines (arg)
4250 "Exchange current line and previous line, leaving point after both.
4251 With argument ARG, takes previous line and moves it past ARG lines.
4252 With argument 0, interchanges line point is in with line mark is in."
4253 (interactive "*p")
4254 (transpose-subr (function
4255 (lambda (arg)
4256 (if (> arg 0)
4257 (progn
4258 ;; Move forward over ARG lines,
4259 ;; but create newlines if necessary.
4260 (setq arg (forward-line arg))
4261 (if (/= (preceding-char) ?\n)
4262 (setq arg (1+ arg)))
4263 (if (> arg 0)
4264 (newline arg)))
4265 (forward-line arg))))
4266 arg))
4268 (defun transpose-subr (mover arg &optional special)
4269 (let ((aux (if special mover
4270 (lambda (x)
4271 (cons (progn (funcall mover x) (point))
4272 (progn (funcall mover (- x)) (point))))))
4273 pos1 pos2)
4274 (cond
4275 ((= arg 0)
4276 (save-excursion
4277 (setq pos1 (funcall aux 1))
4278 (goto-char (mark))
4279 (setq pos2 (funcall aux 1))
4280 (transpose-subr-1 pos1 pos2))
4281 (exchange-point-and-mark))
4282 ((> arg 0)
4283 (setq pos1 (funcall aux -1))
4284 (setq pos2 (funcall aux arg))
4285 (transpose-subr-1 pos1 pos2)
4286 (goto-char (car pos2)))
4288 (setq pos1 (funcall aux -1))
4289 (goto-char (car pos1))
4290 (setq pos2 (funcall aux arg))
4291 (transpose-subr-1 pos1 pos2)))))
4293 (defun transpose-subr-1 (pos1 pos2)
4294 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
4295 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
4296 (when (> (car pos1) (car pos2))
4297 (let ((swap pos1))
4298 (setq pos1 pos2 pos2 swap)))
4299 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
4300 (atomic-change-group
4301 (let (word2)
4302 ;; FIXME: We first delete the two pieces of text, so markers that
4303 ;; used to point to after the text end up pointing to before it :-(
4304 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
4305 (goto-char (car pos2))
4306 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
4307 (goto-char (car pos1))
4308 (insert word2))))
4310 (defun backward-word (&optional arg)
4311 "Move backward until encountering the beginning of a word.
4312 With argument, do this that many times."
4313 (interactive "p")
4314 (forward-word (- (or arg 1))))
4316 (defun mark-word (&optional arg allow-extend)
4317 "Set mark ARG words away from point.
4318 The place mark goes is the same place \\[forward-word] would
4319 move to with the same argument.
4320 Interactively, if this command is repeated
4321 or (in Transient Mark mode) if the mark is active,
4322 it marks the next ARG words after the ones already marked."
4323 (interactive "P\np")
4324 (cond ((and allow-extend
4325 (or (and (eq last-command this-command) (mark t))
4326 (and transient-mark-mode mark-active)))
4327 (setq arg (if arg (prefix-numeric-value arg)
4328 (if (< (mark) (point)) -1 1)))
4329 (set-mark
4330 (save-excursion
4331 (goto-char (mark))
4332 (forward-word arg)
4333 (point))))
4335 (push-mark
4336 (save-excursion
4337 (forward-word (prefix-numeric-value arg))
4338 (point))
4339 nil t))))
4341 (defun kill-word (arg)
4342 "Kill characters forward until encountering the end of a word.
4343 With argument, do this that many times."
4344 (interactive "p")
4345 (kill-region (point) (progn (forward-word arg) (point))))
4347 (defun backward-kill-word (arg)
4348 "Kill characters backward until encountering the beginning of a word.
4349 With argument, do this that many times."
4350 (interactive "p")
4351 (kill-word (- arg)))
4353 (defun current-word (&optional strict really-word)
4354 "Return the symbol or word that point is on (or a nearby one) as a string.
4355 The return value includes no text properties.
4356 If optional arg STRICT is non-nil, return nil unless point is within
4357 or adjacent to a symbol or word. In all cases the value can be nil
4358 if there is no word nearby.
4359 The function, belying its name, normally finds a symbol.
4360 If optional arg REALLY-WORD is non-nil, it finds just a word."
4361 (save-excursion
4362 (let* ((oldpoint (point)) (start (point)) (end (point))
4363 (syntaxes (if really-word "w" "w_"))
4364 (not-syntaxes (concat "^" syntaxes)))
4365 (skip-syntax-backward syntaxes) (setq start (point))
4366 (goto-char oldpoint)
4367 (skip-syntax-forward syntaxes) (setq end (point))
4368 (when (and (eq start oldpoint) (eq end oldpoint)
4369 ;; Point is neither within nor adjacent to a word.
4370 (not strict))
4371 ;; Look for preceding word in same line.
4372 (skip-syntax-backward not-syntaxes
4373 (save-excursion (beginning-of-line)
4374 (point)))
4375 (if (bolp)
4376 ;; No preceding word in same line.
4377 ;; Look for following word in same line.
4378 (progn
4379 (skip-syntax-forward not-syntaxes
4380 (save-excursion (end-of-line)
4381 (point)))
4382 (setq start (point))
4383 (skip-syntax-forward syntaxes)
4384 (setq end (point)))
4385 (setq end (point))
4386 (skip-syntax-backward syntaxes)
4387 (setq start (point))))
4388 ;; If we found something nonempty, return it as a string.
4389 (unless (= start end)
4390 (buffer-substring-no-properties start end)))))
4392 (defcustom fill-prefix nil
4393 "*String for filling to insert at front of new line, or nil for none."
4394 :type '(choice (const :tag "None" nil)
4395 string)
4396 :group 'fill)
4397 (make-variable-buffer-local 'fill-prefix)
4398 (put 'fill-prefix 'safe-local-variable 'string-or-null-p)
4400 (defcustom auto-fill-inhibit-regexp nil
4401 "*Regexp to match lines which should not be auto-filled."
4402 :type '(choice (const :tag "None" nil)
4403 regexp)
4404 :group 'fill)
4406 ;; This function is used as the auto-fill-function of a buffer
4407 ;; when Auto-Fill mode is enabled.
4408 ;; It returns t if it really did any work.
4409 ;; (Actually some major modes use a different auto-fill function,
4410 ;; but this one is the default one.)
4411 (defun do-auto-fill ()
4412 (let (fc justify give-up
4413 (fill-prefix fill-prefix))
4414 (if (or (not (setq justify (current-justification)))
4415 (null (setq fc (current-fill-column)))
4416 (and (eq justify 'left)
4417 (<= (current-column) fc))
4418 (and auto-fill-inhibit-regexp
4419 (save-excursion (beginning-of-line)
4420 (looking-at auto-fill-inhibit-regexp))))
4421 nil ;; Auto-filling not required
4422 (if (memq justify '(full center right))
4423 (save-excursion (unjustify-current-line)))
4425 ;; Choose a fill-prefix automatically.
4426 (when (and adaptive-fill-mode
4427 (or (null fill-prefix) (string= fill-prefix "")))
4428 (let ((prefix
4429 (fill-context-prefix
4430 (save-excursion (backward-paragraph 1) (point))
4431 (save-excursion (forward-paragraph 1) (point)))))
4432 (and prefix (not (equal prefix ""))
4433 ;; Use auto-indentation rather than a guessed empty prefix.
4434 (not (and fill-indent-according-to-mode
4435 (string-match "\\`[ \t]*\\'" prefix)))
4436 (setq fill-prefix prefix))))
4438 (while (and (not give-up) (> (current-column) fc))
4439 ;; Determine where to split the line.
4440 (let* (after-prefix
4441 (fill-point
4442 (save-excursion
4443 (beginning-of-line)
4444 (setq after-prefix (point))
4445 (and fill-prefix
4446 (looking-at (regexp-quote fill-prefix))
4447 (setq after-prefix (match-end 0)))
4448 (move-to-column (1+ fc))
4449 (fill-move-to-break-point after-prefix)
4450 (point))))
4452 ;; See whether the place we found is any good.
4453 (if (save-excursion
4454 (goto-char fill-point)
4455 (or (bolp)
4456 ;; There is no use breaking at end of line.
4457 (save-excursion (skip-chars-forward " ") (eolp))
4458 ;; It is futile to split at the end of the prefix
4459 ;; since we would just insert the prefix again.
4460 (and after-prefix (<= (point) after-prefix))
4461 ;; Don't split right after a comment starter
4462 ;; since we would just make another comment starter.
4463 (and comment-start-skip
4464 (let ((limit (point)))
4465 (beginning-of-line)
4466 (and (re-search-forward comment-start-skip
4467 limit t)
4468 (eq (point) limit))))))
4469 ;; No good place to break => stop trying.
4470 (setq give-up t)
4471 ;; Ok, we have a useful place to break the line. Do it.
4472 (let ((prev-column (current-column)))
4473 ;; If point is at the fill-point, do not `save-excursion'.
4474 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
4475 ;; point will end up before it rather than after it.
4476 (if (save-excursion
4477 (skip-chars-backward " \t")
4478 (= (point) fill-point))
4479 (default-indent-new-line t)
4480 (save-excursion
4481 (goto-char fill-point)
4482 (default-indent-new-line t)))
4483 ;; Now do justification, if required
4484 (if (not (eq justify 'left))
4485 (save-excursion
4486 (end-of-line 0)
4487 (justify-current-line justify nil t)))
4488 ;; If making the new line didn't reduce the hpos of
4489 ;; the end of the line, then give up now;
4490 ;; trying again will not help.
4491 (if (>= (current-column) prev-column)
4492 (setq give-up t))))))
4493 ;; Justify last line.
4494 (justify-current-line justify t t)
4495 t)))
4497 (defvar comment-line-break-function 'comment-indent-new-line
4498 "*Mode-specific function which line breaks and continues a comment.
4499 This function is called during auto-filling when a comment syntax
4500 is defined.
4501 The function should take a single optional argument, which is a flag
4502 indicating whether it should use soft newlines.")
4504 (defun default-indent-new-line (&optional soft)
4505 "Break line at point and indent.
4506 If a comment syntax is defined, call `comment-indent-new-line'.
4508 The inserted newline is marked hard if variable `use-hard-newlines' is true,
4509 unless optional argument SOFT is non-nil."
4510 (interactive)
4511 (if comment-start
4512 (funcall comment-line-break-function soft)
4513 ;; Insert the newline before removing empty space so that markers
4514 ;; get preserved better.
4515 (if soft (insert-and-inherit ?\n) (newline 1))
4516 (save-excursion (forward-char -1) (delete-horizontal-space))
4517 (delete-horizontal-space)
4519 (if (and fill-prefix (not adaptive-fill-mode))
4520 ;; Blindly trust a non-adaptive fill-prefix.
4521 (progn
4522 (indent-to-left-margin)
4523 (insert-before-markers-and-inherit fill-prefix))
4525 (cond
4526 ;; If there's an adaptive prefix, use it unless we're inside
4527 ;; a comment and the prefix is not a comment starter.
4528 (fill-prefix
4529 (indent-to-left-margin)
4530 (insert-and-inherit fill-prefix))
4531 ;; If we're not inside a comment, just try to indent.
4532 (t (indent-according-to-mode))))))
4534 (defvar normal-auto-fill-function 'do-auto-fill
4535 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
4536 Some major modes set this.")
4538 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
4539 ;; FIXME: turn into a proper minor mode.
4540 ;; Add a global minor mode version of it.
4541 (defun auto-fill-mode (&optional arg)
4542 "Toggle Auto Fill mode.
4543 With arg, turn Auto Fill mode on if and only if arg is positive.
4544 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
4545 automatically breaks the line at a previous space.
4547 The value of `normal-auto-fill-function' specifies the function to use
4548 for `auto-fill-function' when turning Auto Fill mode on."
4549 (interactive "P")
4550 (prog1 (setq auto-fill-function
4551 (if (if (null arg)
4552 (not auto-fill-function)
4553 (> (prefix-numeric-value arg) 0))
4554 normal-auto-fill-function
4555 nil))
4556 (force-mode-line-update)))
4558 ;; This holds a document string used to document auto-fill-mode.
4559 (defun auto-fill-function ()
4560 "Automatically break line at a previous space, in insertion of text."
4561 nil)
4563 (defun turn-on-auto-fill ()
4564 "Unconditionally turn on Auto Fill mode."
4565 (auto-fill-mode 1))
4567 (defun turn-off-auto-fill ()
4568 "Unconditionally turn off Auto Fill mode."
4569 (auto-fill-mode -1))
4571 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
4573 (defun set-fill-column (arg)
4574 "Set `fill-column' to specified argument.
4575 Use \\[universal-argument] followed by a number to specify a column.
4576 Just \\[universal-argument] as argument means to use the current column."
4577 (interactive "P")
4578 (if (consp arg)
4579 (setq arg (current-column)))
4580 (if (not (integerp arg))
4581 ;; Disallow missing argument; it's probably a typo for C-x C-f.
4582 (error "set-fill-column requires an explicit argument")
4583 (message "Fill column set to %d (was %d)" arg fill-column)
4584 (setq fill-column arg)))
4586 (defun set-selective-display (arg)
4587 "Set `selective-display' to ARG; clear it if no arg.
4588 When the value of `selective-display' is a number > 0,
4589 lines whose indentation is >= that value are not displayed.
4590 The variable `selective-display' has a separate value for each buffer."
4591 (interactive "P")
4592 (if (eq selective-display t)
4593 (error "selective-display already in use for marked lines"))
4594 (let ((current-vpos
4595 (save-restriction
4596 (narrow-to-region (point-min) (point))
4597 (goto-char (window-start))
4598 (vertical-motion (window-height)))))
4599 (setq selective-display
4600 (and arg (prefix-numeric-value arg)))
4601 (recenter current-vpos))
4602 (set-window-start (selected-window) (window-start (selected-window)))
4603 (princ "selective-display set to " t)
4604 (prin1 selective-display t)
4605 (princ "." t))
4607 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
4609 (defun toggle-truncate-lines (&optional arg)
4610 "Toggle whether to fold or truncate long lines for the current buffer.
4611 With prefix argument ARG, truncate long lines if ARG is positive,
4612 otherwise don't truncate them. Note that in side-by-side
4613 windows, truncation is always enabled."
4614 (interactive "P")
4615 (setq truncate-lines
4616 (if (null arg)
4617 (not truncate-lines)
4618 (> (prefix-numeric-value arg) 0)))
4619 (force-mode-line-update)
4620 (unless truncate-lines
4621 (let ((buffer (current-buffer)))
4622 (walk-windows (lambda (window)
4623 (if (eq buffer (window-buffer window))
4624 (set-window-hscroll window 0)))
4625 nil t)))
4626 (message "Truncate long lines %s"
4627 (if truncate-lines "enabled" "disabled")))
4629 (defvar overwrite-mode-textual " Ovwrt"
4630 "The string displayed in the mode line when in overwrite mode.")
4631 (defvar overwrite-mode-binary " Bin Ovwrt"
4632 "The string displayed in the mode line when in binary overwrite mode.")
4634 (defun overwrite-mode (arg)
4635 "Toggle overwrite mode.
4636 With prefix argument ARG, turn overwrite mode on if ARG is positive,
4637 otherwise turn it off. In overwrite mode, printing characters typed
4638 in replace existing text on a one-for-one basis, rather than pushing
4639 it to the right. At the end of a line, such characters extend the line.
4640 Before a tab, such characters insert until the tab is filled in.
4641 \\[quoted-insert] still inserts characters in overwrite mode; this
4642 is supposed to make it easier to insert characters when necessary."
4643 (interactive "P")
4644 (setq overwrite-mode
4645 (if (if (null arg) (not overwrite-mode)
4646 (> (prefix-numeric-value arg) 0))
4647 'overwrite-mode-textual))
4648 (force-mode-line-update))
4650 (defun binary-overwrite-mode (arg)
4651 "Toggle binary overwrite mode.
4652 With prefix argument ARG, turn binary overwrite mode on if ARG is
4653 positive, otherwise turn it off. In binary overwrite mode, printing
4654 characters typed in replace existing text. Newlines are not treated
4655 specially, so typing at the end of a line joins the line to the next,
4656 with the typed character between them. Typing before a tab character
4657 simply replaces the tab with the character typed. \\[quoted-insert]
4658 replaces the text at the cursor, just as ordinary typing characters do.
4660 Note that binary overwrite mode is not its own minor mode; it is a
4661 specialization of overwrite mode, entered by setting the
4662 `overwrite-mode' variable to `overwrite-mode-binary'."
4663 (interactive "P")
4664 (setq overwrite-mode
4665 (if (if (null arg)
4666 (not (eq overwrite-mode 'overwrite-mode-binary))
4667 (> (prefix-numeric-value arg) 0))
4668 'overwrite-mode-binary))
4669 (force-mode-line-update))
4671 (define-minor-mode line-number-mode
4672 "Toggle Line Number mode.
4673 With arg, turn Line Number mode on if arg is positive, otherwise
4674 turn it off. When Line Number mode is enabled, the line number
4675 appears in the mode line.
4677 Line numbers do not appear for very large buffers and buffers
4678 with very long lines; see variables `line-number-display-limit'
4679 and `line-number-display-limit-width'."
4680 :init-value t :global t :group 'mode-line)
4682 (define-minor-mode column-number-mode
4683 "Toggle Column Number mode.
4684 With arg, turn Column Number mode on if arg is positive,
4685 otherwise turn it off. When Column Number mode is enabled, the
4686 column number appears in the mode line."
4687 :global t :group 'mode-line)
4689 (define-minor-mode size-indication-mode
4690 "Toggle Size Indication mode.
4691 With arg, turn Size Indication mode on if arg is positive,
4692 otherwise turn it off. When Size Indication mode is enabled, the
4693 size of the accessible part of the buffer appears in the mode line."
4694 :global t :group 'mode-line)
4696 (defgroup paren-blinking nil
4697 "Blinking matching of parens and expressions."
4698 :prefix "blink-matching-"
4699 :group 'paren-matching)
4701 (defcustom blink-matching-paren t
4702 "*Non-nil means show matching open-paren when close-paren is inserted."
4703 :type 'boolean
4704 :group 'paren-blinking)
4706 (defcustom blink-matching-paren-on-screen t
4707 "*Non-nil means show matching open-paren when it is on screen.
4708 If nil, don't show it (but the open-paren can still be shown
4709 when it is off screen).
4711 This variable has no effect if `blink-matching-paren' is nil.
4712 \(In that case, the open-paren is never shown.)
4713 It is also ignored if `show-paren-mode' is enabled."
4714 :type 'boolean
4715 :group 'paren-blinking)
4717 (defcustom blink-matching-paren-distance (* 25 1024)
4718 "*If non-nil, maximum distance to search backwards for matching open-paren.
4719 If nil, search stops at the beginning of the accessible portion of the buffer."
4720 :type '(choice (const nil) integer)
4721 :group 'paren-blinking)
4723 (defcustom blink-matching-delay 1
4724 "*Time in seconds to delay after showing a matching paren."
4725 :type 'number
4726 :group 'paren-blinking)
4728 (defcustom blink-matching-paren-dont-ignore-comments nil
4729 "*If nil, `blink-matching-paren' ignores comments.
4730 More precisely, when looking for the matching parenthesis,
4731 it skips the contents of comments that end before point."
4732 :type 'boolean
4733 :group 'paren-blinking)
4735 (defun blink-matching-open ()
4736 "Move cursor momentarily to the beginning of the sexp before point."
4737 (interactive)
4738 (when (and (> (point) (point-min))
4739 blink-matching-paren
4740 ;; Verify an even number of quoting characters precede the close.
4741 (= 1 (logand 1 (- (point)
4742 (save-excursion
4743 (forward-char -1)
4744 (skip-syntax-backward "/\\")
4745 (point))))))
4746 (let* ((oldpos (point))
4747 (message-log-max nil) ; Don't log messages about paren matching.
4748 (atdollar (eq (syntax-class (syntax-after (1- oldpos))) 8))
4749 (isdollar)
4750 (blinkpos
4751 (save-excursion
4752 (save-restriction
4753 (if blink-matching-paren-distance
4754 (narrow-to-region
4755 (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
4756 (- (point) blink-matching-paren-distance))
4757 oldpos))
4758 (let ((parse-sexp-ignore-comments
4759 (and parse-sexp-ignore-comments
4760 (not blink-matching-paren-dont-ignore-comments))))
4761 (condition-case ()
4762 (scan-sexps oldpos -1)
4763 (error nil))))))
4764 (matching-paren
4765 (and blinkpos
4766 ;; Not syntax '$'.
4767 (not (setq isdollar
4768 (eq (syntax-class (syntax-after blinkpos)) 8)))
4769 (let ((syntax (syntax-after blinkpos)))
4770 (and (consp syntax)
4771 (eq (syntax-class syntax) 4)
4772 (cdr syntax))))))
4773 (cond
4774 ;; isdollar is for:
4775 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00871.html
4776 ((not (or (and isdollar blinkpos)
4777 (and atdollar (not blinkpos)) ; see below
4778 (eq matching-paren (char-before oldpos))
4779 ;; The cdr might hold a new paren-class info rather than
4780 ;; a matching-char info, in which case the two CDRs
4781 ;; should match.
4782 (eq matching-paren (cdr (syntax-after (1- oldpos))))))
4783 (message "Mismatched parentheses"))
4784 ((not blinkpos)
4785 (or blink-matching-paren-distance
4786 ;; Don't complain when `$' with no blinkpos, because it
4787 ;; could just be the first one typed in the buffer.
4788 atdollar
4789 (message "Unmatched parenthesis")))
4790 ((pos-visible-in-window-p blinkpos)
4791 ;; Matching open within window, temporarily move to blinkpos but only
4792 ;; if `blink-matching-paren-on-screen' is non-nil.
4793 (and blink-matching-paren-on-screen
4794 (not show-paren-mode)
4795 (save-excursion
4796 (goto-char blinkpos)
4797 (sit-for blink-matching-delay))))
4799 (save-excursion
4800 (goto-char blinkpos)
4801 (let ((open-paren-line-string
4802 ;; Show what precedes the open in its line, if anything.
4803 (cond
4804 ((save-excursion (skip-chars-backward " \t") (not (bolp)))
4805 (buffer-substring (line-beginning-position)
4806 (1+ blinkpos)))
4807 ;; Show what follows the open in its line, if anything.
4808 ((save-excursion
4809 (forward-char 1)
4810 (skip-chars-forward " \t")
4811 (not (eolp)))
4812 (buffer-substring blinkpos
4813 (line-end-position)))
4814 ;; Otherwise show the previous nonblank line,
4815 ;; if there is one.
4816 ((save-excursion (skip-chars-backward "\n \t") (not (bobp)))
4817 (concat
4818 (buffer-substring (progn
4819 (skip-chars-backward "\n \t")
4820 (line-beginning-position))
4821 (progn (end-of-line)
4822 (skip-chars-backward " \t")
4823 (point)))
4824 ;; Replace the newline and other whitespace with `...'.
4825 "..."
4826 (buffer-substring blinkpos (1+ blinkpos))))
4827 ;; There is nothing to show except the char itself.
4828 (t (buffer-substring blinkpos (1+ blinkpos))))))
4829 (message "Matches %s"
4830 (substring-no-properties open-paren-line-string)))))))))
4832 ;; Turned off because it makes dbx bomb out.
4833 (setq blink-paren-function 'blink-matching-open)
4835 ;; This executes C-g typed while Emacs is waiting for a command.
4836 ;; Quitting out of a program does not go through here;
4837 ;; that happens in the QUIT macro at the C code level.
4838 (defun keyboard-quit ()
4839 "Signal a `quit' condition.
4840 During execution of Lisp code, this character causes a quit directly.
4841 At top-level, as an editor command, this simply beeps."
4842 (interactive)
4843 (deactivate-mark)
4844 (if (fboundp 'kmacro-keyboard-quit)
4845 (kmacro-keyboard-quit))
4846 (setq defining-kbd-macro nil)
4847 (signal 'quit nil))
4849 (defvar buffer-quit-function nil
4850 "Function to call to \"quit\" the current buffer, or nil if none.
4851 \\[keyboard-escape-quit] calls this function when its more local actions
4852 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4854 (defun keyboard-escape-quit ()
4855 "Exit the current \"mode\" (in a generalized sense of the word).
4856 This command can exit an interactive command such as `query-replace',
4857 can clear out a prefix argument or a region,
4858 can get out of the minibuffer or other recursive edit,
4859 cancel the use of the current buffer (for special-purpose buffers),
4860 or go back to just one window (by deleting all but the selected window)."
4861 (interactive)
4862 (cond ((eq last-command 'mode-exited) nil)
4863 ((> (minibuffer-depth) 0)
4864 (abort-recursive-edit))
4865 (current-prefix-arg
4866 nil)
4867 ((and transient-mark-mode mark-active)
4868 (deactivate-mark))
4869 ((> (recursion-depth) 0)
4870 (exit-recursive-edit))
4871 (buffer-quit-function
4872 (funcall buffer-quit-function))
4873 ((not (one-window-p t))
4874 (delete-other-windows))
4875 ((string-match "^ \\*" (buffer-name (current-buffer)))
4876 (bury-buffer))))
4878 (defun play-sound-file (file &optional volume device)
4879 "Play sound stored in FILE.
4880 VOLUME and DEVICE correspond to the keywords of the sound
4881 specification for `play-sound'."
4882 (interactive "fPlay sound file: ")
4883 (let ((sound (list :file file)))
4884 (if volume
4885 (plist-put sound :volume volume))
4886 (if device
4887 (plist-put sound :device device))
4888 (push 'sound sound)
4889 (play-sound sound)))
4892 (defcustom read-mail-command 'rmail
4893 "*Your preference for a mail reading package.
4894 This is used by some keybindings which support reading mail.
4895 See also `mail-user-agent' concerning sending mail."
4896 :type '(choice (function-item rmail)
4897 (function-item gnus)
4898 (function-item mh-rmail)
4899 (function :tag "Other"))
4900 :version "21.1"
4901 :group 'mail)
4903 (defcustom mail-user-agent 'sendmail-user-agent
4904 "*Your preference for a mail composition package.
4905 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4906 outgoing email message. This variable lets you specify which
4907 mail-sending package you prefer.
4909 Valid values include:
4911 `sendmail-user-agent' -- use the default Emacs Mail package.
4912 See Info node `(emacs)Sending Mail'.
4913 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4914 See Info node `(mh-e)'.
4915 `message-user-agent' -- use the Gnus Message package.
4916 See Info node `(message)'.
4917 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4918 paraphernalia, particularly the Gcc: header for
4919 archiving.
4921 Additional valid symbols may be available; check with the author of
4922 your package for details. The function should return non-nil if it
4923 succeeds.
4925 See also `read-mail-command' concerning reading mail."
4926 :type '(radio (function-item :tag "Default Emacs mail"
4927 :format "%t\n"
4928 sendmail-user-agent)
4929 (function-item :tag "Emacs interface to MH"
4930 :format "%t\n"
4931 mh-e-user-agent)
4932 (function-item :tag "Gnus Message package"
4933 :format "%t\n"
4934 message-user-agent)
4935 (function-item :tag "Gnus Message with full Gnus features"
4936 :format "%t\n"
4937 gnus-user-agent)
4938 (function :tag "Other"))
4939 :group 'mail)
4941 (define-mail-user-agent 'sendmail-user-agent
4942 'sendmail-user-agent-compose
4943 'mail-send-and-exit)
4945 (defun rfc822-goto-eoh ()
4946 ;; Go to header delimiter line in a mail message, following RFC822 rules
4947 (goto-char (point-min))
4948 (when (re-search-forward
4949 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4950 (goto-char (match-beginning 0))))
4952 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4953 switch-function yank-action
4954 send-actions)
4955 (if switch-function
4956 (let ((special-display-buffer-names nil)
4957 (special-display-regexps nil)
4958 (same-window-buffer-names nil)
4959 (same-window-regexps nil))
4960 (funcall switch-function "*mail*")))
4961 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4962 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4963 (body (cdr (assoc-string "body" other-headers t))))
4964 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4965 continue
4966 (error "Message aborted"))
4967 (save-excursion
4968 (rfc822-goto-eoh)
4969 (while other-headers
4970 (unless (member-ignore-case (car (car other-headers))
4971 '("in-reply-to" "cc" "body"))
4972 (insert (car (car other-headers)) ": "
4973 (cdr (car other-headers))
4974 (if use-hard-newlines hard-newline "\n")))
4975 (setq other-headers (cdr other-headers)))
4976 (when body
4977 (forward-line 1)
4978 (insert body))
4979 t)))
4981 (defun compose-mail (&optional to subject other-headers continue
4982 switch-function yank-action send-actions)
4983 "Start composing a mail message to send.
4984 This uses the user's chosen mail composition package
4985 as selected with the variable `mail-user-agent'.
4986 The optional arguments TO and SUBJECT specify recipients
4987 and the initial Subject field, respectively.
4989 OTHER-HEADERS is an alist specifying additional
4990 header fields. Elements look like (HEADER . VALUE) where both
4991 HEADER and VALUE are strings.
4993 CONTINUE, if non-nil, says to continue editing a message already
4994 being composed.
4996 SWITCH-FUNCTION, if non-nil, is a function to use to
4997 switch to and display the buffer used for mail composition.
4999 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
5000 to insert the raw text of the message being replied to.
5001 It has the form (FUNCTION . ARGS). The user agent will apply
5002 FUNCTION to ARGS, to insert the raw text of the original message.
5003 \(The user agent will also run `mail-citation-hook', *after* the
5004 original text has been inserted in this way.)
5006 SEND-ACTIONS is a list of actions to call when the message is sent.
5007 Each action has the form (FUNCTION . ARGS)."
5008 (interactive
5009 (list nil nil nil current-prefix-arg))
5010 (let ((function (get mail-user-agent 'composefunc)))
5011 (funcall function to subject other-headers continue
5012 switch-function yank-action send-actions)))
5014 (defun compose-mail-other-window (&optional to subject other-headers continue
5015 yank-action send-actions)
5016 "Like \\[compose-mail], but edit the outgoing message in another window."
5017 (interactive
5018 (list nil nil nil current-prefix-arg))
5019 (compose-mail to subject other-headers continue
5020 'switch-to-buffer-other-window yank-action send-actions))
5023 (defun compose-mail-other-frame (&optional to subject other-headers continue
5024 yank-action send-actions)
5025 "Like \\[compose-mail], but edit the outgoing message in another frame."
5026 (interactive
5027 (list nil nil nil current-prefix-arg))
5028 (compose-mail to subject other-headers continue
5029 'switch-to-buffer-other-frame yank-action send-actions))
5031 (defvar set-variable-value-history nil
5032 "History of values entered with `set-variable'.
5034 Maximum length of the history list is determined by the value
5035 of `history-length', which see.")
5037 (defun set-variable (variable value &optional make-local)
5038 "Set VARIABLE to VALUE. VALUE is a Lisp object.
5039 VARIABLE should be a user option variable name, a Lisp variable
5040 meant to be customized by users. You should enter VALUE in Lisp syntax,
5041 so if you want VALUE to be a string, you must surround it with doublequotes.
5042 VALUE is used literally, not evaluated.
5044 If VARIABLE has a `variable-interactive' property, that is used as if
5045 it were the arg to `interactive' (which see) to interactively read VALUE.
5047 If VARIABLE has been defined with `defcustom', then the type information
5048 in the definition is used to check that VALUE is valid.
5050 With a prefix argument, set VARIABLE to VALUE buffer-locally."
5051 (interactive
5052 (let* ((default-var (variable-at-point))
5053 (var (if (user-variable-p default-var)
5054 (read-variable (format "Set variable (default %s): " default-var)
5055 default-var)
5056 (read-variable "Set variable: ")))
5057 (minibuffer-help-form '(describe-variable var))
5058 (prop (get var 'variable-interactive))
5059 (obsolete (car (get var 'byte-obsolete-variable)))
5060 (prompt (format "Set %s %s to value: " var
5061 (cond ((local-variable-p var)
5062 "(buffer-local)")
5063 ((or current-prefix-arg
5064 (local-variable-if-set-p var))
5065 "buffer-locally")
5066 (t "globally"))))
5067 (val (progn
5068 (when obsolete
5069 (message (concat "`%S' is obsolete; "
5070 (if (symbolp obsolete) "use `%S' instead" "%s"))
5071 var obsolete)
5072 (sit-for 3))
5073 (if prop
5074 ;; Use VAR's `variable-interactive' property
5075 ;; as an interactive spec for prompting.
5076 (call-interactively `(lambda (arg)
5077 (interactive ,prop)
5078 arg))
5079 (read
5080 (read-string prompt nil
5081 'set-variable-value-history
5082 (format "%S" (symbol-value var))))))))
5083 (list var val current-prefix-arg)))
5085 (and (custom-variable-p variable)
5086 (not (get variable 'custom-type))
5087 (custom-load-symbol variable))
5088 (let ((type (get variable 'custom-type)))
5089 (when type
5090 ;; Match with custom type.
5091 (require 'cus-edit)
5092 (setq type (widget-convert type))
5093 (unless (widget-apply type :match value)
5094 (error "Value `%S' does not match type %S of %S"
5095 value (car type) variable))))
5097 (if make-local
5098 (make-local-variable variable))
5100 (set variable value)
5102 ;; Force a thorough redisplay for the case that the variable
5103 ;; has an effect on the display, like `tab-width' has.
5104 (force-mode-line-update))
5106 ;; Define the major mode for lists of completions.
5108 (defvar completion-list-mode-map nil
5109 "Local map for completion list buffers.")
5110 (or completion-list-mode-map
5111 (let ((map (make-sparse-keymap)))
5112 (define-key map [mouse-2] 'mouse-choose-completion)
5113 (define-key map [follow-link] 'mouse-face)
5114 (define-key map [down-mouse-2] nil)
5115 (define-key map "\C-m" 'choose-completion)
5116 (define-key map "\e\e\e" 'delete-completion-window)
5117 (define-key map [left] 'previous-completion)
5118 (define-key map [right] 'next-completion)
5119 (setq completion-list-mode-map map)))
5121 ;; Completion mode is suitable only for specially formatted data.
5122 (put 'completion-list-mode 'mode-class 'special)
5124 (defvar completion-reference-buffer nil
5125 "Record the buffer that was current when the completion list was requested.
5126 This is a local variable in the completion list buffer.
5127 Initial value is nil to avoid some compiler warnings.")
5129 (defvar completion-no-auto-exit nil
5130 "Non-nil means `choose-completion-string' should never exit the minibuffer.
5131 This also applies to other functions such as `choose-completion'
5132 and `mouse-choose-completion'.")
5134 (defvar completion-base-size nil
5135 "Number of chars at beginning of minibuffer not involved in completion.
5136 This is a local variable in the completion list buffer
5137 but it talks about the buffer in `completion-reference-buffer'.
5138 If this is nil, it means to compare text to determine which part
5139 of the tail end of the buffer's text is involved in completion.")
5141 (defun delete-completion-window ()
5142 "Delete the completion list window.
5143 Go to the window from which completion was requested."
5144 (interactive)
5145 (let ((buf completion-reference-buffer))
5146 (if (one-window-p t)
5147 (if (window-dedicated-p (selected-window))
5148 (delete-frame (selected-frame)))
5149 (delete-window (selected-window))
5150 (if (get-buffer-window buf)
5151 (select-window (get-buffer-window buf))))))
5153 (defun previous-completion (n)
5154 "Move to the previous item in the completion list."
5155 (interactive "p")
5156 (next-completion (- n)))
5158 (defun next-completion (n)
5159 "Move to the next item in the completion list.
5160 With prefix argument N, move N items (negative N means move backward)."
5161 (interactive "p")
5162 (let ((beg (point-min)) (end (point-max)))
5163 (while (and (> n 0) (not (eobp)))
5164 ;; If in a completion, move to the end of it.
5165 (when (get-text-property (point) 'mouse-face)
5166 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
5167 ;; Move to start of next one.
5168 (unless (get-text-property (point) 'mouse-face)
5169 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
5170 (setq n (1- n)))
5171 (while (and (< n 0) (not (bobp)))
5172 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
5173 ;; If in a completion, move to the start of it.
5174 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
5175 (goto-char (previous-single-property-change
5176 (point) 'mouse-face nil beg)))
5177 ;; Move to end of the previous completion.
5178 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
5179 (goto-char (previous-single-property-change
5180 (point) 'mouse-face nil beg)))
5181 ;; Move to the start of that one.
5182 (goto-char (previous-single-property-change
5183 (point) 'mouse-face nil beg))
5184 (setq n (1+ n))))))
5186 (defun choose-completion ()
5187 "Choose the completion that point is in or next to."
5188 (interactive)
5189 (let (beg end completion (buffer completion-reference-buffer)
5190 (base-size completion-base-size))
5191 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
5192 (setq end (point) beg (1+ (point))))
5193 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
5194 (setq end (1- (point)) beg (point)))
5195 (if (null beg)
5196 (error "No completion here"))
5197 (setq beg (previous-single-property-change beg 'mouse-face))
5198 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
5199 (setq completion (buffer-substring-no-properties beg end))
5200 (let ((owindow (selected-window)))
5201 (if (and (one-window-p t 'selected-frame)
5202 (window-dedicated-p (selected-window)))
5203 ;; This is a special buffer's frame
5204 (iconify-frame (selected-frame))
5205 (or (window-dedicated-p (selected-window))
5206 (bury-buffer)))
5207 (select-window owindow))
5208 (choose-completion-string completion buffer base-size)))
5210 ;; Delete the longest partial match for STRING
5211 ;; that can be found before POINT.
5212 (defun choose-completion-delete-max-match (string)
5213 (let ((opoint (point))
5214 len)
5215 ;; Try moving back by the length of the string.
5216 (goto-char (max (- (point) (length string))
5217 (minibuffer-prompt-end)))
5218 ;; See how far back we were actually able to move. That is the
5219 ;; upper bound on how much we can match and delete.
5220 (setq len (- opoint (point)))
5221 (if completion-ignore-case
5222 (setq string (downcase string)))
5223 (while (and (> len 0)
5224 (let ((tail (buffer-substring (point) opoint)))
5225 (if completion-ignore-case
5226 (setq tail (downcase tail)))
5227 (not (string= tail (substring string 0 len)))))
5228 (setq len (1- len))
5229 (forward-char 1))
5230 (delete-char len)))
5232 (defvar choose-completion-string-functions nil
5233 "Functions that may override the normal insertion of a completion choice.
5234 These functions are called in order with four arguments:
5235 CHOICE - the string to insert in the buffer,
5236 BUFFER - the buffer in which the choice should be inserted,
5237 MINI-P - non-nil if BUFFER is a minibuffer, and
5238 BASE-SIZE - the number of characters in BUFFER before
5239 the string being completed.
5241 If a function in the list returns non-nil, that function is supposed
5242 to have inserted the CHOICE in the BUFFER, and possibly exited
5243 the minibuffer; no further functions will be called.
5245 If all functions in the list return nil, that means to use
5246 the default method of inserting the completion in BUFFER.")
5248 (defun choose-completion-string (choice &optional buffer base-size)
5249 "Switch to BUFFER and insert the completion choice CHOICE.
5250 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
5251 to keep. If it is nil, we call `choose-completion-delete-max-match'
5252 to decide what to delete."
5254 ;; If BUFFER is the minibuffer, exit the minibuffer
5255 ;; unless it is reading a file name and CHOICE is a directory,
5256 ;; or completion-no-auto-exit is non-nil.
5258 (let* ((buffer (or buffer completion-reference-buffer))
5259 (mini-p (minibufferp buffer)))
5260 ;; If BUFFER is a minibuffer, barf unless it's the currently
5261 ;; active minibuffer.
5262 (if (and mini-p
5263 (or (not (active-minibuffer-window))
5264 (not (equal buffer
5265 (window-buffer (active-minibuffer-window))))))
5266 (error "Minibuffer is not active for completion")
5267 ;; Set buffer so buffer-local choose-completion-string-functions works.
5268 (set-buffer buffer)
5269 (unless (run-hook-with-args-until-success
5270 'choose-completion-string-functions
5271 choice buffer mini-p base-size)
5272 ;; Insert the completion into the buffer where it was requested.
5273 (if base-size
5274 (delete-region (+ base-size (if mini-p
5275 (minibuffer-prompt-end)
5276 (point-min)))
5277 (point))
5278 (choose-completion-delete-max-match choice))
5279 (insert choice)
5280 (remove-text-properties (- (point) (length choice)) (point)
5281 '(mouse-face nil))
5282 ;; Update point in the window that BUFFER is showing in.
5283 (let ((window (get-buffer-window buffer t)))
5284 (set-window-point window (point)))
5285 ;; If completing for the minibuffer, exit it with this choice.
5286 (and (not completion-no-auto-exit)
5287 (equal buffer (window-buffer (minibuffer-window)))
5288 minibuffer-completion-table
5289 ;; If this is reading a file name, and the file name chosen
5290 ;; is a directory, don't exit the minibuffer.
5291 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
5292 (file-directory-p (field-string (point-max))))
5293 (let ((mini (active-minibuffer-window)))
5294 (select-window mini)
5295 (when minibuffer-auto-raise
5296 (raise-frame (window-frame mini))))
5297 (exit-minibuffer)))))))
5299 (defun completion-list-mode ()
5300 "Major mode for buffers showing lists of possible completions.
5301 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
5302 to select the completion near point.
5303 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
5304 with the mouse.
5306 \\{completion-list-mode-map}"
5308 (interactive)
5309 (kill-all-local-variables)
5310 (use-local-map completion-list-mode-map)
5311 (setq mode-name "Completion List")
5312 (setq major-mode 'completion-list-mode)
5313 (make-local-variable 'completion-base-size)
5314 (setq completion-base-size nil)
5315 (run-mode-hooks 'completion-list-mode-hook))
5317 (defun completion-list-mode-finish ()
5318 "Finish setup of the completions buffer.
5319 Called from `temp-buffer-show-hook'."
5320 (when (eq major-mode 'completion-list-mode)
5321 (toggle-read-only 1)))
5323 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
5325 (defvar completion-setup-hook nil
5326 "Normal hook run at the end of setting up a completion list buffer.
5327 When this hook is run, the current buffer is the one in which the
5328 command to display the completion list buffer was run.
5329 The completion list buffer is available as the value of `standard-output'.
5330 The common prefix substring for completion may be available as the
5331 value of `completion-common-substring'. See also `display-completion-list'.")
5334 ;; Variables and faces used in `completion-setup-function'.
5336 (defcustom completion-show-help t
5337 "Non-nil means show help message in *Completions* buffer."
5338 :type 'boolean
5339 :version "22.1"
5340 :group 'completion)
5342 (defface completions-first-difference
5343 '((t (:inherit bold)))
5344 "Face put on the first uncommon character in completions in *Completions* buffer."
5345 :group 'completion)
5347 (defface completions-common-part
5348 '((t (:inherit default)))
5349 "Face put on the common prefix substring in completions in *Completions* buffer.
5350 The idea of `completions-common-part' is that you can use it to
5351 make the common parts less visible than normal, so that the rest
5352 of the differing parts is, by contrast, slightly highlighted."
5353 :group 'completion)
5355 ;; This is for packages that need to bind it to a non-default regexp
5356 ;; in order to make the first-differing character highlight work
5357 ;; to their liking
5358 (defvar completion-root-regexp "^/"
5359 "Regexp to use in `completion-setup-function' to find the root directory.")
5361 (defvar completion-common-substring nil
5362 "Common prefix substring to use in `completion-setup-function' to put faces.
5363 The value is set by `display-completion-list' during running `completion-setup-hook'.
5365 To put faces `completions-first-difference' and `completions-common-part'
5366 in the `*Completions*' buffer, the common prefix substring in completions
5367 is needed as a hint. (The minibuffer is a special case. The content
5368 of the minibuffer before point is always the common substring.)")
5370 ;; This function goes in completion-setup-hook, so that it is called
5371 ;; after the text of the completion list buffer is written.
5372 (defun completion-setup-function ()
5373 (let* ((mainbuf (current-buffer))
5374 (mbuf-contents (minibuffer-completion-contents))
5375 common-string-length)
5376 ;; When reading a file name in the minibuffer,
5377 ;; set default-directory in the minibuffer
5378 ;; so it will get copied into the completion list buffer.
5379 (if minibuffer-completing-file-name
5380 (with-current-buffer mainbuf
5381 (setq default-directory
5382 (file-name-directory (expand-file-name mbuf-contents)))))
5383 (with-current-buffer standard-output
5384 (completion-list-mode)
5385 (set (make-local-variable 'completion-reference-buffer) mainbuf)
5386 (setq completion-base-size
5387 (cond
5388 ((and (symbolp minibuffer-completion-table)
5389 (get minibuffer-completion-table 'completion-base-size-function))
5390 ;; To compute base size, a function can use the global value of
5391 ;; completion-common-substring or minibuffer-completion-contents.
5392 (with-current-buffer mainbuf
5393 (funcall (get minibuffer-completion-table
5394 'completion-base-size-function))))
5395 (minibuffer-completing-file-name
5396 ;; For file name completion, use the number of chars before
5397 ;; the start of the file name component at point.
5398 (with-current-buffer mainbuf
5399 (save-excursion
5400 (skip-chars-backward completion-root-regexp)
5401 (- (point) (minibuffer-prompt-end)))))
5402 (minibuffer-completing-symbol nil)
5403 ;; Otherwise, in minibuffer, the base size is 0.
5404 ((minibufferp mainbuf) 0)))
5405 (setq common-string-length
5406 (cond
5407 (completion-common-substring
5408 (length completion-common-substring))
5409 (completion-base-size
5410 (- (length mbuf-contents) completion-base-size))))
5411 ;; Put faces on first uncommon characters and common parts.
5412 (when (and (integerp common-string-length) (>= common-string-length 0))
5413 (let ((element-start (point-min))
5414 (maxp (point-max))
5415 element-common-end)
5416 (while (and (setq element-start
5417 (next-single-property-change
5418 element-start 'mouse-face))
5419 (< (setq element-common-end
5420 (+ element-start common-string-length))
5421 maxp))
5422 (when (get-char-property element-start 'mouse-face)
5423 (if (and (> common-string-length 0)
5424 (get-char-property (1- element-common-end) 'mouse-face))
5425 (put-text-property element-start element-common-end
5426 'font-lock-face 'completions-common-part))
5427 (if (get-char-property element-common-end 'mouse-face)
5428 (put-text-property element-common-end (1+ element-common-end)
5429 'font-lock-face 'completions-first-difference))))))
5430 ;; Maybe insert help string.
5431 (when completion-show-help
5432 (goto-char (point-min))
5433 (if (display-mouse-p)
5434 (insert (substitute-command-keys
5435 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
5436 (insert (substitute-command-keys
5437 "In this buffer, type \\[choose-completion] to \
5438 select the completion near point.\n\n"))))))
5440 (add-hook 'completion-setup-hook 'completion-setup-function)
5442 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
5443 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
5445 (defun switch-to-completions ()
5446 "Select the completion list window."
5447 (interactive)
5448 ;; Make sure we have a completions window.
5449 (or (get-buffer-window "*Completions*")
5450 (minibuffer-completion-help))
5451 (let ((window (get-buffer-window "*Completions*")))
5452 (when window
5453 (select-window window)
5454 (goto-char (point-min))
5455 (search-forward "\n\n" nil t)
5456 (forward-line 1))))
5458 ;;; Support keyboard commands to turn on various modifiers.
5460 ;; These functions -- which are not commands -- each add one modifier
5461 ;; to the following event.
5463 (defun event-apply-alt-modifier (ignore-prompt)
5464 "\\<function-key-map>Add the Alt modifier to the following event.
5465 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
5466 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
5467 (defun event-apply-super-modifier (ignore-prompt)
5468 "\\<function-key-map>Add the Super modifier to the following event.
5469 For example, type \\[event-apply-super-modifier] & to enter Super-&."
5470 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
5471 (defun event-apply-hyper-modifier (ignore-prompt)
5472 "\\<function-key-map>Add the Hyper modifier to the following event.
5473 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
5474 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
5475 (defun event-apply-shift-modifier (ignore-prompt)
5476 "\\<function-key-map>Add the Shift modifier to the following event.
5477 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
5478 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
5479 (defun event-apply-control-modifier (ignore-prompt)
5480 "\\<function-key-map>Add the Ctrl modifier to the following event.
5481 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
5482 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
5483 (defun event-apply-meta-modifier (ignore-prompt)
5484 "\\<function-key-map>Add the Meta modifier to the following event.
5485 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
5486 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
5488 (defun event-apply-modifier (event symbol lshiftby prefix)
5489 "Apply a modifier flag to event EVENT.
5490 SYMBOL is the name of this modifier, as a symbol.
5491 LSHIFTBY is the numeric value of this modifier, in keyboard events.
5492 PREFIX is the string that represents this modifier in an event type symbol."
5493 (if (numberp event)
5494 (cond ((eq symbol 'control)
5495 (if (and (<= (downcase event) ?z)
5496 (>= (downcase event) ?a))
5497 (- (downcase event) ?a -1)
5498 (if (and (<= (downcase event) ?Z)
5499 (>= (downcase event) ?A))
5500 (- (downcase event) ?A -1)
5501 (logior (lsh 1 lshiftby) event))))
5502 ((eq symbol 'shift)
5503 (if (and (<= (downcase event) ?z)
5504 (>= (downcase event) ?a))
5505 (upcase event)
5506 (logior (lsh 1 lshiftby) event)))
5508 (logior (lsh 1 lshiftby) event)))
5509 (if (memq symbol (event-modifiers event))
5510 event
5511 (let ((event-type (if (symbolp event) event (car event))))
5512 (setq event-type (intern (concat prefix (symbol-name event-type))))
5513 (if (symbolp event)
5514 event-type
5515 (cons event-type (cdr event)))))))
5517 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
5518 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
5519 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
5520 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
5521 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
5522 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
5524 ;;;; Keypad support.
5526 ;; Make the keypad keys act like ordinary typing keys. If people add
5527 ;; bindings for the function key symbols, then those bindings will
5528 ;; override these, so this shouldn't interfere with any existing
5529 ;; bindings.
5531 ;; Also tell read-char how to handle these keys.
5532 (mapc
5533 (lambda (keypad-normal)
5534 (let ((keypad (nth 0 keypad-normal))
5535 (normal (nth 1 keypad-normal)))
5536 (put keypad 'ascii-character normal)
5537 (define-key function-key-map (vector keypad) (vector normal))))
5538 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
5539 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
5540 (kp-space ?\s)
5541 (kp-tab ?\t)
5542 (kp-enter ?\r)
5543 (kp-multiply ?*)
5544 (kp-add ?+)
5545 (kp-separator ?,)
5546 (kp-subtract ?-)
5547 (kp-decimal ?.)
5548 (kp-divide ?/)
5549 (kp-equal ?=)))
5551 ;;;;
5552 ;;;; forking a twin copy of a buffer.
5553 ;;;;
5555 (defvar clone-buffer-hook nil
5556 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
5558 (defvar clone-indirect-buffer-hook nil
5559 "Normal hook to run in the new buffer at the end of `clone-indirect-buffer'.")
5561 (defun clone-process (process &optional newname)
5562 "Create a twin copy of PROCESS.
5563 If NEWNAME is nil, it defaults to PROCESS' name;
5564 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
5565 If PROCESS is associated with a buffer, the new process will be associated
5566 with the current buffer instead.
5567 Returns nil if PROCESS has already terminated."
5568 (setq newname (or newname (process-name process)))
5569 (if (string-match "<[0-9]+>\\'" newname)
5570 (setq newname (substring newname 0 (match-beginning 0))))
5571 (when (memq (process-status process) '(run stop open))
5572 (let* ((process-connection-type (process-tty-name process))
5573 (new-process
5574 (if (memq (process-status process) '(open))
5575 (let ((args (process-contact process t)))
5576 (setq args (plist-put args :name newname))
5577 (setq args (plist-put args :buffer
5578 (if (process-buffer process)
5579 (current-buffer))))
5580 (apply 'make-network-process args))
5581 (apply 'start-process newname
5582 (if (process-buffer process) (current-buffer))
5583 (process-command process)))))
5584 (set-process-query-on-exit-flag
5585 new-process (process-query-on-exit-flag process))
5586 (set-process-inherit-coding-system-flag
5587 new-process (process-inherit-coding-system-flag process))
5588 (set-process-filter new-process (process-filter process))
5589 (set-process-sentinel new-process (process-sentinel process))
5590 (set-process-plist new-process (copy-sequence (process-plist process)))
5591 new-process)))
5593 ;; things to maybe add (currently partly covered by `funcall mode'):
5594 ;; - syntax-table
5595 ;; - overlays
5596 (defun clone-buffer (&optional newname display-flag)
5597 "Create and return a twin copy of the current buffer.
5598 Unlike an indirect buffer, the new buffer can be edited
5599 independently of the old one (if it is not read-only).
5600 NEWNAME is the name of the new buffer. It may be modified by
5601 adding or incrementing <N> at the end as necessary to create a
5602 unique buffer name. If nil, it defaults to the name of the
5603 current buffer, with the proper suffix. If DISPLAY-FLAG is
5604 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
5605 clone a file-visiting buffer, or a buffer whose major mode symbol
5606 has a non-nil `no-clone' property, results in an error.
5608 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
5609 current buffer with appropriate suffix. However, if a prefix
5610 argument is given, then the command prompts for NEWNAME in the
5611 minibuffer.
5613 This runs the normal hook `clone-buffer-hook' in the new buffer
5614 after it has been set up properly in other respects."
5615 (interactive
5616 (progn
5617 (if buffer-file-name
5618 (error "Cannot clone a file-visiting buffer"))
5619 (if (get major-mode 'no-clone)
5620 (error "Cannot clone a buffer in %s mode" mode-name))
5621 (list (if current-prefix-arg
5622 (read-buffer "Name of new cloned buffer: " (current-buffer)))
5623 t)))
5624 (if buffer-file-name
5625 (error "Cannot clone a file-visiting buffer"))
5626 (if (get major-mode 'no-clone)
5627 (error "Cannot clone a buffer in %s mode" mode-name))
5628 (setq newname (or newname (buffer-name)))
5629 (if (string-match "<[0-9]+>\\'" newname)
5630 (setq newname (substring newname 0 (match-beginning 0))))
5631 (let ((buf (current-buffer))
5632 (ptmin (point-min))
5633 (ptmax (point-max))
5634 (pt (point))
5635 (mk (if mark-active (mark t)))
5636 (modified (buffer-modified-p))
5637 (mode major-mode)
5638 (lvars (buffer-local-variables))
5639 (process (get-buffer-process (current-buffer)))
5640 (new (generate-new-buffer (or newname (buffer-name)))))
5641 (save-restriction
5642 (widen)
5643 (with-current-buffer new
5644 (insert-buffer-substring buf)))
5645 (with-current-buffer new
5646 (narrow-to-region ptmin ptmax)
5647 (goto-char pt)
5648 (if mk (set-mark mk))
5649 (set-buffer-modified-p modified)
5651 ;; Clone the old buffer's process, if any.
5652 (when process (clone-process process))
5654 ;; Now set up the major mode.
5655 (funcall mode)
5657 ;; Set up other local variables.
5658 (mapc (lambda (v)
5659 (condition-case () ;in case var is read-only
5660 (if (symbolp v)
5661 (makunbound v)
5662 (set (make-local-variable (car v)) (cdr v)))
5663 (error nil)))
5664 lvars)
5666 ;; Run any hooks (typically set up by the major mode
5667 ;; for cloning to work properly).
5668 (run-hooks 'clone-buffer-hook))
5669 (if display-flag
5670 ;; Presumably the current buffer is shown in the selected frame, so
5671 ;; we want to display the clone elsewhere.
5672 (let ((same-window-regexps nil)
5673 (same-window-buffer-names))
5674 (pop-to-buffer new)))
5675 new))
5678 (defun clone-indirect-buffer (newname display-flag &optional norecord)
5679 "Create an indirect buffer that is a twin copy of the current buffer.
5681 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
5682 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
5683 or if not called with a prefix arg, NEWNAME defaults to the current
5684 buffer's name. The name is modified by adding a `<N>' suffix to it
5685 or by incrementing the N in an existing suffix.
5687 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
5688 This is always done when called interactively.
5690 Optional third arg NORECORD non-nil means do not put this buffer at the
5691 front of the list of recently selected ones."
5692 (interactive
5693 (progn
5694 (if (get major-mode 'no-clone-indirect)
5695 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5696 (list (if current-prefix-arg
5697 (read-buffer "Name of indirect buffer: " (current-buffer)))
5698 t)))
5699 (if (get major-mode 'no-clone-indirect)
5700 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5701 (setq newname (or newname (buffer-name)))
5702 (if (string-match "<[0-9]+>\\'" newname)
5703 (setq newname (substring newname 0 (match-beginning 0))))
5704 (let* ((name (generate-new-buffer-name newname))
5705 (buffer (make-indirect-buffer (current-buffer) name t)))
5706 (with-current-buffer buffer
5707 (run-hooks 'clone-indirect-buffer-hook))
5708 (when display-flag
5709 (pop-to-buffer buffer norecord))
5710 buffer))
5713 (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
5714 "Like `clone-indirect-buffer' but display in another window."
5715 (interactive
5716 (progn
5717 (if (get major-mode 'no-clone-indirect)
5718 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5719 (list (if current-prefix-arg
5720 (read-buffer "Name of indirect buffer: " (current-buffer)))
5721 t)))
5722 (let ((pop-up-windows t))
5723 (clone-indirect-buffer newname display-flag norecord)))
5726 ;;; Handling of Backspace and Delete keys.
5728 (defcustom normal-erase-is-backspace 'maybe
5729 "Set the default behavior of the Delete and Backspace keys.
5731 If set to t, Delete key deletes forward and Backspace key deletes
5732 backward.
5734 If set to nil, both Delete and Backspace keys delete backward.
5736 If set to 'maybe (which is the default), Emacs automatically
5737 selects a behavior. On window systems, the behavior depends on
5738 the keyboard used. If the keyboard has both a Backspace key and
5739 a Delete key, and both are mapped to their usual meanings, the
5740 option's default value is set to t, so that Backspace can be used
5741 to delete backward, and Delete can be used to delete forward.
5743 If not running under a window system, customizing this option
5744 accomplishes a similar effect by mapping C-h, which is usually
5745 generated by the Backspace key, to DEL, and by mapping DEL to C-d
5746 via `keyboard-translate'. The former functionality of C-h is
5747 available on the F1 key. You should probably not use this
5748 setting if you don't have both Backspace, Delete and F1 keys.
5750 Setting this variable with setq doesn't take effect. Programmatically,
5751 call `normal-erase-is-backspace-mode' (which see) instead."
5752 :type '(choice (const :tag "Off" nil)
5753 (const :tag "Maybe" maybe)
5754 (other :tag "On" t))
5755 :group 'editing-basics
5756 :version "21.1"
5757 :set (lambda (symbol value)
5758 ;; The fboundp is because of a problem with :set when
5759 ;; dumping Emacs. It doesn't really matter.
5760 (if (fboundp 'normal-erase-is-backspace-mode)
5761 (normal-erase-is-backspace-mode (or value 0))
5762 (set-default symbol value))))
5764 (defun normal-erase-is-backspace-setup-frame (&optional frame)
5765 "Set up `normal-erase-is-backspace-mode' on FRAME, if necessary."
5766 (unless frame (setq frame (selected-frame)))
5767 (with-selected-frame frame
5768 (unless (terminal-parameter nil 'normal-erase-is-backspace)
5769 (normal-erase-is-backspace-mode
5770 (if (if (eq normal-erase-is-backspace 'maybe)
5771 (and (not noninteractive)
5772 (or (memq system-type '(ms-dos windows-nt))
5773 (eq window-system 'mac)
5774 (and (memq window-system '(x))
5775 (fboundp 'x-backspace-delete-keys-p)
5776 (x-backspace-delete-keys-p))
5777 ;; If the terminal Emacs is running on has erase char
5778 ;; set to ^H, use the Backspace key for deleting
5779 ;; backward, and the Delete key for deleting forward.
5780 (and (null window-system)
5781 (eq tty-erase-char ?\^H))))
5782 normal-erase-is-backspace)
5783 1 0)))))
5785 (defun normal-erase-is-backspace-mode (&optional arg)
5786 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
5788 With numeric arg, turn the mode on if and only if ARG is positive.
5790 On window systems, when this mode is on, Delete is mapped to C-d
5791 and Backspace is mapped to DEL; when this mode is off, both
5792 Delete and Backspace are mapped to DEL. (The remapping goes via
5793 `local-function-key-map', so binding Delete or Backspace in the
5794 global or local keymap will override that.)
5796 In addition, on window systems, the bindings of C-Delete, M-Delete,
5797 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
5798 the global keymap in accordance with the functionality of Delete and
5799 Backspace. For example, if Delete is remapped to C-d, which deletes
5800 forward, C-Delete is bound to `kill-word', but if Delete is remapped
5801 to DEL, which deletes backward, C-Delete is bound to
5802 `backward-kill-word'.
5804 If not running on a window system, a similar effect is accomplished by
5805 remapping C-h (normally produced by the Backspace key) and DEL via
5806 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
5807 to C-d; if it's off, the keys are not remapped.
5809 When not running on a window system, and this mode is turned on, the
5810 former functionality of C-h is available on the F1 key. You should
5811 probably not turn on this mode on a text-only terminal if you don't
5812 have both Backspace, Delete and F1 keys.
5814 See also `normal-erase-is-backspace'."
5815 (interactive "P")
5816 (let ((enabled (or (and arg (> (prefix-numeric-value arg) 0))
5817 (and (not arg)
5818 (not (eq 1 (terminal-parameter
5819 nil 'normal-erase-is-backspace)))))))
5820 (set-terminal-parameter nil 'normal-erase-is-backspace
5821 (if enabled 1 0))
5823 (cond ((or (memq window-system '(x w32 mac pc))
5824 (memq system-type '(ms-dos windows-nt)))
5825 (let* ((bindings
5826 `(([C-delete] [C-backspace])
5827 ([M-delete] [M-backspace])
5828 ([C-M-delete] [C-M-backspace])
5829 (,esc-map
5830 [C-delete] [C-backspace])))
5831 (old-state (lookup-key local-function-key-map [delete])))
5833 (if enabled
5834 (progn
5835 (define-key local-function-key-map [delete] [?\C-d])
5836 (define-key local-function-key-map [kp-delete] [?\C-d])
5837 (define-key local-function-key-map [backspace] [?\C-?]))
5838 (define-key local-function-key-map [delete] [?\C-?])
5839 (define-key local-function-key-map [kp-delete] [?\C-?])
5840 (define-key local-function-key-map [backspace] [?\C-?]))
5842 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5843 (unless (equal old-state (lookup-key local-function-key-map [delete]))
5844 (dolist (binding bindings)
5845 (let ((map global-map))
5846 (when (keymapp (car binding))
5847 (setq map (car binding) binding (cdr binding)))
5848 (let* ((key1 (nth 0 binding))
5849 (key2 (nth 1 binding))
5850 (binding1 (lookup-key map key1))
5851 (binding2 (lookup-key map key2)))
5852 (define-key map key1 binding2)
5853 (define-key map key2 binding1)))))))
5855 (if enabled
5856 (progn
5857 (keyboard-translate ?\C-h ?\C-?)
5858 (keyboard-translate ?\C-? ?\C-d))
5859 (keyboard-translate ?\C-h ?\C-h)
5860 (keyboard-translate ?\C-? ?\C-?))))
5862 (run-hooks 'normal-erase-is-backspace-hook)
5863 (if (interactive-p)
5864 (message "Delete key deletes %s"
5865 (if (terminal-parameter nil 'normal-erase-is-backspace)
5866 "forward" "backward")))))
5868 (defvar vis-mode-saved-buffer-invisibility-spec nil
5869 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5871 (define-minor-mode visible-mode
5872 "Toggle Visible mode.
5873 With argument ARG turn Visible mode on if ARG is positive, otherwise
5874 turn it off.
5876 Enabling Visible mode makes all invisible text temporarily visible.
5877 Disabling Visible mode turns off that effect. Visible mode
5878 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5879 :lighter " Vis"
5880 :group 'editing-basics
5881 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5882 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5883 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5884 (when visible-mode
5885 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5886 buffer-invisibility-spec)
5887 (setq buffer-invisibility-spec nil)))
5889 ;; Minibuffer prompt stuff.
5891 ;(defun minibuffer-prompt-modification (start end)
5892 ; (error "You cannot modify the prompt"))
5895 ;(defun minibuffer-prompt-insertion (start end)
5896 ; (let ((inhibit-modification-hooks t))
5897 ; (delete-region start end)
5898 ; ;; Discard undo information for the text insertion itself
5899 ; ;; and for the text deletion.above.
5900 ; (when (consp buffer-undo-list)
5901 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5902 ; (message "You cannot modify the prompt")))
5905 ;(setq minibuffer-prompt-properties
5906 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5907 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5911 ;;;; Problematic external packages.
5913 ;; rms says this should be done by specifying symbols that define
5914 ;; versions together with bad values. This is therefore not as
5915 ;; flexible as it could be. See the thread:
5916 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00300.html
5917 (defconst bad-packages-alist
5918 ;; Not sure exactly which semantic versions have problems.
5919 ;; Definitely 2.0pre3, probably all 2.0pre's before this.
5920 '((semantic semantic-version "\\`2\\.0pre[1-3]\\'"
5921 "The version of `semantic' loaded does not work in Emacs 22.
5922 It can cause constant high CPU load.
5923 Upgrade to at least Semantic 2.0pre4 (distributed with CEDET 1.0pre4).")
5924 ;; CUA-mode does not work with GNU Emacs version 22.1 and newer.
5925 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
5926 ;; provided the `CUA-mode' feature. Since this is no longer true,
5927 ;; we can warn the user if the `CUA-mode' feature is ever provided.
5928 (CUA-mode t nil
5929 "CUA-mode is now part of the standard GNU Emacs distribution,
5930 so you can now enable CUA via the Options menu or by customizing `cua-mode'.
5932 You have loaded an older version of CUA-mode which does not work
5933 correctly with this version of Emacs. You should remove the old
5934 version and use the one distributed with Emacs."))
5935 "Alist of packages known to cause problems in this version of Emacs.
5936 Each element has the form (PACKAGE SYMBOL REGEXP STRING).
5937 PACKAGE is either a regular expression to match file names, or a
5938 symbol (a feature name); see the documentation of
5939 `after-load-alist', to which this variable adds functions.
5940 SYMBOL is either the name of a string variable, or `t'. Upon
5941 loading PACKAGE, if SYMBOL is t or matches REGEXP, display a
5942 warning using STRING as the message.")
5944 (defun bad-package-check (package)
5945 "Run a check using the element from `bad-packages-alist' matching PACKAGE."
5946 (condition-case nil
5947 (let* ((list (assoc package bad-packages-alist))
5948 (symbol (nth 1 list)))
5949 (and list
5950 (boundp symbol)
5951 (or (eq symbol t)
5952 (and (stringp (setq symbol (eval symbol)))
5953 (string-match (nth 2 list) symbol)))
5954 (display-warning :warning (nth 3 list))))
5955 (error nil)))
5957 (mapc (lambda (elem)
5958 (eval-after-load (car elem) `(bad-package-check ',(car elem))))
5959 bad-packages-alist)
5962 (provide 'simple)
5964 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5965 ;;; simple.el ends here