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