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