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