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