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