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