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