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