(Info-revert-find-node): Don't use beginning-of-buffer.
[emacs.git] / lisp / simple.el
blob864340e25d4c0ebade751019a97f42b451f7be36
1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99,
4 ;; 2000, 01, 02, 03, 2004
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; A grab-bag of basic Emacs commands not specifically related to some
30 ;; major mode or to file-handling.
32 ;;; Code:
34 (eval-when-compile
35 (autoload 'widget-convert "wid-edit")
36 (autoload 'shell-mode "shell"))
39 (defgroup killing nil
40 "Killing and yanking commands."
41 :group 'editing)
43 (defgroup paren-matching nil
44 "Highlight (un)matching of parens and expressions."
45 :group 'matching)
47 (define-key global-map [?\C-x right] 'next-buffer)
48 (define-key global-map [?\C-x left] 'prev-buffer)
49 (defun next-buffer ()
50 "Switch to the next buffer in cyclic order."
51 (interactive)
52 (let ((buffer (current-buffer)))
53 (switch-to-buffer (other-buffer buffer))
54 (bury-buffer buffer)))
56 (defun prev-buffer ()
57 "Switch to the previous buffer in cyclic order."
58 (interactive)
59 (let ((list (nreverse (buffer-list)))
60 found)
61 (while (and (not found) list)
62 (let ((buffer (car list)))
63 (if (and (not (get-buffer-window buffer))
64 (not (string-match "\\` " (buffer-name buffer))))
65 (setq found buffer)))
66 (setq list (cdr list)))
67 (switch-to-buffer found)))
69 ;;; next-error support framework
70 (defvar next-error-last-buffer nil
71 "The most recent next-error buffer.
72 A buffer becomes most recent when its compilation, grep, or
73 similar mode is started, or when it is used with \\[next-error]
74 or \\[compile-goto-error].")
76 (defvar next-error-function nil
77 "Function to use to find the next error in the current buffer.
78 The function is called with 2 parameters:
79 ARG is an integer specifying by how many errors to move.
80 RESET is a boolean which, if non-nil, says to go back to the beginning
81 of the errors before moving.
82 Major modes providing compile-like functionality should set this variable
83 to indicate to `next-error' that this is a candidate buffer and how
84 to navigate in it.")
86 (make-variable-buffer-local 'next-error-function)
88 (defsubst next-error-buffer-p (buffer &optional extra-test)
89 "Test if BUFFER is a next-error capable buffer."
90 (with-current-buffer buffer
91 (or (and extra-test (funcall extra-test))
92 next-error-function)))
94 (defun next-error-find-buffer (&optional other-buffer extra-test)
95 "Return a next-error capable buffer."
96 (or
97 ;; 1. If one window on the selected frame displays such buffer, return it.
98 (let ((window-buffers
99 (delete-dups
100 (delq nil (mapcar (lambda (w)
101 (if (next-error-buffer-p
102 (window-buffer w) extra-test)
103 (window-buffer w)))
104 (window-list))))))
105 (if other-buffer
106 (setq window-buffers (delq (current-buffer) window-buffers)))
107 (if (eq (length window-buffers) 1)
108 (car window-buffers)))
109 ;; 2. If next-error-last-buffer is set to a live buffer, use that.
110 (if (and next-error-last-buffer
111 (buffer-name next-error-last-buffer)
112 (next-error-buffer-p next-error-last-buffer extra-test)
113 (or (not other-buffer)
114 (not (eq next-error-last-buffer (current-buffer)))))
115 next-error-last-buffer)
116 ;; 3. If the current buffer is a next-error capable buffer, return it.
117 (if (and (not other-buffer)
118 (next-error-buffer-p (current-buffer) extra-test))
119 (current-buffer))
120 ;; 4. Look for a next-error capable buffer in a buffer list.
121 (let ((buffers (buffer-list)))
122 (while (and buffers
123 (or (not (next-error-buffer-p (car buffers) extra-test))
124 (and other-buffer (eq (car buffers) (current-buffer)))))
125 (setq buffers (cdr buffers)))
126 (if buffers
127 (car buffers)
128 (or (and other-buffer
129 (next-error-buffer-p (current-buffer) extra-test)
130 ;; The current buffer is a next-error capable buffer.
131 (progn
132 (if other-buffer
133 (message "This is the only next-error capable buffer"))
134 (current-buffer)))
135 (error "No next-error capable buffer found"))))))
137 (defun next-error (&optional arg reset)
138 "Visit next next-error message and corresponding source code.
140 If all the error messages parsed so far have been processed already,
141 the message buffer is checked for new ones.
143 A prefix ARG specifies how many error messages to move;
144 negative means move back to previous error messages.
145 Just \\[universal-argument] as a prefix means reparse the error message buffer
146 and start at the first error.
148 The RESET argument specifies that we should restart from the beginning.
150 \\[next-error] normally uses the most recently started
151 compilation, grep, or occur buffer. It can also operate on any
152 buffer with output from the \\[compile], \\[grep] commands, or,
153 more generally, on any buffer in Compilation mode or with
154 Compilation Minor mode enabled, or any buffer in which
155 `next-error-function' is bound to an appropriate function.
156 To specify use of a particular buffer for error messages, type
157 \\[next-error] in that buffer when it is the only one displayed
158 in the current frame.
160 Once \\[next-error] has chosen the buffer for error messages,
161 it stays with that buffer until you use it in some other buffer which
162 uses Compilation mode or Compilation Minor mode.
164 See variables `compilation-parse-errors-function' and
165 \`compilation-error-regexp-alist' for customization ideas."
166 (interactive "P")
167 (if (consp arg) (setq reset t arg nil))
168 (when (setq next-error-last-buffer (next-error-find-buffer))
169 ;; we know here that next-error-function is a valid symbol we can funcall
170 (with-current-buffer next-error-last-buffer
171 (funcall next-error-function (prefix-numeric-value arg) reset))))
173 (defalias 'goto-next-locus 'next-error)
174 (defalias 'next-match 'next-error)
176 (define-key ctl-x-map "`" 'next-error)
178 (defun previous-error (&optional n)
179 "Visit previous next-error message and corresponding source code.
181 Prefix arg N says how many error messages to move backwards (or
182 forwards, if negative).
184 This operates on the output from the \\[compile] and \\[grep] commands."
185 (interactive "p")
186 (next-error (- (or n 1))))
188 (defun first-error (&optional n)
189 "Restart at the first error.
190 Visit corresponding source code.
191 With prefix arg N, visit the source code of the Nth error.
192 This operates on the output from the \\[compile] command, for instance."
193 (interactive "p")
194 (next-error n t))
196 (defun next-error-no-select (&optional n)
197 "Move point to the next error in the next-error buffer and highlight match.
198 Prefix arg N says how many error messages to move forwards (or
199 backwards, if negative).
200 Finds and highlights the source line like \\[next-error], but does not
201 select the source buffer."
202 (interactive "p")
203 (let ((next-error-highlight next-error-highlight-no-select))
204 (next-error n))
205 (pop-to-buffer next-error-last-buffer))
207 (defun previous-error-no-select (&optional n)
208 "Move point to the previous error in the next-error buffer and highlight match.
209 Prefix arg N says how many error messages to move backwards (or
210 forwards, if negative).
211 Finds and highlights the source line like \\[previous-error], but does not
212 select the source buffer."
213 (interactive "p")
214 (next-error-no-select (- (or n 1))))
216 (defgroup next-error nil
217 "next-error support framework."
218 :group 'compilation
219 :version "21.4")
221 (defface next-error
222 '((t (:inherit region)))
223 "Face used to highlight next error locus."
224 :group 'next-error
225 :version "21.4")
227 (defcustom next-error-highlight 0.1
228 "*Highlighting of locations in selected source buffers.
229 If number, highlight the locus in next-error face for given time in seconds.
230 If t, use persistent overlays fontified in next-error face.
231 If nil, don't highlight the locus in the source buffer.
232 If `fringe-arrow', indicate the locus by the fringe arrow."
233 :type '(choice (number :tag "Delay")
234 (const :tag "Persistent overlay" t)
235 (const :tag "No highlighting" nil)
236 (const :tag "Fringe arrow" 'fringe-arrow))
237 :group 'next-error
238 :version "21.4")
240 (defcustom next-error-highlight-no-select 0.1
241 "*Highlighting of locations in non-selected source buffers.
242 If number, highlight the locus in next-error face for given time in seconds.
243 If t, use persistent overlays fontified in next-error face.
244 If nil, don't highlight the locus in the source buffer.
245 If `fringe-arrow', indicate the locus by the fringe arrow."
246 :type '(choice (number :tag "Delay")
247 (const :tag "Persistent overlay" t)
248 (const :tag "No highlighting" nil)
249 (const :tag "Fringe arrow" 'fringe-arrow))
250 :group 'next-error
251 :version "21.4")
253 ;;; Internal variable for `next-error-follow-mode-post-command-hook'.
254 (defvar next-error-follow-last-line nil)
256 (define-minor-mode next-error-follow-minor-mode
257 "Minor mode for compilation, occur and diff modes.
258 When turned on, cursor motion in the compilation, grep, occur or diff
259 buffer causes automatic display of the corresponding source code
260 location."
261 nil " Fol" nil
262 (if (not next-error-follow-minor-mode)
263 (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t)
264 (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t)
265 (make-variable-buffer-local 'next-error-follow-last-line)))
267 ;;; Used as a `post-command-hook' by `next-error-follow-mode'
268 ;;; for the *Compilation* *grep* and *Occur* buffers.
269 (defun next-error-follow-mode-post-command-hook ()
270 (unless (equal next-error-follow-last-line (line-number-at-pos))
271 (setq next-error-follow-last-line (line-number-at-pos))
272 (condition-case nil
273 (let ((compilation-context-lines nil))
274 (setq compilation-current-error (point))
275 (next-error-no-select 0))
276 (error t))))
281 (defun fundamental-mode ()
282 "Major mode not specialized for anything in particular.
283 Other major modes are defined by comparison with this one."
284 (interactive)
285 (kill-all-local-variables)
286 (run-hooks 'after-change-major-mode-hook))
288 ;; Making and deleting lines.
290 (defun newline (&optional arg)
291 "Insert a newline, and move to left margin of the new line if it's blank.
292 If `use-hard-newlines' is non-nil, the newline is marked with the
293 text-property `hard'.
294 With ARG, insert that many newlines.
295 Call `auto-fill-function' if the current column number is greater
296 than the value of `fill-column' and ARG is nil."
297 (interactive "*P")
298 (barf-if-buffer-read-only)
299 ;; Inserting a newline at the end of a line produces better redisplay in
300 ;; try_window_id than inserting at the beginning of a line, and the textual
301 ;; result is the same. So, if we're at beginning of line, pretend to be at
302 ;; the end of the previous line.
303 (let ((flag (and (not (bobp))
304 (bolp)
305 ;; Make sure no functions want to be told about
306 ;; the range of the changes.
307 (not after-change-functions)
308 (not before-change-functions)
309 ;; Make sure there are no markers here.
310 (not (buffer-has-markers-at (1- (point))))
311 (not (buffer-has-markers-at (point)))
312 ;; Make sure no text properties want to know
313 ;; where the change was.
314 (not (get-char-property (1- (point)) 'modification-hooks))
315 (not (get-char-property (1- (point)) 'insert-behind-hooks))
316 (or (eobp)
317 (not (get-char-property (point) 'insert-in-front-hooks)))
318 ;; Make sure the newline before point isn't intangible.
319 (not (get-char-property (1- (point)) 'intangible))
320 ;; Make sure the newline before point isn't read-only.
321 (not (get-char-property (1- (point)) 'read-only))
322 ;; Make sure the newline before point isn't invisible.
323 (not (get-char-property (1- (point)) 'invisible))
324 ;; Make sure the newline before point has the same
325 ;; properties as the char before it (if any).
326 (< (or (previous-property-change (point)) -2)
327 (- (point) 2))))
328 (was-page-start (and (bolp)
329 (looking-at page-delimiter)))
330 (beforepos (point)))
331 (if flag (backward-char 1))
332 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
333 ;; Set last-command-char to tell self-insert what to insert.
334 (let ((last-command-char ?\n)
335 ;; Don't auto-fill if we have a numeric argument.
336 ;; Also not if flag is true (it would fill wrong line);
337 ;; there is no need to since we're at BOL.
338 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
339 (unwind-protect
340 (self-insert-command (prefix-numeric-value arg))
341 ;; If we get an error in self-insert-command, put point at right place.
342 (if flag (forward-char 1))))
343 ;; Even if we did *not* get an error, keep that forward-char;
344 ;; all further processing should apply to the newline that the user
345 ;; thinks he inserted.
347 ;; Mark the newline(s) `hard'.
348 (if use-hard-newlines
349 (set-hard-newline-properties
350 (- (point) (if arg (prefix-numeric-value arg) 1)) (point)))
351 ;; If the newline leaves the previous line blank,
352 ;; and we have a left margin, delete that from the blank line.
353 (or flag
354 (save-excursion
355 (goto-char beforepos)
356 (beginning-of-line)
357 (and (looking-at "[ \t]$")
358 (> (current-left-margin) 0)
359 (delete-region (point) (progn (end-of-line) (point))))))
360 ;; Indent the line after the newline, except in one case:
361 ;; when we added the newline at the beginning of a line
362 ;; which starts a page.
363 (or was-page-start
364 (move-to-left-margin nil t)))
365 nil)
367 (defun set-hard-newline-properties (from to)
368 (let ((sticky (get-text-property from 'rear-nonsticky)))
369 (put-text-property from to 'hard 't)
370 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
371 (if (and (listp sticky) (not (memq 'hard sticky)))
372 (put-text-property from (point) 'rear-nonsticky
373 (cons 'hard sticky)))))
375 (defun open-line (n)
376 "Insert a newline and leave point before it.
377 If there is a fill prefix and/or a left-margin, insert them on the new line
378 if the line would have been blank.
379 With arg N, insert N newlines."
380 (interactive "*p")
381 (let* ((do-fill-prefix (and fill-prefix (bolp)))
382 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
383 (loc (point))
384 ;; Don't expand an abbrev before point.
385 (abbrev-mode nil))
386 (newline n)
387 (goto-char loc)
388 (while (> n 0)
389 (cond ((bolp)
390 (if do-left-margin (indent-to (current-left-margin)))
391 (if do-fill-prefix (insert-and-inherit fill-prefix))))
392 (forward-line 1)
393 (setq n (1- n)))
394 (goto-char loc)
395 (end-of-line)))
397 (defun split-line (&optional arg)
398 "Split current line, moving portion beyond point vertically down.
399 If the current line starts with `fill-prefix', insert it on the new
400 line as well. With prefix ARG, don't insert fill-prefix on new line.
402 When called from Lisp code, ARG may be a prefix string to copy."
403 (interactive "*P")
404 (skip-chars-forward " \t")
405 (let* ((col (current-column))
406 (pos (point))
407 ;; What prefix should we check for (nil means don't).
408 (prefix (cond ((stringp arg) arg)
409 (arg nil)
410 (t fill-prefix)))
411 ;; Does this line start with it?
412 (have-prfx (and prefix
413 (save-excursion
414 (beginning-of-line)
415 (looking-at (regexp-quote prefix))))))
416 (newline 1)
417 (if have-prfx (insert-and-inherit prefix))
418 (indent-to col 0)
419 (goto-char pos)))
421 (defun delete-indentation (&optional arg)
422 "Join this line to previous and fix up whitespace at join.
423 If there is a fill prefix, delete it from the beginning of this line.
424 With argument, join this line to following line."
425 (interactive "*P")
426 (beginning-of-line)
427 (if arg (forward-line 1))
428 (if (eq (preceding-char) ?\n)
429 (progn
430 (delete-region (point) (1- (point)))
431 ;; If the second line started with the fill prefix,
432 ;; delete the prefix.
433 (if (and fill-prefix
434 (<= (+ (point) (length fill-prefix)) (point-max))
435 (string= fill-prefix
436 (buffer-substring (point)
437 (+ (point) (length fill-prefix)))))
438 (delete-region (point) (+ (point) (length fill-prefix))))
439 (fixup-whitespace))))
441 (defalias 'join-line #'delete-indentation) ; easier to find
443 (defun delete-blank-lines ()
444 "On blank line, delete all surrounding blank lines, leaving just one.
445 On isolated blank line, delete that one.
446 On nonblank line, delete any immediately following blank lines."
447 (interactive "*")
448 (let (thisblank singleblank)
449 (save-excursion
450 (beginning-of-line)
451 (setq thisblank (looking-at "[ \t]*$"))
452 ;; Set singleblank if there is just one blank line here.
453 (setq singleblank
454 (and thisblank
455 (not (looking-at "[ \t]*\n[ \t]*$"))
456 (or (bobp)
457 (progn (forward-line -1)
458 (not (looking-at "[ \t]*$")))))))
459 ;; Delete preceding blank lines, and this one too if it's the only one.
460 (if thisblank
461 (progn
462 (beginning-of-line)
463 (if singleblank (forward-line 1))
464 (delete-region (point)
465 (if (re-search-backward "[^ \t\n]" nil t)
466 (progn (forward-line 1) (point))
467 (point-min)))))
468 ;; Delete following blank lines, unless the current line is blank
469 ;; and there are no following blank lines.
470 (if (not (and thisblank singleblank))
471 (save-excursion
472 (end-of-line)
473 (forward-line 1)
474 (delete-region (point)
475 (if (re-search-forward "[^ \t\n]" nil t)
476 (progn (beginning-of-line) (point))
477 (point-max)))))
478 ;; Handle the special case where point is followed by newline and eob.
479 ;; Delete the line, leaving point at eob.
480 (if (looking-at "^[ \t]*\n\\'")
481 (delete-region (point) (point-max)))))
483 (defun delete-trailing-whitespace ()
484 "Delete all the trailing whitespace across the current buffer.
485 All whitespace after the last non-whitespace character in a line is deleted.
486 This respects narrowing, created by \\[narrow-to-region] and friends.
487 A formfeed is not considered whitespace by this function."
488 (interactive "*")
489 (save-match-data
490 (save-excursion
491 (goto-char (point-min))
492 (while (re-search-forward "\\s-$" nil t)
493 (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
494 ;; Don't delete formfeeds, even if they are considered whitespace.
495 (save-match-data
496 (if (looking-at ".*\f")
497 (goto-char (match-end 0))))
498 (delete-region (point) (match-end 0))))))
500 (defun newline-and-indent ()
501 "Insert a newline, then indent according to major mode.
502 Indentation is done using the value of `indent-line-function'.
503 In programming language modes, this is the same as TAB.
504 In some text modes, where TAB inserts a tab, this command indents to the
505 column specified by the function `current-left-margin'."
506 (interactive "*")
507 (delete-horizontal-space t)
508 (newline)
509 (indent-according-to-mode))
511 (defun reindent-then-newline-and-indent ()
512 "Reindent current line, insert newline, then indent the new line.
513 Indentation of both lines is done according to the current major mode,
514 which means calling the current value of `indent-line-function'.
515 In programming language modes, this is the same as TAB.
516 In some text modes, where TAB inserts a tab, this indents to the
517 column specified by the function `current-left-margin'."
518 (interactive "*")
519 (let ((pos (point)))
520 ;; Be careful to insert the newline before indenting the line.
521 ;; Otherwise, the indentation might be wrong.
522 (newline)
523 (save-excursion
524 (goto-char pos)
525 (indent-according-to-mode)
526 (delete-horizontal-space t))
527 (indent-according-to-mode)))
529 (defun quoted-insert (arg)
530 "Read next input character and insert it.
531 This is useful for inserting control characters.
533 If the first character you type after this command is an octal digit,
534 you should type a sequence of octal digits which specify a character code.
535 Any nondigit terminates the sequence. If the terminator is a RET,
536 it is discarded; any other terminator is used itself as input.
537 The variable `read-quoted-char-radix' specifies the radix for this feature;
538 set it to 10 or 16 to use decimal or hex instead of octal.
540 In overwrite mode, this function inserts the character anyway, and
541 does not handle octal digits specially. This means that if you use
542 overwrite as your normal editing mode, you can use this function to
543 insert characters when necessary.
545 In binary overwrite mode, this function does overwrite, and octal
546 digits are interpreted as a character code. This is intended to be
547 useful for editing binary files."
548 (interactive "*p")
549 (let* ((char (let (translation-table-for-input)
550 (if (or (not overwrite-mode)
551 (eq overwrite-mode 'overwrite-mode-binary))
552 (read-quoted-char)
553 (read-char)))))
554 ;; Assume character codes 0240 - 0377 stand for characters in some
555 ;; single-byte character set, and convert them to Emacs
556 ;; characters.
557 (if (and enable-multibyte-characters
558 (>= char ?\240)
559 (<= char ?\377))
560 (setq char (unibyte-char-to-multibyte char)))
561 (if (> arg 0)
562 (if (eq overwrite-mode 'overwrite-mode-binary)
563 (delete-char arg)))
564 (while (> arg 0)
565 (insert-and-inherit char)
566 (setq arg (1- arg)))))
568 (defun forward-to-indentation (&optional arg)
569 "Move forward ARG lines and position at first nonblank character."
570 (interactive "p")
571 (forward-line (or arg 1))
572 (skip-chars-forward " \t"))
574 (defun backward-to-indentation (&optional arg)
575 "Move backward ARG lines and position at first nonblank character."
576 (interactive "p")
577 (forward-line (- (or arg 1)))
578 (skip-chars-forward " \t"))
580 (defun back-to-indentation ()
581 "Move point to the first non-whitespace character on this line."
582 (interactive)
583 (beginning-of-line 1)
584 (skip-syntax-forward " " (line-end-position))
585 ;; Move back over chars that have whitespace syntax but have the p flag.
586 (backward-prefix-chars))
588 (defun fixup-whitespace ()
589 "Fixup white space between objects around point.
590 Leave one space or none, according to the context."
591 (interactive "*")
592 (save-excursion
593 (delete-horizontal-space)
594 (if (or (looking-at "^\\|\\s)")
595 (save-excursion (forward-char -1)
596 (looking-at "$\\|\\s(\\|\\s'")))
598 (insert ?\ ))))
600 (defun delete-horizontal-space (&optional backward-only)
601 "Delete all spaces and tabs around point.
602 If BACKWARD-ONLY is non-nil, only delete spaces before point."
603 (interactive "*")
604 (let ((orig-pos (point)))
605 (delete-region
606 (if backward-only
607 orig-pos
608 (progn
609 (skip-chars-forward " \t")
610 (constrain-to-field nil orig-pos t)))
611 (progn
612 (skip-chars-backward " \t")
613 (constrain-to-field nil orig-pos)))))
615 (defun just-one-space ()
616 "Delete all spaces and tabs around point, leaving one space."
617 (interactive "*")
618 (let ((orig-pos (point)))
619 (skip-chars-backward " \t")
620 (constrain-to-field nil orig-pos)
621 (if (= (following-char) ? )
622 (forward-char 1)
623 (insert ? ))
624 (delete-region
625 (point)
626 (progn
627 (skip-chars-forward " \t")
628 (constrain-to-field nil orig-pos t)))))
630 (defvar inhibit-mark-movement nil
631 "If non-nil, \\[beginning-of-buffer] and \\[end-of-buffer] does not set the mark.")
633 (defun beginning-of-buffer (&optional arg)
634 "Move point to the beginning of the buffer; leave mark at previous position.
635 With \\[universal-argument] prefix, do not set mark at previous position.
636 With numeric arg N, put point N/10 of the way from the beginning.
638 If the buffer is narrowed, this command uses the beginning and size
639 of the accessible part of the buffer.
641 Don't use this command in Lisp programs!
642 \(goto-char (point-min)) is faster and avoids clobbering the mark."
643 (interactive "P")
644 (unless (or inhibit-mark-movement (consp arg))
645 (push-mark))
646 (let ((size (- (point-max) (point-min))))
647 (goto-char (if (and arg (not (consp arg)))
648 (+ (point-min)
649 (if (> size 10000)
650 ;; Avoid overflow for large buffer sizes!
651 (* (prefix-numeric-value arg)
652 (/ size 10))
653 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
654 (point-min))))
655 (if arg (forward-line 1)))
657 (defun end-of-buffer (&optional arg)
658 "Move point to the end of the buffer; leave mark at previous position.
659 With \\[universal-argument] prefix, do not set mark at previous position.
660 With numeric arg N, put point N/10 of the way from the end.
662 If the buffer is narrowed, this command uses the beginning and size
663 of the accessible part of the buffer.
665 Don't use this command in Lisp programs!
666 \(goto-char (point-max)) is faster and avoids clobbering the mark."
667 (interactive "P")
668 (unless (or inhibit-mark-movement (consp arg))
669 (push-mark))
670 (let ((size (- (point-max) (point-min))))
671 (goto-char (if (and arg (not (consp arg)))
672 (- (point-max)
673 (if (> size 10000)
674 ;; Avoid overflow for large buffer sizes!
675 (* (prefix-numeric-value arg)
676 (/ size 10))
677 (/ (* size (prefix-numeric-value arg)) 10)))
678 (point-max))))
679 ;; If we went to a place in the middle of the buffer,
680 ;; adjust it to the beginning of a line.
681 (cond (arg (forward-line 1))
682 ((> (point) (window-end nil t))
683 ;; If the end of the buffer is not already on the screen,
684 ;; then scroll specially to put it near, but not at, the bottom.
685 (overlay-recenter (point))
686 (recenter -3))))
688 (defun mark-whole-buffer ()
689 "Put point at beginning and mark at end of buffer.
690 You probably should not use this function in Lisp programs;
691 it is usually a mistake for a Lisp function to use any subroutine
692 that uses or sets the mark."
693 (interactive)
694 (push-mark (point))
695 (push-mark (point-max) nil t)
696 (goto-char (point-min)))
699 ;; Counting lines, one way or another.
701 (defun goto-line (arg)
702 "Goto line ARG, counting from line 1 at beginning of buffer."
703 (interactive "NGoto line: ")
704 (setq arg (prefix-numeric-value arg))
705 (save-restriction
706 (widen)
707 (goto-char 1)
708 (if (eq selective-display t)
709 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
710 (forward-line (1- arg)))))
712 (defun count-lines-region (start end)
713 "Print number of lines and characters in the region."
714 (interactive "r")
715 (message "Region has %d lines, %d characters"
716 (count-lines start end) (- end start)))
718 (defun what-line ()
719 "Print the current buffer line number and narrowed line number of point."
720 (interactive)
721 (let ((opoint (point)) (start (point-min))
722 (n (line-number-at-pos)))
723 (if (= start 1)
724 (message "Line %d" n)
725 (save-excursion
726 (save-restriction
727 (widen)
728 (message "line %d (narrowed line %d)"
729 (+ n (line-number-at-pos start) -1) n))))))
731 (defun count-lines (start end)
732 "Return number of lines between START and END.
733 This is usually the number of newlines between them,
734 but can be one more if START is not equal to END
735 and the greater of them is not at the start of a line."
736 (save-excursion
737 (save-restriction
738 (narrow-to-region start end)
739 (goto-char (point-min))
740 (if (eq selective-display t)
741 (save-match-data
742 (let ((done 0))
743 (while (re-search-forward "[\n\C-m]" nil t 40)
744 (setq done (+ 40 done)))
745 (while (re-search-forward "[\n\C-m]" nil t 1)
746 (setq done (+ 1 done)))
747 (goto-char (point-max))
748 (if (and (/= start end)
749 (not (bolp)))
750 (1+ done)
751 done)))
752 (- (buffer-size) (forward-line (buffer-size)))))))
754 (defun line-number-at-pos (&optional pos)
755 "Return (narrowed) buffer line number at position POS.
756 If POS is nil, use current buffer location."
757 (let ((opoint (or pos (point))) start)
758 (save-excursion
759 (goto-char (point-min))
760 (setq start (point))
761 (goto-char opoint)
762 (forward-line 0)
763 (1+ (count-lines start (point))))))
765 (defun what-cursor-position (&optional detail)
766 "Print info on cursor position (on screen and within buffer).
767 Also describe the character after point, and give its character code
768 in octal, decimal and hex.
770 For a non-ASCII multibyte character, also give its encoding in the
771 buffer's selected coding system if the coding system encodes the
772 character safely. If the character is encoded into one byte, that
773 code is shown in hex. If the character is encoded into more than one
774 byte, just \"...\" is shown.
776 In addition, with prefix argument, show details about that character
777 in *Help* buffer. See also the command `describe-char'."
778 (interactive "P")
779 (let* ((char (following-char))
780 (beg (point-min))
781 (end (point-max))
782 (pos (point))
783 (total (buffer-size))
784 (percent (if (> total 50000)
785 ;; Avoid overflow from multiplying by 100!
786 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
787 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
788 (hscroll (if (= (window-hscroll) 0)
790 (format " Hscroll=%d" (window-hscroll))))
791 (col (current-column)))
792 (if (= pos end)
793 (if (or (/= beg 1) (/= end (1+ total)))
794 (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
795 pos total percent beg end col hscroll)
796 (message "point=%d of %d (%d%%) column %d %s"
797 pos total percent col hscroll))
798 (let ((coding buffer-file-coding-system)
799 encoded encoding-msg)
800 (if (or (not coding)
801 (eq (coding-system-type coding) t))
802 (setq coding default-buffer-file-coding-system))
803 (if (not (char-valid-p char))
804 (setq encoding-msg
805 (format "(0%o, %d, 0x%x, invalid)" char char char))
806 (setq encoded (and (>= char 128) (encode-coding-char char coding)))
807 (setq encoding-msg
808 (if encoded
809 (format "(0%o, %d, 0x%x, file %s)"
810 char char char
811 (if (> (length encoded) 1)
812 "..."
813 (encoded-string-description encoded coding)))
814 (format "(0%o, %d, 0x%x)" char char char))))
815 (if detail
816 ;; We show the detailed information about CHAR.
817 (describe-char (point)))
818 (if (or (/= beg 1) (/= end (1+ total)))
819 (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
820 (if (< char 256)
821 (single-key-description char)
822 (buffer-substring-no-properties (point) (1+ (point))))
823 encoding-msg pos total percent beg end col hscroll)
824 (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
825 (if (< char 256)
826 (single-key-description char)
827 (buffer-substring-no-properties (point) (1+ (point))))
828 encoding-msg pos total percent col hscroll))))))
830 (defvar read-expression-map
831 (let ((m (make-sparse-keymap)))
832 (define-key m "\M-\t" 'lisp-complete-symbol)
833 (set-keymap-parent m minibuffer-local-map)
835 "Minibuffer keymap used for reading Lisp expressions.")
837 (defvar read-expression-history nil)
839 (defcustom eval-expression-print-level 4
840 "*Value to use for `print-level' when printing value in `eval-expression'.
841 A value of nil means no limit."
842 :group 'lisp
843 :type '(choice (const :tag "No Limit" nil) integer)
844 :version "21.1")
846 (defcustom eval-expression-print-length 12
847 "*Value to use for `print-length' when printing value in `eval-expression'.
848 A value of nil means no limit."
849 :group 'lisp
850 :type '(choice (const :tag "No Limit" nil) integer)
851 :version "21.1")
853 (defcustom eval-expression-debug-on-error t
854 "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'.
855 If nil, don't change the value of `debug-on-error'."
856 :group 'lisp
857 :type 'boolean
858 :version "21.1")
860 (defun eval-expression-print-format (value)
861 "Format VALUE as a result of evaluated expression.
862 Return a formatted string which is displayed in the echo area
863 in addition to the value printed by prin1 in functions which
864 display the result of expression evaluation."
865 (if (and (integerp value)
866 (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
867 (eq this-command last-command)
868 (and (boundp 'edebug-active) edebug-active)))
869 (let ((char-string
870 (if (or (and (boundp 'edebug-active) edebug-active)
871 (memq this-command '(eval-last-sexp eval-print-last-sexp)))
872 (prin1-char value))))
873 (if char-string
874 (format " (0%o, 0x%x) = %s" value value char-string)
875 (format " (0%o, 0x%x)" value value)))))
877 ;; We define this, rather than making `eval' interactive,
878 ;; for the sake of completion of names like eval-region, eval-current-buffer.
879 (defun eval-expression (eval-expression-arg
880 &optional eval-expression-insert-value)
881 "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
882 Value is also consed on to front of the variable `values'.
883 Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
884 insert the result into the current buffer instead of printing it in
885 the echo area."
886 (interactive
887 (list (read-from-minibuffer "Eval: "
888 nil read-expression-map t
889 'read-expression-history)
890 current-prefix-arg))
892 (if (null eval-expression-debug-on-error)
893 (setq values (cons (eval eval-expression-arg) values))
894 (let ((old-value (make-symbol "t")) new-value)
895 ;; Bind debug-on-error to something unique so that we can
896 ;; detect when evaled code changes it.
897 (let ((debug-on-error old-value))
898 (setq values (cons (eval eval-expression-arg) values))
899 (setq new-value debug-on-error))
900 ;; If evaled code has changed the value of debug-on-error,
901 ;; propagate that change to the global binding.
902 (unless (eq old-value new-value)
903 (setq debug-on-error new-value))))
905 (let ((print-length eval-expression-print-length)
906 (print-level eval-expression-print-level))
907 (if eval-expression-insert-value
908 (with-no-warnings
909 (let ((standard-output (current-buffer)))
910 (eval-last-sexp-print-value (car values))))
911 (prog1
912 (prin1 (car values) t)
913 (let ((str (eval-expression-print-format (car values))))
914 (if str (princ str t)))))))
916 (defun edit-and-eval-command (prompt command)
917 "Prompting with PROMPT, let user edit COMMAND and eval result.
918 COMMAND is a Lisp expression. Let user edit that expression in
919 the minibuffer, then read and evaluate the result."
920 (let ((command
921 (let ((print-level nil)
922 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
923 (unwind-protect
924 (read-from-minibuffer prompt
925 (prin1-to-string command)
926 read-expression-map t
927 'command-history)
928 ;; If command was added to command-history as a string,
929 ;; get rid of that. We want only evaluable expressions there.
930 (if (stringp (car command-history))
931 (setq command-history (cdr command-history)))))))
933 ;; If command to be redone does not match front of history,
934 ;; add it to the history.
935 (or (equal command (car command-history))
936 (setq command-history (cons command command-history)))
937 (eval command)))
939 (defun repeat-complex-command (arg)
940 "Edit and re-evaluate last complex command, or ARGth from last.
941 A complex command is one which used the minibuffer.
942 The command is placed in the minibuffer as a Lisp form for editing.
943 The result is executed, repeating the command as changed.
944 If the command has been changed or is not the most recent previous command
945 it is added to the front of the command history.
946 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
947 to get different commands to edit and resubmit."
948 (interactive "p")
949 (let ((elt (nth (1- arg) command-history))
950 newcmd)
951 (if elt
952 (progn
953 (setq newcmd
954 (let ((print-level nil)
955 (minibuffer-history-position arg)
956 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
957 (unwind-protect
958 (read-from-minibuffer
959 "Redo: " (prin1-to-string elt) read-expression-map t
960 (cons 'command-history arg))
962 ;; If command was added to command-history as a
963 ;; string, get rid of that. We want only
964 ;; evaluable expressions there.
965 (if (stringp (car command-history))
966 (setq command-history (cdr command-history))))))
968 ;; If command to be redone does not match front of history,
969 ;; add it to the history.
970 (or (equal newcmd (car command-history))
971 (setq command-history (cons newcmd command-history)))
972 (eval newcmd))
973 (if command-history
974 (error "Argument %d is beyond length of command history" arg)
975 (error "There are no previous complex commands to repeat")))))
977 (defvar minibuffer-history nil
978 "Default minibuffer history list.
979 This is used for all minibuffer input
980 except when an alternate history list is specified.")
981 (defvar minibuffer-history-sexp-flag nil
982 "Control whether history list elements are expressions or strings.
983 If the value of this variable equals current minibuffer depth,
984 they are expressions; otherwise they are strings.
985 \(That convention is designed to do the right thing fora
986 recursive uses of the minibuffer.)")
987 (setq minibuffer-history-variable 'minibuffer-history)
988 (setq minibuffer-history-position nil)
989 (defvar minibuffer-history-search-history nil)
991 (defvar minibuffer-text-before-history nil
992 "Text that was in this minibuffer before any history commands.
993 This is nil if there have not yet been any history commands
994 in this use of the minibuffer.")
996 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
998 (defun minibuffer-history-initialize ()
999 (setq minibuffer-text-before-history nil))
1001 (defun minibuffer-avoid-prompt (new old)
1002 "A point-motion hook for the minibuffer, that moves point out of the prompt."
1003 (constrain-to-field nil (point-max)))
1005 (defcustom minibuffer-history-case-insensitive-variables nil
1006 "*Minibuffer history variables for which matching should ignore case.
1007 If a history variable is a member of this list, then the
1008 \\[previous-matching-history-element] and \\[next-matching-history-element]\
1009 commands ignore case when searching it, regardless of `case-fold-search'."
1010 :type '(repeat variable)
1011 :group 'minibuffer)
1013 (defun previous-matching-history-element (regexp n)
1014 "Find the previous history element that matches REGEXP.
1015 \(Previous history elements refer to earlier actions.)
1016 With prefix argument N, search for Nth previous match.
1017 If N is negative, find the next or Nth next match.
1018 Normally, history elements are matched case-insensitively if
1019 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1020 makes the search case-sensitive.
1021 See also `minibuffer-history-case-insensitive-variables'."
1022 (interactive
1023 (let* ((enable-recursive-minibuffers t)
1024 (regexp (read-from-minibuffer "Previous element matching (regexp): "
1026 minibuffer-local-map
1028 'minibuffer-history-search-history
1029 (car minibuffer-history-search-history))))
1030 ;; Use the last regexp specified, by default, if input is empty.
1031 (list (if (string= regexp "")
1032 (if minibuffer-history-search-history
1033 (car minibuffer-history-search-history)
1034 (error "No previous history search regexp"))
1035 regexp)
1036 (prefix-numeric-value current-prefix-arg))))
1037 (unless (zerop n)
1038 (if (and (zerop minibuffer-history-position)
1039 (null minibuffer-text-before-history))
1040 (setq minibuffer-text-before-history
1041 (minibuffer-contents-no-properties)))
1042 (let ((history (symbol-value minibuffer-history-variable))
1043 (case-fold-search
1044 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
1045 ;; On some systems, ignore case for file names.
1046 (if (memq minibuffer-history-variable
1047 minibuffer-history-case-insensitive-variables)
1049 ;; Respect the user's setting for case-fold-search:
1050 case-fold-search)
1051 nil))
1052 prevpos
1053 match-string
1054 match-offset
1055 (pos minibuffer-history-position))
1056 (while (/= n 0)
1057 (setq prevpos pos)
1058 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1059 (when (= pos prevpos)
1060 (error (if (= pos 1)
1061 "No later matching history item"
1062 "No earlier matching history item")))
1063 (setq match-string
1064 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
1065 (let ((print-level nil))
1066 (prin1-to-string (nth (1- pos) history)))
1067 (nth (1- pos) history)))
1068 (setq match-offset
1069 (if (< n 0)
1070 (and (string-match regexp match-string)
1071 (match-end 0))
1072 (and (string-match (concat ".*\\(" regexp "\\)") match-string)
1073 (match-beginning 1))))
1074 (when match-offset
1075 (setq n (+ n (if (< n 0) 1 -1)))))
1076 (setq minibuffer-history-position pos)
1077 (goto-char (point-max))
1078 (delete-minibuffer-contents)
1079 (insert match-string)
1080 (goto-char (+ (minibuffer-prompt-end) match-offset))))
1081 (if (memq (car (car command-history)) '(previous-matching-history-element
1082 next-matching-history-element))
1083 (setq command-history (cdr command-history))))
1085 (defun next-matching-history-element (regexp n)
1086 "Find the next history element that matches REGEXP.
1087 \(The next history element refers to a more recent action.)
1088 With prefix argument N, search for Nth next match.
1089 If N is negative, find the previous or Nth previous match.
1090 Normally, history elements are matched case-insensitively if
1091 `case-fold-search' is non-nil, but an uppercase letter in REGEXP
1092 makes the search case-sensitive."
1093 (interactive
1094 (let* ((enable-recursive-minibuffers t)
1095 (regexp (read-from-minibuffer "Next element matching (regexp): "
1097 minibuffer-local-map
1099 'minibuffer-history-search-history)))
1100 ;; Use the last regexp specified, by default, if input is empty.
1101 (list (if (string= regexp "")
1102 (setcar minibuffer-history-search-history
1103 (nth 1 minibuffer-history-search-history))
1104 regexp)
1105 (prefix-numeric-value current-prefix-arg))))
1106 (previous-matching-history-element regexp (- n)))
1108 (defvar minibuffer-temporary-goal-position nil)
1110 (defun next-history-element (n)
1111 "Insert the next element of the minibuffer history into the minibuffer."
1112 (interactive "p")
1113 (or (zerop n)
1114 (let ((narg (- minibuffer-history-position n))
1115 (minimum (if minibuffer-default -1 0))
1116 elt minibuffer-returned-to-present)
1117 (if (and (zerop minibuffer-history-position)
1118 (null minibuffer-text-before-history))
1119 (setq minibuffer-text-before-history
1120 (minibuffer-contents-no-properties)))
1121 (if (< narg minimum)
1122 (if minibuffer-default
1123 (error "End of history; no next item")
1124 (error "End of history; no default available")))
1125 (if (> narg (length (symbol-value minibuffer-history-variable)))
1126 (error "Beginning of history; no preceding item"))
1127 (unless (memq last-command '(next-history-element
1128 previous-history-element))
1129 (let ((prompt-end (minibuffer-prompt-end)))
1130 (set (make-local-variable 'minibuffer-temporary-goal-position)
1131 (cond ((<= (point) prompt-end) prompt-end)
1132 ((eobp) nil)
1133 (t (point))))))
1134 (goto-char (point-max))
1135 (delete-minibuffer-contents)
1136 (setq minibuffer-history-position narg)
1137 (cond ((= narg -1)
1138 (setq elt minibuffer-default))
1139 ((= narg 0)
1140 (setq elt (or minibuffer-text-before-history ""))
1141 (setq minibuffer-returned-to-present t)
1142 (setq minibuffer-text-before-history nil))
1143 (t (setq elt (nth (1- minibuffer-history-position)
1144 (symbol-value minibuffer-history-variable)))))
1145 (insert
1146 (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
1147 (not minibuffer-returned-to-present))
1148 (let ((print-level nil))
1149 (prin1-to-string elt))
1150 elt))
1151 (goto-char (or minibuffer-temporary-goal-position (point-max))))))
1153 (defun previous-history-element (n)
1154 "Inserts the previous element of the minibuffer history into the minibuffer."
1155 (interactive "p")
1156 (next-history-element (- n)))
1158 (defun next-complete-history-element (n)
1159 "Get next history element which completes the minibuffer before the point.
1160 The contents of the minibuffer after the point are deleted, and replaced
1161 by the new completion."
1162 (interactive "p")
1163 (let ((point-at-start (point)))
1164 (next-matching-history-element
1165 (concat
1166 "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
1168 ;; next-matching-history-element always puts us at (point-min).
1169 ;; Move to the position we were at before changing the buffer contents.
1170 ;; This is still sensical, because the text before point has not changed.
1171 (goto-char point-at-start)))
1173 (defun previous-complete-history-element (n)
1175 Get previous history element which completes the minibuffer before the point.
1176 The contents of the minibuffer after the point are deleted, and replaced
1177 by the new completion."
1178 (interactive "p")
1179 (next-complete-history-element (- n)))
1181 ;; For compatibility with the old subr of the same name.
1182 (defun minibuffer-prompt-width ()
1183 "Return the display width of the minibuffer prompt.
1184 Return 0 if current buffer is not a mini-buffer."
1185 ;; Return the width of everything before the field at the end of
1186 ;; the buffer; this should be 0 for normal buffers.
1187 (1- (minibuffer-prompt-end)))
1189 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
1190 (defalias 'advertised-undo 'undo)
1192 (defconst undo-equiv-table (make-hash-table :test 'eq :weakness t)
1193 "Table mapping redo records to the corresponding undo one.")
1195 (defvar undo-in-region nil
1196 "Non-nil if `pending-undo-list' is not just a tail of `buffer-undo-list'.")
1198 (defvar undo-no-redo nil
1199 "If t, `undo' doesn't go through redo entries.")
1201 (defun undo (&optional arg)
1202 "Undo some previous changes.
1203 Repeat this command to undo more changes.
1204 A numeric argument serves as a repeat count.
1206 In Transient Mark mode when the mark is active, only undo changes within
1207 the current region. Similarly, when not in Transient Mark mode, just \\[universal-argument]
1208 as an argument limits undo to changes within the current region."
1209 (interactive "*P")
1210 ;; Make last-command indicate for the next command that this was an undo.
1211 ;; That way, another undo will undo more.
1212 ;; If we get to the end of the undo history and get an error,
1213 ;; another undo command will find the undo history empty
1214 ;; and will get another error. To begin undoing the undos,
1215 ;; you must type some other command.
1216 (let ((modified (buffer-modified-p))
1217 (recent-save (recent-auto-save-p)))
1218 ;; If we get an error in undo-start,
1219 ;; the next command should not be a "consecutive undo".
1220 ;; So set `this-command' to something other than `undo'.
1221 (setq this-command 'undo-start)
1223 (unless (eq last-command 'undo)
1224 (setq undo-in-region
1225 (if transient-mark-mode mark-active (and arg (not (numberp arg)))))
1226 (if undo-in-region
1227 (undo-start (region-beginning) (region-end))
1228 (undo-start))
1229 ;; get rid of initial undo boundary
1230 (undo-more 1))
1231 ;; If we got this far, the next command should be a consecutive undo.
1232 (setq this-command 'undo)
1233 ;; Check to see whether we're hitting a redo record, and if
1234 ;; so, ask the user whether she wants to skip the redo/undo pair.
1235 (let ((equiv (gethash pending-undo-list undo-equiv-table)))
1236 (or (eq (selected-window) (minibuffer-window))
1237 (message (if undo-in-region
1238 (if equiv "Redo in region!" "Undo in region!")
1239 (if equiv "Redo!" "Undo!"))))
1240 (when (and equiv undo-no-redo)
1241 ;; The equiv entry might point to another redo record if we have done
1242 ;; undo-redo-undo-redo-... so skip to the very last equiv.
1243 (while (let ((next (gethash equiv undo-equiv-table)))
1244 (if next (setq equiv next))))
1245 (setq pending-undo-list equiv)))
1246 (undo-more
1247 (if (or transient-mark-mode (numberp arg))
1248 (prefix-numeric-value arg)
1250 ;; Record the fact that the just-generated undo records come from an
1251 ;; undo operation, so we can skip them later on.
1252 ;; I don't know how to do that in the undo-in-region case.
1253 (unless undo-in-region
1254 (puthash buffer-undo-list pending-undo-list undo-equiv-table))
1255 ;; Don't specify a position in the undo record for the undo command.
1256 ;; Instead, undoing this should move point to where the change is.
1257 (let ((tail buffer-undo-list)
1258 (prev nil))
1259 (while (car tail)
1260 (when (integerp (car tail))
1261 (let ((pos (car tail)))
1262 (if prev
1263 (setcdr prev (cdr tail))
1264 (setq buffer-undo-list (cdr tail)))
1265 (setq tail (cdr tail))
1266 (while (car tail)
1267 (if (eq pos (car tail))
1268 (if prev
1269 (setcdr prev (cdr tail))
1270 (setq buffer-undo-list (cdr tail)))
1271 (setq prev tail))
1272 (setq tail (cdr tail)))
1273 (setq tail nil)))
1274 (setq prev tail tail (cdr tail))))
1276 (and modified (not (buffer-modified-p))
1277 (delete-auto-save-file-if-necessary recent-save))))
1279 (defun undo-only (&optional arg)
1280 "Undo some previous changes.
1281 Repeat this command to undo more changes.
1282 A numeric argument serves as a repeat count.
1283 Contrary to `undo', this will not redo a previous undo."
1284 (interactive "*p")
1285 (let ((undo-no-redo t)) (undo arg)))
1286 ;; Richard said that we should not use C-x <uppercase letter> and I have
1287 ;; no idea whereas to bind it. Any suggestion welcome. -stef
1288 ;; (define-key ctl-x-map "U" 'undo-only)
1290 (defvar pending-undo-list nil
1291 "Within a run of consecutive undo commands, list remaining to be undone.")
1293 (defvar undo-in-progress nil
1294 "Non-nil while performing an undo.
1295 Some change-hooks test this variable to do something different.")
1297 (defun undo-more (count)
1298 "Undo back N undo-boundaries beyond what was already undone recently.
1299 Call `undo-start' to get ready to undo recent changes,
1300 then call `undo-more' one or more times to undo them."
1301 (or pending-undo-list
1302 (error (format "No further undo information%s"
1303 (if (and transient-mark-mode mark-active)
1304 " for region" ""))))
1305 (let ((undo-in-progress t))
1306 (setq pending-undo-list (primitive-undo count pending-undo-list))))
1308 ;; Deep copy of a list
1309 (defun undo-copy-list (list)
1310 "Make a copy of undo list LIST."
1311 (mapcar 'undo-copy-list-1 list))
1313 (defun undo-copy-list-1 (elt)
1314 (if (consp elt)
1315 (cons (car elt) (undo-copy-list-1 (cdr elt)))
1316 elt))
1318 (defun undo-start (&optional beg end)
1319 "Set `pending-undo-list' to the front of the undo list.
1320 The next call to `undo-more' will undo the most recently made change.
1321 If BEG and END are specified, then only undo elements
1322 that apply to text between BEG and END are used; other undo elements
1323 are ignored. If BEG and END are nil, all undo elements are used."
1324 (if (eq buffer-undo-list t)
1325 (error "No undo information in this buffer"))
1326 (setq pending-undo-list
1327 (if (and beg end (not (= beg end)))
1328 (undo-make-selective-list (min beg end) (max beg end))
1329 buffer-undo-list)))
1331 (defvar undo-adjusted-markers)
1333 (defun undo-make-selective-list (start end)
1334 "Return a list of undo elements for the region START to END.
1335 The elements come from `buffer-undo-list', but we keep only
1336 the elements inside this region, and discard those outside this region.
1337 If we find an element that crosses an edge of this region,
1338 we stop and ignore all further elements."
1339 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
1340 (undo-list (list nil))
1341 undo-adjusted-markers
1342 some-rejected
1343 undo-elt undo-elt temp-undo-list delta)
1344 (while undo-list-copy
1345 (setq undo-elt (car undo-list-copy))
1346 (let ((keep-this
1347 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
1348 ;; This is a "was unmodified" element.
1349 ;; Keep it if we have kept everything thus far.
1350 (not some-rejected))
1352 (undo-elt-in-region undo-elt start end)))))
1353 (if keep-this
1354 (progn
1355 (setq end (+ end (cdr (undo-delta undo-elt))))
1356 ;; Don't put two nils together in the list
1357 (if (not (and (eq (car undo-list) nil)
1358 (eq undo-elt nil)))
1359 (setq undo-list (cons undo-elt undo-list))))
1360 (if (undo-elt-crosses-region undo-elt start end)
1361 (setq undo-list-copy nil)
1362 (setq some-rejected t)
1363 (setq temp-undo-list (cdr undo-list-copy))
1364 (setq delta (undo-delta undo-elt))
1366 (when (/= (cdr delta) 0)
1367 (let ((position (car delta))
1368 (offset (cdr delta)))
1370 ;; Loop down the earlier events adjusting their buffer
1371 ;; positions to reflect the fact that a change to the buffer
1372 ;; isn't being undone. We only need to process those element
1373 ;; types which undo-elt-in-region will return as being in
1374 ;; the region since only those types can ever get into the
1375 ;; output
1377 (while temp-undo-list
1378 (setq undo-elt (car temp-undo-list))
1379 (cond ((integerp undo-elt)
1380 (if (>= undo-elt position)
1381 (setcar temp-undo-list (- undo-elt offset))))
1382 ((atom undo-elt) nil)
1383 ((stringp (car undo-elt))
1384 ;; (TEXT . POSITION)
1385 (let ((text-pos (abs (cdr undo-elt)))
1386 (point-at-end (< (cdr undo-elt) 0 )))
1387 (if (>= text-pos position)
1388 (setcdr undo-elt (* (if point-at-end -1 1)
1389 (- text-pos offset))))))
1390 ((integerp (car undo-elt))
1391 ;; (BEGIN . END)
1392 (when (>= (car undo-elt) position)
1393 (setcar undo-elt (- (car undo-elt) offset))
1394 (setcdr undo-elt (- (cdr undo-elt) offset))))
1395 ((null (car undo-elt))
1396 ;; (nil PROPERTY VALUE BEG . END)
1397 (let ((tail (nthcdr 3 undo-elt)))
1398 (when (>= (car tail) position)
1399 (setcar tail (- (car tail) offset))
1400 (setcdr tail (- (cdr tail) offset))))))
1401 (setq temp-undo-list (cdr temp-undo-list))))))))
1402 (setq undo-list-copy (cdr undo-list-copy)))
1403 (nreverse undo-list)))
1405 (defun undo-elt-in-region (undo-elt start end)
1406 "Determine whether UNDO-ELT falls inside the region START ... END.
1407 If it crosses the edge, we return nil."
1408 (cond ((integerp undo-elt)
1409 (and (>= undo-elt start)
1410 (<= undo-elt end)))
1411 ((eq undo-elt nil)
1413 ((atom undo-elt)
1414 nil)
1415 ((stringp (car undo-elt))
1416 ;; (TEXT . POSITION)
1417 (and (>= (abs (cdr undo-elt)) start)
1418 (< (abs (cdr undo-elt)) end)))
1419 ((and (consp undo-elt) (markerp (car undo-elt)))
1420 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1421 ;; See if MARKER is inside the region.
1422 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1423 (unless alist-elt
1424 (setq alist-elt (cons (car undo-elt)
1425 (marker-position (car undo-elt))))
1426 (setq undo-adjusted-markers
1427 (cons alist-elt undo-adjusted-markers)))
1428 (and (cdr alist-elt)
1429 (>= (cdr alist-elt) start)
1430 (<= (cdr alist-elt) end))))
1431 ((null (car undo-elt))
1432 ;; (nil PROPERTY VALUE BEG . END)
1433 (let ((tail (nthcdr 3 undo-elt)))
1434 (and (>= (car tail) start)
1435 (<= (cdr tail) end))))
1436 ((integerp (car undo-elt))
1437 ;; (BEGIN . END)
1438 (and (>= (car undo-elt) start)
1439 (<= (cdr undo-elt) end)))))
1441 (defun undo-elt-crosses-region (undo-elt start end)
1442 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1443 This assumes we have already decided that UNDO-ELT
1444 is not *inside* the region START...END."
1445 (cond ((atom undo-elt) nil)
1446 ((null (car undo-elt))
1447 ;; (nil PROPERTY VALUE BEG . END)
1448 (let ((tail (nthcdr 3 undo-elt)))
1449 (not (or (< (car tail) end)
1450 (> (cdr tail) start)))))
1451 ((integerp (car undo-elt))
1452 ;; (BEGIN . END)
1453 (not (or (< (car undo-elt) end)
1454 (> (cdr undo-elt) start))))))
1456 ;; Return the first affected buffer position and the delta for an undo element
1457 ;; delta is defined as the change in subsequent buffer positions if we *did*
1458 ;; the undo.
1459 (defun undo-delta (undo-elt)
1460 (if (consp undo-elt)
1461 (cond ((stringp (car undo-elt))
1462 ;; (TEXT . POSITION)
1463 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1464 ((integerp (car undo-elt))
1465 ;; (BEGIN . END)
1466 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1468 '(0 . 0)))
1469 '(0 . 0)))
1471 (defvar shell-command-history nil
1472 "History list for some commands that read shell commands.")
1474 (defvar shell-command-switch "-c"
1475 "Switch used to have the shell execute its command line argument.")
1477 (defvar shell-command-default-error-buffer nil
1478 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
1479 This buffer is used when `shell-command' or `shell-command-on-region'
1480 is run interactively. A value of nil means that output to stderr and
1481 stdout will be intermixed in the output stream.")
1483 (defun shell-command (command &optional output-buffer error-buffer)
1484 "Execute string COMMAND in inferior shell; display output, if any.
1485 With prefix argument, insert the COMMAND's output at point.
1487 If COMMAND ends in ampersand, execute it asynchronously.
1488 The output appears in the buffer `*Async Shell Command*'.
1489 That buffer is in shell mode.
1491 Otherwise, COMMAND is executed synchronously. The output appears in
1492 the buffer `*Shell Command Output*'. If the output is short enough to
1493 display in the echo area (which is determined by the variables
1494 `resize-mini-windows' and `max-mini-window-height'), it is shown
1495 there, but it is nonetheless available in buffer `*Shell Command
1496 Output*' even though that buffer is not automatically displayed.
1498 To specify a coding system for converting non-ASCII characters
1499 in the shell command output, use \\[universal-coding-system-argument]
1500 before this command.
1502 Noninteractive callers can specify coding systems by binding
1503 `coding-system-for-read' and `coding-system-for-write'.
1505 The optional second argument OUTPUT-BUFFER, if non-nil,
1506 says to put the output in some other buffer.
1507 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1508 If OUTPUT-BUFFER is not a buffer and not nil,
1509 insert output in current buffer. (This cannot be done asynchronously.)
1510 In either case, the output is inserted after point (leaving mark after it).
1512 If the command terminates without error, but generates output,
1513 and you did not specify \"insert it in the current buffer\",
1514 the output can be displayed in the echo area or in its buffer.
1515 If the output is short enough to display in the echo area
1516 \(determined by the variable `max-mini-window-height' if
1517 `resize-mini-windows' is non-nil), it is shown there. Otherwise,
1518 the buffer containing the output is displayed.
1520 If there is output and an error, and you did not specify \"insert it
1521 in the current buffer\", a message about the error goes at the end
1522 of the output.
1524 If there is no output, or if output is inserted in the current buffer,
1525 then `*Shell Command Output*' is deleted.
1527 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
1528 or buffer name to which to direct the command's standard error output.
1529 If it is nil, error output is mingled with regular output.
1530 In an interactive call, the variable `shell-command-default-error-buffer'
1531 specifies the value of ERROR-BUFFER."
1533 (interactive (list (read-from-minibuffer "Shell command: "
1534 nil nil nil 'shell-command-history)
1535 current-prefix-arg
1536 shell-command-default-error-buffer))
1537 ;; Look for a handler in case default-directory is a remote file name.
1538 (let ((handler
1539 (find-file-name-handler (directory-file-name default-directory)
1540 'shell-command)))
1541 (if handler
1542 (funcall handler 'shell-command command output-buffer error-buffer)
1543 (if (and output-buffer
1544 (not (or (bufferp output-buffer) (stringp output-buffer))))
1545 ;; Output goes in current buffer.
1546 (let ((error-file
1547 (if error-buffer
1548 (make-temp-file
1549 (expand-file-name "scor"
1550 (or small-temporary-file-directory
1551 temporary-file-directory)))
1552 nil)))
1553 (barf-if-buffer-read-only)
1554 (push-mark nil t)
1555 ;; We do not use -f for csh; we will not support broken use of
1556 ;; .cshrcs. Even the BSD csh manual says to use
1557 ;; "if ($?prompt) exit" before things which are not useful
1558 ;; non-interactively. Besides, if someone wants their other
1559 ;; aliases for shell commands then they can still have them.
1560 (call-process shell-file-name nil
1561 (if error-file
1562 (list t error-file)
1564 nil shell-command-switch command)
1565 (when (and error-file (file-exists-p error-file))
1566 (if (< 0 (nth 7 (file-attributes error-file)))
1567 (with-current-buffer (get-buffer-create error-buffer)
1568 (let ((pos-from-end (- (point-max) (point))))
1569 (or (bobp)
1570 (insert "\f\n"))
1571 ;; Do no formatting while reading error file,
1572 ;; because that can run a shell command, and we
1573 ;; don't want that to cause an infinite recursion.
1574 (format-insert-file error-file nil)
1575 ;; Put point after the inserted errors.
1576 (goto-char (- (point-max) pos-from-end)))
1577 (display-buffer (current-buffer))))
1578 (delete-file error-file))
1579 ;; This is like exchange-point-and-mark, but doesn't
1580 ;; activate the mark. It is cleaner to avoid activation,
1581 ;; even though the command loop would deactivate the mark
1582 ;; because we inserted text.
1583 (goto-char (prog1 (mark t)
1584 (set-marker (mark-marker) (point)
1585 (current-buffer)))))
1586 ;; Output goes in a separate buffer.
1587 ;; Preserve the match data in case called from a program.
1588 (save-match-data
1589 (if (string-match "[ \t]*&[ \t]*\\'" command)
1590 ;; Command ending with ampersand means asynchronous.
1591 (let ((buffer (get-buffer-create
1592 (or output-buffer "*Async Shell Command*")))
1593 (directory default-directory)
1594 proc)
1595 ;; Remove the ampersand.
1596 (setq command (substring command 0 (match-beginning 0)))
1597 ;; If will kill a process, query first.
1598 (setq proc (get-buffer-process buffer))
1599 (if proc
1600 (if (yes-or-no-p "A command is running. Kill it? ")
1601 (kill-process proc)
1602 (error "Shell command in progress")))
1603 (with-current-buffer buffer
1604 (setq buffer-read-only nil)
1605 (erase-buffer)
1606 (display-buffer buffer)
1607 (setq default-directory directory)
1608 (setq proc (start-process "Shell" buffer shell-file-name
1609 shell-command-switch command))
1610 (setq mode-line-process '(":%s"))
1611 (require 'shell) (shell-mode)
1612 (set-process-sentinel proc 'shell-command-sentinel)
1614 (shell-command-on-region (point) (point) command
1615 output-buffer nil error-buffer)))))))
1617 (defun display-message-or-buffer (message
1618 &optional buffer-name not-this-window frame)
1619 "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
1620 MESSAGE may be either a string or a buffer.
1622 A buffer is displayed using `display-buffer' if MESSAGE is too long for
1623 the maximum height of the echo area, as defined by `max-mini-window-height'
1624 if `resize-mini-windows' is non-nil.
1626 Returns either the string shown in the echo area, or when a pop-up
1627 buffer is used, the window used to display it.
1629 If MESSAGE is a string, then the optional argument BUFFER-NAME is the
1630 name of the buffer used to display it in the case where a pop-up buffer
1631 is used, defaulting to `*Message*'. In the case where MESSAGE is a
1632 string and it is displayed in the echo area, it is not specified whether
1633 the contents are inserted into the buffer anyway.
1635 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
1636 and only used if a buffer is displayed."
1637 (cond ((and (stringp message) (not (string-match "\n" message)))
1638 ;; Trivial case where we can use the echo area
1639 (message "%s" message))
1640 ((and (stringp message)
1641 (= (string-match "\n" message) (1- (length message))))
1642 ;; Trivial case where we can just remove single trailing newline
1643 (message "%s" (substring message 0 (1- (length message)))))
1645 ;; General case
1646 (with-current-buffer
1647 (if (bufferp message)
1648 message
1649 (get-buffer-create (or buffer-name "*Message*")))
1651 (unless (bufferp message)
1652 (erase-buffer)
1653 (insert message))
1655 (let ((lines
1656 (if (= (buffer-size) 0)
1658 (count-lines (point-min) (point-max)))))
1659 (cond ((= lines 0))
1660 ((and (or (<= lines 1)
1661 (<= lines
1662 (if resize-mini-windows
1663 (cond ((floatp max-mini-window-height)
1664 (* (frame-height)
1665 max-mini-window-height))
1666 ((integerp max-mini-window-height)
1667 max-mini-window-height)
1670 1)))
1671 ;; Don't use the echo area if the output buffer is
1672 ;; already dispayed in the selected frame.
1673 (not (get-buffer-window (current-buffer))))
1674 ;; Echo area
1675 (goto-char (point-max))
1676 (when (bolp)
1677 (backward-char 1))
1678 (message "%s" (buffer-substring (point-min) (point))))
1680 ;; Buffer
1681 (goto-char (point-min))
1682 (display-buffer (current-buffer)
1683 not-this-window frame))))))))
1686 ;; We have a sentinel to prevent insertion of a termination message
1687 ;; in the buffer itself.
1688 (defun shell-command-sentinel (process signal)
1689 (if (memq (process-status process) '(exit signal))
1690 (message "%s: %s."
1691 (car (cdr (cdr (process-command process))))
1692 (substring signal 0 -1))))
1694 (defun shell-command-on-region (start end command
1695 &optional output-buffer replace
1696 error-buffer display-error-buffer)
1697 "Execute string COMMAND in inferior shell with region as input.
1698 Normally display output (if any) in temp buffer `*Shell Command Output*';
1699 Prefix arg means replace the region with it. Return the exit code of
1700 COMMAND.
1702 To specify a coding system for converting non-ASCII characters
1703 in the input and output to the shell command, use \\[universal-coding-system-argument]
1704 before this command. By default, the input (from the current buffer)
1705 is encoded in the same coding system that will be used to save the file,
1706 `buffer-file-coding-system'. If the output is going to replace the region,
1707 then it is decoded from that same coding system.
1709 The noninteractive arguments are START, END, COMMAND,
1710 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
1711 Noninteractive callers can specify coding systems by binding
1712 `coding-system-for-read' and `coding-system-for-write'.
1714 If the command generates output, the output may be displayed
1715 in the echo area or in a buffer.
1716 If the output is short enough to display in the echo area
1717 \(determined by the variable `max-mini-window-height' if
1718 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1719 it is displayed in the buffer `*Shell Command Output*'. The output
1720 is available in that buffer in both cases.
1722 If there is output and an error, a message about the error
1723 appears at the end of the output.
1725 If there is no output, or if output is inserted in the current buffer,
1726 then `*Shell Command Output*' is deleted.
1728 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1729 that 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 the current buffer.
1733 In either case, the output is inserted after point (leaving mark after it).
1735 If REPLACE, the optional fifth argument, is non-nil, that means insert
1736 the output in place of text from START to END, putting point and mark
1737 around it.
1739 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1740 or buffer name to which to direct the command's standard error output.
1741 If it is nil, error output is mingled with regular output.
1742 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
1743 were any errors. (This is always t, interactively.)
1744 In an interactive call, the variable `shell-command-default-error-buffer'
1745 specifies the value of ERROR-BUFFER."
1746 (interactive (let (string)
1747 (unless (mark)
1748 (error "The mark is not set now, so there is no region"))
1749 ;; Do this before calling region-beginning
1750 ;; and region-end, in case subprocess output
1751 ;; relocates them while we are in the minibuffer.
1752 (setq string (read-from-minibuffer "Shell command on region: "
1753 nil nil nil
1754 'shell-command-history))
1755 ;; call-interactively recognizes region-beginning and
1756 ;; region-end specially, leaving them in the history.
1757 (list (region-beginning) (region-end)
1758 string
1759 current-prefix-arg
1760 current-prefix-arg
1761 shell-command-default-error-buffer
1762 t)))
1763 (let ((error-file
1764 (if error-buffer
1765 (make-temp-file
1766 (expand-file-name "scor"
1767 (or small-temporary-file-directory
1768 temporary-file-directory)))
1769 nil))
1770 exit-status)
1771 (if (or replace
1772 (and output-buffer
1773 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1774 ;; Replace specified region with output from command.
1775 (let ((swap (and replace (< start end))))
1776 ;; Don't muck with mark unless REPLACE says we should.
1777 (goto-char start)
1778 (and replace (push-mark (point) 'nomsg))
1779 (setq exit-status
1780 (call-process-region start end shell-file-name t
1781 (if error-file
1782 (list t error-file)
1784 nil shell-command-switch command))
1785 ;; It is rude to delete a buffer which the command is not using.
1786 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1787 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1788 ;; (kill-buffer shell-buffer)))
1789 ;; Don't muck with mark unless REPLACE says we should.
1790 (and replace swap (exchange-point-and-mark)))
1791 ;; No prefix argument: put the output in a temp buffer,
1792 ;; replacing its entire contents.
1793 (let ((buffer (get-buffer-create
1794 (or output-buffer "*Shell Command Output*"))))
1795 (unwind-protect
1796 (if (eq buffer (current-buffer))
1797 ;; If the input is the same buffer as the output,
1798 ;; delete everything but the specified region,
1799 ;; then replace that region with the output.
1800 (progn (setq buffer-read-only nil)
1801 (delete-region (max start end) (point-max))
1802 (delete-region (point-min) (min start end))
1803 (setq exit-status
1804 (call-process-region (point-min) (point-max)
1805 shell-file-name t
1806 (if error-file
1807 (list t error-file)
1809 nil shell-command-switch
1810 command)))
1811 ;; Clear the output buffer, then run the command with
1812 ;; output there.
1813 (let ((directory default-directory))
1814 (save-excursion
1815 (set-buffer buffer)
1816 (setq buffer-read-only nil)
1817 (if (not output-buffer)
1818 (setq default-directory directory))
1819 (erase-buffer)))
1820 (setq exit-status
1821 (call-process-region start end shell-file-name nil
1822 (if error-file
1823 (list buffer error-file)
1824 buffer)
1825 nil shell-command-switch command)))
1826 ;; Report the output.
1827 (with-current-buffer buffer
1828 (setq mode-line-process
1829 (cond ((null exit-status)
1830 " - Error")
1831 ((stringp exit-status)
1832 (format " - Signal [%s]" exit-status))
1833 ((not (equal 0 exit-status))
1834 (format " - Exit [%d]" exit-status)))))
1835 (if (with-current-buffer buffer (> (point-max) (point-min)))
1836 ;; There's some output, display it
1837 (display-message-or-buffer buffer)
1838 ;; No output; error?
1839 (let ((output
1840 (if (and error-file
1841 (< 0 (nth 7 (file-attributes error-file))))
1842 "some error output"
1843 "no output")))
1844 (cond ((null exit-status)
1845 (message "(Shell command failed with error)"))
1846 ((equal 0 exit-status)
1847 (message "(Shell command succeeded with %s)"
1848 output))
1849 ((stringp exit-status)
1850 (message "(Shell command killed by signal %s)"
1851 exit-status))
1853 (message "(Shell command failed with code %d and %s)"
1854 exit-status output))))
1855 ;; Don't kill: there might be useful info in the undo-log.
1856 ;; (kill-buffer buffer)
1857 ))))
1859 (when (and error-file (file-exists-p error-file))
1860 (if (< 0 (nth 7 (file-attributes error-file)))
1861 (with-current-buffer (get-buffer-create error-buffer)
1862 (let ((pos-from-end (- (point-max) (point))))
1863 (or (bobp)
1864 (insert "\f\n"))
1865 ;; Do no formatting while reading error file,
1866 ;; because that can run a shell command, and we
1867 ;; don't want that to cause an infinite recursion.
1868 (format-insert-file error-file nil)
1869 ;; Put point after the inserted errors.
1870 (goto-char (- (point-max) pos-from-end)))
1871 (and display-error-buffer
1872 (display-buffer (current-buffer)))))
1873 (delete-file error-file))
1874 exit-status))
1876 (defun shell-command-to-string (command)
1877 "Execute shell command COMMAND and return its output as a string."
1878 (with-output-to-string
1879 (with-current-buffer
1880 standard-output
1881 (call-process shell-file-name nil t nil shell-command-switch command))))
1883 (defun process-file (program &optional infile buffer display &rest args)
1884 "Process files synchronously in a separate process.
1885 Similar to `call-process', but may invoke a file handler based on
1886 `default-directory'. The current working directory of the
1887 subprocess is `default-directory'.
1889 File names in INFILE and BUFFER are handled normally, but file
1890 names in ARGS should be relative to `default-directory', as they
1891 are passed to the process verbatim. \(This is a difference to
1892 `call-process' which does not support file handlers for INFILE
1893 and BUFFER.\)
1895 Some file handlers might not support all variants, for example
1896 they might behave as if DISPLAY was nil, regardless of the actual
1897 value passed."
1898 (let ((fh (find-file-name-handler default-directory 'process-file))
1899 lc stderr-file)
1900 (unwind-protect
1901 (if fh (apply fh 'process-file program infile buffer display args)
1902 (when infile (setq lc (file-local-copy infile)))
1903 (setq stderr-file (when (and (consp buffer) (stringp (cadr buffer)))
1904 (make-temp-file "emacs")))
1905 (prog1
1906 (apply 'call-process program
1907 (or lc infile)
1908 (if stderr-file (list (car buffer) stderr-file) buffer)
1909 display args)
1910 (when stderr-file (copy-file stderr-file (cadr buffer)))))
1911 (when stderr-file (delete-file stderr-file))
1912 (when lc (delete-file lc)))))
1916 (defvar universal-argument-map
1917 (let ((map (make-sparse-keymap)))
1918 (define-key map [t] 'universal-argument-other-key)
1919 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1920 (define-key map [switch-frame] nil)
1921 (define-key map [?\C-u] 'universal-argument-more)
1922 (define-key map [?-] 'universal-argument-minus)
1923 (define-key map [?0] 'digit-argument)
1924 (define-key map [?1] 'digit-argument)
1925 (define-key map [?2] 'digit-argument)
1926 (define-key map [?3] 'digit-argument)
1927 (define-key map [?4] 'digit-argument)
1928 (define-key map [?5] 'digit-argument)
1929 (define-key map [?6] 'digit-argument)
1930 (define-key map [?7] 'digit-argument)
1931 (define-key map [?8] 'digit-argument)
1932 (define-key map [?9] 'digit-argument)
1933 (define-key map [kp-0] 'digit-argument)
1934 (define-key map [kp-1] 'digit-argument)
1935 (define-key map [kp-2] 'digit-argument)
1936 (define-key map [kp-3] 'digit-argument)
1937 (define-key map [kp-4] 'digit-argument)
1938 (define-key map [kp-5] 'digit-argument)
1939 (define-key map [kp-6] 'digit-argument)
1940 (define-key map [kp-7] 'digit-argument)
1941 (define-key map [kp-8] 'digit-argument)
1942 (define-key map [kp-9] 'digit-argument)
1943 (define-key map [kp-subtract] 'universal-argument-minus)
1944 map)
1945 "Keymap used while processing \\[universal-argument].")
1947 (defvar universal-argument-num-events nil
1948 "Number of argument-specifying events read by `universal-argument'.
1949 `universal-argument-other-key' uses this to discard those events
1950 from (this-command-keys), and reread only the final command.")
1952 (defvar overriding-map-is-bound nil
1953 "Non-nil when `overriding-terminal-local-map' is `universal-argument-map'.")
1955 (defvar saved-overriding-map nil
1956 "The saved value of `overriding-terminal-local-map'.
1957 That variable gets restored to this value on exiting \"universal
1958 argument mode\".")
1960 (defun ensure-overriding-map-is-bound ()
1961 "Check `overriding-terminal-local-map' is `universal-argument-map'."
1962 (unless overriding-map-is-bound
1963 (setq saved-overriding-map overriding-terminal-local-map)
1964 (setq overriding-terminal-local-map universal-argument-map)
1965 (setq overriding-map-is-bound t)))
1967 (defun restore-overriding-map ()
1968 "Restore `overriding-terminal-local-map' to its saved value."
1969 (setq overriding-terminal-local-map saved-overriding-map)
1970 (setq overriding-map-is-bound nil))
1972 (defun universal-argument ()
1973 "Begin a numeric argument for the following command.
1974 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1975 \\[universal-argument] following the digits or minus sign ends the argument.
1976 \\[universal-argument] without digits or minus sign provides 4 as argument.
1977 Repeating \\[universal-argument] without digits or minus sign
1978 multiplies the argument by 4 each time.
1979 For some commands, just \\[universal-argument] by itself serves as a flag
1980 which is different in effect from any particular numeric argument.
1981 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1982 (interactive)
1983 (setq prefix-arg (list 4))
1984 (setq universal-argument-num-events (length (this-command-keys)))
1985 (ensure-overriding-map-is-bound))
1987 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1988 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1989 (defun universal-argument-more (arg)
1990 (interactive "P")
1991 (if (consp arg)
1992 (setq prefix-arg (list (* 4 (car arg))))
1993 (if (eq arg '-)
1994 (setq prefix-arg (list -4))
1995 (setq prefix-arg arg)
1996 (restore-overriding-map)))
1997 (setq universal-argument-num-events (length (this-command-keys))))
1999 (defun negative-argument (arg)
2000 "Begin a negative numeric argument for the next command.
2001 \\[universal-argument] following digits or minus sign ends the argument."
2002 (interactive "P")
2003 (cond ((integerp arg)
2004 (setq prefix-arg (- arg)))
2005 ((eq arg '-)
2006 (setq prefix-arg nil))
2008 (setq prefix-arg '-)))
2009 (setq universal-argument-num-events (length (this-command-keys)))
2010 (ensure-overriding-map-is-bound))
2012 (defun digit-argument (arg)
2013 "Part of the numeric argument for the next command.
2014 \\[universal-argument] following digits or minus sign ends the argument."
2015 (interactive "P")
2016 (let* ((char (if (integerp last-command-char)
2017 last-command-char
2018 (get last-command-char 'ascii-character)))
2019 (digit (- (logand char ?\177) ?0)))
2020 (cond ((integerp arg)
2021 (setq prefix-arg (+ (* arg 10)
2022 (if (< arg 0) (- digit) digit))))
2023 ((eq arg '-)
2024 ;; Treat -0 as just -, so that -01 will work.
2025 (setq prefix-arg (if (zerop digit) '- (- digit))))
2027 (setq prefix-arg digit))))
2028 (setq universal-argument-num-events (length (this-command-keys)))
2029 (ensure-overriding-map-is-bound))
2031 ;; For backward compatibility, minus with no modifiers is an ordinary
2032 ;; command if digits have already been entered.
2033 (defun universal-argument-minus (arg)
2034 (interactive "P")
2035 (if (integerp arg)
2036 (universal-argument-other-key arg)
2037 (negative-argument arg)))
2039 ;; Anything else terminates the argument and is left in the queue to be
2040 ;; executed as a command.
2041 (defun universal-argument-other-key (arg)
2042 (interactive "P")
2043 (setq prefix-arg arg)
2044 (let* ((key (this-command-keys))
2045 (keylist (listify-key-sequence key)))
2046 (setq unread-command-events
2047 (append (nthcdr universal-argument-num-events keylist)
2048 unread-command-events)))
2049 (reset-this-command-lengths)
2050 (restore-overriding-map))
2052 ;;;; Window system cut and paste hooks.
2054 (defvar interprogram-cut-function nil
2055 "Function to call to make a killed region available to other programs.
2057 Most window systems provide some sort of facility for cutting and
2058 pasting text between the windows of different programs.
2059 This variable holds a function that Emacs calls whenever text
2060 is put in the kill ring, to make the new kill available to other
2061 programs.
2063 The function takes one or two arguments.
2064 The first argument, TEXT, is a string containing
2065 the text which should be made available.
2066 The second, optional, argument PUSH, has the same meaning as the
2067 similar argument to `x-set-cut-buffer', which see.")
2069 (defvar interprogram-paste-function nil
2070 "Function to call to get text cut from other programs.
2072 Most window systems provide some sort of facility for cutting and
2073 pasting text between the windows of different programs.
2074 This variable holds a function that Emacs calls to obtain
2075 text that other programs have provided for pasting.
2077 The function should be called with no arguments. If the function
2078 returns nil, then no other program has provided such text, and the top
2079 of the Emacs kill ring should be used. If the function returns a
2080 string, then the caller of the function \(usually `current-kill')
2081 should put this string in the kill ring as the latest kill.
2083 Note that the function should return a string only if a program other
2084 than Emacs has provided a string for pasting; if Emacs provided the
2085 most recent string, the function should return nil. If it is
2086 difficult to tell whether Emacs or some other program provided the
2087 current string, it is probably good enough to return nil if the string
2088 is equal (according to `string=') to the last text Emacs provided.")
2092 ;;;; The kill ring data structure.
2094 (defvar kill-ring nil
2095 "List of killed text sequences.
2096 Since the kill ring is supposed to interact nicely with cut-and-paste
2097 facilities offered by window systems, use of this variable should
2098 interact nicely with `interprogram-cut-function' and
2099 `interprogram-paste-function'. The functions `kill-new',
2100 `kill-append', and `current-kill' are supposed to implement this
2101 interaction; you may want to use them instead of manipulating the kill
2102 ring directly.")
2104 (defcustom kill-ring-max 60
2105 "*Maximum length of kill ring before oldest elements are thrown away."
2106 :type 'integer
2107 :group 'killing)
2109 (defvar kill-ring-yank-pointer nil
2110 "The tail of the kill ring whose car is the last thing yanked.")
2112 (defun kill-new (string &optional replace yank-handler)
2113 "Make STRING the latest kill in the kill ring.
2114 Set `kill-ring-yank-pointer' to point to it.
2115 If `interprogram-cut-function' is non-nil, apply it to STRING.
2116 Optional second argument REPLACE non-nil means that STRING will replace
2117 the front of the kill ring, rather than being added to the list.
2119 Optional third arguments YANK-HANDLER controls how the STRING is later
2120 inserted into a buffer; see `insert-for-yank' for details.
2121 When a yank handler is specified, STRING must be non-empty (the yank
2122 handler, if non-nil, is stored as a `yank-handler' text property on STRING).
2124 When the yank handler has a non-nil PARAM element, the original STRING
2125 argument is not used by `insert-for-yank'. However, since Lisp code
2126 may access and use elements from the kill-ring directly, the STRING
2127 argument should still be a \"useful\" string for such uses."
2128 (if (> (length string) 0)
2129 (if yank-handler
2130 (put-text-property 0 (length string)
2131 'yank-handler yank-handler string))
2132 (if yank-handler
2133 (signal 'args-out-of-range
2134 (list string "yank-handler specified for empty string"))))
2135 (if (fboundp 'menu-bar-update-yank-menu)
2136 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
2137 (if (and replace kill-ring)
2138 (setcar kill-ring string)
2139 (setq kill-ring (cons string kill-ring))
2140 (if (> (length kill-ring) kill-ring-max)
2141 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
2142 (setq kill-ring-yank-pointer kill-ring)
2143 (if interprogram-cut-function
2144 (funcall interprogram-cut-function string (not replace))))
2146 (defun kill-append (string before-p &optional yank-handler)
2147 "Append STRING to the end of the latest kill in the kill ring.
2148 If BEFORE-P is non-nil, prepend STRING to the kill.
2149 Optional third argument YANK-HANDLER, if non-nil, specifies the
2150 yank-handler text property to be set on the combined kill ring
2151 string. If the specified yank-handler arg differs from the
2152 yank-handler property of the latest kill string, this function
2153 adds the combined string to the kill ring as a new element,
2154 instead of replacing the last kill with it.
2155 If `interprogram-cut-function' is set, pass the resulting kill to it."
2156 (let* ((cur (car kill-ring)))
2157 (kill-new (if before-p (concat string cur) (concat cur string))
2158 (or (= (length cur) 0)
2159 (equal yank-handler (get-text-property 0 'yank-handler cur)))
2160 yank-handler)))
2162 (defun current-kill (n &optional do-not-move)
2163 "Rotate the yanking point by N places, and then return that kill.
2164 If N is zero, `interprogram-paste-function' is set, and calling it
2165 returns a string, then that string is added to the front of the
2166 kill ring and returned as the latest kill.
2167 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
2168 yanking point; just return the Nth kill forward."
2169 (let ((interprogram-paste (and (= n 0)
2170 interprogram-paste-function
2171 (funcall interprogram-paste-function))))
2172 (if interprogram-paste
2173 (progn
2174 ;; Disable the interprogram cut function when we add the new
2175 ;; text to the kill ring, so Emacs doesn't try to own the
2176 ;; selection, with identical text.
2177 (let ((interprogram-cut-function nil))
2178 (kill-new interprogram-paste))
2179 interprogram-paste)
2180 (or kill-ring (error "Kill ring is empty"))
2181 (let ((ARGth-kill-element
2182 (nthcdr (mod (- n (length kill-ring-yank-pointer))
2183 (length kill-ring))
2184 kill-ring)))
2185 (or do-not-move
2186 (setq kill-ring-yank-pointer ARGth-kill-element))
2187 (car ARGth-kill-element)))))
2191 ;;;; Commands for manipulating the kill ring.
2193 (defcustom kill-read-only-ok nil
2194 "*Non-nil means don't signal an error for killing read-only text."
2195 :type 'boolean
2196 :group 'killing)
2198 (put 'text-read-only 'error-conditions
2199 '(text-read-only buffer-read-only error))
2200 (put 'text-read-only 'error-message "Text is read-only")
2202 (defun kill-region (beg end &optional yank-handler)
2203 "Kill between point and mark.
2204 The text is deleted but saved in the kill ring.
2205 The command \\[yank] can retrieve it from there.
2206 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
2208 If you want to append the killed region to the last killed text,
2209 use \\[append-next-kill] before \\[kill-region].
2211 If the buffer is read-only, Emacs will beep and refrain from deleting
2212 the text, but put the text in the kill ring anyway. This means that
2213 you can use the killing commands to copy text from a read-only buffer.
2215 This is the primitive for programs to kill text (as opposed to deleting it).
2216 Supply two arguments, character positions indicating the stretch of text
2217 to be killed.
2218 Any command that calls this function is a \"kill command\".
2219 If the previous command was also a kill command,
2220 the text killed this time appends to the text killed last time
2221 to make one entry in the kill ring.
2223 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
2224 specifies the yank-handler text property to be set on the killed
2225 text. See `insert-for-yank'."
2226 (interactive "r")
2227 (condition-case nil
2228 (let ((string (delete-and-extract-region beg end)))
2229 (when string ;STRING is nil if BEG = END
2230 ;; Add that string to the kill ring, one way or another.
2231 (if (eq last-command 'kill-region)
2232 (kill-append string (< end beg) yank-handler)
2233 (kill-new string nil yank-handler)))
2234 (when (or string (eq last-command 'kill-region))
2235 (setq this-command 'kill-region))
2236 nil)
2237 ((buffer-read-only text-read-only)
2238 ;; The code above failed because the buffer, or some of the characters
2239 ;; in the region, are read-only.
2240 ;; We should beep, in case the user just isn't aware of this.
2241 ;; However, there's no harm in putting
2242 ;; the region's text in the kill ring, anyway.
2243 (copy-region-as-kill beg end)
2244 ;; Set this-command now, so it will be set even if we get an error.
2245 (setq this-command 'kill-region)
2246 ;; This should barf, if appropriate, and give us the correct error.
2247 (if kill-read-only-ok
2248 (progn (message "Read only text copied to kill ring") nil)
2249 ;; Signal an error if the buffer is read-only.
2250 (barf-if-buffer-read-only)
2251 ;; If the buffer isn't read-only, the text is.
2252 (signal 'text-read-only (list (current-buffer)))))))
2254 ;; copy-region-as-kill no longer sets this-command, because it's confusing
2255 ;; to get two copies of the text when the user accidentally types M-w and
2256 ;; then corrects it with the intended C-w.
2257 (defun copy-region-as-kill (beg end)
2258 "Save the region as if killed, but don't kill it.
2259 In Transient Mark mode, deactivate the mark.
2260 If `interprogram-cut-function' is non-nil, also save the text for a window
2261 system cut and paste."
2262 (interactive "r")
2263 (if (eq last-command 'kill-region)
2264 (kill-append (buffer-substring beg end) (< end beg))
2265 (kill-new (buffer-substring beg end)))
2266 (if transient-mark-mode
2267 (setq deactivate-mark t))
2268 nil)
2270 (defun kill-ring-save (beg end)
2271 "Save the region as if killed, but don't kill it.
2272 In Transient Mark mode, deactivate the mark.
2273 If `interprogram-cut-function' is non-nil, also save the text for a window
2274 system cut and paste.
2276 If you want to append the killed line to the last killed text,
2277 use \\[append-next-kill] before \\[kill-ring-save].
2279 This command is similar to `copy-region-as-kill', except that it gives
2280 visual feedback indicating the extent of the region being copied."
2281 (interactive "r")
2282 (copy-region-as-kill beg end)
2283 (if (interactive-p)
2284 (let ((other-end (if (= (point) beg) end beg))
2285 (opoint (point))
2286 ;; Inhibit quitting so we can make a quit here
2287 ;; look like a C-g typed as a command.
2288 (inhibit-quit t))
2289 (if (pos-visible-in-window-p other-end (selected-window))
2290 (unless (and transient-mark-mode
2291 (face-background 'region))
2292 ;; Swap point and mark.
2293 (set-marker (mark-marker) (point) (current-buffer))
2294 (goto-char other-end)
2295 (sit-for blink-matching-delay)
2296 ;; Swap back.
2297 (set-marker (mark-marker) other-end (current-buffer))
2298 (goto-char opoint)
2299 ;; If user quit, deactivate the mark
2300 ;; as C-g would as a command.
2301 (and quit-flag mark-active
2302 (deactivate-mark)))
2303 (let* ((killed-text (current-kill 0))
2304 (message-len (min (length killed-text) 40)))
2305 (if (= (point) beg)
2306 ;; Don't say "killed"; that is misleading.
2307 (message "Saved text until \"%s\""
2308 (substring killed-text (- message-len)))
2309 (message "Saved text from \"%s\""
2310 (substring killed-text 0 message-len))))))))
2312 (defun append-next-kill (&optional interactive)
2313 "Cause following command, if it kills, to append to previous kill.
2314 The argument is used for internal purposes; do not supply one."
2315 (interactive "p")
2316 ;; We don't use (interactive-p), since that breaks kbd macros.
2317 (if interactive
2318 (progn
2319 (setq this-command 'kill-region)
2320 (message "If the next command is a kill, it will append"))
2321 (setq last-command 'kill-region)))
2323 ;; Yanking.
2325 ;; This is actually used in subr.el but defcustom does not work there.
2326 (defcustom yank-excluded-properties
2327 '(read-only invisible intangible field mouse-face help-echo local-map keymap
2328 yank-handler)
2329 "*Text properties to discard when yanking.
2330 The value should be a list of text properties to discard or t,
2331 which means to discard all text properties."
2332 :type '(choice (const :tag "All" t) (repeat symbol))
2333 :group 'killing
2334 :version "21.4")
2336 (defvar yank-window-start nil)
2337 (defvar yank-undo-function nil
2338 "If non-nil, function used by `yank-pop' to delete last stretch of yanked text.
2339 Function is called with two parameters, START and END corresponding to
2340 the value of the mark and point; it is guaranteed that START <= END.
2341 Normally set from the UNDO element of a yank-handler; see `insert-for-yank'.")
2343 (defun yank-pop (&optional arg)
2344 "Replace just-yanked stretch of killed text with a different stretch.
2345 This command is allowed only immediately after a `yank' or a `yank-pop'.
2346 At such a time, the region contains a stretch of reinserted
2347 previously-killed text. `yank-pop' deletes that text and inserts in its
2348 place a different stretch of killed text.
2350 With no argument, the previous kill is inserted.
2351 With argument N, insert the Nth previous kill.
2352 If N is negative, this is a more recent kill.
2354 The sequence of kills wraps around, so that after the oldest one
2355 comes the newest one."
2356 (interactive "*p")
2357 (if (not (eq last-command 'yank))
2358 (error "Previous command was not a yank"))
2359 (setq this-command 'yank)
2360 (unless arg (setq arg 1))
2361 (let ((inhibit-read-only t)
2362 (before (< (point) (mark t))))
2363 (if before
2364 (funcall (or yank-undo-function 'delete-region) (point) (mark t))
2365 (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
2366 (setq yank-undo-function nil)
2367 (set-marker (mark-marker) (point) (current-buffer))
2368 (insert-for-yank (current-kill arg))
2369 ;; Set the window start back where it was in the yank command,
2370 ;; if possible.
2371 (set-window-start (selected-window) yank-window-start t)
2372 (if before
2373 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2374 ;; It is cleaner to avoid activation, even though the command
2375 ;; loop would deactivate the mark because we inserted text.
2376 (goto-char (prog1 (mark t)
2377 (set-marker (mark-marker) (point) (current-buffer))))))
2378 nil)
2380 (defun yank (&optional arg)
2381 "Reinsert the last stretch of killed text.
2382 More precisely, reinsert the stretch of killed text most recently
2383 killed OR yanked. Put point at end, and set mark at beginning.
2384 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
2385 With argument N, reinsert the Nth most recently killed stretch of killed
2386 text.
2387 See also the command \\[yank-pop]."
2388 (interactive "*P")
2389 (setq yank-window-start (window-start))
2390 ;; If we don't get all the way thru, make last-command indicate that
2391 ;; for the following command.
2392 (setq this-command t)
2393 (push-mark (point))
2394 (insert-for-yank (current-kill (cond
2395 ((listp arg) 0)
2396 ((eq arg '-) -2)
2397 (t (1- arg)))))
2398 (if (consp arg)
2399 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
2400 ;; It is cleaner to avoid activation, even though the command
2401 ;; loop would deactivate the mark because we inserted text.
2402 (goto-char (prog1 (mark t)
2403 (set-marker (mark-marker) (point) (current-buffer)))))
2404 ;; If we do get all the way thru, make this-command indicate that.
2405 (if (eq this-command t)
2406 (setq this-command 'yank))
2407 nil)
2409 (defun rotate-yank-pointer (arg)
2410 "Rotate the yanking point in the kill ring.
2411 With argument, rotate that many kills forward (or backward, if negative)."
2412 (interactive "p")
2413 (current-kill arg))
2415 ;; Some kill commands.
2417 ;; Internal subroutine of delete-char
2418 (defun kill-forward-chars (arg)
2419 (if (listp arg) (setq arg (car arg)))
2420 (if (eq arg '-) (setq arg -1))
2421 (kill-region (point) (forward-point arg)))
2423 ;; Internal subroutine of backward-delete-char
2424 (defun kill-backward-chars (arg)
2425 (if (listp arg) (setq arg (car arg)))
2426 (if (eq arg '-) (setq arg -1))
2427 (kill-region (point) (forward-point (- arg))))
2429 (defcustom backward-delete-char-untabify-method 'untabify
2430 "*The method for untabifying when deleting backward.
2431 Can be `untabify' -- turn a tab to many spaces, then delete one space;
2432 `hungry' -- delete all whitespace, both tabs and spaces;
2433 `all' -- delete all whitespace, including tabs, spaces and newlines;
2434 nil -- just delete one character."
2435 :type '(choice (const untabify) (const hungry) (const all) (const nil))
2436 :version "20.3"
2437 :group 'killing)
2439 (defun backward-delete-char-untabify (arg &optional killp)
2440 "Delete characters backward, changing tabs into spaces.
2441 The exact behavior depends on `backward-delete-char-untabify-method'.
2442 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
2443 Interactively, ARG is the prefix arg (default 1)
2444 and KILLP is t if a prefix arg was specified."
2445 (interactive "*p\nP")
2446 (when (eq backward-delete-char-untabify-method 'untabify)
2447 (let ((count arg))
2448 (save-excursion
2449 (while (and (> count 0) (not (bobp)))
2450 (if (= (preceding-char) ?\t)
2451 (let ((col (current-column)))
2452 (forward-char -1)
2453 (setq col (- col (current-column)))
2454 (insert-char ?\ col)
2455 (delete-char 1)))
2456 (forward-char -1)
2457 (setq count (1- count))))))
2458 (delete-backward-char
2459 (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
2460 ((eq backward-delete-char-untabify-method 'all)
2461 " \t\n\r"))))
2462 (if skip
2463 (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
2464 (point)))))
2465 (+ arg (if (zerop wh) 0 (1- wh))))
2466 arg))
2467 killp))
2469 (defun zap-to-char (arg char)
2470 "Kill up to and including ARG'th occurrence of CHAR.
2471 Case is ignored if `case-fold-search' is non-nil in the current buffer.
2472 Goes backward if ARG is negative; error if CHAR not found."
2473 (interactive "p\ncZap to char: ")
2474 (kill-region (point) (progn
2475 (search-forward (char-to-string char) nil nil arg)
2476 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
2477 (point))))
2479 ;; kill-line and its subroutines.
2481 (defcustom kill-whole-line nil
2482 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
2483 :type 'boolean
2484 :group 'killing)
2486 (defun kill-line (&optional arg)
2487 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
2488 With prefix argument, kill that many lines from point.
2489 Negative arguments kill lines backward.
2490 With zero argument, kills the text before point on the current line.
2492 When calling from a program, nil means \"no arg\",
2493 a number counts as a prefix arg.
2495 To kill a whole line, when point is not at the beginning, type \
2496 \\[beginning-of-line] \\[kill-line] \\[kill-line].
2498 If `kill-whole-line' is non-nil, then this command kills the whole line
2499 including its terminating newline, when used at the beginning of a line
2500 with no argument. As a consequence, you can always kill a whole line
2501 by typing \\[beginning-of-line] \\[kill-line].
2503 If you want to append the killed line to the last killed text,
2504 use \\[append-next-kill] before \\[kill-line].
2506 If the buffer is read-only, Emacs will beep and refrain from deleting
2507 the line, but put the line in the kill ring anyway. This means that
2508 you can use this command to copy text from a read-only buffer.
2509 \(If the variable `kill-read-only-ok' is non-nil, then this won't
2510 even beep.)"
2511 (interactive "P")
2512 (kill-region (point)
2513 ;; It is better to move point to the other end of the kill
2514 ;; before killing. That way, in a read-only buffer, point
2515 ;; moves across the text that is copied to the kill ring.
2516 ;; The choice has no effect on undo now that undo records
2517 ;; the value of point from before the command was run.
2518 (progn
2519 (if arg
2520 (forward-visible-line (prefix-numeric-value arg))
2521 (if (eobp)
2522 (signal 'end-of-buffer nil))
2523 (let ((end
2524 (save-excursion
2525 (end-of-visible-line) (point))))
2526 (if (or (save-excursion
2527 ;; If trailing whitespace is visible,
2528 ;; don't treat it as nothing.
2529 (unless show-trailing-whitespace
2530 (skip-chars-forward " \t" end))
2531 (= (point) end))
2532 (and kill-whole-line (bolp)))
2533 (forward-visible-line 1)
2534 (goto-char end))))
2535 (point))))
2537 (defun kill-whole-line (&optional arg)
2538 "Kill current line.
2539 With prefix arg, kill that many lines starting from the current line.
2540 If arg is negative, kill backward. Also kill the preceding newline.
2541 \(This is meant to make C-x z work well with negative arguments.\)
2542 If arg is zero, kill current line but exclude the trailing newline."
2543 (interactive "p")
2544 (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
2545 (signal 'end-of-buffer nil))
2546 (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
2547 (signal 'beginning-of-buffer nil))
2548 (unless (eq last-command 'kill-region)
2549 (kill-new "")
2550 (setq last-command 'kill-region))
2551 (cond ((zerop arg)
2552 ;; We need to kill in two steps, because the previous command
2553 ;; could have been a kill command, in which case the text
2554 ;; before point needs to be prepended to the current kill
2555 ;; ring entry and the text after point appended. Also, we
2556 ;; need to use save-excursion to avoid copying the same text
2557 ;; twice to the kill ring in read-only buffers.
2558 (save-excursion
2559 (kill-region (point) (progn (forward-visible-line 0) (point))))
2560 (kill-region (point) (progn (end-of-visible-line) (point))))
2561 ((< arg 0)
2562 (save-excursion
2563 (kill-region (point) (progn (end-of-visible-line) (point))))
2564 (kill-region (point)
2565 (progn (forward-visible-line (1+ arg))
2566 (unless (bobp) (backward-char))
2567 (point))))
2569 (save-excursion
2570 (kill-region (point) (progn (forward-visible-line 0) (point))))
2571 (kill-region (point)
2572 (progn (forward-visible-line arg) (point))))))
2574 (defun forward-visible-line (arg)
2575 "Move forward by ARG lines, ignoring currently invisible newlines only.
2576 If ARG is negative, move backward -ARG lines.
2577 If ARG is zero, move to the beginning of the current line."
2578 (condition-case nil
2579 (if (> arg 0)
2580 (progn
2581 (while (> arg 0)
2582 (or (zerop (forward-line 1))
2583 (signal 'end-of-buffer nil))
2584 ;; If the newline we just skipped is invisible,
2585 ;; don't count it.
2586 (let ((prop
2587 (get-char-property (1- (point)) 'invisible)))
2588 (if (if (eq buffer-invisibility-spec t)
2589 prop
2590 (or (memq prop buffer-invisibility-spec)
2591 (assq prop buffer-invisibility-spec)))
2592 (setq arg (1+ arg))))
2593 (setq arg (1- arg)))
2594 ;; If invisible text follows, and it is a number of complete lines,
2595 ;; skip it.
2596 (let ((opoint (point)))
2597 (while (and (not (eobp))
2598 (let ((prop
2599 (get-char-property (point) 'invisible)))
2600 (if (eq buffer-invisibility-spec t)
2601 prop
2602 (or (memq prop buffer-invisibility-spec)
2603 (assq prop buffer-invisibility-spec)))))
2604 (goto-char
2605 (if (get-text-property (point) 'invisible)
2606 (or (next-single-property-change (point) 'invisible)
2607 (point-max))
2608 (next-overlay-change (point)))))
2609 (unless (bolp)
2610 (goto-char opoint))))
2611 (let ((first t))
2612 (while (or first (<= arg 0))
2613 (if first
2614 (beginning-of-line)
2615 (or (zerop (forward-line -1))
2616 (signal 'beginning-of-buffer nil)))
2617 ;; If the newline we just moved to is invisible,
2618 ;; don't count it.
2619 (unless (bobp)
2620 (let ((prop
2621 (get-char-property (1- (point)) 'invisible)))
2622 (unless (if (eq buffer-invisibility-spec t)
2623 prop
2624 (or (memq prop buffer-invisibility-spec)
2625 (assq prop buffer-invisibility-spec)))
2626 (setq arg (1+ arg)))))
2627 (setq first nil))
2628 ;; If invisible text follows, and it is a number of complete lines,
2629 ;; skip it.
2630 (let ((opoint (point)))
2631 (while (and (not (bobp))
2632 (let ((prop
2633 (get-char-property (1- (point)) 'invisible)))
2634 (if (eq buffer-invisibility-spec t)
2635 prop
2636 (or (memq prop buffer-invisibility-spec)
2637 (assq prop buffer-invisibility-spec)))))
2638 (goto-char
2639 (if (get-text-property (1- (point)) 'invisible)
2640 (or (previous-single-property-change (point) 'invisible)
2641 (point-min))
2642 (previous-overlay-change (point)))))
2643 (unless (bolp)
2644 (goto-char opoint)))))
2645 ((beginning-of-buffer end-of-buffer)
2646 nil)))
2648 (defun end-of-visible-line ()
2649 "Move to end of current visible line."
2650 (end-of-line)
2651 ;; If the following character is currently invisible,
2652 ;; skip all characters with that same `invisible' property value,
2653 ;; then find the next newline.
2654 (while (and (not (eobp))
2655 (save-excursion
2656 (skip-chars-forward "^\n")
2657 (let ((prop
2658 (get-char-property (point) 'invisible)))
2659 (if (eq buffer-invisibility-spec t)
2660 prop
2661 (or (memq prop buffer-invisibility-spec)
2662 (assq prop buffer-invisibility-spec))))))
2663 (skip-chars-forward "^\n")
2664 (if (get-text-property (point) 'invisible)
2665 (goto-char (next-single-property-change (point) 'invisible))
2666 (goto-char (next-overlay-change (point))))
2667 (end-of-line)))
2669 (defun insert-buffer (buffer)
2670 "Insert after point the contents of BUFFER.
2671 Puts mark after the inserted text.
2672 BUFFER may be a buffer or a buffer name.
2674 This function is meant for the user to run interactively.
2675 Don't call it from programs: use `insert-buffer-substring' instead!"
2676 (interactive
2677 (list
2678 (progn
2679 (barf-if-buffer-read-only)
2680 (read-buffer "Insert buffer: "
2681 (if (eq (selected-window) (next-window (selected-window)))
2682 (other-buffer (current-buffer))
2683 (window-buffer (next-window (selected-window))))
2684 t))))
2685 (push-mark
2686 (save-excursion
2687 (insert-buffer-substring (get-buffer buffer))
2688 (point)))
2689 nil)
2691 (defun append-to-buffer (buffer start end)
2692 "Append to specified buffer the text of the region.
2693 It is inserted into that buffer before its point.
2695 When calling from a program, give three arguments:
2696 BUFFER (or buffer name), START and END.
2697 START and END specify the portion of the current buffer to be copied."
2698 (interactive
2699 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
2700 (region-beginning) (region-end)))
2701 (let ((oldbuf (current-buffer)))
2702 (save-excursion
2703 (let* ((append-to (get-buffer-create buffer))
2704 (windows (get-buffer-window-list append-to t t))
2705 point)
2706 (set-buffer append-to)
2707 (setq point (point))
2708 (barf-if-buffer-read-only)
2709 (insert-buffer-substring oldbuf start end)
2710 (dolist (window windows)
2711 (when (= (window-point window) point)
2712 (set-window-point window (point))))))))
2714 (defun prepend-to-buffer (buffer start end)
2715 "Prepend to specified buffer the text of the region.
2716 It is inserted into that buffer after its point.
2718 When calling from a program, give three arguments:
2719 BUFFER (or buffer name), START and END.
2720 START and END specify the portion of the current buffer to be copied."
2721 (interactive "BPrepend to buffer: \nr")
2722 (let ((oldbuf (current-buffer)))
2723 (save-excursion
2724 (set-buffer (get-buffer-create buffer))
2725 (barf-if-buffer-read-only)
2726 (save-excursion
2727 (insert-buffer-substring oldbuf start end)))))
2729 (defun copy-to-buffer (buffer start end)
2730 "Copy to specified buffer the text of the region.
2731 It is inserted into that buffer, replacing existing text there.
2733 When calling from a program, give three arguments:
2734 BUFFER (or buffer name), START and END.
2735 START and END specify the portion of the current buffer to be copied."
2736 (interactive "BCopy to buffer: \nr")
2737 (let ((oldbuf (current-buffer)))
2738 (save-excursion
2739 (set-buffer (get-buffer-create buffer))
2740 (barf-if-buffer-read-only)
2741 (erase-buffer)
2742 (save-excursion
2743 (insert-buffer-substring oldbuf start end)))))
2745 (put 'mark-inactive 'error-conditions '(mark-inactive error))
2746 (put 'mark-inactive 'error-message "The mark is not active now")
2748 (defun mark (&optional force)
2749 "Return this buffer's mark value as integer; error if mark inactive.
2750 If optional argument FORCE is non-nil, access the mark value
2751 even if the mark is not currently active, and return nil
2752 if there is no mark at all.
2754 If you are using this in an editing command, you are most likely making
2755 a mistake; see the documentation of `set-mark'."
2756 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
2757 (marker-position (mark-marker))
2758 (signal 'mark-inactive nil)))
2760 ;; Many places set mark-active directly, and several of them failed to also
2761 ;; run deactivate-mark-hook. This shorthand should simplify.
2762 (defsubst deactivate-mark ()
2763 "Deactivate the mark by setting `mark-active' to nil.
2764 \(That makes a difference only in Transient Mark mode.)
2765 Also runs the hook `deactivate-mark-hook'."
2766 (cond
2767 ((eq transient-mark-mode 'lambda)
2768 (setq transient-mark-mode nil))
2769 (transient-mark-mode
2770 (setq mark-active nil)
2771 (run-hooks 'deactivate-mark-hook))))
2773 (defun set-mark (pos)
2774 "Set this buffer's mark to POS. Don't use this function!
2775 That is to say, don't use this function unless you want
2776 the user to see that the mark has moved, and you want the previous
2777 mark position to be lost.
2779 Normally, when a new mark is set, the old one should go on the stack.
2780 This is why most applications should use push-mark, not set-mark.
2782 Novice Emacs Lisp programmers often try to use the mark for the wrong
2783 purposes. The mark saves a location for the user's convenience.
2784 Most editing commands should not alter the mark.
2785 To remember a location for internal use in the Lisp program,
2786 store it in a Lisp variable. Example:
2788 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2790 (if pos
2791 (progn
2792 (setq mark-active t)
2793 (run-hooks 'activate-mark-hook)
2794 (set-marker (mark-marker) pos (current-buffer)))
2795 ;; Normally we never clear mark-active except in Transient Mark mode.
2796 ;; But when we actually clear out the mark value too,
2797 ;; we must clear mark-active in any mode.
2798 (setq mark-active nil)
2799 (run-hooks 'deactivate-mark-hook)
2800 (set-marker (mark-marker) nil)))
2802 (defvar mark-ring nil
2803 "The list of former marks of the current buffer, most recent first.")
2804 (make-variable-buffer-local 'mark-ring)
2805 (put 'mark-ring 'permanent-local t)
2807 (defcustom mark-ring-max 16
2808 "*Maximum size of mark ring. Start discarding off end if gets this big."
2809 :type 'integer
2810 :group 'editing-basics)
2812 (defvar global-mark-ring nil
2813 "The list of saved global marks, most recent first.")
2815 (defcustom global-mark-ring-max 16
2816 "*Maximum size of global mark ring. \
2817 Start discarding off end if gets this big."
2818 :type 'integer
2819 :group 'editing-basics)
2821 (defun pop-to-mark-command ()
2822 "Jump to mark, and pop a new position for mark off the ring
2823 \(does not affect global mark ring\)."
2824 (interactive)
2825 (if (null (mark t))
2826 (error "No mark set in this buffer")
2827 (goto-char (mark t))
2828 (pop-mark)))
2830 (defun push-mark-command (arg &optional nomsg)
2831 "Set mark at where point is.
2832 If no prefix arg and mark is already set there, just activate it.
2833 Display `Mark set' unless the optional second arg NOMSG is non-nil."
2834 (interactive "P")
2835 (let ((mark (marker-position (mark-marker))))
2836 (if (or arg (null mark) (/= mark (point)))
2837 (push-mark nil nomsg t)
2838 (setq mark-active t)
2839 (unless nomsg
2840 (message "Mark activated")))))
2842 (defun set-mark-command (arg)
2843 "Set mark at where point is, or jump to mark.
2844 With no prefix argument, set mark, and push old mark position on local
2845 mark ring; also push mark on global mark ring if last mark was set in
2846 another buffer. Immediately repeating the command activates
2847 `transient-mark-mode' temporarily.
2849 With argument, e.g. \\[universal-argument] \\[set-mark-command], \
2850 jump to mark, and pop a new position
2851 for mark off the local mark ring \(this does not affect the global
2852 mark ring\). Use \\[pop-global-mark] to jump to a mark off the global
2853 mark ring \(see `pop-global-mark'\).
2855 Repeating the \\[set-mark-command] command without the prefix jumps to
2856 the next position off the local (or global) mark ring.
2858 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
2859 \\[universal-argument] \\[set-mark-command], unconditionally
2860 set mark where point is.
2862 Novice Emacs Lisp programmers often try to use the mark for the wrong
2863 purposes. See the documentation of `set-mark' for more information."
2864 (interactive "P")
2865 (if (eq transient-mark-mode 'lambda)
2866 (setq transient-mark-mode nil))
2867 (cond
2868 ((and (consp arg) (> (prefix-numeric-value arg) 4))
2869 (push-mark-command nil))
2870 ((not (eq this-command 'set-mark-command))
2871 (if arg
2872 (pop-to-mark-command)
2873 (push-mark-command t)))
2874 ((eq last-command 'pop-to-mark-command)
2875 (setq this-command 'pop-to-mark-command)
2876 (pop-to-mark-command))
2877 ((and (eq last-command 'pop-global-mark) (not arg))
2878 (setq this-command 'pop-global-mark)
2879 (pop-global-mark))
2880 (arg
2881 (setq this-command 'pop-to-mark-command)
2882 (pop-to-mark-command))
2883 ((and (eq last-command 'set-mark-command)
2884 mark-active (null transient-mark-mode))
2885 (setq transient-mark-mode 'lambda)
2886 (message "Transient-mark-mode temporarily enabled"))
2888 (push-mark-command nil))))
2890 (defun push-mark (&optional location nomsg activate)
2891 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2892 If the last global mark pushed was not in the current buffer,
2893 also push LOCATION on the global mark ring.
2894 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2895 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2897 Novice Emacs Lisp programmers often try to use the mark for the wrong
2898 purposes. See the documentation of `set-mark' for more information.
2900 In Transient Mark mode, this does not activate the mark."
2901 (unless (null (mark t))
2902 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2903 (when (> (length mark-ring) mark-ring-max)
2904 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2905 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))
2906 (set-marker (mark-marker) (or location (point)) (current-buffer))
2907 ;; Now push the mark on the global mark ring.
2908 (if (and global-mark-ring
2909 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2910 ;; The last global mark pushed was in this same buffer.
2911 ;; Don't push another one.
2913 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2914 (when (> (length global-mark-ring) global-mark-ring-max)
2915 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) nil)
2916 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))
2917 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2918 (message "Mark set"))
2919 (if (or activate (not transient-mark-mode))
2920 (set-mark (mark t)))
2921 nil)
2923 (defun pop-mark ()
2924 "Pop off mark ring into the buffer's actual mark.
2925 Does not set point. Does nothing if mark ring is empty."
2926 (when mark-ring
2927 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2928 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2929 (deactivate-mark)
2930 (move-marker (car mark-ring) nil)
2931 (if (null (mark t)) (ding))
2932 (setq mark-ring (cdr mark-ring))))
2934 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2935 (defun exchange-point-and-mark (&optional arg)
2936 "Put the mark where point is now, and point where the mark is now.
2937 This command works even when the mark is not active,
2938 and it reactivates the mark.
2939 With prefix arg, `transient-mark-mode' is enabled temporarily."
2940 (interactive "P")
2941 (if arg
2942 (if mark-active
2943 (if (null transient-mark-mode)
2944 (setq transient-mark-mode 'lambda))
2945 (setq arg nil)))
2946 (unless arg
2947 (let ((omark (mark t)))
2948 (if (null omark)
2949 (error "No mark set in this buffer"))
2950 (set-mark (point))
2951 (goto-char omark)
2952 nil)))
2954 (define-minor-mode transient-mark-mode
2955 "Toggle Transient Mark mode.
2956 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2958 In Transient Mark mode, when the mark is active, the region is highlighted.
2959 Changing the buffer \"deactivates\" the mark.
2960 So do certain other operations that set the mark
2961 but whose main purpose is something else--for example,
2962 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
2964 You can also deactivate the mark by typing \\[keyboard-quit] or
2965 \\[keyboard-escape-quit].
2967 Many commands change their behavior when Transient Mark mode is in effect
2968 and the mark is active, by acting on the region instead of their usual
2969 default part of the buffer's text. Examples of such commands include
2970 \\[comment-dwim], \\[flush-lines], \\[ispell], \\[keep-lines],
2971 \\[query-replace], \\[query-replace-regexp], and \\[undo]. Invoke
2972 \\[apropos-documentation] and type \"transient\" or \"mark.*active\" at
2973 the prompt, to see the documentation of commands which are sensitive to
2974 the Transient Mark mode."
2975 :global t :group 'editing-basics :require nil)
2977 (defun pop-global-mark ()
2978 "Pop off global mark ring and jump to the top location."
2979 (interactive)
2980 ;; Pop entries which refer to non-existent buffers.
2981 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2982 (setq global-mark-ring (cdr global-mark-ring)))
2983 (or global-mark-ring
2984 (error "No global mark set"))
2985 (let* ((marker (car global-mark-ring))
2986 (buffer (marker-buffer marker))
2987 (position (marker-position marker)))
2988 (setq global-mark-ring (nconc (cdr global-mark-ring)
2989 (list (car global-mark-ring))))
2990 (set-buffer buffer)
2991 (or (and (>= position (point-min))
2992 (<= position (point-max)))
2993 (widen))
2994 (goto-char position)
2995 (switch-to-buffer buffer)))
2997 (defcustom next-line-add-newlines nil
2998 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2999 :type 'boolean
3000 :version "21.1"
3001 :group 'editing-basics)
3003 (defun next-line (&optional arg)
3004 "Move cursor vertically down ARG lines.
3005 If there is no character in the target line exactly under the current column,
3006 the cursor is positioned after the character in that line which spans this
3007 column, or at the end of the line if it is not long enough.
3008 If there is no line in the buffer after this one, behavior depends on the
3009 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
3010 to create a line, and moves the cursor to that line. Otherwise it moves the
3011 cursor to the end of the buffer.
3013 The command \\[set-goal-column] can be used to create
3014 a semipermanent goal column for this command.
3015 Then instead of trying to move exactly vertically (or as close as possible),
3016 this command moves to the specified goal column (or as close as possible).
3017 The goal column is stored in the variable `goal-column', which is nil
3018 when there is no goal column.
3020 If you are thinking of using this in a Lisp program, consider
3021 using `forward-line' instead. It is usually easier to use
3022 and more reliable (no dependence on goal column, etc.)."
3023 (interactive "p")
3024 (or arg (setq arg 1))
3025 (if (and next-line-add-newlines (= arg 1))
3026 (if (save-excursion (end-of-line) (eobp))
3027 ;; When adding a newline, don't expand an abbrev.
3028 (let ((abbrev-mode nil))
3029 (end-of-line)
3030 (insert "\n"))
3031 (line-move arg))
3032 (if (interactive-p)
3033 (condition-case nil
3034 (line-move arg)
3035 ((beginning-of-buffer end-of-buffer) (ding)))
3036 (line-move arg)))
3037 nil)
3039 (defun previous-line (&optional arg)
3040 "Move cursor vertically up ARG lines.
3041 If there is no character in the target line exactly over the current column,
3042 the cursor is positioned after the character in that line which spans this
3043 column, or at the end of the line if it is not long enough.
3045 The command \\[set-goal-column] can be used to create
3046 a semipermanent goal column for this command.
3047 Then instead of trying to move exactly vertically (or as close as possible),
3048 this command moves to the specified goal column (or as close as possible).
3049 The goal column is stored in the variable `goal-column', which is nil
3050 when there is no goal column.
3052 If you are thinking of using this in a Lisp program, consider using
3053 `forward-line' with a negative argument instead. It is usually easier
3054 to use and more reliable (no dependence on goal column, etc.)."
3055 (interactive "p")
3056 (or arg (setq arg 1))
3057 (if (interactive-p)
3058 (condition-case nil
3059 (line-move (- arg))
3060 ((beginning-of-buffer end-of-buffer) (ding)))
3061 (line-move (- arg)))
3062 nil)
3064 (defcustom track-eol nil
3065 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
3066 This means moving to the end of each line moved onto.
3067 The beginning of a blank line does not count as the end of a line."
3068 :type 'boolean
3069 :group 'editing-basics)
3071 (defcustom goal-column nil
3072 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
3073 :type '(choice integer
3074 (const :tag "None" nil))
3075 :group 'editing-basics)
3076 (make-variable-buffer-local 'goal-column)
3078 (defvar temporary-goal-column 0
3079 "Current goal column for vertical motion.
3080 It is the column where point was
3081 at the start of current run of vertical motion commands.
3082 When the `track-eol' feature is doing its job, the value is 9999.")
3084 (defcustom line-move-ignore-invisible nil
3085 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
3086 Outline mode sets this."
3087 :type 'boolean
3088 :group 'editing-basics)
3090 (defun line-move-invisible (pos)
3091 "Return non-nil if the character after POS is currently invisible."
3092 (let ((prop
3093 (get-char-property pos 'invisible)))
3094 (if (eq buffer-invisibility-spec t)
3095 prop
3096 (or (memq prop buffer-invisibility-spec)
3097 (assq prop buffer-invisibility-spec)))))
3099 ;; This is the guts of next-line and previous-line.
3100 ;; Arg says how many lines to move.
3101 (defun line-move (arg)
3102 ;; Don't run any point-motion hooks, and disregard intangibility,
3103 ;; for intermediate positions.
3104 (let ((inhibit-point-motion-hooks t)
3105 (opoint (point))
3106 new line-end line-beg)
3107 (unwind-protect
3108 (progn
3109 (if (not (memq last-command '(next-line previous-line)))
3110 (setq temporary-goal-column
3111 (if (and track-eol (eolp)
3112 ;; Don't count beg of empty line as end of line
3113 ;; unless we just did explicit end-of-line.
3114 (or (not (bolp)) (eq last-command 'end-of-line)))
3115 9999
3116 (current-column))))
3117 (if (and (not (integerp selective-display))
3118 (not line-move-ignore-invisible))
3119 ;; Use just newline characters.
3120 ;; Set ARG to 0 if we move as many lines as requested.
3121 (or (if (> arg 0)
3122 (progn (if (> arg 1) (forward-line (1- arg)))
3123 ;; This way of moving forward ARG lines
3124 ;; verifies that we have a newline after the last one.
3125 ;; It doesn't get confused by intangible text.
3126 (end-of-line)
3127 (if (zerop (forward-line 1))
3128 (setq arg 0)))
3129 (and (zerop (forward-line arg))
3130 (bolp)
3131 (setq arg 0)))
3132 (signal (if (< arg 0)
3133 'beginning-of-buffer
3134 'end-of-buffer)
3135 nil))
3136 ;; Move by arg lines, but ignore invisible ones.
3137 (while (> arg 0)
3138 ;; If the following character is currently invisible,
3139 ;; skip all characters with that same `invisible' property value.
3140 (while (and (not (eobp)) (line-move-invisible (point)))
3141 (goto-char (next-char-property-change (point))))
3142 ;; Now move a line.
3143 (end-of-line)
3144 (and (zerop (vertical-motion 1))
3145 (signal 'end-of-buffer nil))
3146 (setq arg (1- arg)))
3147 (while (< arg 0)
3148 (beginning-of-line)
3149 (and (zerop (vertical-motion -1))
3150 (signal 'beginning-of-buffer nil))
3151 (setq arg (1+ arg))
3152 (while (and (not (bobp)) (line-move-invisible (1- (point))))
3153 (goto-char (previous-char-property-change (point)))))))
3155 (cond ((> arg 0)
3156 ;; If we did not move down as far as desired,
3157 ;; at least go to end of line.
3158 (end-of-line))
3159 ((< arg 0)
3160 ;; If we did not move down as far as desired,
3161 ;; at least go to end of line.
3162 (beginning-of-line))
3164 (line-move-finish (or goal-column temporary-goal-column) opoint)))))
3165 nil)
3167 (defun line-move-finish (column opoint)
3168 (let ((repeat t))
3169 (while repeat
3170 ;; Set REPEAT to t to repeat the whole thing.
3171 (setq repeat nil)
3173 (let (new
3174 (line-beg (save-excursion (beginning-of-line) (point)))
3175 (line-end
3176 ;; Compute the end of the line
3177 ;; ignoring effectively intangible newlines.
3178 (let ((inhibit-point-motion-hooks nil)
3179 (inhibit-field-text-motion t))
3180 (save-excursion (end-of-line) (point)))))
3182 ;; Move to the desired column.
3183 (line-move-to-column column)
3184 (setq new (point))
3186 ;; Process intangibility within a line.
3187 ;; Move to the chosen destination position from above,
3188 ;; with intangibility processing enabled.
3190 (goto-char (point-min))
3191 (let ((inhibit-point-motion-hooks nil))
3192 (goto-char new)
3194 ;; If intangibility moves us to a different (later) place
3195 ;; in the same line, use that as the destination.
3196 (if (<= (point) line-end)
3197 (setq new (point))
3198 ;; If that position is "too late",
3199 ;; try the previous allowable position.
3200 ;; See if it is ok.
3201 (backward-char)
3202 (if (<= (point) line-end)
3203 (setq new (point))
3204 ;; As a last resort, use the end of the line.
3205 (setq new line-end))))
3207 ;; Now move to the updated destination, processing fields
3208 ;; as well as intangibility.
3209 (goto-char opoint)
3210 (let ((inhibit-point-motion-hooks nil))
3211 (goto-char
3212 (constrain-to-field new opoint nil t
3213 'inhibit-line-move-field-capture)))
3215 ;; If all this moved us to a different line,
3216 ;; retry everything within that new line.
3217 (when (or (< (point) line-beg) (> (point) line-end))
3218 ;; Repeat the intangibility and field processing.
3219 (setq repeat t))))))
3221 (defun line-move-to-column (col)
3222 "Try to find column COL, considering invisibility.
3223 This function works only in certain cases,
3224 because what we really need is for `move-to-column'
3225 and `current-column' to be able to ignore invisible text."
3226 (if (zerop col)
3227 (beginning-of-line)
3228 (move-to-column col))
3230 (when (and line-move-ignore-invisible
3231 (not (bolp)) (line-move-invisible (1- (point))))
3232 (let ((normal-location (point))
3233 (normal-column (current-column)))
3234 ;; If the following character is currently invisible,
3235 ;; skip all characters with that same `invisible' property value.
3236 (while (and (not (eobp))
3237 (line-move-invisible (point)))
3238 (goto-char (next-char-property-change (point))))
3239 ;; Have we advanced to a larger column position?
3240 (if (> (current-column) normal-column)
3241 ;; We have made some progress towards the desired column.
3242 ;; See if we can make any further progress.
3243 (line-move-to-column (+ (current-column) (- col normal-column)))
3244 ;; Otherwise, go to the place we originally found
3245 ;; and move back over invisible text.
3246 ;; that will get us to the same place on the screen
3247 ;; but with a more reasonable buffer position.
3248 (goto-char normal-location)
3249 (let ((line-beg (save-excursion (beginning-of-line) (point))))
3250 (while (and (not (bolp)) (line-move-invisible (1- (point))))
3251 (goto-char (previous-char-property-change (point) line-beg))))))))
3253 ;;; Many people have said they rarely use this feature, and often type
3254 ;;; it by accident. Maybe it shouldn't even be on a key.
3255 (put 'set-goal-column 'disabled t)
3257 (defun set-goal-column (arg)
3258 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
3259 Those commands will move to this position in the line moved to
3260 rather than trying to keep the same horizontal position.
3261 With a non-nil argument, clears out the goal column
3262 so that \\[next-line] and \\[previous-line] resume vertical motion.
3263 The goal column is stored in the variable `goal-column'."
3264 (interactive "P")
3265 (if arg
3266 (progn
3267 (setq goal-column nil)
3268 (message "No goal column"))
3269 (setq goal-column (current-column))
3270 (message (substitute-command-keys
3271 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
3272 goal-column))
3273 nil)
3276 (defun scroll-other-window-down (lines)
3277 "Scroll the \"other window\" down.
3278 For more details, see the documentation for `scroll-other-window'."
3279 (interactive "P")
3280 (scroll-other-window
3281 ;; Just invert the argument's meaning.
3282 ;; We can do that without knowing which window it will be.
3283 (if (eq lines '-) nil
3284 (if (null lines) '-
3285 (- (prefix-numeric-value lines))))))
3286 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
3288 (defun beginning-of-buffer-other-window (arg)
3289 "Move point to the beginning of the buffer in the other window.
3290 Leave mark at previous position.
3291 With arg N, put point N/10 of the way from the true beginning."
3292 (interactive "P")
3293 (let ((orig-window (selected-window))
3294 (window (other-window-for-scrolling)))
3295 ;; We use unwind-protect rather than save-window-excursion
3296 ;; because the latter would preserve the things we want to change.
3297 (unwind-protect
3298 (progn
3299 (select-window window)
3300 ;; Set point and mark in that window's buffer.
3301 (beginning-of-buffer arg)
3302 ;; Set point accordingly.
3303 (recenter '(t)))
3304 (select-window orig-window))))
3306 (defun end-of-buffer-other-window (arg)
3307 "Move point to the end of the buffer in the other window.
3308 Leave mark at previous position.
3309 With arg N, put point N/10 of the way from the true end."
3310 (interactive "P")
3311 ;; See beginning-of-buffer-other-window for comments.
3312 (let ((orig-window (selected-window))
3313 (window (other-window-for-scrolling)))
3314 (unwind-protect
3315 (progn
3316 (select-window window)
3317 (end-of-buffer arg)
3318 (recenter '(t)))
3319 (select-window orig-window))))
3321 (defun transpose-chars (arg)
3322 "Interchange characters around point, moving forward one character.
3323 With prefix arg ARG, effect is to take character before point
3324 and drag it forward past ARG other characters (backward if ARG negative).
3325 If no argument and at end of line, the previous two chars are exchanged."
3326 (interactive "*P")
3327 (and (null arg) (eolp) (forward-char -1))
3328 (transpose-subr 'forward-char (prefix-numeric-value arg)))
3330 (defun transpose-words (arg)
3331 "Interchange words around point, leaving point at end of them.
3332 With prefix arg ARG, effect is to take word before or around point
3333 and drag it forward past ARG other words (backward if ARG negative).
3334 If ARG is zero, the words around or after point and around or after mark
3335 are interchanged."
3336 ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'.
3337 (interactive "*p")
3338 (transpose-subr 'forward-word arg))
3340 (defun transpose-sexps (arg)
3341 "Like \\[transpose-words] but applies to sexps.
3342 Does not work on a sexp that point is in the middle of
3343 if it is a list or string."
3344 (interactive "*p")
3345 (transpose-subr
3346 (lambda (arg)
3347 ;; Here we should try to simulate the behavior of
3348 ;; (cons (progn (forward-sexp x) (point))
3349 ;; (progn (forward-sexp (- x)) (point)))
3350 ;; Except that we don't want to rely on the second forward-sexp
3351 ;; putting us back to where we want to be, since forward-sexp-function
3352 ;; might do funny things like infix-precedence.
3353 (if (if (> arg 0)
3354 (looking-at "\\sw\\|\\s_")
3355 (and (not (bobp))
3356 (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_"))))
3357 ;; Jumping over a symbol. We might be inside it, mind you.
3358 (progn (funcall (if (> arg 0)
3359 'skip-syntax-backward 'skip-syntax-forward)
3360 "w_")
3361 (cons (save-excursion (forward-sexp arg) (point)) (point)))
3362 ;; Otherwise, we're between sexps. Take a step back before jumping
3363 ;; to make sure we'll obey the same precedence no matter which direction
3364 ;; we're going.
3365 (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .")
3366 (cons (save-excursion (forward-sexp arg) (point))
3367 (progn (while (or (forward-comment (if (> arg 0) 1 -1))
3368 (not (zerop (funcall (if (> arg 0)
3369 'skip-syntax-forward
3370 'skip-syntax-backward)
3371 ".")))))
3372 (point)))))
3373 arg 'special))
3375 (defun transpose-lines (arg)
3376 "Exchange current line and previous line, leaving point after both.
3377 With argument ARG, takes previous line and moves it past ARG lines.
3378 With argument 0, interchanges line point is in with line mark is in."
3379 (interactive "*p")
3380 (transpose-subr (function
3381 (lambda (arg)
3382 (if (> arg 0)
3383 (progn
3384 ;; Move forward over ARG lines,
3385 ;; but create newlines if necessary.
3386 (setq arg (forward-line arg))
3387 (if (/= (preceding-char) ?\n)
3388 (setq arg (1+ arg)))
3389 (if (> arg 0)
3390 (newline arg)))
3391 (forward-line arg))))
3392 arg))
3394 (defun transpose-subr (mover arg &optional special)
3395 (let ((aux (if special mover
3396 (lambda (x)
3397 (cons (progn (funcall mover x) (point))
3398 (progn (funcall mover (- x)) (point))))))
3399 pos1 pos2)
3400 (cond
3401 ((= arg 0)
3402 (save-excursion
3403 (setq pos1 (funcall aux 1))
3404 (goto-char (mark))
3405 (setq pos2 (funcall aux 1))
3406 (transpose-subr-1 pos1 pos2))
3407 (exchange-point-and-mark))
3408 ((> arg 0)
3409 (setq pos1 (funcall aux -1))
3410 (setq pos2 (funcall aux arg))
3411 (transpose-subr-1 pos1 pos2)
3412 (goto-char (car pos2)))
3414 (setq pos1 (funcall aux -1))
3415 (goto-char (car pos1))
3416 (setq pos2 (funcall aux arg))
3417 (transpose-subr-1 pos1 pos2)))))
3419 (defun transpose-subr-1 (pos1 pos2)
3420 (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
3421 (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
3422 (when (> (car pos1) (car pos2))
3423 (let ((swap pos1))
3424 (setq pos1 pos2 pos2 swap)))
3425 (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
3426 (atomic-change-group
3427 (let (word2)
3428 ;; FIXME: We first delete the two pieces of text, so markers that
3429 ;; used to point to after the text end up pointing to before it :-(
3430 (setq word2 (delete-and-extract-region (car pos2) (cdr pos2)))
3431 (goto-char (car pos2))
3432 (insert (delete-and-extract-region (car pos1) (cdr pos1)))
3433 (goto-char (car pos1))
3434 (insert word2))))
3436 (defun backward-word (&optional arg)
3437 "Move backward until encountering the beginning of a word.
3438 With argument, do this that many times."
3439 (interactive "p")
3440 (forward-word (- (or arg 1))))
3442 (defun mark-word (arg)
3443 "Set mark arg words away from point.
3444 If this command is repeated, it marks the next ARG words after the ones
3445 already marked."
3446 (interactive "p")
3447 (cond ((and (eq last-command this-command) (mark t))
3448 (set-mark
3449 (save-excursion
3450 (goto-char (mark))
3451 (forward-word arg)
3452 (point))))
3454 (push-mark
3455 (save-excursion
3456 (forward-word arg)
3457 (point))
3458 nil t))))
3460 (defun kill-word (arg)
3461 "Kill characters forward until encountering the end of a word.
3462 With argument, do this that many times."
3463 (interactive "p")
3464 (kill-region (point) (progn (forward-word arg) (point))))
3466 (defun backward-kill-word (arg)
3467 "Kill characters backward until encountering the end of a word.
3468 With argument, do this that many times."
3469 (interactive "p")
3470 (kill-word (- arg)))
3472 (defun current-word (&optional strict really-word)
3473 "Return the symbol or word that point is on (or a nearby one) as a string.
3474 The return value includes no text properties.
3475 If optional arg STRICT is non-nil, return nil unless point is within
3476 or adjacent to a symbol or word. In all cases the value can be nil
3477 if there is no word nearby.
3478 The function, belying its name, normally finds a symbol.
3479 If optional arg REALLY-WORD is non-nil, it finds just a word."
3480 (save-excursion
3481 (let* ((oldpoint (point)) (start (point)) (end (point))
3482 (syntaxes (if really-word "w" "w_"))
3483 (not-syntaxes (concat "^" syntaxes)))
3484 (skip-syntax-backward syntaxes) (setq start (point))
3485 (goto-char oldpoint)
3486 (skip-syntax-forward syntaxes) (setq end (point))
3487 (when (and (eq start oldpoint) (eq end oldpoint)
3488 ;; Point is neither within nor adjacent to a word.
3489 (not strict))
3490 ;; Look for preceding word in same line.
3491 (skip-syntax-backward not-syntaxes
3492 (save-excursion (beginning-of-line)
3493 (point)))
3494 (if (bolp)
3495 ;; No preceding word in same line.
3496 ;; Look for following word in same line.
3497 (progn
3498 (skip-syntax-forward not-syntaxes
3499 (save-excursion (end-of-line)
3500 (point)))
3501 (setq start (point))
3502 (skip-syntax-forward syntaxes)
3503 (setq end (point)))
3504 (setq end (point))
3505 (skip-syntax-backward syntaxes)
3506 (setq start (point))))
3507 ;; If we found something nonempty, return it as a string.
3508 (unless (= start end)
3509 (buffer-substring-no-properties start end)))))
3511 (defcustom fill-prefix nil
3512 "*String for filling to insert at front of new line, or nil for none."
3513 :type '(choice (const :tag "None" nil)
3514 string)
3515 :group 'fill)
3516 (make-variable-buffer-local 'fill-prefix)
3518 (defcustom auto-fill-inhibit-regexp nil
3519 "*Regexp to match lines which should not be auto-filled."
3520 :type '(choice (const :tag "None" nil)
3521 regexp)
3522 :group 'fill)
3524 (defvar comment-line-break-function 'comment-indent-new-line
3525 "*Mode-specific function which line breaks and continues a comment.
3527 This function is only called during auto-filling of a comment section.
3528 The function should take a single optional argument, which is a flag
3529 indicating whether it should use soft newlines.
3531 Setting this variable automatically makes it local to the current buffer.")
3533 ;; This function is used as the auto-fill-function of a buffer
3534 ;; when Auto-Fill mode is enabled.
3535 ;; It returns t if it really did any work.
3536 ;; (Actually some major modes use a different auto-fill function,
3537 ;; but this one is the default one.)
3538 (defun do-auto-fill ()
3539 (let (fc justify give-up
3540 (fill-prefix fill-prefix))
3541 (if (or (not (setq justify (current-justification)))
3542 (null (setq fc (current-fill-column)))
3543 (and (eq justify 'left)
3544 (<= (current-column) fc))
3545 (and auto-fill-inhibit-regexp
3546 (save-excursion (beginning-of-line)
3547 (looking-at auto-fill-inhibit-regexp))))
3548 nil ;; Auto-filling not required
3549 (if (memq justify '(full center right))
3550 (save-excursion (unjustify-current-line)))
3552 ;; Choose a fill-prefix automatically.
3553 (when (and adaptive-fill-mode
3554 (or (null fill-prefix) (string= fill-prefix "")))
3555 (let ((prefix
3556 (fill-context-prefix
3557 (save-excursion (backward-paragraph 1) (point))
3558 (save-excursion (forward-paragraph 1) (point)))))
3559 (and prefix (not (equal prefix ""))
3560 ;; Use auto-indentation rather than a guessed empty prefix.
3561 (not (and fill-indent-according-to-mode
3562 (string-match "\\`[ \t]*\\'" prefix)))
3563 (setq fill-prefix prefix))))
3565 (while (and (not give-up) (> (current-column) fc))
3566 ;; Determine where to split the line.
3567 (let* (after-prefix
3568 (fill-point
3569 (save-excursion
3570 (beginning-of-line)
3571 (setq after-prefix (point))
3572 (and fill-prefix
3573 (looking-at (regexp-quote fill-prefix))
3574 (setq after-prefix (match-end 0)))
3575 (move-to-column (1+ fc))
3576 (fill-move-to-break-point after-prefix)
3577 (point))))
3579 ;; See whether the place we found is any good.
3580 (if (save-excursion
3581 (goto-char fill-point)
3582 (or (bolp)
3583 ;; There is no use breaking at end of line.
3584 (save-excursion (skip-chars-forward " ") (eolp))
3585 ;; It is futile to split at the end of the prefix
3586 ;; since we would just insert the prefix again.
3587 (and after-prefix (<= (point) after-prefix))
3588 ;; Don't split right after a comment starter
3589 ;; since we would just make another comment starter.
3590 (and comment-start-skip
3591 (let ((limit (point)))
3592 (beginning-of-line)
3593 (and (re-search-forward comment-start-skip
3594 limit t)
3595 (eq (point) limit))))))
3596 ;; No good place to break => stop trying.
3597 (setq give-up t)
3598 ;; Ok, we have a useful place to break the line. Do it.
3599 (let ((prev-column (current-column)))
3600 ;; If point is at the fill-point, do not `save-excursion'.
3601 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3602 ;; point will end up before it rather than after it.
3603 (if (save-excursion
3604 (skip-chars-backward " \t")
3605 (= (point) fill-point))
3606 (funcall comment-line-break-function t)
3607 (save-excursion
3608 (goto-char fill-point)
3609 (funcall comment-line-break-function t)))
3610 ;; Now do justification, if required
3611 (if (not (eq justify 'left))
3612 (save-excursion
3613 (end-of-line 0)
3614 (justify-current-line justify nil t)))
3615 ;; If making the new line didn't reduce the hpos of
3616 ;; the end of the line, then give up now;
3617 ;; trying again will not help.
3618 (if (>= (current-column) prev-column)
3619 (setq give-up t))))))
3620 ;; Justify last line.
3621 (justify-current-line justify t t)
3622 t)))
3624 (defvar normal-auto-fill-function 'do-auto-fill
3625 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3626 Some major modes set this.")
3628 ;; FIXME: turn into a proper minor mode.
3629 ;; Add a global minor mode version of it.
3630 (defun auto-fill-mode (&optional arg)
3631 "Toggle Auto Fill mode.
3632 With arg, turn Auto Fill mode on if and only if arg is positive.
3633 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3634 automatically breaks the line at a previous space.
3636 The value of `normal-auto-fill-function' specifies the function to use
3637 for `auto-fill-function' when turning Auto Fill mode on."
3638 (interactive "P")
3639 (prog1 (setq auto-fill-function
3640 (if (if (null arg)
3641 (not auto-fill-function)
3642 (> (prefix-numeric-value arg) 0))
3643 normal-auto-fill-function
3644 nil))
3645 (force-mode-line-update)))
3647 ;; This holds a document string used to document auto-fill-mode.
3648 (defun auto-fill-function ()
3649 "Automatically break line at a previous space, in insertion of text."
3650 nil)
3652 (defun turn-on-auto-fill ()
3653 "Unconditionally turn on Auto Fill mode."
3654 (auto-fill-mode 1))
3656 (defun turn-off-auto-fill ()
3657 "Unconditionally turn off Auto Fill mode."
3658 (auto-fill-mode -1))
3660 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3662 (defun set-fill-column (arg)
3663 "Set `fill-column' to specified argument.
3664 Use \\[universal-argument] followed by a number to specify a column.
3665 Just \\[universal-argument] as argument means to use the current column."
3666 (interactive "P")
3667 (if (consp arg)
3668 (setq arg (current-column)))
3669 (if (not (integerp arg))
3670 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3671 (error "Set-fill-column requires an explicit argument")
3672 (message "Fill column set to %d (was %d)" arg fill-column)
3673 (setq fill-column arg)))
3675 (defun set-selective-display (arg)
3676 "Set `selective-display' to ARG; clear it if no arg.
3677 When the value of `selective-display' is a number > 0,
3678 lines whose indentation is >= that value are not displayed.
3679 The variable `selective-display' has a separate value for each buffer."
3680 (interactive "P")
3681 (if (eq selective-display t)
3682 (error "selective-display already in use for marked lines"))
3683 (let ((current-vpos
3684 (save-restriction
3685 (narrow-to-region (point-min) (point))
3686 (goto-char (window-start))
3687 (vertical-motion (window-height)))))
3688 (setq selective-display
3689 (and arg (prefix-numeric-value arg)))
3690 (recenter current-vpos))
3691 (set-window-start (selected-window) (window-start (selected-window)))
3692 (princ "selective-display set to " t)
3693 (prin1 selective-display t)
3694 (princ "." t))
3696 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
3697 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
3699 (defun toggle-truncate-lines (arg)
3700 "Toggle whether to fold or truncate long lines on the screen.
3701 With arg, truncate long lines iff arg is positive.
3702 Note that in side-by-side windows, truncation is always enabled."
3703 (interactive "P")
3704 (setq truncate-lines
3705 (if (null arg)
3706 (not truncate-lines)
3707 (> (prefix-numeric-value arg) 0)))
3708 (force-mode-line-update)
3709 (unless truncate-lines
3710 (let ((buffer (current-buffer)))
3711 (walk-windows (lambda (window)
3712 (if (eq buffer (window-buffer window))
3713 (set-window-hscroll window 0)))
3714 nil t)))
3715 (message "Truncate long lines %s"
3716 (if truncate-lines "enabled" "disabled")))
3718 (defvar overwrite-mode-textual " Ovwrt"
3719 "The string displayed in the mode line when in overwrite mode.")
3720 (defvar overwrite-mode-binary " Bin Ovwrt"
3721 "The string displayed in the mode line when in binary overwrite mode.")
3723 (defun overwrite-mode (arg)
3724 "Toggle overwrite mode.
3725 With arg, turn overwrite mode on iff arg is positive.
3726 In overwrite mode, printing characters typed in replace existing text
3727 on a one-for-one basis, rather than pushing it to the right. At the
3728 end of a line, such characters extend the line. Before a tab,
3729 such characters insert until the tab is filled in.
3730 \\[quoted-insert] still inserts characters in overwrite mode; this
3731 is supposed to make it easier to insert characters when necessary."
3732 (interactive "P")
3733 (setq overwrite-mode
3734 (if (if (null arg) (not overwrite-mode)
3735 (> (prefix-numeric-value arg) 0))
3736 'overwrite-mode-textual))
3737 (force-mode-line-update))
3739 (defun binary-overwrite-mode (arg)
3740 "Toggle binary overwrite mode.
3741 With arg, turn binary overwrite mode on iff arg is positive.
3742 In binary overwrite mode, printing characters typed in replace
3743 existing text. Newlines are not treated specially, so typing at the
3744 end of a line joins the line to the next, with the typed character
3745 between them. Typing before a tab character simply replaces the tab
3746 with the character typed.
3747 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3748 typing characters do.
3750 Note that binary overwrite mode is not its own minor mode; it is a
3751 specialization of overwrite-mode, entered by setting the
3752 `overwrite-mode' variable to `overwrite-mode-binary'."
3753 (interactive "P")
3754 (setq overwrite-mode
3755 (if (if (null arg)
3756 (not (eq overwrite-mode 'overwrite-mode-binary))
3757 (> (prefix-numeric-value arg) 0))
3758 'overwrite-mode-binary))
3759 (force-mode-line-update))
3761 (define-minor-mode line-number-mode
3762 "Toggle Line Number mode.
3763 With arg, turn Line Number mode on iff arg is positive.
3764 When Line Number mode is enabled, the line number appears
3765 in the mode line.
3767 Line numbers do not appear for very large buffers and buffers
3768 with very long lines; see variables `line-number-display-limit'
3769 and `line-number-display-limit-width'."
3770 :init-value t :global t :group 'editing-basics :require nil)
3772 (define-minor-mode column-number-mode
3773 "Toggle Column Number mode.
3774 With arg, turn Column Number mode on iff arg is positive.
3775 When Column Number mode is enabled, the column number appears
3776 in the mode line."
3777 :global t :group 'editing-basics :require nil)
3779 (define-minor-mode size-indication-mode
3780 "Toggle Size Indication mode.
3781 With arg, turn Size Indication mode on iff arg is positive. When
3782 Size Indication mode is enabled, the size of the accessible part
3783 of the buffer appears in the mode line."
3784 :global t :group 'editing-basics :require nil)
3786 (defgroup paren-blinking nil
3787 "Blinking matching of parens and expressions."
3788 :prefix "blink-matching-"
3789 :group 'paren-matching)
3791 (defcustom blink-matching-paren t
3792 "*Non-nil means show matching open-paren when close-paren is inserted."
3793 :type 'boolean
3794 :group 'paren-blinking)
3796 (defcustom blink-matching-paren-on-screen t
3797 "*Non-nil means show matching open-paren when it is on screen.
3798 If nil, means don't show it (but the open-paren can still be shown
3799 when it is off screen)."
3800 :type 'boolean
3801 :group 'paren-blinking)
3803 (defcustom blink-matching-paren-distance (* 25 1024)
3804 "*If non-nil, is maximum distance to search for matching open-paren."
3805 :type 'integer
3806 :group 'paren-blinking)
3808 (defcustom blink-matching-delay 1
3809 "*Time in seconds to delay after showing a matching paren."
3810 :type 'number
3811 :group 'paren-blinking)
3813 (defcustom blink-matching-paren-dont-ignore-comments nil
3814 "*Non-nil means `blink-matching-paren' will not ignore comments."
3815 :type 'boolean
3816 :group 'paren-blinking)
3818 (defun blink-matching-open ()
3819 "Move cursor momentarily to the beginning of the sexp before point."
3820 (interactive)
3821 (and (> (point) (1+ (point-min)))
3822 blink-matching-paren
3823 ;; Verify an even number of quoting characters precede the close.
3824 (= 1 (logand 1 (- (point)
3825 (save-excursion
3826 (forward-char -1)
3827 (skip-syntax-backward "/\\")
3828 (point)))))
3829 (let* ((oldpos (point))
3830 (blinkpos)
3831 (mismatch)
3832 matching-paren)
3833 (save-excursion
3834 (save-restriction
3835 (if blink-matching-paren-distance
3836 (narrow-to-region (max (point-min)
3837 (- (point) blink-matching-paren-distance))
3838 oldpos))
3839 (condition-case ()
3840 (let ((parse-sexp-ignore-comments
3841 (and parse-sexp-ignore-comments
3842 (not blink-matching-paren-dont-ignore-comments))))
3843 (setq blinkpos (scan-sexps oldpos -1)))
3844 (error nil)))
3845 (and blinkpos
3846 (save-excursion
3847 (goto-char blinkpos)
3848 (not (looking-at "\\s$")))
3849 (setq matching-paren
3850 (or (and parse-sexp-lookup-properties
3851 (let ((prop (get-text-property blinkpos 'syntax-table)))
3852 (and (consp prop)
3853 (eq (car prop) 4)
3854 (cdr prop))))
3855 (matching-paren (char-after blinkpos)))
3856 mismatch
3857 (or (null matching-paren)
3858 (/= (char-after (1- oldpos))
3859 matching-paren))))
3860 (if mismatch (setq blinkpos nil))
3861 (if blinkpos
3862 ;; Don't log messages about paren matching.
3863 (let (message-log-max)
3864 (goto-char blinkpos)
3865 (if (pos-visible-in-window-p)
3866 (and blink-matching-paren-on-screen
3867 (sit-for blink-matching-delay))
3868 (goto-char blinkpos)
3869 (message
3870 "Matches %s"
3871 ;; Show what precedes the open in its line, if anything.
3872 (if (save-excursion
3873 (skip-chars-backward " \t")
3874 (not (bolp)))
3875 (buffer-substring (progn (beginning-of-line) (point))
3876 (1+ blinkpos))
3877 ;; Show what follows the open in its line, if anything.
3878 (if (save-excursion
3879 (forward-char 1)
3880 (skip-chars-forward " \t")
3881 (not (eolp)))
3882 (buffer-substring blinkpos
3883 (progn (end-of-line) (point)))
3884 ;; Otherwise show the previous nonblank line,
3885 ;; if there is one.
3886 (if (save-excursion
3887 (skip-chars-backward "\n \t")
3888 (not (bobp)))
3889 (concat
3890 (buffer-substring (progn
3891 (skip-chars-backward "\n \t")
3892 (beginning-of-line)
3893 (point))
3894 (progn (end-of-line)
3895 (skip-chars-backward " \t")
3896 (point)))
3897 ;; Replace the newline and other whitespace with `...'.
3898 "..."
3899 (buffer-substring blinkpos (1+ blinkpos)))
3900 ;; There is nothing to show except the char itself.
3901 (buffer-substring blinkpos (1+ blinkpos))))))))
3902 (cond (mismatch
3903 (message "Mismatched parentheses"))
3904 ((not blink-matching-paren-distance)
3905 (message "Unmatched parenthesis"))))))))
3907 ;Turned off because it makes dbx bomb out.
3908 (setq blink-paren-function 'blink-matching-open)
3910 ;; This executes C-g typed while Emacs is waiting for a command.
3911 ;; Quitting out of a program does not go through here;
3912 ;; that happens in the QUIT macro at the C code level.
3913 (defun keyboard-quit ()
3914 "Signal a `quit' condition.
3915 During execution of Lisp code, this character causes a quit directly.
3916 At top-level, as an editor command, this simply beeps."
3917 (interactive)
3918 (deactivate-mark)
3919 (if (fboundp 'kmacro-keyboard-quit)
3920 (kmacro-keyboard-quit))
3921 (setq defining-kbd-macro nil)
3922 (signal 'quit nil))
3924 (define-key global-map "\C-g" 'keyboard-quit)
3926 (defvar buffer-quit-function nil
3927 "Function to call to \"quit\" the current buffer, or nil if none.
3928 \\[keyboard-escape-quit] calls this function when its more local actions
3929 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3931 (defun keyboard-escape-quit ()
3932 "Exit the current \"mode\" (in a generalized sense of the word).
3933 This command can exit an interactive command such as `query-replace',
3934 can clear out a prefix argument or a region,
3935 can get out of the minibuffer or other recursive edit,
3936 cancel the use of the current buffer (for special-purpose buffers),
3937 or go back to just one window (by deleting all but the selected window)."
3938 (interactive)
3939 (cond ((eq last-command 'mode-exited) nil)
3940 ((> (minibuffer-depth) 0)
3941 (abort-recursive-edit))
3942 (current-prefix-arg
3943 nil)
3944 ((and transient-mark-mode
3945 mark-active)
3946 (deactivate-mark))
3947 ((> (recursion-depth) 0)
3948 (exit-recursive-edit))
3949 (buffer-quit-function
3950 (funcall buffer-quit-function))
3951 ((not (one-window-p t))
3952 (delete-other-windows))
3953 ((string-match "^ \\*" (buffer-name (current-buffer)))
3954 (bury-buffer))))
3956 (defun play-sound-file (file &optional volume device)
3957 "Play sound stored in FILE.
3958 VOLUME and DEVICE correspond to the keywords of the sound
3959 specification for `play-sound'."
3960 (interactive "fPlay sound file: ")
3961 (let ((sound (list :file file)))
3962 (if volume
3963 (plist-put sound :volume volume))
3964 (if device
3965 (plist-put sound :device device))
3966 (push 'sound sound)
3967 (play-sound sound)))
3969 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3971 (defcustom read-mail-command 'rmail
3972 "*Your preference for a mail reading package.
3973 This is used by some keybindings which support reading mail.
3974 See also `mail-user-agent' concerning sending mail."
3975 :type '(choice (function-item rmail)
3976 (function-item gnus)
3977 (function-item mh-rmail)
3978 (function :tag "Other"))
3979 :version "21.1"
3980 :group 'mail)
3982 (defcustom mail-user-agent 'sendmail-user-agent
3983 "*Your preference for a mail composition package.
3984 Various Emacs Lisp packages (e.g. Reporter) require you to compose an
3985 outgoing email message. This variable lets you specify which
3986 mail-sending package you prefer.
3988 Valid values include:
3990 `sendmail-user-agent' -- use the default Emacs Mail package.
3991 See Info node `(emacs)Sending Mail'.
3992 `mh-e-user-agent' -- use the Emacs interface to the MH mail system.
3993 See Info node `(mh-e)'.
3994 `message-user-agent' -- use the Gnus Message package.
3995 See Info node `(message)'.
3996 `gnus-user-agent' -- like `message-user-agent', but with Gnus
3997 paraphernalia, particularly the Gcc: header for
3998 archiving.
4000 Additional valid symbols may be available; check with the author of
4001 your package for details. The function should return non-nil if it
4002 succeeds.
4004 See also `read-mail-command' concerning reading mail."
4005 :type '(radio (function-item :tag "Default Emacs mail"
4006 :format "%t\n"
4007 sendmail-user-agent)
4008 (function-item :tag "Emacs interface to MH"
4009 :format "%t\n"
4010 mh-e-user-agent)
4011 (function-item :tag "Gnus Message package"
4012 :format "%t\n"
4013 message-user-agent)
4014 (function-item :tag "Gnus Message with full Gnus features"
4015 :format "%t\n"
4016 gnus-user-agent)
4017 (function :tag "Other"))
4018 :group 'mail)
4020 (define-mail-user-agent 'sendmail-user-agent
4021 'sendmail-user-agent-compose
4022 'mail-send-and-exit)
4024 (defun rfc822-goto-eoh ()
4025 ;; Go to header delimiter line in a mail message, following RFC822 rules
4026 (goto-char (point-min))
4027 (when (re-search-forward
4028 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
4029 (goto-char (match-beginning 0))))
4031 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
4032 switch-function yank-action
4033 send-actions)
4034 (if switch-function
4035 (let ((special-display-buffer-names nil)
4036 (special-display-regexps nil)
4037 (same-window-buffer-names nil)
4038 (same-window-regexps nil))
4039 (funcall switch-function "*mail*")))
4040 (let ((cc (cdr (assoc-string "cc" other-headers t)))
4041 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
4042 (body (cdr (assoc-string "body" other-headers t))))
4043 (or (mail continue to subject in-reply-to cc yank-action send-actions)
4044 continue
4045 (error "Message aborted"))
4046 (save-excursion
4047 (rfc822-goto-eoh)
4048 (while other-headers
4049 (unless (member-ignore-case (car (car other-headers))
4050 '("in-reply-to" "cc" "body"))
4051 (insert (car (car other-headers)) ": "
4052 (cdr (car other-headers)) "\n"))
4053 (setq other-headers (cdr other-headers)))
4054 (when body
4055 (forward-line 1)
4056 (insert body))
4057 t)))
4059 (define-mail-user-agent 'mh-e-user-agent
4060 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
4061 'mh-before-send-letter-hook)
4063 (defun compose-mail (&optional to subject other-headers continue
4064 switch-function yank-action send-actions)
4065 "Start composing a mail message to send.
4066 This uses the user's chosen mail composition package
4067 as selected with the variable `mail-user-agent'.
4068 The optional arguments TO and SUBJECT specify recipients
4069 and the initial Subject field, respectively.
4071 OTHER-HEADERS is an alist specifying additional
4072 header fields. Elements look like (HEADER . VALUE) where both
4073 HEADER and VALUE are strings.
4075 CONTINUE, if non-nil, says to continue editing a message already
4076 being composed.
4078 SWITCH-FUNCTION, if non-nil, is a function to use to
4079 switch to and display the buffer used for mail composition.
4081 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
4082 to insert the raw text of the message being replied to.
4083 It has the form (FUNCTION . ARGS). The user agent will apply
4084 FUNCTION to ARGS, to insert the raw text of the original message.
4085 \(The user agent will also run `mail-citation-hook', *after* the
4086 original text has been inserted in this way.)
4088 SEND-ACTIONS is a list of actions to call when the message is sent.
4089 Each action has the form (FUNCTION . ARGS)."
4090 (interactive
4091 (list nil nil nil current-prefix-arg))
4092 (let ((function (get mail-user-agent 'composefunc)))
4093 (funcall function to subject other-headers continue
4094 switch-function yank-action send-actions)))
4096 (defun compose-mail-other-window (&optional to subject other-headers continue
4097 yank-action send-actions)
4098 "Like \\[compose-mail], but edit the outgoing message in another window."
4099 (interactive
4100 (list nil nil nil current-prefix-arg))
4101 (compose-mail to subject other-headers continue
4102 'switch-to-buffer-other-window yank-action send-actions))
4105 (defun compose-mail-other-frame (&optional to subject other-headers continue
4106 yank-action send-actions)
4107 "Like \\[compose-mail], but edit the outgoing message in another frame."
4108 (interactive
4109 (list nil nil nil current-prefix-arg))
4110 (compose-mail to subject other-headers continue
4111 'switch-to-buffer-other-frame yank-action send-actions))
4113 (defvar set-variable-value-history nil
4114 "History of values entered with `set-variable'.")
4116 (defun set-variable (var val &optional make-local)
4117 "Set VARIABLE to VALUE. VALUE is a Lisp object.
4118 When using this interactively, enter a Lisp object for VALUE.
4119 If you want VALUE to be a string, you must surround it with doublequotes.
4120 VALUE is used literally, not evaluated.
4122 If VARIABLE has a `variable-interactive' property, that is used as if
4123 it were the arg to `interactive' (which see) to interactively read VALUE.
4125 If VARIABLE has been defined with `defcustom', then the type information
4126 in the definition is used to check that VALUE is valid.
4128 With a prefix argument, set VARIABLE to VALUE buffer-locally."
4129 (interactive
4130 (let* ((default-var (variable-at-point))
4131 (var (if (symbolp default-var)
4132 (read-variable (format "Set variable (default %s): " default-var)
4133 default-var)
4134 (read-variable "Set variable: ")))
4135 (minibuffer-help-form '(describe-variable var))
4136 (prop (get var 'variable-interactive))
4137 (prompt (format "Set %s%s to value: " var
4138 (cond ((local-variable-p var)
4139 " (buffer-local)")
4140 ((or current-prefix-arg
4141 (local-variable-if-set-p var))
4142 " buffer-locally")
4143 (t " globally"))))
4144 (val (if prop
4145 ;; Use VAR's `variable-interactive' property
4146 ;; as an interactive spec for prompting.
4147 (call-interactively `(lambda (arg)
4148 (interactive ,prop)
4149 arg))
4150 (read
4151 (read-string prompt nil
4152 'set-variable-value-history)))))
4153 (list var val current-prefix-arg)))
4155 (and (custom-variable-p var)
4156 (not (get var 'custom-type))
4157 (custom-load-symbol var))
4158 (let ((type (get var 'custom-type)))
4159 (when type
4160 ;; Match with custom type.
4161 (require 'cus-edit)
4162 (setq type (widget-convert type))
4163 (unless (widget-apply type :match val)
4164 (error "Value `%S' does not match type %S of %S"
4165 val (car type) var))))
4167 (if make-local
4168 (make-local-variable var))
4170 (set var val)
4172 ;; Force a thorough redisplay for the case that the variable
4173 ;; has an effect on the display, like `tab-width' has.
4174 (force-mode-line-update))
4176 ;; Define the major mode for lists of completions.
4178 (defvar completion-list-mode-map nil
4179 "Local map for completion list buffers.")
4180 (or completion-list-mode-map
4181 (let ((map (make-sparse-keymap)))
4182 (define-key map [mouse-2] 'mouse-choose-completion)
4183 (define-key map [down-mouse-2] nil)
4184 (define-key map "\C-m" 'choose-completion)
4185 (define-key map "\e\e\e" 'delete-completion-window)
4186 (define-key map [left] 'previous-completion)
4187 (define-key map [right] 'next-completion)
4188 (setq completion-list-mode-map map)))
4190 ;; Completion mode is suitable only for specially formatted data.
4191 (put 'completion-list-mode 'mode-class 'special)
4193 (defvar completion-reference-buffer nil
4194 "Record the buffer that was current when the completion list was requested.
4195 This is a local variable in the completion list buffer.
4196 Initial value is nil to avoid some compiler warnings.")
4198 (defvar completion-no-auto-exit nil
4199 "Non-nil means `choose-completion-string' should never exit the minibuffer.
4200 This also applies to other functions such as `choose-completion'
4201 and `mouse-choose-completion'.")
4203 (defvar completion-base-size nil
4204 "Number of chars at beginning of minibuffer not involved in completion.
4205 This is a local variable in the completion list buffer
4206 but it talks about the buffer in `completion-reference-buffer'.
4207 If this is nil, it means to compare text to determine which part
4208 of the tail end of the buffer's text is involved in completion.")
4210 (defun delete-completion-window ()
4211 "Delete the completion list window.
4212 Go to the window from which completion was requested."
4213 (interactive)
4214 (let ((buf completion-reference-buffer))
4215 (if (one-window-p t)
4216 (if (window-dedicated-p (selected-window))
4217 (delete-frame (selected-frame)))
4218 (delete-window (selected-window))
4219 (if (get-buffer-window buf)
4220 (select-window (get-buffer-window buf))))))
4222 (defun previous-completion (n)
4223 "Move to the previous item in the completion list."
4224 (interactive "p")
4225 (next-completion (- n)))
4227 (defun next-completion (n)
4228 "Move to the next item in the completion list.
4229 With prefix argument N, move N items (negative N means move backward)."
4230 (interactive "p")
4231 (let ((beg (point-min)) (end (point-max)))
4232 (while (and (> n 0) (not (eobp)))
4233 ;; If in a completion, move to the end of it.
4234 (when (get-text-property (point) 'mouse-face)
4235 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4236 ;; Move to start of next one.
4237 (unless (get-text-property (point) 'mouse-face)
4238 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
4239 (setq n (1- n)))
4240 (while (and (< n 0) (not (bobp)))
4241 (let ((prop (get-text-property (1- (point)) 'mouse-face)))
4242 ;; If in a completion, move to the start of it.
4243 (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
4244 (goto-char (previous-single-property-change
4245 (point) 'mouse-face nil beg)))
4246 ;; Move to end of the previous completion.
4247 (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face))
4248 (goto-char (previous-single-property-change
4249 (point) 'mouse-face nil beg)))
4250 ;; Move to the start of that one.
4251 (goto-char (previous-single-property-change
4252 (point) 'mouse-face nil beg))
4253 (setq n (1+ n))))))
4255 (defun choose-completion ()
4256 "Choose the completion that point is in or next to."
4257 (interactive)
4258 (let (beg end completion (buffer completion-reference-buffer)
4259 (base-size completion-base-size))
4260 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
4261 (setq end (point) beg (1+ (point))))
4262 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
4263 (setq end (1- (point)) beg (point)))
4264 (if (null beg)
4265 (error "No completion here"))
4266 (setq beg (previous-single-property-change beg 'mouse-face))
4267 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
4268 (setq completion (buffer-substring beg end))
4269 (let ((owindow (selected-window)))
4270 (if (and (one-window-p t 'selected-frame)
4271 (window-dedicated-p (selected-window)))
4272 ;; This is a special buffer's frame
4273 (iconify-frame (selected-frame))
4274 (or (window-dedicated-p (selected-window))
4275 (bury-buffer)))
4276 (select-window owindow))
4277 (choose-completion-string completion buffer base-size)))
4279 ;; Delete the longest partial match for STRING
4280 ;; that can be found before POINT.
4281 (defun choose-completion-delete-max-match (string)
4282 (let ((opoint (point))
4283 len)
4284 ;; Try moving back by the length of the string.
4285 (goto-char (max (- (point) (length string))
4286 (minibuffer-prompt-end)))
4287 ;; See how far back we were actually able to move. That is the
4288 ;; upper bound on how much we can match and delete.
4289 (setq len (- opoint (point)))
4290 (if completion-ignore-case
4291 (setq string (downcase string)))
4292 (while (and (> len 0)
4293 (let ((tail (buffer-substring (point) opoint)))
4294 (if completion-ignore-case
4295 (setq tail (downcase tail)))
4296 (not (string= tail (substring string 0 len)))))
4297 (setq len (1- len))
4298 (forward-char 1))
4299 (delete-char len)))
4301 (defvar choose-completion-string-functions nil
4302 "Functions that may override the normal insertion of a completion choice.
4303 These functions are called in order with four arguments:
4304 CHOICE - the string to insert in the buffer,
4305 BUFFER - the buffer in which the choice should be inserted,
4306 MINI-P - non-nil iff BUFFER is a minibuffer, and
4307 BASE-SIZE - the number of characters in BUFFER before
4308 the string being completed.
4310 If a function in the list returns non-nil, that function is supposed
4311 to have inserted the CHOICE in the BUFFER, and possibly exited
4312 the minibuffer; no further functions will be called.
4314 If all functions in the list return nil, that means to use
4315 the default method of inserting the completion in BUFFER.")
4317 (defun choose-completion-string (choice &optional buffer base-size)
4318 "Switch to BUFFER and insert the completion choice CHOICE.
4319 BASE-SIZE, if non-nil, says how many characters of BUFFER's text
4320 to keep. If it is nil, we call `choose-completion-delete-max-match'
4321 to decide what to delete."
4323 ;; If BUFFER is the minibuffer, exit the minibuffer
4324 ;; unless it is reading a file name and CHOICE is a directory,
4325 ;; or completion-no-auto-exit is non-nil.
4327 (let* ((buffer (or buffer completion-reference-buffer))
4328 (mini-p (minibufferp buffer)))
4329 ;; If BUFFER is a minibuffer, barf unless it's the currently
4330 ;; active minibuffer.
4331 (if (and mini-p
4332 (or (not (active-minibuffer-window))
4333 (not (equal buffer
4334 (window-buffer (active-minibuffer-window))))))
4335 (error "Minibuffer is not active for completion")
4336 ;; Set buffer so buffer-local choose-completion-string-functions works.
4337 (set-buffer buffer)
4338 (unless (run-hook-with-args-until-success
4339 'choose-completion-string-functions
4340 choice buffer mini-p base-size)
4341 ;; Insert the completion into the buffer where it was requested.
4342 (if base-size
4343 (delete-region (+ base-size (if mini-p
4344 (minibuffer-prompt-end)
4345 (point-min)))
4346 (point))
4347 (choose-completion-delete-max-match choice))
4348 (insert choice)
4349 (remove-text-properties (- (point) (length choice)) (point)
4350 '(mouse-face nil))
4351 ;; Update point in the window that BUFFER is showing in.
4352 (let ((window (get-buffer-window buffer t)))
4353 (set-window-point window (point)))
4354 ;; If completing for the minibuffer, exit it with this choice.
4355 (and (not completion-no-auto-exit)
4356 (equal buffer (window-buffer (minibuffer-window)))
4357 minibuffer-completion-table
4358 ;; If this is reading a file name, and the file name chosen
4359 ;; is a directory, don't exit the minibuffer.
4360 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
4361 (file-directory-p (field-string (point-max))))
4362 (let ((mini (active-minibuffer-window)))
4363 (select-window mini)
4364 (when minibuffer-auto-raise
4365 (raise-frame (window-frame mini))))
4366 (exit-minibuffer)))))))
4368 (defun completion-list-mode ()
4369 "Major mode for buffers showing lists of possible completions.
4370 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
4371 to select the completion near point.
4372 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
4373 with the mouse."
4374 (interactive)
4375 (kill-all-local-variables)
4376 (use-local-map completion-list-mode-map)
4377 (setq mode-name "Completion List")
4378 (setq major-mode 'completion-list-mode)
4379 (make-local-variable 'completion-base-size)
4380 (setq completion-base-size nil)
4381 (run-hooks 'completion-list-mode-hook))
4383 (defun completion-list-mode-finish ()
4384 "Finish setup of the completions buffer.
4385 Called from `temp-buffer-show-hook'."
4386 (when (eq major-mode 'completion-list-mode)
4387 (toggle-read-only 1)))
4389 (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
4391 (defvar completion-setup-hook nil
4392 "Normal hook run at the end of setting up a completion list buffer.
4393 When this hook is run, the current buffer is the one in which the
4394 command to display the completion list buffer was run.
4395 The completion list buffer is available as the value of `standard-output'.")
4397 ;; This function goes in completion-setup-hook, so that it is called
4398 ;; after the text of the completion list buffer is written.
4399 (defface completions-first-difference
4400 '((t (:inherit bold)))
4401 "Face put on the first uncommon character in completions in *Completions* buffer."
4402 :group 'completion)
4404 (defface completions-common-part
4405 '((t (:inherit default)))
4406 "Face put on the common prefix substring in completions in *Completions* buffer.
4407 The idea of `completions-common-part' is that you can use it to
4408 make the common parts less visible than normal, so that the rest
4409 of the differing parts is, by contrast, slightly highlighted."
4410 :group 'completion)
4412 ;; This is for packages that need to bind it to a non-default regexp
4413 ;; in order to make the first-differing character highlight work
4414 ;; to their liking
4415 (defvar completion-root-regexp "^/"
4416 "Regexp to use in `completion-setup-function' to find the root directory.")
4418 (defun completion-setup-function ()
4419 (let ((mainbuf (current-buffer))
4420 (mbuf-contents (minibuffer-contents)))
4421 ;; When reading a file name in the minibuffer,
4422 ;; set default-directory in the minibuffer
4423 ;; so it will get copied into the completion list buffer.
4424 (if minibuffer-completing-file-name
4425 (with-current-buffer mainbuf
4426 (setq default-directory (file-name-directory mbuf-contents))))
4427 ;; If partial-completion-mode is on, point might not be after the
4428 ;; last character in the minibuffer.
4429 ;; FIXME: This still doesn't work if the text to be completed
4430 ;; starts with a `-'.
4431 (when (and partial-completion-mode (not (eobp)))
4432 (setq mbuf-contents
4433 (substring mbuf-contents 0 (- (point) (point-max)))))
4434 (with-current-buffer standard-output
4435 (completion-list-mode)
4436 (make-local-variable 'completion-reference-buffer)
4437 (setq completion-reference-buffer mainbuf)
4438 (if minibuffer-completing-file-name
4439 ;; For file name completion,
4440 ;; use the number of chars before the start of the
4441 ;; last file name component.
4442 (setq completion-base-size
4443 (with-current-buffer mainbuf
4444 (save-excursion
4445 (goto-char (point-max))
4446 (skip-chars-backward completion-root-regexp)
4447 (- (point) (minibuffer-prompt-end)))))
4448 ;; Otherwise, in minibuffer, the whole input is being completed.
4449 (if (minibufferp mainbuf)
4450 (setq completion-base-size 0)))
4451 ;; Put faces on first uncommon characters and common parts.
4452 (when completion-base-size
4453 (let* ((common-string-length
4454 (- (length mbuf-contents) completion-base-size))
4455 (element-start (next-single-property-change
4456 (point-min)
4457 'mouse-face))
4458 (element-common-end
4459 (+ (or element-start nil) common-string-length))
4460 (maxp (point-max)))
4461 (while (and element-start (< element-common-end maxp))
4462 (when (and (get-char-property element-start 'mouse-face)
4463 (get-char-property element-common-end 'mouse-face))
4464 (put-text-property element-start element-common-end
4465 'font-lock-face 'completions-common-part)
4466 (put-text-property element-common-end (1+ element-common-end)
4467 'font-lock-face 'completions-first-difference))
4468 (setq element-start (next-single-property-change
4469 element-start
4470 'mouse-face))
4471 (if element-start
4472 (setq element-common-end (+ element-start common-string-length))))))
4473 ;; Insert help string.
4474 (goto-char (point-min))
4475 (if (display-mouse-p)
4476 (insert (substitute-command-keys
4477 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
4478 (insert (substitute-command-keys
4479 "In this buffer, type \\[choose-completion] to \
4480 select the completion near point.\n\n")))))
4482 (add-hook 'completion-setup-hook 'completion-setup-function)
4484 (define-key minibuffer-local-completion-map [prior]
4485 'switch-to-completions)
4486 (define-key minibuffer-local-must-match-map [prior]
4487 'switch-to-completions)
4488 (define-key minibuffer-local-completion-map "\M-v"
4489 'switch-to-completions)
4490 (define-key minibuffer-local-must-match-map "\M-v"
4491 'switch-to-completions)
4493 (defun switch-to-completions ()
4494 "Select the completion list window."
4495 (interactive)
4496 ;; Make sure we have a completions window.
4497 (or (get-buffer-window "*Completions*")
4498 (minibuffer-completion-help))
4499 (let ((window (get-buffer-window "*Completions*")))
4500 (when window
4501 (select-window window)
4502 (goto-char (point-min))
4503 (search-forward "\n\n")
4504 (forward-line 1))))
4506 ;; Support keyboard commands to turn on various modifiers.
4508 ;; These functions -- which are not commands -- each add one modifier
4509 ;; to the following event.
4511 (defun event-apply-alt-modifier (ignore-prompt)
4512 "\\<function-key-map>Add the Alt modifier to the following event.
4513 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
4514 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
4515 (defun event-apply-super-modifier (ignore-prompt)
4516 "\\<function-key-map>Add the Super modifier to the following event.
4517 For example, type \\[event-apply-super-modifier] & to enter Super-&."
4518 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
4519 (defun event-apply-hyper-modifier (ignore-prompt)
4520 "\\<function-key-map>Add the Hyper modifier to the following event.
4521 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
4522 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
4523 (defun event-apply-shift-modifier (ignore-prompt)
4524 "\\<function-key-map>Add the Shift modifier to the following event.
4525 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
4526 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
4527 (defun event-apply-control-modifier (ignore-prompt)
4528 "\\<function-key-map>Add the Ctrl modifier to the following event.
4529 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
4530 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
4531 (defun event-apply-meta-modifier (ignore-prompt)
4532 "\\<function-key-map>Add the Meta modifier to the following event.
4533 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
4534 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
4536 (defun event-apply-modifier (event symbol lshiftby prefix)
4537 "Apply a modifier flag to event EVENT.
4538 SYMBOL is the name of this modifier, as a symbol.
4539 LSHIFTBY is the numeric value of this modifier, in keyboard events.
4540 PREFIX is the string that represents this modifier in an event type symbol."
4541 (if (numberp event)
4542 (cond ((eq symbol 'control)
4543 (if (and (<= (downcase event) ?z)
4544 (>= (downcase event) ?a))
4545 (- (downcase event) ?a -1)
4546 (if (and (<= (downcase event) ?Z)
4547 (>= (downcase event) ?A))
4548 (- (downcase event) ?A -1)
4549 (logior (lsh 1 lshiftby) event))))
4550 ((eq symbol 'shift)
4551 (if (and (<= (downcase event) ?z)
4552 (>= (downcase event) ?a))
4553 (upcase event)
4554 (logior (lsh 1 lshiftby) event)))
4556 (logior (lsh 1 lshiftby) event)))
4557 (if (memq symbol (event-modifiers event))
4558 event
4559 (let ((event-type (if (symbolp event) event (car event))))
4560 (setq event-type (intern (concat prefix (symbol-name event-type))))
4561 (if (symbolp event)
4562 event-type
4563 (cons event-type (cdr event)))))))
4565 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4566 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4567 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4568 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4569 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4570 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4572 ;;;; Keypad support.
4574 ;;; Make the keypad keys act like ordinary typing keys. If people add
4575 ;;; bindings for the function key symbols, then those bindings will
4576 ;;; override these, so this shouldn't interfere with any existing
4577 ;;; bindings.
4579 ;; Also tell read-char how to handle these keys.
4580 (mapc
4581 (lambda (keypad-normal)
4582 (let ((keypad (nth 0 keypad-normal))
4583 (normal (nth 1 keypad-normal)))
4584 (put keypad 'ascii-character normal)
4585 (define-key function-key-map (vector keypad) (vector normal))))
4586 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4587 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4588 (kp-space ?\ )
4589 (kp-tab ?\t)
4590 (kp-enter ?\r)
4591 (kp-multiply ?*)
4592 (kp-add ?+)
4593 (kp-separator ?,)
4594 (kp-subtract ?-)
4595 (kp-decimal ?.)
4596 (kp-divide ?/)
4597 (kp-equal ?=)))
4599 ;;;;
4600 ;;;; forking a twin copy of a buffer.
4601 ;;;;
4603 (defvar clone-buffer-hook nil
4604 "Normal hook to run in the new buffer at the end of `clone-buffer'.")
4606 (defun clone-process (process &optional newname)
4607 "Create a twin copy of PROCESS.
4608 If NEWNAME is nil, it defaults to PROCESS' name;
4609 NEWNAME is modified by adding or incrementing <N> at the end as necessary.
4610 If PROCESS is associated with a buffer, the new process will be associated
4611 with the current buffer instead.
4612 Returns nil if PROCESS has already terminated."
4613 (setq newname (or newname (process-name process)))
4614 (if (string-match "<[0-9]+>\\'" newname)
4615 (setq newname (substring newname 0 (match-beginning 0))))
4616 (when (memq (process-status process) '(run stop open))
4617 (let* ((process-connection-type (process-tty-name process))
4618 (new-process
4619 (if (memq (process-status process) '(open))
4620 (let ((args (process-contact process t)))
4621 (setq args (plist-put args :name newname))
4622 (setq args (plist-put args :buffer
4623 (if (process-buffer process)
4624 (current-buffer))))
4625 (apply 'make-network-process args))
4626 (apply 'start-process newname
4627 (if (process-buffer process) (current-buffer))
4628 (process-command process)))))
4629 (set-process-query-on-exit-flag
4630 new-process (process-query-on-exit-flag process))
4631 (set-process-inherit-coding-system-flag
4632 new-process (process-inherit-coding-system-flag process))
4633 (set-process-filter new-process (process-filter process))
4634 (set-process-sentinel new-process (process-sentinel process))
4635 (set-process-plist new-process (copy-sequence (process-plist process)))
4636 new-process)))
4638 ;; things to maybe add (currently partly covered by `funcall mode'):
4639 ;; - syntax-table
4640 ;; - overlays
4641 (defun clone-buffer (&optional newname display-flag)
4642 "Create and return a twin copy of the current buffer.
4643 Unlike an indirect buffer, the new buffer can be edited
4644 independently of the old one (if it is not read-only).
4645 NEWNAME is the name of the new buffer. It may be modified by
4646 adding or incrementing <N> at the end as necessary to create a
4647 unique buffer name. If nil, it defaults to the name of the
4648 current buffer, with the proper suffix. If DISPLAY-FLAG is
4649 non-nil, the new buffer is shown with `pop-to-buffer'. Trying to
4650 clone a file-visiting buffer, or a buffer whose major mode symbol
4651 has a non-nil `no-clone' property, results in an error.
4653 Interactively, DISPLAY-FLAG is t and NEWNAME is the name of the
4654 current buffer with appropriate suffix. However, if a prefix
4655 argument is given, then the command prompts for NEWNAME in the
4656 minibuffer.
4658 This runs the normal hook `clone-buffer-hook' in the new buffer
4659 after it has been set up properly in other respects."
4660 (interactive
4661 (progn
4662 (if buffer-file-name
4663 (error "Cannot clone a file-visiting buffer"))
4664 (if (get major-mode 'no-clone)
4665 (error "Cannot clone a buffer in %s mode" mode-name))
4666 (list (if current-prefix-arg (read-string "Name: "))
4667 t)))
4668 (if buffer-file-name
4669 (error "Cannot clone a file-visiting buffer"))
4670 (if (get major-mode 'no-clone)
4671 (error "Cannot clone a buffer in %s mode" mode-name))
4672 (setq newname (or newname (buffer-name)))
4673 (if (string-match "<[0-9]+>\\'" newname)
4674 (setq newname (substring newname 0 (match-beginning 0))))
4675 (let ((buf (current-buffer))
4676 (ptmin (point-min))
4677 (ptmax (point-max))
4678 (pt (point))
4679 (mk (if mark-active (mark t)))
4680 (modified (buffer-modified-p))
4681 (mode major-mode)
4682 (lvars (buffer-local-variables))
4683 (process (get-buffer-process (current-buffer)))
4684 (new (generate-new-buffer (or newname (buffer-name)))))
4685 (save-restriction
4686 (widen)
4687 (with-current-buffer new
4688 (insert-buffer-substring buf)))
4689 (with-current-buffer new
4690 (narrow-to-region ptmin ptmax)
4691 (goto-char pt)
4692 (if mk (set-mark mk))
4693 (set-buffer-modified-p modified)
4695 ;; Clone the old buffer's process, if any.
4696 (when process (clone-process process))
4698 ;; Now set up the major mode.
4699 (funcall mode)
4701 ;; Set up other local variables.
4702 (mapcar (lambda (v)
4703 (condition-case () ;in case var is read-only
4704 (if (symbolp v)
4705 (makunbound v)
4706 (set (make-local-variable (car v)) (cdr v)))
4707 (error nil)))
4708 lvars)
4710 ;; Run any hooks (typically set up by the major mode
4711 ;; for cloning to work properly).
4712 (run-hooks 'clone-buffer-hook))
4713 (if display-flag (pop-to-buffer new))
4714 new))
4717 (defun clone-indirect-buffer (newname display-flag &optional norecord)
4718 "Create an indirect buffer that is a twin copy of the current buffer.
4720 Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME
4721 from the minibuffer when invoked with a prefix arg. If NEWNAME is nil
4722 or if not called with a prefix arg, NEWNAME defaults to the current
4723 buffer's name. The name is modified by adding a `<N>' suffix to it
4724 or by incrementing the N in an existing suffix.
4726 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
4727 This is always done when called interactively.
4729 Optional last arg NORECORD non-nil means do not put this buffer at the
4730 front of the list of recently selected ones."
4731 (interactive
4732 (progn
4733 (if (get major-mode 'no-clone-indirect)
4734 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4735 (list (if current-prefix-arg
4736 (read-string "BName of indirect buffer: "))
4737 t)))
4738 (if (get major-mode 'no-clone-indirect)
4739 (error "Cannot indirectly clone a buffer in %s mode" mode-name))
4740 (setq newname (or newname (buffer-name)))
4741 (if (string-match "<[0-9]+>\\'" newname)
4742 (setq newname (substring newname 0 (match-beginning 0))))
4743 (let* ((name (generate-new-buffer-name newname))
4744 (buffer (make-indirect-buffer (current-buffer) name t)))
4745 (when display-flag
4746 (pop-to-buffer buffer norecord))
4747 buffer))
4750 (defun clone-indirect-buffer-other-window (buffer &optional norecord)
4751 "Create an indirect buffer that is a twin copy of BUFFER.
4752 Select the new buffer in another window.
4753 Optional second arg NORECORD non-nil means do not put this buffer at
4754 the front of the list of recently selected ones."
4755 (interactive "bClone buffer in other window: ")
4756 (let ((pop-up-windows t))
4757 (set-buffer buffer)
4758 (clone-indirect-buffer nil t norecord)))
4760 (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window)
4762 ;;; Handling of Backspace and Delete keys.
4764 (defcustom normal-erase-is-backspace nil
4765 "If non-nil, Delete key deletes forward and Backspace key deletes backward.
4767 On window systems, the default value of this option is chosen
4768 according to the keyboard used. If the keyboard has both a Backspace
4769 key and a Delete key, and both are mapped to their usual meanings, the
4770 option's default value is set to t, so that Backspace can be used to
4771 delete backward, and Delete can be used to delete forward.
4773 If not running under a window system, customizing this option accomplishes
4774 a similar effect by mapping C-h, which is usually generated by the
4775 Backspace key, to DEL, and by mapping DEL to C-d via
4776 `keyboard-translate'. The former functionality of C-h is available on
4777 the F1 key. You should probably not use this setting if you don't
4778 have both Backspace, Delete and F1 keys.
4780 Setting this variable with setq doesn't take effect. Programmatically,
4781 call `normal-erase-is-backspace-mode' (which see) instead."
4782 :type 'boolean
4783 :group 'editing-basics
4784 :version "21.1"
4785 :set (lambda (symbol value)
4786 ;; The fboundp is because of a problem with :set when
4787 ;; dumping Emacs. It doesn't really matter.
4788 (if (fboundp 'normal-erase-is-backspace-mode)
4789 (normal-erase-is-backspace-mode (or value 0))
4790 (set-default symbol value))))
4793 (defun normal-erase-is-backspace-mode (&optional arg)
4794 "Toggle the Erase and Delete mode of the Backspace and Delete keys.
4796 With numeric arg, turn the mode on if and only if ARG is positive.
4798 On window systems, when this mode is on, Delete is mapped to C-d and
4799 Backspace is mapped to DEL; when this mode is off, both Delete and
4800 Backspace are mapped to DEL. (The remapping goes via
4801 `function-key-map', so binding Delete or Backspace in the global or
4802 local keymap will override that.)
4804 In addition, on window systems, the bindings of C-Delete, M-Delete,
4805 C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
4806 the global keymap in accordance with the functionality of Delete and
4807 Backspace. For example, if Delete is remapped to C-d, which deletes
4808 forward, C-Delete is bound to `kill-word', but if Delete is remapped
4809 to DEL, which deletes backward, C-Delete is bound to
4810 `backward-kill-word'.
4812 If not running on a window system, a similar effect is accomplished by
4813 remapping C-h (normally produced by the Backspace key) and DEL via
4814 `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
4815 to C-d; if it's off, the keys are not remapped.
4817 When not running on a window system, and this mode is turned on, the
4818 former functionality of C-h is available on the F1 key. You should
4819 probably not turn on this mode on a text-only terminal if you don't
4820 have both Backspace, Delete and F1 keys.
4822 See also `normal-erase-is-backspace'."
4823 (interactive "P")
4824 (setq normal-erase-is-backspace
4825 (if arg
4826 (> (prefix-numeric-value arg) 0)
4827 (not normal-erase-is-backspace)))
4829 (cond ((or (memq window-system '(x w32 mac pc))
4830 (memq system-type '(ms-dos windows-nt)))
4831 (let ((bindings
4832 `(([C-delete] [C-backspace])
4833 ([M-delete] [M-backspace])
4834 ([C-M-delete] [C-M-backspace])
4835 (,esc-map
4836 [C-delete] [C-backspace])))
4837 (old-state (lookup-key function-key-map [delete])))
4839 (if normal-erase-is-backspace
4840 (progn
4841 (define-key function-key-map [delete] [?\C-d])
4842 (define-key function-key-map [kp-delete] [?\C-d])
4843 (define-key function-key-map [backspace] [?\C-?]))
4844 (define-key function-key-map [delete] [?\C-?])
4845 (define-key function-key-map [kp-delete] [?\C-?])
4846 (define-key function-key-map [backspace] [?\C-?]))
4848 ;; Maybe swap bindings of C-delete and C-backspace, etc.
4849 (unless (equal old-state (lookup-key function-key-map [delete]))
4850 (dolist (binding bindings)
4851 (let ((map global-map))
4852 (when (keymapp (car binding))
4853 (setq map (car binding) binding (cdr binding)))
4854 (let* ((key1 (nth 0 binding))
4855 (key2 (nth 1 binding))
4856 (binding1 (lookup-key map key1))
4857 (binding2 (lookup-key map key2)))
4858 (define-key map key1 binding2)
4859 (define-key map key2 binding1)))))))
4861 (if normal-erase-is-backspace
4862 (progn
4863 (keyboard-translate ?\C-h ?\C-?)
4864 (keyboard-translate ?\C-? ?\C-d))
4865 (keyboard-translate ?\C-h ?\C-h)
4866 (keyboard-translate ?\C-? ?\C-?))))
4868 (run-hooks 'normal-erase-is-backspace-hook)
4869 (if (interactive-p)
4870 (message "Delete key deletes %s"
4871 (if normal-erase-is-backspace "forward" "backward"))))
4873 (defcustom idle-update-delay 0.5
4874 "*Idle time delay before updating various things on the screen.
4875 Various Emacs features that update auxiliary information when point moves
4876 wait this many seconds after Emacs becomes idle before doing an update."
4877 :type 'number
4878 :group 'display
4879 :version "21.4")
4881 (defvar vis-mode-saved-buffer-invisibility-spec nil
4882 "Saved value of `buffer-invisibility-spec' when Visible mode is on.")
4884 (define-minor-mode visible-mode
4885 "Toggle Visible mode.
4886 With argument ARG turn Visible mode on iff ARG is positive.
4888 Enabling Visible mode makes all invisible text temporarily visible.
4889 Disabling Visible mode turns off that effect. Visible mode
4890 works by saving the value of `buffer-invisibility-spec' and setting it to nil."
4891 :lighter " Vis"
4892 (when (local-variable-p 'vis-mode-saved-buffer-invisibility-spec)
4893 (setq buffer-invisibility-spec vis-mode-saved-buffer-invisibility-spec)
4894 (kill-local-variable 'vis-mode-saved-buffer-invisibility-spec))
4895 (when visible-mode
4896 (set (make-local-variable 'vis-mode-saved-buffer-invisibility-spec)
4897 buffer-invisibility-spec)
4898 (setq buffer-invisibility-spec nil)))
4900 ;; Minibuffer prompt stuff.
4902 ;(defun minibuffer-prompt-modification (start end)
4903 ; (error "You cannot modify the prompt"))
4906 ;(defun minibuffer-prompt-insertion (start end)
4907 ; (let ((inhibit-modification-hooks t))
4908 ; (delete-region start end)
4909 ; ;; Discard undo information for the text insertion itself
4910 ; ;; and for the text deletion.above.
4911 ; (when (consp buffer-undo-list)
4912 ; (setq buffer-undo-list (cddr buffer-undo-list)))
4913 ; (message "You cannot modify the prompt")))
4916 ;(setq minibuffer-prompt-properties
4917 ; (list 'modification-hooks '(minibuffer-prompt-modification)
4918 ; 'insert-in-front-hooks '(minibuffer-prompt-insertion)))
4921 (provide 'simple)
4923 ;; arch-tag: 24af67c0-2a49-44f6-b3b1-312d8b570dfd
4924 ;;; simple.el ends here