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