(yank, yank-pop): Clear out read-only prop.
[emacs.git] / lisp / simple.el
blob6349f1058ae74c9c4adc6ee83069cd3bc2e97bdd
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 (let ((opoint (point))
1496 (inhibit-read-only t))
1497 (insert (current-kill arg))
1498 (remove-text-properties opoint (point) '(read-only nil)))
1499 (if before
1500 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1501 ;; It is cleaner to avoid activation, even though the command
1502 ;; loop would deactivate the mark because we inserted text.
1503 (goto-char (prog1 (mark t)
1504 (set-marker (mark-marker) (point) (current-buffer))))))
1505 nil)
1507 (defun yank (&optional arg)
1508 "Reinsert the last stretch of killed text.
1509 More precisely, reinsert the stretch of killed text most recently
1510 killed OR yanked. Put point at end, and set mark at beginning.
1511 With just C-u as argument, same but put point at beginning (and mark at end).
1512 With argument N, reinsert the Nth most recently killed stretch of killed
1513 text.
1514 See also the command \\[yank-pop]."
1515 (interactive "*P")
1516 ;; If we don't get all the way thru, make last-command indicate that
1517 ;; for the following command.
1518 (setq this-command t)
1519 (push-mark (point))
1520 (let ((opoint (point))
1521 (inhibit-read-only t))
1522 (insert (current-kill (cond
1523 ((listp arg) 0)
1524 ((eq arg '-) -1)
1525 (t (1- arg)))))
1526 (remove-text-properties opoint (point) '(read-only nil)))
1527 (if (consp arg)
1528 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1529 ;; It is cleaner to avoid activation, even though the command
1530 ;; loop would deactivate the mark because we inserted text.
1531 (goto-char (prog1 (mark t)
1532 (set-marker (mark-marker) (point) (current-buffer)))))
1533 ;; If we do get all the way thru, make this-command indicate that.
1534 (setq this-command 'yank)
1535 nil)
1537 (defun rotate-yank-pointer (arg)
1538 "Rotate the yanking point in the kill ring.
1539 With argument, rotate that many kills forward (or backward, if negative)."
1540 (interactive "p")
1541 (current-kill arg))
1544 (defun insert-buffer (buffer)
1545 "Insert after point the contents of BUFFER.
1546 Puts mark after the inserted text.
1547 BUFFER may be a buffer or a buffer name."
1548 (interactive
1549 (list
1550 (progn
1551 (barf-if-buffer-read-only)
1552 (read-buffer "Insert buffer: "
1553 (if (eq (selected-window) (next-window (selected-window)))
1554 (other-buffer (current-buffer))
1555 (window-buffer (next-window (selected-window))))
1556 t))))
1557 (or (bufferp buffer)
1558 (setq buffer (get-buffer buffer)))
1559 (let (start end newmark)
1560 (save-excursion
1561 (save-excursion
1562 (set-buffer buffer)
1563 (setq start (point-min) end (point-max)))
1564 (insert-buffer-substring buffer start end)
1565 (setq newmark (point)))
1566 (push-mark newmark))
1567 nil)
1569 (defun append-to-buffer (buffer start end)
1570 "Append to specified buffer the text of the region.
1571 It is inserted into that buffer before its point.
1573 When calling from a program, give three arguments:
1574 BUFFER (or buffer name), START and END.
1575 START and END specify the portion of the current buffer to be copied."
1576 (interactive
1577 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
1578 (region-beginning) (region-end)))
1579 (let ((oldbuf (current-buffer)))
1580 (save-excursion
1581 (set-buffer (get-buffer-create buffer))
1582 (insert-buffer-substring oldbuf start end))))
1584 (defun prepend-to-buffer (buffer start end)
1585 "Prepend to specified buffer the text of the region.
1586 It is inserted into that buffer after its point.
1588 When calling from a program, give three arguments:
1589 BUFFER (or buffer name), START and END.
1590 START and END specify the portion of the current buffer to be copied."
1591 (interactive "BPrepend to buffer: \nr")
1592 (let ((oldbuf (current-buffer)))
1593 (save-excursion
1594 (set-buffer (get-buffer-create buffer))
1595 (save-excursion
1596 (insert-buffer-substring oldbuf start end)))))
1598 (defun copy-to-buffer (buffer start end)
1599 "Copy to specified buffer the text of the region.
1600 It is inserted into that buffer, replacing existing text there.
1602 When calling from a program, give three arguments:
1603 BUFFER (or buffer name), START and END.
1604 START and END specify the portion of the current buffer to be copied."
1605 (interactive "BCopy to buffer: \nr")
1606 (let ((oldbuf (current-buffer)))
1607 (save-excursion
1608 (set-buffer (get-buffer-create buffer))
1609 (erase-buffer)
1610 (save-excursion
1611 (insert-buffer-substring oldbuf start end)))))
1613 (put 'mark-inactive 'error-conditions '(mark-inactive error))
1614 (put 'mark-inactive 'error-message "The mark is not active now")
1616 (defun mark (&optional force)
1617 "Return this buffer's mark value as integer; error if mark inactive.
1618 If optional argument FORCE is non-nil, access the mark value
1619 even if the mark is not currently active, and return nil
1620 if there is no mark at all.
1622 If you are using this in an editing command, you are most likely making
1623 a mistake; see the documentation of `set-mark'."
1624 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
1625 (marker-position (mark-marker))
1626 (signal 'mark-inactive nil)))
1628 ;; Many places set mark-active directly, and several of them failed to also
1629 ;; run deactivate-mark-hook. This shorthand should simplify.
1630 (defsubst deactivate-mark ()
1631 "Deactivate the mark by setting `mark-active' to nil.
1632 \(That makes a difference only in Transient Mark mode.)
1633 Also runs the hook `deactivate-mark-hook'."
1634 (if transient-mark-mode
1635 (progn
1636 (setq mark-active nil)
1637 (run-hooks 'deactivate-mark-hook))))
1639 (defun set-mark (pos)
1640 "Set this buffer's mark to POS. Don't use this function!
1641 That is to say, don't use this function unless you want
1642 the user to see that the mark has moved, and you want the previous
1643 mark position to be lost.
1645 Normally, when a new mark is set, the old one should go on the stack.
1646 This is why most applications should use push-mark, not set-mark.
1648 Novice Emacs Lisp programmers often try to use the mark for the wrong
1649 purposes. The mark saves a location for the user's convenience.
1650 Most editing commands should not alter the mark.
1651 To remember a location for internal use in the Lisp program,
1652 store it in a Lisp variable. Example:
1654 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1656 (if pos
1657 (progn
1658 (setq mark-active t)
1659 (run-hooks 'activate-mark-hook)
1660 (set-marker (mark-marker) pos (current-buffer)))
1661 ;; Normally we never clear mark-active except in Transient Mark mode.
1662 ;; But when we actually clear out the mark value too,
1663 ;; we must clear mark-active in any mode.
1664 (setq mark-active nil)
1665 (run-hooks 'deactivate-mark-hook)
1666 (set-marker (mark-marker) nil)))
1668 (defvar mark-ring nil
1669 "The list of former marks of the current buffer, most recent first.")
1670 (make-variable-buffer-local 'mark-ring)
1671 (put 'mark-ring 'permanent-local t)
1673 (defcustom mark-ring-max 16
1674 "*Maximum size of mark ring. Start discarding off end if gets this big."
1675 :type 'integer
1676 :group 'editing-basics)
1678 (defvar global-mark-ring nil
1679 "The list of saved global marks, most recent first.")
1681 (defcustom global-mark-ring-max 16
1682 "*Maximum size of global mark ring. \
1683 Start discarding off end if gets this big."
1684 :type 'integer
1685 :group 'editing-basics)
1687 (defun set-mark-command (arg)
1688 "Set mark at where point is, or jump to mark.
1689 With no prefix argument, set mark, push old mark position on local mark
1690 ring, and push mark on global mark ring.
1691 With argument, jump to mark, and pop a new position for mark off the ring
1692 \(does not affect global mark ring\).
1694 Novice Emacs Lisp programmers often try to use the mark for the wrong
1695 purposes. See the documentation of `set-mark' for more information."
1696 (interactive "P")
1697 (if (null arg)
1698 (progn
1699 (push-mark nil nil t))
1700 (if (null (mark t))
1701 (error "No mark set in this buffer")
1702 (goto-char (mark t))
1703 (pop-mark))))
1705 (defun push-mark (&optional location nomsg activate)
1706 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1707 If the last global mark pushed was not in the current buffer,
1708 also push LOCATION on the global mark ring.
1709 Display `Mark set' unless the optional second arg NOMSG is non-nil.
1710 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
1712 Novice Emacs Lisp programmers often try to use the mark for the wrong
1713 purposes. See the documentation of `set-mark' for more information.
1715 In Transient Mark mode, this does not activate the mark."
1716 (if (null (mark t))
1718 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1719 (if (> (length mark-ring) mark-ring-max)
1720 (progn
1721 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1722 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
1723 (set-marker (mark-marker) (or location (point)) (current-buffer))
1724 ;; Now push the mark on the global mark ring.
1725 (if (and global-mark-ring
1726 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
1727 ;; The last global mark pushed was in this same buffer.
1728 ;; Don't push another one.
1730 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
1731 (if (> (length global-mark-ring) global-mark-ring-max)
1732 (progn
1733 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
1734 nil)
1735 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
1736 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
1737 (message "Mark set"))
1738 (if (or activate (not transient-mark-mode))
1739 (set-mark (mark t)))
1740 nil)
1742 (defun pop-mark ()
1743 "Pop off mark ring into the buffer's actual mark.
1744 Does not set point. Does nothing if mark ring is empty."
1745 (if mark-ring
1746 (progn
1747 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
1748 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
1749 (deactivate-mark)
1750 (move-marker (car mark-ring) nil)
1751 (if (null (mark t)) (ding))
1752 (setq mark-ring (cdr mark-ring)))))
1754 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
1755 (defun exchange-point-and-mark ()
1756 "Put the mark where point is now, and point where the mark is now.
1757 This command works even when the mark is not active,
1758 and it reactivates the mark."
1759 (interactive nil)
1760 (let ((omark (mark t)))
1761 (if (null omark)
1762 (error "No mark set in this buffer"))
1763 (set-mark (point))
1764 (goto-char omark)
1765 nil))
1767 (defun transient-mark-mode (arg)
1768 "Toggle Transient Mark mode.
1769 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
1771 In Transient Mark mode, when the mark is active, the region is highlighted.
1772 Changing the buffer \"deactivates\" the mark.
1773 So do certain other operations that set the mark
1774 but whose main purpose is something else--for example,
1775 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
1776 (interactive "P")
1777 (setq transient-mark-mode
1778 (if (null arg)
1779 (not transient-mark-mode)
1780 (> (prefix-numeric-value arg) 0)))
1781 (if (interactive-p)
1782 (if transient-mark-mode
1783 (message "Transient Mark mode enabled")
1784 (message "Transient Mark mode disabled"))))
1786 (defun pop-global-mark ()
1787 "Pop off global mark ring and jump to the top location."
1788 (interactive)
1789 ;; Pop entries which refer to non-existent buffers.
1790 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
1791 (setq global-mark-ring (cdr global-mark-ring)))
1792 (or global-mark-ring
1793 (error "No global mark set"))
1794 (let* ((marker (car global-mark-ring))
1795 (buffer (marker-buffer marker))
1796 (position (marker-position marker)))
1797 (setq global-mark-ring (nconc (cdr global-mark-ring)
1798 (list (car global-mark-ring))))
1799 (set-buffer buffer)
1800 (or (and (>= position (point-min))
1801 (<= position (point-max)))
1802 (widen))
1803 (goto-char position)
1804 (switch-to-buffer buffer)))
1806 (defcustom next-line-add-newlines t
1807 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
1808 :type 'boolean
1809 :group 'editing-basics)
1811 (defun next-line (arg)
1812 "Move cursor vertically down ARG lines.
1813 If there is no character in the target line exactly under the current column,
1814 the cursor is positioned after the character in that line which spans this
1815 column, or at the end of the line if it is not long enough.
1816 If there is no line in the buffer after this one, behavior depends on the
1817 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
1818 to create a line, and moves the cursor to that line. Otherwise it moves the
1819 cursor to the end of the buffer.
1821 The command \\[set-goal-column] can be used to create
1822 a semipermanent goal column for this command.
1823 Then instead of trying to move exactly vertically (or as close as possible),
1824 this command moves to the specified goal column (or as close as possible).
1825 The goal column is stored in the variable `goal-column', which is nil
1826 when there is no goal column.
1828 If you are thinking of using this in a Lisp program, consider
1829 using `forward-line' instead. It is usually easier to use
1830 and more reliable (no dependence on goal column, etc.)."
1831 (interactive "p")
1832 (if (and next-line-add-newlines (= arg 1))
1833 (let ((opoint (point)))
1834 (end-of-line)
1835 (if (eobp)
1836 (newline 1)
1837 (goto-char opoint)
1838 (line-move arg)))
1839 (if (interactive-p)
1840 (condition-case nil
1841 (line-move arg)
1842 ((beginning-of-buffer end-of-buffer) (ding)))
1843 (line-move arg)))
1844 nil)
1846 (defun previous-line (arg)
1847 "Move cursor vertically up ARG lines.
1848 If there is no character in the target line exactly over the current column,
1849 the cursor is positioned after the character in that line which spans this
1850 column, or at the end of the line if it is not long enough.
1852 The command \\[set-goal-column] can be used to create
1853 a semipermanent goal column for this command.
1854 Then instead of trying to move exactly vertically (or as close as possible),
1855 this command moves to the specified goal column (or as close as possible).
1856 The goal column is stored in the variable `goal-column', which is nil
1857 when there is no goal column.
1859 If you are thinking of using this in a Lisp program, consider using
1860 `forward-line' with a negative argument instead. It is usually easier
1861 to use and more reliable (no dependence on goal column, etc.)."
1862 (interactive "p")
1863 (if (interactive-p)
1864 (condition-case nil
1865 (line-move (- arg))
1866 ((beginning-of-buffer end-of-buffer) (ding)))
1867 (line-move (- arg)))
1868 nil)
1870 (defcustom track-eol nil
1871 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1872 This means moving to the end of each line moved onto.
1873 The beginning of a blank line does not count as the end of a line."
1874 :type 'boolean
1875 :group 'editing-basics)
1877 (defcustom goal-column nil
1878 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
1879 :type '(choice integer
1880 (const :tag "None" nil))
1881 :group 'editing-basics)
1882 (make-variable-buffer-local 'goal-column)
1884 (defvar temporary-goal-column 0
1885 "Current goal column for vertical motion.
1886 It is the column where point was
1887 at the start of current run of vertical motion commands.
1888 When the `track-eol' feature is doing its job, the value is 9999.")
1890 (defcustom line-move-ignore-invisible nil
1891 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
1892 Outline mode sets this."
1893 :type 'boolean
1894 :group 'editing-basics)
1896 ;; This is the guts of next-line and previous-line.
1897 ;; Arg says how many lines to move.
1898 (defun line-move (arg)
1899 ;; Don't run any point-motion hooks, and disregard intangibility,
1900 ;; for intermediate positions.
1901 (let ((inhibit-point-motion-hooks t)
1902 (opoint (point))
1903 new)
1904 (unwind-protect
1905 (progn
1906 (if (not (or (eq last-command 'next-line)
1907 (eq last-command 'previous-line)))
1908 (setq temporary-goal-column
1909 (if (and track-eol (eolp)
1910 ;; Don't count beg of empty line as end of line
1911 ;; unless we just did explicit end-of-line.
1912 (or (not (bolp)) (eq last-command 'end-of-line)))
1913 9999
1914 (current-column))))
1915 (if (and (not (integerp selective-display))
1916 (not line-move-ignore-invisible))
1917 ;; Use just newline characters.
1918 (or (if (> arg 0)
1919 (progn (if (> arg 1) (forward-line (1- arg)))
1920 ;; This way of moving forward ARG lines
1921 ;; verifies that we have a newline after the last one.
1922 ;; It doesn't get confused by intangible text.
1923 (end-of-line)
1924 (zerop (forward-line 1)))
1925 (and (zerop (forward-line arg))
1926 (bolp)))
1927 (signal (if (< arg 0)
1928 'beginning-of-buffer
1929 'end-of-buffer)
1930 nil))
1931 ;; Move by arg lines, but ignore invisible ones.
1932 (while (> arg 0)
1933 (end-of-line)
1934 (and (zerop (vertical-motion 1))
1935 (signal 'end-of-buffer nil))
1936 ;; If the following character is currently invisible,
1937 ;; skip all characters with that same `invisible' property value.
1938 (while (and (not (eobp))
1939 (let ((prop
1940 (get-char-property (point) 'invisible)))
1941 (if (eq buffer-invisibility-spec t)
1942 prop
1943 (or (memq prop buffer-invisibility-spec)
1944 (assq prop buffer-invisibility-spec)))))
1945 (if (get-text-property (point) 'invisible)
1946 (goto-char (next-single-property-change (point) 'invisible))
1947 (goto-char (next-overlay-change (point)))))
1948 (setq arg (1- arg)))
1949 (while (< arg 0)
1950 (beginning-of-line)
1951 (and (zerop (vertical-motion -1))
1952 (signal 'beginning-of-buffer nil))
1953 (while (and (not (bobp))
1954 (let ((prop
1955 (get-char-property (1- (point)) 'invisible)))
1956 (if (eq buffer-invisibility-spec t)
1957 prop
1958 (or (memq prop buffer-invisibility-spec)
1959 (assq prop buffer-invisibility-spec)))))
1960 (if (get-text-property (1- (point)) 'invisible)
1961 (goto-char (previous-single-property-change (point) 'invisible))
1962 (goto-char (previous-overlay-change (point)))))
1963 (setq arg (1+ arg))))
1964 (let ((buffer-invisibility-spec nil))
1965 (move-to-column (or goal-column temporary-goal-column))))
1966 (setq new (point))
1967 ;; If we are moving into some intangible text,
1968 ;; look for following text on the same line which isn't intangible
1969 ;; and move there.
1970 (let ((after (and (< new (point-max))
1971 (get-char-property new 'intangible)))
1972 (before (and (> new (point-min))
1973 (get-char-property (1- new) 'intangible)))
1974 line-end)
1975 (when (and before (eq before after))
1976 (setq line-end (save-excursion (end-of-line) (point)))
1977 (goto-char (point-min))
1978 (let ((inhibit-point-motion-hooks nil))
1979 (goto-char new))
1980 (if (<= new line-end)
1981 (setq new (point)))))
1982 ;; Remember where we moved to, go back home,
1983 ;; then do the motion over again
1984 ;; in just one step, with intangibility and point-motion hooks
1985 ;; enabled this time.
1986 (goto-char opoint)
1987 (setq inhibit-point-motion-hooks nil)
1988 (goto-char new)))
1989 nil)
1991 ;;; Many people have said they rarely use this feature, and often type
1992 ;;; it by accident. Maybe it shouldn't even be on a key.
1993 (put 'set-goal-column 'disabled t)
1995 (defun set-goal-column (arg)
1996 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1997 Those commands will move to this position in the line moved to
1998 rather than trying to keep the same horizontal position.
1999 With a non-nil argument, clears out the goal column
2000 so that \\[next-line] and \\[previous-line] resume vertical motion.
2001 The goal column is stored in the variable `goal-column'."
2002 (interactive "P")
2003 (if arg
2004 (progn
2005 (setq goal-column nil)
2006 (message "No goal column"))
2007 (setq goal-column (current-column))
2008 (message (substitute-command-keys
2009 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2010 goal-column))
2011 nil)
2013 ;;; Partial support for horizontal autoscrolling. Someday, this feature
2014 ;;; will be built into the C level and all the (hscroll-point-visible) calls
2015 ;;; will go away.
2017 (defcustom hscroll-step 0
2018 "*The number of columns to try scrolling a window by when point moves out.
2019 If that fails to bring point back on frame, point is centered instead.
2020 If this is zero, point is always centered after it moves off frame."
2021 :type '(choice (const :tag "Alway Center" 0)
2022 (integer :format "%v" 1))
2023 :group 'editing-basics)
2025 (defun hscroll-point-visible ()
2026 "Scrolls the selected window horizontally to make point visible."
2027 (save-excursion
2028 (set-buffer (window-buffer))
2029 (if (not (or truncate-lines
2030 (> (window-hscroll) 0)
2031 (and truncate-partial-width-windows
2032 (< (window-width) (frame-width)))))
2033 ;; Point is always visible when lines are wrapped.
2035 ;; If point is on the invisible part of the line before window-start,
2036 ;; then hscrolling can't bring it back, so reset window-start first.
2037 (and (< (point) (window-start))
2038 (let ((ws-bol (save-excursion
2039 (goto-char (window-start))
2040 (beginning-of-line)
2041 (point))))
2042 (and (>= (point) ws-bol)
2043 (set-window-start nil ws-bol))))
2044 (let* ((here (hscroll-window-column))
2045 (left (min (window-hscroll) 1))
2046 (right (1- (window-width))))
2047 ;; Allow for the truncation glyph, if we're not exactly at eol.
2048 (if (not (and (= here right)
2049 (= (following-char) ?\n)))
2050 (setq right (1- right)))
2051 (cond
2052 ;; If too far away, just recenter. But don't show too much
2053 ;; white space off the end of the line.
2054 ((or (< here (- left hscroll-step))
2055 (> here (+ right hscroll-step)))
2056 (let ((eol (save-excursion (end-of-line) (hscroll-window-column))))
2057 (scroll-left (min (- here (/ (window-width) 2))
2058 (- eol (window-width) -5)))))
2059 ;; Within range. Scroll by one step (or maybe not at all).
2060 ((< here left)
2061 (scroll-right hscroll-step))
2062 ((> here right)
2063 (scroll-left hscroll-step)))))))
2065 ;; This function returns the window's idea of the display column of point,
2066 ;; assuming that the window is already known to be truncated rather than
2067 ;; wrapped, and that we've already handled the case where point is on the
2068 ;; part of the line before window-start. We ignore window-width; if point
2069 ;; is beyond the right margin, we want to know how far. The return value
2070 ;; includes the effects of window-hscroll, window-start, and the prompt
2071 ;; string in the minibuffer. It may be negative due to hscroll.
2072 (defun hscroll-window-column ()
2073 (let* ((hscroll (window-hscroll))
2074 (startpos (save-excursion
2075 (beginning-of-line)
2076 (if (= (point) (save-excursion
2077 (goto-char (window-start))
2078 (beginning-of-line)
2079 (point)))
2080 (goto-char (window-start)))
2081 (point)))
2082 (hpos (+ (if (and (eq (selected-window) (minibuffer-window))
2083 (= 1 (window-start))
2084 (= startpos (point-min)))
2085 (minibuffer-prompt-width)
2087 (min 0 (- 1 hscroll))))
2088 val)
2089 (car (cdr (compute-motion startpos (cons hpos 0)
2090 (point) (cons 0 1)
2091 1000000 (cons hscroll 0) nil)))))
2094 ;; rms: (1) The definitions of arrow keys should not simply restate
2095 ;; what keys they are. The arrow keys should run the ordinary commands.
2096 ;; (2) The arrow keys are just one of many common ways of moving point
2097 ;; within a line. Real horizontal autoscrolling would be a good feature,
2098 ;; but supporting it only for arrow keys is too incomplete to be desirable.
2100 ;;;;; Make arrow keys do the right thing for improved terminal support
2101 ;;;;; When we implement true horizontal autoscrolling, right-arrow and
2102 ;;;;; left-arrow can lose the (if truncate-lines ...) clause and become
2103 ;;;;; aliases. These functions are bound to the corresponding keyboard
2104 ;;;;; events in loaddefs.el.
2106 ;;(defun right-arrow (arg)
2107 ;; "Move right one character on the screen (with prefix ARG, that many chars).
2108 ;;Scroll right if needed to keep point horizontally onscreen."
2109 ;; (interactive "P")
2110 ;; (forward-char arg)
2111 ;; (hscroll-point-visible))
2113 ;;(defun left-arrow (arg)
2114 ;; "Move left one character on the screen (with prefix ARG, that many chars).
2115 ;;Scroll left if needed to keep point horizontally onscreen."
2116 ;; (interactive "P")
2117 ;; (backward-char arg)
2118 ;; (hscroll-point-visible))
2120 (defun scroll-other-window-down (lines)
2121 "Scroll the \"other window\" down.
2122 For more details, see the documentation for `scroll-other-window'."
2123 (interactive "P")
2124 (scroll-other-window
2125 ;; Just invert the argument's meaning.
2126 ;; We can do that without knowing which window it will be.
2127 (if (eq lines '-) nil
2128 (if (null lines) '-
2129 (- (prefix-numeric-value lines))))))
2130 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2132 (defun beginning-of-buffer-other-window (arg)
2133 "Move point to the beginning of the buffer in the other window.
2134 Leave mark at previous position.
2135 With arg N, put point N/10 of the way from the true beginning."
2136 (interactive "P")
2137 (let ((orig-window (selected-window))
2138 (window (other-window-for-scrolling)))
2139 ;; We use unwind-protect rather than save-window-excursion
2140 ;; because the latter would preserve the things we want to change.
2141 (unwind-protect
2142 (progn
2143 (select-window window)
2144 ;; Set point and mark in that window's buffer.
2145 (beginning-of-buffer arg)
2146 ;; Set point accordingly.
2147 (recenter '(t)))
2148 (select-window orig-window))))
2150 (defun end-of-buffer-other-window (arg)
2151 "Move point to the end of the buffer in the other window.
2152 Leave mark at previous position.
2153 With arg N, put point N/10 of the way from the true end."
2154 (interactive "P")
2155 ;; See beginning-of-buffer-other-window for comments.
2156 (let ((orig-window (selected-window))
2157 (window (other-window-for-scrolling)))
2158 (unwind-protect
2159 (progn
2160 (select-window window)
2161 (end-of-buffer arg)
2162 (recenter '(t)))
2163 (select-window orig-window))))
2165 (defun transpose-chars (arg)
2166 "Interchange characters around point, moving forward one character.
2167 With prefix arg ARG, effect is to take character before point
2168 and drag it forward past ARG other characters (backward if ARG negative).
2169 If no argument and at end of line, the previous two chars are exchanged."
2170 (interactive "*P")
2171 (and (null arg) (eolp) (forward-char -1))
2172 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2174 (defun transpose-words (arg)
2175 "Interchange words around point, leaving point at end of them.
2176 With prefix arg ARG, effect is to take word before or around point
2177 and drag it forward past ARG other words (backward if ARG negative).
2178 If ARG is zero, the words around or after point and around or after mark
2179 are interchanged."
2180 (interactive "*p")
2181 (transpose-subr 'forward-word arg))
2183 (defun transpose-sexps (arg)
2184 "Like \\[transpose-words] but applies to sexps.
2185 Does not work on a sexp that point is in the middle of
2186 if it is a list or string."
2187 (interactive "*p")
2188 (transpose-subr 'forward-sexp arg))
2190 (defun transpose-lines (arg)
2191 "Exchange current line and previous line, leaving point after both.
2192 With argument ARG, takes previous line and moves it past ARG lines.
2193 With argument 0, interchanges line point is in with line mark is in."
2194 (interactive "*p")
2195 (transpose-subr (function
2196 (lambda (arg)
2197 (if (= arg 1)
2198 (progn
2199 ;; Move forward over a line,
2200 ;; but create a newline if none exists yet.
2201 (end-of-line)
2202 (if (eobp)
2203 (newline)
2204 (forward-char 1)))
2205 (forward-line arg))))
2206 arg))
2208 (defun transpose-subr (mover arg)
2209 (let (start1 end1 start2 end2)
2210 (if (= arg 0)
2211 (progn
2212 (save-excursion
2213 (funcall mover 1)
2214 (setq end2 (point))
2215 (funcall mover -1)
2216 (setq start2 (point))
2217 (goto-char (mark))
2218 (funcall mover 1)
2219 (setq end1 (point))
2220 (funcall mover -1)
2221 (setq start1 (point))
2222 (transpose-subr-1))
2223 (exchange-point-and-mark)))
2224 (while (> arg 0)
2225 (funcall mover -1)
2226 (setq start1 (point))
2227 (funcall mover 1)
2228 (setq end1 (point))
2229 (funcall mover 1)
2230 (setq end2 (point))
2231 (funcall mover -1)
2232 (setq start2 (point))
2233 (transpose-subr-1)
2234 (goto-char end2)
2235 (setq arg (1- arg)))
2236 (while (< arg 0)
2237 (funcall mover -1)
2238 (setq start2 (point))
2239 (funcall mover -1)
2240 (setq start1 (point))
2241 (funcall mover 1)
2242 (setq end1 (point))
2243 (funcall mover 1)
2244 (setq end2 (point))
2245 (transpose-subr-1)
2246 (setq arg (1+ arg)))))
2248 (defun transpose-subr-1 ()
2249 (if (> (min end1 end2) (max start1 start2))
2250 (error "Don't have two things to transpose"))
2251 (let* ((word1 (buffer-substring start1 end1))
2252 (len1 (length word1))
2253 (word2 (buffer-substring start2 end2))
2254 (len2 (length word2)))
2255 (delete-region start2 end2)
2256 (goto-char start2)
2257 (insert word1)
2258 (goto-char (if (< start1 start2) start1
2259 (+ start1 (- len1 len2))))
2260 (delete-region (point) (+ (point) len1))
2261 (insert word2)))
2263 (defcustom comment-column 32
2264 "*Column to indent right-margin comments to.
2265 Setting this variable automatically makes it local to the current buffer.
2266 Each mode establishes a different default value for this variable; you
2267 can set the value for a particular mode using that mode's hook."
2268 :type 'integer
2269 :group 'fill-comments)
2270 (make-variable-buffer-local 'comment-column)
2272 (defcustom comment-start nil
2273 "*String to insert to start a new comment, or nil if no comment syntax."
2274 :type '(choice (const :tag "None" nil)
2275 string)
2276 :group 'fill-comments)
2278 (defcustom comment-start-skip nil
2279 "*Regexp to match the start of a comment plus everything up to its body.
2280 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
2281 at the place matched by the close of the first pair."
2282 :type '(choice (const :tag "None" nil)
2283 regexp)
2284 :group 'fill-comments)
2286 (defcustom comment-end ""
2287 "*String to insert to end a new comment.
2288 Should be an empty string if comments are terminated by end-of-line."
2289 :type 'string
2290 :group 'fill-comments)
2292 (defvar comment-indent-hook nil
2293 "Obsolete variable for 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 (defvar comment-indent-function
2298 '(lambda () comment-column)
2299 "Function to compute desired indentation for a comment.
2300 This function is called with no args with point at the beginning of
2301 the comment's starting delimiter.")
2303 (defcustom block-comment-start nil
2304 "*String to insert to start a new comment on a line by itself.
2305 If nil, use `comment-start' instead.
2306 Note that the regular expression `comment-start-skip' should skip this string
2307 as well as the `comment-start' string."
2308 :type '(choice (const :tag "Use comment-start" nil)
2309 string)
2310 :group 'fill-comments)
2312 (defcustom block-comment-end nil
2313 "*String to insert to end a new comment on a line by itself.
2314 Should be an empty string if comments are terminated by end-of-line.
2315 If nil, use `comment-end' instead."
2316 :type '(choice (const :tag "Use comment-end" nil)
2317 string)
2318 :group 'fill-comments)
2320 (defun indent-for-comment ()
2321 "Indent this line's comment to comment column, or insert an empty comment."
2322 (interactive "*")
2323 (let* ((empty (save-excursion (beginning-of-line)
2324 (looking-at "[ \t]*$")))
2325 (starter (or (and empty block-comment-start) comment-start))
2326 (ender (or (and empty block-comment-end) comment-end)))
2327 (if (null starter)
2328 (error "No comment syntax defined")
2329 (let* ((eolpos (save-excursion (end-of-line) (point)))
2330 cpos indent begpos)
2331 (beginning-of-line)
2332 (if (re-search-forward comment-start-skip eolpos 'move)
2333 (progn (setq cpos (point-marker))
2334 ;; Find the start of the comment delimiter.
2335 ;; If there were paren-pairs in comment-start-skip,
2336 ;; position at the end of the first pair.
2337 (if (match-end 1)
2338 (goto-char (match-end 1))
2339 ;; If comment-start-skip matched a string with
2340 ;; internal whitespace (not final whitespace) then
2341 ;; the delimiter start at the end of that
2342 ;; whitespace. Otherwise, it starts at the
2343 ;; beginning of what was matched.
2344 (skip-syntax-backward " " (match-beginning 0))
2345 (skip-syntax-backward "^ " (match-beginning 0)))))
2346 (setq begpos (point))
2347 ;; Compute desired indent.
2348 (if (= (current-column)
2349 (setq indent (if comment-indent-hook
2350 (funcall comment-indent-hook)
2351 (funcall comment-indent-function))))
2352 (goto-char begpos)
2353 ;; If that's different from current, change it.
2354 (skip-chars-backward " \t")
2355 (delete-region (point) begpos)
2356 (indent-to indent))
2357 ;; An existing comment?
2358 (if cpos
2359 (progn (goto-char cpos)
2360 (set-marker cpos nil))
2361 ;; No, insert one.
2362 (insert starter)
2363 (save-excursion
2364 (insert ender)))))))
2366 (defun set-comment-column (arg)
2367 "Set the comment column based on point.
2368 With no arg, set the comment column to the current column.
2369 With just minus as arg, kill any comment on this line.
2370 With any other arg, set comment column to indentation of the previous comment
2371 and then align or create a comment on this line at that column."
2372 (interactive "P")
2373 (if (eq arg '-)
2374 (kill-comment nil)
2375 (if arg
2376 (progn
2377 (save-excursion
2378 (beginning-of-line)
2379 (re-search-backward comment-start-skip)
2380 (beginning-of-line)
2381 (re-search-forward comment-start-skip)
2382 (goto-char (match-beginning 0))
2383 (setq comment-column (current-column))
2384 (message "Comment column set to %d" comment-column))
2385 (indent-for-comment))
2386 (setq comment-column (current-column))
2387 (message "Comment column set to %d" comment-column))))
2389 (defun kill-comment (arg)
2390 "Kill the comment on this line, if any.
2391 With argument, kill comments on that many lines starting with this one."
2392 ;; this function loses in a lot of situations. it incorrectly recognises
2393 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
2394 ;; with multi-line comments, can kill extra whitespace if comment wasn't
2395 ;; through end-of-line, et cetera.
2396 (interactive "P")
2397 (or comment-start-skip (error "No comment syntax defined"))
2398 (let ((count (prefix-numeric-value arg)) endc)
2399 (while (> count 0)
2400 (save-excursion
2401 (end-of-line)
2402 (setq endc (point))
2403 (beginning-of-line)
2404 (and (string< "" comment-end)
2405 (setq endc
2406 (progn
2407 (re-search-forward (regexp-quote comment-end) endc 'move)
2408 (skip-chars-forward " \t")
2409 (point))))
2410 (beginning-of-line)
2411 (if (re-search-forward comment-start-skip endc t)
2412 (progn
2413 (goto-char (match-beginning 0))
2414 (skip-chars-backward " \t")
2415 (kill-region (point) endc)
2416 ;; to catch comments a line beginnings
2417 (indent-according-to-mode))))
2418 (if arg (forward-line 1))
2419 (setq count (1- count)))))
2421 (defun comment-region (beg end &optional arg)
2422 "Comment or uncomment each line in the region.
2423 With just C-u prefix arg, uncomment each line in region.
2424 Numeric prefix arg ARG means use ARG comment characters.
2425 If ARG is negative, delete that many comment characters instead.
2426 Comments are terminated on each line, even for syntax in which newline does
2427 not end the comment. Blank lines do not get comments."
2428 ;; if someone wants it to only put a comment-start at the beginning and
2429 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
2430 ;; is easy enough. No option is made here for other than commenting
2431 ;; every line.
2432 (interactive "r\nP")
2433 (or comment-start (error "No comment syntax is defined"))
2434 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2435 (save-excursion
2436 (save-restriction
2437 (let ((cs comment-start) (ce comment-end)
2438 numarg)
2439 (if (consp arg) (setq numarg t)
2440 (setq numarg (prefix-numeric-value arg))
2441 ;; For positive arg > 1, replicate the comment delims now,
2442 ;; then insert the replicated strings just once.
2443 (while (> numarg 1)
2444 (setq cs (concat cs comment-start)
2445 ce (concat ce comment-end))
2446 (setq numarg (1- numarg))))
2447 ;; Loop over all lines from BEG to END.
2448 (narrow-to-region beg end)
2449 (goto-char beg)
2450 (while (not (eobp))
2451 (if (or (eq numarg t) (< numarg 0))
2452 (progn
2453 ;; Delete comment start from beginning of line.
2454 (if (eq numarg t)
2455 (while (looking-at (regexp-quote cs))
2456 (delete-char (length cs)))
2457 (let ((count numarg))
2458 (while (and (> 1 (setq count (1+ count)))
2459 (looking-at (regexp-quote cs)))
2460 (delete-char (length cs)))))
2461 ;; Delete comment end from end of line.
2462 (if (string= "" ce)
2464 (if (eq numarg t)
2465 (progn
2466 (end-of-line)
2467 ;; This is questionable if comment-end ends in
2468 ;; whitespace. That is pretty brain-damaged,
2469 ;; though.
2470 (while (progn (skip-chars-backward " \t")
2471 (and (>= (- (point) (point-min)) (length ce))
2472 (save-excursion
2473 (backward-char (length ce))
2474 (looking-at (regexp-quote ce)))))
2475 (delete-char (- (length ce)))))
2476 (let ((count numarg))
2477 (while (> 1 (setq count (1+ count)))
2478 (end-of-line)
2479 ;; this is questionable if comment-end ends in whitespace
2480 ;; that is pretty brain-damaged though
2481 (skip-chars-backward " \t")
2482 (save-excursion
2483 (backward-char (length ce))
2484 (if (looking-at (regexp-quote ce))
2485 (delete-char (length ce))))))))
2486 (forward-line 1))
2487 ;; Insert at beginning and at end.
2488 (if (looking-at "[ \t]*$") ()
2489 (insert cs)
2490 (if (string= "" ce) ()
2491 (end-of-line)
2492 (insert ce)))
2493 (search-forward "\n" nil 'move)))))))
2495 (defun backward-word (arg)
2496 "Move backward until encountering the end of a word.
2497 With argument, do this that many times.
2498 In programs, it is faster to call `forward-word' with negative arg."
2499 (interactive "p")
2500 (forward-word (- arg)))
2502 (defun mark-word (arg)
2503 "Set mark arg words away from point."
2504 (interactive "p")
2505 (push-mark
2506 (save-excursion
2507 (forward-word arg)
2508 (point))
2509 nil t))
2511 (defun kill-word (arg)
2512 "Kill characters forward until encountering the end of a word.
2513 With argument, do this that many times."
2514 (interactive "p")
2515 (kill-region (point) (progn (forward-word arg) (point))))
2517 (defun backward-kill-word (arg)
2518 "Kill characters backward until encountering the end of a word.
2519 With argument, do this that many times."
2520 (interactive "p")
2521 (kill-word (- arg)))
2523 (defun current-word (&optional strict)
2524 "Return the word point is on (or a nearby word) as a string.
2525 If optional arg STRICT is non-nil, return nil unless point is within
2526 or adjacent to a word."
2527 (save-excursion
2528 (let ((oldpoint (point)) (start (point)) (end (point)))
2529 (skip-syntax-backward "w_") (setq start (point))
2530 (goto-char oldpoint)
2531 (skip-syntax-forward "w_") (setq end (point))
2532 (if (and (eq start oldpoint) (eq end oldpoint))
2533 ;; Point is neither within nor adjacent to a word.
2534 (and (not strict)
2535 (progn
2536 ;; Look for preceding word in same line.
2537 (skip-syntax-backward "^w_"
2538 (save-excursion (beginning-of-line)
2539 (point)))
2540 (if (bolp)
2541 ;; No preceding word in same line.
2542 ;; Look for following word in same line.
2543 (progn
2544 (skip-syntax-forward "^w_"
2545 (save-excursion (end-of-line)
2546 (point)))
2547 (setq start (point))
2548 (skip-syntax-forward "w_")
2549 (setq end (point)))
2550 (setq end (point))
2551 (skip-syntax-backward "w_")
2552 (setq start (point)))
2553 (buffer-substring-no-properties start end)))
2554 (buffer-substring-no-properties start end)))))
2556 (defcustom fill-prefix nil
2557 "*String for filling to insert at front of new line, or nil for none.
2558 Setting this variable automatically makes it local to the current buffer."
2559 :type '(choice (const :tag "None" nil)
2560 string)
2561 :group 'fill)
2562 (make-variable-buffer-local 'fill-prefix)
2564 (defcustom auto-fill-inhibit-regexp nil
2565 "*Regexp to match lines which should not be auto-filled."
2566 :type '(choice (const :tag "None" nil)
2567 regexp)
2568 :group 'fill)
2570 ;; This function is the auto-fill-function of a buffer
2571 ;; when Auto-Fill mode is enabled.
2572 ;; It returns t if it really did any work.
2573 (defun do-auto-fill ()
2574 (let (fc justify bol give-up
2575 (fill-prefix fill-prefix))
2576 (if (or (not (setq justify (current-justification)))
2577 (null (setq fc (current-fill-column)))
2578 (and (eq justify 'left)
2579 (<= (current-column) fc))
2580 (save-excursion (beginning-of-line)
2581 (setq bol (point))
2582 (and auto-fill-inhibit-regexp
2583 (looking-at auto-fill-inhibit-regexp))))
2584 nil ;; Auto-filling not required
2585 (if (memq justify '(full center right))
2586 (save-excursion (unjustify-current-line)))
2588 ;; Choose a fill-prefix automatically.
2589 (if (and adaptive-fill-mode
2590 (or (null fill-prefix) (string= fill-prefix "")))
2591 (let ((prefix
2592 (fill-context-prefix
2593 (save-excursion (backward-paragraph 1) (point))
2594 (save-excursion (forward-paragraph 1) (point)))))
2595 (and prefix (not (equal prefix ""))
2596 (setq fill-prefix prefix))))
2598 (while (and (not give-up) (> (current-column) fc))
2599 ;; Determine where to split the line.
2600 (let ((fill-point
2601 (let ((opoint (point))
2602 bounce
2603 (first t)
2604 after-prefix)
2605 (save-excursion
2606 (beginning-of-line)
2607 (setq after-prefix (point))
2608 (and fill-prefix
2609 (looking-at (regexp-quote fill-prefix))
2610 (setq after-prefix (match-end 0)))
2611 (move-to-column (1+ fc))
2612 ;; Move back to the point where we can break the
2613 ;; line at. We break the line between word or
2614 ;; after/before the character which has character
2615 ;; category `|'. We search space, \c| followed by
2616 ;; a character, or \c| follwoing a character. If
2617 ;; not found, place the point at beginning of line.
2618 (while (or first
2619 ;; If this is after period and a single space,
2620 ;; move back once more--we don't want to break
2621 ;; the line there and make it look like a
2622 ;; sentence end.
2623 (and (not (bobp))
2624 (not bounce)
2625 sentence-end-double-space
2626 (save-excursion (forward-char -1)
2627 (and (looking-at "\\. ")
2628 (not (looking-at "\\. "))))))
2629 (setq first nil)
2630 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
2631 ;; If we find nowhere on the line to break it,
2632 ;; break after one word. Set bounce to t
2633 ;; so we will not keep going in this while loop.
2634 (if (<= (point) after-prefix)
2635 (progn
2636 (re-search-forward "[ \t]" opoint t)
2637 (setq bounce t))
2638 (if (looking-at "[ \t]")
2639 ;; Break the line at word boundary.
2640 (skip-chars-backward " \t")
2641 ;; Break the line after/before \c|.
2642 (forward-char 1))))
2643 (if (and enable-kinsoku enable-multibyte-characters)
2644 (kinsoku (save-excursion
2645 (forward-line 0) (point))))
2646 ;; Let fill-point be set to the place where we end up.
2647 (point)))))
2648 ;; If that place is not the beginning of the line,
2649 ;; break the line there.
2650 (if (save-excursion
2651 (goto-char fill-point)
2652 (not (bolp)))
2653 (let ((prev-column (current-column)))
2654 ;; If point is at the fill-point, do not `save-excursion'.
2655 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2656 ;; point will end up before it rather than after it.
2657 (if (save-excursion
2658 (skip-chars-backward " \t")
2659 (= (point) fill-point))
2660 (indent-new-comment-line t)
2661 (save-excursion
2662 (goto-char fill-point)
2663 (indent-new-comment-line t)))
2664 ;; Now do justification, if required
2665 (if (not (eq justify 'left))
2666 (save-excursion
2667 (end-of-line 0)
2668 (justify-current-line justify nil t)))
2669 ;; If making the new line didn't reduce the hpos of
2670 ;; the end of the line, then give up now;
2671 ;; trying again will not help.
2672 (if (>= (current-column) prev-column)
2673 (setq give-up t)))
2674 ;; No place to break => stop trying.
2675 (setq give-up t))))
2676 ;; Justify last line.
2677 (justify-current-line justify t t)
2678 t)))
2680 (defvar normal-auto-fill-function 'do-auto-fill
2681 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
2682 Some major modes set this.")
2684 (defun auto-fill-mode (&optional arg)
2685 "Toggle Auto Fill mode.
2686 With arg, turn Auto Fill mode on if and only if arg is positive.
2687 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
2688 automatically breaks the line at a previous space.
2690 The value of `normal-auto-fill-function' specifies the function to use
2691 for `auto-fill-function' when turning Auto Fill mode on."
2692 (interactive "P")
2693 (prog1 (setq auto-fill-function
2694 (if (if (null arg)
2695 (not auto-fill-function)
2696 (> (prefix-numeric-value arg) 0))
2697 normal-auto-fill-function
2698 nil))
2699 (force-mode-line-update)))
2701 ;; This holds a document string used to document auto-fill-mode.
2702 (defun auto-fill-function ()
2703 "Automatically break line at a previous space, in insertion of text."
2704 nil)
2706 (defun turn-on-auto-fill ()
2707 "Unconditionally turn on Auto Fill mode."
2708 (auto-fill-mode 1))
2710 (defun set-fill-column (arg)
2711 "Set `fill-column' to specified argument.
2712 Just \\[universal-argument] as argument means to use the current column."
2713 (interactive "P")
2714 (if (consp arg)
2715 (setq arg (current-column)))
2716 (if (not (integerp arg))
2717 ;; Disallow missing argument; it's probably a typo for C-x C-f.
2718 (error "set-fill-column requires an explicit argument")
2719 (message "Fill column set to %d (was %d)" arg fill-column)
2720 (setq fill-column arg)))
2722 (defcustom comment-multi-line nil
2723 "*Non-nil means \\[indent-new-comment-line] should continue same comment
2724 on new line, with no new terminator or starter.
2725 This is obsolete because you might as well use \\[newline-and-indent]."
2726 :type 'boolean
2727 :group 'fill-comments)
2729 (defun indent-new-comment-line (&optional soft)
2730 "Break line at point and indent, continuing comment if within one.
2731 This indents the body of the continued comment
2732 under the previous comment line.
2734 This command is intended for styles where you write a comment per line,
2735 starting a new comment (and terminating it if necessary) on each line.
2736 If you want to continue one comment across several lines, use \\[newline-and-indent].
2738 If a fill column is specified, it overrides the use of the comment column
2739 or comment indentation.
2741 The inserted newline is marked hard if `use-hard-newlines' is true,
2742 unless optional argument SOFT is non-nil."
2743 (interactive)
2744 (let (comcol comstart)
2745 (skip-chars-backward " \t")
2746 (delete-region (point)
2747 (progn (skip-chars-forward " \t")
2748 (point)))
2749 (if soft (insert-and-inherit ?\n) (newline 1))
2750 (if fill-prefix
2751 (progn
2752 (indent-to-left-margin)
2753 (insert-and-inherit fill-prefix))
2754 (if (not comment-multi-line)
2755 (save-excursion
2756 (if (and comment-start-skip
2757 (let ((opoint (point)))
2758 (forward-line -1)
2759 (re-search-forward comment-start-skip opoint t)))
2760 ;; The old line is a comment.
2761 ;; Set WIN to the pos of the comment-start.
2762 ;; But if the comment is empty, look at preceding lines
2763 ;; to find one that has a nonempty comment.
2765 ;; If comment-start-skip contains a \(...\) pair,
2766 ;; the real comment delimiter starts at the end of that pair.
2767 (let ((win (or (match-end 1) (match-beginning 0))))
2768 (while (and (eolp) (not (bobp))
2769 (let (opoint)
2770 (beginning-of-line)
2771 (setq opoint (point))
2772 (forward-line -1)
2773 (re-search-forward comment-start-skip opoint t)))
2774 (setq win (or (match-end 1) (match-beginning 0))))
2775 ;; Indent this line like what we found.
2776 (goto-char win)
2777 (setq comcol (current-column))
2778 (setq comstart
2779 (buffer-substring (point) (match-end 0)))))))
2780 (if comcol
2781 (let ((comment-column comcol)
2782 (comment-start comstart)
2783 (comment-end comment-end))
2784 (and comment-end (not (equal comment-end ""))
2785 ; (if (not comment-multi-line)
2786 (progn
2787 (forward-char -1)
2788 (insert comment-end)
2789 (forward-char 1))
2790 ; (setq comment-column (+ comment-column (length comment-start))
2791 ; comment-start "")
2794 (if (not (eolp))
2795 (setq comment-end ""))
2796 (insert-and-inherit ?\n)
2797 (forward-char -1)
2798 (indent-for-comment)
2799 (save-excursion
2800 ;; Make sure we delete the newline inserted above.
2801 (end-of-line)
2802 (delete-char 1)))
2803 (indent-according-to-mode)))))
2805 (defun set-selective-display (arg)
2806 "Set `selective-display' to ARG; clear it if no arg.
2807 When the value of `selective-display' is a number > 0,
2808 lines whose indentation is >= that value are not displayed.
2809 The variable `selective-display' has a separate value for each buffer."
2810 (interactive "P")
2811 (if (eq selective-display t)
2812 (error "selective-display already in use for marked lines"))
2813 (let ((current-vpos
2814 (save-restriction
2815 (narrow-to-region (point-min) (point))
2816 (goto-char (window-start))
2817 (vertical-motion (window-height)))))
2818 (setq selective-display
2819 (and arg (prefix-numeric-value arg)))
2820 (recenter current-vpos))
2821 (set-window-start (selected-window) (window-start (selected-window)))
2822 (princ "selective-display set to " t)
2823 (prin1 selective-display t)
2824 (princ "." t))
2826 (defvar overwrite-mode-textual " Ovwrt"
2827 "The string displayed in the mode line when in overwrite mode.")
2828 (defvar overwrite-mode-binary " Bin Ovwrt"
2829 "The string displayed in the mode line when in binary overwrite mode.")
2831 (defun overwrite-mode (arg)
2832 "Toggle overwrite mode.
2833 With arg, turn overwrite mode on iff arg is positive.
2834 In overwrite mode, printing characters typed in replace existing text
2835 on a one-for-one basis, rather than pushing it to the right. At the
2836 end of a line, such characters extend the line. Before a tab,
2837 such characters insert until the tab is filled in.
2838 \\[quoted-insert] still inserts characters in overwrite mode; this
2839 is supposed to make it easier to insert characters when necessary."
2840 (interactive "P")
2841 (setq overwrite-mode
2842 (if (if (null arg) (not overwrite-mode)
2843 (> (prefix-numeric-value arg) 0))
2844 'overwrite-mode-textual))
2845 (force-mode-line-update))
2847 (defun binary-overwrite-mode (arg)
2848 "Toggle binary overwrite mode.
2849 With arg, turn binary overwrite mode on iff arg is positive.
2850 In binary overwrite mode, printing characters typed in replace
2851 existing text. Newlines are not treated specially, so typing at the
2852 end of a line joins the line to the next, with the typed character
2853 between them. Typing before a tab character simply replaces the tab
2854 with the character typed.
2855 \\[quoted-insert] replaces the text at the cursor, just as ordinary
2856 typing characters do.
2858 Note that binary overwrite mode is not its own minor mode; it is a
2859 specialization of overwrite-mode, entered by setting the
2860 `overwrite-mode' variable to `overwrite-mode-binary'."
2861 (interactive "P")
2862 (setq overwrite-mode
2863 (if (if (null arg)
2864 (not (eq overwrite-mode 'overwrite-mode-binary))
2865 (> (prefix-numeric-value arg) 0))
2866 'overwrite-mode-binary))
2867 (force-mode-line-update))
2869 (defcustom line-number-mode t
2870 "*Non-nil means display line number in mode line."
2871 :type 'boolean
2872 :group 'editing-basics)
2874 (defun line-number-mode (arg)
2875 "Toggle Line Number mode.
2876 With arg, turn Line Number mode on iff arg is positive.
2877 When Line Number mode is enabled, the line number appears
2878 in the mode line."
2879 (interactive "P")
2880 (setq line-number-mode
2881 (if (null arg) (not line-number-mode)
2882 (> (prefix-numeric-value arg) 0)))
2883 (force-mode-line-update))
2885 (defcustom column-number-mode nil
2886 "*Non-nil means display column number in mode line."
2887 :type 'boolean
2888 :group 'editing-basics)
2890 (defun column-number-mode (arg)
2891 "Toggle Column Number mode.
2892 With arg, turn Column Number mode on iff arg is positive.
2893 When Column Number mode is enabled, the column number appears
2894 in the mode line."
2895 (interactive "P")
2896 (setq column-number-mode
2897 (if (null arg) (not column-number-mode)
2898 (> (prefix-numeric-value arg) 0)))
2899 (force-mode-line-update))
2901 (defgroup paren-blinking nil
2902 "Blinking matching of parens and expressions."
2903 :prefix "blink-matching-"
2904 :group 'paren-matching)
2906 (defcustom blink-matching-paren t
2907 "*Non-nil means show matching open-paren when close-paren is inserted."
2908 :type 'boolean
2909 :group 'paren-blinking)
2911 (defcustom blink-matching-paren-on-screen t
2912 "*Non-nil means show matching open-paren when it is on screen.
2913 If nil, means don't show it (but the open-paren can still be shown
2914 when it is off screen)."
2915 :type 'boolean
2916 :group 'paren-blinking)
2918 (defcustom blink-matching-paren-distance (* 25 1024)
2919 "*If non-nil, is maximum distance to search for matching open-paren."
2920 :type 'integer
2921 :group 'paren-blinking)
2923 (defcustom blink-matching-delay 1
2924 "*Time in seconds to delay after showing a matching paren."
2925 :type 'number
2926 :group 'paren-blinking)
2928 (defcustom blink-matching-paren-dont-ignore-comments nil
2929 "*Non-nil means `blink-matching-paren' will not ignore comments."
2930 :type 'boolean
2931 :group 'paren-blinking)
2933 (defun blink-matching-open ()
2934 "Move cursor momentarily to the beginning of the sexp before point."
2935 (interactive)
2936 (and (> (point) (1+ (point-min)))
2937 blink-matching-paren
2938 ;; Verify an even number of quoting characters precede the close.
2939 (= 1 (logand 1 (- (point)
2940 (save-excursion
2941 (forward-char -1)
2942 (skip-syntax-backward "/\\")
2943 (point)))))
2944 (let* ((oldpos (point))
2945 (blinkpos)
2946 (mismatch))
2947 (save-excursion
2948 (save-restriction
2949 (if blink-matching-paren-distance
2950 (narrow-to-region (max (point-min)
2951 (- (point) blink-matching-paren-distance))
2952 oldpos))
2953 (condition-case ()
2954 (let ((parse-sexp-ignore-comments
2955 (and parse-sexp-ignore-comments
2956 (not blink-matching-paren-dont-ignore-comments))))
2957 (setq blinkpos (scan-sexps oldpos -1)))
2958 (error nil)))
2959 (and blinkpos
2960 (/= (char-syntax (char-after blinkpos))
2961 ?\$)
2962 (setq mismatch
2963 (or (null (matching-paren (char-after blinkpos)))
2964 (/= (char-after (1- oldpos))
2965 (matching-paren (char-after blinkpos))))))
2966 (if mismatch (setq blinkpos nil))
2967 (if blinkpos
2968 (progn
2969 (goto-char blinkpos)
2970 (if (pos-visible-in-window-p)
2971 (and blink-matching-paren-on-screen
2972 (sit-for blink-matching-delay))
2973 (goto-char blinkpos)
2974 (message
2975 "Matches %s"
2976 ;; Show what precedes the open in its line, if anything.
2977 (if (save-excursion
2978 (skip-chars-backward " \t")
2979 (not (bolp)))
2980 (buffer-substring (progn (beginning-of-line) (point))
2981 (1+ blinkpos))
2982 ;; Show what follows the open in its line, if anything.
2983 (if (save-excursion
2984 (forward-char 1)
2985 (skip-chars-forward " \t")
2986 (not (eolp)))
2987 (buffer-substring blinkpos
2988 (progn (end-of-line) (point)))
2989 ;; Otherwise show the previous nonblank line,
2990 ;; if there is one.
2991 (if (save-excursion
2992 (skip-chars-backward "\n \t")
2993 (not (bobp)))
2994 (concat
2995 (buffer-substring (progn
2996 (skip-chars-backward "\n \t")
2997 (beginning-of-line)
2998 (point))
2999 (progn (end-of-line)
3000 (skip-chars-backward " \t")
3001 (point)))
3002 ;; Replace the newline and other whitespace with `...'.
3003 "..."
3004 (buffer-substring blinkpos (1+ blinkpos)))
3005 ;; There is nothing to show except the char itself.
3006 (buffer-substring blinkpos (1+ blinkpos))))))))
3007 (cond (mismatch
3008 (message "Mismatched parentheses"))
3009 ((not blink-matching-paren-distance)
3010 (message "Unmatched parenthesis"))))))))
3012 ;Turned off because it makes dbx bomb out.
3013 (setq blink-paren-function 'blink-matching-open)
3015 ;; This executes C-g typed while Emacs is waiting for a command.
3016 ;; Quitting out of a program does not go through here;
3017 ;; that happens in the QUIT macro at the C code level.
3018 (defun keyboard-quit ()
3019 "Signal a quit condition.
3020 During execution of Lisp code, this character causes a quit directly.
3021 At top-level, as an editor command, this simply beeps."
3022 (interactive)
3023 (deactivate-mark)
3024 (signal 'quit nil))
3026 (define-key global-map "\C-g" 'keyboard-quit)
3028 (defvar buffer-quit-function nil
3029 "Function to call to \"quit\" the current buffer, or nil if none.
3030 \\[keyboard-escape-quit] calls this function when its more local actions
3031 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3033 (defun keyboard-escape-quit ()
3034 "Exit the current \"mode\" (in a generalized sense of the word).
3035 This command can exit an interactive command such as `query-replace',
3036 can clear out a prefix argument or a region,
3037 can get out of the minibuffer or other recursive edit,
3038 cancel the use of the current buffer (for special-purpose buffers),
3039 or go back to just one window (by deleting all but the selected window)."
3040 (interactive)
3041 (cond ((eq last-command 'mode-exited) nil)
3042 ((> (minibuffer-depth) 0)
3043 (abort-recursive-edit))
3044 (current-prefix-arg
3045 nil)
3046 ((and transient-mark-mode
3047 mark-active)
3048 (deactivate-mark))
3049 ((> (recursion-depth) 0)
3050 (exit-recursive-edit))
3051 (buffer-quit-function
3052 (funcall buffer-quit-function))
3053 ((not (one-window-p t))
3054 (delete-other-windows))
3055 ((string-match "^ \\*" (buffer-name (current-buffer)))
3056 (bury-buffer))))
3058 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3060 (defcustom mail-user-agent 'sendmail-user-agent
3061 "*Your preference for a mail composition package.
3062 Various Emacs Lisp packages (e.g. reporter) require you to compose an
3063 outgoing email message. This variable lets you specify which
3064 mail-sending package you prefer.
3066 Valid values include:
3068 sendmail-user-agent -- use the default Emacs Mail package
3069 mh-e-user-agent -- use the Emacs interface to the MH mail system
3070 message-user-agent -- use the GNUS mail sending package
3072 Additional valid symbols may be available; check with the author of
3073 your package for details."
3074 :type '(radio (function-item :tag "Default Emacs mail"
3075 :format "%t\n"
3076 sendmail-user-agent)
3077 (function-item :tag "Emacs interface to MH"
3078 :format "%t\n"
3079 mh-e-user-agent)
3080 (function-item :tag "Gnus mail sending package"
3081 :format "%t\n"
3082 message-user-agent)
3083 (function :tag "Other"))
3084 :group 'mail)
3086 (defun define-mail-user-agent (symbol composefunc sendfunc
3087 &optional abortfunc hookvar)
3088 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3090 SYMBOL can be any Lisp symbol. Its function definition and/or
3091 value as a variable do not matter for this usage; we use only certain
3092 properties on its property list, to encode the rest of the arguments.
3094 COMPOSEFUNC is program callable function that composes an outgoing
3095 mail message buffer. This function should set up the basics of the
3096 buffer without requiring user interaction. It should populate the
3097 standard mail headers, leaving the `to:' and `subject:' headers blank
3098 by default.
3100 COMPOSEFUNC should accept several optional arguments--the same
3101 arguments that `compose-mail' takes. See that function's documentation.
3103 SENDFUNC is the command a user would run to send the message.
3105 Optional ABORTFUNC is the command a user would run to abort the
3106 message. For mail packages that don't have a separate abort function,
3107 this can be `kill-buffer' (the equivalent of omitting this argument).
3109 Optional HOOKVAR is a hook variable that gets run before the message
3110 is actually sent. Callers that use the `mail-user-agent' may
3111 install a hook function temporarily on this hook variable.
3112 If HOOKVAR is nil, `mail-send-hook' is used.
3114 The properties used on SYMBOL are `composefunc', `sendfunc',
3115 `abortfunc', and `hookvar'."
3116 (put symbol 'composefunc composefunc)
3117 (put symbol 'sendfunc sendfunc)
3118 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3119 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3121 (defun assoc-ignore-case (key alist)
3122 "Like `assoc', but assumes KEY is a string and ignores case when comparing."
3123 (setq key (downcase key))
3124 (let (element)
3125 (while (and alist (not element))
3126 (if (equal key (downcase (car (car alist))))
3127 (setq element (car alist)))
3128 (setq alist (cdr alist)))
3129 element))
3131 (define-mail-user-agent 'sendmail-user-agent
3132 'sendmail-user-agent-compose
3133 'mail-send-and-exit)
3135 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3136 switch-function yank-action
3137 send-actions)
3138 (if switch-function
3139 (let ((special-display-buffer-names nil)
3140 (special-display-regexps nil)
3141 (same-window-buffer-names nil)
3142 (same-window-regexps nil))
3143 (funcall switch-function "*mail*")))
3144 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3145 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))))
3146 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3147 continue
3148 (error "Message aborted"))
3149 (save-excursion
3150 (goto-char (point-min))
3151 (search-forward mail-header-separator)
3152 (beginning-of-line)
3153 (while other-headers
3154 (if (not (member (car (car other-headers)) '("in-reply-to" "cc")))
3155 (insert (car (car other-headers)) ": "
3156 (cdr (car other-headers)) "\n"))
3157 (setq other-headers (cdr other-headers)))
3158 t)))
3160 (define-mail-user-agent 'mh-e-user-agent
3161 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3162 'mh-before-send-letter-hook)
3164 (defun compose-mail (&optional to subject other-headers continue
3165 switch-function yank-action send-actions)
3166 "Start composing a mail message to send.
3167 This uses the user's chosen mail composition package
3168 as selected with the variable `mail-user-agent'.
3169 The optional arguments TO and SUBJECT specify recipients
3170 and the initial Subject field, respectively.
3172 OTHER-HEADERS is an alist specifying additional
3173 header fields. Elements look like (HEADER . VALUE) where both
3174 HEADER and VALUE are strings.
3176 CONTINUE, if non-nil, says to continue editing a message already
3177 being composed.
3179 SWITCH-FUNCTION, if non-nil, is a function to use to
3180 switch to and display the buffer used for mail composition.
3182 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3183 to insert the raw text of the message being replied to.
3184 It has the form (FUNCTION . ARGS). The user agent will apply
3185 FUNCTION to ARGS, to insert the raw text of the original message.
3186 \(The user agent will also run `mail-citation-hook', *after* the
3187 original text has been inserted in this way.)
3189 SEND-ACTIONS is a list of actions to call when the message is sent.
3190 Each action has the form (FUNCTION . ARGS)."
3191 (interactive
3192 (list nil nil nil current-prefix-arg))
3193 (let ((function (get mail-user-agent 'composefunc)))
3194 (funcall function to subject other-headers continue
3195 switch-function yank-action send-actions)))
3197 (defun compose-mail-other-window (&optional to subject other-headers continue
3198 yank-action send-actions)
3199 "Like \\[compose-mail], but edit the outgoing message in another window."
3200 (interactive
3201 (list nil nil nil current-prefix-arg))
3202 (compose-mail to subject other-headers continue
3203 'switch-to-buffer-other-window yank-action send-actions))
3206 (defun compose-mail-other-frame (&optional to subject other-headers continue
3207 yank-action send-actions)
3208 "Like \\[compose-mail], but edit the outgoing message in another frame."
3209 (interactive
3210 (list nil nil nil current-prefix-arg))
3211 (compose-mail to subject other-headers continue
3212 'switch-to-buffer-other-frame yank-action send-actions))
3214 (defvar set-variable-value-history nil
3215 "History of values entered with `set-variable'.")
3217 (defun set-variable (var val)
3218 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3219 When using this interactively, enter a Lisp object for VALUE.
3220 If you want VALUE to be a string, you must surround it with doublequotes.
3221 VALUE is used literally, not evaluated.
3223 If VARIABLE has a `variable-interactive' property, that is used as if
3224 it were the arg to `interactive' (which see) to interactively read VALUE.
3226 If VARIABLE has been defined with `defcustom', then the type information
3227 in the definition is used to check that VALUE is valid."
3228 (interactive (let* ((var (read-variable "Set variable: "))
3229 (minibuffer-help-form '(describe-variable var))
3230 (prop (get var 'variable-interactive))
3231 (prompt (format "Set %s to value: " var))
3232 (val (if prop
3233 ;; Use VAR's `variable-interactive' property
3234 ;; as an interactive spec for prompting.
3235 (call-interactively `(lambda (arg)
3236 (interactive ,prop)
3237 arg))
3238 (read
3239 (read-string prompt nil
3240 'set-variable-value-history)))))
3241 (list var val)))
3243 (let ((type (get var 'custom-type)))
3244 (when type
3245 ;; Match with custom type.
3246 (require 'wid-edit)
3247 (setq type (widget-convert type))
3248 (unless (widget-apply type :match val)
3249 (error "Value `%S' does not match type %S of %S"
3250 val (car type) var))))
3251 (set var val))
3253 ;; Define the major mode for lists of completions.
3255 (defvar completion-list-mode-map nil
3256 "Local map for completion list buffers.")
3257 (or completion-list-mode-map
3258 (let ((map (make-sparse-keymap)))
3259 (define-key map [mouse-2] 'mouse-choose-completion)
3260 (define-key map [down-mouse-2] nil)
3261 (define-key map "\C-m" 'choose-completion)
3262 (define-key map "\e\e\e" 'delete-completion-window)
3263 (define-key map [left] 'previous-completion)
3264 (define-key map [right] 'next-completion)
3265 (setq completion-list-mode-map map)))
3267 ;; Completion mode is suitable only for specially formatted data.
3268 (put 'completion-list-mode 'mode-class 'special)
3270 (defvar completion-reference-buffer nil
3271 "Record the buffer that was current when the completion list was requested.
3272 This is a local variable in the completion list buffer.
3273 Initial value is nil to avoid some compiler warnings.")
3275 (defvar completion-no-auto-exit nil
3276 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3277 This also applies to other functions such as `choose-completion'
3278 and `mouse-choose-completion'.")
3280 (defvar completion-base-size nil
3281 "Number of chars at beginning of minibuffer not involved in completion.
3282 This is a local variable in the completion list buffer
3283 but it talks about the buffer in `completion-reference-buffer'.
3284 If this is nil, it means to compare text to determine which part
3285 of the tail end of the buffer's text is involved in completion.")
3287 (defun delete-completion-window ()
3288 "Delete the completion list window.
3289 Go to the window from which completion was requested."
3290 (interactive)
3291 (let ((buf completion-reference-buffer))
3292 (if (one-window-p t)
3293 (if (window-dedicated-p (selected-window))
3294 (delete-frame (selected-frame)))
3295 (delete-window (selected-window))
3296 (if (get-buffer-window buf)
3297 (select-window (get-buffer-window buf))))))
3299 (defun previous-completion (n)
3300 "Move to the previous item in the completion list."
3301 (interactive "p")
3302 (next-completion (- n)))
3304 (defun next-completion (n)
3305 "Move to the next item in the completion list.
3306 With prefix argument N, move N items (negative N means move backward)."
3307 (interactive "p")
3308 (while (and (> n 0) (not (eobp)))
3309 (let ((prop (get-text-property (point) 'mouse-face))
3310 (end (point-max)))
3311 ;; If in a completion, move to the end of it.
3312 (if prop
3313 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3314 ;; Move to start of next one.
3315 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3316 (setq n (1- n)))
3317 (while (and (< n 0) (not (bobp)))
3318 (let ((prop (get-text-property (1- (point)) 'mouse-face))
3319 (end (point-min)))
3320 ;; If in a completion, move to the start of it.
3321 (if prop
3322 (goto-char (previous-single-property-change
3323 (point) 'mouse-face nil end)))
3324 ;; Move to end of the previous completion.
3325 (goto-char (previous-single-property-change (point) 'mouse-face nil end))
3326 ;; Move to the start of that one.
3327 (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
3328 (setq n (1+ n))))
3330 (defun choose-completion ()
3331 "Choose the completion that point is in or next to."
3332 (interactive)
3333 (let (beg end completion (buffer completion-reference-buffer)
3334 (base-size completion-base-size))
3335 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3336 (setq end (point) beg (1+ (point))))
3337 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3338 (setq end (1- (point)) beg (point)))
3339 (if (null beg)
3340 (error "No completion here"))
3341 (setq beg (previous-single-property-change beg 'mouse-face))
3342 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3343 (setq completion (buffer-substring beg end))
3344 (let ((owindow (selected-window)))
3345 (if (and (one-window-p t 'selected-frame)
3346 (window-dedicated-p (selected-window)))
3347 ;; This is a special buffer's frame
3348 (iconify-frame (selected-frame))
3349 (or (window-dedicated-p (selected-window))
3350 (bury-buffer)))
3351 (select-window owindow))
3352 (choose-completion-string completion buffer base-size)))
3354 ;; Delete the longest partial match for STRING
3355 ;; that can be found before POINT.
3356 (defun choose-completion-delete-max-match (string)
3357 (let ((opoint (point))
3358 (len (min (length string)
3359 (- (point) (point-min)))))
3360 (goto-char (- (point) (length string)))
3361 (if completion-ignore-case
3362 (setq string (downcase string)))
3363 (while (and (> len 0)
3364 (let ((tail (buffer-substring (point)
3365 (+ (point) len))))
3366 (if completion-ignore-case
3367 (setq tail (downcase tail)))
3368 (not (string= tail (substring string 0 len)))))
3369 (setq len (1- len))
3370 (forward-char 1))
3371 (delete-char len)))
3373 ;; Switch to BUFFER and insert the completion choice CHOICE.
3374 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3375 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3377 ;; If BUFFER is the minibuffer, exit the minibuffer
3378 ;; unless it is reading a file name and CHOICE is a directory,
3379 ;; or completion-no-auto-exit is non-nil.
3380 (defun choose-completion-string (choice &optional buffer base-size)
3381 (let ((buffer (or buffer completion-reference-buffer)))
3382 ;; If BUFFER is a minibuffer, barf unless it's the currently
3383 ;; active minibuffer.
3384 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
3385 (or (not (active-minibuffer-window))
3386 (not (equal buffer
3387 (window-buffer (active-minibuffer-window))))))
3388 (error "Minibuffer is not active for completion")
3389 ;; Insert the completion into the buffer where completion was requested.
3390 (set-buffer buffer)
3391 (if base-size
3392 (delete-region (+ base-size (point-min)) (point))
3393 (choose-completion-delete-max-match choice))
3394 (insert choice)
3395 (remove-text-properties (- (point) (length choice)) (point)
3396 '(mouse-face nil))
3397 ;; Update point in the window that BUFFER is showing in.
3398 (let ((window (get-buffer-window buffer t)))
3399 (set-window-point window (point)))
3400 ;; If completing for the minibuffer, exit it with this choice.
3401 (and (not completion-no-auto-exit)
3402 (equal buffer (window-buffer (minibuffer-window)))
3403 minibuffer-completion-table
3404 ;; If this is reading a file name, and the file name chosen
3405 ;; is a directory, don't exit the minibuffer.
3406 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3407 (file-directory-p (buffer-string)))
3408 (select-window (active-minibuffer-window))
3409 (exit-minibuffer))))))
3411 (defun completion-list-mode ()
3412 "Major mode for buffers showing lists of possible completions.
3413 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3414 to select the completion near point.
3415 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3416 with the mouse."
3417 (interactive)
3418 (kill-all-local-variables)
3419 (use-local-map completion-list-mode-map)
3420 (setq mode-name "Completion List")
3421 (setq major-mode 'completion-list-mode)
3422 (make-local-variable 'completion-base-size)
3423 (setq completion-base-size nil)
3424 (run-hooks 'completion-list-mode-hook))
3426 (defvar completion-fixup-function nil
3427 "A function to customize how completions are identified in completion lists.
3428 `completion-setup-function' calls this function with no arguments
3429 each time it has found what it thinks is one completion.
3430 Point is at the end of the completion in the completion list buffer.
3431 If this function moves point, it can alter the end of that completion.")
3433 ;; This function goes in completion-setup-hook, so that it is called
3434 ;; after the text of the completion list buffer is written.
3436 (defun completion-setup-function ()
3437 (save-excursion
3438 (let ((mainbuf (current-buffer)))
3439 (set-buffer standard-output)
3440 (completion-list-mode)
3441 (make-local-variable 'completion-reference-buffer)
3442 (setq completion-reference-buffer mainbuf)
3443 (if (eq minibuffer-completion-table 'read-file-name-internal)
3444 ;; For file name completion,
3445 ;; use the number of chars before the start of the
3446 ;; last file name component.
3447 (setq completion-base-size
3448 (save-excursion
3449 (set-buffer mainbuf)
3450 (goto-char (point-max))
3451 (skip-chars-backward (format "^%c" directory-sep-char))
3452 (- (point) (point-min))))
3453 ;; Otherwise, the whole input is the text being completed.
3454 (setq completion-base-size 0))
3455 (goto-char (point-min))
3456 (if window-system
3457 (insert (substitute-command-keys
3458 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3459 (insert (substitute-command-keys
3460 "In this buffer, type \\[choose-completion] to \
3461 select the completion near point.\n\n"))
3462 (forward-line 1)
3463 (while (re-search-forward "[^ \t\n]+\\( [^ \t\n]+\\)*" nil t)
3464 (let ((beg (match-beginning 0))
3465 (end (point)))
3466 (if completion-fixup-function
3467 (funcall completion-fixup-function))
3468 (put-text-property beg (point) 'mouse-face 'highlight)
3469 (goto-char end))))))
3471 (add-hook 'completion-setup-hook 'completion-setup-function)
3473 (define-key minibuffer-local-completion-map [prior]
3474 'switch-to-completions)
3475 (define-key minibuffer-local-must-match-map [prior]
3476 'switch-to-completions)
3477 (define-key minibuffer-local-completion-map "\M-v"
3478 'switch-to-completions)
3479 (define-key minibuffer-local-must-match-map "\M-v"
3480 'switch-to-completions)
3482 (defun switch-to-completions ()
3483 "Select the completion list window."
3484 (interactive)
3485 ;; Make sure we have a completions window.
3486 (or (get-buffer-window "*Completions*")
3487 (minibuffer-completion-help))
3488 (select-window (get-buffer-window "*Completions*"))
3489 (goto-char (point-min))
3490 (search-forward "\n\n")
3491 (forward-line 1))
3493 ;; Support keyboard commands to turn on various modifiers.
3495 ;; These functions -- which are not commands -- each add one modifier
3496 ;; to the following event.
3498 (defun event-apply-alt-modifier (ignore-prompt)
3499 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3500 (defun event-apply-super-modifier (ignore-prompt)
3501 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3502 (defun event-apply-hyper-modifier (ignore-prompt)
3503 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3504 (defun event-apply-shift-modifier (ignore-prompt)
3505 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3506 (defun event-apply-control-modifier (ignore-prompt)
3507 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3508 (defun event-apply-meta-modifier (ignore-prompt)
3509 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3511 (defun event-apply-modifier (event symbol lshiftby prefix)
3512 "Apply a modifier flag to event EVENT.
3513 SYMBOL is the name of this modifier, as a symbol.
3514 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3515 PREFIX is the string that represents this modifier in an event type symbol."
3516 (if (numberp event)
3517 (cond ((eq symbol 'control)
3518 (if (and (<= (downcase event) ?z)
3519 (>= (downcase event) ?a))
3520 (- (downcase event) ?a -1)
3521 (if (and (<= (downcase event) ?Z)
3522 (>= (downcase event) ?A))
3523 (- (downcase event) ?A -1)
3524 (logior (lsh 1 lshiftby) event))))
3525 ((eq symbol 'shift)
3526 (if (and (<= (downcase event) ?z)
3527 (>= (downcase event) ?a))
3528 (upcase event)
3529 (logior (lsh 1 lshiftby) event)))
3531 (logior (lsh 1 lshiftby) event)))
3532 (if (memq symbol (event-modifiers event))
3533 event
3534 (let ((event-type (if (symbolp event) event (car event))))
3535 (setq event-type (intern (concat prefix (symbol-name event-type))))
3536 (if (symbolp event)
3537 event-type
3538 (cons event-type (cdr event)))))))
3540 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
3541 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
3542 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
3543 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
3544 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
3545 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
3547 ;;;; Keypad support.
3549 ;;; Make the keypad keys act like ordinary typing keys. If people add
3550 ;;; bindings for the function key symbols, then those bindings will
3551 ;;; override these, so this shouldn't interfere with any existing
3552 ;;; bindings.
3554 ;; Also tell read-char how to handle these keys.
3555 (mapcar
3556 (lambda (keypad-normal)
3557 (let ((keypad (nth 0 keypad-normal))
3558 (normal (nth 1 keypad-normal)))
3559 (put keypad 'ascii-character normal)
3560 (define-key function-key-map (vector keypad) (vector normal))))
3561 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
3562 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
3563 (kp-space ?\ )
3564 (kp-tab ?\t)
3565 (kp-enter ?\r)
3566 (kp-multiply ?*)
3567 (kp-add ?+)
3568 (kp-separator ?,)
3569 (kp-subtract ?-)
3570 (kp-decimal ?.)
3571 (kp-divide ?/)
3572 (kp-equal ?=)))
3574 ;;; simple.el ends here