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.
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)
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.
28 ;; A grab-bag of basic Emacs commands not specifically related to some
29 ;; major mode or to file-handling.
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."
48 "Killing and yanking commands."
51 (defgroup paren-matching nil
52 "Highlight (un)matching of parens and expressions."
56 "Switch to the next buffer in cyclic order."
58 (let ((buffer (current-buffer)))
59 (switch-to-buffer (other-buffer buffer
))
60 (bury-buffer buffer
)))
63 "Switch to the previous buffer in cyclic order."
65 (let ((list (nreverse (buffer-list)))
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
))))
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."
83 '((t (:inherit region
)))
84 "Face used to highlight next error locus."
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
))
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
))
114 (defcustom next-error-hook nil
115 "*List of hook functions run by `next-error' after visiting source file."
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
141 (make-variable-buffer-local 'next-error-function
)
143 (defsubst next-error-buffer-p
(buffer
144 &optional avoid-current
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
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
189 (delq nil
(mapcar (lambda (w)
190 (if (next-error-buffer-p
193 extra-test-inclusive extra-test-exclusive
)
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
)
207 ;; 4. Look for any acceptable buffer.
208 (let ((buffers (buffer-list)))
210 (not (next-error-buffer-p
211 (car buffers
) avoid-current
212 extra-test-inclusive extra-test-exclusive
)))
213 (setq buffers
(cdr buffers
)))
215 ;; 5. Use the current buffer as a last resort if it qualifies,
216 ;; even despite AVOID-CURRENT.
218 (next-error-buffer-p (current-buffer) nil
219 extra-test-inclusive extra-test-exclusive
)
221 (message "This is the only next-error capable buffer")
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."
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."
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."
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."
300 (let ((next-error-highlight next-error-highlight-no-select
))
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."
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
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))
333 (let ((compilation-context-lines nil
))
334 (setq compilation-current-error
(point))
335 (next-error-no-select 0))
341 (defun fundamental-mode ()
342 "Major mode not specialized for anything in particular.
343 Other major modes are defined by comparison with this one."
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."
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))
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
))
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)
391 (was-page-start (and (bolp)
392 (looking-at page-delimiter
)))
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
)))
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.
418 (goto-char beforepos
)
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.
427 (move-to-left-margin nil t
)))
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
)))))
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."
444 (let* ((do-fill-prefix (and fill-prefix
(bolp)))
445 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
447 ;; Don't expand an abbrev before point.
453 (if do-left-margin
(indent-to (current-left-margin)))
454 (if do-fill-prefix
(insert-and-inherit fill-prefix
))))
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."
467 (skip-chars-forward " \t")
468 (let* ((col (current-column))
470 ;; What prefix should we check for (nil means don't).
471 (prefix (cond ((stringp arg
) arg
)
474 ;; Does this line start with it?
475 (have-prfx (and prefix
478 (looking-at (regexp-quote prefix
))))))
480 (if have-prfx
(insert-and-inherit prefix
))
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."
490 (if arg
(forward-line 1))
491 (if (eq (preceding-char) ?
\n)
493 (delete-region (point) (1- (point)))
494 ;; If the second line started with the fill prefix,
495 ;; delete the prefix.
497 (<= (+ (point) (length fill-prefix
)) (point-max))
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."
511 (let (thisblank singleblank
)
514 (setq thisblank
(looking-at "[ \t]*$"))
515 ;; Set singleblank if there is just one blank line here.
518 (not (looking-at "[ \t]*\n[ \t]*$"))
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.
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))
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
))
537 (delete-region (point)
538 (if (re-search-forward "[^ \t\n]" nil t
)
539 (progn (beginning-of-line) (point))
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."
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.
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'."
570 (delete-horizontal-space t
)
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'."
583 ;; Be careful to insert the newline before indenting the line.
584 ;; Otherwise, the indentation might be wrong.
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."
612 (let* ((char (let (translation-table-for-input)
613 (if (or (not overwrite-mode
)
614 (eq overwrite-mode
'overwrite-mode-binary
))
617 ;; Assume character codes 0240 - 0377 stand for characters in some
618 ;; single-byte character set, and convert them to Emacs
620 (if (and enable-multibyte-characters
623 (setq char
(unibyte-char-to-multibyte char
)))
625 (if (eq overwrite-mode
'overwrite-mode-binary
)
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."
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."
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."
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."
656 (delete-horizontal-space)
657 (if (or (looking-at "^\\|\\s)")
658 (save-excursion (forward-char -
1)
659 (looking-at "$\\|\\s(\\|\\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."
667 (let ((orig-pos (point)))
672 (skip-chars-forward " \t")
673 (constrain-to-field nil orig-pos t
)))
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)."
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
)
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."
706 (and transient-mark-mode mark-active
)
708 (let ((size (- (point-max) (point-min))))
709 (goto-char (if (and arg
(not (consp arg
)))
712 ;; Avoid overflow for large buffer sizes!
713 (* (prefix-numeric-value arg
)
715 (/ (+ 10 (* size
(prefix-numeric-value arg
))) 10)))
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."
731 (and transient-mark-mode mark-active
)
733 (let ((size (- (point-max) (point-min))))
734 (goto-char (if (and arg
(not (consp arg
)))
737 ;; Avoid overflow for large buffer sizes!
738 (* (prefix-numeric-value arg
)
740 (/ (* size
(prefix-numeric-value arg
)) 10)))
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))
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."
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."
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.
778 (skip-chars-backward "0-9")
779 (if (looking-at "[0-9]")
780 (buffer-substring-no-properties
782 (progn (skip-chars-forward "0-9")
784 ;; Decide if we're switching buffers.
786 (if (consp current-prefix-arg
)
787 (other-buffer (current-buffer) t
)))
790 (concat " in " (buffer-name buffer
))
792 ;; Read the argument, offering that number (if any) as default.
793 (list (read-from-minibuffer (format (if default
"Goto line%s (%s): "
801 ;; Switch to the desired buffer, one way or another.
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.
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."
817 (message "Region has %d lines, %d characters"
818 (count-lines start end
) (- end start
)))
821 "Print the current buffer line number and narrowed line number of point."
823 (let ((start (point-min))
824 (n (line-number-at-pos)))
826 (message "Line %d" n
)
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."
840 (narrow-to-region start end
)
841 (goto-char (point-min))
842 (if (eq selective-display t
)
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
)
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
)
861 (goto-char (point-min))
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'."
881 (let* ((char (following-char))
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)))
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
)
903 (eq (coding-system-type coding
) t
))
904 (setq coding default-buffer-file-coding-system
))
905 (if (not (char-valid-p char
))
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
))
913 (let ((to (or (next-single-property-change pos
'display
)
916 (setq under-display
"")
917 (setq under-display
"..."
920 (concat (buffer-substring-no-properties pos to
)
922 (setq encoded
(and (>= char
128) (encode-coding-char char coding
))))
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
))
931 (format "(%d, #o%o, #x%x, file %s)"
933 (if (> (length encoded
) 1)
935 (encoded-string-description encoded coding
)))
936 (format "(%d, #o%o, #x%x)" char char char
)))))
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"
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
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."
967 :type
'(choice (const :tag
"No Limit" nil
) integer
)
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."
974 :type
'(choice (const :tag
"No Limit" nil
) integer
)
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'."
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
)))
994 (if (or (if (boundp 'edebug-active
) edebug-active
)
995 (memq this-command
'(eval-last-sexp eval-print-last-sexp
)))
996 (prin1-char value
))))
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
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
1033 (let ((standard-output (current-buffer)))
1034 (eval-last-sexp-print-value (car values
))))
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."
1045 (let ((print-level nil
)
1046 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
1048 (read-from-minibuffer prompt
1049 (prin1-to-string command
)
1050 read-expression-map t
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
)))
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."
1073 (let ((elt (nth (1- arg
) command-history
))
1078 (let ((print-level nil
)
1079 (minibuffer-history-position arg
)
1080 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
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
)))
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
)
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'."
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"))
1160 (prefix-numeric-value current-prefix-arg
))))
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
))
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:
1179 (pos minibuffer-history-position
))
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")))
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
)))
1194 (and (string-match regexp match-string
)
1196 (and (string-match (concat ".*\\(" regexp
"\\)") match-string
)
1197 (match-beginning 1))))
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."
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"))
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."
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
)
1260 (goto-char (point-max))
1261 (delete-minibuffer-contents)
1262 (setq minibuffer-history-position narg
)
1264 (setq elt minibuffer-default
))
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
)))))
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
))
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."
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."
1289 (let ((point-at-start (point)))
1290 (next-matching-history-element
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."
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."
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))
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
)))))
1369 (undo-start (region-beginning) (region-end))
1371 ;; get rid of initial undo boundary
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
)))
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
)
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
)
1405 (when (integerp (car tail
))
1406 (let ((pos (car tail
)))
1408 (setcdr prev
(cdr tail
))
1409 (setq buffer-undo-list
(cdr tail
)))
1410 (setq tail
(cdr tail
))
1412 (if (eq pos
(car tail
))
1414 (setcdr prev
(cdr tail
))
1415 (setq buffer-undo-list
(cdr tail
)))
1417 (setq tail
(cdr tail
)))
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.
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."
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."
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
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)
1467 (cons (car elt
) (undo-copy-list-1 (cdr 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
))
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
1495 undo-elt undo-elt temp-undo-list delta
)
1496 (while undo-list-copy
1497 (setq undo-elt
(car undo-list-copy
))
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
)))))
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
)
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
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
))
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
)
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
)))
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
))
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
))
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*
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
))
1618 (cons (car undo-elt
) (- (car undo-elt
) (cdr undo-elt
))))
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."
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
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
)
1671 (display-warning '(undo discard-info
)
1673 (format "Buffer %s undo info was %d bytes long.\n"
1675 "The undo info was discarded because it exceeded \
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")
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
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
)
1759 shell-command-default-error-buffer
))
1760 ;; Look for a handler in case default-directory is a remote file name.
1762 (find-file-name-handler (directory-file-name default-directory
)
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.
1772 (expand-file-name "scor"
1773 (or small-temporary-file-directory
1774 temporary-file-directory
)))
1776 (barf-if-buffer-read-only)
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
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))))
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.
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
)
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
))
1823 (if (yes-or-no-p "A command is running. Kill it? ")
1825 (error "Shell command in progress")))
1826 (with-current-buffer buffer
1827 (setq buffer-read-only nil
)
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
)))))
1869 (with-current-buffer
1870 (if (bufferp message
)
1872 (get-buffer-create (or buffer-name
"*Message*")))
1874 (unless (bufferp message
)
1879 (if (= (buffer-size) 0)
1881 (count-lines (point-min) (point-max)))))
1883 ((and (or (<= lines
1)
1885 (if resize-mini-windows
1886 (cond ((floatp max-mini-window-height
)
1888 max-mini-window-height
))
1889 ((integerp max-mini-window-height
)
1890 max-mini-window-height
)
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))))
1898 (goto-char (point-max))
1901 (message "%s" (buffer-substring (point-min) (point))))
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
))
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
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
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)
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: "
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)
1984 shell-command-default-error-buffer
1989 (expand-file-name "scor"
1990 (or small-temporary-file-directory
1991 temporary-file-directory
)))
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.
2001 (and replace
(push-mark (point) 'nomsg
))
2003 (call-process-region start end shell-file-name t
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*"))))
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
))
2027 (call-process-region (point-min) (point-max)
2032 nil shell-command-switch
2034 ;; Clear the output buffer, then run the command with
2036 (let ((directory default-directory
))
2039 (setq buffer-read-only nil
)
2040 (if (not output-buffer
)
2041 (setq default-directory directory
))
2044 (call-process-region start end shell-file-name nil
2046 (list buffer error-file
)
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
)
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?
2064 (< 0 (nth 7 (file-attributes error-file
))))
2067 (cond ((null exit-status
)
2068 (message "(Shell command failed with error)"))
2069 ((equal 0 exit-status
)
2070 (message "(Shell command succeeded with %s)"
2072 ((stringp exit-status
)
2073 (message "(Shell command killed by signal %s)"
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)
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))))
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
))
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
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
2118 Some file handlers might not support all variants, for example
2119 they might behave as if DISPLAY was nil, regardless of the actual
2121 (let ((fh (find-file-name-handler default-directory
'process-file
))
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")))
2129 (apply 'call-process program
2131 (if stderr-file
(list (car buffer
) stderr-file
) buffer
)
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)
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
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]."
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)
2215 (setq prefix-arg (list (* 4 (car 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."
2226 (cond ((integerp arg)
2227 (setq prefix-arg (- 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."
2239 (let* ((char (if (integerp 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))))
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)
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)
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
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."
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
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
2363 (defcustom kill-ring-max 60
2364 "*Maximum length of kill ring before oldest elements are thrown away."
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)
2389 (put-text-property 0 (length string)
2390 'yank-handler yank-handler string))
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)))
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
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))
2439 (or kill-ring (error "Kill ring is empty"))
2440 (let ((ARGth-kill-element
2441 (nthcdr (mod (- n (length kill-ring-yank-pointer))
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."
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
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'."
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))
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."
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))
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."
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.
2545 (let ((other-end (if (= (point) beg) end beg))
2547 ;; Inhibit quitting so we can make a quit here
2548 ;; look like a C-g typed as a command.
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)
2558 (set-marker (mark-marker) other-end (current-buffer))
2560 ;; If user quit, deactivate the mark
2561 ;; as C-g would as a command.
2562 (and quit-flag mark-active
2564 (let* ((killed-text (current-kill 0))
2565 (message-len (min (length killed-text) 40)))
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."
2577 ;; We don't use (interactive-p), since that breaks kbd macros.
2580 (setq this-command 'kill-region)
2581 (message "If the next command is a kill, it will append"))
2582 (setq last-command 'kill-region)))
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))
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."
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))))
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,
2636 (set-window-start (selected-window) yank-window-start t)
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))))))
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
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]."
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)
2664 (insert-for-yank (current-kill (cond
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))
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)."
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))
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)
2719 (while (and (> count 0) (not (bobp)))
2720 (if (= (preceding-char) ?\t)
2721 (let ((col (current-column)))
2723 (setq col (- col (current-column)))
2724 (insert-char ?\s col)
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)
2733 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2735 (+ arg (if (zerop wh) 0 (1- wh))))
2739 (defun zap-to-char (arg char)
2740 "Kill up to and including ARG'th occurrence of CHAR.
2741 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2742 Goes backward if ARG is negative; error if CHAR not found."
2743 (interactive "p\ncZap to char: ")
2744 (kill-region (point) (progn
2745 (search-forward (char-to-string char) nil nil arg)
2746 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2749 ;; kill-line and its subroutines.
2751 (defcustom kill-whole-line nil
2752 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2756 (defun kill-line (&optional arg)
2757 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2758 With prefix argument, kill that many lines from point.
2759 Negative arguments kill lines backward.
2760 With zero argument, kills the text before point on the current line.
2762 When calling from a program, nil means \"no arg\",
2763 a number counts as a prefix arg.
2765 To kill a whole line, when point is not at the beginning, type \
2766 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2768 If `kill-whole-line' is non-nil, then this command kills the whole line
2769 including its terminating newline, when used at the beginning of a line
2770 with no argument. As a consequence, you can always kill a whole line
2771 by typing \\[beginning-of-line] \\[kill-line].
2773 If you want to append the killed line to the last killed text,
2774 use \\[append-next-kill] before \\[kill-line].
2776 If the buffer is read-only, Emacs will beep and refrain from deleting
2777 the line, but put the line in the kill ring anyway. This means that
2778 you can use this command to copy text from a read-only buffer.
2779 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2782 (kill-region (point)
2783 ;; It is better to move point to the other end of the kill
2784 ;; before killing. That way, in a read-only buffer, point
2785 ;; moves across the text that is copied to the kill ring.
2786 ;; The choice has no effect on undo now that undo records
2787 ;; the value of point from before the command was run.
2790 (forward-visible-line (prefix-numeric-value arg))
2792 (signal 'end-of-buffer nil))
2795 (end-of-visible-line) (point))))
2796 (if (or (save-excursion
2797 ;; If trailing whitespace is visible,
2798 ;; don't treat it as nothing.
2799 (unless show-trailing-whitespace
2800 (skip-chars-forward " \t" end))
2802 (and kill-whole-line (bolp)))
2803 (forward-visible-line 1)
2807 (defun kill-whole-line (&optional arg)
2809 With prefix arg, kill that many lines starting from the current line.
2810 If arg is negative, kill backward. Also kill the preceding newline.
2811 \(This is meant to make \\[repeat] work well with negative arguments.\)
2812 If arg is zero, kill current line but exclude the trailing newline."
2814 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2815 (signal 'end-of-buffer nil))
2816 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2817 (signal 'beginning-of-buffer nil))
2818 (unless (eq last-command 'kill-region)
2820 (setq last-command 'kill-region))
2822 ;; We need to kill in two steps, because the previous command
2823 ;; could have been a kill command, in which case the text
2824 ;; before point needs to be prepended to the current kill
2825 ;; ring entry and the text after point appended. Also, we
2826 ;; need to use save-excursion to avoid copying the same text
2827 ;; twice to the kill ring in read-only buffers.
2829 (kill-region (point) (progn (forward-visible-line 0) (point))))
2830 (kill-region (point) (progn (end-of-visible-line) (point))))
2833 (kill-region (point) (progn (end-of-visible-line) (point))))
2834 (kill-region (point)
2835 (progn (forward-visible-line (1+ arg))
2836 (unless (bobp) (backward-char))
2840 (kill-region (point) (progn (forward-visible-line 0) (point))))
2841 (kill-region (point)
2842 (progn (forward-visible-line arg) (point))))))
2844 (defun forward-visible-line (arg)
2845 "Move forward by ARG lines, ignoring currently invisible newlines only.
2846 If ARG is negative, move backward -ARG lines.
2847 If ARG is zero, move to the beginning of the current line."
2852 (or (zerop (forward-line 1))
2853 (signal 'end-of-buffer nil))
2854 ;; If the newline we just skipped is invisible,
2857 (get-char-property (1- (point)) 'invisible)))
2858 (if (if (eq buffer-invisibility-spec t)
2860 (or (memq prop buffer-invisibility-spec)
2861 (assq prop buffer-invisibility-spec)))
2862 (setq arg (1+ arg))))
2863 (setq arg (1- arg)))
2864 ;; If invisible text follows, and it is a number of complete lines,
2866 (let ((opoint (point)))
2867 (while (and (not (eobp))
2869 (get-char-property (point) 'invisible)))
2870 (if (eq buffer-invisibility-spec t)
2872 (or (memq prop buffer-invisibility-spec)
2873 (assq prop buffer-invisibility-spec)))))
2875 (if (get-text-property (point) 'invisible)
2876 (or (next-single-property-change (point) 'invisible)
2878 (next-overlay-change (point)))))
2880 (goto-char opoint))))
2882 (while (or first (<= arg 0))
2885 (or (zerop (forward-line -1))
2886 (signal 'beginning-of-buffer nil)))
2887 ;; If the newline we just moved to is invisible,
2891 (get-char-property (1- (point)) 'invisible)))
2892 (unless (if (eq buffer-invisibility-spec t)
2894 (or (memq prop buffer-invisibility-spec)
2895 (assq prop buffer-invisibility-spec)))
2896 (setq arg (1+ arg)))))
2898 ;; If invisible text follows, and it is a number of complete lines,
2900 (let ((opoint (point)))
2901 (while (and (not (bobp))
2903 (get-char-property (1- (point)) 'invisible)))
2904 (if (eq buffer-invisibility-spec t)
2906 (or (memq prop buffer-invisibility-spec)
2907 (assq prop buffer-invisibility-spec)))))
2909 (if (get-text-property (1- (point)) 'invisible)
2910 (or (previous-single-property-change (point) 'invisible)
2912 (previous-overlay-change (point)))))
2914 (goto-char opoint)))))
2915 ((beginning-of-buffer end-of-buffer)
2918 (defun end-of-visible-line ()
2919 "Move to end of current visible line."
2921 ;; If the following character is currently invisible,
2922 ;; skip all characters with that same `invisible' property value,
2923 ;; then find the next newline.
2924 (while (and (not (eobp))
2926 (skip-chars-forward "^\n")
2928 (get-char-property (point) 'invisible)))
2929 (if (eq buffer-invisibility-spec t)
2931 (or (memq prop buffer-invisibility-spec)
2932 (assq prop buffer-invisibility-spec))))))
2933 (skip-chars-forward "^\n")
2934 (if (get-text-property (point) 'invisible)
2935 (goto-char (next-single-property-change (point) 'invisible))
2936 (goto-char (next-overlay-change (point))))
2939 (defun insert-buffer (buffer)
2940 "Insert after point the contents of BUFFER.
2941 Puts mark after the inserted text.
2942 BUFFER may be a buffer or a buffer name.
2944 This function is meant for the user to run interactively.
2945 Don't call it from programs: use `insert-buffer-substring' instead!"
2949 (barf-if-buffer-read-only)
2950 (read-buffer "Insert buffer: "
2951 (if (eq (selected-window) (next-window (selected-window)))
2952 (other-buffer (current-buffer))
2953 (window-buffer (next-window (selected-window))))
2957 (insert-buffer-substring (get-buffer buffer))
2961 (defun append-to-buffer (buffer start end)
2962 "Append to specified buffer the text of the region.
2963 It is inserted into that buffer before its point.
2965 When calling from a program, give three arguments:
2966 BUFFER (or buffer name), START and END.
2967 START and END specify the portion of the current buffer to be copied."
2969 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2970 (region-beginning) (region-end)))
2971 (let ((oldbuf (current-buffer)))
2973 (let* ((append-to (get-buffer-create buffer))
2974 (windows (get-buffer-window-list append-to t t))
2976 (set-buffer append-to)
2977 (setq point (point))
2978 (barf-if-buffer-read-only)
2979 (insert-buffer-substring oldbuf start end)
2980 (dolist (window windows)
2981 (when (= (window-point window) point)
2982 (set-window-point window (point))))))))
2984 (defun prepend-to-buffer (buffer start end)
2985 "Prepend to specified buffer the text of the region.
2986 It is inserted into that buffer after its point.
2988 When calling from a program, give three arguments:
2989 BUFFER (or buffer name), START and END.
2990 START and END specify the portion of the current buffer to be copied."
2991 (interactive "BPrepend to buffer: \nr")
2992 (let ((oldbuf (current-buffer)))
2994 (set-buffer (get-buffer-create buffer))
2995 (barf-if-buffer-read-only)
2997 (insert-buffer-substring oldbuf start end)))))
2999 (defun copy-to-buffer (buffer start end)
3000 "Copy to specified buffer the text of the region.
3001 It is inserted into that buffer, replacing existing text there.
3003 When calling from a program, give three arguments:
3004 BUFFER (or buffer name), START and END.
3005 START and END specify the portion of the current buffer to be copied."
3006 (interactive "BCopy to buffer: \nr")
3007 (let ((oldbuf (current-buffer)))
3008 (with-current-buffer (get-buffer-create buffer)
3009 (barf-if-buffer-read-only)
3012 (insert-buffer-substring oldbuf start end)))))
3014 (put 'mark-inactive 'error-conditions '(mark-inactive error))
3015 (put 'mark-inactive 'error-message "The mark is not active now")
3017 (defvar activate-mark-hook nil
3018 "Hook run when the mark becomes active.
3019 It is also run at the end of a command, if the mark is active and
3020 it is possible that the region may have changed")
3022 (defvar deactivate-mark-hook nil
3023 "Hook run when the mark becomes inactive.")
3025 (defun mark (&optional force)
3026 "Return this buffer's mark value as integer; error if mark inactive.
3027 If optional argument FORCE is non-nil, access the mark value
3028 even if the mark is not currently active, and return nil
3029 if there is no mark at all.
3031 If you are using this in an editing command, you are most likely making
3032 a mistake; see the documentation of `set-mark'."
3033 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
3034 (marker-position (mark-marker))
3035 (signal 'mark-inactive nil)))
3037 ;; Many places set mark-active directly, and several of them failed to also
3038 ;; run deactivate-mark-hook. This shorthand should simplify.
3039 (defsubst deactivate-mark ()
3040 "Deactivate the mark by setting `mark-active' to nil.
3041 \(That makes a difference only in Transient Mark mode.)
3042 Also runs the hook `deactivate-mark-hook'."
3044 ((eq transient-mark-mode 'lambda)
3045 (setq transient-mark-mode nil))
3046 (transient-mark-mode
3047 (setq mark-active nil)
3048 (run-hooks 'deactivate-mark-hook))))
3050 (defun set-mark (pos)
3051 "Set this buffer's mark to POS. Don't use this function!
3052 That is to say, don't use this function unless you want
3053 the user to see that the mark has moved, and you want the previous
3054 mark position to be lost.
3056 Normally, when a new mark is set, the old one should go on the stack.
3057 This is why most applications should use `push-mark', not `set-mark'.
3059 Novice Emacs Lisp programmers often try to use the mark for the wrong
3060 purposes. The mark saves a location for the user's convenience.
3061 Most editing commands should not alter the mark.
3062 To remember a location for internal use in the Lisp program,
3063 store it in a Lisp variable. Example:
3065 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
3069 (setq mark-active t)
3070 (run-hooks 'activate-mark-hook)
3071 (set-marker (mark-marker) pos (current-buffer)))
3072 ;; Normally we never clear mark-active except in Transient Mark mode.
3073 ;; But when we actually clear out the mark value too,
3074 ;; we must clear mark-active in any mode.
3075 (setq mark-active nil)
3076 (run-hooks 'deactivate-mark-hook)
3077 (set-marker (mark-marker) nil)))
3079 (defvar mark-ring nil
3080 "The list of former marks of the current buffer, most recent first.")
3081 (make-variable-buffer-local 'mark-ring)
3082 (put 'mark-ring 'permanent-local t)
3084 (defcustom mark-ring-max 16
3085 "*Maximum size of mark ring. Start discarding off end if gets this big."
3087 :group 'editing-basics)
3089 (defvar global-mark-ring nil
3090 "The list of saved global marks, most recent first.")
3092 (defcustom global-mark-ring-max 16
3093 "*Maximum size of global mark ring. \
3094 Start discarding off end if gets this big."
3096 :group 'editing-basics)
3098 (defun pop-to-mark-command ()
3099 "Jump to mark, and pop a new position for mark off the ring
3100 \(does not affect global mark ring\)."
3103 (error "No mark set in this buffer")
3104 (goto-char (mark t))
3107 (defun push-mark-command (arg &optional nomsg)
3108 "Set mark at where point is.
3109 If no prefix arg and mark is already set there, just activate it.
3110 Display `Mark set' unless the optional second arg NOMSG is non-nil."
3112 (let ((mark (marker-position (mark-marker))))
3113 (if (or arg (null mark) (/= mark (point)))
3114 (push-mark nil nomsg t)
3115 (setq mark-active t)
3116 (run-hooks 'activate-mark-hook)
3118 (message "Mark activated")))))
3120 (defcustom set-mark-command-repeat-pop nil
3121 "*Non-nil means that repeating \\[set-mark-command] after popping will pop.
3122 This means that if you type C-u \\[set-mark-command] \\[set-mark-command]
3127 (defun set-mark-command (arg)
3128 "Set mark at where point is, or jump to mark.
3129 With no prefix argument, set mark, and push old mark position on local
3130 mark ring; also push mark on global mark ring if last mark was set in
3131 another buffer. Immediately repeating the command activates
3132 `transient-mark-mode' temporarily.
3134 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
3135 jump to mark, and pop a new position
3136 for mark off the local mark ring \(this does not affect the global
3137 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
3138 mark ring \(see `pop-global-mark'\).
3140 Repeating the \\[set-mark-command] command without the prefix jumps to
3141 the next position off the local (or global) mark ring.
3143 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
3144 \\[universal-argument] \\[set-mark-command], unconditionally
3145 set mark where point is.
3147 Novice Emacs Lisp programmers often try to use the mark for the wrong
3148 purposes. See the documentation of `set-mark' for more information."
3150 (if (eq transient-mark-mode 'lambda)
3151 (setq transient-mark-mode nil))
3153 ((and (consp arg) (> (prefix-numeric-value arg) 4))
3154 (push-mark-command nil))
3155 ((not (eq this-command 'set-mark-command))
3157 (pop-to-mark-command)
3158 (push-mark-command t)))
3159 ((and set-mark-command-repeat-pop
3160 (eq last-command 'pop-to-mark-command))
3161 (setq this-command 'pop-to-mark-command)
3162 (pop-to-mark-command))
3163 ((and set-mark-command-repeat-pop
3164 (eq last-command 'pop-global-mark)
3166 (setq this-command 'pop-global-mark)
3169 (setq this-command 'pop-to-mark-command)
3170 (pop-to-mark-command))
3171 ((and (eq last-command 'set-mark-command)
3172 mark-active (null transient-mark-mode))
3173 (setq transient-mark-mode 'lambda)
3174 (message "Transient-mark-mode temporarily enabled"))
3176 (push-mark-command nil))))
3178 (defun push-mark (&optional location nomsg activate)
3179 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
3180 If the last global mark pushed was not in the current buffer,
3181 also push LOCATION on the global mark ring.
3182 Display `Mark set' unless the optional second arg NOMSG is non-nil.
3183 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
3185 Novice Emacs Lisp programmers often try to use the mark for the wrong
3186 purposes. See the documentation of `set-mark' for more information.
3188 In Transient Mark mode, this does not activate the mark."
3189 (unless (null (mark t))
3190 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
3191 (when (> (length mark-ring) mark-ring-max)
3192 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
3193 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
3194 (set-marker (mark-marker) (or location (point)) (current-buffer))
3195 ;; Now push the mark on the global mark ring.
3196 (if (and global-mark-ring
3197 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
3198 ;; The last global mark pushed was in this same buffer.
3199 ;; Don't push another one.
3201 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
3202 (when (> (length global-mark-ring) global-mark-ring-max)
3203 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
3204 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
3205 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
3206 (message "Mark set"))
3207 (if (or activate (not transient-mark-mode))
3208 (set-mark (mark t)))
3212 "Pop off mark ring into the buffer's actual mark.
3213 Does not set point. Does nothing if mark ring is empty."
3215 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
3216 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
3217 (move-marker (car mark-ring) nil)
3218 (if (null (mark t)) (ding))
3219 (setq mark-ring (cdr mark-ring)))
3222 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
3223 (defun exchange-point-and-mark (&optional arg)
3224 "Put the mark where point is now, and point where the mark is now.
3225 This command works even when the mark is not active,
3226 and it reactivates the mark.
3227 With prefix arg, `transient-mark-mode' is enabled temporarily."
3231 (if (null transient-mark-mode)
3232 (setq transient-mark-mode 'lambda))
3235 (let ((omark (mark t)))
3237 (error "No mark set in this buffer"))
3242 (define-minor-mode transient-mark-mode
3243 "Toggle Transient Mark mode.
3244 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
3246 In Transient Mark mode, when the mark is active, the region is highlighted.
3247 Changing the buffer \"deactivates\" the mark.
3248 So do certain other operations that set the mark
3249 but whose main purpose is something else--for example,
3250 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
3252 You can also deactivate the mark by typing \\[keyboard-quit] or
3253 \\[keyboard-escape-quit].
3255 Many commands change their behavior when Transient Mark mode is in effect
3256 and the mark is active, by acting on the region instead of their usual
3257 default part of the buffer's text. Examples of such commands include
3258 \\[comment-dwim], \\[flush-lines], \\[keep-lines], \
3259 \\[query-replace], \\[query-replace-regexp], \\[ispell], and \\[undo].
3260 Invoke \\[apropos-documentation] and type \"transient\" or
3261 \"mark.*active\" at the prompt, to see the documentation of
3262 commands which are sensitive to the Transient Mark mode."
3263 :global t :group 'editing-basics :require nil)
3265 (defvar widen-automatically t
3266 "Non-nil means it is ok for commands to call `widen' when they want to.
3267 Some commands will do this in order to go to positions outside
3268 the current accessible part of the buffer.
3270 If `widen-automatically' is nil, these commands will do something else
3271 as a fallback, and won't change the buffer bounds.")
3273 (defun pop-global-mark ()
3274 "Pop off global mark ring and jump to the top location."
3276 ;; Pop entries which refer to non-existent buffers.
3277 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
3278 (setq global-mark-ring (cdr global-mark-ring)))
3279 (or global-mark-ring
3280 (error "No global mark set"))
3281 (let* ((marker (car global-mark-ring))
3282 (buffer (marker-buffer marker))
3283 (position (marker-position marker)))
3284 (setq global-mark-ring (nconc (cdr global-mark-ring)
3285 (list (car global-mark-ring))))
3287 (or (and (>= position (point-min))
3288 (<= position (point-max)))
3289 (if widen-automatically
3291 (error "Global mark position is outside accessible part of buffer")))
3292 (goto-char position)
3293 (switch-to-buffer buffer)))
3295 (defcustom next-line-add-newlines nil
3296 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
3299 :group 'editing-basics)
3301 (defun next-line (&optional arg try-vscroll)
3302 "Move cursor vertically down ARG lines.
3303 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3304 If there is no character in the target line exactly under the current column,
3305 the cursor is positioned after the character in that line which spans this
3306 column, or at the end of the line if it is not long enough.
3307 If there is no line in the buffer after this one, behavior depends on the
3308 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3309 to create a line, and moves the cursor to that line. Otherwise it moves the
3310 cursor to the end of the buffer.
3312 The command \\[set-goal-column] can be used to create
3313 a semipermanent goal column for this command.
3314 Then instead of trying to move exactly vertically (or as close as possible),
3315 this command moves to the specified goal column (or as close as possible).
3316 The goal column is stored in the variable `goal-column', which is nil
3317 when there is no goal column.
3319 If you are thinking of using this in a Lisp program, consider
3320 using `forward-line' instead. It is usually easier to use
3321 and more reliable (no dependence on goal column, etc.)."
3322 (interactive "p\np")
3323 (or arg (setq arg 1))
3324 (if (and next-line-add-newlines (= arg 1))
3325 (if (save-excursion (end-of-line) (eobp))
3326 ;; When adding a newline, don't expand an abbrev.
3327 (let ((abbrev-mode nil))
3329 (insert hard-newline))
3330 (line-move arg nil nil try-vscroll))
3333 (line-move arg nil nil try-vscroll)
3334 ((beginning-of-buffer end-of-buffer) (ding)))
3335 (line-move arg nil nil try-vscroll)))
3338 (defun previous-line (&optional arg try-vscroll)
3339 "Move cursor vertically up ARG lines.
3340 Interactively, vscroll tall lines if `auto-window-vscroll' is enabled.
3341 If there is no character in the target line exactly over the current column,
3342 the cursor is positioned after the character in that line which spans this
3343 column, or at the end of the line if it is not long enough.
3345 The command \\[set-goal-column] can be used to create
3346 a semipermanent goal column for this command.
3347 Then instead of trying to move exactly vertically (or as close as possible),
3348 this command moves to the specified goal column (or as close as possible).
3349 The goal column is stored in the variable `goal-column', which is nil
3350 when there is no goal column.
3352 If you are thinking of using this in a Lisp program, consider using
3353 `forward-line' with a negative argument instead. It is usually easier
3354 to use and more reliable (no dependence on goal column, etc.)."
3355 (interactive "p\np")
3356 (or arg (setq arg 1))
3359 (line-move (- arg) nil nil try-vscroll)
3360 ((beginning-of-buffer end-of-buffer) (ding)))
3361 (line-move (- arg) nil nil try-vscroll))
3364 (defcustom track-eol nil
3365 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3366 This means moving to the end of each line moved onto.
3367 The beginning of a blank line does not count as the end of a line."
3369 :group 'editing-basics)
3371 (defcustom goal-column nil
3372 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3373 :type '(choice integer
3374 (const :tag "None" nil))
3375 :group 'editing-basics)
3376 (make-variable-buffer-local 'goal-column)
3378 (defvar temporary-goal-column 0
3379 "Current goal column for vertical motion.
3380 It is the column where point was
3381 at the start of current run of vertical motion commands.
3382 When the `track-eol' feature is doing its job, the value is 9999.")
3384 (defcustom line-move-ignore-invisible t
3385 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3386 Outline mode sets this."
3388 :group 'editing-basics)
3390 (defun line-move-invisible-p (pos)
3391 "Return non-nil if the character after POS is currently invisible."
3393 (get-char-property pos 'invisible)))
3394 (if (eq buffer-invisibility-spec t)
3396 (or (memq prop buffer-invisibility-spec)
3397 (assq prop buffer-invisibility-spec)))))
3399 ;; This is like line-move-1 except that it also performs
3400 ;; vertical scrolling of tall images if appropriate.
3401 ;; That is not really a clean thing to do, since it mixes
3402 ;; scrolling with cursor motion. But so far we don't have
3403 ;; a cleaner solution to the problem of making C-n do something
3404 ;; useful given a tall image.
3405 (defun line-move (arg &optional noerror to-end try-vscroll)
3406 (if (and auto-window-vscroll try-vscroll
3407 ;; But don't vscroll in a keyboard macro.
3408 (not defining-kbd-macro)
3409 (not executing-kbd-macro))
3410 (let ((forward (> arg 0))
3411 (part (nth 2 (pos-visible-in-window-p (point) nil t))))
3412 (if (and (consp part)
3413 (> (if forward (cdr part) (car part)) 0))
3414 (set-window-vscroll nil
3416 (+ (window-vscroll nil t)
3418 (* (frame-char-height) arg)))
3420 (- (window-vscroll nil t)
3422 (* (frame-char-height) (- arg))))))
3424 (set-window-vscroll nil 0)
3425 (when (line-move-1 arg noerror to-end)
3427 ;; Update display before calling pos-visible-in-window-p,
3428 ;; because it depends on window-start being up-to-date.
3430 ;; If the current line is partly hidden at the bottom,
3431 ;; scroll it partially up so as to unhide the bottom.
3432 (if (and (setq part (nth 2 (pos-visible-in-window-p
3433 (line-beginning-position) nil t)))
3435 (set-window-vscroll nil (cdr part) t)))
3437 (line-move-1 arg noerror to-end)))
3439 ;; This is the guts of next-line and previous-line.
3440 ;; Arg says how many lines to move.
3441 ;; The value is t if we can move the specified number of lines.
3442 (defun line-move-1 (arg &optional noerror to-end)
3443 ;; Don't run any point-motion hooks, and disregard intangibility,
3444 ;; for intermediate positions.
3445 (let ((inhibit-point-motion-hooks t)
3447 (forward (> arg 0)))
3450 (if (not (memq last-command '(next-line previous-line)))
3451 (setq temporary-goal-column
3452 (if (and track-eol (eolp)
3453 ;; Don't count beg of empty line as end of line
3454 ;; unless we just did explicit end-of-line.
3455 (or (not (bolp)) (eq last-command 'end-of-line)))
3459 (if (and (not (integerp selective-display))
3460 (not line-move-ignore-invisible))
3461 ;; Use just newline characters.
3462 ;; Set ARG to 0 if we move as many lines as requested.
3464 (progn (if (> arg 1) (forward-line (1- arg)))
3465 ;; This way of moving forward ARG lines
3466 ;; verifies that we have a newline after the last one.
3467 ;; It doesn't get confused by intangible text.
3469 (if (zerop (forward-line 1))
3471 (and (zerop (forward-line arg))
3475 (signal (if (< arg 0)
3476 'beginning-of-buffer
3479 ;; Move by arg lines, but ignore invisible ones.
3481 (while (and (> arg 0) (not done))
3482 ;; If the following character is currently invisible,
3483 ;; skip all characters with that same `invisible' property value.
3484 (while (and (not (eobp)) (line-move-invisible-p (point)))
3485 (goto-char (next-char-property-change (point))))
3488 ;; If there's no invisibility here, move over the newline.
3492 (signal 'end-of-buffer nil)
3494 ((and (> arg 1) ;; Use vertical-motion for last move
3495 (not (integerp selective-display))
3496 (not (line-move-invisible-p (point))))
3497 ;; We avoid vertical-motion when possible
3498 ;; because that has to fontify.
3500 ;; Otherwise move a more sophisticated way.
3501 ((zerop (vertical-motion 1))
3503 (signal 'end-of-buffer nil)
3506 (setq arg (1- arg))))
3507 ;; The logic of this is the same as the loop above,
3508 ;; it just goes in the other direction.
3509 (while (and (< arg 0) (not done))
3514 (signal 'beginning-of-buffer nil)
3516 ((and (< arg -1) ;; Use vertical-motion for last move
3517 (not (integerp selective-display))
3518 (not (line-move-invisible-p (1- (point)))))
3520 ((zerop (vertical-motion -1))
3522 (signal 'beginning-of-buffer nil)
3526 (while (and ;; Don't move over previous invis lines
3527 ;; if our target is the middle of this line.
3528 (or (zerop (or goal-column temporary-goal-column))
3530 (not (bobp)) (line-move-invisible-p (1- (point))))
3531 (goto-char (previous-char-property-change (point))))))))
3532 ;; This is the value the function returns.
3536 ;; If we did not move down as far as desired,
3537 ;; at least go to end of line.
3540 ;; If we did not move up as far as desired,
3541 ;; at least go to beginning of line.
3542 (beginning-of-line))
3544 (line-move-finish (or goal-column temporary-goal-column)
3545 opoint forward))))))
3547 (defun line-move-finish (column opoint forward)
3550 ;; Set REPEAT to t to repeat the whole thing.
3554 (line-beg (save-excursion (beginning-of-line) (point)))
3556 ;; Compute the end of the line
3557 ;; ignoring effectively invisible newlines.
3559 ;; Like end-of-line but ignores fields.
3560 (skip-chars-forward "^\n")
3561 (while (and (not (eobp)) (line-move-invisible-p (point)))
3562 (goto-char (next-char-property-change (point)))
3563 (skip-chars-forward "^\n"))
3566 ;; Move to the desired column.
3567 (line-move-to-column column)
3570 ;; Process intangibility within a line.
3571 ;; Move to the chosen destination position from above,
3572 ;; with intangibility processing enabled.
3574 (goto-char (point-min))
3575 (let ((inhibit-point-motion-hooks nil))
3578 ;; If intangibility moves us to a different (later) place
3579 ;; in the same line, use that as the destination.
3580 (if (<= (point) line-end)
3582 ;; If that position is "too late",
3583 ;; try the previous allowable position.
3587 ;; If going forward, don't accept the previous
3588 ;; allowable position if it is before the target line.
3589 (< line-beg (point))
3590 ;; If going backward, don't accept the previous
3591 ;; allowable position if it is still after the target line.
3592 (<= (point) line-end))
3594 ;; As a last resort, use the end of the line.
3595 (setq new line-end))))
3597 ;; Now move to the updated destination, processing fields
3598 ;; as well as intangibility.
3600 (let ((inhibit-point-motion-hooks nil))
3602 (constrain-to-field new opoint nil t
3603 'inhibit-line-move-field-capture)))
3605 ;; If all this moved us to a different line,
3606 ;; retry everything within that new line.
3607 (when (or (< (point) line-beg) (> (point) line-end))
3608 ;; Repeat the intangibility and field processing.
3609 (setq repeat t))))))
3611 (defun line-move-to-column (col)
3612 "Try to find column COL, considering invisibility.
3613 This function works only in certain cases,
3614 because what we really need is for `move-to-column'
3615 and `current-column' to be able to ignore invisible text."
3618 (move-to-column col))
3620 (when (and line-move-ignore-invisible
3621 (not (bolp)) (line-move-invisible-p (1- (point))))
3622 (let ((normal-location (point))
3623 (normal-column (current-column)))
3624 ;; If the following character is currently invisible,
3625 ;; skip all characters with that same `invisible' property value.
3626 (while (and (not (eobp))
3627 (line-move-invisible-p (point)))
3628 (goto-char (next-char-property-change (point))))
3629 ;; Have we advanced to a larger column position?
3630 (if (> (current-column) normal-column)
3631 ;; We have made some progress towards the desired column.
3632 ;; See if we can make any further progress.
3633 (line-move-to-column (+ (current-column) (- col normal-column)))
3634 ;; Otherwise, go to the place we originally found
3635 ;; and move back over invisible text.
3636 ;; that will get us to the same place on the screen
3637 ;; but with a more reasonable buffer position.
3638 (goto-char normal-location)
3639 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3640 (while (and (not (bolp)) (line-move-invisible-p (1- (point))))
3641 (goto-char (previous-char-property-change (point) line-beg))))))))
3643 (defun move-end-of-line (arg)
3644 "Move point to end of current line.
3645 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3646 If point reaches the beginning or end of buffer, it stops there.
3647 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3649 This command does not move point across a field boundary unless doing so
3650 would move beyond there to a different line; if ARG is nil or 1, and
3651 point starts at a field boundary, point does not move. To ignore field
3652 boundaries bind `inhibit-field-text-motion' to t."
3654 (or arg (setq arg 1))
3659 (let ((goal-column 0))
3660 (and (line-move arg t)
3663 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
3664 (goto-char (previous-char-property-change (point))))
3668 (if (and (> (point) newpos)
3669 (eq (preceding-char) ?\n))
3671 (if (and (> (point) newpos) (not (eobp))
3672 (not (eq (following-char) ?\n)))
3673 ;; If we skipped something intangible
3674 ;; and now we're not really at eol,
3679 (defun move-beginning-of-line (arg)
3680 "Move point to beginning of current display line.
3681 With argument ARG not nil or 1, move forward ARG - 1 lines first.
3682 If point reaches the beginning or end of buffer, it stops there.
3683 To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
3685 This command does not move point across a field boundary unless doing so
3686 would move beyond there to a different line; if ARG is nil or 1, and
3687 point starts at a field boundary, point does not move. To ignore field
3688 boundaries bind `inhibit-field-text-motion' to t."
3690 (or arg (setq arg 1))
3692 (line-move (1- arg) t))
3693 (beginning-of-line 1)
3694 (let ((orig (point)))
3696 (if (/= orig (point))
3697 (goto-char (constrain-to-field (point) orig (/= arg 1) t nil)))))
3700 ;;; Many people have said they rarely use this feature, and often type
3701 ;;; it by accident. Maybe it shouldn't even be on a key.
3702 (put 'set-goal-column 'disabled t)
3704 (defun set-goal-column (arg)
3705 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3706 Those commands will move to this position in the line moved to
3707 rather than trying to keep the same horizontal position.
3708 With a non-nil argument, clears out the goal column
3709 so that \\[next-line] and \\[previous-line] resume vertical motion.
3710 The goal column is stored in the variable `goal-column'."
3714 (setq goal-column nil)
3715 (message "No goal column"))
3716 (setq goal-column (current-column))
3717 ;; The older method below can be erroneous if `set-goal-column' is bound
3718 ;; to a sequence containing %
3719 ;;(message (substitute-command-keys
3720 ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3724 (format "Goal column %d " goal-column)
3725 (substitute-command-keys
3726 "(use \\[set-goal-column] with an arg to unset it)")))
3732 (defun scroll-other-window-down (lines)
3733 "Scroll the \"other window\" down.
3734 For more details, see the documentation for `scroll-other-window'."
3736 (scroll-other-window
3737 ;; Just invert the argument's meaning.
3738 ;; We can do that without knowing which window it will be.
3739 (if (eq lines '-) nil
3741 (- (prefix-numeric-value lines))))))
3743 (defun beginning-of-buffer-other-window (arg)
3744 "Move point to the beginning of the buffer in the other window.
3745 Leave mark at previous position.
3746 With arg N, put point N/10 of the way from the true beginning."
3748 (let ((orig-window (selected-window))
3749 (window (other-window-for-scrolling)))
3750 ;; We use unwind-protect rather than save-window-excursion
3751 ;; because the latter would preserve the things we want to change.
3754 (select-window window)
3755 ;; Set point and mark in that window's buffer.
3757 (beginning-of-buffer arg))
3758 ;; Set point accordingly.
3760 (select-window orig-window))))
3762 (defun end-of-buffer-other-window (arg)
3763 "Move point to the end of the buffer in the other window.
3764 Leave mark at previous position.
3765 With arg N, put point N/10 of the way from the true end."
3767 ;; See beginning-of-buffer-other-window for comments.
3768 (let ((orig-window (selected-window))
3769 (window (other-window-for-scrolling)))
3772 (select-window window)
3774 (end-of-buffer arg))
3776 (select-window orig-window))))
3778 (defun transpose-chars (arg)
3779 "Interchange characters around point, moving forward one character.
3780 With prefix arg ARG, effect is to take character before point
3781 and drag it forward past ARG other characters (backward if ARG negative).
3782 If no argument and at end of line, the previous two chars are exchanged."
3784 (and (null arg) (eolp) (forward-char -1))
3785 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3787 (defun transpose-words (arg)
3788 "Interchange words around point, leaving point at end of them.
3789 With prefix arg ARG, effect is to take word before or around point
3790 and drag it forward past ARG other words (backward if ARG negative).
3791 If ARG is zero, the words around or after point and around or after mark
3793 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3795 (transpose-subr 'forward-word arg))
3797 (defun transpose-sexps (arg)
3798 "Like \\[transpose-words] but applies to sexps.
3799 Does not work on a sexp that point is in the middle of
3800 if it is a list or string."
3804 ;; Here we should try to simulate the behavior of
3805 ;; (cons (progn (forward-sexp x) (point))
3806 ;; (progn (forward-sexp (- x)) (point)))
3807 ;; Except that we don't want to rely on the second forward-sexp
3808 ;; putting us back to where we want to be, since forward-sexp-function
3809 ;; might do funny things like infix-precedence.
3811 (looking-at "\\sw\\|\\s_")
3813 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3814 ;; Jumping over a symbol. We might be inside it, mind you.
3815 (progn (funcall (if (> arg 0)
3816 'skip-syntax-backward 'skip-syntax-forward)
3818 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3819 ;; Otherwise, we're between sexps. Take a step back before jumping
3820 ;; to make sure we'll obey the same precedence no matter which direction
3822 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3823 (cons (save-excursion (forward-sexp arg) (point))
3824 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3825 (not (zerop (funcall (if (> arg 0)
3826 'skip-syntax-forward
3827 'skip-syntax-backward)
3832 (defun transpose-lines (arg)
3833 "Exchange current line and previous line, leaving point after both.
3834 With argument ARG, takes previous line and moves it past ARG lines.
3835 With argument 0, interchanges line point is in with line mark is in."
3837 (transpose-subr (function
3841 ;; Move forward over ARG lines,
3842 ;; but create newlines if necessary.
3843 (setq arg (forward-line arg))
3844 (if (/= (preceding-char) ?\n)
3845 (setq arg (1+ arg)))
3848 (forward-line arg))))
3851 (defun transpose-subr (mover arg &optional special)
3852 (let ((aux (if special mover
3854 (cons (progn (funcall mover x) (point))
3855 (progn (funcall mover (- x)) (point))))))
3860 (setq pos1 (funcall aux 1))
3862 (setq pos2 (funcall aux 1))
3863 (transpose-subr-1 pos1 pos2))
3864 (exchange-point-and-mark))
3866 (setq pos1 (funcall aux -1))
3867 (setq pos2 (funcall aux arg))
3868 (transpose-subr-1 pos1 pos2)
3869 (goto-char (car pos2)))
3871 (setq pos1 (funcall aux -1))
3872 (goto-char (car pos1))
3873 (setq pos2 (funcall aux arg))
3874 (transpose-subr-1 pos1 pos2)))))
3876 (defun transpose-subr-1 (pos1 pos2)
3877 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3878 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3879 (when (> (car pos1) (car pos2))
3881 (setq pos1 pos2 pos2 swap)))
3882 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3883 (atomic-change-group
3885 ;; FIXME: We first delete the two pieces of text, so markers that
3886 ;; used to point to after the text end up pointing to before it :-(
3887 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3888 (goto-char (car pos2))
3889 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3890 (goto-char (car pos1))
3893 (defun backward-word (&optional arg)
3894 "Move backward until encountering the beginning of a word.
3895 With argument, do this that many times."
3897 (forward-word (- (or arg 1))))
3899 (defun mark-word (&optional arg allow-extend)
3900 "Set mark ARG words away from point.
3901 The place mark goes is the same place \\[forward-word] would
3902 move to with the same argument.
3903 Interactively, if this command is repeated
3904 or (in Transient Mark mode) if the mark is active,
3905 it marks the next ARG words after the ones already marked."
3906 (interactive "P\np")
3907 (cond ((and allow-extend
3908 (or (and (eq last-command this-command) (mark t))
3909 (and transient-mark-mode mark-active)))
3910 (setq arg (if arg (prefix-numeric-value arg)
3911 (if (< (mark) (point)) -1 1)))
3920 (forward-word (prefix-numeric-value arg))
3924 (defun kill-word (arg)
3925 "Kill characters forward until encountering the end of a word.
3926 With argument, do this that many times."
3928 (kill-region (point) (progn (forward-word arg) (point))))
3930 (defun backward-kill-word (arg)
3931 "Kill characters backward until encountering the end of a word.
3932 With argument, do this that many times."
3934 (kill-word (- arg)))
3936 (defun current-word (&optional strict really-word)
3937 "Return the symbol or word that point is on (or a nearby one) as a string.
3938 The return value includes no text properties.
3939 If optional arg STRICT is non-nil, return nil unless point is within
3940 or adjacent to a symbol or word. In all cases the value can be nil
3941 if there is no word nearby.
3942 The function, belying its name, normally finds a symbol.
3943 If optional arg REALLY-WORD is non-nil, it finds just a word."
3945 (let* ((oldpoint (point)) (start (point)) (end (point))
3946 (syntaxes (if really-word "w" "w_"))
3947 (not-syntaxes (concat "^" syntaxes)))
3948 (skip-syntax-backward syntaxes) (setq start (point))
3949 (goto-char oldpoint)
3950 (skip-syntax-forward syntaxes) (setq end (point))
3951 (when (and (eq start oldpoint) (eq end oldpoint)
3952 ;; Point is neither within nor adjacent to a word.
3954 ;; Look for preceding word in same line.
3955 (skip-syntax-backward not-syntaxes
3956 (save-excursion (beginning-of-line)
3959 ;; No preceding word in same line.
3960 ;; Look for following word in same line.
3962 (skip-syntax-forward not-syntaxes
3963 (save-excursion (end-of-line)
3965 (setq start (point))
3966 (skip-syntax-forward syntaxes)
3969 (skip-syntax-backward syntaxes)
3970 (setq start (point))))
3971 ;; If we found something nonempty, return it as a string.
3972 (unless (= start end)
3973 (buffer-substring-no-properties start end)))))
3975 (defcustom fill-prefix nil
3976 "*String for filling to insert at front of new line, or nil for none."
3977 :type '(choice (const :tag "None" nil)
3980 (make-variable-buffer-local 'fill-prefix)
3982 (defcustom auto-fill-inhibit-regexp nil
3983 "*Regexp to match lines which should not be auto-filled."
3984 :type '(choice (const :tag "None" nil)
3988 (defvar comment-line-break-function 'comment-indent-new-line
3989 "*Mode-specific function which line breaks and continues a comment.
3991 This function is only called during auto-filling of a comment section.
3992 The function should take a single optional argument, which is a flag
3993 indicating whether it should use soft newlines.")
3995 ;; This function is used as the auto-fill-function of a buffer
3996 ;; when Auto-Fill mode is enabled.
3997 ;; It returns t if it really did any work.
3998 ;; (Actually some major modes use a different auto-fill function,
3999 ;; but this one is the default one.)
4000 (defun do-auto-fill ()
4001 (let (fc justify give-up
4002 (fill-prefix fill-prefix))
4003 (if (or (not (setq justify (current-justification)))
4004 (null (setq fc (current-fill-column)))
4005 (and (eq justify 'left)
4006 (<= (current-column) fc))
4007 (and auto-fill-inhibit-regexp
4008 (save-excursion (beginning-of-line)
4009 (looking-at auto-fill-inhibit-regexp))))
4010 nil ;; Auto-filling not required
4011 (if (memq justify '(full center right))
4012 (save-excursion (unjustify-current-line)))
4014 ;; Choose a fill-prefix automatically.
4015 (when (and adaptive-fill-mode
4016 (or (null fill-prefix) (string= fill-prefix "")))
4018 (fill-context-prefix
4019 (save-excursion (backward-paragraph 1) (point))
4020 (save-excursion (forward-paragraph 1) (point)))))
4021 (and prefix (not (equal prefix ""))
4022 ;; Use auto-indentation rather than a guessed empty prefix.
4023 (not (and fill-indent-according-to-mode
4024 (string-match "\\`[ \t]*\\'" prefix)))
4025 (setq fill-prefix prefix))))
4027 (while (and (not give-up) (> (current-column) fc))
4028 ;; Determine where to split the line.
4033 (setq after-prefix (point))
4035 (looking-at (regexp-quote fill-prefix))
4036 (setq after-prefix (match-end 0)))
4037 (move-to-column (1+ fc))
4038 (fill-move-to-break-point after-prefix)
4041 ;; See whether the place we found is any good.
4043 (goto-char fill-point)
4045 ;; There is no use breaking at end of line.
4046 (save-excursion (skip-chars-forward " ") (eolp))
4047 ;; It is futile to split at the end of the prefix
4048 ;; since we would just insert the prefix again.
4049 (and after-prefix (<= (point) after-prefix))
4050 ;; Don't split right after a comment starter
4051 ;; since we would just make another comment starter.
4052 (and comment-start-skip
4053 (let ((limit (point)))
4055 (and (re-search-forward comment-start-skip
4057 (eq (point) limit))))))
4058 ;; No good place to break => stop trying.
4060 ;; Ok, we have a useful place to break the line. Do it.
4061 (let ((prev-column (current-column)))
4062 ;; If point is at the fill-point, do not `save-excursion'.
4063 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
4064 ;; point will end up before it rather than after it.
4066 (skip-chars-backward " \t")
4067 (= (point) fill-point))
4068 (funcall comment-line-break-function t)
4070 (goto-char fill-point)
4071 (funcall comment-line-break-function t)))
4072 ;; Now do justification, if required
4073 (if (not (eq justify 'left))
4076 (justify-current-line justify nil t)))
4077 ;; If making the new line didn't reduce the hpos of
4078 ;; the end of the line, then give up now;
4079 ;; trying again will not help.
4080 (if (>= (current-column) prev-column)
4081 (setq give-up t))))))
4082 ;; Justify last line.
4083 (justify-current-line justify t t)
4086 (defvar normal-auto-fill-function 'do-auto-fill
4087 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
4088 Some major modes set this.")
4090 (put 'auto-fill-function :minor-mode-function 'auto-fill-mode)
4091 ;; FIXME: turn into a proper minor mode.
4092 ;; Add a global minor mode version of it.
4093 (defun auto-fill-mode (&optional arg)
4094 "Toggle Auto Fill mode.
4095 With arg, turn Auto Fill mode on if and only if arg is positive.
4096 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
4097 automatically breaks the line at a previous space.
4099 The value of `normal-auto-fill-function' specifies the function to use
4100 for `auto-fill-function' when turning Auto Fill mode on."
4102 (prog1 (setq auto-fill-function
4104 (not auto-fill-function)
4105 (> (prefix-numeric-value arg) 0))
4106 normal-auto-fill-function
4108 (force-mode-line-update)))
4110 ;; This holds a document string used to document auto-fill-mode.
4111 (defun auto-fill-function ()
4112 "Automatically break line at a previous space, in insertion of text."
4115 (defun turn-on-auto-fill ()
4116 "Unconditionally turn on Auto Fill mode."
4119 (defun turn-off-auto-fill ()
4120 "Unconditionally turn off Auto Fill mode."
4121 (auto-fill-mode -1))
4123 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
4125 (defun set-fill-column (arg)
4126 "Set `fill-column' to specified argument.
4127 Use \\[universal-argument] followed by a number to specify a column.
4128 Just \\[universal-argument] as argument means to use the current column."
4131 (setq arg (current-column)))
4132 (if (not (integerp arg))
4133 ;; Disallow missing argument; it's probably a typo for C-x C-f.
4134 (error "set-fill-column requires an explicit argument")
4135 (message "Fill column set to %d (was %d)" arg fill-column)
4136 (setq fill-column arg)))
4138 (defun set-selective-display (arg)
4139 "Set `selective-display' to ARG; clear it if no arg.
4140 When the value of `selective-display' is a number > 0,
4141 lines whose indentation is >= that value are not displayed.
4142 The variable `selective-display' has a separate value for each buffer."
4144 (if (eq selective-display t)
4145 (error "selective-display already in use for marked lines"))
4148 (narrow-to-region (point-min) (point))
4149 (goto-char (window-start))
4150 (vertical-motion (window-height)))))
4151 (setq selective-display
4152 (and arg (prefix-numeric-value arg)))
4153 (recenter current-vpos))
4154 (set-window-start (selected-window) (window-start (selected-window)))
4155 (princ "selective-display set to " t)
4156 (prin1 selective-display t)
4159 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
4160 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
4162 (defun toggle-truncate-lines (arg)
4163 "Toggle whether to fold or truncate long lines on the screen.
4164 With arg, truncate long lines iff arg is positive.
4165 Note that in side-by-side windows, truncation is always enabled."
4167 (setq truncate-lines
4169 (not truncate-lines)
4170 (> (prefix-numeric-value arg) 0)))
4171 (force-mode-line-update)
4172 (unless truncate-lines
4173 (let ((buffer (current-buffer)))
4174 (walk-windows (lambda (window)
4175 (if (eq buffer (window-buffer window))
4176 (set-window-hscroll window 0)))
4178 (message "Truncate long lines %s"
4179 (if truncate-lines "enabled" "disabled")))
4181 (defvar overwrite-mode-textual " Ovwrt"
4182 "The string displayed in the mode line when in overwrite mode.")
4183 (defvar overwrite-mode-binary " Bin Ovwrt"
4184 "The string displayed in the mode line when in binary overwrite mode.")
4186 (defun overwrite-mode (arg)
4187 "Toggle overwrite mode.
4188 With arg, turn overwrite mode on iff arg is positive.
4189 In overwrite mode, printing characters typed in replace existing text
4190 on a one-for-one basis, rather than pushing it to the right. At the
4191 end of a line, such characters extend the line. Before a tab,
4192 such characters insert until the tab is filled in.
4193 \\[quoted-insert] still inserts characters in overwrite mode; this
4194 is supposed to make it easier to insert characters when necessary."
4196 (setq overwrite-mode
4197 (if (if (null arg) (not overwrite-mode)
4198 (> (prefix-numeric-value arg) 0))
4199 'overwrite-mode-textual))
4200 (force-mode-line-update))
4202 (defun binary-overwrite-mode (arg)
4203 "Toggle binary overwrite mode.
4204 With arg, turn binary overwrite mode on iff arg is positive.
4205 In binary overwrite mode, printing characters typed in replace
4206 existing text. Newlines are not treated specially, so typing at the
4207 end of a line joins the line to the next, with the typed character
4208 between them. Typing before a tab character simply replaces the tab
4209 with the character typed.
4210 \\[quoted-insert] replaces the text at the cursor, just as ordinary
4211 typing characters do.
4213 Note that binary overwrite mode is not its own minor mode; it is a
4214 specialization of overwrite mode, entered by setting the
4215 `overwrite-mode' variable to `overwrite-mode-binary'."
4217 (setq overwrite-mode
4219 (not (eq overwrite-mode 'overwrite-mode-binary))
4220 (> (prefix-numeric-value arg) 0))
4221 'overwrite-mode-binary))
4222 (force-mode-line-update))
4224 (define-minor-mode line-number-mode
4225 "Toggle Line Number mode.
4226 With arg, turn Line Number mode on iff arg is positive.
4227 When Line Number mode is enabled, the line number appears
4230 Line numbers do not appear for very large buffers and buffers
4231 with very long lines; see variables `line-number-display-limit'
4232 and `line-number-display-limit-width'."
4233 :init-value t :global t :group 'editing-basics :require nil)
4235 (define-minor-mode column-number-mode
4236 "Toggle Column Number mode.
4237 With arg, turn Column Number mode on iff arg is positive.
4238 When Column Number mode is enabled, the column number appears
4240 :global t :group 'editing-basics :require nil)
4242 (define-minor-mode size-indication-mode
4243 "Toggle Size Indication mode.
4244 With arg, turn Size Indication mode on iff arg is positive. When
4245 Size Indication mode is enabled, the size of the accessible part
4246 of the buffer appears in the mode line."
4247 :global t :group 'editing-basics :require nil)
4249 (defgroup paren-blinking nil
4250 "Blinking matching of parens and expressions."
4251 :prefix "blink-matching-"
4252 :group 'paren-matching)
4254 (defcustom blink-matching-paren t
4255 "*Non-nil means show matching open-paren when close-paren is inserted."
4257 :group 'paren-blinking)
4259 (defcustom blink-matching-paren-on-screen t
4260 "*Non-nil means show matching open-paren when it is on screen.
4261 If nil, means don't show it (but the open-paren can still be shown
4262 when it is off screen)."
4264 :group 'paren-blinking)
4266 (defcustom blink-matching-paren-distance (* 25 1024)
4267 "*If non-nil, maximum distance to search backwards for matching open-paren.
4268 If nil, search stops at the beginning of the accessible portion of the buffer."
4269 :type '(choice (const nil) integer)
4270 :group 'paren-blinking)
4272 (defcustom blink-matching-delay 1
4273 "*Time in seconds to delay after showing a matching paren."
4275 :group 'paren-blinking)
4277 (defcustom blink-matching-paren-dont-ignore-comments nil
4278 "*Non-nil means `blink-matching-paren' will not ignore comments."
4280 :group 'paren-blinking)
4282 (defun blink-matching-open ()
4283 "Move cursor momentarily to the beginning of the sexp before point."
4285 (when (and (> (point) (point-min))
4286 blink-matching-paren
4287 ;; Verify an even number of quoting characters precede the close.
4288 (= 1 (logand 1 (- (point)
4291 (skip-syntax-backward "/\\")
4293 (let* ((oldpos (point))
4295 message-log-max ; Don't log messages about paren matching.
4297 open-paren-line-string)
4300 (if blink-matching-paren-distance
4301 (narrow-to-region (max (point-min)
4302 (- (point) blink-matching-paren-distance))
4305 (let ((parse-sexp-ignore-comments
4306 (and parse-sexp-ignore-comments
4307 (not blink-matching-paren-dont-ignore-comments))))
4308 (setq blinkpos (scan-sexps oldpos -1)))
4312 (not (eq (syntax-class (syntax-after blinkpos)) 8))
4313 (setq matching-paren
4314 (let ((syntax (syntax-after blinkpos)))
4316 (eq (syntax-class syntax) 4)
4319 ((not (or (eq matching-paren (char-before oldpos))
4320 ;; The cdr might hold a new paren-class info rather than
4321 ;; a matching-char info, in which case the two CDRs
4323 (eq matching-paren (cdr (syntax-after oldpos)))))
4324 (message "Mismatched parentheses"))
4326 (if (not blink-matching-paren-distance)
4327 (message "Unmatched parenthesis")))
4328 ((pos-visible-in-window-p blinkpos)
4329 ;; Matching open within window, temporarily move to blinkpos but only
4330 ;; if `blink-matching-paren-on-screen' is non-nil.
4331 (when blink-matching-paren-on-screen
4333 (goto-char blinkpos)
4334 (sit-for blink-matching-delay))))
4337 (goto-char blinkpos)
4338 (setq open-paren-line-string
4339 ;; Show what precedes the open in its line, if anything.
4341 (skip-chars-backward " \t")
4343 (buffer-substring (line-beginning-position)
4345 ;; Show what follows the open in its line, if anything.
4348 (skip-chars-forward " \t")
4350 (buffer-substring blinkpos
4351 (line-end-position))
4352 ;; Otherwise show the previous nonblank line,
4355 (skip-chars-backward "\n \t")
4358 (buffer-substring (progn
4359 (skip-chars-backward "\n \t")
4360 (line-beginning-position))
4361 (progn (end-of-line)
4362 (skip-chars-backward " \t")
4364 ;; Replace the newline and other whitespace with `...'.
4366 (buffer-substring blinkpos (1+ blinkpos)))
4367 ;; There is nothing to show except the char itself.
4368 (buffer-substring blinkpos (1+ blinkpos)))))))
4369 (message "Matches %s"
4370 (substring-no-properties open-paren-line-string))))))))
4372 ;Turned off because it makes dbx bomb out.
4373 (setq blink-paren-function 'blink-matching-open)
4375 ;; This executes C-g typed while Emacs is waiting for a command.
4376 ;; Quitting out of a program does not go through here;
4377 ;; that happens in the QUIT macro at the C code level.
4378 (defun keyboard-quit ()
4379 "Signal a `quit' condition.
4380 During execution of Lisp code, this character causes a quit directly.
4381 At top-level, as an editor command, this simply beeps."
4384 (if (fboundp 'kmacro-keyboard-quit)
4385 (kmacro-keyboard-quit))
4386 (setq defining-kbd-macro nil)
4389 (defvar buffer-quit-function nil
4390 "Function to call to \"quit\" the current buffer, or nil if none.
4391 \\[keyboard-escape-quit] calls this function when its more local actions
4392 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
4394 (defun keyboard-escape-quit ()
4395 "Exit the current \"mode\" (in a generalized sense of the word).
4396 This command can exit an interactive command such as `query-replace',
4397 can clear out a prefix argument or a region,
4398 can get out of the minibuffer or other recursive edit,
4399 cancel the use of the current buffer (for special-purpose buffers),
4400 or go back to just one window (by deleting all but the selected window)."
4402 (cond ((eq last-command 'mode-exited) nil)
4403 ((> (minibuffer-depth) 0)
4404 (abort-recursive-edit))
4407 ((and transient-mark-mode mark-active)
4409 ((> (recursion-depth) 0)
4410 (exit-recursive-edit))
4411 (buffer-quit-function
4412 (funcall buffer-quit-function))
4413 ((not (one-window-p t))
4414 (delete-other-windows))
4415 ((string-match "^ \\*" (buffer-name (current-buffer)))
4418 (defun play-sound-file (file &optional volume device)
4419 "Play sound stored in FILE.
4420 VOLUME and DEVICE correspond to the keywords of the sound
4421 specification for `play-sound'."
4422 (interactive "fPlay sound file: ")
4423 (let ((sound (list :file file)))
4425 (plist-put sound :volume volume))
4427 (plist-put sound :device device))
4429 (play-sound sound)))
4432 (defcustom read-mail-command 'rmail
4433 "*Your preference for a mail reading package.
4434 This is used by some keybindings which support reading mail.
4435 See also `mail-user-agent' concerning sending mail."
4436 :type '(choice (function-item rmail)
4437 (function-item gnus)
4438 (function-item mh-rmail)
4439 (function :tag "Other"))
4443 (defcustom mail-user-agent 'sendmail-user-agent
4444 "*Your preference for a mail composition package.
4445 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
4446 outgoing email message. This variable lets you specify which
4447 mail-sending package you prefer.
4449 Valid values include:
4451 `sendmail-user-agent' -- use the default Emacs Mail package.
4452 See Info node `(emacs)Sending Mail'.
4453 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
4454 See Info node `(mh-e)'.
4455 `message-user-agent' -- use the Gnus Message package.
4456 See Info node `(message)'.
4457 `gnus-user-agent' -- like `message-user-agent', but with Gnus
4458 paraphernalia, particularly the Gcc: header for
4461 Additional valid symbols may be available; check with the author of
4462 your package for details. The function should return non-nil if it
4465 See also `read-mail-command' concerning reading mail."
4466 :type '(radio (function-item :tag "Default Emacs mail"
4468 sendmail-user-agent)
4469 (function-item :tag "Emacs interface to MH"
4472 (function-item :tag "Gnus Message package"
4475 (function-item :tag "Gnus Message with full Gnus features"
4478 (function :tag "Other"))
4481 (define-mail-user-agent 'sendmail-user-agent
4482 'sendmail-user-agent-compose
4483 'mail-send-and-exit)
4485 (defun rfc822-goto-eoh ()
4486 ;; Go to header delimiter line in a mail message, following RFC822 rules
4487 (goto-char (point-min))
4488 (when (re-search-forward
4489 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4490 (goto-char (match-beginning 0))))
4492 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4493 switch-function yank-action
4496 (let ((special-display-buffer-names nil)
4497 (special-display-regexps nil)
4498 (same-window-buffer-names nil)
4499 (same-window-regexps nil))
4500 (funcall switch-function "*mail*")))
4501 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4502 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4503 (body (cdr (assoc-string "body" other-headers t))))
4504 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4506 (error "Message aborted"))
4509 (while other-headers
4510 (unless (member-ignore-case (car (car other-headers))
4511 '("in-reply-to" "cc" "body"))
4512 (insert (car (car other-headers)) ": "
4513 (cdr (car other-headers)) hard-newline))
4514 (setq other-headers (cdr other-headers)))
4520 (define-mail-user-agent 'mh-e-user-agent
4521 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
4522 'mh-before-send-letter-hook)
4524 (defun compose-mail (&optional to subject other-headers continue
4525 switch-function yank-action send-actions)
4526 "Start composing a mail message to send.
4527 This uses the user's chosen mail composition package
4528 as selected with the variable `mail-user-agent'.
4529 The optional arguments TO and SUBJECT specify recipients
4530 and the initial Subject field, respectively.
4532 OTHER-HEADERS is an alist specifying additional
4533 header fields. Elements look like (HEADER . VALUE) where both
4534 HEADER and VALUE are strings.
4536 CONTINUE, if non-nil, says to continue editing a message already
4539 SWITCH-FUNCTION, if non-nil, is a function to use to
4540 switch to and display the buffer used for mail composition.
4542 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4543 to insert the raw text of the message being replied to.
4544 It has the form (FUNCTION . ARGS). The user agent will apply
4545 FUNCTION to ARGS, to insert the raw text of the original message.
4546 \(The user agent will also run `mail-citation-hook', *after* the
4547 original text has been inserted in this way.)
4549 SEND-ACTIONS is a list of actions to call when the message is sent.
4550 Each action has the form (FUNCTION . ARGS)."
4552 (list nil nil nil current-prefix-arg))
4553 (let ((function (get mail-user-agent 'composefunc)))
4554 (funcall function to subject other-headers continue
4555 switch-function yank-action send-actions)))
4557 (defun compose-mail-other-window (&optional to subject other-headers continue
4558 yank-action send-actions)
4559 "Like \\[compose-mail], but edit the outgoing message in another window."
4561 (list nil nil nil current-prefix-arg))
4562 (compose-mail to subject other-headers continue
4563 'switch-to-buffer-other-window yank-action send-actions))
4566 (defun compose-mail-other-frame (&optional to subject other-headers continue
4567 yank-action send-actions)
4568 "Like \\[compose-mail], but edit the outgoing message in another frame."
4570 (list nil nil nil current-prefix-arg))
4571 (compose-mail to subject other-headers continue
4572 'switch-to-buffer-other-frame yank-action send-actions))
4574 (defvar set-variable-value-history nil
4575 "History of values entered with `set-variable'.")
4577 (defun set-variable (variable value &optional make-local)
4578 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4579 VARIABLE should be a user option variable name, a Lisp variable
4580 meant to be customized by users. You should enter VALUE in Lisp syntax,
4581 so if you want VALUE to be a string, you must surround it with doublequotes.
4582 VALUE is used literally, not evaluated.
4584 If VARIABLE has a `variable-interactive' property, that is used as if
4585 it were the arg to `interactive' (which see) to interactively read VALUE.
4587 If VARIABLE has been defined with `defcustom', then the type information
4588 in the definition is used to check that VALUE is valid.
4590 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4592 (let* ((default-var (variable-at-point))
4593 (var (if (user-variable-p default-var)
4594 (read-variable (format "Set variable (default %s): " default-var)
4596 (read-variable "Set variable: ")))
4597 (minibuffer-help-form '(describe-variable var))
4598 (prop (get var 'variable-interactive))
4599 (obsolete (car (get var 'byte-obsolete-variable)))
4600 (prompt (format "Set %s %s to value: " var
4601 (cond ((local-variable-p var)
4603 ((or current-prefix-arg
4604 (local-variable-if-set-p var))
4609 (message (concat "`%S' is obsolete; "
4610 (if (symbolp obsolete) "use `%S' instead" "%s"))
4614 ;; Use VAR's `variable-interactive' property
4615 ;; as an interactive spec for prompting.
4616 (call-interactively `(lambda (arg)
4620 (read-string prompt nil
4621 'set-variable-value-history
4622 (format "%S" (symbol-value var))))))))
4623 (list var val current-prefix-arg)))
4625 (and (custom-variable-p variable)
4626 (not (get variable 'custom-type))
4627 (custom-load-symbol variable))
4628 (let ((type (get variable 'custom-type)))
4630 ;; Match with custom type.
4632 (setq type (widget-convert type))
4633 (unless (widget-apply type :match value)
4634 (error "Value `%S' does not match type %S of %S"
4635 value (car type) variable))))
4638 (make-local-variable variable))
4640 (set variable value)
4642 ;; Force a thorough redisplay for the case that the variable
4643 ;; has an effect on the display, like `tab-width' has.
4644 (force-mode-line-update))
4646 ;; Define the major mode for lists of completions.
4648 (defvar completion-list-mode-map nil
4649 "Local map for completion list buffers.")
4650 (or completion-list-mode-map
4651 (let ((map (make-sparse-keymap)))
4652 (define-key map [mouse-2] 'mouse-choose-completion)
4653 (define-key map [follow-link] 'mouse-face)
4654 (define-key map [down-mouse-2] nil)
4655 (define-key map "\C-m" 'choose-completion)
4656 (define-key map "\e\e\e" 'delete-completion-window)
4657 (define-key map [left] 'previous-completion)
4658 (define-key map [right] 'next-completion)
4659 (setq completion-list-mode-map map)))
4661 ;; Completion mode is suitable only for specially formatted data.
4662 (put 'completion-list-mode 'mode-class 'special)
4664 (defvar completion-reference-buffer nil
4665 "Record the buffer that was current when the completion list was requested.
4666 This is a local variable in the completion list buffer.
4667 Initial value is nil to avoid some compiler warnings.")
4669 (defvar completion-no-auto-exit nil
4670 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4671 This also applies to other functions such as `choose-completion'
4672 and `mouse-choose-completion'.")
4674 (defvar completion-base-size nil
4675 "Number of chars at beginning of minibuffer not involved in completion.
4676 This is a local variable in the completion list buffer
4677 but it talks about the buffer in `completion-reference-buffer'.
4678 If this is nil, it means to compare text to determine which part
4679 of the tail end of the buffer's text is involved in completion.")
4681 (defun delete-completion-window ()
4682 "Delete the completion list window.
4683 Go to the window from which completion was requested."
4685 (let ((buf completion-reference-buffer))
4686 (if (one-window-p t)
4687 (if (window-dedicated-p (selected-window))
4688 (delete-frame (selected-frame)))
4689 (delete-window (selected-window))
4690 (if (get-buffer-window buf)
4691 (select-window (get-buffer-window buf))))))
4693 (defun previous-completion (n)
4694 "Move to the previous item in the completion list."
4696 (next-completion (- n)))
4698 (defun next-completion (n)
4699 "Move to the next item in the completion list.
4700 With prefix argument N, move N items (negative N means move backward)."
4702 (let ((beg (point-min)) (end (point-max)))
4703 (while (and (> n 0) (not (eobp)))
4704 ;; If in a completion, move to the end of it.
4705 (when (get-text-property (point) 'mouse-face)
4706 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4707 ;; Move to start of next one.
4708 (unless (get-text-property (point) 'mouse-face)
4709 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4711 (while (and (< n 0) (not (bobp)))
4712 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4713 ;; If in a completion, move to the start of it.
4714 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4715 (goto-char (previous-single-property-change
4716 (point) 'mouse-face nil beg)))
4717 ;; Move to end of the previous completion.
4718 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4719 (goto-char (previous-single-property-change
4720 (point) 'mouse-face nil beg)))
4721 ;; Move to the start of that one.
4722 (goto-char (previous-single-property-change
4723 (point) 'mouse-face nil beg))
4726 (defun choose-completion ()
4727 "Choose the completion that point is in or next to."
4729 (let (beg end completion (buffer completion-reference-buffer)
4730 (base-size completion-base-size))
4731 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4732 (setq end (point) beg (1+ (point))))
4733 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4734 (setq end (1- (point)) beg (point)))
4736 (error "No completion here"))
4737 (setq beg (previous-single-property-change beg 'mouse-face))
4738 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4739 (setq completion (buffer-substring beg end))
4740 (let ((owindow (selected-window)))
4741 (if (and (one-window-p t 'selected-frame)
4742 (window-dedicated-p (selected-window)))
4743 ;; This is a special buffer's frame
4744 (iconify-frame (selected-frame))
4745 (or (window-dedicated-p (selected-window))
4747 (select-window owindow))
4748 (choose-completion-string completion buffer base-size)))
4750 ;; Delete the longest partial match for STRING
4751 ;; that can be found before POINT.
4752 (defun choose-completion-delete-max-match (string)
4753 (let ((opoint (point))
4755 ;; Try moving back by the length of the string.
4756 (goto-char (max (- (point) (length string))
4757 (minibuffer-prompt-end)))
4758 ;; See how far back we were actually able to move. That is the
4759 ;; upper bound on how much we can match and delete.
4760 (setq len (- opoint (point)))
4761 (if completion-ignore-case
4762 (setq string (downcase string)))
4763 (while (and (> len 0)
4764 (let ((tail (buffer-substring (point) opoint)))
4765 (if completion-ignore-case
4766 (setq tail (downcase tail)))
4767 (not (string= tail (substring string 0 len)))))
4772 (defvar choose-completion-string-functions nil
4773 "Functions that may override the normal insertion of a completion choice.
4774 These functions are called in order with four arguments:
4775 CHOICE - the string to insert in the buffer,
4776 BUFFER - the buffer in which the choice should be inserted,
4777 MINI-P - non-nil iff BUFFER is a minibuffer, and
4778 BASE-SIZE - the number of characters in BUFFER before
4779 the string being completed.
4781 If a function in the list returns non-nil, that function is supposed
4782 to have inserted the CHOICE in the BUFFER, and possibly exited
4783 the minibuffer; no further functions will be called.
4785 If all functions in the list return nil, that means to use
4786 the default method of inserting the completion in BUFFER.")
4788 (defun choose-completion-string (choice &optional buffer base-size)
4789 "Switch to BUFFER and insert the completion choice CHOICE.
4790 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4791 to keep. If it is nil, we call `choose-completion-delete-max-match'
4792 to decide what to delete."
4794 ;; If BUFFER is the minibuffer, exit the minibuffer
4795 ;; unless it is reading a file name and CHOICE is a directory,
4796 ;; or completion-no-auto-exit is non-nil.
4798 (let* ((buffer (or buffer completion-reference-buffer))
4799 (mini-p (minibufferp buffer)))
4800 ;; If BUFFER is a minibuffer, barf unless it's the currently
4801 ;; active minibuffer.
4803 (or (not (active-minibuffer-window))
4805 (window-buffer (active-minibuffer-window))))))
4806 (error "Minibuffer is not active for completion")
4807 ;; Set buffer so buffer-local choose-completion-string-functions works.
4809 (unless (run-hook-with-args-until-success
4810 'choose-completion-string-functions
4811 choice buffer mini-p base-size)
4812 ;; Insert the completion into the buffer where it was requested.
4814 (delete-region (+ base-size (if mini-p
4815 (minibuffer-prompt-end)
4818 (choose-completion-delete-max-match choice))
4820 (remove-text-properties (- (point) (length choice)) (point)
4822 ;; Update point in the window that BUFFER is showing in.
4823 (let ((window (get-buffer-window buffer t)))
4824 (set-window-point window (point)))
4825 ;; If completing for the minibuffer, exit it with this choice.
4826 (and (not completion-no-auto-exit)
4827 (equal buffer (window-buffer (minibuffer-window)))
4828 minibuffer-completion-table
4829 ;; If this is reading a file name, and the file name chosen
4830 ;; is a directory, don't exit the minibuffer.
4831 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4832 (file-directory-p (field-string (point-max))))
4833 (let ((mini (active-minibuffer-window)))
4834 (select-window mini)
4835 (when minibuffer-auto-raise
4836 (raise-frame (window-frame mini))))
4837 (exit-minibuffer)))))))
4839 (defun completion-list-mode ()
4840 "Major mode for buffers showing lists of possible completions.
4841 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4842 to select the completion near point.
4843 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4846 (kill-all-local-variables)
4847 (use-local-map completion-list-mode-map)
4848 (setq mode-name "Completion List")
4849 (setq major-mode 'completion-list-mode)
4850 (make-local-variable 'completion-base-size)
4851 (setq completion-base-size nil)
4852 (run-mode-hooks 'completion-list-mode-hook))
4854 (defun completion-list-mode-finish ()
4855 "Finish setup of the completions buffer.
4856 Called from `temp-buffer-show-hook'."
4857 (when (eq major-mode 'completion-list-mode)
4858 (toggle-read-only 1)))
4860 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4862 (defvar completion-setup-hook nil
4863 "Normal hook run at the end of setting up a completion list buffer.
4864 When this hook is run, the current buffer is the one in which the
4865 command to display the completion list buffer was run.
4866 The completion list buffer is available as the value of `standard-output'.
4867 The common prefix substring for completion may be available as the
4868 value of `completion-common-substring'. See also `display-completion-list'.")
4871 ;; Variables and faces used in `completion-setup-function'.
4873 (defface completions-first-difference
4874 '((t (:inherit bold)))
4875 "Face put on the first uncommon character in completions in *Completions* buffer."
4878 (defface completions-common-part
4879 '((t (:inherit default)))
4880 "Face put on the common prefix substring in completions in *Completions* buffer.
4881 The idea of `completions-common-part' is that you can use it to
4882 make the common parts less visible than normal, so that the rest
4883 of the differing parts is, by contrast, slightly highlighted."
4886 ;; This is for packages that need to bind it to a non-default regexp
4887 ;; in order to make the first-differing character highlight work
4889 (defvar completion-root-regexp "^/"
4890 "Regexp to use in `completion-setup-function' to find the root directory.")
4892 (defvar completion-common-substring nil
4893 "Common prefix substring to use in `completion-setup-function' to put faces.
4894 The value is set by `display-completion-list' during running `completion-setup-hook'.
4896 To put faces, `completions-first-difference' and `completions-common-part'
4897 into \"*Completions*\* buffer, the common prefix substring in completions is
4898 needed as a hint. (Minibuffer is a special case. The content of minibuffer itself
4899 is the substring.)")
4901 ;; This function goes in completion-setup-hook, so that it is called
4902 ;; after the text of the completion list buffer is written.
4903 (defun completion-setup-function ()
4904 (let* ((mainbuf (current-buffer))
4905 (mbuf-contents (minibuffer-contents))
4906 (common-string-length (length mbuf-contents)))
4907 ;; When reading a file name in the minibuffer,
4908 ;; set default-directory in the minibuffer
4909 ;; so it will get copied into the completion list buffer.
4910 (if minibuffer-completing-file-name
4911 (with-current-buffer mainbuf
4912 (setq default-directory (file-name-directory mbuf-contents))))
4913 ;; If partial-completion-mode is on, point might not be after the
4914 ;; last character in the minibuffer.
4915 ;; FIXME: This hack should be moved to complete.el where we call
4916 ;; display-completion-list.
4917 (when partial-completion-mode
4918 (setq common-string-length
4919 (if (eq (char-after (field-beginning)) ?-)
4920 ;; If the text to be completed starts with a `-', there is no
4922 ;; FIXME: this probably still doesn't do the right thing
4923 ;; when completing file names. It's not even clear what
4926 (- common-string-length (- (point-max) (point))))))
4927 (with-current-buffer standard-output
4928 (completion-list-mode)
4929 (set (make-local-variable 'completion-reference-buffer) mainbuf)
4930 (setq completion-base-size
4931 (if minibuffer-completing-file-name
4932 ;; For file name completion, use the number of chars before
4933 ;; the start of the last file name component.
4934 (with-current-buffer mainbuf
4936 (goto-char (point-max))
4937 (skip-chars-backward completion-root-regexp)
4938 (- (point) (minibuffer-prompt-end))))
4939 ;; Otherwise, in minibuffer, the whole input is being completed.
4940 (if (minibufferp mainbuf) 0)))
4941 (if (and (symbolp minibuffer-completion-table)
4942 (get minibuffer-completion-table 'completion-base-size-function))
4943 (setq completion-base-size
4944 ;; FIXME: without any extra arg, how is this function
4945 ;; expected to return anything else than a constant unless
4946 ;; it redoes part of the work of all-completions?
4947 ;; In most cases this value would better be computed and
4948 ;; returned at the same time as the list of all-completions
4949 ;; is computed. --Stef
4950 (funcall (get minibuffer-completion-table
4951 'completion-base-size-function))))
4952 ;; Put faces on first uncommon characters and common parts.
4953 (when (or completion-common-substring completion-base-size)
4954 (setq common-string-length
4955 (if completion-common-substring
4956 (length completion-common-substring)
4957 (- common-string-length completion-base-size)))
4958 (let ((element-start (point-min))
4961 (while (and (setq element-start
4962 (next-single-property-change
4963 element-start 'mouse-face))
4964 (< (setq element-common-end
4965 (+ element-start common-string-length))
4967 (when (and (get-char-property element-start 'mouse-face)
4968 (get-char-property element-common-end 'mouse-face))
4969 (put-text-property element-start element-common-end
4970 'font-lock-face 'completions-common-part)
4971 (put-text-property element-common-end (1+ element-common-end)
4972 'font-lock-face 'completions-first-difference)))))
4973 ;; Insert help string.
4974 (goto-char (point-min))
4975 (if (display-mouse-p)
4976 (insert (substitute-command-keys
4977 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4978 (insert (substitute-command-keys
4979 "In this buffer, type \\[choose-completion] to \
4980 select the completion near point.\n\n")))))
4982 (add-hook 'completion-setup-hook 'completion-setup-function)
4984 (define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
4985 (define-key minibuffer-local-completion-map "\M-v" 'switch-to-completions)
4987 (defun switch-to-completions ()
4988 "Select the completion list window."
4990 ;; Make sure we have a completions window.
4991 (or (get-buffer-window "*Completions*")
4992 (minibuffer-completion-help))
4993 (let ((window (get-buffer-window "*Completions*")))
4995 (select-window window)
4996 (goto-char (point-min))
4997 (search-forward "\n\n")
5000 ;; Support keyboard commands to turn on various modifiers.
5002 ;; These functions -- which are not commands -- each add one modifier
5003 ;; to the following event.
5005 (defun event-apply-alt-modifier (ignore-prompt)
5006 "\\<function-key-map>Add the Alt modifier to the following event.
5007 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
5008 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
5009 (defun event-apply-super-modifier (ignore-prompt)
5010 "\\<function-key-map>Add the Super modifier to the following event.
5011 For example, type \\[event-apply-super-modifier] & to enter Super-&."
5012 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
5013 (defun event-apply-hyper-modifier (ignore-prompt)
5014 "\\<function-key-map>Add the Hyper modifier to the following event.
5015 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
5016 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
5017 (defun event-apply-shift-modifier (ignore-prompt)
5018 "\\<function-key-map>Add the Shift modifier to the following event.
5019 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
5020 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
5021 (defun event-apply-control-modifier (ignore-prompt)
5022 "\\<function-key-map>Add the Ctrl modifier to the following event.
5023 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
5024 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
5025 (defun event-apply-meta-modifier (ignore-prompt)
5026 "\\<function-key-map>Add the Meta modifier to the following event.
5027 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
5028 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
5030 (defun event-apply-modifier (event symbol lshiftby prefix)
5031 "Apply a modifier flag to event EVENT.
5032 SYMBOL is the name of this modifier, as a symbol.
5033 LSHIFTBY is the numeric value of this modifier, in keyboard events.
5034 PREFIX is the string that represents this modifier in an event type symbol."
5036 (cond ((eq symbol 'control)
5037 (if (and (<= (downcase event) ?z)
5038 (>= (downcase event) ?a))
5039 (- (downcase event) ?a -1)
5040 (if (and (<= (downcase event) ?Z)
5041 (>= (downcase event) ?A))
5042 (- (downcase event) ?A -1)
5043 (logior (lsh 1 lshiftby) event))))
5045 (if (and (<= (downcase event) ?z)
5046 (>= (downcase event) ?a))
5048 (logior (lsh 1 lshiftby) event)))
5050 (logior (lsh 1 lshiftby) event)))
5051 (if (memq symbol (event-modifiers event))
5053 (let ((event-type (if (symbolp event) event (car event))))
5054 (setq event-type (intern (concat prefix (symbol-name event-type))))
5057 (cons event-type (cdr event)))))))
5059 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
5060 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
5061 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
5062 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
5063 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
5064 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
5066 ;;;; Keypad support.
5068 ;;; Make the keypad keys act like ordinary typing keys. If people add
5069 ;;; bindings for the function key symbols, then those bindings will
5070 ;;; override these, so this shouldn't interfere with any existing
5073 ;; Also tell read-char how to handle these keys.
5075 (lambda (keypad-normal)
5076 (let ((keypad (nth 0 keypad-normal))
5077 (normal (nth 1 keypad-normal)))
5078 (put keypad 'ascii-character normal)
5079 (define-key function-key-map (vector keypad) (vector normal))))
5080 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
5081 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
5094 ;;;; forking a twin copy of a buffer.
5097 (defvar clone-buffer-hook nil
5098 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
5100 (defun clone-process (process &optional newname)
5101 "Create a twin copy of PROCESS.
5102 If NEWNAME is nil, it defaults to PROCESS' name;
5103 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
5104 If PROCESS is associated with a buffer, the new process will be associated
5105 with the current buffer instead.
5106 Returns nil if PROCESS has already terminated."
5107 (setq newname (or newname (process-name process)))
5108 (if (string-match "<[0-9]+>\\'" newname)
5109 (setq newname (substring newname 0 (match-beginning 0))))
5110 (when (memq (process-status process) '(run stop open))
5111 (let* ((process-connection-type (process-tty-name process))
5113 (if (memq (process-status process) '(open))
5114 (let ((args (process-contact process t)))
5115 (setq args (plist-put args :name newname))
5116 (setq args (plist-put args :buffer
5117 (if (process-buffer process)
5119 (apply 'make-network-process args))
5120 (apply 'start-process newname
5121 (if (process-buffer process) (current-buffer))
5122 (process-command process)))))
5123 (set-process-query-on-exit-flag
5124 new-process (process-query-on-exit-flag process))
5125 (set-process-inherit-coding-system-flag
5126 new-process (process-inherit-coding-system-flag process))
5127 (set-process-filter new-process (process-filter process))
5128 (set-process-sentinel new-process (process-sentinel process))
5129 (set-process-plist new-process (copy-sequence (process-plist process)))
5132 ;; things to maybe add (currently partly covered by `funcall mode'):
5135 (defun clone-buffer (&optional newname display-flag)
5136 "Create and return a twin copy of the current buffer.
5137 Unlike an indirect buffer, the new buffer can be edited
5138 independently of the old one (if it is not read-only).
5139 NEWNAME is the name of the new buffer. It may be modified by
5140 adding or incrementing <N> at the end as necessary to create a
5141 unique buffer name. If nil, it defaults to the name of the
5142 current buffer, with the proper suffix. If DISPLAY-FLAG is
5143 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
5144 clone a file-visiting buffer, or a buffer whose major mode symbol
5145 has a non-nil `no-clone' property, results in an error.
5147 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
5148 current buffer with appropriate suffix. However, if a prefix
5149 argument is given, then the command prompts for NEWNAME in the
5152 This runs the normal hook `clone-buffer-hook' in the new buffer
5153 after it has been set up properly in other respects."
5156 (if buffer-file-name
5157 (error "Cannot clone a file-visiting buffer"))
5158 (if (get major-mode 'no-clone)
5159 (error "Cannot clone a buffer in %s mode" mode-name))
5160 (list (if current-prefix-arg (read-string "Name: "))
5162 (if buffer-file-name
5163 (error "Cannot clone a file-visiting buffer"))
5164 (if (get major-mode 'no-clone)
5165 (error "Cannot clone a buffer in %s mode" mode-name))
5166 (setq newname (or newname (buffer-name)))
5167 (if (string-match "<[0-9]+>\\'" newname)
5168 (setq newname (substring newname 0 (match-beginning 0))))
5169 (let ((buf (current-buffer))
5173 (mk (if mark-active (mark t)))
5174 (modified (buffer-modified-p))
5176 (lvars (buffer-local-variables))
5177 (process (get-buffer-process (current-buffer)))
5178 (new (generate-new-buffer (or newname (buffer-name)))))
5181 (with-current-buffer new
5182 (insert-buffer-substring buf)))
5183 (with-current-buffer new
5184 (narrow-to-region ptmin ptmax)
5186 (if mk (set-mark mk))
5187 (set-buffer-modified-p modified)
5189 ;; Clone the old buffer's process, if any.
5190 (when process (clone-process process))
5192 ;; Now set up the major mode.
5195 ;; Set up other local variables.
5197 (condition-case () ;in case var is read-only
5200 (set (make-local-variable (car v)) (cdr v)))
5204 ;; Run any hooks (typically set up by the major mode
5205 ;; for cloning to work properly).
5206 (run-hooks 'clone-buffer-hook))
5207 (if display-flag (pop-to-buffer new))
5211 (defun clone-indirect-buffer (newname display-flag &optional norecord)
5212 "Create an indirect buffer that is a twin copy of the current buffer.
5214 Give the indirect buffer name NEWNAME. Interactively, read NEWNAME
5215 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
5216 or if not called with a prefix arg, NEWNAME defaults to the current
5217 buffer's name. The name is modified by adding a `<N>' suffix to it
5218 or by incrementing the N in an existing suffix.
5220 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
5221 This is always done when called interactively.
5223 Optional third arg NORECORD non-nil means do not put this buffer at the
5224 front of the list of recently selected ones."
5227 (if (get major-mode 'no-clone-indirect)
5228 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5229 (list (if current-prefix-arg
5230 (read-string "BName of indirect buffer: "))
5232 (if (get major-mode 'no-clone-indirect)
5233 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
5234 (setq newname (or newname (buffer-name)))
5235 (if (string-match "<[0-9]+>\\'" newname)
5236 (setq newname (substring newname 0 (match-beginning 0))))
5237 (let* ((name (generate-new-buffer-name newname))
5238 (buffer (make-indirect-buffer (current-buffer) name t)))
5240 (pop-to-buffer buffer norecord))
5244 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
5245 "Create an indirect buffer that is a twin copy of BUFFER.
5246 Select the new buffer in another window.
5247 Optional second arg NORECORD non-nil means do not put this buffer at
5248 the front of the list of recently selected ones."
5249 (interactive "bClone buffer in other window: ")
5250 (let ((pop-up-windows t))
5252 (clone-indirect-buffer nil t norecord)))
5255 ;;; Handling of Backspace and Delete keys.
5257 (defcustom normal-erase-is-backspace
5258 (and (not noninteractive)
5259 (or (memq system-type '(ms-dos windows-nt))
5260 (eq window-system 'mac)
5261 (and (memq window-system '(x))
5262 (fboundp 'x-backspace-delete-keys-p)
5263 (x-backspace-delete-keys-p))
5264 ;; If the terminal Emacs is running on has erase char
5265 ;; set to ^H, use the Backspace key for deleting
5266 ;; backward and, and the Delete key for deleting forward.
5267 (and (null window-system)
5268 (eq tty-erase-char ?\^H))))
5269 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
5271 On window systems, the default value of this option is chosen
5272 according to the keyboard used. If the keyboard has both a Backspace
5273 key and a Delete key, and both are mapped to their usual meanings, the
5274 option's default value is set to t, so that Backspace can be used to
5275 delete backward, and Delete can be used to delete forward.
5277 If not running under a window system, customizing this option accomplishes
5278 a similar effect by mapping C-h, which is usually generated by the
5279 Backspace key, to DEL, and by mapping DEL to C-d via
5280 `keyboard-translate'. The former functionality of C-h is available on
5281 the F1 key. You should probably not use this setting if you don't
5282 have both Backspace, Delete and F1 keys.
5284 Setting this variable with setq doesn't take effect. Programmatically,
5285 call `normal-erase-is-backspace-mode' (which see) instead."
5287 :group 'editing-basics
5289 :set (lambda (symbol value)
5290 ;; The fboundp is because of a problem with :set when
5291 ;; dumping Emacs. It doesn't really matter.
5292 (if (fboundp 'normal-erase-is-backspace-mode)
5293 (normal-erase-is-backspace-mode (or value 0))
5294 (set-default symbol value))))
5297 (defun normal-erase-is-backspace-mode (&optional arg)
5298 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
5300 With numeric arg, turn the mode on if and only if ARG is positive.
5302 On window systems, when this mode is on, Delete is mapped to C-d and
5303 Backspace is mapped to DEL; when this mode is off, both Delete and
5304 Backspace are mapped to DEL. (The remapping goes via
5305 `function-key-map', so binding Delete or Backspace in the global or
5306 local keymap will override that.)
5308 In addition, on window systems, the bindings of C-Delete, M-Delete,
5309 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
5310 the global keymap in accordance with the functionality of Delete and
5311 Backspace. For example, if Delete is remapped to C-d, which deletes
5312 forward, C-Delete is bound to `kill-word', but if Delete is remapped
5313 to DEL, which deletes backward, C-Delete is bound to
5314 `backward-kill-word'.
5316 If not running on a window system, a similar effect is accomplished by
5317 remapping C-h (normally produced by the Backspace key) and DEL via
5318 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
5319 to C-d; if it's off, the keys are not remapped.
5321 When not running on a window system, and this mode is turned on, the
5322 former functionality of C-h is available on the F1 key. You should
5323 probably not turn on this mode on a text-only terminal if you don't
5324 have both Backspace, Delete and F1 keys.
5326 See also `normal-erase-is-backspace'."
5328 (setq normal-erase-is-backspace
5330 (> (prefix-numeric-value arg) 0)
5331 (not normal-erase-is-backspace)))
5333 (cond ((or (memq window-system '(x w32 mac pc))
5334 (memq system-type '(ms-dos windows-nt)))
5336 `(([C-delete] [C-backspace])
5337 ([M-delete] [M-backspace])
5338 ([C-M-delete] [C-M-backspace])
5340 [C-delete] [C-backspace])))
5341 (old-state (lookup-key function-key-map [delete])))
5343 (if normal-erase-is-backspace
5345 (define-key function-key-map [delete] [?\C-d])
5346 (define-key function-key-map [kp-delete] [?\C-d])
5347 (define-key function-key-map [backspace] [?\C-?]))
5348 (define-key function-key-map [delete] [?\C-?])
5349 (define-key function-key-map [kp-delete] [?\C-?])
5350 (define-key function-key-map [backspace] [?\C-?]))
5352 ;; Maybe swap bindings of C-delete and C-backspace, etc.
5353 (unless (equal old-state (lookup-key function-key-map [delete]))
5354 (dolist (binding bindings)
5355 (let ((map global-map))
5356 (when (keymapp (car binding))
5357 (setq map (car binding) binding (cdr binding)))
5358 (let* ((key1 (nth 0 binding))
5359 (key2 (nth 1 binding))
5360 (binding1 (lookup-key map key1))
5361 (binding2 (lookup-key map key2)))
5362 (define-key map key1 binding2)
5363 (define-key map key2 binding1)))))))
5365 (if normal-erase-is-backspace
5367 (keyboard-translate ?\C-h ?\C-?)
5368 (keyboard-translate ?\C-? ?\C-d))
5369 (keyboard-translate ?\C-h ?\C-h)
5370 (keyboard-translate ?\C-? ?\C-?))))
5372 (run-hooks 'normal-erase-is-backspace-hook)
5374 (message "Delete key deletes %s"
5375 (if normal-erase-is-backspace "forward" "backward"))))
5377 (defvar vis-mode-saved-buffer-invisibility-spec nil
5378 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
5380 (define-minor-mode visible-mode
5381 "Toggle Visible mode.
5382 With argument ARG turn Visible mode on iff ARG is positive.
5384 Enabling Visible mode makes all invisible text temporarily visible.
5385 Disabling Visible mode turns off that effect. Visible mode
5386 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
5388 :group 'editing-basics
5389 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
5390 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
5391 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
5393 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
5394 buffer-invisibility-spec)
5395 (setq buffer-invisibility-spec nil)))
5397 ;; Minibuffer prompt stuff.
5399 ;(defun minibuffer-prompt-modification (start end)
5400 ; (error "You cannot modify the prompt"))
5403 ;(defun minibuffer-prompt-insertion (start end)
5404 ; (let ((inhibit-modification-hooks t))
5405 ; (delete-region start end)
5406 ; ;; Discard undo information for the text insertion itself
5407 ; ;; and for the text deletion.above.
5408 ; (when (consp buffer-undo-list)
5409 ; (setq buffer-undo-list (cddr buffer-undo-list)))
5410 ; (message "You cannot modify the prompt")))
5413 ;(setq minibuffer-prompt-properties
5414 ; (list 'modification-hooks '(minibuffer-prompt-modification)
5415 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
5420 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
5421 ;;; simple.el ends here