(what-cursor-position): Use Lisp-readable syntax for octal and hex.
[emacs.git] / lisp / simple.el
bloba5c89dbb1d08c11a20d291246ff0951000e70320
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 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; A grab-bag of basic Emacs commands not specifically related to some
29 ;; major mode or to file-handling.
31 ;;; Code:
33 (eval-when-compile
34 (autoload 'widget-convert "wid-edit")
35 (autoload '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 next-buffer ()
56 "Switch to the next buffer in cyclic order."
57 (interactive)
58 (let ((buffer (current-buffer)))
59 (switch-to-buffer (other-buffer buffer))
60 (bury-buffer buffer)))
62 (defun prev-buffer ()
63 "Switch to the previous buffer in cyclic order."
64 (interactive)
65 (let ((list (nreverse (buffer-list)))
66 found)
67 (while (and (not found) list)
68 (let ((buffer (car list)))
69 (if (and (not (get-buffer-window buffer))
70 (not (string-match "\\` " (buffer-name buffer))))
71 (setq found buffer)))
72 (setq list (cdr list)))
73 (switch-to-buffer found)))
75 ;;; next-error support framework
77 (defgroup next-error nil
78 "`next-error' support framework."
79 :group 'compilation
80 :version "22.1")
82 (defface next-error
83 '((t (:inherit region)))
84 "Face used to highlight next error locus."
85 :group 'next-error
86 :version "22.1")
88 (defcustom next-error-highlight 0.1
89 "*Highlighting of locations in selected source buffers.
90 If number, highlight the locus in `next-error' face for given time in seconds.
91 If t, use persistent overlays fontified in `next-error' face.
92 If nil, don't highlight the locus in the source buffer.
93 If `fringe-arrow', indicate the locus by the fringe arrow."
94 :type '(choice (number :tag "Delay")
95 (const :tag "Persistent overlay" t)
96 (const :tag "No highlighting" nil)
97 (const :tag "Fringe arrow" 'fringe-arrow))
98 :group 'next-error
99 :version "22.1")
101 (defcustom next-error-highlight-no-select 0.1
102 "*Highlighting of locations in non-selected source buffers.
103 If number, highlight the locus in `next-error' face for given time in seconds.
104 If t, use persistent overlays fontified in `next-error' face.
105 If nil, don't highlight the locus in the source buffer.
106 If `fringe-arrow', indicate the locus by the fringe arrow."
107 :type '(choice (number :tag "Delay")
108 (const :tag "Persistent overlay" t)
109 (const :tag "No highlighting" nil)
110 (const :tag "Fringe arrow" 'fringe-arrow))
111 :group 'next-error
112 :version "22.1")
114 (defcustom next-error-hook nil
115 "*List of hook functions run by `next-error' after visiting source file."
116 :type 'hook
117 :group 'next-error)
119 (defvar next-error-highlight-timer nil)
121 (defvar next-error-overlay-arrow-position nil)
122 (put 'next-error-overlay-arrow-position 'overlay-arrow-string "=>")
123 (add-to-list 'overlay-arrow-variable-list 'next-error-overlay-arrow-position)
125 (defvar next-error-last-buffer nil
126 "The most recent `next-error' buffer.
127 A buffer becomes most recent when its compilation, grep, or
128 similar mode is started, or when it is used with \\[next-error]
129 or \\[compile-goto-error].")
131 (defvar next-error-function nil
132 "Function to use to find the next error in the current buffer.
133 The function is called with 2 parameters:
134 ARG is an integer specifying by how many errors to move.
135 RESET is a boolean which, if non-nil, says to go back to the beginning
136 of the errors before moving.
137 Major modes providing compile-like functionality should set this variable
138 to indicate to `next-error' that this is a candidate buffer and how
139 to navigate in it.")
141 (make-variable-buffer-local 'next-error-function)
143 (defsubst next-error-buffer-p (buffer
144 &optional avoid-current
145 extra-test-inclusive
146 extra-test-exclusive)
147 "Test if BUFFER is a `next-error' capable buffer.
149 If AVOID-CURRENT is non-nil, treat the current buffer
150 as an absolute last resort only.
152 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
153 that normally would not qualify. If it returns t, the buffer
154 in question is treated as usable.
156 The function EXTRA-TEST-EXCLUSIVE, if non-nil is called in each buffer
157 that would normally be considered usable. If it returns nil,
158 that buffer is rejected."
159 (and (buffer-name buffer) ;First make sure it's live.
160 (not (and avoid-current (eq buffer (current-buffer))))
161 (with-current-buffer buffer
162 (if next-error-function ; This is the normal test.
163 ;; Optionally reject some buffers.
164 (if extra-test-exclusive
165 (funcall extra-test-exclusive)
167 ;; Optionally accept some other buffers.
168 (and extra-test-inclusive
169 (funcall extra-test-inclusive))))))
171 (defun next-error-find-buffer (&optional avoid-current
172 extra-test-inclusive
173 extra-test-exclusive)
174 "Return a `next-error' capable buffer.
175 If AVOID-CURRENT is non-nil, treat the current buffer
176 as an absolute last resort only.
178 The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
179 that normally would not qualify. If it returns t, the buffer
180 in question is treated as usable.
182 The function EXTRA-TEST-EXCLUSIVE, if non-nil is called in each buffer
183 that would normally be considered usable. If it returns nil,
184 that buffer is rejected."
186 ;; 1. If one window on the selected frame displays such buffer, return it.
187 (let ((window-buffers
188 (delete-dups
189 (delq nil (mapcar (lambda (w)
190 (if (next-error-buffer-p
191 (window-buffer w)
192 avoid-current
193 extra-test-inclusive extra-test-exclusive)
194 (window-buffer w)))
195 (window-list))))))
196 (if (eq (length window-buffers) 1)
197 (car window-buffers)))
198 ;; 2. If next-error-last-buffer is an acceptable buffer, use that.
199 (if (and next-error-last-buffer
200 (next-error-buffer-p next-error-last-buffer avoid-current
201 extra-test-inclusive extra-test-exclusive))
202 next-error-last-buffer)
203 ;; 3. If the current buffer is acceptable, choose it.
204 (if (next-error-buffer-p (current-buffer) avoid-current
205 extra-test-inclusive extra-test-exclusive)
206 (current-buffer))
207 ;; 4. Look for any acceptable buffer.
208 (let ((buffers (buffer-list)))
209 (while (and buffers
210 (not (next-error-buffer-p
211 (car buffers) avoid-current
212 extra-test-inclusive extra-test-exclusive)))
213 (setq buffers (cdr buffers)))
214 (car buffers))
215 ;; 5. Use the current buffer as a last resort if it qualifies,
216 ;; even despite AVOID-CURRENT.
217 (and avoid-current
218 (next-error-buffer-p (current-buffer) nil
219 extra-test-inclusive extra-test-exclusive)
220 (progn
221 (message "This is the only next-error capable buffer")
222 (current-buffer)))
223 ;; 6. Give up.
224 (error "No next-error capable buffer found")))
226 (defun next-error (&optional arg reset)
227 "Visit next `next-error' message and corresponding source code.
229 If all the error messages parsed so far have been processed already,
230 the message buffer is checked for new ones.
232 A prefix ARG specifies how many error messages to move;
233 negative means move back to previous error messages.
234 Just \\[universal-argument] as a prefix means reparse the error message buffer
235 and start at the first error.
237 The RESET argument specifies that we should restart from the beginning.
239 \\[next-error] normally uses the most recently started
240 compilation, grep, or occur buffer. It can also operate on any
241 buffer with output from the \\[compile], \\[grep] commands, or,
242 more generally, on any buffer in Compilation mode or with
243 Compilation Minor mode enabled, or any buffer in which
244 `next-error-function' is bound to an appropriate function.
245 To specify use of a particular buffer for error messages, type
246 \\[next-error] in that buffer when it is the only one displayed
247 in the current frame.
249 Once \\[next-error] has chosen the buffer for error messages, it
250 runs `next-error-hook' with `run-hooks', and stays with that buffer
251 until you use it in some other buffer which uses Compilation mode
252 or Compilation Minor mode.
254 See variables `compilation-parse-errors-function' and
255 \`compilation-error-regexp-alist' for customization ideas."
256 (interactive "P")
257 (if (consp arg) (setq reset t arg nil))
258 (when (setq next-error-last-buffer (next-error-find-buffer))
259 ;; we know here that next-error-function is a valid symbol we can funcall
260 (with-current-buffer next-error-last-buffer
261 (funcall next-error-function (prefix-numeric-value arg) reset)
262 (run-hooks 'next-error-hook))))
264 (defun next-error-internal ()
265 "Visit the source code corresponding to the `next-error' message at point."
266 (setq next-error-last-buffer (current-buffer))
267 ;; we know here that next-error-function is a valid symbol we can funcall
268 (with-current-buffer next-error-last-buffer
269 (funcall next-error-function 0 nil)
270 (run-hooks 'next-error-hook)))
272 (defalias 'goto-next-locus 'next-error)
273 (defalias 'next-match 'next-error)
275 (defun previous-error (&optional n)
276 "Visit previous `next-error' message and corresponding source code.
278 Prefix arg N says how many error messages to move backwards (or
279 forwards, if negative).
281 This operates on the output from the \\[compile] and \\[grep] commands."
282 (interactive "p")
283 (next-error (- (or n 1))))
285 (defun first-error (&optional n)
286 "Restart at the first error.
287 Visit corresponding source code.
288 With prefix arg N, visit the source code of the Nth error.
289 This operates on the output from the \\[compile] command, for instance."
290 (interactive "p")
291 (next-error n t))
293 (defun next-error-no-select (&optional n)
294 "Move point to the next error in the `next-error' buffer and highlight match.
295 Prefix arg N says how many error messages to move forwards (or
296 backwards, if negative).
297 Finds and highlights the source line like \\[next-error], but does not
298 select the source buffer."
299 (interactive "p")
300 (let ((next-error-highlight next-error-highlight-no-select))
301 (next-error n))
302 (pop-to-buffer next-error-last-buffer))
304 (defun previous-error-no-select (&optional n)
305 "Move point to the previous error in the `next-error' buffer and highlight match.
306 Prefix arg N says how many error messages to move backwards (or
307 forwards, if negative).
308 Finds and highlights the source line like \\[previous-error], but does not
309 select the source buffer."
310 (interactive "p")
311 (next-error-no-select (- (or n 1))))
313 ;;; Internal variable for `next-error-follow-mode-post-command-hook'.
314 (defvar next-error-follow-last-line nil)
316 (define-minor-mode next-error-follow-minor-mode
317 "Minor mode for compilation, occur and diff modes.
318 When turned on, cursor motion in the compilation, grep, occur or diff
319 buffer causes automatic display of the corresponding source code
320 location."
321 :group 'next-error :init-value nil :lighter " Fol"
322 (if (not next-error-follow-minor-mode)
323 (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t)
324 (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t)
325 (make-local-variable 'next-error-follow-last-line)))
327 ;;; Used as a `post-command-hook' by `next-error-follow-mode'
328 ;;; for the *Compilation* *grep* and *Occur* buffers.
329 (defun next-error-follow-mode-post-command-hook ()
330 (unless (equal next-error-follow-last-line (line-number-at-pos))
331 (setq next-error-follow-last-line (line-number-at-pos))
332 (condition-case nil
333 (let ((compilation-context-lines nil))
334 (setq compilation-current-error (point))
335 (next-error-no-select 0))
336 (error t))))
341 (defun fundamental-mode ()
342 "Major mode not specialized for anything in particular.
343 Other major modes are defined by comparison with this one."
344 (interactive)
345 (kill-all-local-variables)
346 (unless delay-mode-hooks
347 (run-hooks 'after-change-major-mode-hook)))
349 ;; Making and deleting lines.
351 (defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard)))
353 (defun newline (&optional arg)
354 "Insert a newline, and move to left margin of the new line if it's blank.
355 If `use-hard-newlines' is non-nil, the newline is marked with the
356 text-property `hard'.
357 With ARG, insert that many newlines.
358 Call `auto-fill-function' if the current column number is greater
359 than the value of `fill-column' and ARG is nil."
360 (interactive "*P")
361 (barf-if-buffer-read-only)
362 ;; Inserting a newline at the end of a line produces better redisplay in
363 ;; try_window_id than inserting at the beginning of a line, and the textual
364 ;; result is the same. So, if we're at beginning of line, pretend to be at
365 ;; the end of the previous line.
366 (let ((flag (and (not (bobp))
367 (bolp)
368 ;; Make sure no functions want to be told about
369 ;; the range of the changes.
370 (not after-change-functions)
371 (not before-change-functions)
372 ;; Make sure there are no markers here.
373 (not (buffer-has-markers-at (1- (point))))
374 (not (buffer-has-markers-at (point)))
375 ;; Make sure no text properties want to know
376 ;; where the change was.
377 (not (get-char-property (1- (point)) 'modification-hooks))
378 (not (get-char-property (1- (point)) 'insert-behind-hooks))
379 (or (eobp)
380 (not (get-char-property (point) 'insert-in-front-hooks)))
381 ;; Make sure the newline before point isn't intangible.
382 (not (get-char-property (1- (point)) 'intangible))
383 ;; Make sure the newline before point isn't read-only.
384 (not (get-char-property (1- (point)) 'read-only))
385 ;; Make sure the newline before point isn't invisible.
386 (not (get-char-property (1- (point)) 'invisible))
387 ;; Make sure the newline before point has the same
388 ;; properties as the char before it (if any).
389 (< (or (previous-property-change (point)) -2)
390 (- (point) 2))))
391 (was-page-start (and (bolp)
392 (looking-at page-delimiter)))
393 (beforepos (point)))
394 (if flag (backward-char 1))
395 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
396 ;; Set last-command-char to tell self-insert what to insert.
397 (let ((last-command-char ?\n)
398 ;; Don't auto-fill if we have a numeric argument.
399 ;; Also not if flag is true (it would fill wrong line);
400 ;; there is no need to since we're at BOL.
401 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
402 (unwind-protect
403 (self-insert-command (prefix-numeric-value arg))
404 ;; If we get an error in self-insert-command, put point at right place.
405 (if flag (forward-char 1))))
406 ;; Even if we did *not* get an error, keep that forward-char;
407 ;; all further processing should apply to the newline that the user
408 ;; thinks he inserted.
410 ;; Mark the newline(s) `hard'.
411 (if use-hard-newlines
412 (set-hard-newline-properties
413 (- (point) (if arg (prefix-numeric-value arg) 1)) (point)))
414 ;; If the newline leaves the previous line blank,
415 ;; and we have a left margin, delete that from the blank line.
416 (or flag
417 (save-excursion
418 (goto-char beforepos)
419 (beginning-of-line)
420 (and (looking-at "[ \t]$")
421 (> (current-left-margin) 0)
422 (delete-region (point) (progn (end-of-line) (point))))))
423 ;; Indent the line after the newline, except in one case:
424 ;; when we added the newline at the beginning of a line
425 ;; which starts a page.
426 (or was-page-start
427 (move-to-left-margin nil t)))
428 nil)
430 (defun set-hard-newline-properties (from to)
431 (let ((sticky (get-text-property from 'rear-nonsticky)))
432 (put-text-property from to 'hard 't)
433 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
434 (if (and (listp sticky) (not (memq 'hard sticky)))
435 (put-text-property from (point) 'rear-nonsticky
436 (cons 'hard sticky)))))
438 (defun open-line (n)
439 "Insert a newline and leave point before it.
440 If there is a fill prefix and/or a `left-margin', insert them
441 on the new line if the line would have been blank.
442 With arg N, insert N newlines."
443 (interactive "*p")
444 (let* ((do-fill-prefix (and fill-prefix (bolp)))
445 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
446 (loc (point))
447 ;; Don't expand an abbrev before point.
448 (abbrev-mode nil))
449 (newline n)
450 (goto-char loc)
451 (while (> n 0)
452 (cond ((bolp)
453 (if do-left-margin (indent-to (current-left-margin)))
454 (if do-fill-prefix (insert-and-inherit fill-prefix))))
455 (forward-line 1)
456 (setq n (1- n)))
457 (goto-char loc)
458 (end-of-line)))
460 (defun split-line (&optional arg)
461 "Split current line, moving portion beyond point vertically down.
462 If the current line starts with `fill-prefix', insert it on the new
463 line as well. With prefix ARG, don't insert `fill-prefix' on new line.
465 When called from Lisp code, ARG may be a prefix string to copy."
466 (interactive "*P")
467 (skip-chars-forward " \t")
468 (let* ((col (current-column))
469 (pos (point))
470 ;; What prefix should we check for (nil means don't).
471 (prefix (cond ((stringp arg) arg)
472 (arg nil)
473 (t fill-prefix)))
474 ;; Does this line start with it?
475 (have-prfx (and prefix
476 (save-excursion
477 (beginning-of-line)
478 (looking-at (regexp-quote prefix))))))
479 (newline 1)
480 (if have-prfx (insert-and-inherit prefix))
481 (indent-to col 0)
482 (goto-char pos)))
484 (defun delete-indentation (&optional arg)
485 "Join this line to previous and fix up whitespace at join.
486 If there is a fill prefix, delete it from the beginning of this line.
487 With argument, join this line to following line."
488 (interactive "*P")
489 (beginning-of-line)
490 (if arg (forward-line 1))
491 (if (eq (preceding-char) ?\n)
492 (progn
493 (delete-region (point) (1- (point)))
494 ;; If the second line started with the fill prefix,
495 ;; delete the prefix.
496 (if (and fill-prefix
497 (<= (+ (point) (length fill-prefix)) (point-max))
498 (string= fill-prefix
499 (buffer-substring (point)
500 (+ (point) (length fill-prefix)))))
501 (delete-region (point) (+ (point) (length fill-prefix))))
502 (fixup-whitespace))))
504 (defalias 'join-line #'delete-indentation) ; easier to find
506 (defun delete-blank-lines ()
507 "On blank line, delete all surrounding blank lines, leaving just one.
508 On isolated blank line, delete that one.
509 On nonblank line, delete any immediately following blank lines."
510 (interactive "*")
511 (let (thisblank singleblank)
512 (save-excursion
513 (beginning-of-line)
514 (setq thisblank (looking-at "[ \t]*$"))
515 ;; Set singleblank if there is just one blank line here.
516 (setq singleblank
517 (and thisblank
518 (not (looking-at "[ \t]*\n[ \t]*$"))
519 (or (bobp)
520 (progn (forward-line -1)
521 (not (looking-at "[ \t]*$")))))))
522 ;; Delete preceding blank lines, and this one too if it's the only one.
523 (if thisblank
524 (progn
525 (beginning-of-line)
526 (if singleblank (forward-line 1))
527 (delete-region (point)
528 (if (re-search-backward "[^ \t\n]" nil t)
529 (progn (forward-line 1) (point))
530 (point-min)))))
531 ;; Delete following blank lines, unless the current line is blank
532 ;; and there are no following blank lines.
533 (if (not (and thisblank singleblank))
534 (save-excursion
535 (end-of-line)
536 (forward-line 1)
537 (delete-region (point)
538 (if (re-search-forward "[^ \t\n]" nil t)
539 (progn (beginning-of-line) (point))
540 (point-max)))))
541 ;; Handle the special case where point is followed by newline and eob.
542 ;; Delete the line, leaving point at eob.
543 (if (looking-at "^[ \t]*\n\\'")
544 (delete-region (point) (point-max)))))
546 (defun delete-trailing-whitespace ()
547 "Delete all the trailing whitespace across the current buffer.
548 All whitespace after the last non-whitespace character in a line is deleted.
549 This respects narrowing, created by \\[narrow-to-region] and friends.
550 A formfeed is not considered whitespace by this function."
551 (interactive "*")
552 (save-match-data
553 (save-excursion
554 (goto-char (point-min))
555 (while (re-search-forward "\\s-$" nil t)
556 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
557 ;; Don't delete formfeeds, even if they are considered whitespace.
558 (save-match-data
559 (if (looking-at ".*\f")
560 (goto-char (match-end 0))))
561 (delete-region (point) (match-end 0))))))
563 (defun newline-and-indent ()
564 "Insert a newline, then indent according to major mode.
565 Indentation is done using the value of `indent-line-function'.
566 In programming language modes, this is the same as TAB.
567 In some text modes, where TAB inserts a tab, this command indents to the
568 column specified by the function `current-left-margin'."
569 (interactive "*")
570 (delete-horizontal-space t)
571 (newline)
572 (indent-according-to-mode))
574 (defun reindent-then-newline-and-indent ()
575 "Reindent current line, insert newline, then indent the new line.
576 Indentation of both lines is done according to the current major mode,
577 which means calling the current value of `indent-line-function'.
578 In programming language modes, this is the same as TAB.
579 In some text modes, where TAB inserts a tab, this indents to the
580 column specified by the function `current-left-margin'."
581 (interactive "*")
582 (let ((pos (point)))
583 ;; Be careful to insert the newline before indenting the line.
584 ;; Otherwise, the indentation might be wrong.
585 (newline)
586 (save-excursion
587 (goto-char pos)
588 (indent-according-to-mode)
589 (delete-horizontal-space t))
590 (indent-according-to-mode)))
592 (defun quoted-insert (arg)
593 "Read next input character and insert it.
594 This is useful for inserting control characters.
596 If the first character you type after this command is an octal digit,
597 you should type a sequence of octal digits which specify a character code.
598 Any nondigit terminates the sequence. If the terminator is a RET,
599 it is discarded; any other terminator is used itself as input.
600 The variable `read-quoted-char-radix' specifies the radix for this feature;
601 set it to 10 or 16 to use decimal or hex instead of octal.
603 In overwrite mode, this function inserts the character anyway, and
604 does not handle octal digits specially. This means that if you use
605 overwrite as your normal editing mode, you can use this function to
606 insert characters when necessary.
608 In binary overwrite mode, this function does overwrite, and octal
609 digits are interpreted as a character code. This is intended to be
610 useful for editing binary files."
611 (interactive "*p")
612 (let* ((char (let (translation-table-for-input)
613 (if (or (not overwrite-mode)
614 (eq overwrite-mode 'overwrite-mode-binary))
615 (read-quoted-char)
616 (read-char)))))
617 ;; Assume character codes 0240 - 0377 stand for characters in some
618 ;; single-byte character set, and convert them to Emacs
619 ;; characters.
620 (if (and enable-multibyte-characters
621 (>= char ?\240)
622 (<= char ?\377))
623 (setq char (unibyte-char-to-multibyte char)))
624 (if (> arg 0)
625 (if (eq overwrite-mode 'overwrite-mode-binary)
626 (delete-char arg)))
627 (while (> arg 0)
628 (insert-and-inherit char)
629 (setq arg (1- arg)))))
631 (defun forward-to-indentation (&optional arg)
632 "Move forward ARG lines and position at first nonblank character."
633 (interactive "p")
634 (forward-line (or arg 1))
635 (skip-chars-forward " \t"))
637 (defun backward-to-indentation (&optional arg)
638 "Move backward ARG lines and position at first nonblank character."
639 (interactive "p")
640 (forward-line (- (or arg 1)))
641 (skip-chars-forward " \t"))
643 (defun back-to-indentation ()
644 "Move point to the first non-whitespace character on this line."
645 (interactive)
646 (beginning-of-line 1)
647 (skip-syntax-forward " " (line-end-position))
648 ;; Move back over chars that have whitespace syntax but have the p flag.
649 (backward-prefix-chars))
651 (defun fixup-whitespace ()
652 "Fixup white space between objects around point.
653 Leave one space or none, according to the context."
654 (interactive "*")
655 (save-excursion
656 (delete-horizontal-space)
657 (if (or (looking-at "^\\|\\s)")
658 (save-excursion (forward-char -1)
659 (looking-at "$\\|\\s(\\|\\s'")))
661 (insert ?\s))))
663 (defun delete-horizontal-space (&optional backward-only)
664 "Delete all spaces and tabs around point.
665 If BACKWARD-ONLY is non-nil, only delete spaces before point."
666 (interactive "*")
667 (let ((orig-pos (point)))
668 (delete-region
669 (if backward-only
670 orig-pos
671 (progn
672 (skip-chars-forward " \t")
673 (constrain-to-field nil orig-pos t)))
674 (progn
675 (skip-chars-backward " \t")
676 (constrain-to-field nil orig-pos)))))
678 (defun just-one-space (&optional n)
679 "Delete all spaces and tabs around point, leaving one space (or N spaces)."
680 (interactive "*p")
681 (let ((orig-pos (point)))
682 (skip-chars-backward " \t")
683 (constrain-to-field nil orig-pos)
684 (dotimes (i (or n 1))
685 (if (= (following-char) ?\s)
686 (forward-char 1)
687 (insert ?\s)))
688 (delete-region
689 (point)
690 (progn
691 (skip-chars-forward " \t")
692 (constrain-to-field nil orig-pos t)))))
694 (defun beginning-of-buffer (&optional arg)
695 "Move point to the beginning of the buffer; leave mark at previous position.
696 With \\[universal-argument] prefix, do not set mark at previous position.
697 With numeric arg N, put point N/10 of the way from the beginning.
699 If the buffer is narrowed, this command uses the beginning and size
700 of the accessible part of the buffer.
702 Don't use this command in Lisp programs!
703 \(goto-char (point-min)) is faster and avoids clobbering the mark."
704 (interactive "P")
705 (or (consp arg)
706 (and transient-mark-mode mark-active)
707 (push-mark))
708 (let ((size (- (point-max) (point-min))))
709 (goto-char (if (and arg (not (consp arg)))
710 (+ (point-min)
711 (if (> size 10000)
712 ;; Avoid overflow for large buffer sizes!
713 (* (prefix-numeric-value arg)
714 (/ size 10))
715 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
716 (point-min))))
717 (if arg (forward-line 1)))
719 (defun end-of-buffer (&optional arg)
720 "Move point to the end of the buffer; leave mark at previous position.
721 With \\[universal-argument] prefix, do not set mark at previous position.
722 With numeric arg N, put point N/10 of the way from the end.
724 If the buffer is narrowed, this command uses the beginning and size
725 of the accessible part of the buffer.
727 Don't use this command in Lisp programs!
728 \(goto-char (point-max)) is faster and avoids clobbering the mark."
729 (interactive "P")
730 (or (consp arg)
731 (and transient-mark-mode mark-active)
732 (push-mark))
733 (let ((size (- (point-max) (point-min))))
734 (goto-char (if (and arg (not (consp arg)))
735 (- (point-max)
736 (if (> size 10000)
737 ;; Avoid overflow for large buffer sizes!
738 (* (prefix-numeric-value arg)
739 (/ size 10))
740 (/ (* size (prefix-numeric-value arg)) 10)))
741 (point-max))))
742 ;; If we went to a place in the middle of the buffer,
743 ;; adjust it to the beginning of a line.
744 (cond (arg (forward-line 1))
745 ((> (point) (window-end nil t))
746 ;; If the end of the buffer is not already on the screen,
747 ;; then scroll specially to put it near, but not at, the bottom.
748 (overlay-recenter (point))
749 (recenter -3))))
751 (defun mark-whole-buffer ()
752 "Put point at beginning and mark at end of buffer.
753 You probably should not use this function in Lisp programs;
754 it is usually a mistake for a Lisp function to use any subroutine
755 that uses or sets the mark."
756 (interactive)
757 (push-mark (point))
758 (push-mark (point-max) nil t)
759 (goto-char (point-min)))
762 ;; Counting lines, one way or another.
764 (defun goto-line (arg &optional buffer)
765 "Goto line ARG, counting from line 1 at beginning of buffer.
766 Normally, move point in the current buffer.
767 With just \\[universal-argument] as argument, move point in the most recently
768 displayed other buffer, and switch to it. When called from Lisp code,
769 the optional argument BUFFER specifies a buffer to switch to.
771 If there's a number in the buffer at point, it is the default for ARG."
772 (interactive
773 (if (and current-prefix-arg (not (consp current-prefix-arg)))
774 (list (prefix-numeric-value current-prefix-arg))
775 ;; Look for a default, a number in the buffer at point.
776 (let* ((default
777 (save-excursion
778 (skip-chars-backward "0-9")
779 (if (looking-at "[0-9]")
780 (buffer-substring-no-properties
781 (point)
782 (progn (skip-chars-forward "0-9")
783 (point))))))
784 ;; Decide if we're switching buffers.
785 (buffer
786 (if (consp current-prefix-arg)
787 (other-buffer (current-buffer) t)))
788 (buffer-prompt
789 (if buffer
790 (concat " in " (buffer-name buffer))
791 "")))
792 ;; Read the argument, offering that number (if any) as default.
793 (list (read-from-minibuffer (format (if default "Goto line%s (%s): "
794 "Goto line%s: ")
795 buffer-prompt
796 default)
797 nil nil t
798 'minibuffer-history
799 default)
800 buffer))))
801 ;; Switch to the desired buffer, one way or another.
802 (if buffer
803 (let ((window (get-buffer-window buffer)))
804 (if window (select-window window)
805 (switch-to-buffer-other-window buffer))))
806 ;; Move to the specified line number in that buffer.
807 (save-restriction
808 (widen)
809 (goto-char 1)
810 (if (eq selective-display t)
811 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
812 (forward-line (1- arg)))))
814 (defun count-lines-region (start end)
815 "Print number of lines and characters in the region."
816 (interactive "r")
817 (message "Region has %d lines, %d characters"
818 (count-lines start end) (- end start)))
820 (defun what-line ()
821 "Print the current buffer line number and narrowed line number of point."
822 (interactive)
823 (let ((start (point-min))
824 (n (line-number-at-pos)))
825 (if (= start 1)
826 (message "Line %d" n)
827 (save-excursion
828 (save-restriction
829 (widen)
830 (message "line %d (narrowed line %d)"
831 (+ n (line-number-at-pos start) -1) n))))))
833 (defun count-lines (start end)
834 "Return number of lines between START and END.
835 This is usually the number of newlines between them,
836 but can be one more if START is not equal to END
837 and the greater of them is not at the start of a line."
838 (save-excursion
839 (save-restriction
840 (narrow-to-region start end)
841 (goto-char (point-min))
842 (if (eq selective-display t)
843 (save-match-data
844 (let ((done 0))
845 (while (re-search-forward "[\n\C-m]" nil t 40)
846 (setq done (+ 40 done)))
847 (while (re-search-forward "[\n\C-m]" nil t 1)
848 (setq done (+ 1 done)))
849 (goto-char (point-max))
850 (if (and (/= start end)
851 (not (bolp)))
852 (1+ done)
853 done)))
854 (- (buffer-size) (forward-line (buffer-size)))))))
856 (defun line-number-at-pos (&optional pos)
857 "Return (narrowed) buffer line number at position POS.
858 If POS is nil, use current buffer location."
859 (let ((opoint (or pos (point))) start)
860 (save-excursion
861 (goto-char (point-min))
862 (setq start (point))
863 (goto-char opoint)
864 (forward-line 0)
865 (1+ (count-lines start (point))))))
867 (defun what-cursor-position (&optional detail)
868 "Print info on cursor position (on screen and within buffer).
869 Also describe the character after point, and give its character code
870 in octal, decimal and hex.
872 For a non-ASCII multibyte character, also give its encoding in the
873 buffer's selected coding system if the coding system encodes the
874 character safely. If the character is encoded into one byte, that
875 code is shown in hex. If the character is encoded into more than one
876 byte, just \"...\" is shown.
878 In addition, with prefix argument, show details about that character
879 in *Help* buffer. See also the command `describe-char'."
880 (interactive "P")
881 (let* ((char (following-char))
882 (beg (point-min))
883 (end (point-max))
884 (pos (point))
885 (total (buffer-size))
886 (percent (if (> total 50000)
887 ;; Avoid overflow from multiplying by 100!
888 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
889 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
890 (hscroll (if (= (window-hscroll) 0)
892 (format " Hscroll=%d" (window-hscroll))))
893 (col (current-column)))
894 (if (= pos end)
895 (if (or (/= beg 1) (/= end (1+ total)))
896 (message "point=%d of %d (%d%%) <%d-%d> column=%d%s"
897 pos total percent beg end col hscroll)
898 (message "point=%d of %d (EOB) column=%d%s"
899 pos total col hscroll))
900 (let ((coding buffer-file-coding-system)
901 encoded encoding-msg display-prop under-display)
902 (if (or (not coding)
903 (eq (coding-system-type coding) t))
904 (setq coding default-buffer-file-coding-system))
905 (if (not (char-valid-p char))
906 (setq encoding-msg
907 (format "(%d, #o%o, #x%x, invalid)" char char char))
908 ;; Check if the character is displayed with some `display'
909 ;; text property. In that case, set under-display to the
910 ;; buffer substring covered by that property.
911 (setq display-prop (get-text-property pos 'display))
912 (if display-prop
913 (let ((to (or (next-single-property-change pos 'display)
914 (point-max))))
915 (if (< to (+ pos 4))
916 (setq under-display "")
917 (setq under-display "..."
918 to (+ pos 4)))
919 (setq under-display
920 (concat (buffer-substring-no-properties pos to)
921 under-display)))
922 (setq encoded (and (>= char 128) (encode-coding-char char coding))))
923 (setq encoding-msg
924 (if display-prop
925 (if (not (stringp display-prop))
926 (format "(%d, #o%o, #x%x, part of display \"%s\")"
927 char char char under-display)
928 (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")"
929 char char char under-display display-prop))
930 (if encoded
931 (format "(%d, #o%o, #x%x, file %s)"
932 char char char
933 (if (> (length encoded) 1)
934 "..."
935 (encoded-string-description encoded coding)))
936 (format "(%d, #o%o, #x%x)" char char char)))))
937 (if detail
938 ;; We show the detailed information about CHAR.
939 (describe-char (point)))
940 (if (or (/= beg 1) (/= end (1+ total)))
941 (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
942 (if (< char 256)
943 (single-key-description char)
944 (buffer-substring-no-properties (point) (1+ (point))))
945 encoding-msg pos total percent beg end col hscroll)
946 (message "Char: %s %s point=%d of %d (%d%%) column=%d%s"
947 (if enable-multibyte-characters
948 (if (< char 128)
949 (single-key-description char)
950 (buffer-substring-no-properties (point) (1+ (point))))
951 (single-key-description char))
952 encoding-msg pos total percent col hscroll))))))
954 (defvar read-expression-map
955 (let ((m (make-sparse-keymap)))
956 (define-key m "\M-\t" 'lisp-complete-symbol)
957 (set-keymap-parent m minibuffer-local-map)
959 "Minibuffer keymap used for reading Lisp expressions.")
961 (defvar read-expression-history nil)
963 (defcustom eval-expression-print-level 4
964 "Value for `print-level' while printing value in `eval-expression'.
965 A value of nil means no limit."
966 :group 'lisp
967 :type '(choice (const :tag "No Limit" nil) integer)
968 :version "21.1")
970 (defcustom eval-expression-print-length 12
971 "Value for `print-length' while printing value in `eval-expression'.
972 A value of nil means no limit."
973 :group 'lisp
974 :type '(choice (const :tag "No Limit" nil) integer)
975 :version "21.1")
977 (defcustom eval-expression-debug-on-error t
978 "If non-nil set `debug-on-error' to t in `eval-expression'.
979 If nil, don't change the value of `debug-on-error'."
980 :group 'lisp
981 :type 'boolean
982 :version "21.1")
984 (defun eval-expression-print-format (value)
985 "Format VALUE as a result of evaluated expression.
986 Return a formatted string which is displayed in the echo area
987 in addition to the value printed by prin1 in functions which
988 display the result of expression evaluation."
989 (if (and (integerp value)
990 (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
991 (eq this-command last-command)
992 (if (boundp 'edebug-active) edebug-active)))
993 (let ((char-string
994 (if (or (if (boundp 'edebug-active) edebug-active)
995 (memq this-command '(eval-last-sexp eval-print-last-sexp)))
996 (prin1-char value))))
997 (if char-string
998 (format " (#o%o, #x%x, %s)" value value char-string)
999 (format " (#o%o, #x%x)" value value)))))
1001 ;; We define this, rather than making `eval' interactive,
1002 ;; for the sake of completion of names like eval-region, eval-current-buffer.
1003 (defun eval-expression (eval-expression-arg
1004 &optional eval-expression-insert-value)
1005 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
1006 Value is also consed on to front of the variable `values'.
1007 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
1008 insert the result into the current buffer instead of printing it in
1009 the echo area."
1010 (interactive
1011 (list (read-from-minibuffer "Eval: "
1012 nil read-expression-map t
1013 'read-expression-history)
1014 current-prefix-arg))
1016 (if (null eval-expression-debug-on-error)
1017 (setq values (cons (eval eval-expression-arg) values))
1018 (let ((old-value (make-symbol "t")) new-value)
1019 ;; Bind debug-on-error to something unique so that we can
1020 ;; detect when evaled code changes it.
1021 (let ((debug-on-error old-value))
1022 (setq values (cons (eval eval-expression-arg) values))
1023 (setq new-value debug-on-error))
1024 ;; If evaled code has changed the value of debug-on-error,
1025 ;; propagate that change to the global binding.
1026 (unless (eq old-value new-value)
1027 (setq debug-on-error new-value))))
1029 (let ((print-length eval-expression-print-length)
1030 (print-level eval-expression-print-level))
1031 (if eval-expression-insert-value
1032 (with-no-warnings
1033 (let ((standard-output (current-buffer)))
1034 (eval-last-sexp-print-value (car values))))
1035 (prog1
1036 (prin1 (car values) t)
1037 (let ((str (eval-expression-print-format (car values))))
1038 (if str (princ str t)))))))
1040 (defun edit-and-eval-command (prompt command)
1041 "Prompting with PROMPT, let user edit COMMAND and eval result.
1042 COMMAND is a Lisp expression. Let user edit that expression in
1043 the minibuffer, then read and evaluate the result."
1044 (let ((command
1045 (let ((print-level nil)
1046 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1047 (unwind-protect
1048 (read-from-minibuffer prompt
1049 (prin1-to-string command)
1050 read-expression-map t
1051 'command-history)
1052 ;; If command was added to command-history as a string,
1053 ;; get rid of that. We want only evaluable expressions there.
1054 (if (stringp (car command-history))
1055 (setq command-history (cdr command-history)))))))
1057 ;; If command to be redone does not match front of history,
1058 ;; add it to the history.
1059 (or (equal command (car command-history))
1060 (setq command-history (cons command command-history)))
1061 (eval command)))
1063 (defun repeat-complex-command (arg)
1064 "Edit and re-evaluate last complex command, or ARGth from last.
1065 A complex command is one which used the minibuffer.
1066 The command is placed in the minibuffer as a Lisp form for editing.
1067 The result is executed, repeating the command as changed.
1068 If the command has been changed or is not the most recent previous command
1069 it is added to the front of the command history.
1070 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
1071 to get different commands to edit and resubmit."
1072 (interactive "p")
1073 (let ((elt (nth (1- arg) command-history))
1074 newcmd)
1075 (if elt
1076 (progn
1077 (setq newcmd
1078 (let ((print-level nil)
1079 (minibuffer-history-position arg)
1080 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1081 (unwind-protect
1082 (read-from-minibuffer
1083 "Redo: " (prin1-to-string elt) read-expression-map t
1084 (cons 'command-history arg))
1086 ;; If command was added to command-history as a
1087 ;; string, get rid of that. We want only
1088 ;; evaluable expressions there.
1089 (if (stringp (car command-history))
1090 (setq command-history (cdr command-history))))))
1092 ;; If command to be redone does not match front of history,
1093 ;; add it to the history.
1094 (or (equal newcmd (car command-history))
1095 (setq command-history (cons newcmd command-history)))
1096 (eval newcmd))
1097 (if command-history
1098 (error "Argument %d is beyond length of command history" arg)
1099 (error "There are no previous complex commands to repeat")))))
1101 (defvar minibuffer-history nil
1102 "Default minibuffer history list.
1103 This is used for all minibuffer input
1104 except when an alternate history list is specified.")
1105 (defvar minibuffer-history-sexp-flag nil
1106 "Control whether history list elements are expressions or strings.
1107 If the value of this variable equals current minibuffer depth,
1108 they are expressions; otherwise they are strings.
1109 \(That convention is designed to do the right thing fora
1110 recursive uses of the minibuffer.)")
1111 (setq minibuffer-history-variable 'minibuffer-history)
1112 (setq minibuffer-history-position nil)
1113 (defvar minibuffer-history-search-history nil)
1115 (defvar minibuffer-text-before-history nil
1116 "Text that was in this minibuffer before any history commands.
1117 This is nil if there have not yet been any history commands
1118 in this use of the minibuffer.")
1120 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
1122 (defun minibuffer-history-initialize ()
1123 (setq minibuffer-text-before-history nil))
1125 (defun minibuffer-avoid-prompt (new old)
1126 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1127 (constrain-to-field nil (point-max)))
1129 (defcustom minibuffer-history-case-insensitive-variables nil
1130 "*Minibuffer history variables for which matching should ignore case.
1131 If a history variable is a member of this list, then the
1132 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1133 commands ignore case when searching it, regardless of `case-fold-search'."
1134 :type '(repeat variable)
1135 :group 'minibuffer)
1137 (defun previous-matching-history-element (regexp n)
1138 "Find the previous history element that matches REGEXP.
1139 \(Previous history elements refer to earlier actions.)
1140 With prefix argument N, search for Nth previous match.
1141 If N is negative, find the next or Nth next match.
1142 Normally, history elements are matched case-insensitively if
1143 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1144 makes the search case-sensitive.
1145 See also `minibuffer-history-case-insensitive-variables'."
1146 (interactive
1147 (let* ((enable-recursive-minibuffers t)
1148 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1150 minibuffer-local-map
1152 'minibuffer-history-search-history
1153 (car minibuffer-history-search-history))))
1154 ;; Use the last regexp specified, by default, if input is empty.
1155 (list (if (string= regexp "")
1156 (if minibuffer-history-search-history
1157 (car minibuffer-history-search-history)
1158 (error "No previous history search regexp"))
1159 regexp)
1160 (prefix-numeric-value current-prefix-arg))))
1161 (unless (zerop n)
1162 (if (and (zerop minibuffer-history-position)
1163 (null minibuffer-text-before-history))
1164 (setq minibuffer-text-before-history
1165 (minibuffer-contents-no-properties)))
1166 (let ((history (symbol-value minibuffer-history-variable))
1167 (case-fold-search
1168 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
1169 ;; On some systems, ignore case for file names.
1170 (if (memq minibuffer-history-variable
1171 minibuffer-history-case-insensitive-variables)
1173 ;; Respect the user's setting for case-fold-search:
1174 case-fold-search)
1175 nil))
1176 prevpos
1177 match-string
1178 match-offset
1179 (pos minibuffer-history-position))
1180 (while (/= n 0)
1181 (setq prevpos pos)
1182 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1183 (when (= pos prevpos)
1184 (error (if (= pos 1)
1185 "No later matching history item"
1186 "No earlier matching history item")))
1187 (setq match-string
1188 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
1189 (let ((print-level nil))
1190 (prin1-to-string (nth (1- pos) history)))
1191 (nth (1- pos) history)))
1192 (setq match-offset
1193 (if (< n 0)
1194 (and (string-match regexp match-string)
1195 (match-end 0))
1196 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
1197 (match-beginning 1))))
1198 (when match-offset
1199 (setq n (+ n (if (< n 0) 1 -1)))))
1200 (setq minibuffer-history-position pos)
1201 (goto-char (point-max))
1202 (delete-minibuffer-contents)
1203 (insert match-string)
1204 (goto-char (+ (minibuffer-prompt-end) match-offset))))
1205 (if (memq (car (car command-history)) '(previous-matching-history-element
1206 next-matching-history-element))
1207 (setq command-history (cdr command-history))))
1209 (defun next-matching-history-element (regexp n)
1210 "Find the next history element that matches REGEXP.
1211 \(The next history element refers to a more recent action.)
1212 With prefix argument N, search for Nth next match.
1213 If N is negative, find the previous or Nth previous match.
1214 Normally, history elements are matched case-insensitively if
1215 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1216 makes the search case-sensitive."
1217 (interactive
1218 (let* ((enable-recursive-minibuffers t)
1219 (regexp (read-from-minibuffer "Next element matching (regexp): "
1221 minibuffer-local-map
1223 'minibuffer-history-search-history
1224 (car minibuffer-history-search-history))))
1225 ;; Use the last regexp specified, by default, if input is empty.
1226 (list (if (string= regexp "")
1227 (if minibuffer-history-search-history
1228 (car minibuffer-history-search-history)
1229 (error "No previous history search regexp"))
1230 regexp)
1231 (prefix-numeric-value current-prefix-arg))))
1232 (previous-matching-history-element regexp (- n)))
1234 (defvar minibuffer-temporary-goal-position nil)
1236 (defun next-history-element (n)
1237 "Insert the next element of the minibuffer history into the minibuffer."
1238 (interactive "p")
1239 (or (zerop n)
1240 (let ((narg (- minibuffer-history-position n))
1241 (minimum (if minibuffer-default -1 0))
1242 elt minibuffer-returned-to-present)
1243 (if (and (zerop minibuffer-history-position)
1244 (null minibuffer-text-before-history))
1245 (setq minibuffer-text-before-history
1246 (minibuffer-contents-no-properties)))
1247 (if (< narg minimum)
1248 (if minibuffer-default
1249 (error "End of history; no next item")
1250 (error "End of history; no default available")))
1251 (if (> narg (length (symbol-value minibuffer-history-variable)))
1252 (error "Beginning of history; no preceding item"))
1253 (unless (memq last-command '(next-history-element
1254 previous-history-element))
1255 (let ((prompt-end (minibuffer-prompt-end)))
1256 (set (make-local-variable 'minibuffer-temporary-goal-position)
1257 (cond ((<= (point) prompt-end) prompt-end)
1258 ((eobp) nil)
1259 (t (point))))))
1260 (goto-char (point-max))
1261 (delete-minibuffer-contents)
1262 (setq minibuffer-history-position narg)
1263 (cond ((= narg -1)
1264 (setq elt minibuffer-default))
1265 ((= narg 0)
1266 (setq elt (or minibuffer-text-before-history ""))
1267 (setq minibuffer-returned-to-present t)
1268 (setq minibuffer-text-before-history nil))
1269 (t (setq elt (nth (1- minibuffer-history-position)
1270 (symbol-value minibuffer-history-variable)))))
1271 (insert
1272 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
1273 (not minibuffer-returned-to-present))
1274 (let ((print-level nil))
1275 (prin1-to-string elt))
1276 elt))
1277 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
1279 (defun previous-history-element (n)
1280 "Inserts the previous element of the minibuffer history into the minibuffer."
1281 (interactive "p")
1282 (next-history-element (- n)))
1284 (defun next-complete-history-element (n)
1285 "Get next history element which completes the minibuffer before the point.
1286 The contents of the minibuffer after the point are deleted, and replaced
1287 by the new completion."
1288 (interactive "p")
1289 (let ((point-at-start (point)))
1290 (next-matching-history-element
1291 (concat
1292 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1294 ;; next-matching-history-element always puts us at (point-min).
1295 ;; Move to the position we were at before changing the buffer contents.
1296 ;; This is still sensical, because the text before point has not changed.
1297 (goto-char point-at-start)))
1299 (defun previous-complete-history-element (n)
1301 Get previous history element which completes the minibuffer before the point.
1302 The contents of the minibuffer after the point are deleted, and replaced
1303 by the new completion."
1304 (interactive "p")
1305 (next-complete-history-element (- n)))
1307 ;; For compatibility with the old subr of the same name.
1308 (defun minibuffer-prompt-width ()
1309 "Return the display width of the minibuffer prompt.
1310 Return 0 if current buffer is not a minibuffer."
1311 ;; Return the width of everything before the field at the end of
1312 ;; the buffer; this should be 0 for normal buffers.
1313 (1- (minibuffer-prompt-end)))
1315 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1316 (defalias 'advertised-undo 'undo)
1318 (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t)
1319 "Table mapping redo records to the corresponding undo one.
1320 A redo record for undo-in-region maps to t.
1321 A redo record for ordinary undo maps to the following (earlier) undo.")
1323 (defvar undo-in-region nil
1324 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1326 (defvar undo-no-redo nil
1327 "If t, `undo' doesn't go through redo entries.")
1329 (defvar pending-undo-list nil
1330 "Within a run of consecutive undo commands, list remaining to be undone.
1331 If t, we undid all the way to the end of it.")
1333 (defun undo (&optional arg)
1334 "Undo some previous changes.
1335 Repeat this command to undo more changes.
1336 A numeric argument serves as a repeat count.
1338 In Transient Mark mode when the mark is active, only undo changes within
1339 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1340 as an argument limits undo to changes within the current region."
1341 (interactive "*P")
1342 ;; Make last-command indicate for the next command that this was an undo.
1343 ;; That way, another undo will undo more.
1344 ;; If we get to the end of the undo history and get an error,
1345 ;; another undo command will find the undo history empty
1346 ;; and will get another error. To begin undoing the undos,
1347 ;; you must type some other command.
1348 (let ((modified (buffer-modified-p))
1349 (recent-save (recent-auto-save-p))
1350 message)
1351 ;; If we get an error in undo-start,
1352 ;; the next command should not be a "consecutive undo".
1353 ;; So set `this-command' to something other than `undo'.
1354 (setq this-command 'undo-start)
1356 (unless (and (eq last-command 'undo)
1357 (or (eq pending-undo-list t)
1358 ;; If something (a timer or filter?) changed the buffer
1359 ;; since the previous command, don't continue the undo seq.
1360 (let ((list buffer-undo-list))
1361 (while (eq (car list) nil)
1362 (setq list (cdr list)))
1363 ;; If the last undo record made was made by undo
1364 ;; it shows nothing else happened in between.
1365 (gethash list undo-equiv-table))))
1366 (setq undo-in-region
1367 (if transient-mark-mode mark-active (and arg (not (numberp arg)))))
1368 (if undo-in-region
1369 (undo-start (region-beginning) (region-end))
1370 (undo-start))
1371 ;; get rid of initial undo boundary
1372 (undo-more 1))
1373 ;; If we got this far, the next command should be a consecutive undo.
1374 (setq this-command 'undo)
1375 ;; Check to see whether we're hitting a redo record, and if
1376 ;; so, ask the user whether she wants to skip the redo/undo pair.
1377 (let ((equiv (gethash pending-undo-list undo-equiv-table)))
1378 (or (eq (selected-window) (minibuffer-window))
1379 (setq message (if undo-in-region
1380 (if equiv "Redo in region!" "Undo in region!")
1381 (if equiv "Redo!" "Undo!"))))
1382 (when (and (consp equiv) undo-no-redo)
1383 ;; The equiv entry might point to another redo record if we have done
1384 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1385 (while (let ((next (gethash equiv undo-equiv-table)))
1386 (if next (setq equiv next))))
1387 (setq pending-undo-list equiv)))
1388 (undo-more
1389 (if (or transient-mark-mode (numberp arg))
1390 (prefix-numeric-value arg)
1392 ;; Record the fact that the just-generated undo records come from an
1393 ;; undo operation--that is, they are redo records.
1394 ;; In the ordinary case (not within a region), map the redo
1395 ;; record to the following undos.
1396 ;; I don't know how to do that in the undo-in-region case.
1397 (puthash buffer-undo-list
1398 (if undo-in-region t pending-undo-list)
1399 undo-equiv-table)
1400 ;; Don't specify a position in the undo record for the undo command.
1401 ;; Instead, undoing this should move point to where the change is.
1402 (let ((tail buffer-undo-list)
1403 (prev nil))
1404 (while (car tail)
1405 (when (integerp (car tail))
1406 (let ((pos (car tail)))
1407 (if prev
1408 (setcdr prev (cdr tail))
1409 (setq buffer-undo-list (cdr tail)))
1410 (setq tail (cdr tail))
1411 (while (car tail)
1412 (if (eq pos (car tail))
1413 (if prev
1414 (setcdr prev (cdr tail))
1415 (setq buffer-undo-list (cdr tail)))
1416 (setq prev tail))
1417 (setq tail (cdr tail)))
1418 (setq tail nil)))
1419 (setq prev tail tail (cdr tail))))
1420 ;; Record what the current undo list says,
1421 ;; so the next command can tell if the buffer was modified in between.
1422 (and modified (not (buffer-modified-p))
1423 (delete-auto-save-file-if-necessary recent-save))
1424 ;; Display a message announcing success.
1425 (if message
1426 (message message))))
1428 (defun buffer-disable-undo (&optional buffer)
1429 "Make BUFFER stop keeping undo information.
1430 No argument or nil as argument means do this for the current buffer."
1431 (interactive)
1432 (with-current-buffer (if buffer (get-buffer buffer) (current-buffer))
1433 (setq buffer-undo-list t)))
1435 (defun undo-only (&optional arg)
1436 "Undo some previous changes.
1437 Repeat this command to undo more changes.
1438 A numeric argument serves as a repeat count.
1439 Contrary to `undo', this will not redo a previous undo."
1440 (interactive "*p")
1441 (let ((undo-no-redo t)) (undo arg)))
1443 (defvar undo-in-progress nil
1444 "Non-nil while performing an undo.
1445 Some change-hooks test this variable to do something different.")
1447 (defun undo-more (n)
1448 "Undo back N undo-boundaries beyond what was already undone recently.
1449 Call `undo-start' to get ready to undo recent changes,
1450 then call `undo-more' one or more times to undo them."
1451 (or (listp pending-undo-list)
1452 (error (concat "No further undo information"
1453 (and transient-mark-mode mark-active
1454 " for region"))))
1455 (let ((undo-in-progress t))
1456 (setq pending-undo-list (primitive-undo n pending-undo-list))
1457 (if (null pending-undo-list)
1458 (setq pending-undo-list t))))
1460 ;; Deep copy of a list
1461 (defun undo-copy-list (list)
1462 "Make a copy of undo list LIST."
1463 (mapcar 'undo-copy-list-1 list))
1465 (defun undo-copy-list-1 (elt)
1466 (if (consp elt)
1467 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1468 elt))
1470 (defun undo-start (&optional beg end)
1471 "Set `pending-undo-list' to the front of the undo list.
1472 The next call to `undo-more' will undo the most recently made change.
1473 If BEG and END are specified, then only undo elements
1474 that apply to text between BEG and END are used; other undo elements
1475 are ignored. If BEG and END are nil, all undo elements are used."
1476 (if (eq buffer-undo-list t)
1477 (error "No undo information in this buffer"))
1478 (setq pending-undo-list
1479 (if (and beg end (not (= beg end)))
1480 (undo-make-selective-list (min beg end) (max beg end))
1481 buffer-undo-list)))
1483 (defvar undo-adjusted-markers)
1485 (defun undo-make-selective-list (start end)
1486 "Return a list of undo elements for the region START to END.
1487 The elements come from `buffer-undo-list', but we keep only
1488 the elements inside this region, and discard those outside this region.
1489 If we find an element that crosses an edge of this region,
1490 we stop and ignore all further elements."
1491 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1492 (undo-list (list nil))
1493 undo-adjusted-markers
1494 some-rejected
1495 undo-elt undo-elt temp-undo-list delta)
1496 (while undo-list-copy
1497 (setq undo-elt (car undo-list-copy))
1498 (let ((keep-this
1499 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1500 ;; This is a "was unmodified" element.
1501 ;; Keep it if we have kept everything thus far.
1502 (not some-rejected))
1504 (undo-elt-in-region undo-elt start end)))))
1505 (if keep-this
1506 (progn
1507 (setq end (+ end (cdr (undo-delta undo-elt))))
1508 ;; Don't put two nils together in the list
1509 (if (not (and (eq (car undo-list) nil)
1510 (eq undo-elt nil)))
1511 (setq undo-list (cons undo-elt undo-list))))
1512 (if (undo-elt-crosses-region undo-elt start end)
1513 (setq undo-list-copy nil)
1514 (setq some-rejected t)
1515 (setq temp-undo-list (cdr undo-list-copy))
1516 (setq delta (undo-delta undo-elt))
1518 (when (/= (cdr delta) 0)
1519 (let ((position (car delta))
1520 (offset (cdr delta)))
1522 ;; Loop down the earlier events adjusting their buffer
1523 ;; positions to reflect the fact that a change to the buffer
1524 ;; isn't being undone. We only need to process those element
1525 ;; types which undo-elt-in-region will return as being in
1526 ;; the region since only those types can ever get into the
1527 ;; output
1529 (while temp-undo-list
1530 (setq undo-elt (car temp-undo-list))
1531 (cond ((integerp undo-elt)
1532 (if (>= undo-elt position)
1533 (setcar temp-undo-list (- undo-elt offset))))
1534 ((atom undo-elt) nil)
1535 ((stringp (car undo-elt))
1536 ;; (TEXT . POSITION)
1537 (let ((text-pos (abs (cdr undo-elt)))
1538 (point-at-end (< (cdr undo-elt) 0 )))
1539 (if (>= text-pos position)
1540 (setcdr undo-elt (* (if point-at-end -1 1)
1541 (- text-pos offset))))))
1542 ((integerp (car undo-elt))
1543 ;; (BEGIN . END)
1544 (when (>= (car undo-elt) position)
1545 (setcar undo-elt (- (car undo-elt) offset))
1546 (setcdr undo-elt (- (cdr undo-elt) offset))))
1547 ((null (car undo-elt))
1548 ;; (nil PROPERTY VALUE BEG . END)
1549 (let ((tail (nthcdr 3 undo-elt)))
1550 (when (>= (car tail) position)
1551 (setcar tail (- (car tail) offset))
1552 (setcdr tail (- (cdr tail) offset))))))
1553 (setq temp-undo-list (cdr temp-undo-list))))))))
1554 (setq undo-list-copy (cdr undo-list-copy)))
1555 (nreverse undo-list)))
1557 (defun undo-elt-in-region (undo-elt start end)
1558 "Determine whether UNDO-ELT falls inside the region START ... END.
1559 If it crosses the edge, we return nil."
1560 (cond ((integerp undo-elt)
1561 (and (>= undo-elt start)
1562 (<= undo-elt end)))
1563 ((eq undo-elt nil)
1565 ((atom undo-elt)
1566 nil)
1567 ((stringp (car undo-elt))
1568 ;; (TEXT . POSITION)
1569 (and (>= (abs (cdr undo-elt)) start)
1570 (< (abs (cdr undo-elt)) end)))
1571 ((and (consp undo-elt) (markerp (car undo-elt)))
1572 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1573 ;; See if MARKER is inside the region.
1574 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1575 (unless alist-elt
1576 (setq alist-elt (cons (car undo-elt)
1577 (marker-position (car undo-elt))))
1578 (setq undo-adjusted-markers
1579 (cons alist-elt undo-adjusted-markers)))
1580 (and (cdr alist-elt)
1581 (>= (cdr alist-elt) start)
1582 (<= (cdr alist-elt) end))))
1583 ((null (car undo-elt))
1584 ;; (nil PROPERTY VALUE BEG . END)
1585 (let ((tail (nthcdr 3 undo-elt)))
1586 (and (>= (car tail) start)
1587 (<= (cdr tail) end))))
1588 ((integerp (car undo-elt))
1589 ;; (BEGIN . END)
1590 (and (>= (car undo-elt) start)
1591 (<= (cdr undo-elt) end)))))
1593 (defun undo-elt-crosses-region (undo-elt start end)
1594 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1595 This assumes we have already decided that UNDO-ELT
1596 is not *inside* the region START...END."
1597 (cond ((atom undo-elt) nil)
1598 ((null (car undo-elt))
1599 ;; (nil PROPERTY VALUE BEG . END)
1600 (let ((tail (nthcdr 3 undo-elt)))
1601 (not (or (< (car tail) end)
1602 (> (cdr tail) start)))))
1603 ((integerp (car undo-elt))
1604 ;; (BEGIN . END)
1605 (not (or (< (car undo-elt) end)
1606 (> (cdr undo-elt) start))))))
1608 ;; Return the first affected buffer position and the delta for an undo element
1609 ;; delta is defined as the change in subsequent buffer positions if we *did*
1610 ;; the undo.
1611 (defun undo-delta (undo-elt)
1612 (if (consp undo-elt)
1613 (cond ((stringp (car undo-elt))
1614 ;; (TEXT . POSITION)
1615 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1616 ((integerp (car undo-elt))
1617 ;; (BEGIN . END)
1618 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1620 '(0 . 0)))
1621 '(0 . 0)))
1623 (defcustom undo-ask-before-discard t
1624 "If non-nil ask about discarding undo info for the current command.
1625 Normally, Emacs discards the undo info for the current command if
1626 it exceeds `undo-outer-limit'. But if you set this option
1627 non-nil, it asks in the echo area whether to discard the info.
1628 If you answer no, there a slight risk that Emacs might crash, so
1629 only do it if you really want to undo the command.
1631 This option is mainly intended for debugging. You have to be
1632 careful if you use it for other purposes. Garbage collection is
1633 inhibited while the question is asked, meaning that Emacs might
1634 leak memory. So you should make sure that you do not wait
1635 excessively long before answering the question."
1636 :type 'boolean
1637 :group 'undo
1638 :version "22.1")
1640 (defvar undo-extra-outer-limit nil
1641 "If non-nil, an extra level of size that's ok in an undo item.
1642 We don't ask the user about truncating the undo list until the
1643 current item gets bigger than this amount.
1645 This variable only matters if `undo-ask-before-discard' is non-nil.")
1646 (make-variable-buffer-local 'undo-extra-outer-limit)
1648 ;; When the first undo batch in an undo list is longer than
1649 ;; undo-outer-limit, this function gets called to warn the user that
1650 ;; the undo info for the current command was discarded. Garbage
1651 ;; collection is inhibited around the call, so it had better not do a
1652 ;; lot of consing.
1653 (setq undo-outer-limit-function 'undo-outer-limit-truncate)
1654 (defun undo-outer-limit-truncate (size)
1655 (if undo-ask-before-discard
1656 (when (or (null undo-extra-outer-limit)
1657 (> size undo-extra-outer-limit))
1658 ;; Don't ask the question again unless it gets even bigger.
1659 ;; This applies, in particular, if the user quits from the question.
1660 ;; Such a quit quits out of GC, but something else will call GC
1661 ;; again momentarily. It will call this function again,
1662 ;; but we don't want to ask the question again.
1663 (setq undo-extra-outer-limit (+ size 50000))
1664 (if (let (use-dialog-box track-mouse executing-kbd-macro )
1665 (yes-or-no-p (format "Buffer %s undo info is %d bytes long; discard it? "
1666 (buffer-name) size)))
1667 (progn (setq buffer-undo-list nil)
1668 (setq undo-extra-outer-limit nil)
1670 nil))
1671 (display-warning '(undo discard-info)
1672 (concat
1673 (format "Buffer %s undo info was %d bytes long.\n"
1674 (buffer-name) size)
1675 "The undo info was discarded because it exceeded \
1676 `undo-outer-limit'.
1678 This is normal if you executed a command that made a huge change
1679 to the buffer. In that case, to prevent similar problems in the
1680 future, set `undo-outer-limit' to a value that is large enough to
1681 cover the maximum size of normal changes you expect a single
1682 command to make, but not so large that it might exceed the
1683 maximum memory allotted to Emacs.
1685 If you did not execute any such command, the situation is
1686 probably due to a bug and you should report it.
1688 You can disable the popping up of this buffer by adding the entry
1689 \(undo discard-info) to the user option `warning-suppress-types'.\n")
1690 :warning)
1691 (setq buffer-undo-list nil)
1694 (defvar shell-command-history nil
1695 "History list for some commands that read shell commands.")
1697 (defvar shell-command-switch "-c"
1698 "Switch used to have the shell execute its command line argument.")
1700 (defvar shell-command-default-error-buffer nil
1701 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1702 This buffer is used when `shell-command' or `shell-command-on-region'
1703 is run interactively. A value of nil means that output to stderr and
1704 stdout will be intermixed in the output stream.")
1706 (defun shell-command (command &optional output-buffer error-buffer)
1707 "Execute string COMMAND in inferior shell; display output, if any.
1708 With prefix argument, insert the COMMAND's output at point.
1710 If COMMAND ends in ampersand, execute it asynchronously.
1711 The output appears in the buffer `*Async Shell Command*'.
1712 That buffer is in shell mode.
1714 Otherwise, COMMAND is executed synchronously. The output appears in
1715 the buffer `*Shell Command Output*'. If the output is short enough to
1716 display in the echo area (which is determined by the variables
1717 `resize-mini-windows' and `max-mini-window-height'), it is shown
1718 there, but it is nonetheless available in buffer `*Shell Command
1719 Output*' even though that buffer is not automatically displayed.
1721 To specify a coding system for converting non-ASCII characters
1722 in the shell command output, use \\[universal-coding-system-argument]
1723 before this command.
1725 Noninteractive callers can specify coding systems by binding
1726 `coding-system-for-read' and `coding-system-for-write'.
1728 The optional second argument OUTPUT-BUFFER, if non-nil,
1729 says to put the output in some other buffer.
1730 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1731 If OUTPUT-BUFFER is not a buffer and not nil,
1732 insert output in current buffer. (This cannot be done asynchronously.)
1733 In either case, the output is inserted after point (leaving mark after it).
1735 If the command terminates without error, but generates output,
1736 and you did not specify \"insert it in the current buffer\",
1737 the output can be displayed in the echo area or in its buffer.
1738 If the output is short enough to display in the echo area
1739 \(determined by the variable `max-mini-window-height' if
1740 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1741 the buffer containing the output is displayed.
1743 If there is output and an error, and you did not specify \"insert it
1744 in the current buffer\", a message about the error goes at the end
1745 of the output.
1747 If there is no output, or if output is inserted in the current buffer,
1748 then `*Shell Command Output*' is deleted.
1750 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1751 or buffer name to which to direct the command's standard error output.
1752 If it is nil, error output is mingled with regular output.
1753 In an interactive call, the variable `shell-command-default-error-buffer'
1754 specifies the value of ERROR-BUFFER."
1756 (interactive (list (read-from-minibuffer "Shell command: "
1757 nil nil nil 'shell-command-history)
1758 current-prefix-arg
1759 shell-command-default-error-buffer))
1760 ;; Look for a handler in case default-directory is a remote file name.
1761 (let ((handler
1762 (find-file-name-handler (directory-file-name default-directory)
1763 'shell-command)))
1764 (if handler
1765 (funcall handler 'shell-command command output-buffer error-buffer)
1766 (if (and output-buffer
1767 (not (or (bufferp output-buffer) (stringp output-buffer))))
1768 ;; Output goes in current buffer.
1769 (let ((error-file
1770 (if error-buffer
1771 (make-temp-file
1772 (expand-file-name "scor"
1773 (or small-temporary-file-directory
1774 temporary-file-directory)))
1775 nil)))
1776 (barf-if-buffer-read-only)
1777 (push-mark nil t)
1778 ;; We do not use -f for csh; we will not support broken use of
1779 ;; .cshrcs. Even the BSD csh manual says to use
1780 ;; "if ($?prompt) exit" before things which are not useful
1781 ;; non-interactively. Besides, if someone wants their other
1782 ;; aliases for shell commands then they can still have them.
1783 (call-process shell-file-name nil
1784 (if error-file
1785 (list t error-file)
1787 nil shell-command-switch command)
1788 (when (and error-file (file-exists-p error-file))
1789 (if (< 0 (nth 7 (file-attributes error-file)))
1790 (with-current-buffer (get-buffer-create error-buffer)
1791 (let ((pos-from-end (- (point-max) (point))))
1792 (or (bobp)
1793 (insert "\f\n"))
1794 ;; Do no formatting while reading error file,
1795 ;; because that can run a shell command, and we
1796 ;; don't want that to cause an infinite recursion.
1797 (format-insert-file error-file nil)
1798 ;; Put point after the inserted errors.
1799 (goto-char (- (point-max) pos-from-end)))
1800 (display-buffer (current-buffer))))
1801 (delete-file error-file))
1802 ;; This is like exchange-point-and-mark, but doesn't
1803 ;; activate the mark. It is cleaner to avoid activation,
1804 ;; even though the command loop would deactivate the mark
1805 ;; because we inserted text.
1806 (goto-char (prog1 (mark t)
1807 (set-marker (mark-marker) (point)
1808 (current-buffer)))))
1809 ;; Output goes in a separate buffer.
1810 ;; Preserve the match data in case called from a program.
1811 (save-match-data
1812 (if (string-match "[ \t]*&[ \t]*\\'" command)
1813 ;; Command ending with ampersand means asynchronous.
1814 (let ((buffer (get-buffer-create
1815 (or output-buffer "*Async Shell Command*")))
1816 (directory default-directory)
1817 proc)
1818 ;; Remove the ampersand.
1819 (setq command (substring command 0 (match-beginning 0)))
1820 ;; If will kill a process, query first.
1821 (setq proc (get-buffer-process buffer))
1822 (if proc
1823 (if (yes-or-no-p "A command is running. Kill it? ")
1824 (kill-process proc)
1825 (error "Shell command in progress")))
1826 (with-current-buffer buffer
1827 (setq buffer-read-only nil)
1828 (erase-buffer)
1829 (display-buffer buffer)
1830 (setq default-directory directory)
1831 (setq proc (start-process "Shell" buffer shell-file-name
1832 shell-command-switch command))
1833 (setq mode-line-process '(":%s"))
1834 (require 'shell) (shell-mode)
1835 (set-process-sentinel proc 'shell-command-sentinel)
1837 (shell-command-on-region (point) (point) command
1838 output-buffer nil error-buffer)))))))
1840 (defun display-message-or-buffer (message
1841 &optional buffer-name not-this-window frame)
1842 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1843 MESSAGE may be either a string or a buffer.
1845 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1846 the maximum height of the echo area, as defined by `max-mini-window-height'
1847 if `resize-mini-windows' is non-nil.
1849 Returns either the string shown in the echo area, or when a pop-up
1850 buffer is used, the window used to display it.
1852 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1853 name of the buffer used to display it in the case where a pop-up buffer
1854 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1855 string and it is displayed in the echo area, it is not specified whether
1856 the contents are inserted into the buffer anyway.
1858 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1859 and only used if a buffer is displayed."
1860 (cond ((and (stringp message) (not (string-match "\n" message)))
1861 ;; Trivial case where we can use the echo area
1862 (message "%s" message))
1863 ((and (stringp message)
1864 (= (string-match "\n" message) (1- (length message))))
1865 ;; Trivial case where we can just remove single trailing newline
1866 (message "%s" (substring message 0 (1- (length message)))))
1868 ;; General case
1869 (with-current-buffer
1870 (if (bufferp message)
1871 message
1872 (get-buffer-create (or buffer-name "*Message*")))
1874 (unless (bufferp message)
1875 (erase-buffer)
1876 (insert message))
1878 (let ((lines
1879 (if (= (buffer-size) 0)
1881 (count-lines (point-min) (point-max)))))
1882 (cond ((= lines 0))
1883 ((and (or (<= lines 1)
1884 (<= lines
1885 (if resize-mini-windows
1886 (cond ((floatp max-mini-window-height)
1887 (* (frame-height)
1888 max-mini-window-height))
1889 ((integerp max-mini-window-height)
1890 max-mini-window-height)
1893 1)))
1894 ;; Don't use the echo area if the output buffer is
1895 ;; already dispayed in the selected frame.
1896 (not (get-buffer-window (current-buffer))))
1897 ;; Echo area
1898 (goto-char (point-max))
1899 (when (bolp)
1900 (backward-char 1))
1901 (message "%s" (buffer-substring (point-min) (point))))
1903 ;; Buffer
1904 (goto-char (point-min))
1905 (display-buffer (current-buffer)
1906 not-this-window frame))))))))
1909 ;; We have a sentinel to prevent insertion of a termination message
1910 ;; in the buffer itself.
1911 (defun shell-command-sentinel (process signal)
1912 (if (memq (process-status process) '(exit signal))
1913 (message "%s: %s."
1914 (car (cdr (cdr (process-command process))))
1915 (substring signal 0 -1))))
1917 (defun shell-command-on-region (start end command
1918 &optional output-buffer replace
1919 error-buffer display-error-buffer)
1920 "Execute string COMMAND in inferior shell with region as input.
1921 Normally display output (if any) in temp buffer `*Shell Command Output*';
1922 Prefix arg means replace the region with it. Return the exit code of
1923 COMMAND.
1925 To specify a coding system for converting non-ASCII characters
1926 in the input and output to the shell command, use \\[universal-coding-system-argument]
1927 before this command. By default, the input (from the current buffer)
1928 is encoded in the same coding system that will be used to save the file,
1929 `buffer-file-coding-system'. If the output is going to replace the region,
1930 then it is decoded from that same coding system.
1932 The noninteractive arguments are START, END, COMMAND,
1933 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
1934 Noninteractive callers can specify coding systems by binding
1935 `coding-system-for-read' and `coding-system-for-write'.
1937 If the command generates output, the output may be displayed
1938 in the echo area or in a buffer.
1939 If the output is short enough to display in the echo area
1940 \(determined by the variable `max-mini-window-height' if
1941 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1942 it is displayed in the buffer `*Shell Command Output*'. The output
1943 is available in that buffer in both cases.
1945 If there is output and an error, a message about the error
1946 appears at the end of the output.
1948 If there is no output, or if output is inserted in the current buffer,
1949 then `*Shell Command Output*' is deleted.
1951 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1952 that says to put the output in some other buffer.
1953 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1954 If OUTPUT-BUFFER is not a buffer and not nil,
1955 insert output in the current buffer.
1956 In either case, the output is inserted after point (leaving mark after it).
1958 If REPLACE, the optional fifth argument, is non-nil, that means insert
1959 the output in place of text from START to END, putting point and mark
1960 around it.
1962 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1963 or buffer name to which to direct the command's standard error output.
1964 If it is nil, error output is mingled with regular output.
1965 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
1966 were any errors. (This is always t, interactively.)
1967 In an interactive call, the variable `shell-command-default-error-buffer'
1968 specifies the value of ERROR-BUFFER."
1969 (interactive (let (string)
1970 (unless (mark)
1971 (error "The mark is not set now, so there is no region"))
1972 ;; Do this before calling region-beginning
1973 ;; and region-end, in case subprocess output
1974 ;; relocates them while we are in the minibuffer.
1975 (setq string (read-from-minibuffer "Shell command on region: "
1976 nil nil nil
1977 'shell-command-history))
1978 ;; call-interactively recognizes region-beginning and
1979 ;; region-end specially, leaving them in the history.
1980 (list (region-beginning) (region-end)
1981 string
1982 current-prefix-arg
1983 current-prefix-arg
1984 shell-command-default-error-buffer
1985 t)))
1986 (let ((error-file
1987 (if error-buffer
1988 (make-temp-file
1989 (expand-file-name "scor"
1990 (or small-temporary-file-directory
1991 temporary-file-directory)))
1992 nil))
1993 exit-status)
1994 (if (or replace
1995 (and output-buffer
1996 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1997 ;; Replace specified region with output from command.
1998 (let ((swap (and replace (< start end))))
1999 ;; Don't muck with mark unless REPLACE says we should.
2000 (goto-char start)
2001 (and replace (push-mark (point) 'nomsg))
2002 (setq exit-status
2003 (call-process-region start end shell-file-name t
2004 (if error-file
2005 (list t error-file)
2007 nil shell-command-switch command))
2008 ;; It is rude to delete a buffer which the command is not using.
2009 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
2010 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
2011 ;; (kill-buffer shell-buffer)))
2012 ;; Don't muck with mark unless REPLACE says we should.
2013 (and replace swap (exchange-point-and-mark)))
2014 ;; No prefix argument: put the output in a temp buffer,
2015 ;; replacing its entire contents.
2016 (let ((buffer (get-buffer-create
2017 (or output-buffer "*Shell Command Output*"))))
2018 (unwind-protect
2019 (if (eq buffer (current-buffer))
2020 ;; If the input is the same buffer as the output,
2021 ;; delete everything but the specified region,
2022 ;; then replace that region with the output.
2023 (progn (setq buffer-read-only nil)
2024 (delete-region (max start end) (point-max))
2025 (delete-region (point-min) (min start end))
2026 (setq exit-status
2027 (call-process-region (point-min) (point-max)
2028 shell-file-name t
2029 (if error-file
2030 (list t error-file)
2032 nil shell-command-switch
2033 command)))
2034 ;; Clear the output buffer, then run the command with
2035 ;; output there.
2036 (let ((directory default-directory))
2037 (save-excursion
2038 (set-buffer buffer)
2039 (setq buffer-read-only nil)
2040 (if (not output-buffer)
2041 (setq default-directory directory))
2042 (erase-buffer)))
2043 (setq exit-status
2044 (call-process-region start end shell-file-name nil
2045 (if error-file
2046 (list buffer error-file)
2047 buffer)
2048 nil shell-command-switch command)))
2049 ;; Report the output.
2050 (with-current-buffer buffer
2051 (setq mode-line-process
2052 (cond ((null exit-status)
2053 " - Error")
2054 ((stringp exit-status)
2055 (format " - Signal [%s]" exit-status))
2056 ((not (equal 0 exit-status))
2057 (format " - Exit [%d]" exit-status)))))
2058 (if (with-current-buffer buffer (> (point-max) (point-min)))
2059 ;; There's some output, display it
2060 (display-message-or-buffer buffer)
2061 ;; No output; error?
2062 (let ((output
2063 (if (and error-file
2064 (< 0 (nth 7 (file-attributes error-file))))
2065 "some error output"
2066 "no output")))
2067 (cond ((null exit-status)
2068 (message "(Shell command failed with error)"))
2069 ((equal 0 exit-status)
2070 (message "(Shell command succeeded with %s)"
2071 output))
2072 ((stringp exit-status)
2073 (message "(Shell command killed by signal %s)"
2074 exit-status))
2076 (message "(Shell command failed with code %d and %s)"
2077 exit-status output))))
2078 ;; Don't kill: there might be useful info in the undo-log.
2079 ;; (kill-buffer buffer)
2080 ))))
2082 (when (and error-file (file-exists-p error-file))
2083 (if (< 0 (nth 7 (file-attributes error-file)))
2084 (with-current-buffer (get-buffer-create error-buffer)
2085 (let ((pos-from-end (- (point-max) (point))))
2086 (or (bobp)
2087 (insert "\f\n"))
2088 ;; Do no formatting while reading error file,
2089 ;; because that can run a shell command, and we
2090 ;; don't want that to cause an infinite recursion.
2091 (format-insert-file error-file nil)
2092 ;; Put point after the inserted errors.
2093 (goto-char (- (point-max) pos-from-end)))
2094 (and display-error-buffer
2095 (display-buffer (current-buffer)))))
2096 (delete-file error-file))
2097 exit-status))
2099 (defun shell-command-to-string (command)
2100 "Execute shell command COMMAND and return its output as a string."
2101 (with-output-to-string
2102 (with-current-buffer
2103 standard-output
2104 (call-process shell-file-name nil t nil shell-command-switch command))))
2106 (defun process-file (program &optional infile buffer display &rest args)
2107 "Process files synchronously in a separate process.
2108 Similar to `call-process', but may invoke a file handler based on
2109 `default-directory'. The current working directory of the
2110 subprocess is `default-directory'.
2112 File names in INFILE and BUFFER are handled normally, but file
2113 names in ARGS should be relative to `default-directory', as they
2114 are passed to the process verbatim. \(This is a difference to
2115 `call-process' which does not support file handlers for INFILE
2116 and BUFFER.\)
2118 Some file handlers might not support all variants, for example
2119 they might behave as if DISPLAY was nil, regardless of the actual
2120 value passed."
2121 (let ((fh (find-file-name-handler default-directory 'process-file))
2122 lc stderr-file)
2123 (unwind-protect
2124 (if fh (apply fh 'process-file program infile buffer display args)
2125 (when infile (setq lc (file-local-copy infile)))
2126 (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer)))
2127 (make-temp-file "emacs")))
2128 (prog1
2129 (apply 'call-process program
2130 (or lc infile)
2131 (if stderr-file (list (car buffer) stderr-file) buffer)
2132 display args)
2133 (when stderr-file (copy-file stderr-file (cadr buffer)))))
2134 (when stderr-file (delete-file stderr-file))
2135 (when lc (delete-file lc)))))
2139 (defvar universal-argument-map
2140 (let ((map (make-sparse-keymap)))
2141 (define-key map [t] 'universal-argument-other-key)
2142 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
2143 (define-key map [switch-frame] nil)
2144 (define-key map [?\C-u] 'universal-argument-more)
2145 (define-key map [?-] 'universal-argument-minus)
2146 (define-key map [?0] 'digit-argument)
2147 (define-key map [?1] 'digit-argument)
2148 (define-key map [?2] 'digit-argument)
2149 (define-key map [?3] 'digit-argument)
2150 (define-key map [?4] 'digit-argument)
2151 (define-key map [?5] 'digit-argument)
2152 (define-key map [?6] 'digit-argument)
2153 (define-key map [?7] 'digit-argument)
2154 (define-key map [?8] 'digit-argument)
2155 (define-key map [?9] 'digit-argument)
2156 (define-key map [kp-0] 'digit-argument)
2157 (define-key map [kp-1] 'digit-argument)
2158 (define-key map [kp-2] 'digit-argument)
2159 (define-key map [kp-3] 'digit-argument)
2160 (define-key map [kp-4] 'digit-argument)
2161 (define-key map [kp-5] 'digit-argument)
2162 (define-key map [kp-6] 'digit-argument)
2163 (define-key map [kp-7] 'digit-argument)
2164 (define-key map [kp-8] 'digit-argument)
2165 (define-key map [kp-9] 'digit-argument)
2166 (define-key map [kp-subtract] 'universal-argument-minus)
2167 map)
2168 "Keymap used while processing \\[universal-argument].")
2170 (defvar universal-argument-num-events nil
2171 "Number of argument-specifying events read by `universal-argument'.
2172 `universal-argument-other-key' uses this to discard those events
2173 from (this-command-keys), and reread only the final command.")
2175 (defvar overriding-map-is-bound nil
2176 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
2178 (defvar saved-overriding-map nil
2179 "The saved value of `overriding-terminal-local-map'.
2180 That variable gets restored to this value on exiting \"universal
2181 argument mode\".")
2183 (defun ensure-overriding-map-is-bound ()
2184 "Check `overriding-terminal-local-map' is `universal-argument-map'."
2185 (unless overriding-map-is-bound
2186 (setq saved-overriding-map overriding-terminal-local-map)
2187 (setq overriding-terminal-local-map universal-argument-map)
2188 (setq overriding-map-is-bound t)))
2190 (defun restore-overriding-map ()
2191 "Restore `overriding-terminal-local-map' to its saved value."
2192 (setq overriding-terminal-local-map saved-overriding-map)
2193 (setq overriding-map-is-bound nil))
2195 (defun universal-argument ()
2196 "Begin a numeric argument for the following command.
2197 Digits or minus sign following \\[universal-argument] make up the numeric argument.
2198 \\[universal-argument] following the digits or minus sign ends the argument.
2199 \\[universal-argument] without digits or minus sign provides 4 as argument.
2200 Repeating \\[universal-argument] without digits or minus sign
2201 multiplies the argument by 4 each time.
2202 For some commands, just \\[universal-argument] by itself serves as a flag
2203 which is different in effect from any particular numeric argument.
2204 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
2205 (interactive)
2206 (setq prefix-arg (list 4))
2207 (setq universal-argument-num-events (length (this-command-keys)))
2208 (ensure-overriding-map-is-bound))
2210 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
2211 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
2212 (defun universal-argument-more (arg)
2213 (interactive "P")
2214 (if (consp arg)
2215 (setq prefix-arg (list (* 4 (car arg))))
2216 (if (eq arg '-)
2217 (setq prefix-arg (list -4))
2218 (setq prefix-arg arg)
2219 (restore-overriding-map)))
2220 (setq universal-argument-num-events (length (this-command-keys))))
2222 (defun negative-argument (arg)
2223 "Begin a negative numeric argument for the next command.
2224 \\[universal-argument] following digits or minus sign ends the argument."
2225 (interactive "P")
2226 (cond ((integerp arg)
2227 (setq prefix-arg (- arg)))
2228 ((eq arg '-)
2229 (setq prefix-arg nil))
2231 (setq prefix-arg '-)))
2232 (setq universal-argument-num-events (length (this-command-keys)))
2233 (ensure-overriding-map-is-bound))
2235 (defun digit-argument (arg)
2236 "Part of the numeric argument for the next command.
2237 \\[universal-argument] following digits or minus sign ends the argument."
2238 (interactive "P")
2239 (let* ((char (if (integerp last-command-char)
2240 last-command-char
2241 (get last-command-char 'ascii-character)))
2242 (digit (- (logand char ?\177) ?0)))
2243 (cond ((integerp arg)
2244 (setq prefix-arg (+ (* arg 10)
2245 (if (< arg 0) (- digit) digit))))
2246 ((eq arg '-)
2247 ;; Treat -0 as just -, so that -01 will work.
2248 (setq prefix-arg (if (zerop digit) '- (- digit))))
2250 (setq prefix-arg digit))))
2251 (setq universal-argument-num-events (length (this-command-keys)))
2252 (ensure-overriding-map-is-bound))
2254 ;; For backward compatibility, minus with no modifiers is an ordinary
2255 ;; command if digits have already been entered.
2256 (defun universal-argument-minus (arg)
2257 (interactive "P")
2258 (if (integerp arg)
2259 (universal-argument-other-key arg)
2260 (negative-argument arg)))
2262 ;; Anything else terminates the argument and is left in the queue to be
2263 ;; executed as a command.
2264 (defun universal-argument-other-key (arg)
2265 (interactive "P")
2266 (setq prefix-arg arg)
2267 (let* ((key (this-command-keys))
2268 (keylist (listify-key-sequence key)))
2269 (setq unread-command-events
2270 (append (nthcdr universal-argument-num-events keylist)
2271 unread-command-events)))
2272 (reset-this-command-lengths)
2273 (restore-overriding-map))
2275 (defvar buffer-substring-filters nil
2276 "List of filter functions for `filter-buffer-substring'.
2277 Each function must accept a single argument, a string, and return
2278 a string. The buffer substring is passed to the first function
2279 in the list, and the return value of each function is passed to
2280 the next. The return value of the last function is used as the
2281 return value of `filter-buffer-substring'.
2283 If this variable is nil, no filtering is performed.")
2285 (defun filter-buffer-substring (beg end &optional delete)
2286 "Return the buffer substring between BEG and END, after filtering.
2287 The buffer substring is passed through each of the filter
2288 functions in `buffer-substring-filters', and the value from the
2289 last filter function is returned. If `buffer-substring-filters'
2290 is nil, the buffer substring is returned unaltered.
2292 If DELETE is non-nil, the text between BEG and END is deleted
2293 from the buffer.
2295 Point is temporarily set to BEG before calling
2296 `buffer-substring-filters', in case the functions need to know
2297 where the text came from.
2299 This function should be used instead of `buffer-substring' or
2300 `delete-and-extract-region' when you want to allow filtering to
2301 take place. For example, major or minor modes can use
2302 `buffer-substring-filters' to extract characters that are special
2303 to a buffer, and should not be copied into other buffers."
2304 (save-excursion
2305 (goto-char beg)
2306 (let ((string (if delete (delete-and-extract-region beg end)
2307 (buffer-substring beg end))))
2308 (dolist (filter buffer-substring-filters string)
2309 (setq string (funcall filter string))))))
2311 ;;;; Window system cut and paste hooks.
2313 (defvar interprogram-cut-function nil
2314 "Function to call to make a killed region available to other programs.
2316 Most window systems provide some sort of facility for cutting and
2317 pasting text between the windows of different programs.
2318 This variable holds a function that Emacs calls whenever text
2319 is put in the kill ring, to make the new kill available to other
2320 programs.
2322 The function takes one or two arguments.
2323 The first argument, TEXT, is a string containing
2324 the text which should be made available.
2325 The second, optional, argument PUSH, has the same meaning as the
2326 similar argument to `x-set-cut-buffer', which see.")
2328 (defvar interprogram-paste-function nil
2329 "Function to call to get text cut from other programs.
2331 Most window systems provide some sort of facility for cutting and
2332 pasting text between the windows of different programs.
2333 This variable holds a function that Emacs calls to obtain
2334 text that other programs have provided for pasting.
2336 The function should be called with no arguments. If the function
2337 returns nil, then no other program has provided such text, and the top
2338 of the Emacs kill ring should be used. If the function returns a
2339 string, then the caller of the function \(usually `current-kill')
2340 should put this string in the kill ring as the latest kill.
2342 Note that the function should return a string only if a program other
2343 than Emacs has provided a string for pasting; if Emacs provided the
2344 most recent string, the function should return nil. If it is
2345 difficult to tell whether Emacs or some other program provided the
2346 current string, it is probably good enough to return nil if the string
2347 is equal (according to `string=') to the last text Emacs provided.")
2351 ;;;; The kill ring data structure.
2353 (defvar kill-ring nil
2354 "List of killed text sequences.
2355 Since the kill ring is supposed to interact nicely with cut-and-paste
2356 facilities offered by window systems, use of this variable should
2357 interact nicely with `interprogram-cut-function' and
2358 `interprogram-paste-function'. The functions `kill-new',
2359 `kill-append', and `current-kill' are supposed to implement this
2360 interaction; you may want to use them instead of manipulating the kill
2361 ring directly.")
2363 (defcustom kill-ring-max 60
2364 "*Maximum length of kill ring before oldest elements are thrown away."
2365 :type 'integer
2366 :group 'killing)
2368 (defvar kill-ring-yank-pointer nil
2369 "The tail of the kill ring whose car is the last thing yanked.")
2371 (defun kill-new (string &optional replace yank-handler)
2372 "Make STRING the latest kill in the kill ring.
2373 Set `kill-ring-yank-pointer' to point to it.
2374 If `interprogram-cut-function' is non-nil, apply it to STRING.
2375 Optional second argument REPLACE non-nil means that STRING will replace
2376 the front of the kill ring, rather than being added to the list.
2378 Optional third arguments YANK-HANDLER controls how the STRING is later
2379 inserted into a buffer; see `insert-for-yank' for details.
2380 When a yank handler is specified, STRING must be non-empty (the yank
2381 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2383 When the yank handler has a non-nil PARAM element, the original STRING
2384 argument is not used by `insert-for-yank'. However, since Lisp code
2385 may access and use elements from the kill ring directly, the STRING
2386 argument should still be a \"useful\" string for such uses."
2387 (if (> (length string) 0)
2388 (if yank-handler
2389 (put-text-property 0 (length string)
2390 'yank-handler yank-handler string))
2391 (if yank-handler
2392 (signal 'args-out-of-range
2393 (list string "yank-handler specified for empty string"))))
2394 (if (fboundp 'menu-bar-update-yank-menu)
2395 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2396 (if (and replace kill-ring)
2397 (setcar kill-ring string)
2398 (push string kill-ring)
2399 (if (> (length kill-ring) kill-ring-max)
2400 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2401 (setq kill-ring-yank-pointer kill-ring)
2402 (if interprogram-cut-function
2403 (funcall interprogram-cut-function string (not replace))))
2405 (defun kill-append (string before-p &optional yank-handler)
2406 "Append STRING to the end of the latest kill in the kill ring.
2407 If BEFORE-P is non-nil, prepend STRING to the kill.
2408 Optional third argument YANK-HANDLER, if non-nil, specifies the
2409 yank-handler text property to be set on the combined kill ring
2410 string. If the specified yank-handler arg differs from the
2411 yank-handler property of the latest kill string, this function
2412 adds the combined string to the kill ring as a new element,
2413 instead of replacing the last kill with it.
2414 If `interprogram-cut-function' is set, pass the resulting kill to it."
2415 (let* ((cur (car kill-ring)))
2416 (kill-new (if before-p (concat string cur) (concat cur string))
2417 (or (= (length cur) 0)
2418 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2419 yank-handler)))
2421 (defun current-kill (n &optional do-not-move)
2422 "Rotate the yanking point by N places, and then return that kill.
2423 If N is zero, `interprogram-paste-function' is set, and calling it
2424 returns a string, then that string is added to the front of the
2425 kill ring and returned as the latest kill.
2426 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
2427 yanking point; just return the Nth kill forward."
2428 (let ((interprogram-paste (and (= n 0)
2429 interprogram-paste-function
2430 (funcall interprogram-paste-function))))
2431 (if interprogram-paste
2432 (progn
2433 ;; Disable the interprogram cut function when we add the new
2434 ;; text to the kill ring, so Emacs doesn't try to own the
2435 ;; selection, with identical text.
2436 (let ((interprogram-cut-function nil))
2437 (kill-new interprogram-paste))
2438 interprogram-paste)
2439 (or kill-ring (error "Kill ring is empty"))
2440 (let ((ARGth-kill-element
2441 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2442 (length kill-ring))
2443 kill-ring)))
2444 (or do-not-move
2445 (setq kill-ring-yank-pointer ARGth-kill-element))
2446 (car ARGth-kill-element)))))
2450 ;;;; Commands for manipulating the kill ring.
2452 (defcustom kill-read-only-ok nil
2453 "*Non-nil means don't signal an error for killing read-only text."
2454 :type 'boolean
2455 :group 'killing)
2457 (put 'text-read-only 'error-conditions
2458 '(text-read-only buffer-read-only error))
2459 (put 'text-read-only 'error-message "Text is read-only")
2461 (defun kill-region (beg end &optional yank-handler)
2462 "Kill between point and mark.
2463 The text is deleted but saved in the kill ring.
2464 The command \\[yank] can retrieve it from there.
2465 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2467 If you want to append the killed region to the last killed text,
2468 use \\[append-next-kill] before \\[kill-region].
2470 If the buffer is read-only, Emacs will beep and refrain from deleting
2471 the text, but put the text in the kill ring anyway. This means that
2472 you can use the killing commands to copy text from a read-only buffer.
2474 This is the primitive for programs to kill text (as opposed to deleting it).
2475 Supply two arguments, character positions indicating the stretch of text
2476 to be killed.
2477 Any command that calls this function is a \"kill command\".
2478 If the previous command was also a kill command,
2479 the text killed this time appends to the text killed last time
2480 to make one entry in the kill ring.
2482 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2483 specifies the yank-handler text property to be set on the killed
2484 text. See `insert-for-yank'."
2485 (interactive "r")
2486 (condition-case nil
2487 (let ((string (filter-buffer-substring beg end t)))
2488 (when string ;STRING is nil if BEG = END
2489 ;; Add that string to the kill ring, one way or another.
2490 (if (eq last-command 'kill-region)
2491 (kill-append string (< end beg) yank-handler)
2492 (kill-new string nil yank-handler)))
2493 (when (or string (eq last-command 'kill-region))
2494 (setq this-command 'kill-region))
2495 nil)
2496 ((buffer-read-only text-read-only)
2497 ;; The code above failed because the buffer, or some of the characters
2498 ;; in the region, are read-only.
2499 ;; We should beep, in case the user just isn't aware of this.
2500 ;; However, there's no harm in putting
2501 ;; the region's text in the kill ring, anyway.
2502 (copy-region-as-kill beg end)
2503 ;; Set this-command now, so it will be set even if we get an error.
2504 (setq this-command 'kill-region)
2505 ;; This should barf, if appropriate, and give us the correct error.
2506 (if kill-read-only-ok
2507 (progn (message "Read only text copied to kill ring") nil)
2508 ;; Signal an error if the buffer is read-only.
2509 (barf-if-buffer-read-only)
2510 ;; If the buffer isn't read-only, the text is.
2511 (signal 'text-read-only (list (current-buffer)))))))
2513 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2514 ;; to get two copies of the text when the user accidentally types M-w and
2515 ;; then corrects it with the intended C-w.
2516 (defun copy-region-as-kill (beg end)
2517 "Save the region as if killed, but don't kill it.
2518 In Transient Mark mode, deactivate the mark.
2519 If `interprogram-cut-function' is non-nil, also save the text for a window
2520 system cut and paste."
2521 (interactive "r")
2522 (if (eq last-command 'kill-region)
2523 (kill-append (filter-buffer-substring beg end) (< end beg))
2524 (kill-new (filter-buffer-substring beg end)))
2525 (if transient-mark-mode
2526 (setq deactivate-mark t))
2527 nil)
2529 (defun kill-ring-save (beg end)
2530 "Save the region as if killed, but don't kill it.
2531 In Transient Mark mode, deactivate the mark.
2532 If `interprogram-cut-function' is non-nil, also save the text for a window
2533 system cut and paste.
2535 If you want to append the killed line to the last killed text,
2536 use \\[append-next-kill] before \\[kill-ring-save].
2538 This command is similar to `copy-region-as-kill', except that it gives
2539 visual feedback indicating the extent of the region being copied."
2540 (interactive "r")
2541 (copy-region-as-kill beg end)
2542 ;; This use of interactive-p is correct
2543 ;; because the code it controls just gives the user visual feedback.
2544 (if (interactive-p)
2545 (let ((other-end (if (= (point) beg) end beg))
2546 (opoint (point))
2547 ;; Inhibit quitting so we can make a quit here
2548 ;; look like a C-g typed as a command.
2549 (inhibit-quit t))
2550 (if (pos-visible-in-window-p other-end (selected-window))
2551 (unless (and transient-mark-mode
2552 (face-background 'region))
2553 ;; Swap point and mark.
2554 (set-marker (mark-marker) (point) (current-buffer))
2555 (goto-char other-end)
2556 (sit-for blink-matching-delay)
2557 ;; Swap back.
2558 (set-marker (mark-marker) other-end (current-buffer))
2559 (goto-char opoint)
2560 ;; If user quit, deactivate the mark
2561 ;; as C-g would as a command.
2562 (and quit-flag mark-active
2563 (deactivate-mark)))
2564 (let* ((killed-text (current-kill 0))
2565 (message-len (min (length killed-text) 40)))
2566 (if (= (point) beg)
2567 ;; Don't say "killed"; that is misleading.
2568 (message "Saved text until \"%s\""
2569 (substring killed-text (- message-len)))
2570 (message "Saved text from \"%s\""
2571 (substring killed-text 0 message-len))))))))
2573 (defun append-next-kill (&optional interactive)
2574 "Cause following command, if it kills, to append to previous kill.
2575 The argument is used for internal purposes; do not supply one."
2576 (interactive "p")
2577 ;; We don't use (interactive-p), since that breaks kbd macros.
2578 (if interactive
2579 (progn
2580 (setq this-command 'kill-region)
2581 (message "If the next command is a kill, it will append"))
2582 (setq last-command 'kill-region)))
2584 ;; Yanking.
2586 ;; This is actually used in subr.el but defcustom does not work there.
2587 (defcustom yank-excluded-properties
2588 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2589 yank-handler follow-link)
2590 "*Text properties to discard when yanking.
2591 The value should be a list of text properties to discard or t,
2592 which means to discard all text properties."
2593 :type '(choice (const :tag "All" t) (repeat symbol))
2594 :group 'killing
2595 :version "22.1")
2597 (defvar yank-window-start nil)
2598 (defvar yank-undo-function nil
2599 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2600 Function is called with two parameters, START and END corresponding to
2601 the value of the mark and point; it is guaranteed that START <= END.
2602 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2604 (defun yank-pop (&optional arg)
2605 "Replace just-yanked stretch of killed text with a different stretch.
2606 This command is allowed only immediately after a `yank' or a `yank-pop'.
2607 At such a time, the region contains a stretch of reinserted
2608 previously-killed text. `yank-pop' deletes that text and inserts in its
2609 place a different stretch of killed text.
2611 With no argument, the previous kill is inserted.
2612 With argument N, insert the Nth previous kill.
2613 If N is negative, this is a more recent kill.
2615 The sequence of kills wraps around, so that after the oldest one
2616 comes the newest one.
2618 When this command inserts killed text into the buffer, it honors
2619 `yank-excluded-properties' and `yank-handler' as described in the
2620 doc string for `insert-for-yank-1', which see."
2621 (interactive "*p")
2622 (if (not (eq last-command 'yank))
2623 (error "Previous command was not a yank"))
2624 (setq this-command 'yank)
2625 (unless arg (setq arg 1))
2626 (let ((inhibit-read-only t)
2627 (before (< (point) (mark t))))
2628 (if before
2629 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2630 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2631 (setq yank-undo-function nil)
2632 (set-marker (mark-marker) (point) (current-buffer))
2633 (insert-for-yank (current-kill arg))
2634 ;; Set the window start back where it was in the yank command,
2635 ;; if possible.
2636 (set-window-start (selected-window) yank-window-start t)
2637 (if before
2638 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2639 ;; It is cleaner to avoid activation, even though the command
2640 ;; loop would deactivate the mark because we inserted text.
2641 (goto-char (prog1 (mark t)
2642 (set-marker (mark-marker) (point) (current-buffer))))))
2643 nil)
2645 (defun yank (&optional arg)
2646 "Reinsert the last stretch of killed text.
2647 More precisely, reinsert the stretch of killed text most recently
2648 killed OR yanked. Put point at end, and set mark at beginning.
2649 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2650 With argument N, reinsert the Nth most recently killed stretch of killed
2651 text.
2653 When this command inserts killed text into the buffer, it honors
2654 `yank-excluded-properties' and `yank-handler' as described in the
2655 doc string for `insert-for-yank-1', which see.
2657 See also the command \\[yank-pop]."
2658 (interactive "*P")
2659 (setq yank-window-start (window-start))
2660 ;; If we don't get all the way thru, make last-command indicate that
2661 ;; for the following command.
2662 (setq this-command t)
2663 (push-mark (point))
2664 (insert-for-yank (current-kill (cond
2665 ((listp arg) 0)
2666 ((eq arg '-) -2)
2667 (t (1- arg)))))
2668 (if (consp arg)
2669 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2670 ;; It is cleaner to avoid activation, even though the command
2671 ;; loop would deactivate the mark because we inserted text.
2672 (goto-char (prog1 (mark t)
2673 (set-marker (mark-marker) (point) (current-buffer)))))
2674 ;; If we do get all the way thru, make this-command indicate that.
2675 (if (eq this-command t)
2676 (setq this-command 'yank))
2677 nil)
2679 (defun rotate-yank-pointer (arg)
2680 "Rotate the yanking point in the kill ring.
2681 With argument, rotate that many kills forward (or backward, if negative)."
2682 (interactive "p")
2683 (current-kill arg))
2685 ;; Some kill commands.
2687 ;; Internal subroutine of delete-char
2688 (defun kill-forward-chars (arg)
2689 (if (listp arg) (setq arg (car arg)))
2690 (if (eq arg '-) (setq arg -1))
2691 (kill-region (point) (forward-point arg)))
2693 ;; Internal subroutine of backward-delete-char
2694 (defun kill-backward-chars (arg)
2695 (if (listp arg) (setq arg (car arg)))
2696 (if (eq arg '-) (setq arg -1))
2697 (kill-region (point) (forward-point (- arg))))
2699 (defcustom backward-delete-char-untabify-method 'untabify
2700 "*The method for untabifying when deleting backward.
2701 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2702 `hungry' -- delete all whitespace, both tabs and spaces;
2703 `all' -- delete all whitespace, including tabs, spaces and newlines;
2704 nil -- just delete one character."
2705 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2706 :version "20.3"
2707 :group 'killing)
2709 (defun backward-delete-char-untabify (arg &optional killp)
2710 "Delete characters backward, changing tabs into spaces.
2711 The exact behavior depends on `backward-delete-char-untabify-method'.
2712 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2713 Interactively, ARG is the prefix arg (default 1)
2714 and KILLP is t if a prefix arg was specified."
2715 (interactive "*p\nP")
2716 (when (eq backward-delete-char-untabify-method 'untabify)
2717 (let ((count arg))
2718 (save-excursion
2719 (while (and (> count 0) (not (bobp)))
2720 (if (= (preceding-char) ?\t)
2721 (let ((col (current-column)))
2722 (forward-char -1)
2723 (setq col (- col (current-column)))
2724 (insert-char ?\s col)
2725 (delete-char 1)))
2726 (forward-char -1)
2727 (setq count (1- count))))))
2728 (delete-backward-char
2729 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2730 ((eq backward-delete-char-untabify-method 'all)
2731 " \t\n\r"))))
2732 (if skip
2733 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2734 (point)))))
2735 (+ arg (if (zerop wh) 0 (1- wh))))
2736 arg))
2737 killp))
2739 (defun zap-to-char (arg char)
2740 "Kill up to and including ARG'th occurrence of CHAR.
2741 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2742 Goes backward if ARG is negative; error if CHAR not found."
2743 (interactive "p\ncZap to char: ")
2744 (kill-region (point) (progn
2745 (search-forward (char-to-string char) nil nil arg)
2746 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2747 (point))))
2749 ;; kill-line and its subroutines.
2751 (defcustom kill-whole-line nil
2752 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2753 :type 'boolean
2754 :group 'killing)
2756 (defun kill-line (&optional arg)
2757 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2758 With prefix argument, kill that many lines from point.
2759 Negative arguments kill lines backward.
2760 With zero argument, kills the text before point on the current line.
2762 When calling from a program, nil means \"no arg\",
2763 a number counts as a prefix arg.
2765 To kill a whole line, when point is not at the beginning, type \
2766 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2768 If `kill-whole-line' is non-nil, then this command kills the whole line
2769 including its terminating newline, when used at the beginning of a line
2770 with no argument. As a consequence, you can always kill a whole line
2771 by typing \\[beginning-of-line] \\[kill-line].
2773 If you want to append the killed line to the last killed text,
2774 use \\[append-next-kill] before \\[kill-line].
2776 If the buffer is read-only, Emacs will beep and refrain from deleting
2777 the line, but put the line in the kill ring anyway. This means that
2778 you can use this command to copy text from a read-only buffer.
2779 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2780 even beep.)"
2781 (interactive "P")
2782 (kill-region (point)
2783 ;; It is better to move point to the other end of the kill
2784 ;; before killing. That way, in a read-only buffer, point
2785 ;; moves across the text that is copied to the kill ring.
2786 ;; The choice has no effect on undo now that undo records
2787 ;; the value of point from before the command was run.
2788 (progn
2789 (if arg
2790 (forward-visible-line (prefix-numeric-value arg))
2791 (if (eobp)
2792 (signal 'end-of-buffer nil))
2793 (let ((end
2794 (save-excursion
2795 (end-of-visible-line) (point))))
2796 (if (or (save-excursion
2797 ;; If trailing whitespace is visible,
2798 ;; don't treat it as nothing.
2799 (unless show-trailing-whitespace
2800 (skip-chars-forward " \t" end))
2801 (= (point) end))
2802 (and kill-whole-line (bolp)))
2803 (forward-visible-line 1)
2804 (goto-char end))))
2805 (point))))
2807 (defun kill-whole-line (&optional arg)
2808 "Kill current line.
2809 With prefix arg, kill that many lines starting from the current line.
2810 If arg is negative, kill backward. Also kill the preceding newline.
2811 \(This is meant to make \\[repeat] work well with negative arguments.\)
2812 If arg is zero, kill current line but exclude the trailing newline."
2813 (interactive "p")
2814 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2815 (signal 'end-of-buffer nil))
2816 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2817 (signal 'beginning-of-buffer nil))
2818 (unless (eq last-command 'kill-region)
2819 (kill-new "")
2820 (setq last-command 'kill-region))
2821 (cond ((zerop arg)
2822 ;; We need to kill in two steps, because the previous command
2823 ;; could have been a kill command, in which case the text
2824 ;; before point needs to be prepended to the current kill
2825 ;; ring entry and the text after point appended. Also, we
2826 ;; need to use save-excursion to avoid copying the same text
2827 ;; twice to the kill ring in read-only buffers.
2828 (save-excursion
2829 (kill-region (point) (progn (forward-visible-line 0) (point))))
2830 (kill-region (point) (progn (end-of-visible-line) (point))))
2831 ((< arg 0)
2832 (save-excursion
2833 (kill-region (point) (progn (end-of-visible-line) (point))))
2834 (kill-region (point)
2835 (progn (forward-visible-line (1+ arg))
2836 (unless (bobp) (backward-char))
2837 (point))))
2839 (save-excursion
2840 (kill-region (point) (progn (forward-visible-line 0) (point))))
2841 (kill-region (point)
2842 (progn (forward-visible-line arg) (point))))))
2844 (defun forward-visible-line (arg)
2845 "Move forward by ARG lines, ignoring currently invisible newlines only.
2846 If ARG is negative, move backward -ARG lines.
2847 If ARG is zero, move to the beginning of the current line."
2848 (condition-case nil
2849 (if (> arg 0)
2850 (progn
2851 (while (> arg 0)
2852 (or (zerop (forward-line 1))
2853 (signal 'end-of-buffer nil))
2854 ;; If the newline we just skipped is invisible,
2855 ;; don't count it.
2856 (let ((prop
2857 (get-char-property (1- (point)) 'invisible)))
2858 (if (if (eq buffer-invisibility-spec t)
2859 prop
2860 (or (memq prop buffer-invisibility-spec)
2861 (assq prop buffer-invisibility-spec)))
2862 (setq arg (1+ arg))))
2863 (setq arg (1- arg)))
2864 ;; If invisible text follows, and it is a number of complete lines,
2865 ;; skip it.
2866 (let ((opoint (point)))
2867 (while (and (not (eobp))
2868 (let ((prop
2869 (get-char-property (point) 'invisible)))
2870 (if (eq buffer-invisibility-spec t)
2871 prop
2872 (or (memq prop buffer-invisibility-spec)
2873 (assq prop buffer-invisibility-spec)))))
2874 (goto-char
2875 (if (get-text-property (point) 'invisible)
2876 (or (next-single-property-change (point) 'invisible)
2877 (point-max))
2878 (next-overlay-change (point)))))
2879 (unless (bolp)
2880 (goto-char opoint))))
2881 (let ((first t))
2882 (while (or first (<= arg 0))
2883 (if first
2884 (beginning-of-line)
2885 (or (zerop (forward-line -1))
2886 (signal 'beginning-of-buffer nil)))
2887 ;; If the newline we just moved to is invisible,
2888 ;; don't count it.
2889 (unless (bobp)
2890 (let ((prop
2891 (get-char-property (1- (point)) 'invisible)))
2892 (unless (if (eq buffer-invisibility-spec t)
2893 prop
2894 (or (memq prop buffer-invisibility-spec)
2895 (assq prop buffer-invisibility-spec)))
2896 (setq arg (1+ arg)))))
2897 (setq first nil))
2898 ;; If invisible text follows, and it is a number of complete lines,
2899 ;; skip it.
2900 (let ((opoint (point)))
2901 (while (and (not (bobp))
2902 (let ((prop
2903 (get-char-property (1- (point)) 'invisible)))
2904 (if (eq buffer-invisibility-spec t)
2905 prop
2906 (or (memq prop buffer-invisibility-spec)
2907 (assq prop buffer-invisibility-spec)))))
2908 (goto-char
2909 (if (get-text-property (1- (point)) 'invisible)
2910 (or (previous-single-property-change (point) 'invisible)
2911 (point-min))
2912 (previous-overlay-change (point)))))
2913 (unless (bolp)
2914 (goto-char opoint)))))
2915 ((beginning-of-buffer end-of-buffer)
2916 nil)))
2918 (defun end-of-visible-line ()
2919 "Move to end of current visible line."
2920 (end-of-line)
2921 ;; If the following character is currently invisible,
2922 ;; skip all characters with that same `invisible' property value,
2923 ;; then find the next newline.
2924 (while (and (not (eobp))
2925 (save-excursion
2926 (skip-chars-forward "^\n")
2927 (let ((prop
2928 (get-char-property (point) 'invisible)))
2929 (if (eq buffer-invisibility-spec t)
2930 prop
2931 (or (memq prop buffer-invisibility-spec)
2932 (assq prop buffer-invisibility-spec))))))
2933 (skip-chars-forward "^\n")
2934 (if (get-text-property (point) 'invisible)
2935 (goto-char (next-single-property-change (point) 'invisible))
2936 (goto-char (next-overlay-change (point))))
2937 (end-of-line)))
2939 (defun insert-buffer (buffer)
2940 "Insert after point the contents of BUFFER.
2941 Puts mark after the inserted text.
2942 BUFFER may be a buffer or a buffer name.
2944 This function is meant for the user to run interactively.
2945 Don't call it from programs: use `insert-buffer-substring' instead!"
2946 (interactive
2947 (list
2948 (progn
2949 (barf-if-buffer-read-only)
2950 (read-buffer "Insert buffer: "
2951 (if (eq (selected-window) (next-window (selected-window)))
2952 (other-buffer (current-buffer))
2953 (window-buffer (next-window (selected-window))))
2954 t))))
2955 (push-mark
2956 (save-excursion
2957 (insert-buffer-substring (get-buffer buffer))
2958 (point)))
2959 nil)
2961 (defun append-to-buffer (buffer start end)
2962 "Append to specified buffer the text of the region.
2963 It is inserted into that buffer before its point.
2965 When calling from a program, give three arguments:
2966 BUFFER (or buffer name), START and END.
2967 START and END specify the portion of the current buffer to be copied."
2968 (interactive
2969 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2970 (region-beginning) (region-end)))
2971 (let ((oldbuf (current-buffer)))
2972 (save-excursion
2973 (let* ((append-to (get-buffer-create buffer))
2974 (windows (get-buffer-window-list append-to t t))
2975 point)
2976 (set-buffer append-to)
2977 (setq point (point))
2978 (barf-if-buffer-read-only)
2979 (insert-buffer-substring oldbuf start end)
2980 (dolist (window windows)
2981 (when (= (window-point window) point)
2982 (set-window-point window (point))))))))
2984 (defun prepend-to-buffer (buffer start end)
2985 "Prepend to specified buffer the text of the region.
2986 It is inserted into that buffer after its point.
2988 When calling from a program, give three arguments:
2989 BUFFER (or buffer name), START and END.
2990 START and END specify the portion of the current buffer to be copied."
2991 (interactive "BPrepend to buffer: \nr")
2992 (let ((oldbuf (current-buffer)))
2993 (save-excursion
2994 (set-buffer (get-buffer-create buffer))
2995 (barf-if-buffer-read-only)
2996 (save-excursion
2997 (insert-buffer-substring oldbuf start end)))))
2999 (defun copy-to-buffer (buffer start end)
3000 "Copy to specified buffer the text of the region.
3001 It is inserted into that buffer, replacing existing text there.
3003 When calling from a program, give three arguments:
3004 BUFFER (or buffer name), START and END.
3005 START and END specify the portion of the current buffer to be copied."
3006 (interactive "BCopy to buffer: \nr")
3007 (let ((oldbuf (current-buffer)))
3008 (with-current-buffer (get-buffer-create buffer)
3009 (barf-if-buffer-read-only)
3010 (erase-buffer)
3011 (save-excursion
3012 (insert-buffer-substring oldbuf start end)))))
3014 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3015 (put 'mark-inactive 'error-message "The mark is not active now")
3017 (defvar activate-mark-hook nil
3018 "Hook run when the mark becomes active.
3019 It is also run at the end of a command, if the mark is active and
3020 it is possible that the region may have changed")
3022 (defvar deactivate-mark-hook nil
3023 "Hook run when the mark becomes inactive.")
3025 (defun mark (&optional force)
3026 "Return this buffer's mark value as integer; error if mark inactive.
3027 If optional argument FORCE is non-nil, access the mark value
3028 even if the mark is not currently active, and return nil
3029 if there is no mark at all.
3031 If you are using this in an editing command, you are most likely making
3032 a mistake; see the documentation of `set-mark'."
3033 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3034 (marker-position (mark-marker))
3035 (signal 'mark-inactive nil)))
3037 ;; Many places set mark-active directly, and several of them failed to also
3038 ;; run deactivate-mark-hook. This shorthand should simplify.
3039 (defsubst deactivate-mark ()
3040 "Deactivate the mark by setting `mark-active' to nil.
3041 \(That makes a difference only in Transient Mark mode.)
3042 Also runs the hook `deactivate-mark-hook'."
3043 (cond
3044 ((eq transient-mark-mode 'lambda)
3045 (setq transient-mark-mode nil))
3046 (transient-mark-mode
3047 (setq mark-active nil)
3048 (run-hooks 'deactivate-mark-hook))))
3050 (defun set-mark (pos)
3051 "Set this buffer's mark to POS. Don't use this function!
3052 That is to say, don't use this function unless you want
3053 the user to see that the mark has moved, and you want the previous
3054 mark position to be lost.
3056 Normally, when a new mark is set, the old one should go on the stack.
3057 This is why most applications should use `push-mark', not `set-mark'.
3059 Novice Emacs Lisp programmers often try to use the mark for the wrong
3060 purposes. The mark saves a location for the user's convenience.
3061 Most editing commands should not alter the mark.
3062 To remember a location for internal use in the Lisp program,
3063 store it in a Lisp variable. Example:
3065 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3067 (if pos
3068 (progn
3069 (setq mark-active t)
3070 (run-hooks 'activate-mark-hook)
3071 (set-marker (mark-marker) pos (current-buffer)))
3072 ;; Normally we never clear mark-active except in Transient Mark mode.
3073 ;; But when we actually clear out the mark value too,
3074 ;; we must clear mark-active in any mode.
3075 (setq mark-active nil)
3076 (run-hooks 'deactivate-mark-hook)
3077 (set-marker (mark-marker) nil)))
3079 (defvar mark-ring nil
3080 "The list of former marks of the current buffer, most recent first.")
3081 (make-variable-buffer-local 'mark-ring)
3082 (put 'mark-ring 'permanent-local t)
3084 (defcustom mark-ring-max 16
3085 "*Maximum size of mark ring. Start discarding off end if gets this big."
3086 :type 'integer
3087 :group 'editing-basics)
3089 (defvar global-mark-ring nil
3090 "The list of saved global marks, most recent first.")
3092 (defcustom global-mark-ring-max 16
3093 "*Maximum size of global mark ring. \
3094 Start discarding off end if gets this big."
3095 :type 'integer
3096 :group 'editing-basics)
3098 (defun pop-to-mark-command ()
3099 "Jump to mark, and pop a new position for mark off the ring
3100 \(does not affect global mark ring\)."
3101 (interactive)
3102 (if (null (mark t))
3103 (error "No mark set in this buffer")
3104 (goto-char (mark t))
3105 (pop-mark)))
3107 (defun push-mark-command (arg &optional nomsg)
3108 "Set mark at where point is.
3109 If no prefix arg and mark is already set there, just activate it.
3110 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3111 (interactive "P")
3112 (let ((mark (marker-position (mark-marker))))
3113 (if (or arg (null mark) (/= mark (point)))
3114 (push-mark nil nomsg t)
3115 (setq mark-active t)
3116 (run-hooks 'activate-mark-hook)
3117 (unless nomsg
3118 (message "Mark activated")))))
3120 (defcustom set-mark-command-repeat-pop nil
3121 "*Non-nil means that repeating \\[set-mark-command] after popping will pop.
3122 This means that if you type C-u \\[set-mark-command] \\[set-mark-command]
3123 will pop twice."
3124 :type 'boolean
3125 :group 'editing)
3127 (defun set-mark-command (arg)
3128 "Set mark at where point is, or jump to mark.
3129 With no prefix argument, set mark, and push old mark position on local
3130 mark ring; also push mark on global mark ring if last mark was set in
3131 another buffer. Immediately repeating the command activates
3132 `transient-mark-mode' temporarily.
3134 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
3135 jump to mark, and pop a new position
3136 for mark off the local mark ring \(this does not affect the global
3137 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
3138 mark ring \(see `pop-global-mark'\).
3140 Repeating the \\[set-mark-command] command without the prefix jumps to
3141 the next position off the local (or global) mark ring.
3143 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
3144 \\[universal-argument] \\[set-mark-command], unconditionally
3145 set mark where point is.
3147 Novice Emacs Lisp programmers often try to use the mark for the wrong
3148 purposes. See the documentation of `set-mark' for more information."
3149 (interactive "P")
3150 (if (eq transient-mark-mode 'lambda)
3151 (setq transient-mark-mode nil))
3152 (cond
3153 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3154 (push-mark-command nil))
3155 ((not (eq this-command 'set-mark-command))
3156 (if arg
3157 (pop-to-mark-command)
3158 (push-mark-command t)))
3159 ((and set-mark-command-repeat-pop
3160 (eq last-command 'pop-to-mark-command))
3161 (setq this-command 'pop-to-mark-command)
3162 (pop-to-mark-command))
3163 ((and set-mark-command-repeat-pop
3164 (eq last-command 'pop-global-mark)
3165 (not arg))
3166 (setq this-command 'pop-global-mark)
3167 (pop-global-mark))
3168 (arg
3169 (setq this-command 'pop-to-mark-command)
3170 (pop-to-mark-command))
3171 ((and (eq last-command 'set-mark-command)
3172 mark-active (null transient-mark-mode))
3173 (setq transient-mark-mode 'lambda)
3174 (message "Transient-mark-mode temporarily enabled"))
3176 (push-mark-command nil))))
3178 (defun push-mark (&optional location nomsg activate)
3179 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3180 If the last global mark pushed was not in the current buffer,
3181 also push LOCATION on the global mark ring.
3182 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3183 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
3185 Novice Emacs Lisp programmers often try to use the mark for the wrong
3186 purposes. See the documentation of `set-mark' for more information.
3188 In Transient Mark mode, this does not activate the mark."
3189 (unless (null (mark t))
3190 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3191 (when (> (length mark-ring) mark-ring-max)
3192 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3193 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3194 (set-marker (mark-marker) (or location (point)) (current-buffer))
3195 ;; Now push the mark on the global mark ring.
3196 (if (and global-mark-ring
3197 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3198 ;; The last global mark pushed was in this same buffer.
3199 ;; Don't push another one.
3201 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3202 (when (> (length global-mark-ring) global-mark-ring-max)
3203 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3204 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3205 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3206 (message "Mark set"))
3207 (if (or activate (not transient-mark-mode))
3208 (set-mark (mark t)))
3209 nil)
3211 (defun pop-mark ()
3212 "Pop off mark ring into the buffer's actual mark.
3213 Does not set point. Does nothing if mark ring is empty."
3214 (when mark-ring
3215 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3216 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3217 (move-marker (car mark-ring) nil)
3218 (if (null (mark t)) (ding))
3219 (setq mark-ring (cdr mark-ring)))
3220 (deactivate-mark))
3222 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3223 (defun exchange-point-and-mark (&optional arg)
3224 "Put the mark where point is now, and point where the mark is now.
3225 This command works even when the mark is not active,
3226 and it reactivates the mark.
3227 With prefix arg, `transient-mark-mode' is enabled temporarily."
3228 (interactive "P")
3229 (if arg
3230 (if mark-active
3231 (if (null transient-mark-mode)
3232 (setq transient-mark-mode 'lambda))
3233 (setq arg nil)))
3234 (unless arg
3235 (let ((omark (mark t)))
3236 (if (null omark)
3237 (error "No mark set in this buffer"))
3238 (set-mark (point))
3239 (goto-char omark)
3240 nil)))
3242 (define-minor-mode transient-mark-mode
3243 "Toggle Transient Mark mode.
3244 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3246 In Transient Mark mode, when the mark is active, the region is highlighted.
3247 Changing the buffer \"deactivates\" the mark.
3248 So do certain other operations that set the mark
3249 but whose main purpose is something else--for example,
3250 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3252 You can also deactivate the mark by typing \\[keyboard-quit] or
3253 \\[keyboard-escape-quit].
3255 Many commands change their behavior when Transient Mark mode is in effect
3256 and the mark is active, by acting on the region instead of their usual
3257 default part of the buffer's text. Examples of such commands include
3258 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3259 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3260 Invoke \\[apropos-documentation] and type \"transient\" or
3261 \"mark.*active\" at the prompt, to see the documentation of
3262 commands which are sensitive to the Transient Mark mode."
3263 :global t :group 'editing-basics :require nil)
3265 (defvar widen-automatically t
3266 "Non-nil means it is ok for commands to call `widen' when they want to.
3267 Some commands will do this in order to go to positions outside
3268 the current accessible part of the buffer.
3270 If `widen-automatically' is nil, these commands will do something else
3271 as a fallback, and won't change the buffer bounds.")
3273 (defun pop-global-mark ()
3274 "Pop off global mark ring and jump to the top location."
3275 (interactive)
3276 ;; Pop entries which refer to non-existent buffers.
3277 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3278 (setq global-mark-ring (cdr global-mark-ring)))
3279 (or global-mark-ring
3280 (error "No global mark set"))
3281 (let* ((marker (car global-mark-ring))
3282 (buffer (marker-buffer marker))
3283 (position (marker-position marker)))
3284 (setq global-mark-ring (nconc (cdr global-mark-ring)
3285 (list (car global-mark-ring))))
3286 (set-buffer buffer)
3287 (or (and (>= position (point-min))
3288 (<= position (point-max)))
3289 (if widen-automatically
3290 (widen)
3291 (error "Global mark position is outside accessible part of buffer")))
3292 (goto-char position)
3293 (switch-to-buffer buffer)))
3295 (defcustom next-line-add-newlines nil
3296 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3297 :type 'boolean
3298 :version "21.1"
3299 :group 'editing-basics)
3301 (defun next-line (&optional arg try-vscroll)
3302 "Move cursor vertically down ARG lines.
3303 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3304 If there is no character in the target line exactly under the current column,
3305 the cursor is positioned after the character in that line which spans this
3306 column, or at the end of the line if it is not long enough.
3307 If there is no line in the buffer after this one, behavior depends on the
3308 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3309 to create a line, and moves the cursor to that line. Otherwise it moves the
3310 cursor to the end of the buffer.
3312 The command \\[set-goal-column] can be used to create
3313 a semipermanent goal column for this command.
3314 Then instead of trying to move exactly vertically (or as close as possible),
3315 this command moves to the specified goal column (or as close as possible).
3316 The goal column is stored in the variable `goal-column', which is nil
3317 when there is no goal column.
3319 If you are thinking of using this in a Lisp program, consider
3320 using `forward-line' instead. It is usually easier to use
3321 and more reliable (no dependence on goal column, etc.)."
3322 (interactive "p\np")
3323 (or arg (setq arg 1))
3324 (if (and next-line-add-newlines (= arg 1))
3325 (if (save-excursion (end-of-line) (eobp))
3326 ;; When adding a newline, don't expand an abbrev.
3327 (let ((abbrev-mode nil))
3328 (end-of-line)
3329 (insert hard-newline))
3330 (line-move arg nil nil try-vscroll))
3331 (if (interactive-p)
3332 (condition-case nil
3333 (line-move arg nil nil try-vscroll)
3334 ((beginning-of-buffer end-of-buffer) (ding)))
3335 (line-move arg nil nil try-vscroll)))
3336 nil)
3338 (defun previous-line (&optional arg try-vscroll)
3339 "Move cursor vertically up ARG lines.
3340 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3341 If there is no character in the target line exactly over the current column,
3342 the cursor is positioned after the character in that line which spans this
3343 column, or at the end of the line if it is not long enough.
3345 The command \\[set-goal-column] can be used to create
3346 a semipermanent goal column for this command.
3347 Then instead of trying to move exactly vertically (or as close as possible),
3348 this command moves to the specified goal column (or as close as possible).
3349 The goal column is stored in the variable `goal-column', which is nil
3350 when there is no goal column.
3352 If you are thinking of using this in a Lisp program, consider using
3353 `forward-line' with a negative argument instead. It is usually easier
3354 to use and more reliable (no dependence on goal column, etc.)."
3355 (interactive "p\np")
3356 (or arg (setq arg 1))
3357 (if (interactive-p)
3358 (condition-case nil
3359 (line-move (- arg) nil nil try-vscroll)
3360 ((beginning-of-buffer end-of-buffer) (ding)))
3361 (line-move (- arg) nil nil try-vscroll))
3362 nil)
3364 (defcustom track-eol nil
3365 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3366 This means moving to the end of each line moved onto.
3367 The beginning of a blank line does not count as the end of a line."
3368 :type 'boolean
3369 :group 'editing-basics)
3371 (defcustom goal-column nil
3372 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3373 :type '(choice integer
3374 (const :tag "None" nil))
3375 :group 'editing-basics)
3376 (make-variable-buffer-local 'goal-column)
3378 (defvar temporary-goal-column 0
3379 "Current goal column for vertical motion.
3380 It is the column where point was
3381 at the start of current run of vertical motion commands.
3382 When the `track-eol' feature is doing its job, the value is 9999.")
3384 (defcustom line-move-ignore-invisible t
3385 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3386 Outline mode sets this."
3387 :type 'boolean
3388 :group 'editing-basics)
3390 (defun line-move-invisible-p (pos)
3391 "Return non-nil if the character after POS is currently invisible."
3392 (let ((prop
3393 (get-char-property pos 'invisible)))
3394 (if (eq buffer-invisibility-spec t)
3395 prop
3396 (or (memq prop buffer-invisibility-spec)
3397 (assq prop buffer-invisibility-spec)))))
3399 ;; This is like line-move-1 except that it also performs
3400 ;; vertical scrolling of tall images if appropriate.
3401 ;; That is not really a clean thing to do, since it mixes
3402 ;; scrolling with cursor motion. But so far we don't have
3403 ;; a cleaner solution to the problem of making C-n do something
3404 ;; useful given a tall image.
3405 (defun line-move (arg &optional noerror to-end try-vscroll)
3406 (if (and auto-window-vscroll try-vscroll
3407 ;; But don't vscroll in a keyboard macro.
3408 (not defining-kbd-macro)
3409 (not executing-kbd-macro))
3410 (let ((forward (> arg 0))
3411 (part (nth 2 (pos-visible-in-window-p (point) nil t))))
3412 (if (and (consp part)
3413 (> (if forward (cdr part) (car part)) 0))
3414 (set-window-vscroll nil
3415 (if forward
3416 (+ (window-vscroll nil t)
3417 (min (cdr part)
3418 (* (frame-char-height) arg)))
3419 (max 0
3420 (- (window-vscroll nil t)
3421 (min (car part)
3422 (* (frame-char-height) (- arg))))))
3424 (set-window-vscroll nil 0)
3425 (when (line-move-1 arg noerror to-end)
3426 (when (not forward)
3427 ;; Update display before calling pos-visible-in-window-p,
3428 ;; because it depends on window-start being up-to-date.
3429 (sit-for 0)
3430 ;; If the current line is partly hidden at the bottom,
3431 ;; scroll it partially up so as to unhide the bottom.
3432 (if (and (setq part (nth 2 (pos-visible-in-window-p
3433 (line-beginning-position) nil t)))
3434 (> (cdr part) 0))
3435 (set-window-vscroll nil (cdr part) t)))
3436 t)))
3437 (line-move-1 arg noerror to-end)))
3439 ;; This is the guts of next-line and previous-line.
3440 ;; Arg says how many lines to move.
3441 ;; The value is t if we can move the specified number of lines.
3442 (defun line-move-1 (arg &optional noerror to-end)
3443 ;; Don't run any point-motion hooks, and disregard intangibility,
3444 ;; for intermediate positions.
3445 (let ((inhibit-point-motion-hooks t)
3446 (opoint (point))
3447 (forward (> arg 0)))
3448 (unwind-protect
3449 (progn
3450 (if (not (memq last-command '(next-line previous-line)))
3451 (setq temporary-goal-column
3452 (if (and track-eol (eolp)
3453 ;; Don't count beg of empty line as end of line
3454 ;; unless we just did explicit end-of-line.
3455 (or (not (bolp)) (eq last-command 'end-of-line)))
3456 9999
3457 (current-column))))
3459 (if (and (not (integerp selective-display))
3460 (not line-move-ignore-invisible))
3461 ;; Use just newline characters.
3462 ;; Set ARG to 0 if we move as many lines as requested.
3463 (or (if (> arg 0)
3464 (progn (if (> arg 1) (forward-line (1- arg)))
3465 ;; This way of moving forward ARG lines
3466 ;; verifies that we have a newline after the last one.
3467 ;; It doesn't get confused by intangible text.
3468 (end-of-line)
3469 (if (zerop (forward-line 1))
3470 (setq arg 0)))
3471 (and (zerop (forward-line arg))
3472 (bolp)
3473 (setq arg 0)))
3474 (unless noerror
3475 (signal (if (< arg 0)
3476 'beginning-of-buffer
3477 'end-of-buffer)
3478 nil)))
3479 ;; Move by arg lines, but ignore invisible ones.
3480 (let (done)
3481 (while (and (> arg 0) (not done))
3482 ;; If the following character is currently invisible,
3483 ;; skip all characters with that same `invisible' property value.
3484 (while (and (not (eobp)) (line-move-invisible-p (point)))
3485 (goto-char (next-char-property-change (point))))
3486 ;; Now move a line.
3487 (end-of-line)
3488 ;; If there's no invisibility here, move over the newline.
3489 (cond
3490 ((eobp)
3491 (if (not noerror)
3492 (signal 'end-of-buffer nil)
3493 (setq done t)))
3494 ((and (> arg 1) ;; Use vertical-motion for last move
3495 (not (integerp selective-display))
3496 (not (line-move-invisible-p (point))))
3497 ;; We avoid vertical-motion when possible
3498 ;; because that has to fontify.
3499 (forward-line 1))
3500 ;; Otherwise move a more sophisticated way.
3501 ((zerop (vertical-motion 1))
3502 (if (not noerror)
3503 (signal 'end-of-buffer nil)
3504 (setq done t))))
3505 (unless done
3506 (setq arg (1- arg))))
3507 ;; The logic of this is the same as the loop above,
3508 ;; it just goes in the other direction.
3509 (while (and (< arg 0) (not done))
3510 (beginning-of-line)
3511 (cond
3512 ((bobp)
3513 (if (not noerror)
3514 (signal 'beginning-of-buffer nil)
3515 (setq done t)))
3516 ((and (< arg -1) ;; Use vertical-motion for last move
3517 (not (integerp selective-display))
3518 (not (line-move-invisible-p (1- (point)))))
3519 (forward-line -1))
3520 ((zerop (vertical-motion -1))
3521 (if (not noerror)
3522 (signal 'beginning-of-buffer nil)
3523 (setq done t))))
3524 (unless done
3525 (setq arg (1+ arg))
3526 (while (and ;; Don't move over previous invis lines
3527 ;; if our target is the middle of this line.
3528 (or (zerop (or goal-column temporary-goal-column))
3529 (< arg 0))
3530 (not (bobp)) (line-move-invisible-p (1- (point))))
3531 (goto-char (previous-char-property-change (point))))))))
3532 ;; This is the value the function returns.
3533 (= arg 0))
3535 (cond ((> arg 0)
3536 ;; If we did not move down as far as desired,
3537 ;; at least go to end of line.
3538 (end-of-line))
3539 ((< arg 0)
3540 ;; If we did not move up as far as desired,
3541 ;; at least go to beginning of line.
3542 (beginning-of-line))
3544 (line-move-finish (or goal-column temporary-goal-column)
3545 opoint forward))))))
3547 (defun line-move-finish (column opoint forward)
3548 (let ((repeat t))
3549 (while repeat
3550 ;; Set REPEAT to t to repeat the whole thing.
3551 (setq repeat nil)
3553 (let (new
3554 (line-beg (save-excursion (beginning-of-line) (point)))
3555 (line-end
3556 ;; Compute the end of the line
3557 ;; ignoring effectively invisible newlines.
3558 (save-excursion
3559 ;; Like end-of-line but ignores fields.
3560 (skip-chars-forward "^\n")
3561 (while (and (not (eobp)) (line-move-invisible-p (point)))
3562 (goto-char (next-char-property-change (point)))
3563 (skip-chars-forward "^\n"))
3564 (point))))
3566 ;; Move to the desired column.
3567 (line-move-to-column column)
3568 (setq new (point))
3570 ;; Process intangibility within a line.
3571 ;; Move to the chosen destination position from above,
3572 ;; with intangibility processing enabled.
3574 (goto-char (point-min))
3575 (let ((inhibit-point-motion-hooks nil))
3576 (goto-char new)
3578 ;; If intangibility moves us to a different (later) place
3579 ;; in the same line, use that as the destination.
3580 (if (<= (point) line-end)
3581 (setq new (point))
3582 ;; If that position is "too late",
3583 ;; try the previous allowable position.
3584 ;; See if it is ok.
3585 (backward-char)
3586 (if (if forward
3587 ;; If going forward, don't accept the previous
3588 ;; allowable position if it is before the target line.
3589 (< line-beg (point))
3590 ;; If going backward, don't accept the previous
3591 ;; allowable position if it is still after the target line.
3592 (<= (point) line-end))
3593 (setq new (point))
3594 ;; As a last resort, use the end of the line.
3595 (setq new line-end))))
3597 ;; Now move to the updated destination, processing fields
3598 ;; as well as intangibility.
3599 (goto-char opoint)
3600 (let ((inhibit-point-motion-hooks nil))
3601 (goto-char
3602 (constrain-to-field new opoint nil t
3603 'inhibit-line-move-field-capture)))
3605 ;; If all this moved us to a different line,
3606 ;; retry everything within that new line.
3607 (when (or (< (point) line-beg) (> (point) line-end))
3608 ;; Repeat the intangibility and field processing.
3609 (setq repeat t))))))
3611 (defun line-move-to-column (col)
3612 "Try to find column COL, considering invisibility.
3613 This function works only in certain cases,
3614 because what we really need is for `move-to-column'
3615 and `current-column' to be able to ignore invisible text."
3616 (if (zerop col)
3617 (beginning-of-line)
3618 (move-to-column col))
3620 (when (and line-move-ignore-invisible
3621 (not (bolp)) (line-move-invisible-p (1- (point))))
3622 (let ((normal-location (point))
3623 (normal-column (current-column)))
3624 ;; If the following character is currently invisible,
3625 ;; skip all characters with that same `invisible' property value.
3626 (while (and (not (eobp))
3627 (line-move-invisible-p (point)))
3628 (goto-char (next-char-property-change (point))))
3629 ;; Have we advanced to a larger column position?
3630 (if (> (current-column) normal-column)
3631 ;; We have made some progress towards the desired column.
3632 ;; See if we can make any further progress.
3633 (line-move-to-column (+ (current-column) (- col normal-column)))
3634 ;; Otherwise, go to the place we originally found
3635 ;; and move back over invisible text.
3636 ;; that will get us to the same place on the screen
3637 ;; but with a more reasonable buffer position.
3638 (goto-char normal-location)
3639 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3640 (while (and (not (bolp)) (line-move-invisible-p (1- (point))))
3641 (goto-char (previous-char-property-change (point) line-beg))))))))
3643 (defun move-end-of-line (arg)
3644 "Move point to end of current line.
3645 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3646 If point reaches the beginning or end of buffer, it stops there.
3647 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3649 This command does not move point across a field boundary unless doing so
3650 would move beyond there to a different line; if ARG is nil or 1, and
3651 point starts at a field boundary, point does not move. To ignore field
3652 boundaries bind `inhibit-field-text-motion' to t."
3653 (interactive "p")
3654 (or arg (setq arg 1))
3655 (let (done)
3656 (while (not done)
3657 (let ((newpos
3658 (save-excursion
3659 (let ((goal-column 0))
3660 (and (line-move arg t)
3661 (not (bobp))
3662 (progn
3663 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
3664 (goto-char (previous-char-property-change (point))))
3665 (backward-char 1)))
3666 (point)))))
3667 (goto-char newpos)
3668 (if (and (> (point) newpos)
3669 (eq (preceding-char) ?\n))
3670 (backward-char 1)
3671 (if (and (> (point) newpos) (not (eobp))
3672 (not (eq (following-char) ?\n)))
3673 ;; If we skipped something intangible
3674 ;; and now we're not really at eol,
3675 ;; keep going.
3676 (setq arg 1)
3677 (setq done t)))))))
3679 (defun move-beginning-of-line (arg)
3680 "Move point to beginning of current display line.
3681 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3682 If point reaches the beginning or end of buffer, it stops there.
3683 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3685 This command does not move point across a field boundary unless doing so
3686 would move beyond there to a different line; if ARG is nil or 1, and
3687 point starts at a field boundary, point does not move. To ignore field
3688 boundaries bind `inhibit-field-text-motion' to t."
3689 (interactive "p")
3690 (or arg (setq arg 1))
3691 (if (/= arg 1)
3692 (line-move (1- arg) t))
3693 (beginning-of-line 1)
3694 (let ((orig (point)))
3695 (vertical-motion 0)
3696 (if (/= orig (point))
3697 (goto-char (constrain-to-field (point) orig (/= arg 1) t nil)))))
3700 ;;; Many people have said they rarely use this feature, and often type
3701 ;;; it by accident. Maybe it shouldn't even be on a key.
3702 (put 'set-goal-column 'disabled t)
3704 (defun set-goal-column (arg)
3705 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3706 Those commands will move to this position in the line moved to
3707 rather than trying to keep the same horizontal position.
3708 With a non-nil argument, clears out the goal column
3709 so that \\[next-line] and \\[previous-line] resume vertical motion.
3710 The goal column is stored in the variable `goal-column'."
3711 (interactive "P")
3712 (if arg
3713 (progn
3714 (setq goal-column nil)
3715 (message "No goal column"))
3716 (setq goal-column (current-column))
3717 ;; The older method below can be erroneous if `set-goal-column' is bound
3718 ;; to a sequence containing %
3719 ;;(message (substitute-command-keys
3720 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3721 ;;goal-column)
3722 (message "%s"
3723 (concat
3724 (format "Goal column %d " goal-column)
3725 (substitute-command-keys
3726 "(use \\[set-goal-column] with an arg to unset it)")))
3729 nil)
3732 (defun scroll-other-window-down (lines)
3733 "Scroll the \"other window\" down.
3734 For more details, see the documentation for `scroll-other-window'."
3735 (interactive "P")
3736 (scroll-other-window
3737 ;; Just invert the argument's meaning.
3738 ;; We can do that without knowing which window it will be.
3739 (if (eq lines '-) nil
3740 (if (null lines) '-
3741 (- (prefix-numeric-value lines))))))
3743 (defun beginning-of-buffer-other-window (arg)
3744 "Move point to the beginning of the buffer in the other window.
3745 Leave mark at previous position.
3746 With arg N, put point N/10 of the way from the true beginning."
3747 (interactive "P")
3748 (let ((orig-window (selected-window))
3749 (window (other-window-for-scrolling)))
3750 ;; We use unwind-protect rather than save-window-excursion
3751 ;; because the latter would preserve the things we want to change.
3752 (unwind-protect
3753 (progn
3754 (select-window window)
3755 ;; Set point and mark in that window's buffer.
3756 (with-no-warnings
3757 (beginning-of-buffer arg))
3758 ;; Set point accordingly.
3759 (recenter '(t)))
3760 (select-window orig-window))))
3762 (defun end-of-buffer-other-window (arg)
3763 "Move point to the end of the buffer in the other window.
3764 Leave mark at previous position.
3765 With arg N, put point N/10 of the way from the true end."
3766 (interactive "P")
3767 ;; See beginning-of-buffer-other-window for comments.
3768 (let ((orig-window (selected-window))
3769 (window (other-window-for-scrolling)))
3770 (unwind-protect
3771 (progn
3772 (select-window window)
3773 (with-no-warnings
3774 (end-of-buffer arg))
3775 (recenter '(t)))
3776 (select-window orig-window))))
3778 (defun transpose-chars (arg)
3779 "Interchange characters around point, moving forward one character.
3780 With prefix arg ARG, effect is to take character before point
3781 and drag it forward past ARG other characters (backward if ARG negative).
3782 If no argument and at end of line, the previous two chars are exchanged."
3783 (interactive "*P")
3784 (and (null arg) (eolp) (forward-char -1))
3785 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3787 (defun transpose-words (arg)
3788 "Interchange words around point, leaving point at end of them.
3789 With prefix arg ARG, effect is to take word before or around point
3790 and drag it forward past ARG other words (backward if ARG negative).
3791 If ARG is zero, the words around or after point and around or after mark
3792 are interchanged."
3793 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3794 (interactive "*p")
3795 (transpose-subr 'forward-word arg))
3797 (defun transpose-sexps (arg)
3798 "Like \\[transpose-words] but applies to sexps.
3799 Does not work on a sexp that point is in the middle of
3800 if it is a list or string."
3801 (interactive "*p")
3802 (transpose-subr
3803 (lambda (arg)
3804 ;; Here we should try to simulate the behavior of
3805 ;; (cons (progn (forward-sexp x) (point))
3806 ;; (progn (forward-sexp (- x)) (point)))
3807 ;; Except that we don't want to rely on the second forward-sexp
3808 ;; putting us back to where we want to be, since forward-sexp-function
3809 ;; might do funny things like infix-precedence.
3810 (if (if (> arg 0)
3811 (looking-at "\\sw\\|\\s_")
3812 (and (not (bobp))
3813 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3814 ;; Jumping over a symbol. We might be inside it, mind you.
3815 (progn (funcall (if (> arg 0)
3816 'skip-syntax-backward 'skip-syntax-forward)
3817 "w_")
3818 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3819 ;; Otherwise, we're between sexps. Take a step back before jumping
3820 ;; to make sure we'll obey the same precedence no matter which direction
3821 ;; we're going.
3822 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3823 (cons (save-excursion (forward-sexp arg) (point))
3824 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3825 (not (zerop (funcall (if (> arg 0)
3826 'skip-syntax-forward
3827 'skip-syntax-backward)
3828 ".")))))
3829 (point)))))
3830 arg 'special))
3832 (defun transpose-lines (arg)
3833 "Exchange current line and previous line, leaving point after both.
3834 With argument ARG, takes previous line and moves it past ARG lines.
3835 With argument 0, interchanges line point is in with line mark is in."
3836 (interactive "*p")
3837 (transpose-subr (function
3838 (lambda (arg)
3839 (if (> arg 0)
3840 (progn
3841 ;; Move forward over ARG lines,
3842 ;; but create newlines if necessary.
3843 (setq arg (forward-line arg))
3844 (if (/= (preceding-char) ?\n)
3845 (setq arg (1+ arg)))
3846 (if (> arg 0)
3847 (newline arg)))
3848 (forward-line arg))))
3849 arg))
3851 (defun transpose-subr (mover arg &optional special)
3852 (let ((aux (if special mover
3853 (lambda (x)
3854 (cons (progn (funcall mover x) (point))
3855 (progn (funcall mover (- x)) (point))))))
3856 pos1 pos2)
3857 (cond
3858 ((= arg 0)
3859 (save-excursion
3860 (setq pos1 (funcall aux 1))
3861 (goto-char (mark))
3862 (setq pos2 (funcall aux 1))
3863 (transpose-subr-1 pos1 pos2))
3864 (exchange-point-and-mark))
3865 ((> arg 0)
3866 (setq pos1 (funcall aux -1))
3867 (setq pos2 (funcall aux arg))
3868 (transpose-subr-1 pos1 pos2)
3869 (goto-char (car pos2)))
3871 (setq pos1 (funcall aux -1))
3872 (goto-char (car pos1))
3873 (setq pos2 (funcall aux arg))
3874 (transpose-subr-1 pos1 pos2)))))
3876 (defun transpose-subr-1 (pos1 pos2)
3877 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3878 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3879 (when (> (car pos1) (car pos2))
3880 (let ((swap pos1))
3881 (setq pos1 pos2 pos2 swap)))
3882 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3883 (atomic-change-group
3884 (let (word2)
3885 ;; FIXME: We first delete the two pieces of text, so markers that
3886 ;; used to point to after the text end up pointing to before it :-(
3887 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3888 (goto-char (car pos2))
3889 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3890 (goto-char (car pos1))
3891 (insert word2))))
3893 (defun backward-word (&optional arg)
3894 "Move backward until encountering the beginning of a word.
3895 With argument, do this that many times."
3896 (interactive "p")
3897 (forward-word (- (or arg 1))))
3899 (defun mark-word (&optional arg allow-extend)
3900 "Set mark ARG words away from point.
3901 The place mark goes is the same place \\[forward-word] would
3902 move to with the same argument.
3903 Interactively, if this command is repeated
3904 or (in Transient Mark mode) if the mark is active,
3905 it marks the next ARG words after the ones already marked."
3906 (interactive "P\np")
3907 (cond ((and allow-extend
3908 (or (and (eq last-command this-command) (mark t))
3909 (and transient-mark-mode mark-active)))
3910 (setq arg (if arg (prefix-numeric-value arg)
3911 (if (< (mark) (point)) -1 1)))
3912 (set-mark
3913 (save-excursion
3914 (goto-char (mark))
3915 (forward-word arg)
3916 (point))))
3918 (push-mark
3919 (save-excursion
3920 (forward-word (prefix-numeric-value arg))
3921 (point))
3922 nil t))))
3924 (defun kill-word (arg)
3925 "Kill characters forward until encountering the end of a word.
3926 With argument, do this that many times."
3927 (interactive "p")
3928 (kill-region (point) (progn (forward-word arg) (point))))
3930 (defun backward-kill-word (arg)
3931 "Kill characters backward until encountering the end of a word.
3932 With argument, do this that many times."
3933 (interactive "p")
3934 (kill-word (- arg)))
3936 (defun current-word (&optional strict really-word)
3937 "Return the symbol or word that point is on (or a nearby one) as a string.
3938 The return value includes no text properties.
3939 If optional arg STRICT is non-nil, return nil unless point is within
3940 or adjacent to a symbol or word. In all cases the value can be nil
3941 if there is no word nearby.
3942 The function, belying its name, normally finds a symbol.
3943 If optional arg REALLY-WORD is non-nil, it finds just a word."
3944 (save-excursion
3945 (let* ((oldpoint (point)) (start (point)) (end (point))
3946 (syntaxes (if really-word "w" "w_"))
3947 (not-syntaxes (concat "^" syntaxes)))
3948 (skip-syntax-backward syntaxes) (setq start (point))
3949 (goto-char oldpoint)
3950 (skip-syntax-forward syntaxes) (setq end (point))
3951 (when (and (eq start oldpoint) (eq end oldpoint)
3952 ;; Point is neither within nor adjacent to a word.
3953 (not strict))
3954 ;; Look for preceding word in same line.
3955 (skip-syntax-backward not-syntaxes
3956 (save-excursion (beginning-of-line)
3957 (point)))
3958 (if (bolp)
3959 ;; No preceding word in same line.
3960 ;; Look for following word in same line.
3961 (progn
3962 (skip-syntax-forward not-syntaxes
3963 (save-excursion (end-of-line)
3964 (point)))
3965 (setq start (point))
3966 (skip-syntax-forward syntaxes)
3967 (setq end (point)))
3968 (setq end (point))
3969 (skip-syntax-backward syntaxes)
3970 (setq start (point))))
3971 ;; If we found something nonempty, return it as a string.
3972 (unless (= start end)
3973 (buffer-substring-no-properties start end)))))
3975 (defcustom fill-prefix nil
3976 "*String for filling to insert at front of new line, or nil for none."
3977 :type '(choice (const :tag "None" nil)
3978 string)
3979 :group 'fill)
3980 (make-variable-buffer-local 'fill-prefix)
3982 (defcustom auto-fill-inhibit-regexp nil
3983 "*Regexp to match lines which should not be auto-filled."
3984 :type '(choice (const :tag "None" nil)
3985 regexp)
3986 :group 'fill)
3988 (defvar comment-line-break-function 'comment-indent-new-line
3989 "*Mode-specific function which line breaks and continues a comment.
3991 This function is only called during auto-filling of a comment section.
3992 The function should take a single optional argument, which is a flag
3993 indicating whether it should use soft newlines.")
3995 ;; This function is used as the auto-fill-function of a buffer
3996 ;; when Auto-Fill mode is enabled.
3997 ;; It returns t if it really did any work.
3998 ;; (Actually some major modes use a different auto-fill function,
3999 ;; but this one is the default one.)
4000 (defun do-auto-fill ()
4001 (let (fc justify give-up
4002 (fill-prefix fill-prefix))
4003 (if (or (not (setq justify (current-justification)))
4004 (null (setq fc (current-fill-column)))
4005 (and (eq justify 'left)
4006 (<= (current-column) fc))
4007 (and auto-fill-inhibit-regexp
4008 (save-excursion (beginning-of-line)
4009 (looking-at auto-fill-inhibit-regexp))))
4010 nil ;; Auto-filling not required
4011 (if (memq justify '(full center right))
4012 (save-excursion (unjustify-current-line)))
4014 ;; Choose a fill-prefix automatically.
4015 (when (and adaptive-fill-mode
4016 (or (null fill-prefix) (string= fill-prefix "")))
4017 (let ((prefix
4018 (fill-context-prefix
4019 (save-excursion (backward-paragraph 1) (point))
4020 (save-excursion (forward-paragraph 1) (point)))))
4021 (and prefix (not (equal prefix ""))
4022 ;; Use auto-indentation rather than a guessed empty prefix.
4023 (not (and fill-indent-according-to-mode
4024 (string-match "\\`[ \t]*\\'" prefix)))
4025 (setq fill-prefix prefix))))
4027 (while (and (not give-up) (> (current-column) fc))
4028 ;; Determine where to split the line.
4029 (let* (after-prefix
4030 (fill-point
4031 (save-excursion
4032 (beginning-of-line)
4033 (setq after-prefix (point))
4034 (and fill-prefix
4035 (looking-at (regexp-quote fill-prefix))
4036 (setq after-prefix (match-end 0)))
4037 (move-to-column (1+ fc))
4038 (fill-move-to-break-point after-prefix)
4039 (point))))
4041 ;; See whether the place we found is any good.
4042 (if (save-excursion
4043 (goto-char fill-point)
4044 (or (bolp)
4045 ;; There is no use breaking at end of line.
4046 (save-excursion (skip-chars-forward " ") (eolp))
4047 ;; It is futile to split at the end of the prefix
4048 ;; since we would just insert the prefix again.
4049 (and after-prefix (<= (point) after-prefix))
4050 ;; Don't split right after a comment starter
4051 ;; since we would just make another comment starter.
4052 (and comment-start-skip
4053 (let ((limit (point)))
4054 (beginning-of-line)
4055 (and (re-search-forward comment-start-skip
4056 limit t)
4057 (eq (point) limit))))))
4058 ;; No good place to break => stop trying.
4059 (setq give-up t)
4060 ;; Ok, we have a useful place to break the line. Do it.
4061 (let ((prev-column (current-column)))
4062 ;; If point is at the fill-point, do not `save-excursion'.
4063 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
4064 ;; point will end up before it rather than after it.
4065 (if (save-excursion
4066 (skip-chars-backward " \t")
4067 (= (point) fill-point))
4068 (funcall comment-line-break-function t)
4069 (save-excursion
4070 (goto-char fill-point)
4071 (funcall comment-line-break-function t)))
4072 ;; Now do justification, if required
4073 (if (not (eq justify 'left))
4074 (save-excursion
4075 (end-of-line 0)
4076 (justify-current-line justify nil t)))
4077 ;; If making the new line didn't reduce the hpos of
4078 ;; the end of the line, then give up now;
4079 ;; trying again will not help.
4080 (if (>= (current-column) prev-column)
4081 (setq give-up t))))))
4082 ;; Justify last line.
4083 (justify-current-line justify t t)
4084 t)))
4086 (defvar normal-auto-fill-function 'do-auto-fill
4087 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
4088 Some major modes set this.")
4090 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
4091 ;; FIXME: turn into a proper minor mode.
4092 ;; Add a global minor mode version of it.
4093 (defun auto-fill-mode (&optional arg)
4094 "Toggle Auto Fill mode.
4095 With arg, turn Auto Fill mode on if and only if arg is positive.
4096 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
4097 automatically breaks the line at a previous space.
4099 The value of `normal-auto-fill-function' specifies the function to use
4100 for `auto-fill-function' when turning Auto Fill mode on."
4101 (interactive "P")
4102 (prog1 (setq auto-fill-function
4103 (if (if (null arg)
4104 (not auto-fill-function)
4105 (> (prefix-numeric-value arg) 0))
4106 normal-auto-fill-function
4107 nil))
4108 (force-mode-line-update)))
4110 ;; This holds a document string used to document auto-fill-mode.
4111 (defun auto-fill-function ()
4112 "Automatically break line at a previous space, in insertion of text."
4113 nil)
4115 (defun turn-on-auto-fill ()
4116 "Unconditionally turn on Auto Fill mode."
4117 (auto-fill-mode 1))
4119 (defun turn-off-auto-fill ()
4120 "Unconditionally turn off Auto Fill mode."
4121 (auto-fill-mode -1))
4123 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
4125 (defun set-fill-column (arg)
4126 "Set `fill-column' to specified argument.
4127 Use \\[universal-argument] followed by a number to specify a column.
4128 Just \\[universal-argument] as argument means to use the current column."
4129 (interactive "P")
4130 (if (consp arg)
4131 (setq arg (current-column)))
4132 (if (not (integerp arg))
4133 ;; Disallow missing argument; it's probably a typo for C-x C-f.
4134 (error "set-fill-column requires an explicit argument")
4135 (message "Fill column set to %d (was %d)" arg fill-column)
4136 (setq fill-column arg)))
4138 (defun set-selective-display (arg)
4139 "Set `selective-display' to ARG; clear it if no arg.
4140 When the value of `selective-display' is a number > 0,
4141 lines whose indentation is >= that value are not displayed.
4142 The variable `selective-display' has a separate value for each buffer."
4143 (interactive "P")
4144 (if (eq selective-display t)
4145 (error "selective-display already in use for marked lines"))
4146 (let ((current-vpos
4147 (save-restriction
4148 (narrow-to-region (point-min) (point))
4149 (goto-char (window-start))
4150 (vertical-motion (window-height)))))
4151 (setq selective-display
4152 (and arg (prefix-numeric-value arg)))
4153 (recenter current-vpos))
4154 (set-window-start (selected-window) (window-start (selected-window)))
4155 (princ "selective-display set to " t)
4156 (prin1 selective-display t)
4157 (princ "." t))
4159 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
4160 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
4162 (defun toggle-truncate-lines (arg)
4163 "Toggle whether to fold or truncate long lines on the screen.
4164 With arg, truncate long lines iff arg is positive.
4165 Note that in side-by-side windows, truncation is always enabled."
4166 (interactive "P")
4167 (setq truncate-lines
4168 (if (null arg)
4169 (not truncate-lines)
4170 (> (prefix-numeric-value arg) 0)))
4171 (force-mode-line-update)
4172 (unless truncate-lines
4173 (let ((buffer (current-buffer)))
4174 (walk-windows (lambda (window)
4175 (if (eq buffer (window-buffer window))
4176 (set-window-hscroll window 0)))
4177 nil t)))
4178 (message "Truncate long lines %s"
4179 (if truncate-lines "enabled" "disabled")))
4181 (defvar overwrite-mode-textual " Ovwrt"
4182 "The string displayed in the mode line when in overwrite mode.")
4183 (defvar overwrite-mode-binary " Bin Ovwrt"
4184 "The string displayed in the mode line when in binary overwrite mode.")
4186 (defun overwrite-mode (arg)
4187 "Toggle overwrite mode.
4188 With arg, turn overwrite mode on iff arg is positive.
4189 In overwrite mode, printing characters typed in replace existing text
4190 on a one-for-one basis, rather than pushing it to the right. At the
4191 end of a line, such characters extend the line. Before a tab,
4192 such characters insert until the tab is filled in.
4193 \\[quoted-insert] still inserts characters in overwrite mode; this
4194 is supposed to make it easier to insert characters when necessary."
4195 (interactive "P")
4196 (setq overwrite-mode
4197 (if (if (null arg) (not overwrite-mode)
4198 (> (prefix-numeric-value arg) 0))
4199 'overwrite-mode-textual))
4200 (force-mode-line-update))
4202 (defun binary-overwrite-mode (arg)
4203 "Toggle binary overwrite mode.
4204 With arg, turn binary overwrite mode on iff arg is positive.
4205 In binary overwrite mode, printing characters typed in replace
4206 existing text. Newlines are not treated specially, so typing at the
4207 end of a line joins the line to the next, with the typed character
4208 between them. Typing before a tab character simply replaces the tab
4209 with the character typed.
4210 \\[quoted-insert] replaces the text at the cursor, just as ordinary
4211 typing characters do.
4213 Note that binary overwrite mode is not its own minor mode; it is a
4214 specialization of overwrite mode, entered by setting the
4215 `overwrite-mode' variable to `overwrite-mode-binary'."
4216 (interactive "P")
4217 (setq overwrite-mode
4218 (if (if (null arg)
4219 (not (eq overwrite-mode 'overwrite-mode-binary))
4220 (> (prefix-numeric-value arg) 0))
4221 'overwrite-mode-binary))
4222 (force-mode-line-update))
4224 (define-minor-mode line-number-mode
4225 "Toggle Line Number mode.
4226 With arg, turn Line Number mode on iff arg is positive.
4227 When Line Number mode is enabled, the line number appears
4228 in the mode line.
4230 Line numbers do not appear for very large buffers and buffers
4231 with very long lines; see variables `line-number-display-limit'
4232 and `line-number-display-limit-width'."
4233 :init-value t :global t :group 'editing-basics :require nil)
4235 (define-minor-mode column-number-mode
4236 "Toggle Column Number mode.
4237 With arg, turn Column Number mode on iff arg is positive.
4238 When Column Number mode is enabled, the column number appears
4239 in the mode line."
4240 :global t :group 'editing-basics :require nil)
4242 (define-minor-mode size-indication-mode
4243 "Toggle Size Indication mode.
4244 With arg, turn Size Indication mode on iff arg is positive. When
4245 Size Indication mode is enabled, the size of the accessible part
4246 of the buffer appears in the mode line."
4247 :global t :group 'editing-basics :require nil)
4249 (defgroup paren-blinking nil
4250 "Blinking matching of parens and expressions."
4251 :prefix "blink-matching-"
4252 :group 'paren-matching)
4254 (defcustom blink-matching-paren t
4255 "*Non-nil means show matching open-paren when close-paren is inserted."
4256 :type 'boolean
4257 :group 'paren-blinking)
4259 (defcustom blink-matching-paren-on-screen t
4260 "*Non-nil means show matching open-paren when it is on screen.
4261 If nil, means don't show it (but the open-paren can still be shown
4262 when it is off screen)."
4263 :type 'boolean
4264 :group 'paren-blinking)
4266 (defcustom blink-matching-paren-distance (* 25 1024)
4267 "*If non-nil, maximum distance to search backwards for matching open-paren.
4268 If nil, search stops at the beginning of the accessible portion of the buffer."
4269 :type '(choice (const nil) integer)
4270 :group 'paren-blinking)
4272 (defcustom blink-matching-delay 1
4273 "*Time in seconds to delay after showing a matching paren."
4274 :type 'number
4275 :group 'paren-blinking)
4277 (defcustom blink-matching-paren-dont-ignore-comments nil
4278 "*Non-nil means `blink-matching-paren' will not ignore comments."
4279 :type 'boolean
4280 :group 'paren-blinking)
4282 (defun blink-matching-open ()
4283 "Move cursor momentarily to the beginning of the sexp before point."
4284 (interactive)
4285 (when (and (> (point) (point-min))
4286 blink-matching-paren
4287 ;; Verify an even number of quoting characters precede the close.
4288 (= 1 (logand 1 (- (point)
4289 (save-excursion
4290 (forward-char -1)
4291 (skip-syntax-backward "/\\")
4292 (point))))))
4293 (let* ((oldpos (point))
4294 blinkpos
4295 message-log-max ; Don't log messages about paren matching.
4296 matching-paren
4297 open-paren-line-string)
4298 (save-excursion
4299 (save-restriction
4300 (if blink-matching-paren-distance
4301 (narrow-to-region (max (point-min)
4302 (- (point) blink-matching-paren-distance))
4303 oldpos))
4304 (condition-case ()
4305 (let ((parse-sexp-ignore-comments
4306 (and parse-sexp-ignore-comments
4307 (not blink-matching-paren-dont-ignore-comments))))
4308 (setq blinkpos (scan-sexps oldpos -1)))
4309 (error nil)))
4310 (and blinkpos
4311 ;; Not syntax '$'.
4312 (not (eq (syntax-class (syntax-after blinkpos)) 8))
4313 (setq matching-paren
4314 (let ((syntax (syntax-after blinkpos)))
4315 (and (consp syntax)
4316 (eq (syntax-class syntax) 4)
4317 (cdr syntax)))))
4318 (cond
4319 ((not (eq matching-paren (char-before oldpos)))
4320 (message "Mismatched parentheses"))
4321 ((not blinkpos)
4322 (if (not blink-matching-paren-distance)
4323 (message "Unmatched parenthesis")))
4324 ((pos-visible-in-window-p blinkpos)
4325 ;; Matching open within window, temporarily move to blinkpos but only
4326 ;; if `blink-matching-paren-on-screen' is non-nil.
4327 (when blink-matching-paren-on-screen
4328 (save-excursion
4329 (goto-char blinkpos)
4330 (sit-for blink-matching-delay))))
4332 (save-excursion
4333 (goto-char blinkpos)
4334 (setq open-paren-line-string
4335 ;; Show what precedes the open in its line, if anything.
4336 (if (save-excursion
4337 (skip-chars-backward " \t")
4338 (not (bolp)))
4339 (buffer-substring (line-beginning-position)
4340 (1+ blinkpos))
4341 ;; Show what follows the open in its line, if anything.
4342 (if (save-excursion
4343 (forward-char 1)
4344 (skip-chars-forward " \t")
4345 (not (eolp)))
4346 (buffer-substring blinkpos
4347 (line-end-position))
4348 ;; Otherwise show the previous nonblank line,
4349 ;; if there is one.
4350 (if (save-excursion
4351 (skip-chars-backward "\n \t")
4352 (not (bobp)))
4353 (concat
4354 (buffer-substring (progn
4355 (skip-chars-backward "\n \t")
4356 (line-beginning-position))
4357 (progn (end-of-line)
4358 (skip-chars-backward " \t")
4359 (point)))
4360 ;; Replace the newline and other whitespace with `...'.
4361 "..."
4362 (buffer-substring blinkpos (1+ blinkpos)))
4363 ;; There is nothing to show except the char itself.
4364 (buffer-substring blinkpos (1+ blinkpos)))))))
4365 (message "Matches %s"
4366 (substring-no-properties open-paren-line-string))))))))
4368 ;Turned off because it makes dbx bomb out.
4369 (setq blink-paren-function 'blink-matching-open)
4371 ;; This executes C-g typed while Emacs is waiting for a command.
4372 ;; Quitting out of a program does not go through here;
4373 ;; that happens in the QUIT macro at the C code level.
4374 (defun keyboard-quit ()
4375 "Signal a `quit' condition.
4376 During execution of Lisp code, this character causes a quit directly.
4377 At top-level, as an editor command, this simply beeps."
4378 (interactive)
4379 (deactivate-mark)
4380 (if (fboundp 'kmacro-keyboard-quit)
4381 (kmacro-keyboard-quit))
4382 (setq defining-kbd-macro nil)
4383 (signal 'quit nil))
4385 (defvar buffer-quit-function nil
4386 "Function to call to \"quit\" the current buffer, or nil if none.
4387 \\[keyboard-escape-quit] calls this function when its more local actions
4388 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4390 (defun keyboard-escape-quit ()
4391 "Exit the current \"mode\" (in a generalized sense of the word).
4392 This command can exit an interactive command such as `query-replace',
4393 can clear out a prefix argument or a region,
4394 can get out of the minibuffer or other recursive edit,
4395 cancel the use of the current buffer (for special-purpose buffers),
4396 or go back to just one window (by deleting all but the selected window)."
4397 (interactive)
4398 (cond ((eq last-command 'mode-exited) nil)
4399 ((> (minibuffer-depth) 0)
4400 (abort-recursive-edit))
4401 (current-prefix-arg
4402 nil)
4403 ((and transient-mark-mode mark-active)
4404 (deactivate-mark))
4405 ((> (recursion-depth) 0)
4406 (exit-recursive-edit))
4407 (buffer-quit-function
4408 (funcall buffer-quit-function))
4409 ((not (one-window-p t))
4410 (delete-other-windows))
4411 ((string-match "^ \\*" (buffer-name (current-buffer)))
4412 (bury-buffer))))
4414 (defun play-sound-file (file &optional volume device)
4415 "Play sound stored in FILE.
4416 VOLUME and DEVICE correspond to the keywords of the sound
4417 specification for `play-sound'."
4418 (interactive "fPlay sound file: ")
4419 (let ((sound (list :file file)))
4420 (if volume
4421 (plist-put sound :volume volume))
4422 (if device
4423 (plist-put sound :device device))
4424 (push 'sound sound)
4425 (play-sound sound)))
4428 (defcustom read-mail-command 'rmail
4429 "*Your preference for a mail reading package.
4430 This is used by some keybindings which support reading mail.
4431 See also `mail-user-agent' concerning sending mail."
4432 :type '(choice (function-item rmail)
4433 (function-item gnus)
4434 (function-item mh-rmail)
4435 (function :tag "Other"))
4436 :version "21.1"
4437 :group 'mail)
4439 (defcustom mail-user-agent 'sendmail-user-agent
4440 "*Your preference for a mail composition package.
4441 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4442 outgoing email message. This variable lets you specify which
4443 mail-sending package you prefer.
4445 Valid values include:
4447 `sendmail-user-agent' -- use the default Emacs Mail package.
4448 See Info node `(emacs)Sending Mail'.
4449 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4450 See Info node `(mh-e)'.
4451 `message-user-agent' -- use the Gnus Message package.
4452 See Info node `(message)'.
4453 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4454 paraphernalia, particularly the Gcc: header for
4455 archiving.
4457 Additional valid symbols may be available; check with the author of
4458 your package for details. The function should return non-nil if it
4459 succeeds.
4461 See also `read-mail-command' concerning reading mail."
4462 :type '(radio (function-item :tag "Default Emacs mail"
4463 :format "%t\n"
4464 sendmail-user-agent)
4465 (function-item :tag "Emacs interface to MH"
4466 :format "%t\n"
4467 mh-e-user-agent)
4468 (function-item :tag "Gnus Message package"
4469 :format "%t\n"
4470 message-user-agent)
4471 (function-item :tag "Gnus Message with full Gnus features"
4472 :format "%t\n"
4473 gnus-user-agent)
4474 (function :tag "Other"))
4475 :group 'mail)
4477 (define-mail-user-agent 'sendmail-user-agent
4478 'sendmail-user-agent-compose
4479 'mail-send-and-exit)
4481 (defun rfc822-goto-eoh ()
4482 ;; Go to header delimiter line in a mail message, following RFC822 rules
4483 (goto-char (point-min))
4484 (when (re-search-forward
4485 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4486 (goto-char (match-beginning 0))))
4488 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4489 switch-function yank-action
4490 send-actions)
4491 (if switch-function
4492 (let ((special-display-buffer-names nil)
4493 (special-display-regexps nil)
4494 (same-window-buffer-names nil)
4495 (same-window-regexps nil))
4496 (funcall switch-function "*mail*")))
4497 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4498 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4499 (body (cdr (assoc-string "body" other-headers t))))
4500 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4501 continue
4502 (error "Message aborted"))
4503 (save-excursion
4504 (rfc822-goto-eoh)
4505 (while other-headers
4506 (unless (member-ignore-case (car (car other-headers))
4507 '("in-reply-to" "cc" "body"))
4508 (insert (car (car other-headers)) ": "
4509 (cdr (car other-headers)) hard-newline))
4510 (setq other-headers (cdr other-headers)))
4511 (when body
4512 (forward-line 1)
4513 (insert body))
4514 t)))
4516 (define-mail-user-agent 'mh-e-user-agent
4517 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
4518 'mh-before-send-letter-hook)
4520 (defun compose-mail (&optional to subject other-headers continue
4521 switch-function yank-action send-actions)
4522 "Start composing a mail message to send.
4523 This uses the user's chosen mail composition package
4524 as selected with the variable `mail-user-agent'.
4525 The optional arguments TO and SUBJECT specify recipients
4526 and the initial Subject field, respectively.
4528 OTHER-HEADERS is an alist specifying additional
4529 header fields. Elements look like (HEADER . VALUE) where both
4530 HEADER and VALUE are strings.
4532 CONTINUE, if non-nil, says to continue editing a message already
4533 being composed.
4535 SWITCH-FUNCTION, if non-nil, is a function to use to
4536 switch to and display the buffer used for mail composition.
4538 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4539 to insert the raw text of the message being replied to.
4540 It has the form (FUNCTION . ARGS). The user agent will apply
4541 FUNCTION to ARGS, to insert the raw text of the original message.
4542 \(The user agent will also run `mail-citation-hook', *after* the
4543 original text has been inserted in this way.)
4545 SEND-ACTIONS is a list of actions to call when the message is sent.
4546 Each action has the form (FUNCTION . ARGS)."
4547 (interactive
4548 (list nil nil nil current-prefix-arg))
4549 (let ((function (get mail-user-agent 'composefunc)))
4550 (funcall function to subject other-headers continue
4551 switch-function yank-action send-actions)))
4553 (defun compose-mail-other-window (&optional to subject other-headers continue
4554 yank-action send-actions)
4555 "Like \\[compose-mail], but edit the outgoing message in another window."
4556 (interactive
4557 (list nil nil nil current-prefix-arg))
4558 (compose-mail to subject other-headers continue
4559 'switch-to-buffer-other-window yank-action send-actions))
4562 (defun compose-mail-other-frame (&optional to subject other-headers continue
4563 yank-action send-actions)
4564 "Like \\[compose-mail], but edit the outgoing message in another frame."
4565 (interactive
4566 (list nil nil nil current-prefix-arg))
4567 (compose-mail to subject other-headers continue
4568 'switch-to-buffer-other-frame yank-action send-actions))
4570 (defvar set-variable-value-history nil
4571 "History of values entered with `set-variable'.")
4573 (defun set-variable (variable value &optional make-local)
4574 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4575 VARIABLE should be a user option variable name, a Lisp variable
4576 meant to be customized by users. You should enter VALUE in Lisp syntax,
4577 so if you want VALUE to be a string, you must surround it with doublequotes.
4578 VALUE is used literally, not evaluated.
4580 If VARIABLE has a `variable-interactive' property, that is used as if
4581 it were the arg to `interactive' (which see) to interactively read VALUE.
4583 If VARIABLE has been defined with `defcustom', then the type information
4584 in the definition is used to check that VALUE is valid.
4586 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4587 (interactive
4588 (let* ((default-var (variable-at-point))
4589 (var (if (user-variable-p default-var)
4590 (read-variable (format "Set variable (default %s): " default-var)
4591 default-var)
4592 (read-variable "Set variable: ")))
4593 (minibuffer-help-form '(describe-variable var))
4594 (prop (get var 'variable-interactive))
4595 (obsolete (car (get var 'byte-obsolete-variable)))
4596 (prompt (format "Set %s %s to value: " var
4597 (cond ((local-variable-p var)
4598 "(buffer-local)")
4599 ((or current-prefix-arg
4600 (local-variable-if-set-p var))
4601 "buffer-locally")
4602 (t "globally"))))
4603 (val (progn
4604 (when obsolete
4605 (message (concat "`%S' is obsolete; "
4606 (if (symbolp obsolete) "use `%S' instead" "%s"))
4607 var obsolete)
4608 (sit-for 3))
4609 (if prop
4610 ;; Use VAR's `variable-interactive' property
4611 ;; as an interactive spec for prompting.
4612 (call-interactively `(lambda (arg)
4613 (interactive ,prop)
4614 arg))
4615 (read
4616 (read-string prompt nil
4617 'set-variable-value-history
4618 (format "%S" (symbol-value var))))))))
4619 (list var val current-prefix-arg)))
4621 (and (custom-variable-p variable)
4622 (not (get variable 'custom-type))
4623 (custom-load-symbol variable))
4624 (let ((type (get variable 'custom-type)))
4625 (when type
4626 ;; Match with custom type.
4627 (require 'cus-edit)
4628 (setq type (widget-convert type))
4629 (unless (widget-apply type :match value)
4630 (error "Value `%S' does not match type %S of %S"
4631 value (car type) variable))))
4633 (if make-local
4634 (make-local-variable variable))
4636 (set variable value)
4638 ;; Force a thorough redisplay for the case that the variable
4639 ;; has an effect on the display, like `tab-width' has.
4640 (force-mode-line-update))
4642 ;; Define the major mode for lists of completions.
4644 (defvar completion-list-mode-map nil
4645 "Local map for completion list buffers.")
4646 (or completion-list-mode-map
4647 (let ((map (make-sparse-keymap)))
4648 (define-key map [mouse-2] 'mouse-choose-completion)
4649 (define-key map [follow-link] 'mouse-face)
4650 (define-key map [down-mouse-2] nil)
4651 (define-key map "\C-m" 'choose-completion)
4652 (define-key map "\e\e\e" 'delete-completion-window)
4653 (define-key map [left] 'previous-completion)
4654 (define-key map [right] 'next-completion)
4655 (setq completion-list-mode-map map)))
4657 ;; Completion mode is suitable only for specially formatted data.
4658 (put 'completion-list-mode 'mode-class 'special)
4660 (defvar completion-reference-buffer nil
4661 "Record the buffer that was current when the completion list was requested.
4662 This is a local variable in the completion list buffer.
4663 Initial value is nil to avoid some compiler warnings.")
4665 (defvar completion-no-auto-exit nil
4666 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4667 This also applies to other functions such as `choose-completion'
4668 and `mouse-choose-completion'.")
4670 (defvar completion-base-size nil
4671 "Number of chars at beginning of minibuffer not involved in completion.
4672 This is a local variable in the completion list buffer
4673 but it talks about the buffer in `completion-reference-buffer'.
4674 If this is nil, it means to compare text to determine which part
4675 of the tail end of the buffer's text is involved in completion.")
4677 (defun delete-completion-window ()
4678 "Delete the completion list window.
4679 Go to the window from which completion was requested."
4680 (interactive)
4681 (let ((buf completion-reference-buffer))
4682 (if (one-window-p t)
4683 (if (window-dedicated-p (selected-window))
4684 (delete-frame (selected-frame)))
4685 (delete-window (selected-window))
4686 (if (get-buffer-window buf)
4687 (select-window (get-buffer-window buf))))))
4689 (defun previous-completion (n)
4690 "Move to the previous item in the completion list."
4691 (interactive "p")
4692 (next-completion (- n)))
4694 (defun next-completion (n)
4695 "Move to the next item in the completion list.
4696 With prefix argument N, move N items (negative N means move backward)."
4697 (interactive "p")
4698 (let ((beg (point-min)) (end (point-max)))
4699 (while (and (> n 0) (not (eobp)))
4700 ;; If in a completion, move to the end of it.
4701 (when (get-text-property (point) 'mouse-face)
4702 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4703 ;; Move to start of next one.
4704 (unless (get-text-property (point) 'mouse-face)
4705 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4706 (setq n (1- n)))
4707 (while (and (< n 0) (not (bobp)))
4708 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4709 ;; If in a completion, move to the start of it.
4710 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4711 (goto-char (previous-single-property-change
4712 (point) 'mouse-face nil beg)))
4713 ;; Move to end of the previous completion.
4714 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4715 (goto-char (previous-single-property-change
4716 (point) 'mouse-face nil beg)))
4717 ;; Move to the start of that one.
4718 (goto-char (previous-single-property-change
4719 (point) 'mouse-face nil beg))
4720 (setq n (1+ n))))))
4722 (defun choose-completion ()
4723 "Choose the completion that point is in or next to."
4724 (interactive)
4725 (let (beg end completion (buffer completion-reference-buffer)
4726 (base-size completion-base-size))
4727 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4728 (setq end (point) beg (1+ (point))))
4729 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4730 (setq end (1- (point)) beg (point)))
4731 (if (null beg)
4732 (error "No completion here"))
4733 (setq beg (previous-single-property-change beg 'mouse-face))
4734 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4735 (setq completion (buffer-substring beg end))
4736 (let ((owindow (selected-window)))
4737 (if (and (one-window-p t 'selected-frame)
4738 (window-dedicated-p (selected-window)))
4739 ;; This is a special buffer's frame
4740 (iconify-frame (selected-frame))
4741 (or (window-dedicated-p (selected-window))
4742 (bury-buffer)))
4743 (select-window owindow))
4744 (choose-completion-string completion buffer base-size)))
4746 ;; Delete the longest partial match for STRING
4747 ;; that can be found before POINT.
4748 (defun choose-completion-delete-max-match (string)
4749 (let ((opoint (point))
4750 len)
4751 ;; Try moving back by the length of the string.
4752 (goto-char (max (- (point) (length string))
4753 (minibuffer-prompt-end)))
4754 ;; See how far back we were actually able to move. That is the
4755 ;; upper bound on how much we can match and delete.
4756 (setq len (- opoint (point)))
4757 (if completion-ignore-case
4758 (setq string (downcase string)))
4759 (while (and (> len 0)
4760 (let ((tail (buffer-substring (point) opoint)))
4761 (if completion-ignore-case
4762 (setq tail (downcase tail)))
4763 (not (string= tail (substring string 0 len)))))
4764 (setq len (1- len))
4765 (forward-char 1))
4766 (delete-char len)))
4768 (defvar choose-completion-string-functions nil
4769 "Functions that may override the normal insertion of a completion choice.
4770 These functions are called in order with four arguments:
4771 CHOICE - the string to insert in the buffer,
4772 BUFFER - the buffer in which the choice should be inserted,
4773 MINI-P - non-nil iff BUFFER is a minibuffer, and
4774 BASE-SIZE - the number of characters in BUFFER before
4775 the string being completed.
4777 If a function in the list returns non-nil, that function is supposed
4778 to have inserted the CHOICE in the BUFFER, and possibly exited
4779 the minibuffer; no further functions will be called.
4781 If all functions in the list return nil, that means to use
4782 the default method of inserting the completion in BUFFER.")
4784 (defun choose-completion-string (choice &optional buffer base-size)
4785 "Switch to BUFFER and insert the completion choice CHOICE.
4786 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4787 to keep. If it is nil, we call `choose-completion-delete-max-match'
4788 to decide what to delete."
4790 ;; If BUFFER is the minibuffer, exit the minibuffer
4791 ;; unless it is reading a file name and CHOICE is a directory,
4792 ;; or completion-no-auto-exit is non-nil.
4794 (let* ((buffer (or buffer completion-reference-buffer))
4795 (mini-p (minibufferp buffer)))
4796 ;; If BUFFER is a minibuffer, barf unless it's the currently
4797 ;; active minibuffer.
4798 (if (and mini-p
4799 (or (not (active-minibuffer-window))
4800 (not (equal buffer
4801 (window-buffer (active-minibuffer-window))))))
4802 (error "Minibuffer is not active for completion")
4803 ;; Set buffer so buffer-local choose-completion-string-functions works.
4804 (set-buffer buffer)
4805 (unless (run-hook-with-args-until-success
4806 'choose-completion-string-functions
4807 choice buffer mini-p base-size)
4808 ;; Insert the completion into the buffer where it was requested.
4809 (if base-size
4810 (delete-region (+ base-size (if mini-p
4811 (minibuffer-prompt-end)
4812 (point-min)))
4813 (point))
4814 (choose-completion-delete-max-match choice))
4815 (insert choice)
4816 (remove-text-properties (- (point) (length choice)) (point)
4817 '(mouse-face nil))
4818 ;; Update point in the window that BUFFER is showing in.
4819 (let ((window (get-buffer-window buffer t)))
4820 (set-window-point window (point)))
4821 ;; If completing for the minibuffer, exit it with this choice.
4822 (and (not completion-no-auto-exit)
4823 (equal buffer (window-buffer (minibuffer-window)))
4824 minibuffer-completion-table
4825 ;; If this is reading a file name, and the file name chosen
4826 ;; is a directory, don't exit the minibuffer.
4827 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4828 (file-directory-p (field-string (point-max))))
4829 (let ((mini (active-minibuffer-window)))
4830 (select-window mini)
4831 (when minibuffer-auto-raise
4832 (raise-frame (window-frame mini))))
4833 (exit-minibuffer)))))))
4835 (defun completion-list-mode ()
4836 "Major mode for buffers showing lists of possible completions.
4837 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4838 to select the completion near point.
4839 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4840 with the mouse."
4841 (interactive)
4842 (kill-all-local-variables)
4843 (use-local-map completion-list-mode-map)
4844 (setq mode-name "Completion List")
4845 (setq major-mode 'completion-list-mode)
4846 (make-local-variable 'completion-base-size)
4847 (setq completion-base-size nil)
4848 (run-mode-hooks 'completion-list-mode-hook))
4850 (defun completion-list-mode-finish ()
4851 "Finish setup of the completions buffer.
4852 Called from `temp-buffer-show-hook'."
4853 (when (eq major-mode 'completion-list-mode)
4854 (toggle-read-only 1)))
4856 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4858 (defvar completion-setup-hook nil
4859 "Normal hook run at the end of setting up a completion list buffer.
4860 When this hook is run, the current buffer is the one in which the
4861 command to display the completion list buffer was run.
4862 The completion list buffer is available as the value of `standard-output'.
4863 The common prefix substring for completion may be available as the
4864 value of `completion-common-substring'. See also `display-completion-list'.")
4867 ;; Variables and faces used in `completion-setup-function'.
4869 (defface completions-first-difference
4870 '((t (:inherit bold)))
4871 "Face put on the first uncommon character in completions in *Completions* buffer."
4872 :group 'completion)
4874 (defface completions-common-part
4875 '((t (:inherit default)))
4876 "Face put on the common prefix substring in completions in *Completions* buffer.
4877 The idea of `completions-common-part' is that you can use it to
4878 make the common parts less visible than normal, so that the rest
4879 of the differing parts is, by contrast, slightly highlighted."
4880 :group 'completion)
4882 ;; This is for packages that need to bind it to a non-default regexp
4883 ;; in order to make the first-differing character highlight work
4884 ;; to their liking
4885 (defvar completion-root-regexp "^/"
4886 "Regexp to use in `completion-setup-function' to find the root directory.")
4888 (defvar completion-common-substring nil
4889 "Common prefix substring to use in `completion-setup-function' to put faces.
4890 The value is set by `display-completion-list' during running `completion-setup-hook'.
4892 To put faces, `completions-first-difference' and `completions-common-part'
4893 into \"*Completions*\* buffer, the common prefix substring in completions is
4894 needed as a hint. (Minibuffer is a special case. The content of minibuffer itself
4895 is the substring.)")
4897 ;; This function goes in completion-setup-hook, so that it is called
4898 ;; after the text of the completion list buffer is written.
4899 (defun completion-setup-function ()
4900 (let* ((mainbuf (current-buffer))
4901 (mbuf-contents (minibuffer-contents))
4902 (common-string-length (length mbuf-contents)))
4903 ;; When reading a file name in the minibuffer,
4904 ;; set default-directory in the minibuffer
4905 ;; so it will get copied into the completion list buffer.
4906 (if minibuffer-completing-file-name
4907 (with-current-buffer mainbuf
4908 (setq default-directory (file-name-directory mbuf-contents))))
4909 ;; If partial-completion-mode is on, point might not be after the
4910 ;; last character in the minibuffer.
4911 ;; FIXME: This hack should be moved to complete.el where we call
4912 ;; display-completion-list.
4913 (when partial-completion-mode
4914 (setq common-string-length
4915 (if (eq (char-after (field-beginning)) ?-)
4916 ;; If the text to be completed starts with a `-', there is no
4917 ;; common prefix.
4918 ;; FIXME: this probably still doesn't do the right thing
4919 ;; when completing file names. It's not even clear what
4920 ;; is TRT.
4922 (- common-string-length (- (point-max) (point))))))
4923 (with-current-buffer standard-output
4924 (completion-list-mode)
4925 (set (make-local-variable 'completion-reference-buffer) mainbuf)
4926 (setq completion-base-size
4927 (if minibuffer-completing-file-name
4928 ;; For file name completion, use the number of chars before
4929 ;; the start of the last file name component.
4930 (with-current-buffer mainbuf
4931 (save-excursion
4932 (goto-char (point-max))
4933 (skip-chars-backward completion-root-regexp)
4934 (- (point) (minibuffer-prompt-end))))
4935 ;; Otherwise, in minibuffer, the whole input is being completed.
4936 (if (minibufferp mainbuf) 0)))
4937 (if (and (symbolp minibuffer-completion-table)
4938 (get minibuffer-completion-table 'completion-base-size-function))
4939 (setq completion-base-size
4940 ;; FIXME: without any extra arg, how is this function
4941 ;; expected to return anything else than a constant unless
4942 ;; it redoes part of the work of all-completions?
4943 ;; In most cases this value would better be computed and
4944 ;; returned at the same time as the list of all-completions
4945 ;; is computed. --Stef
4946 (funcall (get minibuffer-completion-table
4947 'completion-base-size-function))))
4948 ;; Put faces on first uncommon characters and common parts.
4949 (when (or completion-common-substring completion-base-size)
4950 (setq common-string-length
4951 (if completion-common-substring
4952 (length completion-common-substring)
4953 (- common-string-length completion-base-size)))
4954 (let ((element-start (point-min))
4955 (maxp (point-max))
4956 element-common-end)
4957 (while (and (setq element-start
4958 (next-single-property-change
4959 element-start 'mouse-face))
4960 (< (setq element-common-end
4961 (+ element-start common-string-length))
4962 maxp))
4963 (when (and (get-char-property element-start 'mouse-face)
4964 (get-char-property element-common-end 'mouse-face))
4965 (put-text-property element-start element-common-end
4966 'font-lock-face 'completions-common-part)
4967 (put-text-property element-common-end (1+ element-common-end)
4968 'font-lock-face 'completions-first-difference)))))
4969 ;; Insert help string.
4970 (goto-char (point-min))
4971 (if (display-mouse-p)
4972 (insert (substitute-command-keys
4973 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4974 (insert (substitute-command-keys
4975 "In this buffer, type \\[choose-completion] to \
4976 select the completion near point.\n\n")))))
4978 (add-hook 'completion-setup-hook 'completion-setup-function)
4980 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
4981 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
4983 (defun switch-to-completions ()
4984 "Select the completion list window."
4985 (interactive)
4986 ;; Make sure we have a completions window.
4987 (or (get-buffer-window "*Completions*")
4988 (minibuffer-completion-help))
4989 (let ((window (get-buffer-window "*Completions*")))
4990 (when window
4991 (select-window window)
4992 (goto-char (point-min))
4993 (search-forward "\n\n")
4994 (forward-line 1))))
4996 ;; Support keyboard commands to turn on various modifiers.
4998 ;; These functions -- which are not commands -- each add one modifier
4999 ;; to the following event.
5001 (defun event-apply-alt-modifier (ignore-prompt)
5002 "\\<function-key-map>Add the Alt modifier to the following event.
5003 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
5004 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
5005 (defun event-apply-super-modifier (ignore-prompt)
5006 "\\<function-key-map>Add the Super modifier to the following event.
5007 For example, type \\[event-apply-super-modifier] & to enter Super-&."
5008 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
5009 (defun event-apply-hyper-modifier (ignore-prompt)
5010 "\\<function-key-map>Add the Hyper modifier to the following event.
5011 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
5012 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
5013 (defun event-apply-shift-modifier (ignore-prompt)
5014 "\\<function-key-map>Add the Shift modifier to the following event.
5015 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
5016 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
5017 (defun event-apply-control-modifier (ignore-prompt)
5018 "\\<function-key-map>Add the Ctrl modifier to the following event.
5019 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
5020 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
5021 (defun event-apply-meta-modifier (ignore-prompt)
5022 "\\<function-key-map>Add the Meta modifier to the following event.
5023 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
5024 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
5026 (defun event-apply-modifier (event symbol lshiftby prefix)
5027 "Apply a modifier flag to event EVENT.
5028 SYMBOL is the name of this modifier, as a symbol.
5029 LSHIFTBY is the numeric value of this modifier, in keyboard events.
5030 PREFIX is the string that represents this modifier in an event type symbol."
5031 (if (numberp event)
5032 (cond ((eq symbol 'control)
5033 (if (and (<= (downcase event) ?z)
5034 (>= (downcase event) ?a))
5035 (- (downcase event) ?a -1)
5036 (if (and (<= (downcase event) ?Z)
5037 (>= (downcase event) ?A))
5038 (- (downcase event) ?A -1)
5039 (logior (lsh 1 lshiftby) event))))
5040 ((eq symbol 'shift)
5041 (if (and (<= (downcase event) ?z)
5042 (>= (downcase event) ?a))
5043 (upcase event)
5044 (logior (lsh 1 lshiftby) event)))
5046 (logior (lsh 1 lshiftby) event)))
5047 (if (memq symbol (event-modifiers event))
5048 event
5049 (let ((event-type (if (symbolp event) event (car event))))
5050 (setq event-type (intern (concat prefix (symbol-name event-type))))
5051 (if (symbolp event)
5052 event-type
5053 (cons event-type (cdr event)))))))
5055 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
5056 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
5057 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
5058 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
5059 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
5060 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
5062 ;;;; Keypad support.
5064 ;;; Make the keypad keys act like ordinary typing keys. If people add
5065 ;;; bindings for the function key symbols, then those bindings will
5066 ;;; override these, so this shouldn't interfere with any existing
5067 ;;; bindings.
5069 ;; Also tell read-char how to handle these keys.
5070 (mapc
5071 (lambda (keypad-normal)
5072 (let ((keypad (nth 0 keypad-normal))
5073 (normal (nth 1 keypad-normal)))
5074 (put keypad 'ascii-character normal)
5075 (define-key function-key-map (vector keypad) (vector normal))))
5076 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
5077 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
5078 (kp-space ?\s)
5079 (kp-tab ?\t)
5080 (kp-enter ?\r)
5081 (kp-multiply ?*)
5082 (kp-add ?+)
5083 (kp-separator ?,)
5084 (kp-subtract ?-)
5085 (kp-decimal ?.)
5086 (kp-divide ?/)
5087 (kp-equal ?=)))
5089 ;;;;
5090 ;;;; forking a twin copy of a buffer.
5091 ;;;;
5093 (defvar clone-buffer-hook nil
5094 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
5096 (defun clone-process (process &optional newname)
5097 "Create a twin copy of PROCESS.
5098 If NEWNAME is nil, it defaults to PROCESS' name;
5099 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
5100 If PROCESS is associated with a buffer, the new process will be associated
5101 with the current buffer instead.
5102 Returns nil if PROCESS has already terminated."
5103 (setq newname (or newname (process-name process)))
5104 (if (string-match "<[0-9]+>\\'" newname)
5105 (setq newname (substring newname 0 (match-beginning 0))))
5106 (when (memq (process-status process) '(run stop open))
5107 (let* ((process-connection-type (process-tty-name process))
5108 (new-process
5109 (if (memq (process-status process) '(open))
5110 (let ((args (process-contact process t)))
5111 (setq args (plist-put args :name newname))
5112 (setq args (plist-put args :buffer
5113 (if (process-buffer process)
5114 (current-buffer))))
5115 (apply 'make-network-process args))
5116 (apply 'start-process newname
5117 (if (process-buffer process) (current-buffer))
5118 (process-command process)))))
5119 (set-process-query-on-exit-flag
5120 new-process (process-query-on-exit-flag process))
5121 (set-process-inherit-coding-system-flag
5122 new-process (process-inherit-coding-system-flag process))
5123 (set-process-filter new-process (process-filter process))
5124 (set-process-sentinel new-process (process-sentinel process))
5125 (set-process-plist new-process (copy-sequence (process-plist process)))
5126 new-process)))
5128 ;; things to maybe add (currently partly covered by `funcall mode'):
5129 ;; - syntax-table
5130 ;; - overlays
5131 (defun clone-buffer (&optional newname display-flag)
5132 "Create and return a twin copy of the current buffer.
5133 Unlike an indirect buffer, the new buffer can be edited
5134 independently of the old one (if it is not read-only).
5135 NEWNAME is the name of the new buffer. It may be modified by
5136 adding or incrementing <N> at the end as necessary to create a
5137 unique buffer name. If nil, it defaults to the name of the
5138 current buffer, with the proper suffix. If DISPLAY-FLAG is
5139 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
5140 clone a file-visiting buffer, or a buffer whose major mode symbol
5141 has a non-nil `no-clone' property, results in an error.
5143 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
5144 current buffer with appropriate suffix. However, if a prefix
5145 argument is given, then the command prompts for NEWNAME in the
5146 minibuffer.
5148 This runs the normal hook `clone-buffer-hook' in the new buffer
5149 after it has been set up properly in other respects."
5150 (interactive
5151 (progn
5152 (if buffer-file-name
5153 (error "Cannot clone a file-visiting buffer"))
5154 (if (get major-mode 'no-clone)
5155 (error "Cannot clone a buffer in %s mode" mode-name))
5156 (list (if current-prefix-arg (read-string "Name: "))
5157 t)))
5158 (if buffer-file-name
5159 (error "Cannot clone a file-visiting buffer"))
5160 (if (get major-mode 'no-clone)
5161 (error "Cannot clone a buffer in %s mode" mode-name))
5162 (setq newname (or newname (buffer-name)))
5163 (if (string-match "<[0-9]+>\\'" newname)
5164 (setq newname (substring newname 0 (match-beginning 0))))
5165 (let ((buf (current-buffer))
5166 (ptmin (point-min))
5167 (ptmax (point-max))
5168 (pt (point))
5169 (mk (if mark-active (mark t)))
5170 (modified (buffer-modified-p))
5171 (mode major-mode)
5172 (lvars (buffer-local-variables))
5173 (process (get-buffer-process (current-buffer)))
5174 (new (generate-new-buffer (or newname (buffer-name)))))
5175 (save-restriction
5176 (widen)
5177 (with-current-buffer new
5178 (insert-buffer-substring buf)))
5179 (with-current-buffer new
5180 (narrow-to-region ptmin ptmax)
5181 (goto-char pt)
5182 (if mk (set-mark mk))
5183 (set-buffer-modified-p modified)
5185 ;; Clone the old buffer's process, if any.
5186 (when process (clone-process process))
5188 ;; Now set up the major mode.
5189 (funcall mode)
5191 ;; Set up other local variables.
5192 (mapcar (lambda (v)
5193 (condition-case () ;in case var is read-only
5194 (if (symbolp v)
5195 (makunbound v)
5196 (set (make-local-variable (car v)) (cdr v)))
5197 (error nil)))
5198 lvars)
5200 ;; Run any hooks (typically set up by the major mode
5201 ;; for cloning to work properly).
5202 (run-hooks 'clone-buffer-hook))
5203 (if display-flag (pop-to-buffer new))
5204 new))
5207 (defun clone-indirect-buffer (newname display-flag &optional norecord)
5208 "Create an indirect buffer that is a twin copy of the current buffer.
5210 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
5211 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
5212 or if not called with a prefix arg, NEWNAME defaults to the current
5213 buffer's name. The name is modified by adding a `<N>' suffix to it
5214 or by incrementing the N in an existing suffix.
5216 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
5217 This is always done when called interactively.
5219 Optional third arg NORECORD non-nil means do not put this buffer at the
5220 front of the list of recently selected ones."
5221 (interactive
5222 (progn
5223 (if (get major-mode 'no-clone-indirect)
5224 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5225 (list (if current-prefix-arg
5226 (read-string "BName of indirect buffer: "))
5227 t)))
5228 (if (get major-mode 'no-clone-indirect)
5229 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5230 (setq newname (or newname (buffer-name)))
5231 (if (string-match "<[0-9]+>\\'" newname)
5232 (setq newname (substring newname 0 (match-beginning 0))))
5233 (let* ((name (generate-new-buffer-name newname))
5234 (buffer (make-indirect-buffer (current-buffer) name t)))
5235 (when display-flag
5236 (pop-to-buffer buffer norecord))
5237 buffer))
5240 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
5241 "Create an indirect buffer that is a twin copy of BUFFER.
5242 Select the new buffer in another window.
5243 Optional second arg NORECORD non-nil means do not put this buffer at
5244 the front of the list of recently selected ones."
5245 (interactive "bClone buffer in other window: ")
5246 (let ((pop-up-windows t))
5247 (set-buffer buffer)
5248 (clone-indirect-buffer nil t norecord)))
5251 ;;; Handling of Backspace and Delete keys.
5253 (defcustom normal-erase-is-backspace
5254 (and (not noninteractive)
5255 (or (memq system-type '(ms-dos windows-nt))
5256 (eq window-system 'mac)
5257 (and (memq window-system '(x))
5258 (fboundp 'x-backspace-delete-keys-p)
5259 (x-backspace-delete-keys-p))
5260 ;; If the terminal Emacs is running on has erase char
5261 ;; set to ^H, use the Backspace key for deleting
5262 ;; backward and, and the Delete key for deleting forward.
5263 (and (null window-system)
5264 (eq tty-erase-char ?\^H))))
5265 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
5267 On window systems, the default value of this option is chosen
5268 according to the keyboard used. If the keyboard has both a Backspace
5269 key and a Delete key, and both are mapped to their usual meanings, the
5270 option's default value is set to t, so that Backspace can be used to
5271 delete backward, and Delete can be used to delete forward.
5273 If not running under a window system, customizing this option accomplishes
5274 a similar effect by mapping C-h, which is usually generated by the
5275 Backspace key, to DEL, and by mapping DEL to C-d via
5276 `keyboard-translate'. The former functionality of C-h is available on
5277 the F1 key. You should probably not use this setting if you don't
5278 have both Backspace, Delete and F1 keys.
5280 Setting this variable with setq doesn't take effect. Programmatically,
5281 call `normal-erase-is-backspace-mode' (which see) instead."
5282 :type 'boolean
5283 :group 'editing-basics
5284 :version "21.1"
5285 :set (lambda (symbol value)
5286 ;; The fboundp is because of a problem with :set when
5287 ;; dumping Emacs. It doesn't really matter.
5288 (if (fboundp 'normal-erase-is-backspace-mode)
5289 (normal-erase-is-backspace-mode (or value 0))
5290 (set-default symbol value))))
5293 (defun normal-erase-is-backspace-mode (&optional arg)
5294 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
5296 With numeric arg, turn the mode on if and only if ARG is positive.
5298 On window systems, when this mode is on, Delete is mapped to C-d and
5299 Backspace is mapped to DEL; when this mode is off, both Delete and
5300 Backspace are mapped to DEL. (The remapping goes via
5301 `function-key-map', so binding Delete or Backspace in the global or
5302 local keymap will override that.)
5304 In addition, on window systems, the bindings of C-Delete, M-Delete,
5305 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
5306 the global keymap in accordance with the functionality of Delete and
5307 Backspace. For example, if Delete is remapped to C-d, which deletes
5308 forward, C-Delete is bound to `kill-word', but if Delete is remapped
5309 to DEL, which deletes backward, C-Delete is bound to
5310 `backward-kill-word'.
5312 If not running on a window system, a similar effect is accomplished by
5313 remapping C-h (normally produced by the Backspace key) and DEL via
5314 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
5315 to C-d; if it's off, the keys are not remapped.
5317 When not running on a window system, and this mode is turned on, the
5318 former functionality of C-h is available on the F1 key. You should
5319 probably not turn on this mode on a text-only terminal if you don't
5320 have both Backspace, Delete and F1 keys.
5322 See also `normal-erase-is-backspace'."
5323 (interactive "P")
5324 (setq normal-erase-is-backspace
5325 (if arg
5326 (> (prefix-numeric-value arg) 0)
5327 (not normal-erase-is-backspace)))
5329 (cond ((or (memq window-system '(x w32 mac pc))
5330 (memq system-type '(ms-dos windows-nt)))
5331 (let ((bindings
5332 `(([C-delete] [C-backspace])
5333 ([M-delete] [M-backspace])
5334 ([C-M-delete] [C-M-backspace])
5335 (,esc-map
5336 [C-delete] [C-backspace])))
5337 (old-state (lookup-key function-key-map [delete])))
5339 (if normal-erase-is-backspace
5340 (progn
5341 (define-key function-key-map [delete] [?\C-d])
5342 (define-key function-key-map [kp-delete] [?\C-d])
5343 (define-key function-key-map [backspace] [?\C-?]))
5344 (define-key function-key-map [delete] [?\C-?])
5345 (define-key function-key-map [kp-delete] [?\C-?])
5346 (define-key function-key-map [backspace] [?\C-?]))
5348 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5349 (unless (equal old-state (lookup-key function-key-map [delete]))
5350 (dolist (binding bindings)
5351 (let ((map global-map))
5352 (when (keymapp (car binding))
5353 (setq map (car binding) binding (cdr binding)))
5354 (let* ((key1 (nth 0 binding))
5355 (key2 (nth 1 binding))
5356 (binding1 (lookup-key map key1))
5357 (binding2 (lookup-key map key2)))
5358 (define-key map key1 binding2)
5359 (define-key map key2 binding1)))))))
5361 (if normal-erase-is-backspace
5362 (progn
5363 (keyboard-translate ?\C-h ?\C-?)
5364 (keyboard-translate ?\C-? ?\C-d))
5365 (keyboard-translate ?\C-h ?\C-h)
5366 (keyboard-translate ?\C-? ?\C-?))))
5368 (run-hooks 'normal-erase-is-backspace-hook)
5369 (if (interactive-p)
5370 (message "Delete key deletes %s"
5371 (if normal-erase-is-backspace "forward" "backward"))))
5373 (defvar vis-mode-saved-buffer-invisibility-spec nil
5374 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5376 (define-minor-mode visible-mode
5377 "Toggle Visible mode.
5378 With argument ARG turn Visible mode on iff ARG is positive.
5380 Enabling Visible mode makes all invisible text temporarily visible.
5381 Disabling Visible mode turns off that effect. Visible mode
5382 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5383 :lighter " Vis"
5384 :group 'editing-basics
5385 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5386 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5387 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5388 (when visible-mode
5389 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5390 buffer-invisibility-spec)
5391 (setq buffer-invisibility-spec nil)))
5393 ;; Minibuffer prompt stuff.
5395 ;(defun minibuffer-prompt-modification (start end)
5396 ; (error "You cannot modify the prompt"))
5399 ;(defun minibuffer-prompt-insertion (start end)
5400 ; (let ((inhibit-modification-hooks t))
5401 ; (delete-region start end)
5402 ; ;; Discard undo information for the text insertion itself
5403 ; ;; and for the text deletion.above.
5404 ; (when (consp buffer-undo-list)
5405 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5406 ; (message "You cannot modify the prompt")))
5409 ;(setq minibuffer-prompt-properties
5410 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5411 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5414 (provide 'simple)
5416 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5417 ;;; simple.el ends here