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