*** empty log message ***
[emacs.git] / lisp / simple.el
blob93f6329a255f0975ebe61f23c6e18cb49878bbca
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 input-method-function)
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 (if (char-table-p translation-table-for-input)
2745 (setq char (or (aref translation-table-for-input char) char)))
2746 (kill-region (point) (progn
2747 (search-forward (char-to-string char) nil nil arg)
2748 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2749 (point))))
2751 ;; kill-line and its subroutines.
2753 (defcustom kill-whole-line nil
2754 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2755 :type 'boolean
2756 :group 'killing)
2758 (defun kill-line (&optional arg)
2759 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2760 With prefix argument, kill that many lines from point.
2761 Negative arguments kill lines backward.
2762 With zero argument, kills the text before point on the current line.
2764 When calling from a program, nil means \"no arg\",
2765 a number counts as a prefix arg.
2767 To kill a whole line, when point is not at the beginning, type \
2768 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2770 If `kill-whole-line' is non-nil, then this command kills the whole line
2771 including its terminating newline, when used at the beginning of a line
2772 with no argument. As a consequence, you can always kill a whole line
2773 by typing \\[beginning-of-line] \\[kill-line].
2775 If you want to append the killed line to the last killed text,
2776 use \\[append-next-kill] before \\[kill-line].
2778 If the buffer is read-only, Emacs will beep and refrain from deleting
2779 the line, but put the line in the kill ring anyway. This means that
2780 you can use this command to copy text from a read-only buffer.
2781 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2782 even beep.)"
2783 (interactive "P")
2784 (kill-region (point)
2785 ;; It is better to move point to the other end of the kill
2786 ;; before killing. That way, in a read-only buffer, point
2787 ;; moves across the text that is copied to the kill ring.
2788 ;; The choice has no effect on undo now that undo records
2789 ;; the value of point from before the command was run.
2790 (progn
2791 (if arg
2792 (forward-visible-line (prefix-numeric-value arg))
2793 (if (eobp)
2794 (signal 'end-of-buffer nil))
2795 (let ((end
2796 (save-excursion
2797 (end-of-visible-line) (point))))
2798 (if (or (save-excursion
2799 ;; If trailing whitespace is visible,
2800 ;; don't treat it as nothing.
2801 (unless show-trailing-whitespace
2802 (skip-chars-forward " \t" end))
2803 (= (point) end))
2804 (and kill-whole-line (bolp)))
2805 (forward-visible-line 1)
2806 (goto-char end))))
2807 (point))))
2809 (defun kill-whole-line (&optional arg)
2810 "Kill current line.
2811 With prefix arg, kill that many lines starting from the current line.
2812 If arg is negative, kill backward. Also kill the preceding newline.
2813 \(This is meant to make \\[repeat] work well with negative arguments.\)
2814 If arg is zero, kill current line but exclude the trailing newline."
2815 (interactive "p")
2816 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2817 (signal 'end-of-buffer nil))
2818 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2819 (signal 'beginning-of-buffer nil))
2820 (unless (eq last-command 'kill-region)
2821 (kill-new "")
2822 (setq last-command 'kill-region))
2823 (cond ((zerop arg)
2824 ;; We need to kill in two steps, because the previous command
2825 ;; could have been a kill command, in which case the text
2826 ;; before point needs to be prepended to the current kill
2827 ;; ring entry and the text after point appended. Also, we
2828 ;; need to use save-excursion to avoid copying the same text
2829 ;; twice to the kill ring in read-only buffers.
2830 (save-excursion
2831 (kill-region (point) (progn (forward-visible-line 0) (point))))
2832 (kill-region (point) (progn (end-of-visible-line) (point))))
2833 ((< arg 0)
2834 (save-excursion
2835 (kill-region (point) (progn (end-of-visible-line) (point))))
2836 (kill-region (point)
2837 (progn (forward-visible-line (1+ arg))
2838 (unless (bobp) (backward-char))
2839 (point))))
2841 (save-excursion
2842 (kill-region (point) (progn (forward-visible-line 0) (point))))
2843 (kill-region (point)
2844 (progn (forward-visible-line arg) (point))))))
2846 (defun forward-visible-line (arg)
2847 "Move forward by ARG lines, ignoring currently invisible newlines only.
2848 If ARG is negative, move backward -ARG lines.
2849 If ARG is zero, move to the beginning of the current line."
2850 (condition-case nil
2851 (if (> arg 0)
2852 (progn
2853 (while (> arg 0)
2854 (or (zerop (forward-line 1))
2855 (signal 'end-of-buffer nil))
2856 ;; If the newline we just skipped is invisible,
2857 ;; don't count it.
2858 (let ((prop
2859 (get-char-property (1- (point)) 'invisible)))
2860 (if (if (eq buffer-invisibility-spec t)
2861 prop
2862 (or (memq prop buffer-invisibility-spec)
2863 (assq prop buffer-invisibility-spec)))
2864 (setq arg (1+ arg))))
2865 (setq arg (1- arg)))
2866 ;; If invisible text follows, and it is a number of complete lines,
2867 ;; skip it.
2868 (let ((opoint (point)))
2869 (while (and (not (eobp))
2870 (let ((prop
2871 (get-char-property (point) 'invisible)))
2872 (if (eq buffer-invisibility-spec t)
2873 prop
2874 (or (memq prop buffer-invisibility-spec)
2875 (assq prop buffer-invisibility-spec)))))
2876 (goto-char
2877 (if (get-text-property (point) 'invisible)
2878 (or (next-single-property-change (point) 'invisible)
2879 (point-max))
2880 (next-overlay-change (point)))))
2881 (unless (bolp)
2882 (goto-char opoint))))
2883 (let ((first t))
2884 (while (or first (<= arg 0))
2885 (if first
2886 (beginning-of-line)
2887 (or (zerop (forward-line -1))
2888 (signal 'beginning-of-buffer nil)))
2889 ;; If the newline we just moved to is invisible,
2890 ;; don't count it.
2891 (unless (bobp)
2892 (let ((prop
2893 (get-char-property (1- (point)) 'invisible)))
2894 (unless (if (eq buffer-invisibility-spec t)
2895 prop
2896 (or (memq prop buffer-invisibility-spec)
2897 (assq prop buffer-invisibility-spec)))
2898 (setq arg (1+ arg)))))
2899 (setq first nil))
2900 ;; If invisible text follows, and it is a number of complete lines,
2901 ;; skip it.
2902 (let ((opoint (point)))
2903 (while (and (not (bobp))
2904 (let ((prop
2905 (get-char-property (1- (point)) 'invisible)))
2906 (if (eq buffer-invisibility-spec t)
2907 prop
2908 (or (memq prop buffer-invisibility-spec)
2909 (assq prop buffer-invisibility-spec)))))
2910 (goto-char
2911 (if (get-text-property (1- (point)) 'invisible)
2912 (or (previous-single-property-change (point) 'invisible)
2913 (point-min))
2914 (previous-overlay-change (point)))))
2915 (unless (bolp)
2916 (goto-char opoint)))))
2917 ((beginning-of-buffer end-of-buffer)
2918 nil)))
2920 (defun end-of-visible-line ()
2921 "Move to end of current visible line."
2922 (end-of-line)
2923 ;; If the following character is currently invisible,
2924 ;; skip all characters with that same `invisible' property value,
2925 ;; then find the next newline.
2926 (while (and (not (eobp))
2927 (save-excursion
2928 (skip-chars-forward "^\n")
2929 (let ((prop
2930 (get-char-property (point) 'invisible)))
2931 (if (eq buffer-invisibility-spec t)
2932 prop
2933 (or (memq prop buffer-invisibility-spec)
2934 (assq prop buffer-invisibility-spec))))))
2935 (skip-chars-forward "^\n")
2936 (if (get-text-property (point) 'invisible)
2937 (goto-char (next-single-property-change (point) 'invisible))
2938 (goto-char (next-overlay-change (point))))
2939 (end-of-line)))
2941 (defun insert-buffer (buffer)
2942 "Insert after point the contents of BUFFER.
2943 Puts mark after the inserted text.
2944 BUFFER may be a buffer or a buffer name.
2946 This function is meant for the user to run interactively.
2947 Don't call it from programs: use `insert-buffer-substring' instead!"
2948 (interactive
2949 (list
2950 (progn
2951 (barf-if-buffer-read-only)
2952 (read-buffer "Insert buffer: "
2953 (if (eq (selected-window) (next-window (selected-window)))
2954 (other-buffer (current-buffer))
2955 (window-buffer (next-window (selected-window))))
2956 t))))
2957 (push-mark
2958 (save-excursion
2959 (insert-buffer-substring (get-buffer buffer))
2960 (point)))
2961 nil)
2963 (defun append-to-buffer (buffer start end)
2964 "Append to specified buffer the text of the region.
2965 It is inserted into that buffer before its point.
2967 When calling from a program, give three arguments:
2968 BUFFER (or buffer name), START and END.
2969 START and END specify the portion of the current buffer to be copied."
2970 (interactive
2971 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2972 (region-beginning) (region-end)))
2973 (let ((oldbuf (current-buffer)))
2974 (save-excursion
2975 (let* ((append-to (get-buffer-create buffer))
2976 (windows (get-buffer-window-list append-to t t))
2977 point)
2978 (set-buffer append-to)
2979 (setq point (point))
2980 (barf-if-buffer-read-only)
2981 (insert-buffer-substring oldbuf start end)
2982 (dolist (window windows)
2983 (when (= (window-point window) point)
2984 (set-window-point window (point))))))))
2986 (defun prepend-to-buffer (buffer start end)
2987 "Prepend to specified buffer the text of the region.
2988 It is inserted into that buffer after its point.
2990 When calling from a program, give three arguments:
2991 BUFFER (or buffer name), START and END.
2992 START and END specify the portion of the current buffer to be copied."
2993 (interactive "BPrepend to buffer: \nr")
2994 (let ((oldbuf (current-buffer)))
2995 (save-excursion
2996 (set-buffer (get-buffer-create buffer))
2997 (barf-if-buffer-read-only)
2998 (save-excursion
2999 (insert-buffer-substring oldbuf start end)))))
3001 (defun copy-to-buffer (buffer start end)
3002 "Copy to specified buffer the text of the region.
3003 It is inserted into that buffer, replacing existing text there.
3005 When calling from a program, give three arguments:
3006 BUFFER (or buffer name), START and END.
3007 START and END specify the portion of the current buffer to be copied."
3008 (interactive "BCopy to buffer: \nr")
3009 (let ((oldbuf (current-buffer)))
3010 (with-current-buffer (get-buffer-create buffer)
3011 (barf-if-buffer-read-only)
3012 (erase-buffer)
3013 (save-excursion
3014 (insert-buffer-substring oldbuf start end)))))
3016 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3017 (put 'mark-inactive 'error-message "The mark is not active now")
3019 (defvar activate-mark-hook nil
3020 "Hook run when the mark becomes active.
3021 It is also run at the end of a command, if the mark is active and
3022 it is possible that the region may have changed")
3024 (defvar deactivate-mark-hook nil
3025 "Hook run when the mark becomes inactive.")
3027 (defun mark (&optional force)
3028 "Return this buffer's mark value as integer; error if mark inactive.
3029 If optional argument FORCE is non-nil, access the mark value
3030 even if the mark is not currently active, and return nil
3031 if there is no mark at all.
3033 If you are using this in an editing command, you are most likely making
3034 a mistake; see the documentation of `set-mark'."
3035 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3036 (marker-position (mark-marker))
3037 (signal 'mark-inactive nil)))
3039 ;; Many places set mark-active directly, and several of them failed to also
3040 ;; run deactivate-mark-hook. This shorthand should simplify.
3041 (defsubst deactivate-mark ()
3042 "Deactivate the mark by setting `mark-active' to nil.
3043 \(That makes a difference only in Transient Mark mode.)
3044 Also runs the hook `deactivate-mark-hook'."
3045 (cond
3046 ((eq transient-mark-mode 'lambda)
3047 (setq transient-mark-mode nil))
3048 (transient-mark-mode
3049 (setq mark-active nil)
3050 (run-hooks 'deactivate-mark-hook))))
3052 (defun set-mark (pos)
3053 "Set this buffer's mark to POS. Don't use this function!
3054 That is to say, don't use this function unless you want
3055 the user to see that the mark has moved, and you want the previous
3056 mark position to be lost.
3058 Normally, when a new mark is set, the old one should go on the stack.
3059 This is why most applications should use `push-mark', not `set-mark'.
3061 Novice Emacs Lisp programmers often try to use the mark for the wrong
3062 purposes. The mark saves a location for the user's convenience.
3063 Most editing commands should not alter the mark.
3064 To remember a location for internal use in the Lisp program,
3065 store it in a Lisp variable. Example:
3067 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3069 (if pos
3070 (progn
3071 (setq mark-active t)
3072 (run-hooks 'activate-mark-hook)
3073 (set-marker (mark-marker) pos (current-buffer)))
3074 ;; Normally we never clear mark-active except in Transient Mark mode.
3075 ;; But when we actually clear out the mark value too,
3076 ;; we must clear mark-active in any mode.
3077 (setq mark-active nil)
3078 (run-hooks 'deactivate-mark-hook)
3079 (set-marker (mark-marker) nil)))
3081 (defvar mark-ring nil
3082 "The list of former marks of the current buffer, most recent first.")
3083 (make-variable-buffer-local 'mark-ring)
3084 (put 'mark-ring 'permanent-local t)
3086 (defcustom mark-ring-max 16
3087 "*Maximum size of mark ring. Start discarding off end if gets this big."
3088 :type 'integer
3089 :group 'editing-basics)
3091 (defvar global-mark-ring nil
3092 "The list of saved global marks, most recent first.")
3094 (defcustom global-mark-ring-max 16
3095 "*Maximum size of global mark ring. \
3096 Start discarding off end if gets this big."
3097 :type 'integer
3098 :group 'editing-basics)
3100 (defun pop-to-mark-command ()
3101 "Jump to mark, and pop a new position for mark off the ring
3102 \(does not affect global mark ring\)."
3103 (interactive)
3104 (if (null (mark t))
3105 (error "No mark set in this buffer")
3106 (goto-char (mark t))
3107 (pop-mark)))
3109 (defun push-mark-command (arg &optional nomsg)
3110 "Set mark at where point is.
3111 If no prefix arg and mark is already set there, just activate it.
3112 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3113 (interactive "P")
3114 (let ((mark (marker-position (mark-marker))))
3115 (if (or arg (null mark) (/= mark (point)))
3116 (push-mark nil nomsg t)
3117 (setq mark-active t)
3118 (run-hooks 'activate-mark-hook)
3119 (unless nomsg
3120 (message "Mark activated")))))
3122 (defcustom set-mark-command-repeat-pop nil
3123 "*Non-nil means that repeating \\[set-mark-command] after popping will pop.
3124 This means that if you type C-u \\[set-mark-command] \\[set-mark-command]
3125 will pop twice."
3126 :type 'boolean
3127 :group 'editing)
3129 (defun set-mark-command (arg)
3130 "Set mark at where point is, or jump to mark.
3131 With no prefix argument, set mark, and push old mark position on local
3132 mark ring; also push mark on global mark ring if last mark was set in
3133 another buffer. Immediately repeating the command activates
3134 `transient-mark-mode' temporarily.
3136 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
3137 jump to mark, and pop a new position
3138 for mark off the local mark ring \(this does not affect the global
3139 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
3140 mark ring \(see `pop-global-mark'\).
3142 Repeating the \\[set-mark-command] command without the prefix jumps to
3143 the next position off the local (or global) mark ring.
3145 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
3146 \\[universal-argument] \\[set-mark-command], unconditionally
3147 set mark where point is.
3149 Novice Emacs Lisp programmers often try to use the mark for the wrong
3150 purposes. See the documentation of `set-mark' for more information."
3151 (interactive "P")
3152 (if (eq transient-mark-mode 'lambda)
3153 (setq transient-mark-mode nil))
3154 (cond
3155 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3156 (push-mark-command nil))
3157 ((not (eq this-command 'set-mark-command))
3158 (if arg
3159 (pop-to-mark-command)
3160 (push-mark-command t)))
3161 ((and set-mark-command-repeat-pop
3162 (eq last-command 'pop-to-mark-command))
3163 (setq this-command 'pop-to-mark-command)
3164 (pop-to-mark-command))
3165 ((and set-mark-command-repeat-pop
3166 (eq last-command 'pop-global-mark)
3167 (not arg))
3168 (setq this-command 'pop-global-mark)
3169 (pop-global-mark))
3170 (arg
3171 (setq this-command 'pop-to-mark-command)
3172 (pop-to-mark-command))
3173 ((and (eq last-command 'set-mark-command)
3174 mark-active (null transient-mark-mode))
3175 (setq transient-mark-mode 'lambda)
3176 (message "Transient-mark-mode temporarily enabled"))
3178 (push-mark-command nil))))
3180 (defun push-mark (&optional location nomsg activate)
3181 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3182 If the last global mark pushed was not in the current buffer,
3183 also push LOCATION on the global mark ring.
3184 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3185 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
3187 Novice Emacs Lisp programmers often try to use the mark for the wrong
3188 purposes. See the documentation of `set-mark' for more information.
3190 In Transient Mark mode, this does not activate the mark."
3191 (unless (null (mark t))
3192 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3193 (when (> (length mark-ring) mark-ring-max)
3194 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3195 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3196 (set-marker (mark-marker) (or location (point)) (current-buffer))
3197 ;; Now push the mark on the global mark ring.
3198 (if (and global-mark-ring
3199 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3200 ;; The last global mark pushed was in this same buffer.
3201 ;; Don't push another one.
3203 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3204 (when (> (length global-mark-ring) global-mark-ring-max)
3205 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3206 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3207 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3208 (message "Mark set"))
3209 (if (or activate (not transient-mark-mode))
3210 (set-mark (mark t)))
3211 nil)
3213 (defun pop-mark ()
3214 "Pop off mark ring into the buffer's actual mark.
3215 Does not set point. Does nothing if mark ring is empty."
3216 (when mark-ring
3217 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3218 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3219 (move-marker (car mark-ring) nil)
3220 (if (null (mark t)) (ding))
3221 (setq mark-ring (cdr mark-ring)))
3222 (deactivate-mark))
3224 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3225 (defun exchange-point-and-mark (&optional arg)
3226 "Put the mark where point is now, and point where the mark is now.
3227 This command works even when the mark is not active,
3228 and it reactivates the mark.
3229 With prefix arg, `transient-mark-mode' is enabled temporarily."
3230 (interactive "P")
3231 (if arg
3232 (if mark-active
3233 (if (null transient-mark-mode)
3234 (setq transient-mark-mode 'lambda))
3235 (setq arg nil)))
3236 (unless arg
3237 (let ((omark (mark t)))
3238 (if (null omark)
3239 (error "No mark set in this buffer"))
3240 (set-mark (point))
3241 (goto-char omark)
3242 nil)))
3244 (define-minor-mode transient-mark-mode
3245 "Toggle Transient Mark mode.
3246 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3248 In Transient Mark mode, when the mark is active, the region is highlighted.
3249 Changing the buffer \"deactivates\" the mark.
3250 So do certain other operations that set the mark
3251 but whose main purpose is something else--for example,
3252 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3254 You can also deactivate the mark by typing \\[keyboard-quit] or
3255 \\[keyboard-escape-quit].
3257 Many commands change their behavior when Transient Mark mode is in effect
3258 and the mark is active, by acting on the region instead of their usual
3259 default part of the buffer's text. Examples of such commands include
3260 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3261 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3262 Invoke \\[apropos-documentation] and type \"transient\" or
3263 \"mark.*active\" at the prompt, to see the documentation of
3264 commands which are sensitive to the Transient Mark mode."
3265 :global t :group 'editing-basics)
3267 (defvar widen-automatically t
3268 "Non-nil means it is ok for commands to call `widen' when they want to.
3269 Some commands will do this in order to go to positions outside
3270 the current accessible part of the buffer.
3272 If `widen-automatically' is nil, these commands will do something else
3273 as a fallback, and won't change the buffer bounds.")
3275 (defun pop-global-mark ()
3276 "Pop off global mark ring and jump to the top location."
3277 (interactive)
3278 ;; Pop entries which refer to non-existent buffers.
3279 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3280 (setq global-mark-ring (cdr global-mark-ring)))
3281 (or global-mark-ring
3282 (error "No global mark set"))
3283 (let* ((marker (car global-mark-ring))
3284 (buffer (marker-buffer marker))
3285 (position (marker-position marker)))
3286 (setq global-mark-ring (nconc (cdr global-mark-ring)
3287 (list (car global-mark-ring))))
3288 (set-buffer buffer)
3289 (or (and (>= position (point-min))
3290 (<= position (point-max)))
3291 (if widen-automatically
3292 (widen)
3293 (error "Global mark position is outside accessible part of buffer")))
3294 (goto-char position)
3295 (switch-to-buffer buffer)))
3297 (defcustom next-line-add-newlines nil
3298 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3299 :type 'boolean
3300 :version "21.1"
3301 :group 'editing-basics)
3303 (defun next-line (&optional arg try-vscroll)
3304 "Move cursor vertically down ARG lines.
3305 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3306 If there is no character in the target line exactly under the current column,
3307 the cursor is positioned after the character in that line which spans this
3308 column, or at the end of the line if it is not long enough.
3309 If there is no line in the buffer after this one, behavior depends on the
3310 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3311 to create a line, and moves the cursor to that line. Otherwise it moves the
3312 cursor to the end of the buffer.
3314 The command \\[set-goal-column] can be used to create
3315 a semipermanent goal column for this command.
3316 Then instead of trying to move exactly vertically (or as close as possible),
3317 this command moves to the specified goal column (or as close as possible).
3318 The goal column is stored in the variable `goal-column', which is nil
3319 when there is no goal column.
3321 If you are thinking of using this in a Lisp program, consider
3322 using `forward-line' instead. It is usually easier to use
3323 and more reliable (no dependence on goal column, etc.)."
3324 (interactive "p\np")
3325 (or arg (setq arg 1))
3326 (if (and next-line-add-newlines (= arg 1))
3327 (if (save-excursion (end-of-line) (eobp))
3328 ;; When adding a newline, don't expand an abbrev.
3329 (let ((abbrev-mode nil))
3330 (end-of-line)
3331 (insert (if use-hard-newlines hard-newline "\n")))
3332 (line-move arg nil nil try-vscroll))
3333 (if (interactive-p)
3334 (condition-case nil
3335 (line-move arg nil nil try-vscroll)
3336 ((beginning-of-buffer end-of-buffer) (ding)))
3337 (line-move arg nil nil try-vscroll)))
3338 nil)
3340 (defun previous-line (&optional arg try-vscroll)
3341 "Move cursor vertically up ARG lines.
3342 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3343 If there is no character in the target line exactly over the current column,
3344 the cursor is positioned after the character in that line which spans this
3345 column, or at the end of the line if it is not long enough.
3347 The command \\[set-goal-column] can be used to create
3348 a semipermanent goal column for this command.
3349 Then instead of trying to move exactly vertically (or as close as possible),
3350 this command moves to the specified goal column (or as close as possible).
3351 The goal column is stored in the variable `goal-column', which is nil
3352 when there is no goal column.
3354 If you are thinking of using this in a Lisp program, consider using
3355 `forward-line' with a negative argument instead. It is usually easier
3356 to use and more reliable (no dependence on goal column, etc.)."
3357 (interactive "p\np")
3358 (or arg (setq arg 1))
3359 (if (interactive-p)
3360 (condition-case nil
3361 (line-move (- arg) nil nil try-vscroll)
3362 ((beginning-of-buffer end-of-buffer) (ding)))
3363 (line-move (- arg) nil nil try-vscroll))
3364 nil)
3366 (defcustom track-eol nil
3367 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3368 This means moving to the end of each line moved onto.
3369 The beginning of a blank line does not count as the end of a line."
3370 :type 'boolean
3371 :group 'editing-basics)
3373 (defcustom goal-column nil
3374 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3375 :type '(choice integer
3376 (const :tag "None" nil))
3377 :group 'editing-basics)
3378 (make-variable-buffer-local 'goal-column)
3380 (defvar temporary-goal-column 0
3381 "Current goal column for vertical motion.
3382 It is the column where point was
3383 at the start of current run of vertical motion commands.
3384 When the `track-eol' feature is doing its job, the value is 9999.")
3386 (defcustom line-move-ignore-invisible t
3387 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3388 Outline mode sets this."
3389 :type 'boolean
3390 :group 'editing-basics)
3392 (defun line-move-invisible-p (pos)
3393 "Return non-nil if the character after POS is currently invisible."
3394 (let ((prop
3395 (get-char-property pos 'invisible)))
3396 (if (eq buffer-invisibility-spec t)
3397 prop
3398 (or (memq prop buffer-invisibility-spec)
3399 (assq prop buffer-invisibility-spec)))))
3401 ;; This is like line-move-1 except that it also performs
3402 ;; vertical scrolling of tall images if appropriate.
3403 ;; That is not really a clean thing to do, since it mixes
3404 ;; scrolling with cursor motion. But so far we don't have
3405 ;; a cleaner solution to the problem of making C-n do something
3406 ;; useful given a tall image.
3407 (defun line-move (arg &optional noerror to-end try-vscroll)
3408 (if (and auto-window-vscroll try-vscroll
3409 ;; But don't vscroll in a keyboard macro.
3410 (not defining-kbd-macro)
3411 (not executing-kbd-macro))
3412 (let ((forward (> arg 0))
3413 (part (nth 2 (pos-visible-in-window-p (point) nil t))))
3414 (if (and (consp part)
3415 (> (if forward (cdr part) (car part)) 0))
3416 (set-window-vscroll nil
3417 (if forward
3418 (+ (window-vscroll nil t)
3419 (min (cdr part)
3420 (* (frame-char-height) arg)))
3421 (max 0
3422 (- (window-vscroll nil t)
3423 (min (car part)
3424 (* (frame-char-height) (- arg))))))
3426 (set-window-vscroll nil 0)
3427 (when (line-move-1 arg noerror to-end)
3428 (when (not forward)
3429 ;; Update display before calling pos-visible-in-window-p,
3430 ;; because it depends on window-start being up-to-date.
3431 (sit-for 0)
3432 ;; If the current line is partly hidden at the bottom,
3433 ;; scroll it partially up so as to unhide the bottom.
3434 (if (and (setq part (nth 2 (pos-visible-in-window-p
3435 (line-beginning-position) nil t)))
3436 (> (cdr part) 0))
3437 (set-window-vscroll nil (cdr part) t)))
3438 t)))
3439 (line-move-1 arg noerror to-end)))
3441 ;; This is the guts of next-line and previous-line.
3442 ;; Arg says how many lines to move.
3443 ;; The value is t if we can move the specified number of lines.
3444 (defun line-move-1 (arg &optional noerror to-end)
3445 ;; Don't run any point-motion hooks, and disregard intangibility,
3446 ;; for intermediate positions.
3447 (let ((inhibit-point-motion-hooks t)
3448 (opoint (point))
3449 (forward (> arg 0)))
3450 (unwind-protect
3451 (progn
3452 (if (not (memq last-command '(next-line previous-line)))
3453 (setq temporary-goal-column
3454 (if (and track-eol (eolp)
3455 ;; Don't count beg of empty line as end of line
3456 ;; unless we just did explicit end-of-line.
3457 (or (not (bolp)) (eq last-command 'end-of-line)))
3458 9999
3459 (current-column))))
3461 (if (and (not (integerp selective-display))
3462 (not line-move-ignore-invisible))
3463 ;; Use just newline characters.
3464 ;; Set ARG to 0 if we move as many lines as requested.
3465 (or (if (> arg 0)
3466 (progn (if (> arg 1) (forward-line (1- arg)))
3467 ;; This way of moving forward ARG lines
3468 ;; verifies that we have a newline after the last one.
3469 ;; It doesn't get confused by intangible text.
3470 (end-of-line)
3471 (if (zerop (forward-line 1))
3472 (setq arg 0)))
3473 (and (zerop (forward-line arg))
3474 (bolp)
3475 (setq arg 0)))
3476 (unless noerror
3477 (signal (if (< arg 0)
3478 'beginning-of-buffer
3479 'end-of-buffer)
3480 nil)))
3481 ;; Move by arg lines, but ignore invisible ones.
3482 (let (done)
3483 (while (and (> arg 0) (not done))
3484 ;; If the following character is currently invisible,
3485 ;; skip all characters with that same `invisible' property value.
3486 (while (and (not (eobp)) (line-move-invisible-p (point)))
3487 (goto-char (next-char-property-change (point))))
3488 ;; Now move a line.
3489 (end-of-line)
3490 ;; If there's no invisibility here, move over the newline.
3491 (cond
3492 ((eobp)
3493 (if (not noerror)
3494 (signal 'end-of-buffer nil)
3495 (setq done t)))
3496 ((and (> arg 1) ;; Use vertical-motion for last move
3497 (not (integerp selective-display))
3498 (not (line-move-invisible-p (point))))
3499 ;; We avoid vertical-motion when possible
3500 ;; because that has to fontify.
3501 (forward-line 1))
3502 ;; Otherwise move a more sophisticated way.
3503 ((zerop (vertical-motion 1))
3504 (if (not noerror)
3505 (signal 'end-of-buffer nil)
3506 (setq done t))))
3507 (unless done
3508 (setq arg (1- arg))))
3509 ;; The logic of this is the same as the loop above,
3510 ;; it just goes in the other direction.
3511 (while (and (< arg 0) (not done))
3512 (beginning-of-line)
3513 (cond
3514 ((bobp)
3515 (if (not noerror)
3516 (signal 'beginning-of-buffer nil)
3517 (setq done t)))
3518 ((and (< arg -1) ;; Use vertical-motion for last move
3519 (not (integerp selective-display))
3520 (not (line-move-invisible-p (1- (point)))))
3521 (forward-line -1))
3522 ((zerop (vertical-motion -1))
3523 (if (not noerror)
3524 (signal 'beginning-of-buffer nil)
3525 (setq done t))))
3526 (unless done
3527 (setq arg (1+ arg))
3528 (while (and ;; Don't move over previous invis lines
3529 ;; if our target is the middle of this line.
3530 (or (zerop (or goal-column temporary-goal-column))
3531 (< arg 0))
3532 (not (bobp)) (line-move-invisible-p (1- (point))))
3533 (goto-char (previous-char-property-change (point))))))))
3534 ;; This is the value the function returns.
3535 (= arg 0))
3537 (cond ((> arg 0)
3538 ;; If we did not move down as far as desired,
3539 ;; at least go to end of line.
3540 (end-of-line))
3541 ((< arg 0)
3542 ;; If we did not move up as far as desired,
3543 ;; at least go to beginning of line.
3544 (beginning-of-line))
3546 (line-move-finish (or goal-column temporary-goal-column)
3547 opoint forward))))))
3549 (defun line-move-finish (column opoint forward)
3550 (let ((repeat t))
3551 (while repeat
3552 ;; Set REPEAT to t to repeat the whole thing.
3553 (setq repeat nil)
3555 (let (new
3556 (line-beg (save-excursion (beginning-of-line) (point)))
3557 (line-end
3558 ;; Compute the end of the line
3559 ;; ignoring effectively invisible newlines.
3560 (save-excursion
3561 ;; Like end-of-line but ignores fields.
3562 (skip-chars-forward "^\n")
3563 (while (and (not (eobp)) (line-move-invisible-p (point)))
3564 (goto-char (next-char-property-change (point)))
3565 (skip-chars-forward "^\n"))
3566 (point))))
3568 ;; Move to the desired column.
3569 (line-move-to-column column)
3570 (setq new (point))
3572 ;; Process intangibility within a line.
3573 ;; Move to the chosen destination position from above,
3574 ;; with intangibility processing enabled.
3576 (goto-char (point-min))
3577 (let ((inhibit-point-motion-hooks nil))
3578 (goto-char new)
3580 ;; If intangibility moves us to a different (later) place
3581 ;; in the same line, use that as the destination.
3582 (if (<= (point) line-end)
3583 (setq new (point))
3584 ;; If that position is "too late",
3585 ;; try the previous allowable position.
3586 ;; See if it is ok.
3587 (backward-char)
3588 (if (if forward
3589 ;; If going forward, don't accept the previous
3590 ;; allowable position if it is before the target line.
3591 (< line-beg (point))
3592 ;; If going backward, don't accept the previous
3593 ;; allowable position if it is still after the target line.
3594 (<= (point) line-end))
3595 (setq new (point))
3596 ;; As a last resort, use the end of the line.
3597 (setq new line-end))))
3599 ;; Now move to the updated destination, processing fields
3600 ;; as well as intangibility.
3601 (goto-char opoint)
3602 (let ((inhibit-point-motion-hooks nil))
3603 (goto-char
3604 (constrain-to-field new opoint nil t
3605 'inhibit-line-move-field-capture)))
3607 ;; If all this moved us to a different line,
3608 ;; retry everything within that new line.
3609 (when (or (< (point) line-beg) (> (point) line-end))
3610 ;; Repeat the intangibility and field processing.
3611 (setq repeat t))))))
3613 (defun line-move-to-column (col)
3614 "Try to find column COL, considering invisibility.
3615 This function works only in certain cases,
3616 because what we really need is for `move-to-column'
3617 and `current-column' to be able to ignore invisible text."
3618 (if (zerop col)
3619 (beginning-of-line)
3620 (move-to-column col))
3622 (when (and line-move-ignore-invisible
3623 (not (bolp)) (line-move-invisible-p (1- (point))))
3624 (let ((normal-location (point))
3625 (normal-column (current-column)))
3626 ;; If the following character is currently invisible,
3627 ;; skip all characters with that same `invisible' property value.
3628 (while (and (not (eobp))
3629 (line-move-invisible-p (point)))
3630 (goto-char (next-char-property-change (point))))
3631 ;; Have we advanced to a larger column position?
3632 (if (> (current-column) normal-column)
3633 ;; We have made some progress towards the desired column.
3634 ;; See if we can make any further progress.
3635 (line-move-to-column (+ (current-column) (- col normal-column)))
3636 ;; Otherwise, go to the place we originally found
3637 ;; and move back over invisible text.
3638 ;; that will get us to the same place on the screen
3639 ;; but with a more reasonable buffer position.
3640 (goto-char normal-location)
3641 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3642 (while (and (not (bolp)) (line-move-invisible-p (1- (point))))
3643 (goto-char (previous-char-property-change (point) line-beg))))))))
3645 (defun move-end-of-line (arg)
3646 "Move point to end of current line.
3647 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3648 If point reaches the beginning or end of buffer, it stops there.
3649 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3651 This command does not move point across a field boundary unless doing so
3652 would move beyond there to a different line; if ARG is nil or 1, and
3653 point starts at a field boundary, point does not move. To ignore field
3654 boundaries bind `inhibit-field-text-motion' to t."
3655 (interactive "p")
3656 (or arg (setq arg 1))
3657 (let (done)
3658 (while (not done)
3659 (let ((newpos
3660 (save-excursion
3661 (let ((goal-column 0))
3662 (and (line-move arg t)
3663 (not (bobp))
3664 (progn
3665 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
3666 (goto-char (previous-char-property-change (point))))
3667 (backward-char 1)))
3668 (point)))))
3669 (goto-char newpos)
3670 (if (and (> (point) newpos)
3671 (eq (preceding-char) ?\n))
3672 (backward-char 1)
3673 (if (and (> (point) newpos) (not (eobp))
3674 (not (eq (following-char) ?\n)))
3675 ;; If we skipped something intangible
3676 ;; and now we're not really at eol,
3677 ;; keep going.
3678 (setq arg 1)
3679 (setq done t)))))))
3681 (defun move-beginning-of-line (arg)
3682 "Move point to beginning of current display line.
3683 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3684 If point reaches the beginning or end of buffer, it stops there.
3685 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3687 This command does not move point across a field boundary unless doing so
3688 would move beyond there to a different line; if ARG is nil or 1, and
3689 point starts at a field boundary, point does not move. To ignore field
3690 boundaries bind `inhibit-field-text-motion' to t."
3691 (interactive "p")
3692 (or arg (setq arg 1))
3693 (if (/= arg 1)
3694 (line-move (1- arg) t))
3695 (beginning-of-line 1)
3696 (let ((orig (point)))
3697 (vertical-motion 0)
3698 (if (/= orig (point))
3699 (goto-char (constrain-to-field (point) orig (/= arg 1) t nil)))))
3702 ;;; Many people have said they rarely use this feature, and often type
3703 ;;; it by accident. Maybe it shouldn't even be on a key.
3704 (put 'set-goal-column 'disabled t)
3706 (defun set-goal-column (arg)
3707 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3708 Those commands will move to this position in the line moved to
3709 rather than trying to keep the same horizontal position.
3710 With a non-nil argument, clears out the goal column
3711 so that \\[next-line] and \\[previous-line] resume vertical motion.
3712 The goal column is stored in the variable `goal-column'."
3713 (interactive "P")
3714 (if arg
3715 (progn
3716 (setq goal-column nil)
3717 (message "No goal column"))
3718 (setq goal-column (current-column))
3719 ;; The older method below can be erroneous if `set-goal-column' is bound
3720 ;; to a sequence containing %
3721 ;;(message (substitute-command-keys
3722 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3723 ;;goal-column)
3724 (message "%s"
3725 (concat
3726 (format "Goal column %d " goal-column)
3727 (substitute-command-keys
3728 "(use \\[set-goal-column] with an arg to unset it)")))
3731 nil)
3734 (defun scroll-other-window-down (lines)
3735 "Scroll the \"other window\" down.
3736 For more details, see the documentation for `scroll-other-window'."
3737 (interactive "P")
3738 (scroll-other-window
3739 ;; Just invert the argument's meaning.
3740 ;; We can do that without knowing which window it will be.
3741 (if (eq lines '-) nil
3742 (if (null lines) '-
3743 (- (prefix-numeric-value lines))))))
3745 (defun beginning-of-buffer-other-window (arg)
3746 "Move point to the beginning of the buffer in the other window.
3747 Leave mark at previous position.
3748 With arg N, put point N/10 of the way from the true beginning."
3749 (interactive "P")
3750 (let ((orig-window (selected-window))
3751 (window (other-window-for-scrolling)))
3752 ;; We use unwind-protect rather than save-window-excursion
3753 ;; because the latter would preserve the things we want to change.
3754 (unwind-protect
3755 (progn
3756 (select-window window)
3757 ;; Set point and mark in that window's buffer.
3758 (with-no-warnings
3759 (beginning-of-buffer arg))
3760 ;; Set point accordingly.
3761 (recenter '(t)))
3762 (select-window orig-window))))
3764 (defun end-of-buffer-other-window (arg)
3765 "Move point to the end of the buffer in the other window.
3766 Leave mark at previous position.
3767 With arg N, put point N/10 of the way from the true end."
3768 (interactive "P")
3769 ;; See beginning-of-buffer-other-window for comments.
3770 (let ((orig-window (selected-window))
3771 (window (other-window-for-scrolling)))
3772 (unwind-protect
3773 (progn
3774 (select-window window)
3775 (with-no-warnings
3776 (end-of-buffer arg))
3777 (recenter '(t)))
3778 (select-window orig-window))))
3780 (defun transpose-chars (arg)
3781 "Interchange characters around point, moving forward one character.
3782 With prefix arg ARG, effect is to take character before point
3783 and drag it forward past ARG other characters (backward if ARG negative).
3784 If no argument and at end of line, the previous two chars are exchanged."
3785 (interactive "*P")
3786 (and (null arg) (eolp) (forward-char -1))
3787 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3789 (defun transpose-words (arg)
3790 "Interchange words around point, leaving point at end of them.
3791 With prefix arg ARG, effect is to take word before or around point
3792 and drag it forward past ARG other words (backward if ARG negative).
3793 If ARG is zero, the words around or after point and around or after mark
3794 are interchanged."
3795 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3796 (interactive "*p")
3797 (transpose-subr 'forward-word arg))
3799 (defun transpose-sexps (arg)
3800 "Like \\[transpose-words] but applies to sexps.
3801 Does not work on a sexp that point is in the middle of
3802 if it is a list or string."
3803 (interactive "*p")
3804 (transpose-subr
3805 (lambda (arg)
3806 ;; Here we should try to simulate the behavior of
3807 ;; (cons (progn (forward-sexp x) (point))
3808 ;; (progn (forward-sexp (- x)) (point)))
3809 ;; Except that we don't want to rely on the second forward-sexp
3810 ;; putting us back to where we want to be, since forward-sexp-function
3811 ;; might do funny things like infix-precedence.
3812 (if (if (> arg 0)
3813 (looking-at "\\sw\\|\\s_")
3814 (and (not (bobp))
3815 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3816 ;; Jumping over a symbol. We might be inside it, mind you.
3817 (progn (funcall (if (> arg 0)
3818 'skip-syntax-backward 'skip-syntax-forward)
3819 "w_")
3820 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3821 ;; Otherwise, we're between sexps. Take a step back before jumping
3822 ;; to make sure we'll obey the same precedence no matter which direction
3823 ;; we're going.
3824 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3825 (cons (save-excursion (forward-sexp arg) (point))
3826 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3827 (not (zerop (funcall (if (> arg 0)
3828 'skip-syntax-forward
3829 'skip-syntax-backward)
3830 ".")))))
3831 (point)))))
3832 arg 'special))
3834 (defun transpose-lines (arg)
3835 "Exchange current line and previous line, leaving point after both.
3836 With argument ARG, takes previous line and moves it past ARG lines.
3837 With argument 0, interchanges line point is in with line mark is in."
3838 (interactive "*p")
3839 (transpose-subr (function
3840 (lambda (arg)
3841 (if (> arg 0)
3842 (progn
3843 ;; Move forward over ARG lines,
3844 ;; but create newlines if necessary.
3845 (setq arg (forward-line arg))
3846 (if (/= (preceding-char) ?\n)
3847 (setq arg (1+ arg)))
3848 (if (> arg 0)
3849 (newline arg)))
3850 (forward-line arg))))
3851 arg))
3853 (defun transpose-subr (mover arg &optional special)
3854 (let ((aux (if special mover
3855 (lambda (x)
3856 (cons (progn (funcall mover x) (point))
3857 (progn (funcall mover (- x)) (point))))))
3858 pos1 pos2)
3859 (cond
3860 ((= arg 0)
3861 (save-excursion
3862 (setq pos1 (funcall aux 1))
3863 (goto-char (mark))
3864 (setq pos2 (funcall aux 1))
3865 (transpose-subr-1 pos1 pos2))
3866 (exchange-point-and-mark))
3867 ((> arg 0)
3868 (setq pos1 (funcall aux -1))
3869 (setq pos2 (funcall aux arg))
3870 (transpose-subr-1 pos1 pos2)
3871 (goto-char (car pos2)))
3873 (setq pos1 (funcall aux -1))
3874 (goto-char (car pos1))
3875 (setq pos2 (funcall aux arg))
3876 (transpose-subr-1 pos1 pos2)))))
3878 (defun transpose-subr-1 (pos1 pos2)
3879 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3880 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3881 (when (> (car pos1) (car pos2))
3882 (let ((swap pos1))
3883 (setq pos1 pos2 pos2 swap)))
3884 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3885 (atomic-change-group
3886 (let (word2)
3887 ;; FIXME: We first delete the two pieces of text, so markers that
3888 ;; used to point to after the text end up pointing to before it :-(
3889 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3890 (goto-char (car pos2))
3891 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3892 (goto-char (car pos1))
3893 (insert word2))))
3895 (defun backward-word (&optional arg)
3896 "Move backward until encountering the beginning of a word.
3897 With argument, do this that many times."
3898 (interactive "p")
3899 (forward-word (- (or arg 1))))
3901 (defun mark-word (&optional arg allow-extend)
3902 "Set mark ARG words away from point.
3903 The place mark goes is the same place \\[forward-word] would
3904 move to with the same argument.
3905 Interactively, if this command is repeated
3906 or (in Transient Mark mode) if the mark is active,
3907 it marks the next ARG words after the ones already marked."
3908 (interactive "P\np")
3909 (cond ((and allow-extend
3910 (or (and (eq last-command this-command) (mark t))
3911 (and transient-mark-mode mark-active)))
3912 (setq arg (if arg (prefix-numeric-value arg)
3913 (if (< (mark) (point)) -1 1)))
3914 (set-mark
3915 (save-excursion
3916 (goto-char (mark))
3917 (forward-word arg)
3918 (point))))
3920 (push-mark
3921 (save-excursion
3922 (forward-word (prefix-numeric-value arg))
3923 (point))
3924 nil t))))
3926 (defun kill-word (arg)
3927 "Kill characters forward until encountering the end of a word.
3928 With argument, do this that many times."
3929 (interactive "p")
3930 (kill-region (point) (progn (forward-word arg) (point))))
3932 (defun backward-kill-word (arg)
3933 "Kill characters backward until encountering the end of a word.
3934 With argument, do this that many times."
3935 (interactive "p")
3936 (kill-word (- arg)))
3938 (defun current-word (&optional strict really-word)
3939 "Return the symbol or word that point is on (or a nearby one) as a string.
3940 The return value includes no text properties.
3941 If optional arg STRICT is non-nil, return nil unless point is within
3942 or adjacent to a symbol or word. In all cases the value can be nil
3943 if there is no word nearby.
3944 The function, belying its name, normally finds a symbol.
3945 If optional arg REALLY-WORD is non-nil, it finds just a word."
3946 (save-excursion
3947 (let* ((oldpoint (point)) (start (point)) (end (point))
3948 (syntaxes (if really-word "w" "w_"))
3949 (not-syntaxes (concat "^" syntaxes)))
3950 (skip-syntax-backward syntaxes) (setq start (point))
3951 (goto-char oldpoint)
3952 (skip-syntax-forward syntaxes) (setq end (point))
3953 (when (and (eq start oldpoint) (eq end oldpoint)
3954 ;; Point is neither within nor adjacent to a word.
3955 (not strict))
3956 ;; Look for preceding word in same line.
3957 (skip-syntax-backward not-syntaxes
3958 (save-excursion (beginning-of-line)
3959 (point)))
3960 (if (bolp)
3961 ;; No preceding word in same line.
3962 ;; Look for following word in same line.
3963 (progn
3964 (skip-syntax-forward not-syntaxes
3965 (save-excursion (end-of-line)
3966 (point)))
3967 (setq start (point))
3968 (skip-syntax-forward syntaxes)
3969 (setq end (point)))
3970 (setq end (point))
3971 (skip-syntax-backward syntaxes)
3972 (setq start (point))))
3973 ;; If we found something nonempty, return it as a string.
3974 (unless (= start end)
3975 (buffer-substring-no-properties start end)))))
3977 (defcustom fill-prefix nil
3978 "*String for filling to insert at front of new line, or nil for none."
3979 :type '(choice (const :tag "None" nil)
3980 string)
3981 :group 'fill)
3982 (make-variable-buffer-local 'fill-prefix)
3984 (defcustom auto-fill-inhibit-regexp nil
3985 "*Regexp to match lines which should not be auto-filled."
3986 :type '(choice (const :tag "None" nil)
3987 regexp)
3988 :group 'fill)
3990 (defvar comment-line-break-function 'comment-indent-new-line
3991 "*Mode-specific function which line breaks and continues a comment.
3993 This function is only called during auto-filling of a comment section.
3994 The function should take a single optional argument, which is a flag
3995 indicating whether it should use soft newlines.")
3997 ;; This function is used as the auto-fill-function of a buffer
3998 ;; when Auto-Fill mode is enabled.
3999 ;; It returns t if it really did any work.
4000 ;; (Actually some major modes use a different auto-fill function,
4001 ;; but this one is the default one.)
4002 (defun do-auto-fill ()
4003 (let (fc justify give-up
4004 (fill-prefix fill-prefix))
4005 (if (or (not (setq justify (current-justification)))
4006 (null (setq fc (current-fill-column)))
4007 (and (eq justify 'left)
4008 (<= (current-column) fc))
4009 (and auto-fill-inhibit-regexp
4010 (save-excursion (beginning-of-line)
4011 (looking-at auto-fill-inhibit-regexp))))
4012 nil ;; Auto-filling not required
4013 (if (memq justify '(full center right))
4014 (save-excursion (unjustify-current-line)))
4016 ;; Choose a fill-prefix automatically.
4017 (when (and adaptive-fill-mode
4018 (or (null fill-prefix) (string= fill-prefix "")))
4019 (let ((prefix
4020 (fill-context-prefix
4021 (save-excursion (backward-paragraph 1) (point))
4022 (save-excursion (forward-paragraph 1) (point)))))
4023 (and prefix (not (equal prefix ""))
4024 ;; Use auto-indentation rather than a guessed empty prefix.
4025 (not (and fill-indent-according-to-mode
4026 (string-match "\\`[ \t]*\\'" prefix)))
4027 (setq fill-prefix prefix))))
4029 (while (and (not give-up) (> (current-column) fc))
4030 ;; Determine where to split the line.
4031 (let* (after-prefix
4032 (fill-point
4033 (save-excursion
4034 (beginning-of-line)
4035 (setq after-prefix (point))
4036 (and fill-prefix
4037 (looking-at (regexp-quote fill-prefix))
4038 (setq after-prefix (match-end 0)))
4039 (move-to-column (1+ fc))
4040 (fill-move-to-break-point after-prefix)
4041 (point))))
4043 ;; See whether the place we found is any good.
4044 (if (save-excursion
4045 (goto-char fill-point)
4046 (or (bolp)
4047 ;; There is no use breaking at end of line.
4048 (save-excursion (skip-chars-forward " ") (eolp))
4049 ;; It is futile to split at the end of the prefix
4050 ;; since we would just insert the prefix again.
4051 (and after-prefix (<= (point) after-prefix))
4052 ;; Don't split right after a comment starter
4053 ;; since we would just make another comment starter.
4054 (and comment-start-skip
4055 (let ((limit (point)))
4056 (beginning-of-line)
4057 (and (re-search-forward comment-start-skip
4058 limit t)
4059 (eq (point) limit))))))
4060 ;; No good place to break => stop trying.
4061 (setq give-up t)
4062 ;; Ok, we have a useful place to break the line. Do it.
4063 (let ((prev-column (current-column)))
4064 ;; If point is at the fill-point, do not `save-excursion'.
4065 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
4066 ;; point will end up before it rather than after it.
4067 (if (save-excursion
4068 (skip-chars-backward " \t")
4069 (= (point) fill-point))
4070 (funcall comment-line-break-function t)
4071 (save-excursion
4072 (goto-char fill-point)
4073 (funcall comment-line-break-function t)))
4074 ;; Now do justification, if required
4075 (if (not (eq justify 'left))
4076 (save-excursion
4077 (end-of-line 0)
4078 (justify-current-line justify nil t)))
4079 ;; If making the new line didn't reduce the hpos of
4080 ;; the end of the line, then give up now;
4081 ;; trying again will not help.
4082 (if (>= (current-column) prev-column)
4083 (setq give-up t))))))
4084 ;; Justify last line.
4085 (justify-current-line justify t t)
4086 t)))
4088 (defvar normal-auto-fill-function 'do-auto-fill
4089 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
4090 Some major modes set this.")
4092 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
4093 ;; FIXME: turn into a proper minor mode.
4094 ;; Add a global minor mode version of it.
4095 (defun auto-fill-mode (&optional arg)
4096 "Toggle Auto Fill mode.
4097 With arg, turn Auto Fill mode on if and only if arg is positive.
4098 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
4099 automatically breaks the line at a previous space.
4101 The value of `normal-auto-fill-function' specifies the function to use
4102 for `auto-fill-function' when turning Auto Fill mode on."
4103 (interactive "P")
4104 (prog1 (setq auto-fill-function
4105 (if (if (null arg)
4106 (not auto-fill-function)
4107 (> (prefix-numeric-value arg) 0))
4108 normal-auto-fill-function
4109 nil))
4110 (force-mode-line-update)))
4112 ;; This holds a document string used to document auto-fill-mode.
4113 (defun auto-fill-function ()
4114 "Automatically break line at a previous space, in insertion of text."
4115 nil)
4117 (defun turn-on-auto-fill ()
4118 "Unconditionally turn on Auto Fill mode."
4119 (auto-fill-mode 1))
4121 (defun turn-off-auto-fill ()
4122 "Unconditionally turn off Auto Fill mode."
4123 (auto-fill-mode -1))
4125 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
4127 (defun set-fill-column (arg)
4128 "Set `fill-column' to specified argument.
4129 Use \\[universal-argument] followed by a number to specify a column.
4130 Just \\[universal-argument] as argument means to use the current column."
4131 (interactive "P")
4132 (if (consp arg)
4133 (setq arg (current-column)))
4134 (if (not (integerp arg))
4135 ;; Disallow missing argument; it's probably a typo for C-x C-f.
4136 (error "set-fill-column requires an explicit argument")
4137 (message "Fill column set to %d (was %d)" arg fill-column)
4138 (setq fill-column arg)))
4140 (defun set-selective-display (arg)
4141 "Set `selective-display' to ARG; clear it if no arg.
4142 When the value of `selective-display' is a number > 0,
4143 lines whose indentation is >= that value are not displayed.
4144 The variable `selective-display' has a separate value for each buffer."
4145 (interactive "P")
4146 (if (eq selective-display t)
4147 (error "selective-display already in use for marked lines"))
4148 (let ((current-vpos
4149 (save-restriction
4150 (narrow-to-region (point-min) (point))
4151 (goto-char (window-start))
4152 (vertical-motion (window-height)))))
4153 (setq selective-display
4154 (and arg (prefix-numeric-value arg)))
4155 (recenter current-vpos))
4156 (set-window-start (selected-window) (window-start (selected-window)))
4157 (princ "selective-display set to " t)
4158 (prin1 selective-display t)
4159 (princ "." t))
4161 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
4162 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
4164 (defun toggle-truncate-lines (arg)
4165 "Toggle whether to fold or truncate long lines on the screen.
4166 With arg, truncate long lines iff arg is positive.
4167 Note that in side-by-side windows, truncation is always enabled."
4168 (interactive "P")
4169 (setq truncate-lines
4170 (if (null arg)
4171 (not truncate-lines)
4172 (> (prefix-numeric-value arg) 0)))
4173 (force-mode-line-update)
4174 (unless truncate-lines
4175 (let ((buffer (current-buffer)))
4176 (walk-windows (lambda (window)
4177 (if (eq buffer (window-buffer window))
4178 (set-window-hscroll window 0)))
4179 nil t)))
4180 (message "Truncate long lines %s"
4181 (if truncate-lines "enabled" "disabled")))
4183 (defvar overwrite-mode-textual " Ovwrt"
4184 "The string displayed in the mode line when in overwrite mode.")
4185 (defvar overwrite-mode-binary " Bin Ovwrt"
4186 "The string displayed in the mode line when in binary overwrite mode.")
4188 (defun overwrite-mode (arg)
4189 "Toggle overwrite mode.
4190 With arg, turn overwrite mode on iff arg is positive.
4191 In overwrite mode, printing characters typed in replace existing text
4192 on a one-for-one basis, rather than pushing it to the right. At the
4193 end of a line, such characters extend the line. Before a tab,
4194 such characters insert until the tab is filled in.
4195 \\[quoted-insert] still inserts characters in overwrite mode; this
4196 is supposed to make it easier to insert characters when necessary."
4197 (interactive "P")
4198 (setq overwrite-mode
4199 (if (if (null arg) (not overwrite-mode)
4200 (> (prefix-numeric-value arg) 0))
4201 'overwrite-mode-textual))
4202 (force-mode-line-update))
4204 (defun binary-overwrite-mode (arg)
4205 "Toggle binary overwrite mode.
4206 With arg, turn binary overwrite mode on iff arg is positive.
4207 In binary overwrite mode, printing characters typed in replace
4208 existing text. Newlines are not treated specially, so typing at the
4209 end of a line joins the line to the next, with the typed character
4210 between them. Typing before a tab character simply replaces the tab
4211 with the character typed.
4212 \\[quoted-insert] replaces the text at the cursor, just as ordinary
4213 typing characters do.
4215 Note that binary overwrite mode is not its own minor mode; it is a
4216 specialization of overwrite mode, entered by setting the
4217 `overwrite-mode' variable to `overwrite-mode-binary'."
4218 (interactive "P")
4219 (setq overwrite-mode
4220 (if (if (null arg)
4221 (not (eq overwrite-mode 'overwrite-mode-binary))
4222 (> (prefix-numeric-value arg) 0))
4223 'overwrite-mode-binary))
4224 (force-mode-line-update))
4226 (define-minor-mode line-number-mode
4227 "Toggle Line Number mode.
4228 With arg, turn Line Number mode on iff arg is positive.
4229 When Line Number mode is enabled, the line number appears
4230 in the mode line.
4232 Line numbers do not appear for very large buffers and buffers
4233 with very long lines; see variables `line-number-display-limit'
4234 and `line-number-display-limit-width'."
4235 :init-value t :global t :group 'editing-basics)
4237 (define-minor-mode column-number-mode
4238 "Toggle Column Number mode.
4239 With arg, turn Column Number mode on iff arg is positive.
4240 When Column Number mode is enabled, the column number appears
4241 in the mode line."
4242 :global t :group 'editing-basics)
4244 (define-minor-mode size-indication-mode
4245 "Toggle Size Indication mode.
4246 With arg, turn Size Indication mode on iff arg is positive. When
4247 Size Indication mode is enabled, the size of the accessible part
4248 of the buffer appears in the mode line."
4249 :global t :group 'editing-basics)
4251 (defgroup paren-blinking nil
4252 "Blinking matching of parens and expressions."
4253 :prefix "blink-matching-"
4254 :group 'paren-matching)
4256 (defcustom blink-matching-paren t
4257 "*Non-nil means show matching open-paren when close-paren is inserted."
4258 :type 'boolean
4259 :group 'paren-blinking)
4261 (defcustom blink-matching-paren-on-screen t
4262 "*Non-nil means show matching open-paren when it is on screen.
4263 If nil, means don't show it (but the open-paren can still be shown
4264 when it is off screen).
4266 This variable has no effect if `blink-matching-paren' is nil.
4267 \(In that case, the open-paren is never shown.)
4268 It is also ignored if `show-paren-mode' is enabled."
4269 :type 'boolean
4270 :group 'paren-blinking)
4272 (defcustom blink-matching-paren-distance (* 25 1024)
4273 "*If non-nil, maximum distance to search backwards for matching open-paren.
4274 If nil, search stops at the beginning of the accessible portion of the buffer."
4275 :type '(choice (const nil) integer)
4276 :group 'paren-blinking)
4278 (defcustom blink-matching-delay 1
4279 "*Time in seconds to delay after showing a matching paren."
4280 :type 'number
4281 :group 'paren-blinking)
4283 (defcustom blink-matching-paren-dont-ignore-comments nil
4284 "*Non-nil means `blink-matching-paren' will not ignore comments."
4285 :type 'boolean
4286 :group 'paren-blinking)
4288 (defun blink-matching-open ()
4289 "Move cursor momentarily to the beginning of the sexp before point."
4290 (interactive)
4291 (when (and (> (point) (point-min))
4292 blink-matching-paren
4293 ;; Verify an even number of quoting characters precede the close.
4294 (= 1 (logand 1 (- (point)
4295 (save-excursion
4296 (forward-char -1)
4297 (skip-syntax-backward "/\\")
4298 (point))))))
4299 (let* ((oldpos (point))
4300 blinkpos
4301 message-log-max ; Don't log messages about paren matching.
4302 matching-paren
4303 open-paren-line-string)
4304 (save-excursion
4305 (save-restriction
4306 (if blink-matching-paren-distance
4307 (narrow-to-region (max (point-min)
4308 (- (point) blink-matching-paren-distance))
4309 oldpos))
4310 (condition-case ()
4311 (let ((parse-sexp-ignore-comments
4312 (and parse-sexp-ignore-comments
4313 (not blink-matching-paren-dont-ignore-comments))))
4314 (setq blinkpos (scan-sexps oldpos -1)))
4315 (error nil)))
4316 (and blinkpos
4317 ;; Not syntax '$'.
4318 (not (eq (syntax-class (syntax-after blinkpos)) 8))
4319 (setq matching-paren
4320 (let ((syntax (syntax-after blinkpos)))
4321 (and (consp syntax)
4322 (eq (syntax-class syntax) 4)
4323 (cdr syntax)))))
4324 (cond
4325 ((not (or (eq matching-paren (char-before oldpos))
4326 ;; The cdr might hold a new paren-class info rather than
4327 ;; a matching-char info, in which case the two CDRs
4328 ;; should match.
4329 (eq matching-paren (cdr (syntax-after (1- oldpos))))))
4330 (message "Mismatched parentheses"))
4331 ((not blinkpos)
4332 (if (not blink-matching-paren-distance)
4333 (message "Unmatched parenthesis")))
4334 ((pos-visible-in-window-p blinkpos)
4335 ;; Matching open within window, temporarily move to blinkpos but only
4336 ;; if `blink-matching-paren-on-screen' is non-nil.
4337 (and blink-matching-paren-on-screen
4338 (not show-paren-mode)
4339 (save-excursion
4340 (goto-char blinkpos)
4341 (sit-for blink-matching-delay))))
4343 (save-excursion
4344 (goto-char blinkpos)
4345 (setq open-paren-line-string
4346 ;; Show what precedes the open in its line, if anything.
4347 (if (save-excursion
4348 (skip-chars-backward " \t")
4349 (not (bolp)))
4350 (buffer-substring (line-beginning-position)
4351 (1+ blinkpos))
4352 ;; Show what follows the open in its line, if anything.
4353 (if (save-excursion
4354 (forward-char 1)
4355 (skip-chars-forward " \t")
4356 (not (eolp)))
4357 (buffer-substring blinkpos
4358 (line-end-position))
4359 ;; Otherwise show the previous nonblank line,
4360 ;; if there is one.
4361 (if (save-excursion
4362 (skip-chars-backward "\n \t")
4363 (not (bobp)))
4364 (concat
4365 (buffer-substring (progn
4366 (skip-chars-backward "\n \t")
4367 (line-beginning-position))
4368 (progn (end-of-line)
4369 (skip-chars-backward " \t")
4370 (point)))
4371 ;; Replace the newline and other whitespace with `...'.
4372 "..."
4373 (buffer-substring blinkpos (1+ blinkpos)))
4374 ;; There is nothing to show except the char itself.
4375 (buffer-substring blinkpos (1+ blinkpos)))))))
4376 (message "Matches %s"
4377 (substring-no-properties open-paren-line-string))))))))
4379 ;Turned off because it makes dbx bomb out.
4380 (setq blink-paren-function 'blink-matching-open)
4382 ;; This executes C-g typed while Emacs is waiting for a command.
4383 ;; Quitting out of a program does not go through here;
4384 ;; that happens in the QUIT macro at the C code level.
4385 (defun keyboard-quit ()
4386 "Signal a `quit' condition.
4387 During execution of Lisp code, this character causes a quit directly.
4388 At top-level, as an editor command, this simply beeps."
4389 (interactive)
4390 (deactivate-mark)
4391 (if (fboundp 'kmacro-keyboard-quit)
4392 (kmacro-keyboard-quit))
4393 (setq defining-kbd-macro nil)
4394 (signal 'quit nil))
4396 (defvar buffer-quit-function nil
4397 "Function to call to \"quit\" the current buffer, or nil if none.
4398 \\[keyboard-escape-quit] calls this function when its more local actions
4399 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4401 (defun keyboard-escape-quit ()
4402 "Exit the current \"mode\" (in a generalized sense of the word).
4403 This command can exit an interactive command such as `query-replace',
4404 can clear out a prefix argument or a region,
4405 can get out of the minibuffer or other recursive edit,
4406 cancel the use of the current buffer (for special-purpose buffers),
4407 or go back to just one window (by deleting all but the selected window)."
4408 (interactive)
4409 (cond ((eq last-command 'mode-exited) nil)
4410 ((> (minibuffer-depth) 0)
4411 (abort-recursive-edit))
4412 (current-prefix-arg
4413 nil)
4414 ((and transient-mark-mode mark-active)
4415 (deactivate-mark))
4416 ((> (recursion-depth) 0)
4417 (exit-recursive-edit))
4418 (buffer-quit-function
4419 (funcall buffer-quit-function))
4420 ((not (one-window-p t))
4421 (delete-other-windows))
4422 ((string-match "^ \\*" (buffer-name (current-buffer)))
4423 (bury-buffer))))
4425 (defun play-sound-file (file &optional volume device)
4426 "Play sound stored in FILE.
4427 VOLUME and DEVICE correspond to the keywords of the sound
4428 specification for `play-sound'."
4429 (interactive "fPlay sound file: ")
4430 (let ((sound (list :file file)))
4431 (if volume
4432 (plist-put sound :volume volume))
4433 (if device
4434 (plist-put sound :device device))
4435 (push 'sound sound)
4436 (play-sound sound)))
4439 (defcustom read-mail-command 'rmail
4440 "*Your preference for a mail reading package.
4441 This is used by some keybindings which support reading mail.
4442 See also `mail-user-agent' concerning sending mail."
4443 :type '(choice (function-item rmail)
4444 (function-item gnus)
4445 (function-item mh-rmail)
4446 (function :tag "Other"))
4447 :version "21.1"
4448 :group 'mail)
4450 (defcustom mail-user-agent 'sendmail-user-agent
4451 "*Your preference for a mail composition package.
4452 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4453 outgoing email message. This variable lets you specify which
4454 mail-sending package you prefer.
4456 Valid values include:
4458 `sendmail-user-agent' -- use the default Emacs Mail package.
4459 See Info node `(emacs)Sending Mail'.
4460 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4461 See Info node `(mh-e)'.
4462 `message-user-agent' -- use the Gnus Message package.
4463 See Info node `(message)'.
4464 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4465 paraphernalia, particularly the Gcc: header for
4466 archiving.
4468 Additional valid symbols may be available; check with the author of
4469 your package for details. The function should return non-nil if it
4470 succeeds.
4472 See also `read-mail-command' concerning reading mail."
4473 :type '(radio (function-item :tag "Default Emacs mail"
4474 :format "%t\n"
4475 sendmail-user-agent)
4476 (function-item :tag "Emacs interface to MH"
4477 :format "%t\n"
4478 mh-e-user-agent)
4479 (function-item :tag "Gnus Message package"
4480 :format "%t\n"
4481 message-user-agent)
4482 (function-item :tag "Gnus Message with full Gnus features"
4483 :format "%t\n"
4484 gnus-user-agent)
4485 (function :tag "Other"))
4486 :group 'mail)
4488 (define-mail-user-agent 'sendmail-user-agent
4489 'sendmail-user-agent-compose
4490 'mail-send-and-exit)
4492 (defun rfc822-goto-eoh ()
4493 ;; Go to header delimiter line in a mail message, following RFC822 rules
4494 (goto-char (point-min))
4495 (when (re-search-forward
4496 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4497 (goto-char (match-beginning 0))))
4499 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4500 switch-function yank-action
4501 send-actions)
4502 (if switch-function
4503 (let ((special-display-buffer-names nil)
4504 (special-display-regexps nil)
4505 (same-window-buffer-names nil)
4506 (same-window-regexps nil))
4507 (funcall switch-function "*mail*")))
4508 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4509 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4510 (body (cdr (assoc-string "body" other-headers t))))
4511 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4512 continue
4513 (error "Message aborted"))
4514 (save-excursion
4515 (rfc822-goto-eoh)
4516 (while other-headers
4517 (unless (member-ignore-case (car (car other-headers))
4518 '("in-reply-to" "cc" "body"))
4519 (insert (car (car other-headers)) ": "
4520 (cdr (car other-headers))
4521 (if use-hard-newlines hard-newline "\n")))
4522 (setq other-headers (cdr other-headers)))
4523 (when body
4524 (forward-line 1)
4525 (insert body))
4526 t)))
4528 (define-mail-user-agent 'mh-e-user-agent
4529 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
4530 'mh-before-send-letter-hook)
4532 (defun compose-mail (&optional to subject other-headers continue
4533 switch-function yank-action send-actions)
4534 "Start composing a mail message to send.
4535 This uses the user's chosen mail composition package
4536 as selected with the variable `mail-user-agent'.
4537 The optional arguments TO and SUBJECT specify recipients
4538 and the initial Subject field, respectively.
4540 OTHER-HEADERS is an alist specifying additional
4541 header fields. Elements look like (HEADER . VALUE) where both
4542 HEADER and VALUE are strings.
4544 CONTINUE, if non-nil, says to continue editing a message already
4545 being composed.
4547 SWITCH-FUNCTION, if non-nil, is a function to use to
4548 switch to and display the buffer used for mail composition.
4550 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4551 to insert the raw text of the message being replied to.
4552 It has the form (FUNCTION . ARGS). The user agent will apply
4553 FUNCTION to ARGS, to insert the raw text of the original message.
4554 \(The user agent will also run `mail-citation-hook', *after* the
4555 original text has been inserted in this way.)
4557 SEND-ACTIONS is a list of actions to call when the message is sent.
4558 Each action has the form (FUNCTION . ARGS)."
4559 (interactive
4560 (list nil nil nil current-prefix-arg))
4561 (let ((function (get mail-user-agent 'composefunc)))
4562 (funcall function to subject other-headers continue
4563 switch-function yank-action send-actions)))
4565 (defun compose-mail-other-window (&optional to subject other-headers continue
4566 yank-action send-actions)
4567 "Like \\[compose-mail], but edit the outgoing message in another window."
4568 (interactive
4569 (list nil nil nil current-prefix-arg))
4570 (compose-mail to subject other-headers continue
4571 'switch-to-buffer-other-window yank-action send-actions))
4574 (defun compose-mail-other-frame (&optional to subject other-headers continue
4575 yank-action send-actions)
4576 "Like \\[compose-mail], but edit the outgoing message in another frame."
4577 (interactive
4578 (list nil nil nil current-prefix-arg))
4579 (compose-mail to subject other-headers continue
4580 'switch-to-buffer-other-frame yank-action send-actions))
4582 (defvar set-variable-value-history nil
4583 "History of values entered with `set-variable'.")
4585 (defun set-variable (variable value &optional make-local)
4586 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4587 VARIABLE should be a user option variable name, a Lisp variable
4588 meant to be customized by users. You should enter VALUE in Lisp syntax,
4589 so if you want VALUE to be a string, you must surround it with doublequotes.
4590 VALUE is used literally, not evaluated.
4592 If VARIABLE has a `variable-interactive' property, that is used as if
4593 it were the arg to `interactive' (which see) to interactively read VALUE.
4595 If VARIABLE has been defined with `defcustom', then the type information
4596 in the definition is used to check that VALUE is valid.
4598 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4599 (interactive
4600 (let* ((default-var (variable-at-point))
4601 (var (if (user-variable-p default-var)
4602 (read-variable (format "Set variable (default %s): " default-var)
4603 default-var)
4604 (read-variable "Set variable: ")))
4605 (minibuffer-help-form '(describe-variable var))
4606 (prop (get var 'variable-interactive))
4607 (obsolete (car (get var 'byte-obsolete-variable)))
4608 (prompt (format "Set %s %s to value: " var
4609 (cond ((local-variable-p var)
4610 "(buffer-local)")
4611 ((or current-prefix-arg
4612 (local-variable-if-set-p var))
4613 "buffer-locally")
4614 (t "globally"))))
4615 (val (progn
4616 (when obsolete
4617 (message (concat "`%S' is obsolete; "
4618 (if (symbolp obsolete) "use `%S' instead" "%s"))
4619 var obsolete)
4620 (sit-for 3))
4621 (if prop
4622 ;; Use VAR's `variable-interactive' property
4623 ;; as an interactive spec for prompting.
4624 (call-interactively `(lambda (arg)
4625 (interactive ,prop)
4626 arg))
4627 (read
4628 (read-string prompt nil
4629 'set-variable-value-history
4630 (format "%S" (symbol-value var))))))))
4631 (list var val current-prefix-arg)))
4633 (and (custom-variable-p variable)
4634 (not (get variable 'custom-type))
4635 (custom-load-symbol variable))
4636 (let ((type (get variable 'custom-type)))
4637 (when type
4638 ;; Match with custom type.
4639 (require 'cus-edit)
4640 (setq type (widget-convert type))
4641 (unless (widget-apply type :match value)
4642 (error "Value `%S' does not match type %S of %S"
4643 value (car type) variable))))
4645 (if make-local
4646 (make-local-variable variable))
4648 (set variable value)
4650 ;; Force a thorough redisplay for the case that the variable
4651 ;; has an effect on the display, like `tab-width' has.
4652 (force-mode-line-update))
4654 ;; Define the major mode for lists of completions.
4656 (defvar completion-list-mode-map nil
4657 "Local map for completion list buffers.")
4658 (or completion-list-mode-map
4659 (let ((map (make-sparse-keymap)))
4660 (define-key map [mouse-2] 'mouse-choose-completion)
4661 (define-key map [follow-link] 'mouse-face)
4662 (define-key map [down-mouse-2] nil)
4663 (define-key map "\C-m" 'choose-completion)
4664 (define-key map "\e\e\e" 'delete-completion-window)
4665 (define-key map [left] 'previous-completion)
4666 (define-key map [right] 'next-completion)
4667 (setq completion-list-mode-map map)))
4669 ;; Completion mode is suitable only for specially formatted data.
4670 (put 'completion-list-mode 'mode-class 'special)
4672 (defvar completion-reference-buffer nil
4673 "Record the buffer that was current when the completion list was requested.
4674 This is a local variable in the completion list buffer.
4675 Initial value is nil to avoid some compiler warnings.")
4677 (defvar completion-no-auto-exit nil
4678 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4679 This also applies to other functions such as `choose-completion'
4680 and `mouse-choose-completion'.")
4682 (defvar completion-base-size nil
4683 "Number of chars at beginning of minibuffer not involved in completion.
4684 This is a local variable in the completion list buffer
4685 but it talks about the buffer in `completion-reference-buffer'.
4686 If this is nil, it means to compare text to determine which part
4687 of the tail end of the buffer's text is involved in completion.")
4689 (defun delete-completion-window ()
4690 "Delete the completion list window.
4691 Go to the window from which completion was requested."
4692 (interactive)
4693 (let ((buf completion-reference-buffer))
4694 (if (one-window-p t)
4695 (if (window-dedicated-p (selected-window))
4696 (delete-frame (selected-frame)))
4697 (delete-window (selected-window))
4698 (if (get-buffer-window buf)
4699 (select-window (get-buffer-window buf))))))
4701 (defun previous-completion (n)
4702 "Move to the previous item in the completion list."
4703 (interactive "p")
4704 (next-completion (- n)))
4706 (defun next-completion (n)
4707 "Move to the next item in the completion list.
4708 With prefix argument N, move N items (negative N means move backward)."
4709 (interactive "p")
4710 (let ((beg (point-min)) (end (point-max)))
4711 (while (and (> n 0) (not (eobp)))
4712 ;; If in a completion, move to the end of it.
4713 (when (get-text-property (point) 'mouse-face)
4714 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4715 ;; Move to start of next one.
4716 (unless (get-text-property (point) 'mouse-face)
4717 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4718 (setq n (1- n)))
4719 (while (and (< n 0) (not (bobp)))
4720 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4721 ;; If in a completion, move to the start of it.
4722 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4723 (goto-char (previous-single-property-change
4724 (point) 'mouse-face nil beg)))
4725 ;; Move to end of the previous completion.
4726 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4727 (goto-char (previous-single-property-change
4728 (point) 'mouse-face nil beg)))
4729 ;; Move to the start of that one.
4730 (goto-char (previous-single-property-change
4731 (point) 'mouse-face nil beg))
4732 (setq n (1+ n))))))
4734 (defun choose-completion ()
4735 "Choose the completion that point is in or next to."
4736 (interactive)
4737 (let (beg end completion (buffer completion-reference-buffer)
4738 (base-size completion-base-size))
4739 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4740 (setq end (point) beg (1+ (point))))
4741 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4742 (setq end (1- (point)) beg (point)))
4743 (if (null beg)
4744 (error "No completion here"))
4745 (setq beg (previous-single-property-change beg 'mouse-face))
4746 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4747 (setq completion (buffer-substring beg end))
4748 (let ((owindow (selected-window)))
4749 (if (and (one-window-p t 'selected-frame)
4750 (window-dedicated-p (selected-window)))
4751 ;; This is a special buffer's frame
4752 (iconify-frame (selected-frame))
4753 (or (window-dedicated-p (selected-window))
4754 (bury-buffer)))
4755 (select-window owindow))
4756 (choose-completion-string completion buffer base-size)))
4758 ;; Delete the longest partial match for STRING
4759 ;; that can be found before POINT.
4760 (defun choose-completion-delete-max-match (string)
4761 (let ((opoint (point))
4762 len)
4763 ;; Try moving back by the length of the string.
4764 (goto-char (max (- (point) (length string))
4765 (minibuffer-prompt-end)))
4766 ;; See how far back we were actually able to move. That is the
4767 ;; upper bound on how much we can match and delete.
4768 (setq len (- opoint (point)))
4769 (if completion-ignore-case
4770 (setq string (downcase string)))
4771 (while (and (> len 0)
4772 (let ((tail (buffer-substring (point) opoint)))
4773 (if completion-ignore-case
4774 (setq tail (downcase tail)))
4775 (not (string= tail (substring string 0 len)))))
4776 (setq len (1- len))
4777 (forward-char 1))
4778 (delete-char len)))
4780 (defvar choose-completion-string-functions nil
4781 "Functions that may override the normal insertion of a completion choice.
4782 These functions are called in order with four arguments:
4783 CHOICE - the string to insert in the buffer,
4784 BUFFER - the buffer in which the choice should be inserted,
4785 MINI-P - non-nil iff BUFFER is a minibuffer, and
4786 BASE-SIZE - the number of characters in BUFFER before
4787 the string being completed.
4789 If a function in the list returns non-nil, that function is supposed
4790 to have inserted the CHOICE in the BUFFER, and possibly exited
4791 the minibuffer; no further functions will be called.
4793 If all functions in the list return nil, that means to use
4794 the default method of inserting the completion in BUFFER.")
4796 (defun choose-completion-string (choice &optional buffer base-size)
4797 "Switch to BUFFER and insert the completion choice CHOICE.
4798 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4799 to keep. If it is nil, we call `choose-completion-delete-max-match'
4800 to decide what to delete."
4802 ;; If BUFFER is the minibuffer, exit the minibuffer
4803 ;; unless it is reading a file name and CHOICE is a directory,
4804 ;; or completion-no-auto-exit is non-nil.
4806 (let* ((buffer (or buffer completion-reference-buffer))
4807 (mini-p (minibufferp buffer)))
4808 ;; If BUFFER is a minibuffer, barf unless it's the currently
4809 ;; active minibuffer.
4810 (if (and mini-p
4811 (or (not (active-minibuffer-window))
4812 (not (equal buffer
4813 (window-buffer (active-minibuffer-window))))))
4814 (error "Minibuffer is not active for completion")
4815 ;; Set buffer so buffer-local choose-completion-string-functions works.
4816 (set-buffer buffer)
4817 (unless (run-hook-with-args-until-success
4818 'choose-completion-string-functions
4819 choice buffer mini-p base-size)
4820 ;; Insert the completion into the buffer where it was requested.
4821 (if base-size
4822 (delete-region (+ base-size (if mini-p
4823 (minibuffer-prompt-end)
4824 (point-min)))
4825 (point))
4826 (choose-completion-delete-max-match choice))
4827 (insert choice)
4828 (remove-text-properties (- (point) (length choice)) (point)
4829 '(mouse-face nil))
4830 ;; Update point in the window that BUFFER is showing in.
4831 (let ((window (get-buffer-window buffer t)))
4832 (set-window-point window (point)))
4833 ;; If completing for the minibuffer, exit it with this choice.
4834 (and (not completion-no-auto-exit)
4835 (equal buffer (window-buffer (minibuffer-window)))
4836 minibuffer-completion-table
4837 ;; If this is reading a file name, and the file name chosen
4838 ;; is a directory, don't exit the minibuffer.
4839 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4840 (file-directory-p (field-string (point-max))))
4841 (let ((mini (active-minibuffer-window)))
4842 (select-window mini)
4843 (when minibuffer-auto-raise
4844 (raise-frame (window-frame mini))))
4845 (exit-minibuffer)))))))
4847 (defun completion-list-mode ()
4848 "Major mode for buffers showing lists of possible completions.
4849 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4850 to select the completion near point.
4851 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4852 with the mouse."
4853 (interactive)
4854 (kill-all-local-variables)
4855 (use-local-map completion-list-mode-map)
4856 (setq mode-name "Completion List")
4857 (setq major-mode 'completion-list-mode)
4858 (make-local-variable 'completion-base-size)
4859 (setq completion-base-size nil)
4860 (run-mode-hooks 'completion-list-mode-hook))
4862 (defun completion-list-mode-finish ()
4863 "Finish setup of the completions buffer.
4864 Called from `temp-buffer-show-hook'."
4865 (when (eq major-mode 'completion-list-mode)
4866 (toggle-read-only 1)))
4868 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4870 (defvar completion-setup-hook nil
4871 "Normal hook run at the end of setting up a completion list buffer.
4872 When this hook is run, the current buffer is the one in which the
4873 command to display the completion list buffer was run.
4874 The completion list buffer is available as the value of `standard-output'.
4875 The common prefix substring for completion may be available as the
4876 value of `completion-common-substring'. See also `display-completion-list'.")
4879 ;; Variables and faces used in `completion-setup-function'.
4881 (defface completions-first-difference
4882 '((t (:inherit bold)))
4883 "Face put on the first uncommon character in completions in *Completions* buffer."
4884 :group 'completion)
4886 (defface completions-common-part
4887 '((t (:inherit default)))
4888 "Face put on the common prefix substring in completions in *Completions* buffer.
4889 The idea of `completions-common-part' is that you can use it to
4890 make the common parts less visible than normal, so that the rest
4891 of the differing parts is, by contrast, slightly highlighted."
4892 :group 'completion)
4894 ;; This is for packages that need to bind it to a non-default regexp
4895 ;; in order to make the first-differing character highlight work
4896 ;; to their liking
4897 (defvar completion-root-regexp "^/"
4898 "Regexp to use in `completion-setup-function' to find the root directory.")
4900 (defvar completion-common-substring nil
4901 "Common prefix substring to use in `completion-setup-function' to put faces.
4902 The value is set by `display-completion-list' during running `completion-setup-hook'.
4904 To put faces, `completions-first-difference' and `completions-common-part'
4905 into \"*Completions*\* buffer, the common prefix substring in completions is
4906 needed as a hint. (Minibuffer is a special case. The content of minibuffer itself
4907 is the substring.)")
4909 ;; This function goes in completion-setup-hook, so that it is called
4910 ;; after the text of the completion list buffer is written.
4911 (defun completion-setup-function ()
4912 (let* ((mainbuf (current-buffer))
4913 (mbuf-contents (minibuffer-contents))
4914 (common-string-length (length mbuf-contents)))
4915 ;; When reading a file name in the minibuffer,
4916 ;; set default-directory in the minibuffer
4917 ;; so it will get copied into the completion list buffer.
4918 (if minibuffer-completing-file-name
4919 (with-current-buffer mainbuf
4920 (setq default-directory (file-name-directory mbuf-contents))))
4921 ;; If partial-completion-mode is on, point might not be after the
4922 ;; last character in the minibuffer.
4923 ;; FIXME: This hack should be moved to complete.el where we call
4924 ;; display-completion-list.
4925 (when partial-completion-mode
4926 (setq common-string-length
4927 (if (eq (char-after (field-beginning)) ?-)
4928 ;; If the text to be completed starts with a `-', there is no
4929 ;; common prefix.
4930 ;; FIXME: this probably still doesn't do the right thing
4931 ;; when completing file names. It's not even clear what
4932 ;; is TRT.
4934 (- common-string-length (- (point-max) (point))))))
4935 (with-current-buffer standard-output
4936 (completion-list-mode)
4937 (set (make-local-variable 'completion-reference-buffer) mainbuf)
4938 (setq completion-base-size
4939 (if minibuffer-completing-file-name
4940 ;; For file name completion, use the number of chars before
4941 ;; the start of the last file name component.
4942 (with-current-buffer mainbuf
4943 (save-excursion
4944 (goto-char (point-max))
4945 (skip-chars-backward completion-root-regexp)
4946 (- (point) (minibuffer-prompt-end))))
4947 ;; Otherwise, in minibuffer, the whole input is being completed.
4948 (if (minibufferp mainbuf) 0)))
4949 (if (and (symbolp minibuffer-completion-table)
4950 (get minibuffer-completion-table 'completion-base-size-function))
4951 (setq completion-base-size
4952 ;; FIXME: without any extra arg, how is this function
4953 ;; expected to return anything else than a constant unless
4954 ;; it redoes part of the work of all-completions?
4955 ;; In most cases this value would better be computed and
4956 ;; returned at the same time as the list of all-completions
4957 ;; is computed. --Stef
4958 (funcall (get minibuffer-completion-table
4959 'completion-base-size-function))))
4960 ;; Put faces on first uncommon characters and common parts.
4961 (when (or completion-common-substring completion-base-size)
4962 (setq common-string-length
4963 (if completion-common-substring
4964 (length completion-common-substring)
4965 (- common-string-length completion-base-size)))
4966 (let ((element-start (point-min))
4967 (maxp (point-max))
4968 element-common-end)
4969 (while (and (setq element-start
4970 (next-single-property-change
4971 element-start 'mouse-face))
4972 (< (setq element-common-end
4973 (+ element-start common-string-length))
4974 maxp))
4975 (when (get-char-property element-start 'mouse-face)
4976 (if (get-char-property (1- element-common-end) 'mouse-face)
4977 (put-text-property element-start element-common-end
4978 'font-lock-face 'completions-common-part))
4979 (if (get-char-property element-common-end 'mouse-face)
4980 (put-text-property element-common-end (1+ element-common-end)
4981 'font-lock-face 'completions-first-difference))))))
4982 ;; Insert help string.
4983 (goto-char (point-min))
4984 (if (display-mouse-p)
4985 (insert (substitute-command-keys
4986 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4987 (insert (substitute-command-keys
4988 "In this buffer, type \\[choose-completion] to \
4989 select the completion near point.\n\n")))))
4991 (add-hook 'completion-setup-hook 'completion-setup-function)
4993 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
4994 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
4996 (defun switch-to-completions ()
4997 "Select the completion list window."
4998 (interactive)
4999 ;; Make sure we have a completions window.
5000 (or (get-buffer-window "*Completions*")
5001 (minibuffer-completion-help))
5002 (let ((window (get-buffer-window "*Completions*")))
5003 (when window
5004 (select-window window)
5005 (goto-char (point-min))
5006 (search-forward "\n\n")
5007 (forward-line 1))))
5009 ;; Support keyboard commands to turn on various modifiers.
5011 ;; These functions -- which are not commands -- each add one modifier
5012 ;; to the following event.
5014 (defun event-apply-alt-modifier (ignore-prompt)
5015 "\\<function-key-map>Add the Alt modifier to the following event.
5016 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
5017 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
5018 (defun event-apply-super-modifier (ignore-prompt)
5019 "\\<function-key-map>Add the Super modifier to the following event.
5020 For example, type \\[event-apply-super-modifier] & to enter Super-&."
5021 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
5022 (defun event-apply-hyper-modifier (ignore-prompt)
5023 "\\<function-key-map>Add the Hyper modifier to the following event.
5024 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
5025 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
5026 (defun event-apply-shift-modifier (ignore-prompt)
5027 "\\<function-key-map>Add the Shift modifier to the following event.
5028 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
5029 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
5030 (defun event-apply-control-modifier (ignore-prompt)
5031 "\\<function-key-map>Add the Ctrl modifier to the following event.
5032 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
5033 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
5034 (defun event-apply-meta-modifier (ignore-prompt)
5035 "\\<function-key-map>Add the Meta modifier to the following event.
5036 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
5037 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
5039 (defun event-apply-modifier (event symbol lshiftby prefix)
5040 "Apply a modifier flag to event EVENT.
5041 SYMBOL is the name of this modifier, as a symbol.
5042 LSHIFTBY is the numeric value of this modifier, in keyboard events.
5043 PREFIX is the string that represents this modifier in an event type symbol."
5044 (if (numberp event)
5045 (cond ((eq symbol 'control)
5046 (if (and (<= (downcase event) ?z)
5047 (>= (downcase event) ?a))
5048 (- (downcase event) ?a -1)
5049 (if (and (<= (downcase event) ?Z)
5050 (>= (downcase event) ?A))
5051 (- (downcase event) ?A -1)
5052 (logior (lsh 1 lshiftby) event))))
5053 ((eq symbol 'shift)
5054 (if (and (<= (downcase event) ?z)
5055 (>= (downcase event) ?a))
5056 (upcase event)
5057 (logior (lsh 1 lshiftby) event)))
5059 (logior (lsh 1 lshiftby) event)))
5060 (if (memq symbol (event-modifiers event))
5061 event
5062 (let ((event-type (if (symbolp event) event (car event))))
5063 (setq event-type (intern (concat prefix (symbol-name event-type))))
5064 (if (symbolp event)
5065 event-type
5066 (cons event-type (cdr event)))))))
5068 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
5069 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
5070 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
5071 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
5072 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
5073 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
5075 ;;;; Keypad support.
5077 ;;; Make the keypad keys act like ordinary typing keys. If people add
5078 ;;; bindings for the function key symbols, then those bindings will
5079 ;;; override these, so this shouldn't interfere with any existing
5080 ;;; bindings.
5082 ;; Also tell read-char how to handle these keys.
5083 (mapc
5084 (lambda (keypad-normal)
5085 (let ((keypad (nth 0 keypad-normal))
5086 (normal (nth 1 keypad-normal)))
5087 (put keypad 'ascii-character normal)
5088 (define-key function-key-map (vector keypad) (vector normal))))
5089 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
5090 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
5091 (kp-space ?\s)
5092 (kp-tab ?\t)
5093 (kp-enter ?\r)
5094 (kp-multiply ?*)
5095 (kp-add ?+)
5096 (kp-separator ?,)
5097 (kp-subtract ?-)
5098 (kp-decimal ?.)
5099 (kp-divide ?/)
5100 (kp-equal ?=)))
5102 ;;;;
5103 ;;;; forking a twin copy of a buffer.
5104 ;;;;
5106 (defvar clone-buffer-hook nil
5107 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
5109 (defun clone-process (process &optional newname)
5110 "Create a twin copy of PROCESS.
5111 If NEWNAME is nil, it defaults to PROCESS' name;
5112 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
5113 If PROCESS is associated with a buffer, the new process will be associated
5114 with the current buffer instead.
5115 Returns nil if PROCESS has already terminated."
5116 (setq newname (or newname (process-name process)))
5117 (if (string-match "<[0-9]+>\\'" newname)
5118 (setq newname (substring newname 0 (match-beginning 0))))
5119 (when (memq (process-status process) '(run stop open))
5120 (let* ((process-connection-type (process-tty-name process))
5121 (new-process
5122 (if (memq (process-status process) '(open))
5123 (let ((args (process-contact process t)))
5124 (setq args (plist-put args :name newname))
5125 (setq args (plist-put args :buffer
5126 (if (process-buffer process)
5127 (current-buffer))))
5128 (apply 'make-network-process args))
5129 (apply 'start-process newname
5130 (if (process-buffer process) (current-buffer))
5131 (process-command process)))))
5132 (set-process-query-on-exit-flag
5133 new-process (process-query-on-exit-flag process))
5134 (set-process-inherit-coding-system-flag
5135 new-process (process-inherit-coding-system-flag process))
5136 (set-process-filter new-process (process-filter process))
5137 (set-process-sentinel new-process (process-sentinel process))
5138 (set-process-plist new-process (copy-sequence (process-plist process)))
5139 new-process)))
5141 ;; things to maybe add (currently partly covered by `funcall mode'):
5142 ;; - syntax-table
5143 ;; - overlays
5144 (defun clone-buffer (&optional newname display-flag)
5145 "Create and return a twin copy of the current buffer.
5146 Unlike an indirect buffer, the new buffer can be edited
5147 independently of the old one (if it is not read-only).
5148 NEWNAME is the name of the new buffer. It may be modified by
5149 adding or incrementing <N> at the end as necessary to create a
5150 unique buffer name. If nil, it defaults to the name of the
5151 current buffer, with the proper suffix. If DISPLAY-FLAG is
5152 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
5153 clone a file-visiting buffer, or a buffer whose major mode symbol
5154 has a non-nil `no-clone' property, results in an error.
5156 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
5157 current buffer with appropriate suffix. However, if a prefix
5158 argument is given, then the command prompts for NEWNAME in the
5159 minibuffer.
5161 This runs the normal hook `clone-buffer-hook' in the new buffer
5162 after it has been set up properly in other respects."
5163 (interactive
5164 (progn
5165 (if buffer-file-name
5166 (error "Cannot clone a file-visiting buffer"))
5167 (if (get major-mode 'no-clone)
5168 (error "Cannot clone a buffer in %s mode" mode-name))
5169 (list (if current-prefix-arg (read-string "Name: "))
5170 t)))
5171 (if buffer-file-name
5172 (error "Cannot clone a file-visiting buffer"))
5173 (if (get major-mode 'no-clone)
5174 (error "Cannot clone a buffer in %s mode" mode-name))
5175 (setq newname (or newname (buffer-name)))
5176 (if (string-match "<[0-9]+>\\'" newname)
5177 (setq newname (substring newname 0 (match-beginning 0))))
5178 (let ((buf (current-buffer))
5179 (ptmin (point-min))
5180 (ptmax (point-max))
5181 (pt (point))
5182 (mk (if mark-active (mark t)))
5183 (modified (buffer-modified-p))
5184 (mode major-mode)
5185 (lvars (buffer-local-variables))
5186 (process (get-buffer-process (current-buffer)))
5187 (new (generate-new-buffer (or newname (buffer-name)))))
5188 (save-restriction
5189 (widen)
5190 (with-current-buffer new
5191 (insert-buffer-substring buf)))
5192 (with-current-buffer new
5193 (narrow-to-region ptmin ptmax)
5194 (goto-char pt)
5195 (if mk (set-mark mk))
5196 (set-buffer-modified-p modified)
5198 ;; Clone the old buffer's process, if any.
5199 (when process (clone-process process))
5201 ;; Now set up the major mode.
5202 (funcall mode)
5204 ;; Set up other local variables.
5205 (mapcar (lambda (v)
5206 (condition-case () ;in case var is read-only
5207 (if (symbolp v)
5208 (makunbound v)
5209 (set (make-local-variable (car v)) (cdr v)))
5210 (error nil)))
5211 lvars)
5213 ;; Run any hooks (typically set up by the major mode
5214 ;; for cloning to work properly).
5215 (run-hooks 'clone-buffer-hook))
5216 (if display-flag (pop-to-buffer new))
5217 new))
5220 (defun clone-indirect-buffer (newname display-flag &optional norecord)
5221 "Create an indirect buffer that is a twin copy of the current buffer.
5223 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
5224 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
5225 or if not called with a prefix arg, NEWNAME defaults to the current
5226 buffer's name. The name is modified by adding a `<N>' suffix to it
5227 or by incrementing the N in an existing suffix.
5229 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
5230 This is always done when called interactively.
5232 Optional third arg NORECORD non-nil means do not put this buffer at the
5233 front of the list of recently selected ones."
5234 (interactive
5235 (progn
5236 (if (get major-mode 'no-clone-indirect)
5237 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5238 (list (if current-prefix-arg
5239 (read-string "BName of indirect buffer: "))
5240 t)))
5241 (if (get major-mode 'no-clone-indirect)
5242 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5243 (setq newname (or newname (buffer-name)))
5244 (if (string-match "<[0-9]+>\\'" newname)
5245 (setq newname (substring newname 0 (match-beginning 0))))
5246 (let* ((name (generate-new-buffer-name newname))
5247 (buffer (make-indirect-buffer (current-buffer) name t)))
5248 (when display-flag
5249 (pop-to-buffer buffer norecord))
5250 buffer))
5253 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
5254 "Create an indirect buffer that is a twin copy of BUFFER.
5255 Select the new buffer in another window.
5256 Optional second arg NORECORD non-nil means do not put this buffer at
5257 the front of the list of recently selected ones."
5258 (interactive "bClone buffer in other window: ")
5259 (let ((pop-up-windows t))
5260 (set-buffer buffer)
5261 (clone-indirect-buffer nil t norecord)))
5264 ;;; Handling of Backspace and Delete keys.
5266 (defcustom normal-erase-is-backspace
5267 (and (not noninteractive)
5268 (or (memq system-type '(ms-dos windows-nt))
5269 (eq window-system 'mac)
5270 (and (memq window-system '(x))
5271 (fboundp 'x-backspace-delete-keys-p)
5272 (x-backspace-delete-keys-p))
5273 ;; If the terminal Emacs is running on has erase char
5274 ;; set to ^H, use the Backspace key for deleting
5275 ;; backward and, and the Delete key for deleting forward.
5276 (and (null window-system)
5277 (eq tty-erase-char ?\^H))))
5278 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
5280 On window systems, the default value of this option is chosen
5281 according to the keyboard used. If the keyboard has both a Backspace
5282 key and a Delete key, and both are mapped to their usual meanings, the
5283 option's default value is set to t, so that Backspace can be used to
5284 delete backward, and Delete can be used to delete forward.
5286 If not running under a window system, customizing this option accomplishes
5287 a similar effect by mapping C-h, which is usually generated by the
5288 Backspace key, to DEL, and by mapping DEL to C-d via
5289 `keyboard-translate'. The former functionality of C-h is available on
5290 the F1 key. You should probably not use this setting if you don't
5291 have both Backspace, Delete and F1 keys.
5293 Setting this variable with setq doesn't take effect. Programmatically,
5294 call `normal-erase-is-backspace-mode' (which see) instead."
5295 :type 'boolean
5296 :group 'editing-basics
5297 :version "21.1"
5298 :set (lambda (symbol value)
5299 ;; The fboundp is because of a problem with :set when
5300 ;; dumping Emacs. It doesn't really matter.
5301 (if (fboundp 'normal-erase-is-backspace-mode)
5302 (normal-erase-is-backspace-mode (or value 0))
5303 (set-default symbol value))))
5306 (defun normal-erase-is-backspace-mode (&optional arg)
5307 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
5309 With numeric arg, turn the mode on if and only if ARG is positive.
5311 On window systems, when this mode is on, Delete is mapped to C-d and
5312 Backspace is mapped to DEL; when this mode is off, both Delete and
5313 Backspace are mapped to DEL. (The remapping goes via
5314 `function-key-map', so binding Delete or Backspace in the global or
5315 local keymap will override that.)
5317 In addition, on window systems, the bindings of C-Delete, M-Delete,
5318 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
5319 the global keymap in accordance with the functionality of Delete and
5320 Backspace. For example, if Delete is remapped to C-d, which deletes
5321 forward, C-Delete is bound to `kill-word', but if Delete is remapped
5322 to DEL, which deletes backward, C-Delete is bound to
5323 `backward-kill-word'.
5325 If not running on a window system, a similar effect is accomplished by
5326 remapping C-h (normally produced by the Backspace key) and DEL via
5327 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
5328 to C-d; if it's off, the keys are not remapped.
5330 When not running on a window system, and this mode is turned on, the
5331 former functionality of C-h is available on the F1 key. You should
5332 probably not turn on this mode on a text-only terminal if you don't
5333 have both Backspace, Delete and F1 keys.
5335 See also `normal-erase-is-backspace'."
5336 (interactive "P")
5337 (setq normal-erase-is-backspace
5338 (if arg
5339 (> (prefix-numeric-value arg) 0)
5340 (not normal-erase-is-backspace)))
5342 (cond ((or (memq window-system '(x w32 mac pc))
5343 (memq system-type '(ms-dos windows-nt)))
5344 (let ((bindings
5345 `(([C-delete] [C-backspace])
5346 ([M-delete] [M-backspace])
5347 ([C-M-delete] [C-M-backspace])
5348 (,esc-map
5349 [C-delete] [C-backspace])))
5350 (old-state (lookup-key function-key-map [delete])))
5352 (if normal-erase-is-backspace
5353 (progn
5354 (define-key function-key-map [delete] [?\C-d])
5355 (define-key function-key-map [kp-delete] [?\C-d])
5356 (define-key function-key-map [backspace] [?\C-?]))
5357 (define-key function-key-map [delete] [?\C-?])
5358 (define-key function-key-map [kp-delete] [?\C-?])
5359 (define-key function-key-map [backspace] [?\C-?]))
5361 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5362 (unless (equal old-state (lookup-key function-key-map [delete]))
5363 (dolist (binding bindings)
5364 (let ((map global-map))
5365 (when (keymapp (car binding))
5366 (setq map (car binding) binding (cdr binding)))
5367 (let* ((key1 (nth 0 binding))
5368 (key2 (nth 1 binding))
5369 (binding1 (lookup-key map key1))
5370 (binding2 (lookup-key map key2)))
5371 (define-key map key1 binding2)
5372 (define-key map key2 binding1)))))))
5374 (if normal-erase-is-backspace
5375 (progn
5376 (keyboard-translate ?\C-h ?\C-?)
5377 (keyboard-translate ?\C-? ?\C-d))
5378 (keyboard-translate ?\C-h ?\C-h)
5379 (keyboard-translate ?\C-? ?\C-?))))
5381 (run-hooks 'normal-erase-is-backspace-hook)
5382 (if (interactive-p)
5383 (message "Delete key deletes %s"
5384 (if normal-erase-is-backspace "forward" "backward"))))
5386 (defvar vis-mode-saved-buffer-invisibility-spec nil
5387 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5389 (define-minor-mode visible-mode
5390 "Toggle Visible mode.
5391 With argument ARG turn Visible mode on iff ARG is positive.
5393 Enabling Visible mode makes all invisible text temporarily visible.
5394 Disabling Visible mode turns off that effect. Visible mode
5395 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5396 :lighter " Vis"
5397 :group 'editing-basics
5398 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5399 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5400 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5401 (when visible-mode
5402 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5403 buffer-invisibility-spec)
5404 (setq buffer-invisibility-spec nil)))
5406 ;; Minibuffer prompt stuff.
5408 ;(defun minibuffer-prompt-modification (start end)
5409 ; (error "You cannot modify the prompt"))
5412 ;(defun minibuffer-prompt-insertion (start end)
5413 ; (let ((inhibit-modification-hooks t))
5414 ; (delete-region start end)
5415 ; ;; Discard undo information for the text insertion itself
5416 ; ;; and for the text deletion.above.
5417 ; (when (consp buffer-undo-list)
5418 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5419 ; (message "You cannot modify the prompt")))
5422 ;(setq minibuffer-prompt-properties
5423 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5424 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5427 (provide 'simple)
5429 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5430 ;;; simple.el ends here