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