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