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