(number, original-date, add-to-diary-list)
[emacs.git] / lisp / simple.el
blobc140e719d584b3d4caea1178206cdb2106281c09
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.
823 With just \\[universal-argument] as argument, move point in the most recently
824 displayed other buffer, and switch to it. When called from Lisp code,
825 the optional argument BUFFER specifies a buffer to switch to.
827 If there's a number in the buffer at point, it is the default for ARG."
828 (interactive
829 (if (and current-prefix-arg (not (consp current-prefix-arg)))
830 (list (prefix-numeric-value current-prefix-arg))
831 ;; Look for a default, a number in the buffer at point.
832 (let* ((default
833 (save-excursion
834 (skip-chars-backward "0-9")
835 (if (looking-at "[0-9]")
836 (buffer-substring-no-properties
837 (point)
838 (progn (skip-chars-forward "0-9")
839 (point))))))
840 ;; Decide if we're switching buffers.
841 (buffer
842 (if (consp current-prefix-arg)
843 (other-buffer (current-buffer) t)))
844 (buffer-prompt
845 (if buffer
846 (concat " in " (buffer-name buffer))
847 "")))
848 ;; Read the argument, offering that number (if any) as default.
849 (list (read-from-minibuffer (format (if default "Goto line%s (%s): "
850 "Goto line%s: ")
851 buffer-prompt
852 default)
853 nil nil t
854 'minibuffer-history
855 default)
856 buffer))))
857 ;; Switch to the desired buffer, one way or another.
858 (if buffer
859 (let ((window (get-buffer-window buffer)))
860 (if window (select-window window)
861 (switch-to-buffer-other-window buffer))))
862 ;; Move to the specified line number in that buffer.
863 (save-restriction
864 (widen)
865 (goto-char 1)
866 (if (eq selective-display t)
867 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
868 (forward-line (1- arg)))))
870 (defun count-lines-region (start end)
871 "Print number of lines and characters in the region."
872 (interactive "r")
873 (message "Region has %d lines, %d characters"
874 (count-lines start end) (- end start)))
876 (defun what-line ()
877 "Print the current buffer line number and narrowed line number of point."
878 (interactive)
879 (let ((start (point-min))
880 (n (line-number-at-pos)))
881 (if (= start 1)
882 (message "Line %d" n)
883 (save-excursion
884 (save-restriction
885 (widen)
886 (message "line %d (narrowed line %d)"
887 (+ n (line-number-at-pos start) -1) n))))))
889 (defun count-lines (start end)
890 "Return number of lines between START and END.
891 This is usually the number of newlines between them,
892 but can be one more if START is not equal to END
893 and the greater of them is not at the start of a line."
894 (save-excursion
895 (save-restriction
896 (narrow-to-region start end)
897 (goto-char (point-min))
898 (if (eq selective-display t)
899 (save-match-data
900 (let ((done 0))
901 (while (re-search-forward "[\n\C-m]" nil t 40)
902 (setq done (+ 40 done)))
903 (while (re-search-forward "[\n\C-m]" nil t 1)
904 (setq done (+ 1 done)))
905 (goto-char (point-max))
906 (if (and (/= start end)
907 (not (bolp)))
908 (1+ done)
909 done)))
910 (- (buffer-size) (forward-line (buffer-size)))))))
912 (defun line-number-at-pos (&optional pos)
913 "Return (narrowed) buffer line number at position POS.
914 If POS is nil, use current buffer location.
915 Counting starts at (point-min), so the value refers
916 to the contents of the accessible portion of the buffer."
917 (let ((opoint (or pos (point))) start)
918 (save-excursion
919 (goto-char (point-min))
920 (setq start (point))
921 (goto-char opoint)
922 (forward-line 0)
923 (1+ (count-lines start (point))))))
925 (defun what-cursor-position (&optional detail)
926 "Print info on cursor position (on screen and within buffer).
927 Also describe the character after point, and give its character code
928 in octal, decimal and hex.
930 For a non-ASCII multibyte character, also give its encoding in the
931 buffer's selected coding system if the coding system encodes the
932 character safely. If the character is encoded into one byte, that
933 code is shown in hex. If the character is encoded into more than one
934 byte, just \"...\" is shown.
936 In addition, with prefix argument, show details about that character
937 in *Help* buffer. See also the command `describe-char'."
938 (interactive "P")
939 (let* ((char (following-char))
940 (beg (point-min))
941 (end (point-max))
942 (pos (point))
943 (total (buffer-size))
944 (percent (if (> total 50000)
945 ;; Avoid overflow from multiplying by 100!
946 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
947 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
948 (hscroll (if (= (window-hscroll) 0)
950 (format " Hscroll=%d" (window-hscroll))))
951 (col (current-column)))
952 (if (= pos end)
953 (if (or (/= beg 1) (/= end (1+ total)))
954 (message "point=%d of %d (%d%%) <%d-%d> column=%d%s"
955 pos total percent beg end col hscroll)
956 (message "point=%d of %d (EOB) column=%d%s"
957 pos total col hscroll))
958 (let ((coding buffer-file-coding-system)
959 encoded encoding-msg display-prop under-display)
960 (if (or (not coding)
961 (eq (coding-system-type coding) t))
962 (setq coding default-buffer-file-coding-system))
963 (if (eq (char-charset char) 'eight-bit)
964 (setq encoding-msg
965 (format "(%d, #o%o, #x%x, raw-byte)" char char char))
966 ;; Check if the character is displayed with some `display'
967 ;; text property. In that case, set under-display to the
968 ;; buffer substring covered by that property.
969 (setq display-prop (get-text-property pos 'display))
970 (if display-prop
971 (let ((to (or (next-single-property-change pos 'display)
972 (point-max))))
973 (if (< to (+ pos 4))
974 (setq under-display "")
975 (setq under-display "..."
976 to (+ pos 4)))
977 (setq under-display
978 (concat (buffer-substring-no-properties pos to)
979 under-display)))
980 (setq encoded (and (>= char 128) (encode-coding-char char coding))))
981 (setq encoding-msg
982 (if display-prop
983 (if (not (stringp display-prop))
984 (format "(%d, #o%o, #x%x, part of display \"%s\")"
985 char char char under-display)
986 (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")"
987 char char char under-display display-prop))
988 (if encoded
989 (format "(%d, #o%o, #x%x, file %s)"
990 char char char
991 (if (> (length encoded) 1)
992 "..."
993 (encoded-string-description encoded coding)))
994 (format "(%d, #o%o, #x%x)" char char char)))))
995 (if detail
996 ;; We show the detailed information about CHAR.
997 (describe-char (point)))
998 (if (or (/= beg 1) (/= end (1+ total)))
999 (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
1000 (if (< char 256)
1001 (single-key-description char)
1002 (buffer-substring-no-properties (point) (1+ (point))))
1003 encoding-msg pos total percent beg end col hscroll)
1004 (message "Char: %s %s point=%d of %d (%d%%) column=%d%s"
1005 (if enable-multibyte-characters
1006 (if (< char 128)
1007 (single-key-description char)
1008 (buffer-substring-no-properties (point) (1+ (point))))
1009 (single-key-description char))
1010 encoding-msg pos total percent col hscroll))))))
1012 ;; Initialize read-expression-map. It is defined at C level.
1013 (let ((m (make-sparse-keymap)))
1014 (define-key m "\M-\t" 'lisp-complete-symbol)
1015 (set-keymap-parent m minibuffer-local-map)
1016 (setq read-expression-map m))
1018 (defvar read-expression-history nil)
1020 (defvar minibuffer-completing-symbol nil
1021 "Non-nil means completing a Lisp symbol in the minibuffer.")
1023 (defcustom eval-expression-print-level 4
1024 "Value for `print-level' while printing value in `eval-expression'.
1025 A value of nil means no limit."
1026 :group 'lisp
1027 :type '(choice (const :tag "No Limit" nil) integer)
1028 :version "21.1")
1030 (defcustom eval-expression-print-length 12
1031 "Value for `print-length' while printing value in `eval-expression'.
1032 A value of nil means no limit."
1033 :group 'lisp
1034 :type '(choice (const :tag "No Limit" nil) integer)
1035 :version "21.1")
1037 (defcustom eval-expression-debug-on-error t
1038 "If non-nil set `debug-on-error' to t in `eval-expression'.
1039 If nil, don't change the value of `debug-on-error'."
1040 :group 'lisp
1041 :type 'boolean
1042 :version "21.1")
1044 (defun eval-expression-print-format (value)
1045 "Format VALUE as a result of evaluated expression.
1046 Return a formatted string which is displayed in the echo area
1047 in addition to the value printed by prin1 in functions which
1048 display the result of expression evaluation."
1049 (if (and (integerp value)
1050 (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
1051 (eq this-command last-command)
1052 (if (boundp 'edebug-active) edebug-active)))
1053 (let ((char-string
1054 (if (or (if (boundp 'edebug-active) edebug-active)
1055 (memq this-command '(eval-last-sexp eval-print-last-sexp)))
1056 (prin1-char value))))
1057 (if char-string
1058 (format " (#o%o, #x%x, %s)" value value char-string)
1059 (format " (#o%o, #x%x)" value value)))))
1061 ;; We define this, rather than making `eval' interactive,
1062 ;; for the sake of completion of names like eval-region, eval-buffer.
1063 (defun eval-expression (eval-expression-arg
1064 &optional eval-expression-insert-value)
1065 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
1066 Value is also consed on to front of the variable `values'.
1067 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
1068 insert the result into the current buffer instead of printing it in
1069 the echo area.
1071 If `eval-expression-debug-on-error' is non-nil, which is the default,
1072 this command arranges for all errors to enter the debugger."
1073 (interactive
1074 (list (let ((minibuffer-completing-symbol t))
1075 (read-from-minibuffer "Eval: "
1076 nil read-expression-map t
1077 'read-expression-history))
1078 current-prefix-arg))
1080 (if (null eval-expression-debug-on-error)
1081 (setq values (cons (eval eval-expression-arg) values))
1082 (let ((old-value (make-symbol "t")) new-value)
1083 ;; Bind debug-on-error to something unique so that we can
1084 ;; detect when evaled code changes it.
1085 (let ((debug-on-error old-value))
1086 (setq values (cons (eval eval-expression-arg) values))
1087 (setq new-value debug-on-error))
1088 ;; If evaled code has changed the value of debug-on-error,
1089 ;; propagate that change to the global binding.
1090 (unless (eq old-value new-value)
1091 (setq debug-on-error new-value))))
1093 (let ((print-length eval-expression-print-length)
1094 (print-level eval-expression-print-level))
1095 (if eval-expression-insert-value
1096 (with-no-warnings
1097 (let ((standard-output (current-buffer)))
1098 (prin1 (car values))))
1099 (prog1
1100 (prin1 (car values) t)
1101 (let ((str (eval-expression-print-format (car values))))
1102 (if str (princ str t)))))))
1104 (defun edit-and-eval-command (prompt command)
1105 "Prompting with PROMPT, let user edit COMMAND and eval result.
1106 COMMAND is a Lisp expression. Let user edit that expression in
1107 the minibuffer, then read and evaluate the result."
1108 (let ((command
1109 (let ((print-level nil)
1110 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1111 (unwind-protect
1112 (read-from-minibuffer prompt
1113 (prin1-to-string command)
1114 read-expression-map t
1115 'command-history)
1116 ;; If command was added to command-history as a string,
1117 ;; get rid of that. We want only evaluable expressions there.
1118 (if (stringp (car command-history))
1119 (setq command-history (cdr command-history)))))))
1121 ;; If command to be redone does not match front of history,
1122 ;; add it to the history.
1123 (or (equal command (car command-history))
1124 (setq command-history (cons command command-history)))
1125 (eval command)))
1127 (defun repeat-complex-command (arg)
1128 "Edit and re-evaluate last complex command, or ARGth from last.
1129 A complex command is one which used the minibuffer.
1130 The command is placed in the minibuffer as a Lisp form for editing.
1131 The result is executed, repeating the command as changed.
1132 If the command has been changed or is not the most recent previous command
1133 it is added to the front of the command history.
1134 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
1135 to get different commands to edit and resubmit."
1136 (interactive "p")
1137 (let ((elt (nth (1- arg) command-history))
1138 newcmd)
1139 (if elt
1140 (progn
1141 (setq newcmd
1142 (let ((print-level nil)
1143 (minibuffer-history-position arg)
1144 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1145 (unwind-protect
1146 (read-from-minibuffer
1147 "Redo: " (prin1-to-string elt) read-expression-map t
1148 (cons 'command-history arg))
1150 ;; If command was added to command-history as a
1151 ;; string, get rid of that. We want only
1152 ;; evaluable expressions there.
1153 (if (stringp (car command-history))
1154 (setq command-history (cdr command-history))))))
1156 ;; If command to be redone does not match front of history,
1157 ;; add it to the history.
1158 (or (equal newcmd (car command-history))
1159 (setq command-history (cons newcmd command-history)))
1160 (eval newcmd))
1161 (if command-history
1162 (error "Argument %d is beyond length of command history" arg)
1163 (error "There are no previous complex commands to repeat")))))
1165 (defvar minibuffer-history nil
1166 "Default minibuffer history list.
1167 This is used for all minibuffer input
1168 except when an alternate history list is specified.
1170 Maximum length of the history list is determined by the value
1171 of `history-length', which see.")
1172 (defvar minibuffer-history-sexp-flag nil
1173 "Control whether history list elements are expressions or strings.
1174 If the value of this variable equals current minibuffer depth,
1175 they are expressions; otherwise they are strings.
1176 \(That convention is designed to do the right thing for
1177 recursive uses of the minibuffer.)")
1178 (setq minibuffer-history-variable 'minibuffer-history)
1179 (setq minibuffer-history-position nil) ;; Defvar is in C code.
1180 (defvar minibuffer-history-search-history nil)
1182 (defvar minibuffer-text-before-history nil
1183 "Text that was in this minibuffer before any history commands.
1184 This is nil if there have not yet been any history commands
1185 in this use of the minibuffer.")
1187 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
1189 (defun minibuffer-history-initialize ()
1190 (setq minibuffer-text-before-history nil))
1192 (defun minibuffer-avoid-prompt (new old)
1193 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1194 (constrain-to-field nil (point-max)))
1196 (defcustom minibuffer-history-case-insensitive-variables nil
1197 "*Minibuffer history variables for which matching should ignore case.
1198 If a history variable is a member of this list, then the
1199 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1200 commands ignore case when searching it, regardless of `case-fold-search'."
1201 :type '(repeat variable)
1202 :group 'minibuffer)
1204 (defun previous-matching-history-element (regexp n)
1205 "Find the previous history element that matches REGEXP.
1206 \(Previous history elements refer to earlier actions.)
1207 With prefix argument N, search for Nth previous match.
1208 If N is negative, find the next or Nth next match.
1209 Normally, history elements are matched case-insensitively if
1210 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1211 makes the search case-sensitive.
1212 See also `minibuffer-history-case-insensitive-variables'."
1213 (interactive
1214 (let* ((enable-recursive-minibuffers t)
1215 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1217 minibuffer-local-map
1219 'minibuffer-history-search-history
1220 (car minibuffer-history-search-history))))
1221 ;; Use the last regexp specified, by default, if input is empty.
1222 (list (if (string= regexp "")
1223 (if minibuffer-history-search-history
1224 (car minibuffer-history-search-history)
1225 (error "No previous history search regexp"))
1226 regexp)
1227 (prefix-numeric-value current-prefix-arg))))
1228 (unless (zerop n)
1229 (if (and (zerop minibuffer-history-position)
1230 (null minibuffer-text-before-history))
1231 (setq minibuffer-text-before-history
1232 (minibuffer-contents-no-properties)))
1233 (let ((history (symbol-value minibuffer-history-variable))
1234 (case-fold-search
1235 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
1236 ;; On some systems, ignore case for file names.
1237 (if (memq minibuffer-history-variable
1238 minibuffer-history-case-insensitive-variables)
1240 ;; Respect the user's setting for case-fold-search:
1241 case-fold-search)
1242 nil))
1243 prevpos
1244 match-string
1245 match-offset
1246 (pos minibuffer-history-position))
1247 (while (/= n 0)
1248 (setq prevpos pos)
1249 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1250 (when (= pos prevpos)
1251 (error (if (= pos 1)
1252 "No later matching history item"
1253 "No earlier matching history item")))
1254 (setq match-string
1255 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
1256 (let ((print-level nil))
1257 (prin1-to-string (nth (1- pos) history)))
1258 (nth (1- pos) history)))
1259 (setq match-offset
1260 (if (< n 0)
1261 (and (string-match regexp match-string)
1262 (match-end 0))
1263 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
1264 (match-beginning 1))))
1265 (when match-offset
1266 (setq n (+ n (if (< n 0) 1 -1)))))
1267 (setq minibuffer-history-position pos)
1268 (goto-char (point-max))
1269 (delete-minibuffer-contents)
1270 (insert match-string)
1271 (goto-char (+ (minibuffer-prompt-end) match-offset))))
1272 (if (memq (car (car command-history)) '(previous-matching-history-element
1273 next-matching-history-element))
1274 (setq command-history (cdr command-history))))
1276 (defun next-matching-history-element (regexp n)
1277 "Find the next history element that matches REGEXP.
1278 \(The next history element refers to a more recent action.)
1279 With prefix argument N, search for Nth next match.
1280 If N is negative, find the previous or Nth previous match.
1281 Normally, history elements are matched case-insensitively if
1282 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1283 makes the search case-sensitive."
1284 (interactive
1285 (let* ((enable-recursive-minibuffers t)
1286 (regexp (read-from-minibuffer "Next element matching (regexp): "
1288 minibuffer-local-map
1290 'minibuffer-history-search-history
1291 (car minibuffer-history-search-history))))
1292 ;; Use the last regexp specified, by default, if input is empty.
1293 (list (if (string= regexp "")
1294 (if minibuffer-history-search-history
1295 (car minibuffer-history-search-history)
1296 (error "No previous history search regexp"))
1297 regexp)
1298 (prefix-numeric-value current-prefix-arg))))
1299 (previous-matching-history-element regexp (- n)))
1301 (defvar minibuffer-temporary-goal-position nil)
1303 (defun goto-history-element (nabs)
1304 "Puts element of the minibuffer history in the minibuffer.
1305 The argument NABS specifies the absolute history position."
1306 (interactive "p")
1307 (let ((minimum (if minibuffer-default
1308 (- (if (listp minibuffer-default)
1309 (length minibuffer-default)
1312 elt minibuffer-returned-to-present)
1313 (if (and (zerop minibuffer-history-position)
1314 (null minibuffer-text-before-history))
1315 (setq minibuffer-text-before-history
1316 (minibuffer-contents-no-properties)))
1317 (if (< nabs minimum)
1318 (if minibuffer-default
1319 (error "End of history; no next item")
1320 (error "End of history; no default available")))
1321 (if (> nabs (length (symbol-value minibuffer-history-variable)))
1322 (error "Beginning of history; no preceding item"))
1323 (unless (memq last-command '(next-history-element
1324 previous-history-element))
1325 (let ((prompt-end (minibuffer-prompt-end)))
1326 (set (make-local-variable 'minibuffer-temporary-goal-position)
1327 (cond ((<= (point) prompt-end) prompt-end)
1328 ((eobp) nil)
1329 (t (point))))))
1330 (goto-char (point-max))
1331 (delete-minibuffer-contents)
1332 (setq minibuffer-history-position nabs)
1333 (cond ((< nabs 0)
1334 (setq elt (if (listp minibuffer-default)
1335 (nth (1- (abs nabs)) minibuffer-default)
1336 minibuffer-default)))
1337 ((= nabs 0)
1338 (setq elt (or minibuffer-text-before-history ""))
1339 (setq minibuffer-returned-to-present t)
1340 (setq minibuffer-text-before-history nil))
1341 (t (setq elt (nth (1- minibuffer-history-position)
1342 (symbol-value minibuffer-history-variable)))))
1343 (insert
1344 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
1345 (not minibuffer-returned-to-present))
1346 (let ((print-level nil))
1347 (prin1-to-string elt))
1348 elt))
1349 (goto-char (or minibuffer-temporary-goal-position (point-max)))))
1351 (defun next-history-element (n)
1352 "Puts next element of the minibuffer history in the minibuffer.
1353 With argument N, it uses the Nth following element."
1354 (interactive "p")
1355 (or (zerop n)
1356 (goto-history-element (- minibuffer-history-position n))))
1358 (defun previous-history-element (n)
1359 "Puts previous element of the minibuffer history in the minibuffer.
1360 With argument N, it uses the Nth previous element."
1361 (interactive "p")
1362 (or (zerop n)
1363 (goto-history-element (+ minibuffer-history-position n))))
1365 (defun next-complete-history-element (n)
1366 "Get next history element which completes the minibuffer before the point.
1367 The contents of the minibuffer after the point are deleted, and replaced
1368 by the new completion."
1369 (interactive "p")
1370 (let ((point-at-start (point)))
1371 (next-matching-history-element
1372 (concat
1373 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1375 ;; next-matching-history-element always puts us at (point-min).
1376 ;; Move to the position we were at before changing the buffer contents.
1377 ;; This is still sensical, because the text before point has not changed.
1378 (goto-char point-at-start)))
1380 (defun previous-complete-history-element (n)
1382 Get previous history element which completes the minibuffer before the point.
1383 The contents of the minibuffer after the point are deleted, and replaced
1384 by the new completion."
1385 (interactive "p")
1386 (next-complete-history-element (- n)))
1388 ;; For compatibility with the old subr of the same name.
1389 (defun minibuffer-prompt-width ()
1390 "Return the display width of the minibuffer prompt.
1391 Return 0 if current buffer is not a minibuffer."
1392 ;; Return the width of everything before the field at the end of
1393 ;; the buffer; this should be 0 for normal buffers.
1394 (1- (minibuffer-prompt-end)))
1396 ;; isearch minibuffer history
1397 (add-hook 'minibuffer-setup-hook 'minibuffer-history-isearch-setup)
1399 (defvar minibuffer-history-isearch-message-overlay)
1400 (make-variable-buffer-local 'minibuffer-history-isearch-message-overlay)
1402 (defun minibuffer-history-isearch-setup ()
1403 "Set up a minibuffer for using isearch to search the minibuffer history.
1404 Intended to be added to `minibuffer-setup-hook'."
1405 (set (make-local-variable 'isearch-search-fun-function)
1406 'minibuffer-history-isearch-search)
1407 (set (make-local-variable 'isearch-message-function)
1408 'minibuffer-history-isearch-message)
1409 (set (make-local-variable 'isearch-wrap-function)
1410 'minibuffer-history-isearch-wrap)
1411 (set (make-local-variable 'isearch-push-state-function)
1412 'minibuffer-history-isearch-push-state)
1413 (add-hook 'isearch-mode-end-hook 'minibuffer-history-isearch-end nil t))
1415 (defun minibuffer-history-isearch-end ()
1416 "Clean up the minibuffer after terminating isearch in the minibuffer."
1417 (if minibuffer-history-isearch-message-overlay
1418 (delete-overlay minibuffer-history-isearch-message-overlay)))
1420 (defun minibuffer-history-isearch-search ()
1421 "Return the proper search function, for isearch in minibuffer history."
1422 (cond
1423 (isearch-word
1424 (if isearch-forward 'word-search-forward 'word-search-backward))
1426 (lambda (string bound noerror)
1427 (let ((search-fun
1428 ;; Use standard functions to search within minibuffer text
1429 (cond
1430 (isearch-regexp
1431 (if isearch-forward 're-search-forward 're-search-backward))
1433 (if isearch-forward 'search-forward 'search-backward))))
1434 found)
1435 ;; Avoid lazy-highlighting matches in the minibuffer prompt when
1436 ;; searching forward. Lazy-highlight calls this lambda with the
1437 ;; bound arg, so skip the minibuffer prompt.
1438 (if (and bound isearch-forward (< (point) (minibuffer-prompt-end)))
1439 (goto-char (minibuffer-prompt-end)))
1441 ;; 1. First try searching in the initial minibuffer text
1442 (funcall search-fun string
1443 (if isearch-forward bound (minibuffer-prompt-end))
1444 noerror)
1445 ;; 2. If the above search fails, start putting next/prev history
1446 ;; elements in the minibuffer successively, and search the string
1447 ;; in them. Do this only when bound is nil (i.e. not while
1448 ;; lazy-highlighting search strings in the current minibuffer text).
1449 (unless bound
1450 (condition-case nil
1451 (progn
1452 (while (not found)
1453 (cond (isearch-forward
1454 (next-history-element 1)
1455 (goto-char (minibuffer-prompt-end)))
1457 (previous-history-element 1)
1458 (goto-char (point-max))))
1459 (setq isearch-barrier (point) isearch-opoint (point))
1460 ;; After putting the next/prev history element, search
1461 ;; the string in them again, until next-history-element
1462 ;; or previous-history-element raises an error at the
1463 ;; beginning/end of history.
1464 (setq found (funcall search-fun string
1465 (unless isearch-forward
1466 ;; For backward search, don't search
1467 ;; in the minibuffer prompt
1468 (minibuffer-prompt-end))
1469 noerror)))
1470 ;; Return point of the new search result
1471 (point))
1472 ;; Return nil when next(prev)-history-element fails
1473 (error nil)))))))))
1475 (defun minibuffer-history-isearch-message (&optional c-q-hack ellipsis)
1476 "Display the minibuffer history search prompt.
1477 If there are no search errors, this function displays an overlay with
1478 the isearch prompt which replaces the original minibuffer prompt.
1479 Otherwise, it displays the standard isearch message returned from
1480 `isearch-message'."
1481 (if (not (and (minibufferp) isearch-success (not isearch-error)))
1482 ;; Use standard function `isearch-message' when not in the minibuffer,
1483 ;; or search fails, or has an error (like incomplete regexp).
1484 ;; This function overwrites minibuffer text with isearch message,
1485 ;; so it's possible to see what is wrong in the search string.
1486 (isearch-message c-q-hack ellipsis)
1487 ;; Otherwise, put the overlay with the standard isearch prompt over
1488 ;; the initial minibuffer prompt.
1489 (if (overlayp minibuffer-history-isearch-message-overlay)
1490 (move-overlay minibuffer-history-isearch-message-overlay
1491 (point-min) (minibuffer-prompt-end))
1492 (setq minibuffer-history-isearch-message-overlay
1493 (make-overlay (point-min) (minibuffer-prompt-end)))
1494 (overlay-put minibuffer-history-isearch-message-overlay 'evaporate t))
1495 (overlay-put minibuffer-history-isearch-message-overlay
1496 'display (isearch-message-prefix c-q-hack ellipsis))
1497 ;; And clear any previous isearch message.
1498 (message "")))
1500 (defun minibuffer-history-isearch-wrap ()
1501 "Wrap the minibuffer history search when search is failed.
1502 Move point to the first history element for a forward search,
1503 or to the last history element for a backward search."
1504 (unless isearch-word
1505 ;; When `minibuffer-history-isearch-search' fails on reaching the
1506 ;; beginning/end of the history, wrap the search to the first/last
1507 ;; minibuffer history element.
1508 (if isearch-forward
1509 (goto-history-element (length (symbol-value minibuffer-history-variable)))
1510 (goto-history-element 0))
1511 (setq isearch-success t))
1512 (goto-char (if isearch-forward (minibuffer-prompt-end) (point-max))))
1514 (defun minibuffer-history-isearch-push-state ()
1515 "Save a function restoring the state of minibuffer history search.
1516 Save `minibuffer-history-position' to the additional state parameter
1517 in the search status stack."
1518 `(lambda (cmd)
1519 (minibuffer-history-isearch-pop-state cmd ,minibuffer-history-position)))
1521 (defun minibuffer-history-isearch-pop-state (cmd hist-pos)
1522 "Restore the minibuffer history search state.
1523 Go to the history element by the absolute history position `hist-pos'."
1524 (goto-history-element hist-pos))
1527 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1528 (defalias 'advertised-undo 'undo)
1530 (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t)
1531 "Table mapping redo records to the corresponding undo one.
1532 A redo record for undo-in-region maps to t.
1533 A redo record for ordinary undo maps to the following (earlier) undo.")
1535 (defvar undo-in-region nil
1536 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1538 (defvar undo-no-redo nil
1539 "If t, `undo' doesn't go through redo entries.")
1541 (defvar pending-undo-list nil
1542 "Within a run of consecutive undo commands, list remaining to be undone.
1543 If t, we undid all the way to the end of it.")
1545 (defun undo (&optional arg)
1546 "Undo some previous changes.
1547 Repeat this command to undo more changes.
1548 A numeric argument serves as a repeat count.
1550 In Transient Mark mode when the mark is active, only undo changes within
1551 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1552 as an argument limits undo to changes within the current region."
1553 (interactive "*P")
1554 ;; Make last-command indicate for the next command that this was an undo.
1555 ;; That way, another undo will undo more.
1556 ;; If we get to the end of the undo history and get an error,
1557 ;; another undo command will find the undo history empty
1558 ;; and will get another error. To begin undoing the undos,
1559 ;; you must type some other command.
1560 (let ((modified (buffer-modified-p))
1561 (recent-save (recent-auto-save-p))
1562 message)
1563 ;; If we get an error in undo-start,
1564 ;; the next command should not be a "consecutive undo".
1565 ;; So set `this-command' to something other than `undo'.
1566 (setq this-command 'undo-start)
1568 (unless (and (eq last-command 'undo)
1569 (or (eq pending-undo-list t)
1570 ;; If something (a timer or filter?) changed the buffer
1571 ;; since the previous command, don't continue the undo seq.
1572 (let ((list buffer-undo-list))
1573 (while (eq (car list) nil)
1574 (setq list (cdr list)))
1575 ;; If the last undo record made was made by undo
1576 ;; it shows nothing else happened in between.
1577 (gethash list undo-equiv-table))))
1578 (setq undo-in-region
1579 (if transient-mark-mode mark-active (and arg (not (numberp arg)))))
1580 (if undo-in-region
1581 (undo-start (region-beginning) (region-end))
1582 (undo-start))
1583 ;; get rid of initial undo boundary
1584 (undo-more 1))
1585 ;; If we got this far, the next command should be a consecutive undo.
1586 (setq this-command 'undo)
1587 ;; Check to see whether we're hitting a redo record, and if
1588 ;; so, ask the user whether she wants to skip the redo/undo pair.
1589 (let ((equiv (gethash pending-undo-list undo-equiv-table)))
1590 (or (eq (selected-window) (minibuffer-window))
1591 (setq message (if undo-in-region
1592 (if equiv "Redo in region!" "Undo in region!")
1593 (if equiv "Redo!" "Undo!"))))
1594 (when (and (consp equiv) undo-no-redo)
1595 ;; The equiv entry might point to another redo record if we have done
1596 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1597 (while (let ((next (gethash equiv undo-equiv-table)))
1598 (if next (setq equiv next))))
1599 (setq pending-undo-list equiv)))
1600 (undo-more
1601 (if (or transient-mark-mode (numberp arg))
1602 (prefix-numeric-value arg)
1604 ;; Record the fact that the just-generated undo records come from an
1605 ;; undo operation--that is, they are redo records.
1606 ;; In the ordinary case (not within a region), map the redo
1607 ;; record to the following undos.
1608 ;; I don't know how to do that in the undo-in-region case.
1609 (puthash buffer-undo-list
1610 (if undo-in-region t pending-undo-list)
1611 undo-equiv-table)
1612 ;; Don't specify a position in the undo record for the undo command.
1613 ;; Instead, undoing this should move point to where the change is.
1614 (let ((tail buffer-undo-list)
1615 (prev nil))
1616 (while (car tail)
1617 (when (integerp (car tail))
1618 (let ((pos (car tail)))
1619 (if prev
1620 (setcdr prev (cdr tail))
1621 (setq buffer-undo-list (cdr tail)))
1622 (setq tail (cdr tail))
1623 (while (car tail)
1624 (if (eq pos (car tail))
1625 (if prev
1626 (setcdr prev (cdr tail))
1627 (setq buffer-undo-list (cdr tail)))
1628 (setq prev tail))
1629 (setq tail (cdr tail)))
1630 (setq tail nil)))
1631 (setq prev tail tail (cdr tail))))
1632 ;; Record what the current undo list says,
1633 ;; so the next command can tell if the buffer was modified in between.
1634 (and modified (not (buffer-modified-p))
1635 (delete-auto-save-file-if-necessary recent-save))
1636 ;; Display a message announcing success.
1637 (if message
1638 (message "%s" message))))
1640 (defun buffer-disable-undo (&optional buffer)
1641 "Make BUFFER stop keeping undo information.
1642 No argument or nil as argument means do this for the current buffer."
1643 (interactive)
1644 (with-current-buffer (if buffer (get-buffer buffer) (current-buffer))
1645 (setq buffer-undo-list t)))
1647 (defun undo-only (&optional arg)
1648 "Undo some previous changes.
1649 Repeat this command to undo more changes.
1650 A numeric argument serves as a repeat count.
1651 Contrary to `undo', this will not redo a previous undo."
1652 (interactive "*p")
1653 (let ((undo-no-redo t)) (undo arg)))
1655 (defvar undo-in-progress nil
1656 "Non-nil while performing an undo.
1657 Some change-hooks test this variable to do something different.")
1659 (defun undo-more (n)
1660 "Undo back N undo-boundaries beyond what was already undone recently.
1661 Call `undo-start' to get ready to undo recent changes,
1662 then call `undo-more' one or more times to undo them."
1663 (or (listp pending-undo-list)
1664 (error (concat "No further undo information"
1665 (and undo-in-region " for region"))))
1666 (let ((undo-in-progress t))
1667 (setq pending-undo-list (primitive-undo n pending-undo-list))
1668 (if (null pending-undo-list)
1669 (setq pending-undo-list t))))
1671 ;; Deep copy of a list
1672 (defun undo-copy-list (list)
1673 "Make a copy of undo list LIST."
1674 (mapcar 'undo-copy-list-1 list))
1676 (defun undo-copy-list-1 (elt)
1677 (if (consp elt)
1678 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1679 elt))
1681 (defun undo-start (&optional beg end)
1682 "Set `pending-undo-list' to the front of the undo list.
1683 The next call to `undo-more' will undo the most recently made change.
1684 If BEG and END are specified, then only undo elements
1685 that apply to text between BEG and END are used; other undo elements
1686 are ignored. If BEG and END are nil, all undo elements are used."
1687 (if (eq buffer-undo-list t)
1688 (error "No undo information in this buffer"))
1689 (setq pending-undo-list
1690 (if (and beg end (not (= beg end)))
1691 (undo-make-selective-list (min beg end) (max beg end))
1692 buffer-undo-list)))
1694 (defvar undo-adjusted-markers)
1696 (defun undo-make-selective-list (start end)
1697 "Return a list of undo elements for the region START to END.
1698 The elements come from `buffer-undo-list', but we keep only
1699 the elements inside this region, and discard those outside this region.
1700 If we find an element that crosses an edge of this region,
1701 we stop and ignore all further elements."
1702 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1703 (undo-list (list nil))
1704 undo-adjusted-markers
1705 some-rejected
1706 undo-elt undo-elt temp-undo-list delta)
1707 (while undo-list-copy
1708 (setq undo-elt (car undo-list-copy))
1709 (let ((keep-this
1710 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1711 ;; This is a "was unmodified" element.
1712 ;; Keep it if we have kept everything thus far.
1713 (not some-rejected))
1715 (undo-elt-in-region undo-elt start end)))))
1716 (if keep-this
1717 (progn
1718 (setq end (+ end (cdr (undo-delta undo-elt))))
1719 ;; Don't put two nils together in the list
1720 (if (not (and (eq (car undo-list) nil)
1721 (eq undo-elt nil)))
1722 (setq undo-list (cons undo-elt undo-list))))
1723 (if (undo-elt-crosses-region undo-elt start end)
1724 (setq undo-list-copy nil)
1725 (setq some-rejected t)
1726 (setq temp-undo-list (cdr undo-list-copy))
1727 (setq delta (undo-delta undo-elt))
1729 (when (/= (cdr delta) 0)
1730 (let ((position (car delta))
1731 (offset (cdr delta)))
1733 ;; Loop down the earlier events adjusting their buffer
1734 ;; positions to reflect the fact that a change to the buffer
1735 ;; isn't being undone. We only need to process those element
1736 ;; types which undo-elt-in-region will return as being in
1737 ;; the region since only those types can ever get into the
1738 ;; output
1740 (while temp-undo-list
1741 (setq undo-elt (car temp-undo-list))
1742 (cond ((integerp undo-elt)
1743 (if (>= undo-elt position)
1744 (setcar temp-undo-list (- undo-elt offset))))
1745 ((atom undo-elt) nil)
1746 ((stringp (car undo-elt))
1747 ;; (TEXT . POSITION)
1748 (let ((text-pos (abs (cdr undo-elt)))
1749 (point-at-end (< (cdr undo-elt) 0 )))
1750 (if (>= text-pos position)
1751 (setcdr undo-elt (* (if point-at-end -1 1)
1752 (- text-pos offset))))))
1753 ((integerp (car undo-elt))
1754 ;; (BEGIN . END)
1755 (when (>= (car undo-elt) position)
1756 (setcar undo-elt (- (car undo-elt) offset))
1757 (setcdr undo-elt (- (cdr undo-elt) offset))))
1758 ((null (car undo-elt))
1759 ;; (nil PROPERTY VALUE BEG . END)
1760 (let ((tail (nthcdr 3 undo-elt)))
1761 (when (>= (car tail) position)
1762 (setcar tail (- (car tail) offset))
1763 (setcdr tail (- (cdr tail) offset))))))
1764 (setq temp-undo-list (cdr temp-undo-list))))))))
1765 (setq undo-list-copy (cdr undo-list-copy)))
1766 (nreverse undo-list)))
1768 (defun undo-elt-in-region (undo-elt start end)
1769 "Determine whether UNDO-ELT falls inside the region START ... END.
1770 If it crosses the edge, we return nil."
1771 (cond ((integerp undo-elt)
1772 (and (>= undo-elt start)
1773 (<= undo-elt end)))
1774 ((eq undo-elt nil)
1776 ((atom undo-elt)
1777 nil)
1778 ((stringp (car undo-elt))
1779 ;; (TEXT . POSITION)
1780 (and (>= (abs (cdr undo-elt)) start)
1781 (< (abs (cdr undo-elt)) end)))
1782 ((and (consp undo-elt) (markerp (car undo-elt)))
1783 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1784 ;; See if MARKER is inside the region.
1785 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1786 (unless alist-elt
1787 (setq alist-elt (cons (car undo-elt)
1788 (marker-position (car undo-elt))))
1789 (setq undo-adjusted-markers
1790 (cons alist-elt undo-adjusted-markers)))
1791 (and (cdr alist-elt)
1792 (>= (cdr alist-elt) start)
1793 (<= (cdr alist-elt) end))))
1794 ((null (car undo-elt))
1795 ;; (nil PROPERTY VALUE BEG . END)
1796 (let ((tail (nthcdr 3 undo-elt)))
1797 (and (>= (car tail) start)
1798 (<= (cdr tail) end))))
1799 ((integerp (car undo-elt))
1800 ;; (BEGIN . END)
1801 (and (>= (car undo-elt) start)
1802 (<= (cdr undo-elt) end)))))
1804 (defun undo-elt-crosses-region (undo-elt start end)
1805 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1806 This assumes we have already decided that UNDO-ELT
1807 is not *inside* the region START...END."
1808 (cond ((atom undo-elt) nil)
1809 ((null (car undo-elt))
1810 ;; (nil PROPERTY VALUE BEG . END)
1811 (let ((tail (nthcdr 3 undo-elt)))
1812 (and (< (car tail) end)
1813 (> (cdr tail) start))))
1814 ((integerp (car undo-elt))
1815 ;; (BEGIN . END)
1816 (and (< (car undo-elt) end)
1817 (> (cdr undo-elt) start)))))
1819 ;; Return the first affected buffer position and the delta for an undo element
1820 ;; delta is defined as the change in subsequent buffer positions if we *did*
1821 ;; the undo.
1822 (defun undo-delta (undo-elt)
1823 (if (consp undo-elt)
1824 (cond ((stringp (car undo-elt))
1825 ;; (TEXT . POSITION)
1826 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1827 ((integerp (car undo-elt))
1828 ;; (BEGIN . END)
1829 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1831 '(0 . 0)))
1832 '(0 . 0)))
1834 (defcustom undo-ask-before-discard nil
1835 "If non-nil ask about discarding undo info for the current command.
1836 Normally, Emacs discards the undo info for the current command if
1837 it exceeds `undo-outer-limit'. But if you set this option
1838 non-nil, it asks in the echo area whether to discard the info.
1839 If you answer no, there is a slight risk that Emacs might crash, so
1840 only do it if you really want to undo the command.
1842 This option is mainly intended for debugging. You have to be
1843 careful if you use it for other purposes. Garbage collection is
1844 inhibited while the question is asked, meaning that Emacs might
1845 leak memory. So you should make sure that you do not wait
1846 excessively long before answering the question."
1847 :type 'boolean
1848 :group 'undo
1849 :version "22.1")
1851 (defvar undo-extra-outer-limit nil
1852 "If non-nil, an extra level of size that's ok in an undo item.
1853 We don't ask the user about truncating the undo list until the
1854 current item gets bigger than this amount.
1856 This variable only matters if `undo-ask-before-discard' is non-nil.")
1857 (make-variable-buffer-local 'undo-extra-outer-limit)
1859 ;; When the first undo batch in an undo list is longer than
1860 ;; undo-outer-limit, this function gets called to warn the user that
1861 ;; the undo info for the current command was discarded. Garbage
1862 ;; collection is inhibited around the call, so it had better not do a
1863 ;; lot of consing.
1864 (setq undo-outer-limit-function 'undo-outer-limit-truncate)
1865 (defun undo-outer-limit-truncate (size)
1866 (if undo-ask-before-discard
1867 (when (or (null undo-extra-outer-limit)
1868 (> size undo-extra-outer-limit))
1869 ;; Don't ask the question again unless it gets even bigger.
1870 ;; This applies, in particular, if the user quits from the question.
1871 ;; Such a quit quits out of GC, but something else will call GC
1872 ;; again momentarily. It will call this function again,
1873 ;; but we don't want to ask the question again.
1874 (setq undo-extra-outer-limit (+ size 50000))
1875 (if (let (use-dialog-box track-mouse executing-kbd-macro )
1876 (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? "
1877 (buffer-name) size)))
1878 (progn (setq buffer-undo-list nil)
1879 (setq undo-extra-outer-limit nil)
1881 nil))
1882 (display-warning '(undo discard-info)
1883 (concat
1884 (format "Buffer `%s' undo info was %d bytes long.\n"
1885 (buffer-name) size)
1886 "The undo info was discarded because it exceeded \
1887 `undo-outer-limit'.
1889 This is normal if you executed a command that made a huge change
1890 to the buffer. In that case, to prevent similar problems in the
1891 future, set `undo-outer-limit' to a value that is large enough to
1892 cover the maximum size of normal changes you expect a single
1893 command to make, but not so large that it might exceed the
1894 maximum memory allotted to Emacs.
1896 If you did not execute any such command, the situation is
1897 probably due to a bug and you should report it.
1899 You can disable the popping up of this buffer by adding the entry
1900 \(undo discard-info) to the user option `warning-suppress-types'.\n")
1901 :warning)
1902 (setq buffer-undo-list nil)
1905 (defvar shell-command-history nil
1906 "History list for some commands that read shell commands.
1908 Maximum length of the history list is determined by the value
1909 of `history-length', which see.")
1911 (defvar shell-command-switch "-c"
1912 "Switch used to have the shell execute its command line argument.")
1914 (defvar shell-command-default-error-buffer nil
1915 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1916 This buffer is used when `shell-command' or `shell-command-on-region'
1917 is run interactively. A value of nil means that output to stderr and
1918 stdout will be intermixed in the output stream.")
1920 (defun shell-command (command &optional output-buffer error-buffer)
1921 "Execute string COMMAND in inferior shell; display output, if any.
1922 With prefix argument, insert the COMMAND's output at point.
1924 If COMMAND ends in ampersand, execute it asynchronously.
1925 The output appears in the buffer `*Async Shell Command*'.
1926 That buffer is in shell mode.
1928 Otherwise, COMMAND is executed synchronously. The output appears in
1929 the buffer `*Shell Command Output*'. If the output is short enough to
1930 display in the echo area (which is determined by the variables
1931 `resize-mini-windows' and `max-mini-window-height'), it is shown
1932 there, but it is nonetheless available in buffer `*Shell Command
1933 Output*' even though that buffer is not automatically displayed.
1935 To specify a coding system for converting non-ASCII characters
1936 in the shell command output, use \\[universal-coding-system-argument]
1937 before this command.
1939 Noninteractive callers can specify coding systems by binding
1940 `coding-system-for-read' and `coding-system-for-write'.
1942 The optional second argument OUTPUT-BUFFER, if non-nil,
1943 says to put the output in some other buffer.
1944 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1945 If OUTPUT-BUFFER is not a buffer and not nil,
1946 insert output in current buffer. (This cannot be done asynchronously.)
1947 In either case, the output is inserted after point (leaving mark after it).
1949 If the command terminates without error, but generates output,
1950 and you did not specify \"insert it in the current buffer\",
1951 the output can be displayed in the echo area or in its buffer.
1952 If the output is short enough to display in the echo area
1953 \(determined by the variable `max-mini-window-height' if
1954 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1955 the buffer containing the output is displayed.
1957 If there is output and an error, and you did not specify \"insert it
1958 in the current buffer\", a message about the error goes at the end
1959 of the output.
1961 If there is no output, or if output is inserted in the current buffer,
1962 then `*Shell Command Output*' is deleted.
1964 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1965 or buffer name to which to direct the command's standard error output.
1966 If it is nil, error output is mingled with regular output.
1967 In an interactive call, the variable `shell-command-default-error-buffer'
1968 specifies the value of ERROR-BUFFER."
1970 (interactive (list (read-from-minibuffer "Shell command: "
1971 nil nil nil 'shell-command-history)
1972 current-prefix-arg
1973 shell-command-default-error-buffer))
1974 ;; Look for a handler in case default-directory is a remote file name.
1975 (let ((handler
1976 (find-file-name-handler (directory-file-name default-directory)
1977 'shell-command)))
1978 (if handler
1979 (funcall handler 'shell-command command output-buffer error-buffer)
1980 (if (and output-buffer
1981 (not (or (bufferp output-buffer) (stringp output-buffer))))
1982 ;; Output goes in current buffer.
1983 (let ((error-file
1984 (if error-buffer
1985 (make-temp-file
1986 (expand-file-name "scor"
1987 (or small-temporary-file-directory
1988 temporary-file-directory)))
1989 nil)))
1990 (barf-if-buffer-read-only)
1991 (push-mark nil t)
1992 ;; We do not use -f for csh; we will not support broken use of
1993 ;; .cshrcs. Even the BSD csh manual says to use
1994 ;; "if ($?prompt) exit" before things which are not useful
1995 ;; non-interactively. Besides, if someone wants their other
1996 ;; aliases for shell commands then they can still have them.
1997 (call-process shell-file-name nil
1998 (if error-file
1999 (list t error-file)
2001 nil shell-command-switch command)
2002 (when (and error-file (file-exists-p error-file))
2003 (if (< 0 (nth 7 (file-attributes error-file)))
2004 (with-current-buffer (get-buffer-create error-buffer)
2005 (let ((pos-from-end (- (point-max) (point))))
2006 (or (bobp)
2007 (insert "\f\n"))
2008 ;; Do no formatting while reading error file,
2009 ;; because that can run a shell command, and we
2010 ;; don't want that to cause an infinite recursion.
2011 (format-insert-file error-file nil)
2012 ;; Put point after the inserted errors.
2013 (goto-char (- (point-max) pos-from-end)))
2014 (display-buffer (current-buffer))))
2015 (delete-file error-file))
2016 ;; This is like exchange-point-and-mark, but doesn't
2017 ;; activate the mark. It is cleaner to avoid activation,
2018 ;; even though the command loop would deactivate the mark
2019 ;; because we inserted text.
2020 (goto-char (prog1 (mark t)
2021 (set-marker (mark-marker) (point)
2022 (current-buffer)))))
2023 ;; Output goes in a separate buffer.
2024 ;; Preserve the match data in case called from a program.
2025 (save-match-data
2026 (if (string-match "[ \t]*&[ \t]*\\'" command)
2027 ;; Command ending with ampersand means asynchronous.
2028 (let ((buffer (get-buffer-create
2029 (or output-buffer "*Async Shell Command*")))
2030 (directory default-directory)
2031 proc)
2032 ;; Remove the ampersand.
2033 (setq command (substring command 0 (match-beginning 0)))
2034 ;; If will kill a process, query first.
2035 (setq proc (get-buffer-process buffer))
2036 (if proc
2037 (if (yes-or-no-p "A command is running. Kill it? ")
2038 (kill-process proc)
2039 (error "Shell command in progress")))
2040 (with-current-buffer buffer
2041 (setq buffer-read-only nil)
2042 (erase-buffer)
2043 (display-buffer buffer)
2044 (setq default-directory directory)
2045 (setq proc (start-process "Shell" buffer shell-file-name
2046 shell-command-switch command))
2047 (setq mode-line-process '(":%s"))
2048 (require 'shell) (shell-mode)
2049 (set-process-sentinel proc 'shell-command-sentinel)
2051 (shell-command-on-region (point) (point) command
2052 output-buffer nil error-buffer)))))))
2054 (defun display-message-or-buffer (message
2055 &optional buffer-name not-this-window frame)
2056 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
2057 MESSAGE may be either a string or a buffer.
2059 A buffer is displayed using `display-buffer' if MESSAGE is too long for
2060 the maximum height of the echo area, as defined by `max-mini-window-height'
2061 if `resize-mini-windows' is non-nil.
2063 Returns either the string shown in the echo area, or when a pop-up
2064 buffer is used, the window used to display it.
2066 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
2067 name of the buffer used to display it in the case where a pop-up buffer
2068 is used, defaulting to `*Message*'. In the case where MESSAGE is a
2069 string and it is displayed in the echo area, it is not specified whether
2070 the contents are inserted into the buffer anyway.
2072 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
2073 and only used if a buffer is displayed."
2074 (cond ((and (stringp message) (not (string-match "\n" message)))
2075 ;; Trivial case where we can use the echo area
2076 (message "%s" message))
2077 ((and (stringp message)
2078 (= (string-match "\n" message) (1- (length message))))
2079 ;; Trivial case where we can just remove single trailing newline
2080 (message "%s" (substring message 0 (1- (length message)))))
2082 ;; General case
2083 (with-current-buffer
2084 (if (bufferp message)
2085 message
2086 (get-buffer-create (or buffer-name "*Message*")))
2088 (unless (bufferp message)
2089 (erase-buffer)
2090 (insert message))
2092 (let ((lines
2093 (if (= (buffer-size) 0)
2095 (count-screen-lines nil nil nil (minibuffer-window)))))
2096 (cond ((= lines 0))
2097 ((and (or (<= lines 1)
2098 (<= lines
2099 (if resize-mini-windows
2100 (cond ((floatp max-mini-window-height)
2101 (* (frame-height)
2102 max-mini-window-height))
2103 ((integerp max-mini-window-height)
2104 max-mini-window-height)
2107 1)))
2108 ;; Don't use the echo area if the output buffer is
2109 ;; already dispayed in the selected frame.
2110 (not (get-buffer-window (current-buffer))))
2111 ;; Echo area
2112 (goto-char (point-max))
2113 (when (bolp)
2114 (backward-char 1))
2115 (message "%s" (buffer-substring (point-min) (point))))
2117 ;; Buffer
2118 (goto-char (point-min))
2119 (display-buffer (current-buffer)
2120 not-this-window frame))))))))
2123 ;; We have a sentinel to prevent insertion of a termination message
2124 ;; in the buffer itself.
2125 (defun shell-command-sentinel (process signal)
2126 (if (memq (process-status process) '(exit signal))
2127 (message "%s: %s."
2128 (car (cdr (cdr (process-command process))))
2129 (substring signal 0 -1))))
2131 (defun shell-command-on-region (start end command
2132 &optional output-buffer replace
2133 error-buffer display-error-buffer)
2134 "Execute string COMMAND in inferior shell with region as input.
2135 Normally display output (if any) in temp buffer `*Shell Command Output*';
2136 Prefix arg means replace the region with it. Return the exit code of
2137 COMMAND.
2139 To specify a coding system for converting non-ASCII characters
2140 in the input and output to the shell command, use \\[universal-coding-system-argument]
2141 before this command. By default, the input (from the current buffer)
2142 is encoded in the same coding system that will be used to save the file,
2143 `buffer-file-coding-system'. If the output is going to replace the region,
2144 then it is decoded from that same coding system.
2146 The noninteractive arguments are START, END, COMMAND,
2147 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
2148 Noninteractive callers can specify coding systems by binding
2149 `coding-system-for-read' and `coding-system-for-write'.
2151 If the command generates output, the output may be displayed
2152 in the echo area or in a buffer.
2153 If the output is short enough to display in the echo area
2154 \(determined by the variable `max-mini-window-height' if
2155 `resize-mini-windows' is non-nil), it is shown there. Otherwise
2156 it is displayed in the buffer `*Shell Command Output*'. The output
2157 is available in that buffer in both cases.
2159 If there is output and an error, a message about the error
2160 appears at the end of the output.
2162 If there is no output, or if output is inserted in the current buffer,
2163 then `*Shell Command Output*' is deleted.
2165 If the optional fourth argument OUTPUT-BUFFER is non-nil,
2166 that says to put the output in some other buffer.
2167 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
2168 If OUTPUT-BUFFER is not a buffer and not nil,
2169 insert output in the current buffer.
2170 In either case, the output is inserted after point (leaving mark after it).
2172 If REPLACE, the optional fifth argument, is non-nil, that means insert
2173 the output in place of text from START to END, putting point and mark
2174 around it.
2176 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
2177 or buffer name to which to direct the command's standard error output.
2178 If it is nil, error output is mingled with regular output.
2179 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
2180 were any errors. (This is always t, interactively.)
2181 In an interactive call, the variable `shell-command-default-error-buffer'
2182 specifies the value of ERROR-BUFFER."
2183 (interactive (let (string)
2184 (unless (mark)
2185 (error "The mark is not set now, so there is no region"))
2186 ;; Do this before calling region-beginning
2187 ;; and region-end, in case subprocess output
2188 ;; relocates them while we are in the minibuffer.
2189 (setq string (read-from-minibuffer "Shell command on region: "
2190 nil nil nil
2191 'shell-command-history))
2192 ;; call-interactively recognizes region-beginning and
2193 ;; region-end specially, leaving them in the history.
2194 (list (region-beginning) (region-end)
2195 string
2196 current-prefix-arg
2197 current-prefix-arg
2198 shell-command-default-error-buffer
2199 t)))
2200 (let ((error-file
2201 (if error-buffer
2202 (make-temp-file
2203 (expand-file-name "scor"
2204 (or small-temporary-file-directory
2205 temporary-file-directory)))
2206 nil))
2207 exit-status)
2208 (if (or replace
2209 (and output-buffer
2210 (not (or (bufferp output-buffer) (stringp output-buffer)))))
2211 ;; Replace specified region with output from command.
2212 (let ((swap (and replace (< start end))))
2213 ;; Don't muck with mark unless REPLACE says we should.
2214 (goto-char start)
2215 (and replace (push-mark (point) 'nomsg))
2216 (setq exit-status
2217 (call-process-region start end shell-file-name t
2218 (if error-file
2219 (list t error-file)
2221 nil shell-command-switch command))
2222 ;; It is rude to delete a buffer which the command is not using.
2223 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
2224 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
2225 ;; (kill-buffer shell-buffer)))
2226 ;; Don't muck with mark unless REPLACE says we should.
2227 (and replace swap (exchange-point-and-mark)))
2228 ;; No prefix argument: put the output in a temp buffer,
2229 ;; replacing its entire contents.
2230 (let ((buffer (get-buffer-create
2231 (or output-buffer "*Shell Command Output*"))))
2232 (unwind-protect
2233 (if (eq buffer (current-buffer))
2234 ;; If the input is the same buffer as the output,
2235 ;; delete everything but the specified region,
2236 ;; then replace that region with the output.
2237 (progn (setq buffer-read-only nil)
2238 (delete-region (max start end) (point-max))
2239 (delete-region (point-min) (min start end))
2240 (setq exit-status
2241 (call-process-region (point-min) (point-max)
2242 shell-file-name t
2243 (if error-file
2244 (list t error-file)
2246 nil shell-command-switch
2247 command)))
2248 ;; Clear the output buffer, then run the command with
2249 ;; output there.
2250 (let ((directory default-directory))
2251 (save-excursion
2252 (set-buffer buffer)
2253 (setq buffer-read-only nil)
2254 (if (not output-buffer)
2255 (setq default-directory directory))
2256 (erase-buffer)))
2257 (setq exit-status
2258 (call-process-region start end shell-file-name nil
2259 (if error-file
2260 (list buffer error-file)
2261 buffer)
2262 nil shell-command-switch command)))
2263 ;; Report the output.
2264 (with-current-buffer buffer
2265 (setq mode-line-process
2266 (cond ((null exit-status)
2267 " - Error")
2268 ((stringp exit-status)
2269 (format " - Signal [%s]" exit-status))
2270 ((not (equal 0 exit-status))
2271 (format " - Exit [%d]" exit-status)))))
2272 (if (with-current-buffer buffer (> (point-max) (point-min)))
2273 ;; There's some output, display it
2274 (display-message-or-buffer buffer)
2275 ;; No output; error?
2276 (let ((output
2277 (if (and error-file
2278 (< 0 (nth 7 (file-attributes error-file))))
2279 "some error output"
2280 "no output")))
2281 (cond ((null exit-status)
2282 (message "(Shell command failed with error)"))
2283 ((equal 0 exit-status)
2284 (message "(Shell command succeeded with %s)"
2285 output))
2286 ((stringp exit-status)
2287 (message "(Shell command killed by signal %s)"
2288 exit-status))
2290 (message "(Shell command failed with code %d and %s)"
2291 exit-status output))))
2292 ;; Don't kill: there might be useful info in the undo-log.
2293 ;; (kill-buffer buffer)
2294 ))))
2296 (when (and error-file (file-exists-p error-file))
2297 (if (< 0 (nth 7 (file-attributes error-file)))
2298 (with-current-buffer (get-buffer-create error-buffer)
2299 (let ((pos-from-end (- (point-max) (point))))
2300 (or (bobp)
2301 (insert "\f\n"))
2302 ;; Do no formatting while reading error file,
2303 ;; because that can run a shell command, and we
2304 ;; don't want that to cause an infinite recursion.
2305 (format-insert-file error-file nil)
2306 ;; Put point after the inserted errors.
2307 (goto-char (- (point-max) pos-from-end)))
2308 (and display-error-buffer
2309 (display-buffer (current-buffer)))))
2310 (delete-file error-file))
2311 exit-status))
2313 (defun shell-command-to-string (command)
2314 "Execute shell command COMMAND and return its output as a string."
2315 (with-output-to-string
2316 (with-current-buffer
2317 standard-output
2318 (call-process shell-file-name nil t nil shell-command-switch command))))
2320 (defun process-file (program &optional infile buffer display &rest args)
2321 "Process files synchronously in a separate process.
2322 Similar to `call-process', but may invoke a file handler based on
2323 `default-directory'. The current working directory of the
2324 subprocess is `default-directory'.
2326 File names in INFILE and BUFFER are handled normally, but file
2327 names in ARGS should be relative to `default-directory', as they
2328 are passed to the process verbatim. \(This is a difference to
2329 `call-process' which does not support file handlers for INFILE
2330 and BUFFER.\)
2332 Some file handlers might not support all variants, for example
2333 they might behave as if DISPLAY was nil, regardless of the actual
2334 value passed."
2335 (let ((fh (find-file-name-handler default-directory 'process-file))
2336 lc stderr-file)
2337 (unwind-protect
2338 (if fh (apply fh 'process-file program infile buffer display args)
2339 (when infile (setq lc (file-local-copy infile)))
2340 (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer)))
2341 (make-temp-file "emacs")))
2342 (prog1
2343 (apply 'call-process program
2344 (or lc infile)
2345 (if stderr-file (list (car buffer) stderr-file) buffer)
2346 display args)
2347 (when stderr-file (copy-file stderr-file (cadr buffer)))))
2348 (when stderr-file (delete-file stderr-file))
2349 (when lc (delete-file lc)))))
2351 (defun start-file-process (name buffer program &rest program-args)
2352 "Start a program in a subprocess. Return the process object for it.
2353 Similar to `start-process', but may invoke a file handler based on
2354 `default-directory'. The current working directory of the
2355 subprocess is `default-directory'.
2357 PROGRAM and PROGRAM-ARGS might be file names. They are not
2358 objects of file handler invocation."
2359 (let ((fh (find-file-name-handler default-directory 'start-file-process)))
2360 (if fh (apply fh 'start-file-process name buffer program program-args)
2361 (apply 'start-process name buffer program program-args))))
2365 (defvar universal-argument-map
2366 (let ((map (make-sparse-keymap)))
2367 (define-key map [t] 'universal-argument-other-key)
2368 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
2369 (define-key map [switch-frame] nil)
2370 (define-key map [?\C-u] 'universal-argument-more)
2371 (define-key map [?-] 'universal-argument-minus)
2372 (define-key map [?0] 'digit-argument)
2373 (define-key map [?1] 'digit-argument)
2374 (define-key map [?2] 'digit-argument)
2375 (define-key map [?3] 'digit-argument)
2376 (define-key map [?4] 'digit-argument)
2377 (define-key map [?5] 'digit-argument)
2378 (define-key map [?6] 'digit-argument)
2379 (define-key map [?7] 'digit-argument)
2380 (define-key map [?8] 'digit-argument)
2381 (define-key map [?9] 'digit-argument)
2382 (define-key map [kp-0] 'digit-argument)
2383 (define-key map [kp-1] 'digit-argument)
2384 (define-key map [kp-2] 'digit-argument)
2385 (define-key map [kp-3] 'digit-argument)
2386 (define-key map [kp-4] 'digit-argument)
2387 (define-key map [kp-5] 'digit-argument)
2388 (define-key map [kp-6] 'digit-argument)
2389 (define-key map [kp-7] 'digit-argument)
2390 (define-key map [kp-8] 'digit-argument)
2391 (define-key map [kp-9] 'digit-argument)
2392 (define-key map [kp-subtract] 'universal-argument-minus)
2393 map)
2394 "Keymap used while processing \\[universal-argument].")
2396 (defvar universal-argument-num-events nil
2397 "Number of argument-specifying events read by `universal-argument'.
2398 `universal-argument-other-key' uses this to discard those events
2399 from (this-command-keys), and reread only the final command.")
2401 (defvar overriding-map-is-bound nil
2402 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
2404 (defvar saved-overriding-map nil
2405 "The saved value of `overriding-terminal-local-map'.
2406 That variable gets restored to this value on exiting \"universal
2407 argument mode\".")
2409 (defun ensure-overriding-map-is-bound ()
2410 "Check `overriding-terminal-local-map' is `universal-argument-map'."
2411 (unless overriding-map-is-bound
2412 (setq saved-overriding-map overriding-terminal-local-map)
2413 (setq overriding-terminal-local-map universal-argument-map)
2414 (setq overriding-map-is-bound t)))
2416 (defun restore-overriding-map ()
2417 "Restore `overriding-terminal-local-map' to its saved value."
2418 (setq overriding-terminal-local-map saved-overriding-map)
2419 (setq overriding-map-is-bound nil))
2421 (defun universal-argument ()
2422 "Begin a numeric argument for the following command.
2423 Digits or minus sign following \\[universal-argument] make up the numeric argument.
2424 \\[universal-argument] following the digits or minus sign ends the argument.
2425 \\[universal-argument] without digits or minus sign provides 4 as argument.
2426 Repeating \\[universal-argument] without digits or minus sign
2427 multiplies the argument by 4 each time.
2428 For some commands, just \\[universal-argument] by itself serves as a flag
2429 which is different in effect from any particular numeric argument.
2430 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
2431 (interactive)
2432 (setq prefix-arg (list 4))
2433 (setq universal-argument-num-events (length (this-command-keys)))
2434 (ensure-overriding-map-is-bound))
2436 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
2437 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
2438 (defun universal-argument-more (arg)
2439 (interactive "P")
2440 (if (consp arg)
2441 (setq prefix-arg (list (* 4 (car arg))))
2442 (if (eq arg '-)
2443 (setq prefix-arg (list -4))
2444 (setq prefix-arg arg)
2445 (restore-overriding-map)))
2446 (setq universal-argument-num-events (length (this-command-keys))))
2448 (defun negative-argument (arg)
2449 "Begin a negative numeric argument for the next command.
2450 \\[universal-argument] following digits or minus sign ends the argument."
2451 (interactive "P")
2452 (cond ((integerp arg)
2453 (setq prefix-arg (- arg)))
2454 ((eq arg '-)
2455 (setq prefix-arg nil))
2457 (setq prefix-arg '-)))
2458 (setq universal-argument-num-events (length (this-command-keys)))
2459 (ensure-overriding-map-is-bound))
2461 (defun digit-argument (arg)
2462 "Part of the numeric argument for the next command.
2463 \\[universal-argument] following digits or minus sign ends the argument."
2464 (interactive "P")
2465 (let* ((char (if (integerp last-command-char)
2466 last-command-char
2467 (get last-command-char 'ascii-character)))
2468 (digit (- (logand char ?\177) ?0)))
2469 (cond ((integerp arg)
2470 (setq prefix-arg (+ (* arg 10)
2471 (if (< arg 0) (- digit) digit))))
2472 ((eq arg '-)
2473 ;; Treat -0 as just -, so that -01 will work.
2474 (setq prefix-arg (if (zerop digit) '- (- digit))))
2476 (setq prefix-arg digit))))
2477 (setq universal-argument-num-events (length (this-command-keys)))
2478 (ensure-overriding-map-is-bound))
2480 ;; For backward compatibility, minus with no modifiers is an ordinary
2481 ;; command if digits have already been entered.
2482 (defun universal-argument-minus (arg)
2483 (interactive "P")
2484 (if (integerp arg)
2485 (universal-argument-other-key arg)
2486 (negative-argument arg)))
2488 ;; Anything else terminates the argument and is left in the queue to be
2489 ;; executed as a command.
2490 (defun universal-argument-other-key (arg)
2491 (interactive "P")
2492 (setq prefix-arg arg)
2493 (let* ((key (this-command-keys))
2494 (keylist (listify-key-sequence key)))
2495 (setq unread-command-events
2496 (append (nthcdr universal-argument-num-events keylist)
2497 unread-command-events)))
2498 (reset-this-command-lengths)
2499 (restore-overriding-map))
2501 (defvar buffer-substring-filters nil
2502 "List of filter functions for `filter-buffer-substring'.
2503 Each function must accept a single argument, a string, and return
2504 a string. The buffer substring is passed to the first function
2505 in the list, and the return value of each function is passed to
2506 the next. The return value of the last function is used as the
2507 return value of `filter-buffer-substring'.
2509 If this variable is nil, no filtering is performed.")
2511 (defun filter-buffer-substring (beg end &optional delete noprops)
2512 "Return the buffer substring between BEG and END, after filtering.
2513 The buffer substring is passed through each of the filter
2514 functions in `buffer-substring-filters', and the value from the
2515 last filter function is returned. If `buffer-substring-filters'
2516 is nil, the buffer substring is returned unaltered.
2518 If DELETE is non-nil, the text between BEG and END is deleted
2519 from the buffer.
2521 If NOPROPS is non-nil, final string returned does not include
2522 text properties, while the string passed to the filters still
2523 includes text properties from the buffer text.
2525 Point is temporarily set to BEG before calling
2526 `buffer-substring-filters', in case the functions need to know
2527 where the text came from.
2529 This function should be used instead of `buffer-substring',
2530 `buffer-substring-no-properties', or `delete-and-extract-region'
2531 when you want to allow filtering to take place. For example,
2532 major or minor modes can use `buffer-substring-filters' to
2533 extract characters that are special to a buffer, and should not
2534 be copied into other buffers."
2535 (cond
2536 ((or delete buffer-substring-filters)
2537 (save-excursion
2538 (goto-char beg)
2539 (let ((string (if delete (delete-and-extract-region beg end)
2540 (buffer-substring beg end))))
2541 (dolist (filter buffer-substring-filters)
2542 (setq string (funcall filter string)))
2543 (if noprops
2544 (set-text-properties 0 (length string) nil string))
2545 string)))
2546 (noprops
2547 (buffer-substring-no-properties beg end))
2549 (buffer-substring beg end))))
2552 ;;;; Window system cut and paste hooks.
2554 (defvar interprogram-cut-function nil
2555 "Function to call to make a killed region available to other programs.
2557 Most window systems provide some sort of facility for cutting and
2558 pasting text between the windows of different programs.
2559 This variable holds a function that Emacs calls whenever text
2560 is put in the kill ring, to make the new kill available to other
2561 programs.
2563 The function takes one or two arguments.
2564 The first argument, TEXT, is a string containing
2565 the text which should be made available.
2566 The second, optional, argument PUSH, has the same meaning as the
2567 similar argument to `x-set-cut-buffer', which see.")
2569 (defvar interprogram-paste-function nil
2570 "Function to call to get text cut from other programs.
2572 Most window systems provide some sort of facility for cutting and
2573 pasting text between the windows of different programs.
2574 This variable holds a function that Emacs calls to obtain
2575 text that other programs have provided for pasting.
2577 The function should be called with no arguments. If the function
2578 returns nil, then no other program has provided such text, and the top
2579 of the Emacs kill ring should be used. If the function returns a
2580 string, then the caller of the function \(usually `current-kill')
2581 should put this string in the kill ring as the latest kill.
2583 This function may also return a list of strings if the window
2584 system supports multiple selections. The first string will be
2585 used as the pasted text, but the other will be placed in the
2586 kill ring for easy access via `yank-pop'.
2588 Note that the function should return a string only if a program other
2589 than Emacs has provided a string for pasting; if Emacs provided the
2590 most recent string, the function should return nil. If it is
2591 difficult to tell whether Emacs or some other program provided the
2592 current string, it is probably good enough to return nil if the string
2593 is equal (according to `string=') to the last text Emacs provided.")
2597 ;;;; The kill ring data structure.
2599 (defvar kill-ring nil
2600 "List of killed text sequences.
2601 Since the kill ring is supposed to interact nicely with cut-and-paste
2602 facilities offered by window systems, use of this variable should
2603 interact nicely with `interprogram-cut-function' and
2604 `interprogram-paste-function'. The functions `kill-new',
2605 `kill-append', and `current-kill' are supposed to implement this
2606 interaction; you may want to use them instead of manipulating the kill
2607 ring directly.")
2609 (defcustom kill-ring-max 60
2610 "*Maximum length of kill ring before oldest elements are thrown away."
2611 :type 'integer
2612 :group 'killing)
2614 (defvar kill-ring-yank-pointer nil
2615 "The tail of the kill ring whose car is the last thing yanked.")
2617 (defun kill-new (string &optional replace yank-handler)
2618 "Make STRING the latest kill in the kill ring.
2619 Set `kill-ring-yank-pointer' to point to it.
2620 If `interprogram-cut-function' is non-nil, apply it to STRING.
2621 Optional second argument REPLACE non-nil means that STRING will replace
2622 the front of the kill ring, rather than being added to the list.
2624 Optional third arguments YANK-HANDLER controls how the STRING is later
2625 inserted into a buffer; see `insert-for-yank' for details.
2626 When a yank handler is specified, STRING must be non-empty (the yank
2627 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2629 When the yank handler has a non-nil PARAM element, the original STRING
2630 argument is not used by `insert-for-yank'. However, since Lisp code
2631 may access and use elements from the kill ring directly, the STRING
2632 argument should still be a \"useful\" string for such uses."
2633 (if (> (length string) 0)
2634 (if yank-handler
2635 (put-text-property 0 (length string)
2636 'yank-handler yank-handler string))
2637 (if yank-handler
2638 (signal 'args-out-of-range
2639 (list string "yank-handler specified for empty string"))))
2640 (if (fboundp 'menu-bar-update-yank-menu)
2641 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2642 (if (and replace kill-ring)
2643 (setcar kill-ring string)
2644 (push string kill-ring)
2645 (if (> (length kill-ring) kill-ring-max)
2646 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2647 (setq kill-ring-yank-pointer kill-ring)
2648 (if interprogram-cut-function
2649 (funcall interprogram-cut-function string (not replace))))
2651 (defun kill-append (string before-p &optional yank-handler)
2652 "Append STRING to the end of the latest kill in the kill ring.
2653 If BEFORE-P is non-nil, prepend STRING to the kill.
2654 Optional third argument YANK-HANDLER, if non-nil, specifies the
2655 yank-handler text property to be set on the combined kill ring
2656 string. If the specified yank-handler arg differs from the
2657 yank-handler property of the latest kill string, this function
2658 adds the combined string to the kill ring as a new element,
2659 instead of replacing the last kill with it.
2660 If `interprogram-cut-function' is set, pass the resulting kill to it."
2661 (let* ((cur (car kill-ring)))
2662 (kill-new (if before-p (concat string cur) (concat cur string))
2663 (or (= (length cur) 0)
2664 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2665 yank-handler)))
2667 (defcustom yank-pop-change-selection nil
2668 "If non-nil, rotating the kill ring changes the window system selection."
2669 :type 'boolean
2670 :group 'killing
2671 :version "23.1")
2673 (defun current-kill (n &optional do-not-move)
2674 "Rotate the yanking point by N places, and then return that kill.
2675 If N is zero, `interprogram-paste-function' is set, and calling it returns a
2676 string or list of strings, then that string (or list) is added to the front
2677 of the kill ring and the string (or first string in the list) is returned as
2678 the latest kill.
2680 If N is not zero, and if `yank-pop-change-selection' is
2681 non-nil, use `interprogram-cut-function' to transfer the
2682 kill at the new yank point into the window system selection.
2684 If optional arg DO-NOT-MOVE is non-nil, then don't actually
2685 move the yanking point; just return the Nth kill forward."
2687 (let ((interprogram-paste (and (= n 0)
2688 interprogram-paste-function
2689 (funcall interprogram-paste-function))))
2690 (if interprogram-paste
2691 (progn
2692 ;; Disable the interprogram cut function when we add the new
2693 ;; text to the kill ring, so Emacs doesn't try to own the
2694 ;; selection, with identical text.
2695 (let ((interprogram-cut-function nil))
2696 (if (listp interprogram-paste)
2697 (mapc 'kill-new (nreverse interprogram-paste))
2698 (kill-new interprogram-paste)))
2699 (car kill-ring))
2700 (or kill-ring (error "Kill ring is empty"))
2701 (let ((ARGth-kill-element
2702 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2703 (length kill-ring))
2704 kill-ring)))
2705 (unless do-not-move
2706 (setq kill-ring-yank-pointer ARGth-kill-element)
2707 (when (and yank-pop-change-selection
2708 (> n 0)
2709 interprogram-cut-function)
2710 (funcall interprogram-cut-function (car ARGth-kill-element))))
2711 (car ARGth-kill-element)))))
2715 ;;;; Commands for manipulating the kill ring.
2717 (defcustom kill-read-only-ok nil
2718 "*Non-nil means don't signal an error for killing read-only text."
2719 :type 'boolean
2720 :group 'killing)
2722 (put 'text-read-only 'error-conditions
2723 '(text-read-only buffer-read-only error))
2724 (put 'text-read-only 'error-message "Text is read-only")
2726 (defun kill-region (beg end &optional yank-handler)
2727 "Kill (\"cut\") text between point and mark.
2728 This deletes the text from the buffer and saves it in the kill ring.
2729 The command \\[yank] can retrieve it from there.
2730 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2732 If you want to append the killed region to the last killed text,
2733 use \\[append-next-kill] before \\[kill-region].
2735 If the buffer is read-only, Emacs will beep and refrain from deleting
2736 the text, but put the text in the kill ring anyway. This means that
2737 you can use the killing commands to copy text from a read-only buffer.
2739 This is the primitive for programs to kill text (as opposed to deleting it).
2740 Supply two arguments, character positions indicating the stretch of text
2741 to be killed.
2742 Any command that calls this function is a \"kill command\".
2743 If the previous command was also a kill command,
2744 the text killed this time appends to the text killed last time
2745 to make one entry in the kill ring.
2747 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2748 specifies the yank-handler text property to be set on the killed
2749 text. See `insert-for-yank'."
2750 ;; Pass point first, then mark, because the order matters
2751 ;; when calling kill-append.
2752 (interactive (list (point) (mark)))
2753 (unless (and beg end)
2754 (error "The mark is not set now, so there is no region"))
2755 (condition-case nil
2756 (let ((string (filter-buffer-substring beg end t)))
2757 (when string ;STRING is nil if BEG = END
2758 ;; Add that string to the kill ring, one way or another.
2759 (if (eq last-command 'kill-region)
2760 (kill-append string (< end beg) yank-handler)
2761 (kill-new string nil yank-handler)))
2762 (when (or string (eq last-command 'kill-region))
2763 (setq this-command 'kill-region))
2764 nil)
2765 ((buffer-read-only text-read-only)
2766 ;; The code above failed because the buffer, or some of the characters
2767 ;; in the region, are read-only.
2768 ;; We should beep, in case the user just isn't aware of this.
2769 ;; However, there's no harm in putting
2770 ;; the region's text in the kill ring, anyway.
2771 (copy-region-as-kill beg end)
2772 ;; Set this-command now, so it will be set even if we get an error.
2773 (setq this-command 'kill-region)
2774 ;; This should barf, if appropriate, and give us the correct error.
2775 (if kill-read-only-ok
2776 (progn (message "Read only text copied to kill ring") nil)
2777 ;; Signal an error if the buffer is read-only.
2778 (barf-if-buffer-read-only)
2779 ;; If the buffer isn't read-only, the text is.
2780 (signal 'text-read-only (list (current-buffer)))))))
2782 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2783 ;; to get two copies of the text when the user accidentally types M-w and
2784 ;; then corrects it with the intended C-w.
2785 (defun copy-region-as-kill (beg end)
2786 "Save the region as if killed, but don't kill it.
2787 In Transient Mark mode, deactivate the mark.
2788 If `interprogram-cut-function' is non-nil, also save the text for a window
2789 system cut and paste.
2791 This command's old key binding has been given to `kill-ring-save'."
2792 (interactive "r")
2793 (if (eq last-command 'kill-region)
2794 (kill-append (filter-buffer-substring beg end) (< end beg))
2795 (kill-new (filter-buffer-substring beg end)))
2796 (if transient-mark-mode
2797 (setq deactivate-mark t))
2798 nil)
2800 (defun kill-ring-save (beg end)
2801 "Save the region as if killed, but don't kill it.
2802 In Transient Mark mode, deactivate the mark.
2803 If `interprogram-cut-function' is non-nil, also save the text for a window
2804 system cut and paste.
2806 If you want to append the killed line to the last killed text,
2807 use \\[append-next-kill] before \\[kill-ring-save].
2809 This command is similar to `copy-region-as-kill', except that it gives
2810 visual feedback indicating the extent of the region being copied."
2811 (interactive "r")
2812 (copy-region-as-kill beg end)
2813 ;; This use of interactive-p is correct
2814 ;; because the code it controls just gives the user visual feedback.
2815 (if (interactive-p)
2816 (let ((other-end (if (= (point) beg) end beg))
2817 (opoint (point))
2818 ;; Inhibit quitting so we can make a quit here
2819 ;; look like a C-g typed as a command.
2820 (inhibit-quit t))
2821 (if (pos-visible-in-window-p other-end (selected-window))
2822 (unless (and transient-mark-mode
2823 (face-background 'region))
2824 ;; Swap point and mark.
2825 (set-marker (mark-marker) (point) (current-buffer))
2826 (goto-char other-end)
2827 (sit-for blink-matching-delay)
2828 ;; Swap back.
2829 (set-marker (mark-marker) other-end (current-buffer))
2830 (goto-char opoint)
2831 ;; If user quit, deactivate the mark
2832 ;; as C-g would as a command.
2833 (and quit-flag mark-active
2834 (deactivate-mark)))
2835 (let* ((killed-text (current-kill 0))
2836 (message-len (min (length killed-text) 40)))
2837 (if (= (point) beg)
2838 ;; Don't say "killed"; that is misleading.
2839 (message "Saved text until \"%s\""
2840 (substring killed-text (- message-len)))
2841 (message "Saved text from \"%s\""
2842 (substring killed-text 0 message-len))))))))
2844 (defun append-next-kill (&optional interactive)
2845 "Cause following command, if it kills, to append to previous kill.
2846 The argument is used for internal purposes; do not supply one."
2847 (interactive "p")
2848 ;; We don't use (interactive-p), since that breaks kbd macros.
2849 (if interactive
2850 (progn
2851 (setq this-command 'kill-region)
2852 (message "If the next command is a kill, it will append"))
2853 (setq last-command 'kill-region)))
2855 ;; Yanking.
2857 ;; This is actually used in subr.el but defcustom does not work there.
2858 (defcustom yank-excluded-properties
2859 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2860 yank-handler follow-link fontified)
2861 "Text properties to discard when yanking.
2862 The value should be a list of text properties to discard or t,
2863 which means to discard all text properties."
2864 :type '(choice (const :tag "All" t) (repeat symbol))
2865 :group 'killing
2866 :version "22.1")
2868 (defvar yank-window-start nil)
2869 (defvar yank-undo-function nil
2870 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2871 Function is called with two parameters, START and END corresponding to
2872 the value of the mark and point; it is guaranteed that START <= END.
2873 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2875 (defun yank-pop (&optional arg)
2876 "Replace just-yanked stretch of killed text with a different stretch.
2877 This command is allowed only immediately after a `yank' or a `yank-pop'.
2878 At such a time, the region contains a stretch of reinserted
2879 previously-killed text. `yank-pop' deletes that text and inserts in its
2880 place a different stretch of killed text.
2882 With no argument, the previous kill is inserted.
2883 With argument N, insert the Nth previous kill.
2884 If N is negative, this is a more recent kill.
2886 The sequence of kills wraps around, so that after the oldest one
2887 comes the newest one.
2889 When this command inserts killed text into the buffer, it honors
2890 `yank-excluded-properties' and `yank-handler' as described in the
2891 doc string for `insert-for-yank-1', which see."
2892 (interactive "*p")
2893 (if (not (eq last-command 'yank))
2894 (error "Previous command was not a yank"))
2895 (setq this-command 'yank)
2896 (unless arg (setq arg 1))
2897 (let ((inhibit-read-only t)
2898 (before (< (point) (mark t))))
2899 (if before
2900 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2901 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2902 (setq yank-undo-function nil)
2903 (set-marker (mark-marker) (point) (current-buffer))
2904 (insert-for-yank (current-kill arg))
2905 ;; Set the window start back where it was in the yank command,
2906 ;; if possible.
2907 (set-window-start (selected-window) yank-window-start t)
2908 (if before
2909 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2910 ;; It is cleaner to avoid activation, even though the command
2911 ;; loop would deactivate the mark because we inserted text.
2912 (goto-char (prog1 (mark t)
2913 (set-marker (mark-marker) (point) (current-buffer))))))
2914 nil)
2916 (defun yank (&optional arg)
2917 "Reinsert (\"paste\") the last stretch of killed text.
2918 More precisely, reinsert the stretch of killed text most recently
2919 killed OR yanked. Put point at end, and set mark at beginning.
2920 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2921 With argument N, reinsert the Nth most recently killed stretch of killed
2922 text.
2924 When this command inserts killed text into the buffer, it honors
2925 `yank-excluded-properties' and `yank-handler' as described in the
2926 doc string for `insert-for-yank-1', which see.
2928 See also the command `yank-pop' (\\[yank-pop])."
2929 (interactive "*P")
2930 (setq yank-window-start (window-start))
2931 ;; If we don't get all the way thru, make last-command indicate that
2932 ;; for the following command.
2933 (setq this-command t)
2934 (push-mark (point))
2935 (insert-for-yank (current-kill (cond
2936 ((listp arg) 0)
2937 ((eq arg '-) -2)
2938 (t (1- arg)))))
2939 (if (consp arg)
2940 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2941 ;; It is cleaner to avoid activation, even though the command
2942 ;; loop would deactivate the mark because we inserted text.
2943 (goto-char (prog1 (mark t)
2944 (set-marker (mark-marker) (point) (current-buffer)))))
2945 ;; If we do get all the way thru, make this-command indicate that.
2946 (if (eq this-command t)
2947 (setq this-command 'yank))
2948 nil)
2950 (defun rotate-yank-pointer (arg)
2951 "Rotate the yanking point in the kill ring.
2952 With argument, rotate that many kills forward (or backward, if negative)."
2953 (interactive "p")
2954 (current-kill arg))
2956 ;; Some kill commands.
2958 ;; Internal subroutine of delete-char
2959 (defun kill-forward-chars (arg)
2960 (if (listp arg) (setq arg (car arg)))
2961 (if (eq arg '-) (setq arg -1))
2962 (kill-region (point) (forward-point arg)))
2964 ;; Internal subroutine of backward-delete-char
2965 (defun kill-backward-chars (arg)
2966 (if (listp arg) (setq arg (car arg)))
2967 (if (eq arg '-) (setq arg -1))
2968 (kill-region (point) (forward-point (- arg))))
2970 (defcustom backward-delete-char-untabify-method 'untabify
2971 "*The method for untabifying when deleting backward.
2972 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2973 `hungry' -- delete all whitespace, both tabs and spaces;
2974 `all' -- delete all whitespace, including tabs, spaces and newlines;
2975 nil -- just delete one character."
2976 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2977 :version "20.3"
2978 :group 'killing)
2980 (defun backward-delete-char-untabify (arg &optional killp)
2981 "Delete characters backward, changing tabs into spaces.
2982 The exact behavior depends on `backward-delete-char-untabify-method'.
2983 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2984 Interactively, ARG is the prefix arg (default 1)
2985 and KILLP is t if a prefix arg was specified."
2986 (interactive "*p\nP")
2987 (when (eq backward-delete-char-untabify-method 'untabify)
2988 (let ((count arg))
2989 (save-excursion
2990 (while (and (> count 0) (not (bobp)))
2991 (if (= (preceding-char) ?\t)
2992 (let ((col (current-column)))
2993 (forward-char -1)
2994 (setq col (- col (current-column)))
2995 (insert-char ?\s col)
2996 (delete-char 1)))
2997 (forward-char -1)
2998 (setq count (1- count))))))
2999 (delete-backward-char
3000 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
3001 ((eq backward-delete-char-untabify-method 'all)
3002 " \t\n\r"))))
3003 (if skip
3004 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
3005 (point)))))
3006 (+ arg (if (zerop wh) 0 (1- wh))))
3007 arg))
3008 killp))
3010 (defun zap-to-char (arg char)
3011 "Kill up to and including ARG'th occurrence of CHAR.
3012 Case is ignored if `case-fold-search' is non-nil in the current buffer.
3013 Goes backward if ARG is negative; error if CHAR not found."
3014 (interactive "p\ncZap to char: ")
3015 (if (char-table-p translation-table-for-input)
3016 (setq char (or (aref translation-table-for-input char) char)))
3017 (kill-region (point) (progn
3018 (search-forward (char-to-string char) nil nil arg)
3019 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
3020 (point))))
3022 ;; kill-line and its subroutines.
3024 (defcustom kill-whole-line nil
3025 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
3026 :type 'boolean
3027 :group 'killing)
3029 (defun kill-line (&optional arg)
3030 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
3031 With prefix argument, kill that many lines from point.
3032 Negative arguments kill lines backward.
3033 With zero argument, kills the text before point on the current line.
3035 When calling from a program, nil means \"no arg\",
3036 a number counts as a prefix arg.
3038 To kill a whole line, when point is not at the beginning, type \
3039 \\[move-beginning-of-line] \\[kill-line] \\[kill-line].
3041 If `kill-whole-line' is non-nil, then this command kills the whole line
3042 including its terminating newline, when used at the beginning of a line
3043 with no argument. As a consequence, you can always kill a whole line
3044 by typing \\[move-beginning-of-line] \\[kill-line].
3046 If you want to append the killed line to the last killed text,
3047 use \\[append-next-kill] before \\[kill-line].
3049 If the buffer is read-only, Emacs will beep and refrain from deleting
3050 the line, but put the line in the kill ring anyway. This means that
3051 you can use this command to copy text from a read-only buffer.
3052 \(If the variable `kill-read-only-ok' is non-nil, then this won't
3053 even beep.)"
3054 (interactive "P")
3055 (kill-region (point)
3056 ;; It is better to move point to the other end of the kill
3057 ;; before killing. That way, in a read-only buffer, point
3058 ;; moves across the text that is copied to the kill ring.
3059 ;; The choice has no effect on undo now that undo records
3060 ;; the value of point from before the command was run.
3061 (progn
3062 (if arg
3063 (forward-visible-line (prefix-numeric-value arg))
3064 (if (eobp)
3065 (signal 'end-of-buffer nil))
3066 (let ((end
3067 (save-excursion
3068 (end-of-visible-line) (point))))
3069 (if (or (save-excursion
3070 ;; If trailing whitespace is visible,
3071 ;; don't treat it as nothing.
3072 (unless show-trailing-whitespace
3073 (skip-chars-forward " \t" end))
3074 (= (point) end))
3075 (and kill-whole-line (bolp)))
3076 (forward-visible-line 1)
3077 (goto-char end))))
3078 (point))))
3080 (defun kill-whole-line (&optional arg)
3081 "Kill current line.
3082 With prefix arg, kill that many lines starting from the current line.
3083 If arg is negative, kill backward. Also kill the preceding newline.
3084 \(This is meant to make \\[repeat] work well with negative arguments.\)
3085 If arg is zero, kill current line but exclude the trailing newline."
3086 (interactive "p")
3087 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
3088 (signal 'end-of-buffer nil))
3089 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
3090 (signal 'beginning-of-buffer nil))
3091 (unless (eq last-command 'kill-region)
3092 (kill-new "")
3093 (setq last-command 'kill-region))
3094 (cond ((zerop arg)
3095 ;; We need to kill in two steps, because the previous command
3096 ;; could have been a kill command, in which case the text
3097 ;; before point needs to be prepended to the current kill
3098 ;; ring entry and the text after point appended. Also, we
3099 ;; need to use save-excursion to avoid copying the same text
3100 ;; twice to the kill ring in read-only buffers.
3101 (save-excursion
3102 (kill-region (point) (progn (forward-visible-line 0) (point))))
3103 (kill-region (point) (progn (end-of-visible-line) (point))))
3104 ((< arg 0)
3105 (save-excursion
3106 (kill-region (point) (progn (end-of-visible-line) (point))))
3107 (kill-region (point)
3108 (progn (forward-visible-line (1+ arg))
3109 (unless (bobp) (backward-char))
3110 (point))))
3112 (save-excursion
3113 (kill-region (point) (progn (forward-visible-line 0) (point))))
3114 (kill-region (point)
3115 (progn (forward-visible-line arg) (point))))))
3117 (defun forward-visible-line (arg)
3118 "Move forward by ARG lines, ignoring currently invisible newlines only.
3119 If ARG is negative, move backward -ARG lines.
3120 If ARG is zero, move to the beginning of the current line."
3121 (condition-case nil
3122 (if (> arg 0)
3123 (progn
3124 (while (> arg 0)
3125 (or (zerop (forward-line 1))
3126 (signal 'end-of-buffer nil))
3127 ;; If the newline we just skipped is invisible,
3128 ;; don't count it.
3129 (let ((prop
3130 (get-char-property (1- (point)) 'invisible)))
3131 (if (if (eq buffer-invisibility-spec t)
3132 prop
3133 (or (memq prop buffer-invisibility-spec)
3134 (assq prop buffer-invisibility-spec)))
3135 (setq arg (1+ arg))))
3136 (setq arg (1- arg)))
3137 ;; If invisible text follows, and it is a number of complete lines,
3138 ;; skip it.
3139 (let ((opoint (point)))
3140 (while (and (not (eobp))
3141 (let ((prop
3142 (get-char-property (point) 'invisible)))
3143 (if (eq buffer-invisibility-spec t)
3144 prop
3145 (or (memq prop buffer-invisibility-spec)
3146 (assq prop buffer-invisibility-spec)))))
3147 (goto-char
3148 (if (get-text-property (point) 'invisible)
3149 (or (next-single-property-change (point) 'invisible)
3150 (point-max))
3151 (next-overlay-change (point)))))
3152 (unless (bolp)
3153 (goto-char opoint))))
3154 (let ((first t))
3155 (while (or first (<= arg 0))
3156 (if first
3157 (beginning-of-line)
3158 (or (zerop (forward-line -1))
3159 (signal 'beginning-of-buffer nil)))
3160 ;; If the newline we just moved to is invisible,
3161 ;; don't count it.
3162 (unless (bobp)
3163 (let ((prop
3164 (get-char-property (1- (point)) 'invisible)))
3165 (unless (if (eq buffer-invisibility-spec t)
3166 prop
3167 (or (memq prop buffer-invisibility-spec)
3168 (assq prop buffer-invisibility-spec)))
3169 (setq arg (1+ arg)))))
3170 (setq first nil))
3171 ;; If invisible text follows, and it is a number of complete lines,
3172 ;; skip it.
3173 (let ((opoint (point)))
3174 (while (and (not (bobp))
3175 (let ((prop
3176 (get-char-property (1- (point)) 'invisible)))
3177 (if (eq buffer-invisibility-spec t)
3178 prop
3179 (or (memq prop buffer-invisibility-spec)
3180 (assq prop buffer-invisibility-spec)))))
3181 (goto-char
3182 (if (get-text-property (1- (point)) 'invisible)
3183 (or (previous-single-property-change (point) 'invisible)
3184 (point-min))
3185 (previous-overlay-change (point)))))
3186 (unless (bolp)
3187 (goto-char opoint)))))
3188 ((beginning-of-buffer end-of-buffer)
3189 nil)))
3191 (defun end-of-visible-line ()
3192 "Move to end of current visible line."
3193 (end-of-line)
3194 ;; If the following character is currently invisible,
3195 ;; skip all characters with that same `invisible' property value,
3196 ;; then find the next newline.
3197 (while (and (not (eobp))
3198 (save-excursion
3199 (skip-chars-forward "^\n")
3200 (let ((prop
3201 (get-char-property (point) 'invisible)))
3202 (if (eq buffer-invisibility-spec t)
3203 prop
3204 (or (memq prop buffer-invisibility-spec)
3205 (assq prop buffer-invisibility-spec))))))
3206 (skip-chars-forward "^\n")
3207 (if (get-text-property (point) 'invisible)
3208 (goto-char (next-single-property-change (point) 'invisible))
3209 (goto-char (next-overlay-change (point))))
3210 (end-of-line)))
3212 (defun insert-buffer (buffer)
3213 "Insert after point the contents of BUFFER.
3214 Puts mark after the inserted text.
3215 BUFFER may be a buffer or a buffer name.
3217 This function is meant for the user to run interactively.
3218 Don't call it from programs: use `insert-buffer-substring' instead!"
3219 (interactive
3220 (list
3221 (progn
3222 (barf-if-buffer-read-only)
3223 (read-buffer "Insert buffer: "
3224 (if (eq (selected-window) (next-window (selected-window)))
3225 (other-buffer (current-buffer))
3226 (window-buffer (next-window (selected-window))))
3227 t))))
3228 (push-mark
3229 (save-excursion
3230 (insert-buffer-substring (get-buffer buffer))
3231 (point)))
3232 nil)
3234 (defun append-to-buffer (buffer start end)
3235 "Append to specified buffer the text of the region.
3236 It is inserted into that buffer before its point.
3238 When calling from a program, give three arguments:
3239 BUFFER (or buffer name), START and END.
3240 START and END specify the portion of the current buffer to be copied."
3241 (interactive
3242 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
3243 (region-beginning) (region-end)))
3244 (let ((oldbuf (current-buffer)))
3245 (save-excursion
3246 (let* ((append-to (get-buffer-create buffer))
3247 (windows (get-buffer-window-list append-to t t))
3248 point)
3249 (set-buffer append-to)
3250 (setq point (point))
3251 (barf-if-buffer-read-only)
3252 (insert-buffer-substring oldbuf start end)
3253 (dolist (window windows)
3254 (when (= (window-point window) point)
3255 (set-window-point window (point))))))))
3257 (defun prepend-to-buffer (buffer start end)
3258 "Prepend to specified buffer the text of the region.
3259 It is inserted into that buffer after its point.
3261 When calling from a program, give three arguments:
3262 BUFFER (or buffer name), START and END.
3263 START and END specify the portion of the current buffer to be copied."
3264 (interactive "BPrepend to buffer: \nr")
3265 (let ((oldbuf (current-buffer)))
3266 (save-excursion
3267 (set-buffer (get-buffer-create buffer))
3268 (barf-if-buffer-read-only)
3269 (save-excursion
3270 (insert-buffer-substring oldbuf start end)))))
3272 (defun copy-to-buffer (buffer start end)
3273 "Copy to specified buffer the text of the region.
3274 It is inserted into that buffer, replacing existing text there.
3276 When calling from a program, give three arguments:
3277 BUFFER (or buffer name), START and END.
3278 START and END specify the portion of the current buffer to be copied."
3279 (interactive "BCopy to buffer: \nr")
3280 (let ((oldbuf (current-buffer)))
3281 (with-current-buffer (get-buffer-create buffer)
3282 (barf-if-buffer-read-only)
3283 (erase-buffer)
3284 (save-excursion
3285 (insert-buffer-substring oldbuf start end)))))
3287 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3288 (put 'mark-inactive 'error-message "The mark is not active now")
3290 (defvar activate-mark-hook nil
3291 "Hook run when the mark becomes active.
3292 It is also run at the end of a command, if the mark is active and
3293 it is possible that the region may have changed.")
3295 (defvar deactivate-mark-hook nil
3296 "Hook run when the mark becomes inactive.")
3298 (defun mark (&optional force)
3299 "Return this buffer's mark value as integer, or nil if never set.
3301 In Transient Mark mode, this function signals an error if
3302 the mark is not active. However, if `mark-even-if-inactive' is non-nil,
3303 or the argument FORCE is non-nil, it disregards whether the mark
3304 is active, and returns an integer or nil in the usual way.
3306 If you are using this in an editing command, you are most likely making
3307 a mistake; see the documentation of `set-mark'."
3308 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3309 (marker-position (mark-marker))
3310 (signal 'mark-inactive nil)))
3312 ;; Many places set mark-active directly, and several of them failed to also
3313 ;; run deactivate-mark-hook. This shorthand should simplify.
3314 (defsubst deactivate-mark ()
3315 "Deactivate the mark by setting `mark-active' to nil.
3316 \(That makes a difference only in Transient Mark mode.)
3317 Also runs the hook `deactivate-mark-hook'."
3318 (cond
3319 ((eq transient-mark-mode 'lambda)
3320 (setq transient-mark-mode nil))
3321 (transient-mark-mode
3322 (setq mark-active nil)
3323 (run-hooks 'deactivate-mark-hook))))
3325 (defcustom select-active-regions nil
3326 "If non-nil, an active region automatically becomes the window selection."
3327 :type 'boolean
3328 :group 'killing
3329 :version "23.1")
3331 (defun set-mark (pos)
3332 "Set this buffer's mark to POS. Don't use this function!
3333 That is to say, don't use this function unless you want
3334 the user to see that the mark has moved, and you want the previous
3335 mark position to be lost.
3337 Normally, when a new mark is set, the old one should go on the stack.
3338 This is why most applications should use `push-mark', not `set-mark'.
3340 Novice Emacs Lisp programmers often try to use the mark for the wrong
3341 purposes. The mark saves a location for the user's convenience.
3342 Most editing commands should not alter the mark.
3343 To remember a location for internal use in the Lisp program,
3344 store it in a Lisp variable. Example:
3346 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3348 (if pos
3349 (progn
3350 (setq mark-active t)
3351 (run-hooks 'activate-mark-hook)
3352 (and select-active-regions
3353 (x-set-selection
3354 nil (buffer-substring (region-beginning) (region-end))))
3355 (set-marker (mark-marker) pos (current-buffer)))
3356 ;; Normally we never clear mark-active except in Transient Mark mode.
3357 ;; But when we actually clear out the mark value too,
3358 ;; we must clear mark-active in any mode.
3359 (setq mark-active nil)
3360 (run-hooks 'deactivate-mark-hook)
3361 (set-marker (mark-marker) nil)))
3363 (defcustom use-empty-active-region nil
3364 "If non-nil, an active region takes control even if empty.
3365 This applies to certain commands which, in Transient Mark mode,
3366 apply to the active region if there is one. If the setting is t,
3367 these commands apply to an empty active region if there is one.
3368 If the setting is nil, these commands treat an empty active
3369 region as if it were not active."
3370 :type 'boolean
3371 :version "23.1"
3372 :group 'editing-basics)
3374 (defun use-region-p ()
3375 "Return t if certain commands should apply to the region.
3376 Certain commands normally apply to text near point,
3377 but in Transient Mark mode when the mark is active they apply
3378 to the region instead. Such commands should use this subroutine to
3379 test whether to do that.
3381 This function also obeys `use-empty-active-region'."
3382 (and transient-mark-mode mark-active
3383 (or use-empty-active-region (> (region-end) (region-beginning)))))
3385 (defun region-active-p ()
3386 "Return t if Transient Mark mode is enabled and the mark is active.
3387 This is NOT the best function to use to test whether a command should
3388 operate on the region instead of the usual behavior -- for that,
3389 use `use-region-p'."
3390 (and transient-mark-mode mark-active))
3392 (defvar mark-ring nil
3393 "The list of former marks of the current buffer, most recent first.")
3394 (make-variable-buffer-local 'mark-ring)
3395 (put 'mark-ring 'permanent-local t)
3397 (defcustom mark-ring-max 16
3398 "*Maximum size of mark ring. Start discarding off end if gets this big."
3399 :type 'integer
3400 :group 'editing-basics)
3402 (defvar global-mark-ring nil
3403 "The list of saved global marks, most recent first.")
3405 (defcustom global-mark-ring-max 16
3406 "*Maximum size of global mark ring. \
3407 Start discarding off end if gets this big."
3408 :type 'integer
3409 :group 'editing-basics)
3411 (defun pop-to-mark-command ()
3412 "Jump to mark, and pop a new position for mark off the ring
3413 \(does not affect global mark ring\)."
3414 (interactive)
3415 (if (null (mark t))
3416 (error "No mark set in this buffer")
3417 (if (= (point) (mark t))
3418 (message "Mark popped"))
3419 (goto-char (mark t))
3420 (pop-mark)))
3422 (defun push-mark-command (arg &optional nomsg)
3423 "Set mark at where point is.
3424 If no prefix arg and mark is already set there, just activate it.
3425 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3426 (interactive "P")
3427 (let ((mark (marker-position (mark-marker))))
3428 (if (or arg (null mark) (/= mark (point)))
3429 (push-mark nil nomsg t)
3430 (setq mark-active t)
3431 (run-hooks 'activate-mark-hook)
3432 (unless nomsg
3433 (message "Mark activated")))))
3435 (defcustom set-mark-command-repeat-pop nil
3436 "*Non-nil means repeating \\[set-mark-command] after popping mark pops it again.
3437 That means that C-u \\[set-mark-command] \\[set-mark-command]
3438 will pop the mark twice, and
3439 C-u \\[set-mark-command] \\[set-mark-command] \\[set-mark-command]
3440 will pop the mark three times.
3442 A value of nil means \\[set-mark-command]'s behavior does not change
3443 after C-u \\[set-mark-command]."
3444 :type 'boolean
3445 :group 'editing-basics)
3447 (defun set-mark-command (arg)
3448 "Set the mark where point is, or jump to the mark.
3449 Setting the mark also alters the region, which is the text
3450 between point and mark; this is the closest equivalent in
3451 Emacs to what some editors call the \"selection\".
3453 With no prefix argument, set the mark at point, and push the
3454 old mark position on local mark ring. Also push the old mark on
3455 global mark ring, if the previous mark was set in another buffer.
3457 When Transient Mark Mode is off, immediately repeating this
3458 command activates `transient-mark-mode' temporarily.
3460 With prefix argument \(e.g., \\[universal-argument] \\[set-mark-command]\), \
3461 jump to the mark, and set the mark from
3462 position popped off the local mark ring \(this does not affect the global
3463 mark ring\). Use \\[pop-global-mark] to jump to a mark popped off the global
3464 mark ring \(see `pop-global-mark'\).
3466 If `set-mark-command-repeat-pop' is non-nil, repeating
3467 the \\[set-mark-command] command with no prefix argument pops the next position
3468 off the local (or global) mark ring and jumps there.
3470 With \\[universal-argument] \\[universal-argument] as prefix
3471 argument, unconditionally set mark where point is, even if
3472 `set-mark-command-repeat-pop' is non-nil.
3474 Novice Emacs Lisp programmers often try to use the mark for the wrong
3475 purposes. See the documentation of `set-mark' for more information."
3476 (interactive "P")
3477 (if (eq transient-mark-mode 'lambda)
3478 (setq transient-mark-mode nil))
3479 (cond
3480 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3481 (push-mark-command nil))
3482 ((not (eq this-command 'set-mark-command))
3483 (if arg
3484 (pop-to-mark-command)
3485 (push-mark-command t)))
3486 ((and set-mark-command-repeat-pop
3487 (eq last-command 'pop-to-mark-command))
3488 (setq this-command 'pop-to-mark-command)
3489 (pop-to-mark-command))
3490 ((and set-mark-command-repeat-pop
3491 (eq last-command 'pop-global-mark)
3492 (not arg))
3493 (setq this-command 'pop-global-mark)
3494 (pop-global-mark))
3495 (arg
3496 (setq this-command 'pop-to-mark-command)
3497 (pop-to-mark-command))
3498 ((and (eq last-command 'set-mark-command)
3499 mark-active (null transient-mark-mode))
3500 (setq transient-mark-mode 'lambda)
3501 (message "Transient-mark-mode temporarily enabled"))
3502 ((and (eq last-command 'set-mark-command)
3503 transient-mark-mode)
3504 (deactivate-mark))
3506 (push-mark-command nil))))
3508 (defun push-mark (&optional location nomsg activate)
3509 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3510 If the last global mark pushed was not in the current buffer,
3511 also push LOCATION on the global mark ring.
3512 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3514 Novice Emacs Lisp programmers often try to use the mark for the wrong
3515 purposes. See the documentation of `set-mark' for more information.
3517 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil."
3518 (unless (null (mark t))
3519 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3520 (when (> (length mark-ring) mark-ring-max)
3521 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3522 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3523 (set-marker (mark-marker) (or location (point)) (current-buffer))
3524 ;; Now push the mark on the global mark ring.
3525 (if (and global-mark-ring
3526 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3527 ;; The last global mark pushed was in this same buffer.
3528 ;; Don't push another one.
3530 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3531 (when (> (length global-mark-ring) global-mark-ring-max)
3532 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3533 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3534 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3535 (message "Mark set"))
3536 (if (or activate (not transient-mark-mode))
3537 (set-mark (mark t)))
3538 nil)
3540 (defun pop-mark ()
3541 "Pop off mark ring into the buffer's actual mark.
3542 Does not set point. Does nothing if mark ring is empty."
3543 (when mark-ring
3544 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3545 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3546 (move-marker (car mark-ring) nil)
3547 (if (null (mark t)) (ding))
3548 (setq mark-ring (cdr mark-ring)))
3549 (deactivate-mark))
3551 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3552 (defun exchange-point-and-mark (&optional arg)
3553 "Put the mark where point is now, and point where the mark is now.
3554 This command works even when the mark is not active,
3555 and it reactivates the mark.
3556 With prefix arg, `transient-mark-mode' is enabled temporarily."
3557 (interactive "P")
3558 (if arg
3559 (if mark-active
3560 (if (null transient-mark-mode)
3561 (setq transient-mark-mode 'lambda))
3562 (setq arg nil)))
3563 (unless arg
3564 (let ((omark (mark t)))
3565 (if (null omark)
3566 (error "No mark set in this buffer"))
3567 (set-mark (point))
3568 (goto-char omark)
3569 nil)))
3571 (define-minor-mode transient-mark-mode
3572 "Toggle Transient Mark mode.
3573 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3575 In Transient Mark mode, when the mark is active, the region is highlighted.
3576 Changing the buffer \"deactivates\" the mark.
3577 So do certain other operations that set the mark
3578 but whose main purpose is something else--for example,
3579 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3581 You can also deactivate the mark by typing \\[keyboard-quit] or
3582 \\[keyboard-escape-quit].
3584 Many commands change their behavior when Transient Mark mode is in effect
3585 and the mark is active, by acting on the region instead of their usual
3586 default part of the buffer's text. Examples of such commands include
3587 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3588 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3589 Invoke \\[apropos-documentation] and type \"transient\" or
3590 \"mark.*active\" at the prompt, to see the documentation of
3591 commands which are sensitive to the Transient Mark mode."
3592 :global t
3593 ;;; :init-value (not noninteractive)
3594 :group 'editing-basics)
3596 (defvar widen-automatically t
3597 "Non-nil means it is ok for commands to call `widen' when they want to.
3598 Some commands will do this in order to go to positions outside
3599 the current accessible part of the buffer.
3601 If `widen-automatically' is nil, these commands will do something else
3602 as a fallback, and won't change the buffer bounds.")
3604 (defun pop-global-mark ()
3605 "Pop off global mark ring and jump to the top location."
3606 (interactive)
3607 ;; Pop entries which refer to non-existent buffers.
3608 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3609 (setq global-mark-ring (cdr global-mark-ring)))
3610 (or global-mark-ring
3611 (error "No global mark set"))
3612 (let* ((marker (car global-mark-ring))
3613 (buffer (marker-buffer marker))
3614 (position (marker-position marker)))
3615 (setq global-mark-ring (nconc (cdr global-mark-ring)
3616 (list (car global-mark-ring))))
3617 (set-buffer buffer)
3618 (or (and (>= position (point-min))
3619 (<= position (point-max)))
3620 (if widen-automatically
3621 (widen)
3622 (error "Global mark position is outside accessible part of buffer")))
3623 (goto-char position)
3624 (switch-to-buffer buffer)))
3626 (defcustom next-line-add-newlines nil
3627 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3628 :type 'boolean
3629 :version "21.1"
3630 :group 'editing-basics)
3632 (defun next-line (&optional arg try-vscroll)
3633 "Move cursor vertically down ARG lines.
3634 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3635 If there is no character in the target line exactly under the current column,
3636 the cursor is positioned after the character in that line which spans this
3637 column, or at the end of the line if it is not long enough.
3638 If there is no line in the buffer after this one, behavior depends on the
3639 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3640 to create a line, and moves the cursor to that line. Otherwise it moves the
3641 cursor to the end of the buffer.
3643 The command \\[set-goal-column] can be used to create
3644 a semipermanent goal column for this command.
3645 Then instead of trying to move exactly vertically (or as close as possible),
3646 this command moves to the specified goal column (or as close as possible).
3647 The goal column is stored in the variable `goal-column', which is nil
3648 when there is no goal column.
3650 If you are thinking of using this in a Lisp program, consider
3651 using `forward-line' instead. It is usually easier to use
3652 and more reliable (no dependence on goal column, etc.)."
3653 (interactive "p\np")
3654 (or arg (setq arg 1))
3655 (if (and next-line-add-newlines (= arg 1))
3656 (if (save-excursion (end-of-line) (eobp))
3657 ;; When adding a newline, don't expand an abbrev.
3658 (let ((abbrev-mode nil))
3659 (end-of-line)
3660 (insert (if use-hard-newlines hard-newline "\n")))
3661 (line-move arg nil nil try-vscroll))
3662 (if (interactive-p)
3663 (condition-case nil
3664 (line-move arg nil nil try-vscroll)
3665 ((beginning-of-buffer end-of-buffer) (ding)))
3666 (line-move arg nil nil try-vscroll)))
3667 nil)
3669 (defun previous-line (&optional arg try-vscroll)
3670 "Move cursor vertically up ARG lines.
3671 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3672 If there is no character in the target line exactly over the current column,
3673 the cursor is positioned after the character in that line which spans this
3674 column, or at the end of the line if it is not long enough.
3676 The command \\[set-goal-column] can be used to create
3677 a semipermanent goal column for this command.
3678 Then instead of trying to move exactly vertically (or as close as possible),
3679 this command moves to the specified goal column (or as close as possible).
3680 The goal column is stored in the variable `goal-column', which is nil
3681 when there is no goal column.
3683 If you are thinking of using this in a Lisp program, consider using
3684 `forward-line' with a negative argument instead. It is usually easier
3685 to use and more reliable (no dependence on goal column, etc.)."
3686 (interactive "p\np")
3687 (or arg (setq arg 1))
3688 (if (interactive-p)
3689 (condition-case nil
3690 (line-move (- arg) nil nil try-vscroll)
3691 ((beginning-of-buffer end-of-buffer) (ding)))
3692 (line-move (- arg) nil nil try-vscroll))
3693 nil)
3695 (defcustom track-eol nil
3696 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3697 This means moving to the end of each line moved onto.
3698 The beginning of a blank line does not count as the end of a line."
3699 :type 'boolean
3700 :group 'editing-basics)
3702 (defcustom goal-column nil
3703 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3704 :type '(choice integer
3705 (const :tag "None" nil))
3706 :group 'editing-basics)
3707 (make-variable-buffer-local 'goal-column)
3709 (defvar temporary-goal-column 0
3710 "Current goal column for vertical motion.
3711 It is the column where point was
3712 at the start of current run of vertical motion commands.
3713 When the `track-eol' feature is doing its job, the value is `most-positive-fixnum'.")
3715 (defcustom line-move-ignore-invisible t
3716 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3717 Outline mode sets this."
3718 :type 'boolean
3719 :group 'editing-basics)
3721 ;; Returns non-nil if partial move was done.
3722 (defun line-move-partial (arg noerror to-end)
3723 (if (< arg 0)
3724 ;; Move backward (up).
3725 ;; If already vscrolled, reduce vscroll
3726 (let ((vs (window-vscroll nil t)))
3727 (when (> vs (frame-char-height))
3728 (set-window-vscroll nil (- vs (frame-char-height)) t)))
3730 ;; Move forward (down).
3731 (let* ((lh (window-line-height -1))
3732 (vpos (nth 1 lh))
3733 (ypos (nth 2 lh))
3734 (rbot (nth 3 lh))
3735 py vs)
3736 (when (or (null lh)
3737 (>= rbot (frame-char-height))
3738 (<= ypos (- (frame-char-height))))
3739 (unless lh
3740 (let ((wend (pos-visible-in-window-p t nil t)))
3741 (setq rbot (nth 3 wend)
3742 vpos (nth 5 wend))))
3743 (cond
3744 ;; If last line of window is fully visible, move forward.
3745 ((or (null rbot) (= rbot 0))
3746 nil)
3747 ;; If cursor is not in the bottom scroll margin, move forward.
3748 ((and (> vpos 0)
3749 (< (setq py
3750 (or (nth 1 (window-line-height))
3751 (let ((ppos (posn-at-point)))
3752 (cdr (or (posn-actual-col-row ppos)
3753 (posn-col-row ppos))))))
3754 (min (- (window-text-height) scroll-margin 1) (1- vpos))))
3755 nil)
3756 ;; When already vscrolled, we vscroll some more if we can,
3757 ;; or clear vscroll and move forward at end of tall image.
3758 ((> (setq vs (window-vscroll nil t)) 0)
3759 (when (> rbot 0)
3760 (set-window-vscroll nil (+ vs (min rbot (frame-char-height))) t)))
3761 ;; If cursor just entered the bottom scroll margin, move forward,
3762 ;; but also vscroll one line so redisplay wont recenter.
3763 ((and (> vpos 0)
3764 (= py (min (- (window-text-height) scroll-margin 1)
3765 (1- vpos))))
3766 (set-window-vscroll nil (frame-char-height) t)
3767 (line-move-1 arg noerror to-end)
3769 ;; If there are lines above the last line, scroll-up one line.
3770 ((> vpos 0)
3771 (scroll-up 1)
3773 ;; Finally, start vscroll.
3775 (set-window-vscroll nil (frame-char-height) t)))))))
3778 ;; This is like line-move-1 except that it also performs
3779 ;; vertical scrolling of tall images if appropriate.
3780 ;; That is not really a clean thing to do, since it mixes
3781 ;; scrolling with cursor motion. But so far we don't have
3782 ;; a cleaner solution to the problem of making C-n do something
3783 ;; useful given a tall image.
3784 (defun line-move (arg &optional noerror to-end try-vscroll)
3785 (unless (and auto-window-vscroll try-vscroll
3786 ;; Only vscroll for single line moves
3787 (= (abs arg) 1)
3788 ;; But don't vscroll in a keyboard macro.
3789 (not defining-kbd-macro)
3790 (not executing-kbd-macro)
3791 (line-move-partial arg noerror to-end))
3792 (set-window-vscroll nil 0 t)
3793 (line-move-1 arg noerror to-end)))
3795 ;; This is the guts of next-line and previous-line.
3796 ;; Arg says how many lines to move.
3797 ;; The value is t if we can move the specified number of lines.
3798 (defun line-move-1 (arg &optional noerror to-end)
3799 ;; Don't run any point-motion hooks, and disregard intangibility,
3800 ;; for intermediate positions.
3801 (let ((inhibit-point-motion-hooks t)
3802 (opoint (point))
3803 (orig-arg arg))
3804 (unwind-protect
3805 (progn
3806 (if (not (memq last-command '(next-line previous-line)))
3807 (setq temporary-goal-column
3808 (if (and track-eol (eolp)
3809 ;; Don't count beg of empty line as end of line
3810 ;; unless we just did explicit end-of-line.
3811 (or (not (bolp)) (eq last-command 'move-end-of-line)))
3812 most-positive-fixnum
3813 (current-column))))
3815 (if (not (or (integerp selective-display)
3816 line-move-ignore-invisible))
3817 ;; Use just newline characters.
3818 ;; Set ARG to 0 if we move as many lines as requested.
3819 (or (if (> arg 0)
3820 (progn (if (> arg 1) (forward-line (1- arg)))
3821 ;; This way of moving forward ARG lines
3822 ;; verifies that we have a newline after the last one.
3823 ;; It doesn't get confused by intangible text.
3824 (end-of-line)
3825 (if (zerop (forward-line 1))
3826 (setq arg 0)))
3827 (and (zerop (forward-line arg))
3828 (bolp)
3829 (setq arg 0)))
3830 (unless noerror
3831 (signal (if (< arg 0)
3832 'beginning-of-buffer
3833 'end-of-buffer)
3834 nil)))
3835 ;; Move by arg lines, but ignore invisible ones.
3836 (let (done)
3837 (while (and (> arg 0) (not done))
3838 ;; If the following character is currently invisible,
3839 ;; skip all characters with that same `invisible' property value.
3840 (while (and (not (eobp)) (invisible-p (point)))
3841 (goto-char (next-char-property-change (point))))
3842 ;; Move a line.
3843 ;; We don't use `end-of-line', since we want to escape
3844 ;; from field boundaries ocurring exactly at point.
3845 (goto-char (constrain-to-field
3846 (let ((inhibit-field-text-motion t))
3847 (line-end-position))
3848 (point) t t
3849 'inhibit-line-move-field-capture))
3850 ;; If there's no invisibility here, move over the newline.
3851 (cond
3852 ((eobp)
3853 (if (not noerror)
3854 (signal 'end-of-buffer nil)
3855 (setq done t)))
3856 ((and (> arg 1) ;; Use vertical-motion for last move
3857 (not (integerp selective-display))
3858 (not (invisible-p (point))))
3859 ;; We avoid vertical-motion when possible
3860 ;; because that has to fontify.
3861 (forward-line 1))
3862 ;; Otherwise move a more sophisticated way.
3863 ((zerop (vertical-motion 1))
3864 (if (not noerror)
3865 (signal 'end-of-buffer nil)
3866 (setq done t))))
3867 (unless done
3868 (setq arg (1- arg))))
3869 ;; The logic of this is the same as the loop above,
3870 ;; it just goes in the other direction.
3871 (while (and (< arg 0) (not done))
3872 ;; For completely consistency with the forward-motion
3873 ;; case, we should call beginning-of-line here.
3874 ;; However, if point is inside a field and on a
3875 ;; continued line, the call to (vertical-motion -1)
3876 ;; below won't move us back far enough; then we return
3877 ;; to the same column in line-move-finish, and point
3878 ;; gets stuck -- cyd
3879 (forward-line 0)
3880 (cond
3881 ((bobp)
3882 (if (not noerror)
3883 (signal 'beginning-of-buffer nil)
3884 (setq done t)))
3885 ((and (< arg -1) ;; Use vertical-motion for last move
3886 (not (integerp selective-display))
3887 (not (invisible-p (1- (point)))))
3888 (forward-line -1))
3889 ((zerop (vertical-motion -1))
3890 (if (not noerror)
3891 (signal 'beginning-of-buffer nil)
3892 (setq done t))))
3893 (unless done
3894 (setq arg (1+ arg))
3895 (while (and ;; Don't move over previous invis lines
3896 ;; if our target is the middle of this line.
3897 (or (zerop (or goal-column temporary-goal-column))
3898 (< arg 0))
3899 (not (bobp)) (invisible-p (1- (point))))
3900 (goto-char (previous-char-property-change (point))))))))
3901 ;; This is the value the function returns.
3902 (= arg 0))
3904 (cond ((> arg 0)
3905 ;; If we did not move down as far as desired,
3906 ;; at least go to end of line.
3907 (end-of-line))
3908 ((< arg 0)
3909 ;; If we did not move up as far as desired,
3910 ;; at least go to beginning of line.
3911 (beginning-of-line))
3913 (line-move-finish (or goal-column temporary-goal-column)
3914 opoint (> orig-arg 0)))))))
3916 (defun line-move-finish (column opoint forward)
3917 (let ((repeat t))
3918 (while repeat
3919 ;; Set REPEAT to t to repeat the whole thing.
3920 (setq repeat nil)
3922 (let (new
3923 (old (point))
3924 (line-beg (save-excursion (beginning-of-line) (point)))
3925 (line-end
3926 ;; Compute the end of the line
3927 ;; ignoring effectively invisible newlines.
3928 (save-excursion
3929 ;; Like end-of-line but ignores fields.
3930 (skip-chars-forward "^\n")
3931 (while (and (not (eobp)) (invisible-p (point)))
3932 (goto-char (next-char-property-change (point)))
3933 (skip-chars-forward "^\n"))
3934 (point))))
3936 ;; Move to the desired column.
3937 (line-move-to-column column)
3939 ;; Corner case: suppose we start out in a field boundary in
3940 ;; the middle of a continued line. When we get to
3941 ;; line-move-finish, point is at the start of a new *screen*
3942 ;; line but the same text line; then line-move-to-column would
3943 ;; move us backwards. Test using C-n with point on the "x" in
3944 ;; (insert "a" (propertize "x" 'field t) (make-string 89 ?y))
3945 (and forward
3946 (< (point) old)
3947 (goto-char old))
3949 (setq new (point))
3951 ;; Process intangibility within a line.
3952 ;; With inhibit-point-motion-hooks bound to nil, a call to
3953 ;; goto-char moves point past intangible text.
3955 ;; However, inhibit-point-motion-hooks controls both the
3956 ;; intangibility and the point-entered/point-left hooks. The
3957 ;; following hack avoids calling the point-* hooks
3958 ;; unnecessarily. Note that we move *forward* past intangible
3959 ;; text when the initial and final points are the same.
3960 (goto-char new)
3961 (let ((inhibit-point-motion-hooks nil))
3962 (goto-char new)
3964 ;; If intangibility moves us to a different (later) place
3965 ;; in the same line, use that as the destination.
3966 (if (<= (point) line-end)
3967 (setq new (point))
3968 ;; If that position is "too late",
3969 ;; try the previous allowable position.
3970 ;; See if it is ok.
3971 (backward-char)
3972 (if (if forward
3973 ;; If going forward, don't accept the previous
3974 ;; allowable position if it is before the target line.
3975 (< line-beg (point))
3976 ;; If going backward, don't accept the previous
3977 ;; allowable position if it is still after the target line.
3978 (<= (point) line-end))
3979 (setq new (point))
3980 ;; As a last resort, use the end of the line.
3981 (setq new line-end))))
3983 ;; Now move to the updated destination, processing fields
3984 ;; as well as intangibility.
3985 (goto-char opoint)
3986 (let ((inhibit-point-motion-hooks nil))
3987 (goto-char
3988 ;; Ignore field boundaries if the initial and final
3989 ;; positions have the same `field' property, even if the
3990 ;; fields are non-contiguous. This seems to be "nicer"
3991 ;; behavior in many situations.
3992 (if (eq (get-char-property new 'field)
3993 (get-char-property opoint 'field))
3995 (constrain-to-field new opoint t t
3996 'inhibit-line-move-field-capture))))
3998 ;; If all this moved us to a different line,
3999 ;; retry everything within that new line.
4000 (when (or (< (point) line-beg) (> (point) line-end))
4001 ;; Repeat the intangibility and field processing.
4002 (setq repeat t))))))
4004 (defun line-move-to-column (col)
4005 "Try to find column COL, considering invisibility.
4006 This function works only in certain cases,
4007 because what we really need is for `move-to-column'
4008 and `current-column' to be able to ignore invisible text."
4009 (if (zerop col)
4010 (beginning-of-line)
4011 (move-to-column col))
4013 (when (and line-move-ignore-invisible
4014 (not (bolp)) (invisible-p (1- (point))))
4015 (let ((normal-location (point))
4016 (normal-column (current-column)))
4017 ;; If the following character is currently invisible,
4018 ;; skip all characters with that same `invisible' property value.
4019 (while (and (not (eobp))
4020 (invisible-p (point)))
4021 (goto-char (next-char-property-change (point))))
4022 ;; Have we advanced to a larger column position?
4023 (if (> (current-column) normal-column)
4024 ;; We have made some progress towards the desired column.
4025 ;; See if we can make any further progress.
4026 (line-move-to-column (+ (current-column) (- col normal-column)))
4027 ;; Otherwise, go to the place we originally found
4028 ;; and move back over invisible text.
4029 ;; that will get us to the same place on the screen
4030 ;; but with a more reasonable buffer position.
4031 (goto-char normal-location)
4032 (let ((line-beg (save-excursion (beginning-of-line) (point))))
4033 (while (and (not (bolp)) (invisible-p (1- (point))))
4034 (goto-char (previous-char-property-change (point) line-beg))))))))
4036 (defun move-end-of-line (arg)
4037 "Move point to end of current line as displayed.
4038 \(If there's an image in the line, this disregards newlines
4039 which are part of the text that the image rests on.)
4041 With argument ARG not nil or 1, move forward ARG - 1 lines first.
4042 If point reaches the beginning or end of buffer, it stops there.
4043 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4044 (interactive "p")
4045 (or arg (setq arg 1))
4046 (let (done)
4047 (while (not done)
4048 (let ((newpos
4049 (save-excursion
4050 (let ((goal-column 0))
4051 (and (line-move arg t)
4052 (not (bobp))
4053 (progn
4054 (while (and (not (bobp)) (invisible-p (1- (point))))
4055 (goto-char (previous-single-char-property-change
4056 (point) 'invisible)))
4057 (backward-char 1)))
4058 (point)))))
4059 (goto-char newpos)
4060 (if (and (> (point) newpos)
4061 (eq (preceding-char) ?\n))
4062 (backward-char 1)
4063 (if (and (> (point) newpos) (not (eobp))
4064 (not (eq (following-char) ?\n)))
4065 ;; If we skipped something intangible
4066 ;; and now we're not really at eol,
4067 ;; keep going.
4068 (setq arg 1)
4069 (setq done t)))))))
4071 (defun move-beginning-of-line (arg)
4072 "Move point to beginning of current line as displayed.
4073 \(If there's an image in the line, this disregards newlines
4074 which are part of the text that the image rests on.)
4076 With argument ARG not nil or 1, move forward ARG - 1 lines first.
4077 If point reaches the beginning or end of buffer, it stops there.
4078 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
4079 (interactive "p")
4080 (or arg (setq arg 1))
4082 (let ((orig (point))
4083 first-vis first-vis-field-value)
4085 ;; Move by lines, if ARG is not 1 (the default).
4086 (if (/= arg 1)
4087 (line-move (1- arg) t))
4089 ;; Move to beginning-of-line, ignoring fields and invisibles.
4090 (skip-chars-backward "^\n")
4091 (while (and (not (bobp)) (invisible-p (1- (point))))
4092 (goto-char (previous-char-property-change (point)))
4093 (skip-chars-backward "^\n"))
4095 ;; Now find first visible char in the line
4096 (while (and (not (eobp)) (invisible-p (point)))
4097 (goto-char (next-char-property-change (point))))
4098 (setq first-vis (point))
4100 ;; See if fields would stop us from reaching FIRST-VIS.
4101 (setq first-vis-field-value
4102 (constrain-to-field first-vis orig (/= arg 1) t nil))
4104 (goto-char (if (/= first-vis-field-value first-vis)
4105 ;; If yes, obey them.
4106 first-vis-field-value
4107 ;; Otherwise, move to START with attention to fields.
4108 ;; (It is possible that fields never matter in this case.)
4109 (constrain-to-field (point) orig
4110 (/= arg 1) t nil)))))
4113 ;;; Many people have said they rarely use this feature, and often type
4114 ;;; it by accident. Maybe it shouldn't even be on a key.
4115 (put 'set-goal-column 'disabled t)
4117 (defun set-goal-column (arg)
4118 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
4119 Those commands will move to this position in the line moved to
4120 rather than trying to keep the same horizontal position.
4121 With a non-nil argument, clears out the goal column
4122 so that \\[next-line] and \\[previous-line] resume vertical motion.
4123 The goal column is stored in the variable `goal-column'."
4124 (interactive "P")
4125 (if arg
4126 (progn
4127 (setq goal-column nil)
4128 (message "No goal column"))
4129 (setq goal-column (current-column))
4130 ;; The older method below can be erroneous if `set-goal-column' is bound
4131 ;; to a sequence containing %
4132 ;;(message (substitute-command-keys
4133 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
4134 ;;goal-column)
4135 (message "%s"
4136 (concat
4137 (format "Goal column %d " goal-column)
4138 (substitute-command-keys
4139 "(use \\[set-goal-column] with an arg to unset it)")))
4142 nil)
4145 (defun scroll-other-window-down (lines)
4146 "Scroll the \"other window\" down.
4147 For more details, see the documentation for `scroll-other-window'."
4148 (interactive "P")
4149 (scroll-other-window
4150 ;; Just invert the argument's meaning.
4151 ;; We can do that without knowing which window it will be.
4152 (if (eq lines '-) nil
4153 (if (null lines) '-
4154 (- (prefix-numeric-value lines))))))
4156 (defun beginning-of-buffer-other-window (arg)
4157 "Move point to the beginning of the buffer in the other window.
4158 Leave mark at previous position.
4159 With arg N, put point N/10 of the way from the true beginning."
4160 (interactive "P")
4161 (let ((orig-window (selected-window))
4162 (window (other-window-for-scrolling)))
4163 ;; We use unwind-protect rather than save-window-excursion
4164 ;; because the latter would preserve the things we want to change.
4165 (unwind-protect
4166 (progn
4167 (select-window window)
4168 ;; Set point and mark in that window's buffer.
4169 (with-no-warnings
4170 (beginning-of-buffer arg))
4171 ;; Set point accordingly.
4172 (recenter '(t)))
4173 (select-window orig-window))))
4175 (defun end-of-buffer-other-window (arg)
4176 "Move point to the end of the buffer in the other window.
4177 Leave mark at previous position.
4178 With arg N, put point N/10 of the way from the true end."
4179 (interactive "P")
4180 ;; See beginning-of-buffer-other-window for comments.
4181 (let ((orig-window (selected-window))
4182 (window (other-window-for-scrolling)))
4183 (unwind-protect
4184 (progn
4185 (select-window window)
4186 (with-no-warnings
4187 (end-of-buffer arg))
4188 (recenter '(t)))
4189 (select-window orig-window))))
4191 (defun transpose-chars (arg)
4192 "Interchange characters around point, moving forward one character.
4193 With prefix arg ARG, effect is to take character before point
4194 and drag it forward past ARG other characters (backward if ARG negative).
4195 If no argument and at end of line, the previous two chars are exchanged."
4196 (interactive "*P")
4197 (and (null arg) (eolp) (forward-char -1))
4198 (transpose-subr 'forward-char (prefix-numeric-value arg)))
4200 (defun transpose-words (arg)
4201 "Interchange words around point, leaving point at end of them.
4202 With prefix arg ARG, effect is to take word before or around point
4203 and drag it forward past ARG other words (backward if ARG negative).
4204 If ARG is zero, the words around or after point and around or after mark
4205 are interchanged."
4206 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
4207 (interactive "*p")
4208 (transpose-subr 'forward-word arg))
4210 (defun transpose-sexps (arg)
4211 "Like \\[transpose-words] but applies to sexps.
4212 Does not work on a sexp that point is in the middle of
4213 if it is a list or string."
4214 (interactive "*p")
4215 (transpose-subr
4216 (lambda (arg)
4217 ;; Here we should try to simulate the behavior of
4218 ;; (cons (progn (forward-sexp x) (point))
4219 ;; (progn (forward-sexp (- x)) (point)))
4220 ;; Except that we don't want to rely on the second forward-sexp
4221 ;; putting us back to where we want to be, since forward-sexp-function
4222 ;; might do funny things like infix-precedence.
4223 (if (if (> arg 0)
4224 (looking-at "\\sw\\|\\s_")
4225 (and (not (bobp))
4226 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
4227 ;; Jumping over a symbol. We might be inside it, mind you.
4228 (progn (funcall (if (> arg 0)
4229 'skip-syntax-backward 'skip-syntax-forward)
4230 "w_")
4231 (cons (save-excursion (forward-sexp arg) (point)) (point)))
4232 ;; Otherwise, we're between sexps. Take a step back before jumping
4233 ;; to make sure we'll obey the same precedence no matter which direction
4234 ;; we're going.
4235 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
4236 (cons (save-excursion (forward-sexp arg) (point))
4237 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
4238 (not (zerop (funcall (if (> arg 0)
4239 'skip-syntax-forward
4240 'skip-syntax-backward)
4241 ".")))))
4242 (point)))))
4243 arg 'special))
4245 (defun transpose-lines (arg)
4246 "Exchange current line and previous line, leaving point after both.
4247 With argument ARG, takes previous line and moves it past ARG lines.
4248 With argument 0, interchanges line point is in with line mark is in."
4249 (interactive "*p")
4250 (transpose-subr (function
4251 (lambda (arg)
4252 (if (> arg 0)
4253 (progn
4254 ;; Move forward over ARG lines,
4255 ;; but create newlines if necessary.
4256 (setq arg (forward-line arg))
4257 (if (/= (preceding-char) ?\n)
4258 (setq arg (1+ arg)))
4259 (if (> arg 0)
4260 (newline arg)))
4261 (forward-line arg))))
4262 arg))
4264 (defun transpose-subr (mover arg &optional special)
4265 (let ((aux (if special mover
4266 (lambda (x)
4267 (cons (progn (funcall mover x) (point))
4268 (progn (funcall mover (- x)) (point))))))
4269 pos1 pos2)
4270 (cond
4271 ((= arg 0)
4272 (save-excursion
4273 (setq pos1 (funcall aux 1))
4274 (goto-char (mark))
4275 (setq pos2 (funcall aux 1))
4276 (transpose-subr-1 pos1 pos2))
4277 (exchange-point-and-mark))
4278 ((> arg 0)
4279 (setq pos1 (funcall aux -1))
4280 (setq pos2 (funcall aux arg))
4281 (transpose-subr-1 pos1 pos2)
4282 (goto-char (car pos2)))
4284 (setq pos1 (funcall aux -1))
4285 (goto-char (car pos1))
4286 (setq pos2 (funcall aux arg))
4287 (transpose-subr-1 pos1 pos2)))))
4289 (defun transpose-subr-1 (pos1 pos2)
4290 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
4291 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
4292 (when (> (car pos1) (car pos2))
4293 (let ((swap pos1))
4294 (setq pos1 pos2 pos2 swap)))
4295 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
4296 (atomic-change-group
4297 (let (word2)
4298 ;; FIXME: We first delete the two pieces of text, so markers that
4299 ;; used to point to after the text end up pointing to before it :-(
4300 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
4301 (goto-char (car pos2))
4302 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
4303 (goto-char (car pos1))
4304 (insert word2))))
4306 (defun backward-word (&optional arg)
4307 "Move backward until encountering the beginning of a word.
4308 With argument, do this that many times."
4309 (interactive "p")
4310 (forward-word (- (or arg 1))))
4312 (defun mark-word (&optional arg allow-extend)
4313 "Set mark ARG words away from point.
4314 The place mark goes is the same place \\[forward-word] would
4315 move to with the same argument.
4316 Interactively, if this command is repeated
4317 or (in Transient Mark mode) if the mark is active,
4318 it marks the next ARG words after the ones already marked."
4319 (interactive "P\np")
4320 (cond ((and allow-extend
4321 (or (and (eq last-command this-command) (mark t))
4322 (and transient-mark-mode mark-active)))
4323 (setq arg (if arg (prefix-numeric-value arg)
4324 (if (< (mark) (point)) -1 1)))
4325 (set-mark
4326 (save-excursion
4327 (goto-char (mark))
4328 (forward-word arg)
4329 (point))))
4331 (push-mark
4332 (save-excursion
4333 (forward-word (prefix-numeric-value arg))
4334 (point))
4335 nil t))))
4337 (defun kill-word (arg)
4338 "Kill characters forward until encountering the end of a word.
4339 With argument, do this that many times."
4340 (interactive "p")
4341 (kill-region (point) (progn (forward-word arg) (point))))
4343 (defun backward-kill-word (arg)
4344 "Kill characters backward until encountering the beginning of a word.
4345 With argument, do this that many times."
4346 (interactive "p")
4347 (kill-word (- arg)))
4349 (defun current-word (&optional strict really-word)
4350 "Return the symbol or word that point is on (or a nearby one) as a string.
4351 The return value includes no text properties.
4352 If optional arg STRICT is non-nil, return nil unless point is within
4353 or adjacent to a symbol or word. In all cases the value can be nil
4354 if there is no word nearby.
4355 The function, belying its name, normally finds a symbol.
4356 If optional arg REALLY-WORD is non-nil, it finds just a word."
4357 (save-excursion
4358 (let* ((oldpoint (point)) (start (point)) (end (point))
4359 (syntaxes (if really-word "w" "w_"))
4360 (not-syntaxes (concat "^" syntaxes)))
4361 (skip-syntax-backward syntaxes) (setq start (point))
4362 (goto-char oldpoint)
4363 (skip-syntax-forward syntaxes) (setq end (point))
4364 (when (and (eq start oldpoint) (eq end oldpoint)
4365 ;; Point is neither within nor adjacent to a word.
4366 (not strict))
4367 ;; Look for preceding word in same line.
4368 (skip-syntax-backward not-syntaxes
4369 (save-excursion (beginning-of-line)
4370 (point)))
4371 (if (bolp)
4372 ;; No preceding word in same line.
4373 ;; Look for following word in same line.
4374 (progn
4375 (skip-syntax-forward not-syntaxes
4376 (save-excursion (end-of-line)
4377 (point)))
4378 (setq start (point))
4379 (skip-syntax-forward syntaxes)
4380 (setq end (point)))
4381 (setq end (point))
4382 (skip-syntax-backward syntaxes)
4383 (setq start (point))))
4384 ;; If we found something nonempty, return it as a string.
4385 (unless (= start end)
4386 (buffer-substring-no-properties start end)))))
4388 (defcustom fill-prefix nil
4389 "*String for filling to insert at front of new line, or nil for none."
4390 :type '(choice (const :tag "None" nil)
4391 string)
4392 :group 'fill)
4393 (make-variable-buffer-local 'fill-prefix)
4394 (put 'fill-prefix 'safe-local-variable 'string-or-null-p)
4396 (defcustom auto-fill-inhibit-regexp nil
4397 "*Regexp to match lines which should not be auto-filled."
4398 :type '(choice (const :tag "None" nil)
4399 regexp)
4400 :group 'fill)
4402 ;; This function is used as the auto-fill-function of a buffer
4403 ;; when Auto-Fill mode is enabled.
4404 ;; It returns t if it really did any work.
4405 ;; (Actually some major modes use a different auto-fill function,
4406 ;; but this one is the default one.)
4407 (defun do-auto-fill ()
4408 (let (fc justify give-up
4409 (fill-prefix fill-prefix))
4410 (if (or (not (setq justify (current-justification)))
4411 (null (setq fc (current-fill-column)))
4412 (and (eq justify 'left)
4413 (<= (current-column) fc))
4414 (and auto-fill-inhibit-regexp
4415 (save-excursion (beginning-of-line)
4416 (looking-at auto-fill-inhibit-regexp))))
4417 nil ;; Auto-filling not required
4418 (if (memq justify '(full center right))
4419 (save-excursion (unjustify-current-line)))
4421 ;; Choose a fill-prefix automatically.
4422 (when (and adaptive-fill-mode
4423 (or (null fill-prefix) (string= fill-prefix "")))
4424 (let ((prefix
4425 (fill-context-prefix
4426 (save-excursion (backward-paragraph 1) (point))
4427 (save-excursion (forward-paragraph 1) (point)))))
4428 (and prefix (not (equal prefix ""))
4429 ;; Use auto-indentation rather than a guessed empty prefix.
4430 (not (and fill-indent-according-to-mode
4431 (string-match "\\`[ \t]*\\'" prefix)))
4432 (setq fill-prefix prefix))))
4434 (while (and (not give-up) (> (current-column) fc))
4435 ;; Determine where to split the line.
4436 (let* (after-prefix
4437 (fill-point
4438 (save-excursion
4439 (beginning-of-line)
4440 (setq after-prefix (point))
4441 (and fill-prefix
4442 (looking-at (regexp-quote fill-prefix))
4443 (setq after-prefix (match-end 0)))
4444 (move-to-column (1+ fc))
4445 (fill-move-to-break-point after-prefix)
4446 (point))))
4448 ;; See whether the place we found is any good.
4449 (if (save-excursion
4450 (goto-char fill-point)
4451 (or (bolp)
4452 ;; There is no use breaking at end of line.
4453 (save-excursion (skip-chars-forward " ") (eolp))
4454 ;; It is futile to split at the end of the prefix
4455 ;; since we would just insert the prefix again.
4456 (and after-prefix (<= (point) after-prefix))
4457 ;; Don't split right after a comment starter
4458 ;; since we would just make another comment starter.
4459 (and comment-start-skip
4460 (let ((limit (point)))
4461 (beginning-of-line)
4462 (and (re-search-forward comment-start-skip
4463 limit t)
4464 (eq (point) limit))))))
4465 ;; No good place to break => stop trying.
4466 (setq give-up t)
4467 ;; Ok, we have a useful place to break the line. Do it.
4468 (let ((prev-column (current-column)))
4469 ;; If point is at the fill-point, do not `save-excursion'.
4470 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
4471 ;; point will end up before it rather than after it.
4472 (if (save-excursion
4473 (skip-chars-backward " \t")
4474 (= (point) fill-point))
4475 (default-indent-new-line t)
4476 (save-excursion
4477 (goto-char fill-point)
4478 (default-indent-new-line t)))
4479 ;; Now do justification, if required
4480 (if (not (eq justify 'left))
4481 (save-excursion
4482 (end-of-line 0)
4483 (justify-current-line justify nil t)))
4484 ;; If making the new line didn't reduce the hpos of
4485 ;; the end of the line, then give up now;
4486 ;; trying again will not help.
4487 (if (>= (current-column) prev-column)
4488 (setq give-up t))))))
4489 ;; Justify last line.
4490 (justify-current-line justify t t)
4491 t)))
4493 (defvar comment-line-break-function 'comment-indent-new-line
4494 "*Mode-specific function which line breaks and continues a comment.
4495 This function is called during auto-filling when a comment syntax
4496 is defined.
4497 The function should take a single optional argument, which is a flag
4498 indicating whether it should use soft newlines.")
4500 (defun default-indent-new-line (&optional soft)
4501 "Break line at point and indent.
4502 If a comment syntax is defined, call `comment-indent-new-line'.
4504 The inserted newline is marked hard if variable `use-hard-newlines' is true,
4505 unless optional argument SOFT is non-nil."
4506 (interactive)
4507 (if comment-start
4508 (funcall comment-line-break-function soft)
4509 ;; Insert the newline before removing empty space so that markers
4510 ;; get preserved better.
4511 (if soft (insert-and-inherit ?\n) (newline 1))
4512 (save-excursion (forward-char -1) (delete-horizontal-space))
4513 (delete-horizontal-space)
4515 (if (and fill-prefix (not adaptive-fill-mode))
4516 ;; Blindly trust a non-adaptive fill-prefix.
4517 (progn
4518 (indent-to-left-margin)
4519 (insert-before-markers-and-inherit fill-prefix))
4521 (cond
4522 ;; If there's an adaptive prefix, use it unless we're inside
4523 ;; a comment and the prefix is not a comment starter.
4524 (fill-prefix
4525 (indent-to-left-margin)
4526 (insert-and-inherit fill-prefix))
4527 ;; If we're not inside a comment, just try to indent.
4528 (t (indent-according-to-mode))))))
4530 (defvar normal-auto-fill-function 'do-auto-fill
4531 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
4532 Some major modes set this.")
4534 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
4535 ;; FIXME: turn into a proper minor mode.
4536 ;; Add a global minor mode version of it.
4537 (defun auto-fill-mode (&optional arg)
4538 "Toggle Auto Fill mode.
4539 With arg, turn Auto Fill mode on if and only if arg is positive.
4540 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
4541 automatically breaks the line at a previous space.
4543 The value of `normal-auto-fill-function' specifies the function to use
4544 for `auto-fill-function' when turning Auto Fill mode on."
4545 (interactive "P")
4546 (prog1 (setq auto-fill-function
4547 (if (if (null arg)
4548 (not auto-fill-function)
4549 (> (prefix-numeric-value arg) 0))
4550 normal-auto-fill-function
4551 nil))
4552 (force-mode-line-update)))
4554 ;; This holds a document string used to document auto-fill-mode.
4555 (defun auto-fill-function ()
4556 "Automatically break line at a previous space, in insertion of text."
4557 nil)
4559 (defun turn-on-auto-fill ()
4560 "Unconditionally turn on Auto Fill mode."
4561 (auto-fill-mode 1))
4563 (defun turn-off-auto-fill ()
4564 "Unconditionally turn off Auto Fill mode."
4565 (auto-fill-mode -1))
4567 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
4569 (defun set-fill-column (arg)
4570 "Set `fill-column' to specified argument.
4571 Use \\[universal-argument] followed by a number to specify a column.
4572 Just \\[universal-argument] as argument means to use the current column."
4573 (interactive "P")
4574 (if (consp arg)
4575 (setq arg (current-column)))
4576 (if (not (integerp arg))
4577 ;; Disallow missing argument; it's probably a typo for C-x C-f.
4578 (error "set-fill-column requires an explicit argument")
4579 (message "Fill column set to %d (was %d)" arg fill-column)
4580 (setq fill-column arg)))
4582 (defun set-selective-display (arg)
4583 "Set `selective-display' to ARG; clear it if no arg.
4584 When the value of `selective-display' is a number > 0,
4585 lines whose indentation is >= that value are not displayed.
4586 The variable `selective-display' has a separate value for each buffer."
4587 (interactive "P")
4588 (if (eq selective-display t)
4589 (error "selective-display already in use for marked lines"))
4590 (let ((current-vpos
4591 (save-restriction
4592 (narrow-to-region (point-min) (point))
4593 (goto-char (window-start))
4594 (vertical-motion (window-height)))))
4595 (setq selective-display
4596 (and arg (prefix-numeric-value arg)))
4597 (recenter current-vpos))
4598 (set-window-start (selected-window) (window-start (selected-window)))
4599 (princ "selective-display set to " t)
4600 (prin1 selective-display t)
4601 (princ "." t))
4603 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
4605 (defun toggle-truncate-lines (&optional arg)
4606 "Toggle whether to fold or truncate long lines for the current buffer.
4607 With prefix argument ARG, truncate long lines if ARG is positive,
4608 otherwise don't truncate them. Note that in side-by-side
4609 windows, truncation is always enabled."
4610 (interactive "P")
4611 (setq truncate-lines
4612 (if (null arg)
4613 (not truncate-lines)
4614 (> (prefix-numeric-value arg) 0)))
4615 (force-mode-line-update)
4616 (unless truncate-lines
4617 (let ((buffer (current-buffer)))
4618 (walk-windows (lambda (window)
4619 (if (eq buffer (window-buffer window))
4620 (set-window-hscroll window 0)))
4621 nil t)))
4622 (message "Truncate long lines %s"
4623 (if truncate-lines "enabled" "disabled")))
4625 (defvar overwrite-mode-textual " Ovwrt"
4626 "The string displayed in the mode line when in overwrite mode.")
4627 (defvar overwrite-mode-binary " Bin Ovwrt"
4628 "The string displayed in the mode line when in binary overwrite mode.")
4630 (defun overwrite-mode (arg)
4631 "Toggle overwrite mode.
4632 With prefix argument ARG, turn overwrite mode on if ARG is positive,
4633 otherwise turn it off. In overwrite mode, printing characters typed
4634 in replace existing text on a one-for-one basis, rather than pushing
4635 it to the right. At the end of a line, such characters extend the line.
4636 Before a tab, such characters insert until the tab is filled in.
4637 \\[quoted-insert] still inserts characters in overwrite mode; this
4638 is supposed to make it easier to insert characters when necessary."
4639 (interactive "P")
4640 (setq overwrite-mode
4641 (if (if (null arg) (not overwrite-mode)
4642 (> (prefix-numeric-value arg) 0))
4643 'overwrite-mode-textual))
4644 (force-mode-line-update))
4646 (defun binary-overwrite-mode (arg)
4647 "Toggle binary overwrite mode.
4648 With prefix argument ARG, turn binary overwrite mode on if ARG is
4649 positive, otherwise turn it off. In binary overwrite mode, printing
4650 characters typed in replace existing text. Newlines are not treated
4651 specially, so typing at the end of a line joins the line to the next,
4652 with the typed character between them. Typing before a tab character
4653 simply replaces the tab with the character typed. \\[quoted-insert]
4654 replaces the text at the cursor, just as ordinary typing characters do.
4656 Note that binary overwrite mode is not its own minor mode; it is a
4657 specialization of overwrite mode, entered by setting the
4658 `overwrite-mode' variable to `overwrite-mode-binary'."
4659 (interactive "P")
4660 (setq overwrite-mode
4661 (if (if (null arg)
4662 (not (eq overwrite-mode 'overwrite-mode-binary))
4663 (> (prefix-numeric-value arg) 0))
4664 'overwrite-mode-binary))
4665 (force-mode-line-update))
4667 (define-minor-mode line-number-mode
4668 "Toggle Line Number mode.
4669 With arg, turn Line Number mode on if arg is positive, otherwise
4670 turn it off. When Line Number mode is enabled, the line number
4671 appears in the mode line.
4673 Line numbers do not appear for very large buffers and buffers
4674 with very long lines; see variables `line-number-display-limit'
4675 and `line-number-display-limit-width'."
4676 :init-value t :global t :group 'mode-line)
4678 (define-minor-mode column-number-mode
4679 "Toggle Column Number mode.
4680 With arg, turn Column Number mode on if arg is positive,
4681 otherwise turn it off. When Column Number mode is enabled, the
4682 column number appears in the mode line."
4683 :global t :group 'mode-line)
4685 (define-minor-mode size-indication-mode
4686 "Toggle Size Indication mode.
4687 With arg, turn Size Indication mode on if arg is positive,
4688 otherwise turn it off. When Size Indication mode is enabled, the
4689 size of the accessible part of the buffer appears in the mode line."
4690 :global t :group 'mode-line)
4692 (defgroup paren-blinking nil
4693 "Blinking matching of parens and expressions."
4694 :prefix "blink-matching-"
4695 :group 'paren-matching)
4697 (defcustom blink-matching-paren t
4698 "*Non-nil means show matching open-paren when close-paren is inserted."
4699 :type 'boolean
4700 :group 'paren-blinking)
4702 (defcustom blink-matching-paren-on-screen t
4703 "*Non-nil means show matching open-paren when it is on screen.
4704 If nil, don't show it (but the open-paren can still be shown
4705 when it is off screen).
4707 This variable has no effect if `blink-matching-paren' is nil.
4708 \(In that case, the open-paren is never shown.)
4709 It is also ignored if `show-paren-mode' is enabled."
4710 :type 'boolean
4711 :group 'paren-blinking)
4713 (defcustom blink-matching-paren-distance (* 25 1024)
4714 "*If non-nil, maximum distance to search backwards for matching open-paren.
4715 If nil, search stops at the beginning of the accessible portion of the buffer."
4716 :type '(choice (const nil) integer)
4717 :group 'paren-blinking)
4719 (defcustom blink-matching-delay 1
4720 "*Time in seconds to delay after showing a matching paren."
4721 :type 'number
4722 :group 'paren-blinking)
4724 (defcustom blink-matching-paren-dont-ignore-comments nil
4725 "*If nil, `blink-matching-paren' ignores comments.
4726 More precisely, when looking for the matching parenthesis,
4727 it skips the contents of comments that end before point."
4728 :type 'boolean
4729 :group 'paren-blinking)
4731 (defun blink-matching-open ()
4732 "Move cursor momentarily to the beginning of the sexp before point."
4733 (interactive)
4734 (when (and (> (point) (point-min))
4735 blink-matching-paren
4736 ;; Verify an even number of quoting characters precede the close.
4737 (= 1 (logand 1 (- (point)
4738 (save-excursion
4739 (forward-char -1)
4740 (skip-syntax-backward "/\\")
4741 (point))))))
4742 (let* ((oldpos (point))
4743 (message-log-max nil) ; Don't log messages about paren matching.
4744 (atdollar (eq (syntax-class (syntax-after (1- oldpos))) 8))
4745 (isdollar)
4746 (blinkpos
4747 (save-excursion
4748 (save-restriction
4749 (if blink-matching-paren-distance
4750 (narrow-to-region
4751 (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
4752 (- (point) blink-matching-paren-distance))
4753 oldpos))
4754 (let ((parse-sexp-ignore-comments
4755 (and parse-sexp-ignore-comments
4756 (not blink-matching-paren-dont-ignore-comments))))
4757 (condition-case ()
4758 (scan-sexps oldpos -1)
4759 (error nil))))))
4760 (matching-paren
4761 (and blinkpos
4762 ;; Not syntax '$'.
4763 (not (setq isdollar
4764 (eq (syntax-class (syntax-after blinkpos)) 8)))
4765 (let ((syntax (syntax-after blinkpos)))
4766 (and (consp syntax)
4767 (eq (syntax-class syntax) 4)
4768 (cdr syntax))))))
4769 (cond
4770 ;; isdollar is for:
4771 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00871.html
4772 ((not (or (and isdollar blinkpos)
4773 (and atdollar (not blinkpos)) ; see below
4774 (eq matching-paren (char-before oldpos))
4775 ;; The cdr might hold a new paren-class info rather than
4776 ;; a matching-char info, in which case the two CDRs
4777 ;; should match.
4778 (eq matching-paren (cdr (syntax-after (1- oldpos))))))
4779 (message "Mismatched parentheses"))
4780 ((not blinkpos)
4781 (or blink-matching-paren-distance
4782 ;; Don't complain when `$' with no blinkpos, because it
4783 ;; could just be the first one typed in the buffer.
4784 atdollar
4785 (message "Unmatched parenthesis")))
4786 ((pos-visible-in-window-p blinkpos)
4787 ;; Matching open within window, temporarily move to blinkpos but only
4788 ;; if `blink-matching-paren-on-screen' is non-nil.
4789 (and blink-matching-paren-on-screen
4790 (not show-paren-mode)
4791 (save-excursion
4792 (goto-char blinkpos)
4793 (sit-for blink-matching-delay))))
4795 (save-excursion
4796 (goto-char blinkpos)
4797 (let ((open-paren-line-string
4798 ;; Show what precedes the open in its line, if anything.
4799 (cond
4800 ((save-excursion (skip-chars-backward " \t") (not (bolp)))
4801 (buffer-substring (line-beginning-position)
4802 (1+ blinkpos)))
4803 ;; Show what follows the open in its line, if anything.
4804 ((save-excursion
4805 (forward-char 1)
4806 (skip-chars-forward " \t")
4807 (not (eolp)))
4808 (buffer-substring blinkpos
4809 (line-end-position)))
4810 ;; Otherwise show the previous nonblank line,
4811 ;; if there is one.
4812 ((save-excursion (skip-chars-backward "\n \t") (not (bobp)))
4813 (concat
4814 (buffer-substring (progn
4815 (skip-chars-backward "\n \t")
4816 (line-beginning-position))
4817 (progn (end-of-line)
4818 (skip-chars-backward " \t")
4819 (point)))
4820 ;; Replace the newline and other whitespace with `...'.
4821 "..."
4822 (buffer-substring blinkpos (1+ blinkpos))))
4823 ;; There is nothing to show except the char itself.
4824 (t (buffer-substring blinkpos (1+ blinkpos))))))
4825 (message "Matches %s"
4826 (substring-no-properties open-paren-line-string)))))))))
4828 ;; Turned off because it makes dbx bomb out.
4829 (setq blink-paren-function 'blink-matching-open)
4831 ;; This executes C-g typed while Emacs is waiting for a command.
4832 ;; Quitting out of a program does not go through here;
4833 ;; that happens in the QUIT macro at the C code level.
4834 (defun keyboard-quit ()
4835 "Signal a `quit' condition.
4836 During execution of Lisp code, this character causes a quit directly.
4837 At top-level, as an editor command, this simply beeps."
4838 (interactive)
4839 (deactivate-mark)
4840 (if (fboundp 'kmacro-keyboard-quit)
4841 (kmacro-keyboard-quit))
4842 (setq defining-kbd-macro nil)
4843 (signal 'quit nil))
4845 (defvar buffer-quit-function nil
4846 "Function to call to \"quit\" the current buffer, or nil if none.
4847 \\[keyboard-escape-quit] calls this function when its more local actions
4848 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4850 (defun keyboard-escape-quit ()
4851 "Exit the current \"mode\" (in a generalized sense of the word).
4852 This command can exit an interactive command such as `query-replace',
4853 can clear out a prefix argument or a region,
4854 can get out of the minibuffer or other recursive edit,
4855 cancel the use of the current buffer (for special-purpose buffers),
4856 or go back to just one window (by deleting all but the selected window)."
4857 (interactive)
4858 (cond ((eq last-command 'mode-exited) nil)
4859 ((> (minibuffer-depth) 0)
4860 (abort-recursive-edit))
4861 (current-prefix-arg
4862 nil)
4863 ((and transient-mark-mode mark-active)
4864 (deactivate-mark))
4865 ((> (recursion-depth) 0)
4866 (exit-recursive-edit))
4867 (buffer-quit-function
4868 (funcall buffer-quit-function))
4869 ((not (one-window-p t))
4870 (delete-other-windows))
4871 ((string-match "^ \\*" (buffer-name (current-buffer)))
4872 (bury-buffer))))
4874 (defun play-sound-file (file &optional volume device)
4875 "Play sound stored in FILE.
4876 VOLUME and DEVICE correspond to the keywords of the sound
4877 specification for `play-sound'."
4878 (interactive "fPlay sound file: ")
4879 (let ((sound (list :file file)))
4880 (if volume
4881 (plist-put sound :volume volume))
4882 (if device
4883 (plist-put sound :device device))
4884 (push 'sound sound)
4885 (play-sound sound)))
4888 (defcustom read-mail-command 'rmail
4889 "*Your preference for a mail reading package.
4890 This is used by some keybindings which support reading mail.
4891 See also `mail-user-agent' concerning sending mail."
4892 :type '(choice (function-item rmail)
4893 (function-item gnus)
4894 (function-item mh-rmail)
4895 (function :tag "Other"))
4896 :version "21.1"
4897 :group 'mail)
4899 (defcustom mail-user-agent 'sendmail-user-agent
4900 "*Your preference for a mail composition package.
4901 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4902 outgoing email message. This variable lets you specify which
4903 mail-sending package you prefer.
4905 Valid values include:
4907 `sendmail-user-agent' -- use the default Emacs Mail package.
4908 See Info node `(emacs)Sending Mail'.
4909 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4910 See Info node `(mh-e)'.
4911 `message-user-agent' -- use the Gnus Message package.
4912 See Info node `(message)'.
4913 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4914 paraphernalia, particularly the Gcc: header for
4915 archiving.
4917 Additional valid symbols may be available; check with the author of
4918 your package for details. The function should return non-nil if it
4919 succeeds.
4921 See also `read-mail-command' concerning reading mail."
4922 :type '(radio (function-item :tag "Default Emacs mail"
4923 :format "%t\n"
4924 sendmail-user-agent)
4925 (function-item :tag "Emacs interface to MH"
4926 :format "%t\n"
4927 mh-e-user-agent)
4928 (function-item :tag "Gnus Message package"
4929 :format "%t\n"
4930 message-user-agent)
4931 (function-item :tag "Gnus Message with full Gnus features"
4932 :format "%t\n"
4933 gnus-user-agent)
4934 (function :tag "Other"))
4935 :group 'mail)
4937 (define-mail-user-agent 'sendmail-user-agent
4938 'sendmail-user-agent-compose
4939 'mail-send-and-exit)
4941 (defun rfc822-goto-eoh ()
4942 ;; Go to header delimiter line in a mail message, following RFC822 rules
4943 (goto-char (point-min))
4944 (when (re-search-forward
4945 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4946 (goto-char (match-beginning 0))))
4948 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4949 switch-function yank-action
4950 send-actions)
4951 (if switch-function
4952 (let ((special-display-buffer-names nil)
4953 (special-display-regexps nil)
4954 (same-window-buffer-names nil)
4955 (same-window-regexps nil))
4956 (funcall switch-function "*mail*")))
4957 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4958 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4959 (body (cdr (assoc-string "body" other-headers t))))
4960 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4961 continue
4962 (error "Message aborted"))
4963 (save-excursion
4964 (rfc822-goto-eoh)
4965 (while other-headers
4966 (unless (member-ignore-case (car (car other-headers))
4967 '("in-reply-to" "cc" "body"))
4968 (insert (car (car other-headers)) ": "
4969 (cdr (car other-headers))
4970 (if use-hard-newlines hard-newline "\n")))
4971 (setq other-headers (cdr other-headers)))
4972 (when body
4973 (forward-line 1)
4974 (insert body))
4975 t)))
4977 (defun compose-mail (&optional to subject other-headers continue
4978 switch-function yank-action send-actions)
4979 "Start composing a mail message to send.
4980 This uses the user's chosen mail composition package
4981 as selected with the variable `mail-user-agent'.
4982 The optional arguments TO and SUBJECT specify recipients
4983 and the initial Subject field, respectively.
4985 OTHER-HEADERS is an alist specifying additional
4986 header fields. Elements look like (HEADER . VALUE) where both
4987 HEADER and VALUE are strings.
4989 CONTINUE, if non-nil, says to continue editing a message already
4990 being composed.
4992 SWITCH-FUNCTION, if non-nil, is a function to use to
4993 switch to and display the buffer used for mail composition.
4995 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4996 to insert the raw text of the message being replied to.
4997 It has the form (FUNCTION . ARGS). The user agent will apply
4998 FUNCTION to ARGS, to insert the raw text of the original message.
4999 \(The user agent will also run `mail-citation-hook', *after* the
5000 original text has been inserted in this way.)
5002 SEND-ACTIONS is a list of actions to call when the message is sent.
5003 Each action has the form (FUNCTION . ARGS)."
5004 (interactive
5005 (list nil nil nil current-prefix-arg))
5006 (let ((function (get mail-user-agent 'composefunc)))
5007 (funcall function to subject other-headers continue
5008 switch-function yank-action send-actions)))
5010 (defun compose-mail-other-window (&optional to subject other-headers continue
5011 yank-action send-actions)
5012 "Like \\[compose-mail], but edit the outgoing message in another window."
5013 (interactive
5014 (list nil nil nil current-prefix-arg))
5015 (compose-mail to subject other-headers continue
5016 'switch-to-buffer-other-window yank-action send-actions))
5019 (defun compose-mail-other-frame (&optional to subject other-headers continue
5020 yank-action send-actions)
5021 "Like \\[compose-mail], but edit the outgoing message in another frame."
5022 (interactive
5023 (list nil nil nil current-prefix-arg))
5024 (compose-mail to subject other-headers continue
5025 'switch-to-buffer-other-frame yank-action send-actions))
5027 (defvar set-variable-value-history nil
5028 "History of values entered with `set-variable'.
5030 Maximum length of the history list is determined by the value
5031 of `history-length', which see.")
5033 (defun set-variable (variable value &optional make-local)
5034 "Set VARIABLE to VALUE. VALUE is a Lisp object.
5035 VARIABLE should be a user option variable name, a Lisp variable
5036 meant to be customized by users. You should enter VALUE in Lisp syntax,
5037 so if you want VALUE to be a string, you must surround it with doublequotes.
5038 VALUE is used literally, not evaluated.
5040 If VARIABLE has a `variable-interactive' property, that is used as if
5041 it were the arg to `interactive' (which see) to interactively read VALUE.
5043 If VARIABLE has been defined with `defcustom', then the type information
5044 in the definition is used to check that VALUE is valid.
5046 With a prefix argument, set VARIABLE to VALUE buffer-locally."
5047 (interactive
5048 (let* ((default-var (variable-at-point))
5049 (var (if (user-variable-p default-var)
5050 (read-variable (format "Set variable (default %s): " default-var)
5051 default-var)
5052 (read-variable "Set variable: ")))
5053 (minibuffer-help-form '(describe-variable var))
5054 (prop (get var 'variable-interactive))
5055 (obsolete (car (get var 'byte-obsolete-variable)))
5056 (prompt (format "Set %s %s to value: " var
5057 (cond ((local-variable-p var)
5058 "(buffer-local)")
5059 ((or current-prefix-arg
5060 (local-variable-if-set-p var))
5061 "buffer-locally")
5062 (t "globally"))))
5063 (val (progn
5064 (when obsolete
5065 (message (concat "`%S' is obsolete; "
5066 (if (symbolp obsolete) "use `%S' instead" "%s"))
5067 var obsolete)
5068 (sit-for 3))
5069 (if prop
5070 ;; Use VAR's `variable-interactive' property
5071 ;; as an interactive spec for prompting.
5072 (call-interactively `(lambda (arg)
5073 (interactive ,prop)
5074 arg))
5075 (read
5076 (read-string prompt nil
5077 'set-variable-value-history
5078 (format "%S" (symbol-value var))))))))
5079 (list var val current-prefix-arg)))
5081 (and (custom-variable-p variable)
5082 (not (get variable 'custom-type))
5083 (custom-load-symbol variable))
5084 (let ((type (get variable 'custom-type)))
5085 (when type
5086 ;; Match with custom type.
5087 (require 'cus-edit)
5088 (setq type (widget-convert type))
5089 (unless (widget-apply type :match value)
5090 (error "Value `%S' does not match type %S of %S"
5091 value (car type) variable))))
5093 (if make-local
5094 (make-local-variable variable))
5096 (set variable value)
5098 ;; Force a thorough redisplay for the case that the variable
5099 ;; has an effect on the display, like `tab-width' has.
5100 (force-mode-line-update))
5102 ;; Define the major mode for lists of completions.
5104 (defvar completion-list-mode-map nil
5105 "Local map for completion list buffers.")
5106 (or completion-list-mode-map
5107 (let ((map (make-sparse-keymap)))
5108 (define-key map [mouse-2] 'mouse-choose-completion)
5109 (define-key map [follow-link] 'mouse-face)
5110 (define-key map [down-mouse-2] nil)
5111 (define-key map "\C-m" 'choose-completion)
5112 (define-key map "\e\e\e" 'delete-completion-window)
5113 (define-key map [left] 'previous-completion)
5114 (define-key map [right] 'next-completion)
5115 (setq completion-list-mode-map map)))
5117 ;; Completion mode is suitable only for specially formatted data.
5118 (put 'completion-list-mode 'mode-class 'special)
5120 (defvar completion-reference-buffer nil
5121 "Record the buffer that was current when the completion list was requested.
5122 This is a local variable in the completion list buffer.
5123 Initial value is nil to avoid some compiler warnings.")
5125 (defvar completion-no-auto-exit nil
5126 "Non-nil means `choose-completion-string' should never exit the minibuffer.
5127 This also applies to other functions such as `choose-completion'
5128 and `mouse-choose-completion'.")
5130 (defvar completion-base-size nil
5131 "Number of chars at beginning of minibuffer not involved in completion.
5132 This is a local variable in the completion list buffer
5133 but it talks about the buffer in `completion-reference-buffer'.
5134 If this is nil, it means to compare text to determine which part
5135 of the tail end of the buffer's text is involved in completion.")
5137 (defun delete-completion-window ()
5138 "Delete the completion list window.
5139 Go to the window from which completion was requested."
5140 (interactive)
5141 (let ((buf completion-reference-buffer))
5142 (if (one-window-p t)
5143 (if (window-dedicated-p (selected-window))
5144 (delete-frame (selected-frame)))
5145 (delete-window (selected-window))
5146 (if (get-buffer-window buf)
5147 (select-window (get-buffer-window buf))))))
5149 (defun previous-completion (n)
5150 "Move to the previous item in the completion list."
5151 (interactive "p")
5152 (next-completion (- n)))
5154 (defun next-completion (n)
5155 "Move to the next item in the completion list.
5156 With prefix argument N, move N items (negative N means move backward)."
5157 (interactive "p")
5158 (let ((beg (point-min)) (end (point-max)))
5159 (while (and (> n 0) (not (eobp)))
5160 ;; If in a completion, move to the end of it.
5161 (when (get-text-property (point) 'mouse-face)
5162 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
5163 ;; Move to start of next one.
5164 (unless (get-text-property (point) 'mouse-face)
5165 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
5166 (setq n (1- n)))
5167 (while (and (< n 0) (not (bobp)))
5168 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
5169 ;; If in a completion, move to the start of it.
5170 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
5171 (goto-char (previous-single-property-change
5172 (point) 'mouse-face nil beg)))
5173 ;; Move to end of the previous completion.
5174 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
5175 (goto-char (previous-single-property-change
5176 (point) 'mouse-face nil beg)))
5177 ;; Move to the start of that one.
5178 (goto-char (previous-single-property-change
5179 (point) 'mouse-face nil beg))
5180 (setq n (1+ n))))))
5182 (defun choose-completion ()
5183 "Choose the completion that point is in or next to."
5184 (interactive)
5185 (let (beg end completion (buffer completion-reference-buffer)
5186 (base-size completion-base-size))
5187 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
5188 (setq end (point) beg (1+ (point))))
5189 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
5190 (setq end (1- (point)) beg (point)))
5191 (if (null beg)
5192 (error "No completion here"))
5193 (setq beg (previous-single-property-change beg 'mouse-face))
5194 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
5195 (setq completion (buffer-substring-no-properties beg end))
5196 (let ((owindow (selected-window)))
5197 (if (and (one-window-p t 'selected-frame)
5198 (window-dedicated-p (selected-window)))
5199 ;; This is a special buffer's frame
5200 (iconify-frame (selected-frame))
5201 (or (window-dedicated-p (selected-window))
5202 (bury-buffer)))
5203 (select-window owindow))
5204 (choose-completion-string completion buffer base-size)))
5206 ;; Delete the longest partial match for STRING
5207 ;; that can be found before POINT.
5208 (defun choose-completion-delete-max-match (string)
5209 (let ((opoint (point))
5210 len)
5211 ;; Try moving back by the length of the string.
5212 (goto-char (max (- (point) (length string))
5213 (minibuffer-prompt-end)))
5214 ;; See how far back we were actually able to move. That is the
5215 ;; upper bound on how much we can match and delete.
5216 (setq len (- opoint (point)))
5217 (if completion-ignore-case
5218 (setq string (downcase string)))
5219 (while (and (> len 0)
5220 (let ((tail (buffer-substring (point) opoint)))
5221 (if completion-ignore-case
5222 (setq tail (downcase tail)))
5223 (not (string= tail (substring string 0 len)))))
5224 (setq len (1- len))
5225 (forward-char 1))
5226 (delete-char len)))
5228 (defvar choose-completion-string-functions nil
5229 "Functions that may override the normal insertion of a completion choice.
5230 These functions are called in order with four arguments:
5231 CHOICE - the string to insert in the buffer,
5232 BUFFER - the buffer in which the choice should be inserted,
5233 MINI-P - non-nil if BUFFER is a minibuffer, and
5234 BASE-SIZE - the number of characters in BUFFER before
5235 the string being completed.
5237 If a function in the list returns non-nil, that function is supposed
5238 to have inserted the CHOICE in the BUFFER, and possibly exited
5239 the minibuffer; no further functions will be called.
5241 If all functions in the list return nil, that means to use
5242 the default method of inserting the completion in BUFFER.")
5244 (defun choose-completion-string (choice &optional buffer base-size)
5245 "Switch to BUFFER and insert the completion choice CHOICE.
5246 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
5247 to keep. If it is nil, we call `choose-completion-delete-max-match'
5248 to decide what to delete."
5250 ;; If BUFFER is the minibuffer, exit the minibuffer
5251 ;; unless it is reading a file name and CHOICE is a directory,
5252 ;; or completion-no-auto-exit is non-nil.
5254 (let* ((buffer (or buffer completion-reference-buffer))
5255 (mini-p (minibufferp buffer)))
5256 ;; If BUFFER is a minibuffer, barf unless it's the currently
5257 ;; active minibuffer.
5258 (if (and mini-p
5259 (or (not (active-minibuffer-window))
5260 (not (equal buffer
5261 (window-buffer (active-minibuffer-window))))))
5262 (error "Minibuffer is not active for completion")
5263 ;; Set buffer so buffer-local choose-completion-string-functions works.
5264 (set-buffer buffer)
5265 (unless (run-hook-with-args-until-success
5266 'choose-completion-string-functions
5267 choice buffer mini-p base-size)
5268 ;; Insert the completion into the buffer where it was requested.
5269 (if base-size
5270 (delete-region (+ base-size (if mini-p
5271 (minibuffer-prompt-end)
5272 (point-min)))
5273 (point))
5274 (choose-completion-delete-max-match choice))
5275 (insert choice)
5276 (remove-text-properties (- (point) (length choice)) (point)
5277 '(mouse-face nil))
5278 ;; Update point in the window that BUFFER is showing in.
5279 (let ((window (get-buffer-window buffer t)))
5280 (set-window-point window (point)))
5281 ;; If completing for the minibuffer, exit it with this choice.
5282 (and (not completion-no-auto-exit)
5283 (equal buffer (window-buffer (minibuffer-window)))
5284 minibuffer-completion-table
5285 ;; If this is reading a file name, and the file name chosen
5286 ;; is a directory, don't exit the minibuffer.
5287 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
5288 (file-directory-p (field-string (point-max))))
5289 (let ((mini (active-minibuffer-window)))
5290 (select-window mini)
5291 (when minibuffer-auto-raise
5292 (raise-frame (window-frame mini))))
5293 (exit-minibuffer)))))))
5295 (defun completion-list-mode ()
5296 "Major mode for buffers showing lists of possible completions.
5297 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
5298 to select the completion near point.
5299 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
5300 with the mouse.
5302 \\{completion-list-mode-map}"
5304 (interactive)
5305 (kill-all-local-variables)
5306 (use-local-map completion-list-mode-map)
5307 (setq mode-name "Completion List")
5308 (setq major-mode 'completion-list-mode)
5309 (make-local-variable 'completion-base-size)
5310 (setq completion-base-size nil)
5311 (run-mode-hooks 'completion-list-mode-hook))
5313 (defun completion-list-mode-finish ()
5314 "Finish setup of the completions buffer.
5315 Called from `temp-buffer-show-hook'."
5316 (when (eq major-mode 'completion-list-mode)
5317 (toggle-read-only 1)))
5319 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
5321 (defvar completion-setup-hook nil
5322 "Normal hook run at the end of setting up a completion list buffer.
5323 When this hook is run, the current buffer is the one in which the
5324 command to display the completion list buffer was run.
5325 The completion list buffer is available as the value of `standard-output'.
5326 The common prefix substring for completion may be available as the
5327 value of `completion-common-substring'. See also `display-completion-list'.")
5330 ;; Variables and faces used in `completion-setup-function'.
5332 (defcustom completion-show-help t
5333 "Non-nil means show help message in *Completions* buffer."
5334 :type 'boolean
5335 :version "22.1"
5336 :group 'completion)
5338 (defface completions-first-difference
5339 '((t (:inherit bold)))
5340 "Face put on the first uncommon character in completions in *Completions* buffer."
5341 :group 'completion)
5343 (defface completions-common-part
5344 '((t (:inherit default)))
5345 "Face put on the common prefix substring in completions in *Completions* buffer.
5346 The idea of `completions-common-part' is that you can use it to
5347 make the common parts less visible than normal, so that the rest
5348 of the differing parts is, by contrast, slightly highlighted."
5349 :group 'completion)
5351 ;; This is for packages that need to bind it to a non-default regexp
5352 ;; in order to make the first-differing character highlight work
5353 ;; to their liking
5354 (defvar completion-root-regexp "^/"
5355 "Regexp to use in `completion-setup-function' to find the root directory.")
5357 (defvar completion-common-substring nil
5358 "Common prefix substring to use in `completion-setup-function' to put faces.
5359 The value is set by `display-completion-list' during running `completion-setup-hook'.
5361 To put faces `completions-first-difference' and `completions-common-part'
5362 in the `*Completions*' buffer, the common prefix substring in completions
5363 is needed as a hint. (The minibuffer is a special case. The content
5364 of the minibuffer before point is always the common substring.)")
5366 ;; This function goes in completion-setup-hook, so that it is called
5367 ;; after the text of the completion list buffer is written.
5368 (defun completion-setup-function ()
5369 (let* ((mainbuf (current-buffer))
5370 (mbuf-contents (minibuffer-completion-contents))
5371 common-string-length)
5372 ;; When reading a file name in the minibuffer,
5373 ;; set default-directory in the minibuffer
5374 ;; so it will get copied into the completion list buffer.
5375 (if minibuffer-completing-file-name
5376 (with-current-buffer mainbuf
5377 (setq default-directory
5378 (file-name-directory (expand-file-name mbuf-contents)))))
5379 (with-current-buffer standard-output
5380 (completion-list-mode)
5381 (set (make-local-variable 'completion-reference-buffer) mainbuf)
5382 (setq completion-base-size
5383 (cond
5384 ((and (symbolp minibuffer-completion-table)
5385 (get minibuffer-completion-table 'completion-base-size-function))
5386 ;; To compute base size, a function can use the global value of
5387 ;; completion-common-substring or minibuffer-completion-contents.
5388 (with-current-buffer mainbuf
5389 (funcall (get minibuffer-completion-table
5390 'completion-base-size-function))))
5391 (minibuffer-completing-file-name
5392 ;; For file name completion, use the number of chars before
5393 ;; the start of the file name component at point.
5394 (with-current-buffer mainbuf
5395 (save-excursion
5396 (skip-chars-backward completion-root-regexp)
5397 (- (point) (minibuffer-prompt-end)))))
5398 (minibuffer-completing-symbol nil)
5399 ;; Otherwise, in minibuffer, the base size is 0.
5400 ((minibufferp mainbuf) 0)))
5401 (setq common-string-length
5402 (cond
5403 (completion-common-substring
5404 (length completion-common-substring))
5405 (completion-base-size
5406 (- (length mbuf-contents) completion-base-size))))
5407 ;; Put faces on first uncommon characters and common parts.
5408 (when (and (integerp common-string-length) (>= common-string-length 0))
5409 (let ((element-start (point-min))
5410 (maxp (point-max))
5411 element-common-end)
5412 (while (and (setq element-start
5413 (next-single-property-change
5414 element-start 'mouse-face))
5415 (< (setq element-common-end
5416 (+ element-start common-string-length))
5417 maxp))
5418 (when (get-char-property element-start 'mouse-face)
5419 (if (and (> common-string-length 0)
5420 (get-char-property (1- element-common-end) 'mouse-face))
5421 (put-text-property element-start element-common-end
5422 'font-lock-face 'completions-common-part))
5423 (if (get-char-property element-common-end 'mouse-face)
5424 (put-text-property element-common-end (1+ element-common-end)
5425 'font-lock-face 'completions-first-difference))))))
5426 ;; Maybe insert help string.
5427 (when completion-show-help
5428 (goto-char (point-min))
5429 (if (display-mouse-p)
5430 (insert (substitute-command-keys
5431 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
5432 (insert (substitute-command-keys
5433 "In this buffer, type \\[choose-completion] to \
5434 select the completion near point.\n\n"))))))
5436 (add-hook 'completion-setup-hook 'completion-setup-function)
5438 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
5439 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
5441 (defun switch-to-completions ()
5442 "Select the completion list window."
5443 (interactive)
5444 ;; Make sure we have a completions window.
5445 (or (get-buffer-window "*Completions*")
5446 (minibuffer-completion-help))
5447 (let ((window (get-buffer-window "*Completions*")))
5448 (when window
5449 (select-window window)
5450 (goto-char (point-min))
5451 (search-forward "\n\n" nil t)
5452 (forward-line 1))))
5454 ;;; Support keyboard commands to turn on various modifiers.
5456 ;; These functions -- which are not commands -- each add one modifier
5457 ;; to the following event.
5459 (defun event-apply-alt-modifier (ignore-prompt)
5460 "\\<function-key-map>Add the Alt modifier to the following event.
5461 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
5462 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
5463 (defun event-apply-super-modifier (ignore-prompt)
5464 "\\<function-key-map>Add the Super modifier to the following event.
5465 For example, type \\[event-apply-super-modifier] & to enter Super-&."
5466 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
5467 (defun event-apply-hyper-modifier (ignore-prompt)
5468 "\\<function-key-map>Add the Hyper modifier to the following event.
5469 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
5470 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
5471 (defun event-apply-shift-modifier (ignore-prompt)
5472 "\\<function-key-map>Add the Shift modifier to the following event.
5473 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
5474 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
5475 (defun event-apply-control-modifier (ignore-prompt)
5476 "\\<function-key-map>Add the Ctrl modifier to the following event.
5477 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
5478 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
5479 (defun event-apply-meta-modifier (ignore-prompt)
5480 "\\<function-key-map>Add the Meta modifier to the following event.
5481 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
5482 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
5484 (defun event-apply-modifier (event symbol lshiftby prefix)
5485 "Apply a modifier flag to event EVENT.
5486 SYMBOL is the name of this modifier, as a symbol.
5487 LSHIFTBY is the numeric value of this modifier, in keyboard events.
5488 PREFIX is the string that represents this modifier in an event type symbol."
5489 (if (numberp event)
5490 (cond ((eq symbol 'control)
5491 (if (and (<= (downcase event) ?z)
5492 (>= (downcase event) ?a))
5493 (- (downcase event) ?a -1)
5494 (if (and (<= (downcase event) ?Z)
5495 (>= (downcase event) ?A))
5496 (- (downcase event) ?A -1)
5497 (logior (lsh 1 lshiftby) event))))
5498 ((eq symbol 'shift)
5499 (if (and (<= (downcase event) ?z)
5500 (>= (downcase event) ?a))
5501 (upcase event)
5502 (logior (lsh 1 lshiftby) event)))
5504 (logior (lsh 1 lshiftby) event)))
5505 (if (memq symbol (event-modifiers event))
5506 event
5507 (let ((event-type (if (symbolp event) event (car event))))
5508 (setq event-type (intern (concat prefix (symbol-name event-type))))
5509 (if (symbolp event)
5510 event-type
5511 (cons event-type (cdr event)))))))
5513 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
5514 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
5515 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
5516 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
5517 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
5518 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
5520 ;;;; Keypad support.
5522 ;; Make the keypad keys act like ordinary typing keys. If people add
5523 ;; bindings for the function key symbols, then those bindings will
5524 ;; override these, so this shouldn't interfere with any existing
5525 ;; bindings.
5527 ;; Also tell read-char how to handle these keys.
5528 (mapc
5529 (lambda (keypad-normal)
5530 (let ((keypad (nth 0 keypad-normal))
5531 (normal (nth 1 keypad-normal)))
5532 (put keypad 'ascii-character normal)
5533 (define-key function-key-map (vector keypad) (vector normal))))
5534 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
5535 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
5536 (kp-space ?\s)
5537 (kp-tab ?\t)
5538 (kp-enter ?\r)
5539 (kp-multiply ?*)
5540 (kp-add ?+)
5541 (kp-separator ?,)
5542 (kp-subtract ?-)
5543 (kp-decimal ?.)
5544 (kp-divide ?/)
5545 (kp-equal ?=)))
5547 ;;;;
5548 ;;;; forking a twin copy of a buffer.
5549 ;;;;
5551 (defvar clone-buffer-hook nil
5552 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
5554 (defvar clone-indirect-buffer-hook nil
5555 "Normal hook to run in the new buffer at the end of `clone-indirect-buffer'.")
5557 (defun clone-process (process &optional newname)
5558 "Create a twin copy of PROCESS.
5559 If NEWNAME is nil, it defaults to PROCESS' name;
5560 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
5561 If PROCESS is associated with a buffer, the new process will be associated
5562 with the current buffer instead.
5563 Returns nil if PROCESS has already terminated."
5564 (setq newname (or newname (process-name process)))
5565 (if (string-match "<[0-9]+>\\'" newname)
5566 (setq newname (substring newname 0 (match-beginning 0))))
5567 (when (memq (process-status process) '(run stop open))
5568 (let* ((process-connection-type (process-tty-name process))
5569 (new-process
5570 (if (memq (process-status process) '(open))
5571 (let ((args (process-contact process t)))
5572 (setq args (plist-put args :name newname))
5573 (setq args (plist-put args :buffer
5574 (if (process-buffer process)
5575 (current-buffer))))
5576 (apply 'make-network-process args))
5577 (apply 'start-process newname
5578 (if (process-buffer process) (current-buffer))
5579 (process-command process)))))
5580 (set-process-query-on-exit-flag
5581 new-process (process-query-on-exit-flag process))
5582 (set-process-inherit-coding-system-flag
5583 new-process (process-inherit-coding-system-flag process))
5584 (set-process-filter new-process (process-filter process))
5585 (set-process-sentinel new-process (process-sentinel process))
5586 (set-process-plist new-process (copy-sequence (process-plist process)))
5587 new-process)))
5589 ;; things to maybe add (currently partly covered by `funcall mode'):
5590 ;; - syntax-table
5591 ;; - overlays
5592 (defun clone-buffer (&optional newname display-flag)
5593 "Create and return a twin copy of the current buffer.
5594 Unlike an indirect buffer, the new buffer can be edited
5595 independently of the old one (if it is not read-only).
5596 NEWNAME is the name of the new buffer. It may be modified by
5597 adding or incrementing <N> at the end as necessary to create a
5598 unique buffer name. If nil, it defaults to the name of the
5599 current buffer, with the proper suffix. If DISPLAY-FLAG is
5600 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
5601 clone a file-visiting buffer, or a buffer whose major mode symbol
5602 has a non-nil `no-clone' property, results in an error.
5604 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
5605 current buffer with appropriate suffix. However, if a prefix
5606 argument is given, then the command prompts for NEWNAME in the
5607 minibuffer.
5609 This runs the normal hook `clone-buffer-hook' in the new buffer
5610 after it has been set up properly in other respects."
5611 (interactive
5612 (progn
5613 (if buffer-file-name
5614 (error "Cannot clone a file-visiting buffer"))
5615 (if (get major-mode 'no-clone)
5616 (error "Cannot clone a buffer in %s mode" mode-name))
5617 (list (if current-prefix-arg
5618 (read-buffer "Name of new cloned buffer: " (current-buffer)))
5619 t)))
5620 (if buffer-file-name
5621 (error "Cannot clone a file-visiting buffer"))
5622 (if (get major-mode 'no-clone)
5623 (error "Cannot clone a buffer in %s mode" mode-name))
5624 (setq newname (or newname (buffer-name)))
5625 (if (string-match "<[0-9]+>\\'" newname)
5626 (setq newname (substring newname 0 (match-beginning 0))))
5627 (let ((buf (current-buffer))
5628 (ptmin (point-min))
5629 (ptmax (point-max))
5630 (pt (point))
5631 (mk (if mark-active (mark t)))
5632 (modified (buffer-modified-p))
5633 (mode major-mode)
5634 (lvars (buffer-local-variables))
5635 (process (get-buffer-process (current-buffer)))
5636 (new (generate-new-buffer (or newname (buffer-name)))))
5637 (save-restriction
5638 (widen)
5639 (with-current-buffer new
5640 (insert-buffer-substring buf)))
5641 (with-current-buffer new
5642 (narrow-to-region ptmin ptmax)
5643 (goto-char pt)
5644 (if mk (set-mark mk))
5645 (set-buffer-modified-p modified)
5647 ;; Clone the old buffer's process, if any.
5648 (when process (clone-process process))
5650 ;; Now set up the major mode.
5651 (funcall mode)
5653 ;; Set up other local variables.
5654 (mapc (lambda (v)
5655 (condition-case () ;in case var is read-only
5656 (if (symbolp v)
5657 (makunbound v)
5658 (set (make-local-variable (car v)) (cdr v)))
5659 (error nil)))
5660 lvars)
5662 ;; Run any hooks (typically set up by the major mode
5663 ;; for cloning to work properly).
5664 (run-hooks 'clone-buffer-hook))
5665 (if display-flag
5666 ;; Presumably the current buffer is shown in the selected frame, so
5667 ;; we want to display the clone elsewhere.
5668 (let ((same-window-regexps nil)
5669 (same-window-buffer-names))
5670 (pop-to-buffer new)))
5671 new))
5674 (defun clone-indirect-buffer (newname display-flag &optional norecord)
5675 "Create an indirect buffer that is a twin copy of the current buffer.
5677 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
5678 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
5679 or if not called with a prefix arg, NEWNAME defaults to the current
5680 buffer's name. The name is modified by adding a `<N>' suffix to it
5681 or by incrementing the N in an existing suffix.
5683 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
5684 This is always done when called interactively.
5686 Optional third arg NORECORD non-nil means do not put this buffer at the
5687 front of the list of recently selected ones."
5688 (interactive
5689 (progn
5690 (if (get major-mode 'no-clone-indirect)
5691 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5692 (list (if current-prefix-arg
5693 (read-buffer "Name of indirect buffer: " (current-buffer)))
5694 t)))
5695 (if (get major-mode 'no-clone-indirect)
5696 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5697 (setq newname (or newname (buffer-name)))
5698 (if (string-match "<[0-9]+>\\'" newname)
5699 (setq newname (substring newname 0 (match-beginning 0))))
5700 (let* ((name (generate-new-buffer-name newname))
5701 (buffer (make-indirect-buffer (current-buffer) name t)))
5702 (with-current-buffer buffer
5703 (run-hooks 'clone-indirect-buffer-hook))
5704 (when display-flag
5705 (pop-to-buffer buffer norecord))
5706 buffer))
5709 (defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
5710 "Like `clone-indirect-buffer' but display in another window."
5711 (interactive
5712 (progn
5713 (if (get major-mode 'no-clone-indirect)
5714 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5715 (list (if current-prefix-arg
5716 (read-buffer "Name of indirect buffer: " (current-buffer)))
5717 t)))
5718 (let ((pop-up-windows t))
5719 (clone-indirect-buffer newname display-flag norecord)))
5722 ;;; Handling of Backspace and Delete keys.
5724 (defcustom normal-erase-is-backspace 'maybe
5725 "Set the default behavior of the Delete and Backspace keys.
5727 If set to t, Delete key deletes forward and Backspace key deletes
5728 backward.
5730 If set to nil, both Delete and Backspace keys delete backward.
5732 If set to 'maybe (which is the default), Emacs automatically
5733 selects a behavior. On window systems, the behavior depends on
5734 the keyboard used. If the keyboard has both a Backspace key and
5735 a Delete key, and both are mapped to their usual meanings, the
5736 option's default value is set to t, so that Backspace can be used
5737 to delete backward, and Delete can be used to delete forward.
5739 If not running under a window system, customizing this option
5740 accomplishes a similar effect by mapping C-h, which is usually
5741 generated by the Backspace key, to DEL, and by mapping DEL to C-d
5742 via `keyboard-translate'. The former functionality of C-h is
5743 available on the F1 key. You should probably not use this
5744 setting if you don't have both Backspace, Delete and F1 keys.
5746 Setting this variable with setq doesn't take effect. Programmatically,
5747 call `normal-erase-is-backspace-mode' (which see) instead."
5748 :type '(choice (const :tag "Off" nil)
5749 (const :tag "Maybe" maybe)
5750 (other :tag "On" t))
5751 :group 'editing-basics
5752 :version "21.1"
5753 :set (lambda (symbol value)
5754 ;; The fboundp is because of a problem with :set when
5755 ;; dumping Emacs. It doesn't really matter.
5756 (if (fboundp 'normal-erase-is-backspace-mode)
5757 (normal-erase-is-backspace-mode (or value 0))
5758 (set-default symbol value))))
5760 (defun normal-erase-is-backspace-setup-frame (&optional frame)
5761 "Set up `normal-erase-is-backspace-mode' on FRAME, if necessary."
5762 (unless frame (setq frame (selected-frame)))
5763 (with-selected-frame frame
5764 (unless (terminal-parameter nil 'normal-erase-is-backspace)
5765 (normal-erase-is-backspace-mode
5766 (if (if (eq normal-erase-is-backspace 'maybe)
5767 (and (not noninteractive)
5768 (or (memq system-type '(ms-dos windows-nt))
5769 (eq window-system 'mac)
5770 (and (memq window-system '(x))
5771 (fboundp 'x-backspace-delete-keys-p)
5772 (x-backspace-delete-keys-p))
5773 ;; If the terminal Emacs is running on has erase char
5774 ;; set to ^H, use the Backspace key for deleting
5775 ;; backward, and the Delete key for deleting forward.
5776 (and (null window-system)
5777 (eq tty-erase-char ?\^H))))
5778 normal-erase-is-backspace)
5779 1 0)))))
5781 (defun normal-erase-is-backspace-mode (&optional arg)
5782 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
5784 With numeric arg, turn the mode on if and only if ARG is positive.
5786 On window systems, when this mode is on, Delete is mapped to C-d
5787 and Backspace is mapped to DEL; when this mode is off, both
5788 Delete and Backspace are mapped to DEL. (The remapping goes via
5789 `local-function-key-map', so binding Delete or Backspace in the
5790 global or local keymap will override that.)
5792 In addition, on window systems, the bindings of C-Delete, M-Delete,
5793 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
5794 the global keymap in accordance with the functionality of Delete and
5795 Backspace. For example, if Delete is remapped to C-d, which deletes
5796 forward, C-Delete is bound to `kill-word', but if Delete is remapped
5797 to DEL, which deletes backward, C-Delete is bound to
5798 `backward-kill-word'.
5800 If not running on a window system, a similar effect is accomplished by
5801 remapping C-h (normally produced by the Backspace key) and DEL via
5802 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
5803 to C-d; if it's off, the keys are not remapped.
5805 When not running on a window system, and this mode is turned on, the
5806 former functionality of C-h is available on the F1 key. You should
5807 probably not turn on this mode on a text-only terminal if you don't
5808 have both Backspace, Delete and F1 keys.
5810 See also `normal-erase-is-backspace'."
5811 (interactive "P")
5812 (let ((enabled (or (and arg (> (prefix-numeric-value arg) 0))
5813 (and (not arg)
5814 (not (eq 1 (terminal-parameter
5815 nil 'normal-erase-is-backspace)))))))
5816 (set-terminal-parameter nil 'normal-erase-is-backspace
5817 (if enabled 1 0))
5819 (cond ((or (memq window-system '(x w32 mac pc))
5820 (memq system-type '(ms-dos windows-nt)))
5821 (let* ((bindings
5822 `(([C-delete] [C-backspace])
5823 ([M-delete] [M-backspace])
5824 ([C-M-delete] [C-M-backspace])
5825 (,esc-map
5826 [C-delete] [C-backspace])))
5827 (old-state (lookup-key local-function-key-map [delete])))
5829 (if enabled
5830 (progn
5831 (define-key local-function-key-map [delete] [?\C-d])
5832 (define-key local-function-key-map [kp-delete] [?\C-d])
5833 (define-key local-function-key-map [backspace] [?\C-?]))
5834 (define-key local-function-key-map [delete] [?\C-?])
5835 (define-key local-function-key-map [kp-delete] [?\C-?])
5836 (define-key local-function-key-map [backspace] [?\C-?]))
5838 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5839 (unless (equal old-state (lookup-key local-function-key-map [delete]))
5840 (dolist (binding bindings)
5841 (let ((map global-map))
5842 (when (keymapp (car binding))
5843 (setq map (car binding) binding (cdr binding)))
5844 (let* ((key1 (nth 0 binding))
5845 (key2 (nth 1 binding))
5846 (binding1 (lookup-key map key1))
5847 (binding2 (lookup-key map key2)))
5848 (define-key map key1 binding2)
5849 (define-key map key2 binding1)))))))
5851 (if enabled
5852 (progn
5853 (keyboard-translate ?\C-h ?\C-?)
5854 (keyboard-translate ?\C-? ?\C-d))
5855 (keyboard-translate ?\C-h ?\C-h)
5856 (keyboard-translate ?\C-? ?\C-?))))
5858 (run-hooks 'normal-erase-is-backspace-hook)
5859 (if (interactive-p)
5860 (message "Delete key deletes %s"
5861 (if (terminal-parameter nil 'normal-erase-is-backspace)
5862 "forward" "backward")))))
5864 (defvar vis-mode-saved-buffer-invisibility-spec nil
5865 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5867 (define-minor-mode visible-mode
5868 "Toggle Visible mode.
5869 With argument ARG turn Visible mode on if ARG is positive, otherwise
5870 turn it off.
5872 Enabling Visible mode makes all invisible text temporarily visible.
5873 Disabling Visible mode turns off that effect. Visible mode
5874 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5875 :lighter " Vis"
5876 :group 'editing-basics
5877 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5878 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5879 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5880 (when visible-mode
5881 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5882 buffer-invisibility-spec)
5883 (setq buffer-invisibility-spec nil)))
5885 ;; Minibuffer prompt stuff.
5887 ;(defun minibuffer-prompt-modification (start end)
5888 ; (error "You cannot modify the prompt"))
5891 ;(defun minibuffer-prompt-insertion (start end)
5892 ; (let ((inhibit-modification-hooks t))
5893 ; (delete-region start end)
5894 ; ;; Discard undo information for the text insertion itself
5895 ; ;; and for the text deletion.above.
5896 ; (when (consp buffer-undo-list)
5897 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5898 ; (message "You cannot modify the prompt")))
5901 ;(setq minibuffer-prompt-properties
5902 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5903 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5907 ;;;; Problematic external packages.
5909 ;; rms says this should be done by specifying symbols that define
5910 ;; versions together with bad values. This is therefore not as
5911 ;; flexible as it could be. See the thread:
5912 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00300.html
5913 (defconst bad-packages-alist
5914 ;; Not sure exactly which semantic versions have problems.
5915 ;; Definitely 2.0pre3, probably all 2.0pre's before this.
5916 '((semantic semantic-version "\\`2\\.0pre[1-3]\\'"
5917 "The version of `semantic' loaded does not work in Emacs 22.
5918 It can cause constant high CPU load.
5919 Upgrade to at least Semantic 2.0pre4 (distributed with CEDET 1.0pre4).")
5920 ;; CUA-mode does not work with GNU Emacs version 22.1 and newer.
5921 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
5922 ;; provided the `CUA-mode' feature. Since this is no longer true,
5923 ;; we can warn the user if the `CUA-mode' feature is ever provided.
5924 (CUA-mode t nil
5925 "CUA-mode is now part of the standard GNU Emacs distribution,
5926 so you can now enable CUA via the Options menu or by customizing `cua-mode'.
5928 You have loaded an older version of CUA-mode which does not work
5929 correctly with this version of Emacs. You should remove the old
5930 version and use the one distributed with Emacs."))
5931 "Alist of packages known to cause problems in this version of Emacs.
5932 Each element has the form (PACKAGE SYMBOL REGEXP STRING).
5933 PACKAGE is either a regular expression to match file names, or a
5934 symbol (a feature name); see the documentation of
5935 `after-load-alist', to which this variable adds functions.
5936 SYMBOL is either the name of a string variable, or `t'. Upon
5937 loading PACKAGE, if SYMBOL is t or matches REGEXP, display a
5938 warning using STRING as the message.")
5940 (defun bad-package-check (package)
5941 "Run a check using the element from `bad-packages-alist' matching PACKAGE."
5942 (condition-case nil
5943 (let* ((list (assoc package bad-packages-alist))
5944 (symbol (nth 1 list)))
5945 (and list
5946 (boundp symbol)
5947 (or (eq symbol t)
5948 (and (stringp (setq symbol (eval symbol)))
5949 (string-match (nth 2 list) symbol)))
5950 (display-warning :warning (nth 3 list))))
5951 (error nil)))
5953 (mapc (lambda (elem)
5954 (eval-after-load (car elem) `(bad-package-check ',(car elem))))
5955 bad-packages-alist)
5958 (provide 'simple)
5960 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5961 ;;; simple.el ends here