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