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