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