1 ;;; simple.el --- basic editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 1999
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)
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.
25 ;; A grab-bag of basic Emacs commands not specifically related to some
26 ;; major mode or to file-handling.
31 "Killing and yanking commands"
34 (defgroup fill-comments nil
35 "Indenting and filling of comments."
39 (defgroup paren-matching nil
40 "Highlight (un)matching of parens and expressions."
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."
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))
57 ;; Make sure no functions want to be told about
58 ;; the range of the changes.
59 (not after-change-function)
60 (not before-change-function)
61 (not after-change-functions)
62 (not before-change-functions)
63 ;; Make sure there are no markers here.
64 (not (buffer-has-markers-at (1- (point))))
65 (not (buffer-has-markers-at (point)))
66 ;; Make sure no text properties want to know
67 ;; where the change was.
68 (not (get-char-property (1- (point)) 'modification-hooks))
69 (not (get-char-property (1- (point)) 'insert-behind-hooks))
71 (not (get-char-property (point) 'insert-in-front-hooks)))
72 ;; Make sure the newline before point isn't intangible.
73 (not (get-char-property (1- (point)) 'intangible))
74 ;; Make sure the newline before point isn't read-only.
75 (not (get-char-property (1- (point)) 'read-only))
76 ;; Make sure the newline before point isn't invisible.
77 (not (get-char-property (1- (point)) 'invisible))
78 ;; Make sure the newline before point has the same
79 ;; properties as the char before it (if any).
80 (< (or (previous-property-change (point)) -2)
82 (was-page-start (and (bolp)
83 (looking-at page-delimiter)))
85 (if flag (backward-char 1))
86 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
87 ;; Set last-command-char to tell self-insert what to insert.
88 (let ((last-command-char ?\n)
89 ;; Don't auto-fill if we have a numeric argument.
90 ;; Also not if flag is true (it would fill wrong line);
91 ;; there is no need to since we're at BOL.
92 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
94 (self-insert-command (prefix-numeric-value arg))
95 ;; If we get an error in self-insert-command, put point at right place.
96 (if flag (forward-char 1))))
97 ;; Even if we did *not* get an error, keep that forward-char;
98 ;; all further processing should apply to the newline that the user
99 ;; thinks he inserted.
101 ;; Mark the newline(s) `hard'.
102 (if use-hard-newlines
103 (set-hard-newline-properties
104 (- (point) (if arg (prefix-numeric-value arg) 1)) (point)))
105 ;; If the newline leaves the previous line blank,
106 ;; and we have a left margin, delete that from the blank line.
109 (goto-char beforepos)
111 (and (looking-at "[ \t]$")
112 (> (current-left-margin) 0)
113 (delete-region (point) (progn (end-of-line) (point))))))
114 ;; Indent the line after the newline, except in one case:
115 ;; when we added the newline at the beginning of a line
116 ;; which starts a page.
118 (move-to-left-margin nil t)))
121 (defun set-hard-newline-properties (from to)
122 (let ((sticky (get-text-property from 'rear-nonsticky)))
123 (put-text-property from to 'hard 't)
124 ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
125 (if (and (listp sticky) (not (memq 'hard sticky)))
126 (put-text-property from (point) 'rear-nonsticky
127 (cons 'hard sticky)))))
129 (defun open-line (arg)
130 "Insert a newline and leave point before it.
131 If there is a fill prefix and/or a left-margin, insert them on the new line
132 if the line would have been blank.
133 With arg N, insert N newlines."
135 (let* ((do-fill-prefix (and fill-prefix (bolp)))
136 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
142 (if do-left-margin (indent-to (current-left-margin)))
143 (if do-fill-prefix (insert-and-inherit fill-prefix))))
150 "Split current line, moving portion beyond point vertically down."
152 (skip-chars-forward " \t")
153 (let ((col (current-column))
159 (defun quoted-insert (arg)
160 "Read next input character and insert it.
161 This is useful for inserting control characters.
163 If the first character you type after this command is an octal digit,
164 you should type a sequence of octal digits which specify a character code.
165 Any nondigit terminates the sequence. If the terminator is a RET,
166 it is discarded; any other terminator is used itself as input.
167 The variable `read-quoted-char-radix' specifies the radix for this feature;
168 set it to 10 or 16 to use decimal or hex instead of octal.
170 In overwrite mode, this function inserts the character anyway, and
171 does not handle octal digits specially. This means that if you use
172 overwrite as your normal editing mode, you can use this function to
173 insert characters when necessary.
175 In binary overwrite mode, this function does overwrite, and octal
176 digits are interpreted as a character code. This is intended to be
177 useful for editing binary files."
179 (let ((char (if (or (not overwrite-mode)
180 (eq overwrite-mode 'overwrite-mode-binary))
183 ;; Assume character codes 0240 - 0377 stand for characters in some
184 ;; single-byte character set, and convert them to Emacs
186 (if (and enable-multibyte-characters
189 (setq char (unibyte-char-to-multibyte char)))
191 (if (eq overwrite-mode 'overwrite-mode-binary)
194 (insert-and-inherit char)
195 (setq arg (1- arg)))))
197 (defun delete-indentation (&optional arg)
198 "Join this line to previous and fix up whitespace at join.
199 If there is a fill prefix, delete it from the beginning of this line.
200 With argument, join this line to following line."
203 (if arg (forward-line 1))
204 (if (eq (preceding-char) ?\n)
206 (delete-region (point) (1- (point)))
207 ;; If the second line started with the fill prefix,
208 ;; delete the prefix.
210 (<= (+ (point) (length fill-prefix)) (point-max))
212 (buffer-substring (point)
213 (+ (point) (length fill-prefix)))))
214 (delete-region (point) (+ (point) (length fill-prefix))))
215 (fixup-whitespace))))
217 (defalias 'join-line #'delete-indentation) ; easier to find
219 (defun fixup-whitespace ()
220 "Fixup white space between objects around point.
221 Leave one space or none, according to the context."
224 (delete-horizontal-space)
225 (if (or (looking-at "^\\|\\s)")
226 (save-excursion (forward-char -1)
227 (looking-at "$\\|\\s(\\|\\s'")))
231 (defun delete-horizontal-space ()
232 "Delete all spaces and tabs around point."
234 (skip-chars-backward " \t")
235 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
237 (defun just-one-space ()
238 "Delete all spaces and tabs around point, leaving one space."
240 (skip-chars-backward " \t")
241 (if (= (following-char) ? )
244 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
246 (defun delete-blank-lines ()
247 "On blank line, delete all surrounding blank lines, leaving just one.
248 On isolated blank line, delete that one.
249 On nonblank line, delete any immediately following blank lines."
251 (let (thisblank singleblank)
254 (setq thisblank (looking-at "[ \t]*$"))
255 ;; Set singleblank if there is just one blank line here.
258 (not (looking-at "[ \t]*\n[ \t]*$"))
260 (progn (forward-line -1)
261 (not (looking-at "[ \t]*$")))))))
262 ;; Delete preceding blank lines, and this one too if it's the only one.
266 (if singleblank (forward-line 1))
267 (delete-region (point)
268 (if (re-search-backward "[^ \t\n]" nil t)
269 (progn (forward-line 1) (point))
271 ;; Delete following blank lines, unless the current line is blank
272 ;; and there are no following blank lines.
273 (if (not (and thisblank singleblank))
277 (delete-region (point)
278 (if (re-search-forward "[^ \t\n]" nil t)
279 (progn (beginning-of-line) (point))
281 ;; Handle the special case where point is followed by newline and eob.
282 ;; Delete the line, leaving point at eob.
283 (if (looking-at "^[ \t]*\n\\'")
284 (delete-region (point) (point-max)))))
286 (defun back-to-indentation ()
287 "Move point to the first non-whitespace character on this line."
289 (beginning-of-line 1)
290 (skip-chars-forward " \t"))
292 (defun newline-and-indent ()
293 "Insert a newline, then indent according to major mode.
294 Indentation is done using the value of `indent-line-function'.
295 In programming language modes, this is the same as TAB.
296 In some text modes, where TAB inserts a tab, this command indents to the
297 column specified by the function `current-left-margin'."
299 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
301 (indent-according-to-mode))
303 (defun reindent-then-newline-and-indent ()
304 "Reindent current line, insert newline, then indent the new line.
305 Indentation of both lines is done according to the current major mode,
306 which means calling the current value of `indent-line-function'.
307 In programming language modes, this is the same as TAB.
308 In some text modes, where TAB inserts a tab, this indents to the
309 column specified by the function `current-left-margin'."
312 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
313 (indent-according-to-mode))
315 (indent-according-to-mode))
317 ;; Internal subroutine of delete-char
318 (defun kill-forward-chars (arg)
319 (if (listp arg) (setq arg (car arg)))
320 (if (eq arg '-) (setq arg -1))
321 (kill-region (point) (forward-point arg)))
323 ;; Internal subroutine of backward-delete-char
324 (defun kill-backward-chars (arg)
325 (if (listp arg) (setq arg (car arg)))
326 (if (eq arg '-) (setq arg -1))
327 (kill-region (point) (forward-point (- arg))))
329 (defcustom backward-delete-char-untabify-method 'untabify
330 "*The method for untabifying when deleting backward.
331 Can be `untabify' -- turn a tab to many spaces, then delete one space.
332 `hungry' -- delete all whitespace, both tabs and spaces.
333 nil -- just delete one character."
334 :type '(choice (const untabify) (const hungry) (const nil))
337 (defun backward-delete-char-untabify (arg &optional killp)
338 "Delete characters backward, changing tabs into spaces.
339 The exact behavior depends on `backward-delete-char-untabify-method'.
340 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
341 Interactively, ARG is the prefix arg (default 1)
342 and KILLP is t if a prefix arg was specified."
343 (interactive "*p\nP")
344 (when (eq backward-delete-char-untabify-method 'untabify)
347 (while (and (> count 0) (not (bobp)))
348 (if (= (preceding-char) ?\t)
349 (let ((col (current-column)))
351 (setq col (- col (current-column)))
355 (setq count (1- count))))))
356 (delete-backward-char
357 (if (eq backward-delete-char-untabify-method 'hungry)
358 (let ((wh (- (point) (save-excursion (skip-chars-backward " \t")
360 (+ arg (if (zerop wh) 0 (1- wh))))
364 (defun zap-to-char (arg char)
365 "Kill up to and including ARG'th occurrence of CHAR.
366 Goes backward if ARG is negative; error if CHAR not found."
367 (interactive "p\ncZap to char: ")
368 (kill-region (point) (progn
369 (search-forward (char-to-string char) nil nil arg)
370 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
373 (defun beginning-of-buffer (&optional arg)
374 "Move point to the beginning of the buffer; leave mark at previous position.
375 With arg N, put point N/10 of the way from the beginning.
377 If the buffer is narrowed, this command uses the beginning and size
378 of the accessible part of the buffer.
380 Don't use this command in Lisp programs!
381 \(goto-char (point-min)) is faster and avoids clobbering the mark."
384 (let ((size (- (point-max) (point-min))))
388 ;; Avoid overflow for large buffer sizes!
389 (* (prefix-numeric-value arg)
391 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
393 (if arg (forward-line 1)))
395 (defun end-of-buffer (&optional arg)
396 "Move point to the end of the buffer; leave mark at previous position.
397 With arg N, put point N/10 of the way from the end.
399 If the buffer is narrowed, this command uses the beginning and size
400 of the accessible part of the buffer.
402 Don't use this command in Lisp programs!
403 \(goto-char (point-max)) is faster and avoids clobbering the mark."
406 (let ((size (- (point-max) (point-min))))
410 ;; Avoid overflow for large buffer sizes!
411 (* (prefix-numeric-value arg)
413 (/ (* size (prefix-numeric-value arg)) 10)))
415 ;; If we went to a place in the middle of the buffer,
416 ;; adjust it to the beginning of a line.
417 (if arg (forward-line 1)
418 ;; If the end of the buffer is not already on the screen,
419 ;; then scroll specially to put it near, but not at, the bottom.
420 (if (let ((old-point (point)))
422 (goto-char (window-start))
423 (vertical-motion (window-height))
424 (< (point) old-point)))
426 (overlay-recenter (point))
429 (defun mark-whole-buffer ()
430 "Put point at beginning and mark at end of buffer.
431 You probably should not use this function in Lisp programs;
432 it is usually a mistake for a Lisp function to use any subroutine
433 that uses or sets the mark."
436 (push-mark (point-max) nil t)
437 (goto-char (point-min)))
439 (defun count-lines-region (start end)
440 "Print number of lines and characters in the region."
442 (message "Region has %d lines, %d characters"
443 (count-lines start end) (- end start)))
446 "Print the current buffer line number and narrowed line number of point."
448 (let ((opoint (point)) start)
451 (goto-char (point-min))
458 (message "line %d (narrowed line %d)"
459 (1+ (count-lines 1 (point)))
460 (1+ (count-lines start (point))))
461 (message "Line %d" (1+ (count-lines 1 (point)))))))))
464 (defun count-lines (start end)
465 "Return number of lines between START and END.
466 This is usually the number of newlines between them,
467 but can be one more if START is not equal to END
468 and the greater of them is not at the start of a line."
471 (narrow-to-region start end)
472 (goto-char (point-min))
473 (if (eq selective-display t)
476 (while (re-search-forward "[\n\C-m]" nil t 40)
477 (setq done (+ 40 done)))
478 (while (re-search-forward "[\n\C-m]" nil t 1)
479 (setq done (+ 1 done)))
480 (goto-char (point-max))
481 (if (and (/= start end)
485 (- (buffer-size) (forward-line (buffer-size)))))))
487 (defun what-cursor-position (&optional detail)
488 "Print info on cursor position (on screen and within buffer).
489 Also describe the character after point, and give its character code
490 in octal, decimal and hex. For a non-ASCII multibyte character,
491 also give its encoding in the buffer's selected coding system,
494 With prefix argument, print additional details about that character,
495 instead of the cursor position. This includes the character set name,
496 the codes that identify the character within that character set,
497 and the corresponding external character components.
499 Each language environment may show different external character components."
501 (let* ((char (following-char))
505 (total (buffer-size))
506 (percent (if (> total 50000)
507 ;; Avoid overflow from multiplying by 100!
508 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
509 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
510 (hscroll (if (= (window-hscroll) 0)
512 (format " Hscroll=%d" (window-hscroll))))
513 (col (current-column)))
515 (if (or (/= beg 1) (/= end (1+ total)))
516 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
517 pos total percent beg end col hscroll)
518 (message "point=%d of %d(%d%%) column %d %s"
519 pos total percent col hscroll))
520 (let ((charset (char-charset char))
521 (coding-system buffer-file-coding-system)
522 slot external encoding-msg)
523 ;; To decided an external character code, we use
524 ;; charset-origin-alist property of buffer-file-coding-system.
525 ;; But, if buffer-file-coding-system is nil of undecided, use
526 ;; that property of default-buffer-file-coding-system. If
527 ;; that property value is nil, we don't show external
529 (if (or (not coding-system)
530 (eq (coding-system-type coding-system) t))
531 (setq coding-system default-buffer-file-coding-system))
532 (if (and coding-system
534 (coding-system-get coding-system 'charset-origin-alist))
535 (setq slot (assq charset slot)))
536 (setq external (list (nth 1 slot) (funcall (nth 2 slot) char))))
539 (format "(0%o, %d, 0x%x, ext 0x%x)"
540 char char char (nth 1 external))
541 (format "(0%o, %d, 0x%x)" char char char)))
543 ;; We show the detailed information of CHAR.
545 (if (eq charset 'composition)
546 ;; For a composite character, we show the components
548 (setq internal (concat "(composition of \""
549 (decompose-composite-char char)
552 (setq internal (split-char char))
554 (setq external (cons (charset-short-name charset)
555 (copy-sequence (cdr internal))))
556 (if (= (charset-iso-graphic-plane charset) 1)
558 (setcar (cdr external) (+ (nth 1 external) 128))
560 (setcar (nthcdr 2 external)
561 (+ (nth 2 external) 128)))))))
562 (message "Char: %s %s %s %s"
564 (single-key-description char)
565 (char-to-string char))
566 encoding-msg internal (or external "")))
567 (if (or (/= beg 1) (/= end (1+ total)))
568 (message "Char: %s %s point=%d of %d(%d%%) <%d - %d> column %d %s"
570 (single-key-description char)
571 (char-to-string char))
572 encoding-msg pos total percent beg end col hscroll)
573 (message "Char: %s %s point=%d of %d(%d%%) column %d %s"
575 (single-key-description char)
576 (char-to-string char))
577 encoding-msg pos total percent col hscroll)))))))
579 (defun fundamental-mode ()
580 "Major mode not specialized for anything in particular.
581 Other major modes are defined by comparison with this one."
583 (kill-all-local-variables))
585 (defvar read-expression-map (cons 'keymap minibuffer-local-map)
586 "Minibuffer keymap used for reading Lisp expressions.")
587 (define-key read-expression-map "\M-\t" 'lisp-complete-symbol)
589 (defvar read-expression-history nil)
591 ;; We define this, rather than making `eval' interactive,
592 ;; for the sake of completion of names like eval-region, eval-current-buffer.
593 (defun eval-expression (eval-expression-arg
594 &optional eval-expression-insert-value)
595 "Evaluate EXPRESSION and print value in minibuffer.
596 Value is also consed on to front of the variable `values'."
598 (list (read-from-minibuffer "Eval: "
599 nil read-expression-map t
600 'read-expression-history)
602 (setq values (cons (eval eval-expression-arg) values))
604 (if eval-expression-insert-value (current-buffer) t)))
606 (defun edit-and-eval-command (prompt command)
607 "Prompting with PROMPT, let user edit COMMAND and eval result.
608 COMMAND is a Lisp expression. Let user edit that expression in
609 the minibuffer, then read and evaluate the result."
610 (let ((command (read-from-minibuffer prompt
611 (prin1-to-string command)
612 read-expression-map t
613 '(command-history . 1))))
614 ;; If command was added to command-history as a string,
615 ;; get rid of that. We want only evaluable expressions there.
616 (if (stringp (car command-history))
617 (setq command-history (cdr command-history)))
619 ;; If command to be redone does not match front of history,
620 ;; add it to the history.
621 (or (equal command (car command-history))
622 (setq command-history (cons command command-history)))
625 (defun repeat-complex-command (arg)
626 "Edit and re-evaluate last complex command, or ARGth from last.
627 A complex command is one which used the minibuffer.
628 The command is placed in the minibuffer as a Lisp form for editing.
629 The result is executed, repeating the command as changed.
630 If the command has been changed or is not the most recent previous command
631 it is added to the front of the command history.
632 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
633 to get different commands to edit and resubmit."
635 (let ((elt (nth (1- arg) command-history))
640 (let ((print-level nil)
641 (minibuffer-history-position arg)
642 (minibuffer-history-sexp-flag (1+ (minibuffer-depth))))
643 (read-from-minibuffer
644 "Redo: " (prin1-to-string elt) read-expression-map t
645 (cons 'command-history arg))))
647 ;; If command was added to command-history as a string,
648 ;; get rid of that. We want only evaluable expressions there.
649 (if (stringp (car command-history))
650 (setq command-history (cdr command-history)))
652 ;; If command to be redone does not match front of history,
653 ;; add it to the history.
654 (or (equal newcmd (car command-history))
655 (setq command-history (cons newcmd command-history)))
659 (defvar minibuffer-history nil
660 "Default minibuffer history list.
661 This is used for all minibuffer input
662 except when an alternate history list is specified.")
663 (defvar minibuffer-history-sexp-flag nil
664 "Non-nil when doing history operations on `command-history'.
665 More generally, indicates that the history list being acted on
666 contains expressions rather than strings.
667 It is only valid if its value equals the current minibuffer depth,
668 to handle recursive uses of the minibuffer.")
669 (setq minibuffer-history-variable 'minibuffer-history)
670 (setq minibuffer-history-position nil)
671 (defvar minibuffer-history-search-history nil)
674 (lambda (key-and-command)
676 (lambda (keymap-and-completionp)
677 ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
678 ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
679 ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
680 (define-key (symbol-value (car keymap-and-completionp))
681 (car key-and-command)
682 (let ((command (cdr key-and-command)))
684 ;; (and ... nil) => ... turns back on the completion-oriented
685 ;; history commands which rms turned off since they seem to
686 ;; do things he doesn't like.
687 (if (and (cdr keymap-and-completionp) nil) ;XXX turned off
688 (progn (error "EMACS BUG!") (cdr command))
691 '((minibuffer-local-map . nil)
692 (minibuffer-local-ns-map . nil)
693 (minibuffer-local-completion-map . t)
694 (minibuffer-local-must-match-map . t)
695 (read-expression-map . nil))))
696 '(("\en" . (next-history-element . next-complete-history-element))
697 ([next] . (next-history-element . next-complete-history-element))
698 ("\ep" . (previous-history-element . previous-complete-history-element))
699 ([prior] . (previous-history-element . previous-complete-history-element))
700 ("\er" . previous-matching-history-element)
701 ("\es" . next-matching-history-element)))
703 (defvar minibuffer-text-before-history nil
704 "Text that was in this minibuffer before any history commands.
705 This is nil if there have not yet been any history commands
706 in this use of the minibuffer.")
708 (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize)
710 (defun minibuffer-history-initialize ()
711 (setq minibuffer-text-before-history nil))
713 (defcustom minibuffer-history-case-insensitive-variables nil
714 "*Minibuffer history variables for which matching should ignore case.
715 If a history variable is a member of this list, then the
716 \\[previous-matching-history-element] and \\[next-matching-history-element]\
717 commands ignore case when searching it, regardless of `case-fold-search'."
718 :type '(repeat variable)
721 (defun previous-matching-history-element (regexp n)
722 "Find the previous history element that matches REGEXP.
723 \(Previous history elements refer to earlier actions.)
724 With prefix argument N, search for Nth previous match.
725 If N is negative, find the next or Nth next match.
726 An uppercase letter in REGEXP makes the search case-sensitive.
727 See also `minibuffer-history-case-insensitive-variables'."
729 (let* ((enable-recursive-minibuffers t)
730 (regexp (read-from-minibuffer "Previous element matching (regexp): "
734 'minibuffer-history-search-history)))
735 ;; Use the last regexp specified, by default, if input is empty.
736 (list (if (string= regexp "")
737 (if minibuffer-history-search-history
738 (car minibuffer-history-search-history)
739 (error "No previous history search regexp"))
741 (prefix-numeric-value current-prefix-arg))))
742 (if (and (zerop minibuffer-history-position)
743 (null minibuffer-text-before-history))
744 (setq minibuffer-text-before-history (buffer-string)))
745 (let ((history (symbol-value minibuffer-history-variable))
747 (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
748 ;; On some systems, ignore case for file names.
749 (if (memq minibuffer-history-variable
750 minibuffer-history-case-insensitive-variables)
752 ;; Respect the user's setting for case-fold-search:
756 (pos minibuffer-history-position))
759 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
762 "No later matching history item"
763 "No earlier matching history item")))
764 (if (string-match regexp
765 (if (eq minibuffer-history-sexp-flag
767 (let ((print-level nil))
768 (prin1-to-string (nth (1- pos) history)))
769 (nth (1- pos) history)))
770 (setq n (+ n (if (< n 0) 1 -1)))))
771 (setq minibuffer-history-position pos)
773 (let ((elt (nth (1- pos) history)))
774 (insert (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
775 (let ((print-level nil))
776 (prin1-to-string elt))
778 (goto-char (point-min)))
779 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
780 (eq (car (car command-history)) 'next-matching-history-element))
781 (setq command-history (cdr command-history))))
783 (defun next-matching-history-element (regexp n)
784 "Find the next history element that matches REGEXP.
785 \(The next history element refers to a more recent action.)
786 With prefix argument N, search for Nth next match.
787 If N is negative, find the previous or Nth previous match.
788 An uppercase letter in REGEXP makes the search case-sensitive."
790 (let* ((enable-recursive-minibuffers t)
791 (regexp (read-from-minibuffer "Next element matching (regexp): "
795 'minibuffer-history-search-history)))
796 ;; Use the last regexp specified, by default, if input is empty.
797 (list (if (string= regexp "")
798 (setcar minibuffer-history-search-history
799 (nth 1 minibuffer-history-search-history))
801 (prefix-numeric-value current-prefix-arg))))
802 (previous-matching-history-element regexp (- n)))
804 (defun next-history-element (n)
805 "Insert the next element of the minibuffer history into the minibuffer."
808 (let ((narg (- minibuffer-history-position n))
809 (minimum (if minibuffer-default -1 0))
811 (if (and (zerop minibuffer-history-position)
812 (null minibuffer-text-before-history))
813 (setq minibuffer-text-before-history (buffer-string)))
815 (if minibuffer-default
816 (error "End of history; no next item")
817 (error "End of history; no default available")))
818 (if (> narg (length (symbol-value minibuffer-history-variable)))
819 (error "Beginning of history; no preceding item"))
821 (setq minibuffer-history-position narg)
823 (setq elt minibuffer-default))
825 (setq elt (or minibuffer-text-before-history ""))
826 (setq minibuffer-text-before-history nil))
827 (t (setq elt (nth (1- minibuffer-history-position)
828 (symbol-value minibuffer-history-variable)))))
830 (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
831 (let ((print-level nil))
832 (prin1-to-string elt))
834 (goto-char (point-min)))))
836 (defun previous-history-element (n)
837 "Inserts the previous element of the minibuffer history into the minibuffer."
839 (next-history-element (- n)))
841 (defun next-complete-history-element (n)
842 "Get next element of history which is a completion of minibuffer contents."
844 (let ((point-at-start (point)))
845 (next-matching-history-element
846 (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
847 ;; next-matching-history-element always puts us at (point-min).
848 ;; Move to the position we were at before changing the buffer contents.
849 ;; This is still sensical, because the text before point has not changed.
850 (goto-char point-at-start)))
852 (defun previous-complete-history-element (n)
854 Get previous element of history which is a completion of minibuffer contents."
856 (next-complete-history-element (- n)))
858 (defun goto-line (arg)
859 "Goto line ARG, counting from line 1 at beginning of buffer."
860 (interactive "NGoto line: ")
861 (setq arg (prefix-numeric-value arg))
865 (if (eq selective-display t)
866 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
867 (forward-line (1- arg)))))
869 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
870 (defalias 'advertised-undo 'undo)
872 (defun undo (&optional arg)
873 "Undo some previous changes.
874 Repeat this command to undo more changes.
875 A numeric argument serves as a repeat count.
877 Just C-u as argument requests selective undo,
878 limited to changes within the current region.
879 Likewise in Transient Mark mode when the mark is active."
881 ;; If we don't get all the way thru, make last-command indicate that
882 ;; for the following command.
883 (setq this-command t)
884 (let ((modified (buffer-modified-p))
885 (recent-save (recent-auto-save-p)))
886 (or (eq (selected-window) (minibuffer-window))
888 (or (eq last-command 'undo)
889 (progn (if (or arg (and transient-mark-mode mark-active))
890 (undo-start (region-beginning) (region-end))
893 (undo-more (if arg (prefix-numeric-value arg) 1))
894 ;; Don't specify a position in the undo record for the undo command.
895 ;; Instead, undoing this should move point to where the change is.
896 (let ((tail buffer-undo-list)
898 (while (and tail (not done) (not (null (car tail))))
899 (if (integerp (car tail))
902 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
903 (setq tail (cdr tail))))
904 (and modified (not (buffer-modified-p))
905 (delete-auto-save-file-if-necessary recent-save)))
906 ;; If we do get all the way thru, make this-command indicate that.
907 (setq this-command 'undo))
909 (defvar pending-undo-list nil
910 "Within a run of consecutive undo commands, list remaining to be undone.")
912 (defvar undo-in-progress nil
913 "Non-nil while performing an undo.
914 Some change-hooks test this variable to do something different.")
916 (defun undo-more (count)
917 "Undo back N undo-boundaries beyond what was already undone recently.
918 Call `undo-start' to get ready to undo recent changes,
919 then call `undo-more' one or more times to undo them."
920 (or pending-undo-list
921 (error "No further undo information"))
922 (let ((undo-in-progress t))
923 (setq pending-undo-list (primitive-undo count pending-undo-list))))
925 ;; Deep copy of a list
926 (defun undo-copy-list (list)
927 "Make a copy of undo list LIST."
928 (mapcar 'undo-copy-list-1 list))
930 (defun undo-copy-list-1 (elt)
932 (cons (car elt) (undo-copy-list-1 (cdr elt)))
935 (defun undo-start (&optional beg end)
936 "Set `pending-undo-list' to the front of the undo list.
937 The next call to `undo-more' will undo the most recently made change.
938 If BEG and END are specified, then only undo elements
939 that apply to text between BEG and END are used; other undo elements
940 are ignored. If BEG and END are nil, all undo elements are used."
941 (if (eq buffer-undo-list t)
942 (error "No undo information in this buffer"))
943 (setq pending-undo-list
944 (if (and beg end (not (= beg end)))
945 (undo-make-selective-list (min beg end) (max beg end))
948 (defvar undo-adjusted-markers)
950 (defun undo-make-selective-list (start end)
951 "Return a list of undo elements for the region START to END.
952 The elements come from `buffer-undo-list', but we keep only
953 the elements inside this region, and discard those outside this region.
954 If we find an element that crosses an edge of this region,
955 we stop and ignore all further elements."
956 (let ((undo-list-copy (undo-copy-list buffer-undo-list))
957 (undo-list (list nil))
958 undo-adjusted-markers
960 undo-elt undo-elt temp-undo-list delta)
961 (while undo-list-copy
962 (setq undo-elt (car undo-list-copy))
964 (cond ((and (consp undo-elt) (eq (car undo-elt) t))
965 ;; This is a "was unmodified" element.
966 ;; Keep it if we have kept everything thus far.
969 (undo-elt-in-region undo-elt start end)))))
972 (setq end (+ end (cdr (undo-delta undo-elt))))
973 ;; Don't put two nils together in the list
974 (if (not (and (eq (car undo-list) nil)
976 (setq undo-list (cons undo-elt undo-list))))
977 (if (undo-elt-crosses-region undo-elt start end)
978 (setq undo-list-copy nil)
979 (setq some-rejected t)
980 (setq temp-undo-list (cdr undo-list-copy))
981 (setq delta (undo-delta undo-elt))
983 (when (/= (cdr delta) 0)
984 (let ((position (car delta))
985 (offset (cdr delta)))
987 ;; Loop down the earlier events adjusting their buffer positions
988 ;; to reflect the fact that a change to the buffer isn't being
989 ;; undone. We only need to process those element types which
990 ;; undo-elt-in-region will return as being in the region since
991 ;; only those types can ever get into the output
993 (while temp-undo-list
994 (setq undo-elt (car temp-undo-list))
995 (cond ((integerp undo-elt)
996 (if (>= undo-elt position)
997 (setcar temp-undo-list (- undo-elt offset))))
998 ((atom undo-elt) nil)
999 ((stringp (car undo-elt))
1000 ;; (TEXT . POSITION)
1001 (let ((text-pos (abs (cdr undo-elt)))
1002 (point-at-end (< (cdr undo-elt) 0 )))
1003 (if (>= text-pos position)
1004 (setcdr undo-elt (* (if point-at-end -1 1)
1005 (- text-pos offset))))))
1006 ((integerp (car undo-elt))
1008 (when (>= (car undo-elt) position)
1009 (setcar undo-elt (- (car undo-elt) offset))
1010 (setcdr undo-elt (- (cdr undo-elt) offset))))
1011 ((null (car undo-elt))
1012 ;; (nil PROPERTY VALUE BEG . END)
1013 (let ((tail (nthcdr 3 undo-elt)))
1014 (when (>= (car tail) position)
1015 (setcar tail (- (car tail) offset))
1016 (setcdr tail (- (cdr tail) offset))))))
1017 (setq temp-undo-list (cdr temp-undo-list))))))))
1018 (setq undo-list-copy (cdr undo-list-copy)))
1019 (nreverse undo-list)))
1021 (defun undo-elt-in-region (undo-elt start end)
1022 "Determine whether UNDO-ELT falls inside the region START ... END.
1023 If it crosses the edge, we return nil."
1024 (cond ((integerp undo-elt)
1025 (and (>= undo-elt start)
1031 ((stringp (car undo-elt))
1032 ;; (TEXT . POSITION)
1033 (and (>= (abs (cdr undo-elt)) start)
1034 (< (abs (cdr undo-elt)) end)))
1035 ((and (consp undo-elt) (markerp (car undo-elt)))
1036 ;; This is a marker-adjustment element (MARKER . ADJUSTMENT).
1037 ;; See if MARKER is inside the region.
1038 (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers)))
1040 (setq alist-elt (cons (car undo-elt)
1041 (marker-position (car undo-elt))))
1042 (setq undo-adjusted-markers
1043 (cons alist-elt undo-adjusted-markers)))
1044 (and (cdr alist-elt)
1045 (>= (cdr alist-elt) start)
1046 (< (cdr alist-elt) end))))
1047 ((null (car undo-elt))
1048 ;; (nil PROPERTY VALUE BEG . END)
1049 (let ((tail (nthcdr 3 undo-elt)))
1050 (and (>= (car tail) start)
1051 (< (cdr tail) end))))
1052 ((integerp (car undo-elt))
1054 (and (>= (car undo-elt) start)
1055 (< (cdr undo-elt) end)))))
1057 (defun undo-elt-crosses-region (undo-elt start end)
1058 "Test whether UNDO-ELT crosses one edge of that region START ... END.
1059 This assumes we have already decided that UNDO-ELT
1060 is not *inside* the region START...END."
1061 (cond ((atom undo-elt) nil)
1062 ((null (car undo-elt))
1063 ;; (nil PROPERTY VALUE BEG . END)
1064 (let ((tail (nthcdr 3 undo-elt)))
1065 (not (or (< (car tail) end)
1066 (> (cdr tail) start)))))
1067 ((integerp (car undo-elt))
1069 (not (or (< (car undo-elt) end)
1070 (> (cdr undo-elt) start))))))
1072 ;; Return the first affected buffer position and the delta for an undo element
1073 ;; delta is defined as the change in subsequent buffer positions if we *did*
1075 (defun undo-delta (undo-elt)
1076 (if (consp undo-elt)
1077 (cond ((stringp (car undo-elt))
1078 ;; (TEXT . POSITION)
1079 (cons (abs (cdr undo-elt)) (length (car undo-elt))))
1080 ((integerp (car undo-elt))
1082 (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt))))
1087 (defvar shell-command-history nil
1088 "History list for some commands that read shell commands.")
1090 (defvar shell-command-switch "-c"
1091 "Switch used to have the shell execute its command line argument.")
1093 (defun shell-command (command &optional output-buffer)
1094 "Execute string COMMAND in inferior shell; display output, if any.
1096 If COMMAND ends in ampersand, execute it asynchronously.
1097 The output appears in the buffer `*Async Shell Command*'.
1098 That buffer is in shell mode.
1100 Otherwise, COMMAND is executed synchronously. The output appears in the
1101 buffer `*Shell Command Output*'.
1102 If the output is one line, it is displayed in the echo area *as well*,
1103 but it is nonetheless available in buffer `*Shell Command Output*',
1104 even though that buffer is not automatically displayed.
1105 If there is no output, or if output is inserted in the current buffer,
1106 then `*Shell Command Output*' is deleted.
1108 To specify a coding system for converting non-ASCII characters
1109 in the shell command output, use \\[universal-coding-system-argument]
1110 before this command.
1112 Noninteractive callers can specify coding systems by binding
1113 `coding-system-for-read' and `coding-system-for-write'.
1115 The optional second argument OUTPUT-BUFFER, if non-nil,
1116 says to put the output in some other buffer.
1117 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1118 If OUTPUT-BUFFER is not a buffer and not nil,
1119 insert output in current buffer. (This cannot be done asynchronously.)
1120 In either case, the output is inserted after point (leaving mark after it)."
1121 (interactive (list (read-from-minibuffer "Shell command: "
1122 nil nil nil 'shell-command-history)
1123 current-prefix-arg))
1124 ;; Look for a handler in case default-directory is a remote file name.
1126 (find-file-name-handler (directory-file-name default-directory)
1129 (funcall handler 'shell-command command output-buffer)
1130 (if (and output-buffer
1131 (not (or (bufferp output-buffer) (stringp output-buffer))))
1132 (progn (barf-if-buffer-read-only)
1134 ;; We do not use -f for csh; we will not support broken use of
1135 ;; .cshrcs. Even the BSD csh manual says to use
1136 ;; "if ($?prompt) exit" before things which are not useful
1137 ;; non-interactively. Besides, if someone wants their other
1138 ;; aliases for shell commands then they can still have them.
1139 (call-process shell-file-name nil t nil
1140 shell-command-switch command)
1141 ;; This is like exchange-point-and-mark, but doesn't
1142 ;; activate the mark. It is cleaner to avoid activation,
1143 ;; even though the command loop would deactivate the mark
1144 ;; because we inserted text.
1145 (goto-char (prog1 (mark t)
1146 (set-marker (mark-marker) (point)
1147 (current-buffer)))))
1148 ;; Preserve the match data in case called from a program.
1150 (if (string-match "[ \t]*&[ \t]*$" command)
1151 ;; Command ending with ampersand means asynchronous.
1152 (let ((buffer (get-buffer-create
1153 (or output-buffer "*Async Shell Command*")))
1154 (directory default-directory)
1156 ;; Remove the ampersand.
1157 (setq command (substring command 0 (match-beginning 0)))
1158 ;; If will kill a process, query first.
1159 (setq proc (get-buffer-process buffer))
1161 (if (yes-or-no-p "A command is running. Kill it? ")
1163 (error "Shell command in progress")))
1166 (setq buffer-read-only nil)
1168 (display-buffer buffer)
1169 (setq default-directory directory)
1170 (setq proc (start-process "Shell" buffer shell-file-name
1171 shell-command-switch command))
1172 (setq mode-line-process '(":%s"))
1173 (require 'shell) (shell-mode)
1174 (set-process-sentinel proc 'shell-command-sentinel)
1176 (shell-command-on-region (point) (point) command output-buffer)
1179 ;; We have a sentinel to prevent insertion of a termination message
1180 ;; in the buffer itself.
1181 (defun shell-command-sentinel (process signal)
1182 (if (memq (process-status process) '(exit signal))
1184 (car (cdr (cdr (process-command process))))
1185 (substring signal 0 -1))))
1187 (defvar shell-command-on-region-default-error-buffer nil
1188 "*Name of buffer that `shell-command-on-region' uses for stderr.
1189 This buffer is used when `shell-command-on-region' is run interactively.
1190 A nil value for this variable means that output to stderr and stdout
1191 will be intermixed in the output stream.")
1193 (defun shell-command-on-region (start end command
1194 &optional output-buffer replace
1196 "Execute string COMMAND in inferior shell with region as input.
1197 Normally display output (if any) in temp buffer `*Shell Command Output*';
1198 Prefix arg means replace the region with it. Return the exit code of
1201 To specify a coding system for converting non-ASCII characters
1202 in the input and output to the shell command, use \\[universal-coding-system-argument]
1203 before this command. By default, the input (from the current buffer)
1204 is encoded in the same coding system that will be used to save the file,
1205 `buffer-file-coding-system'. If the output is going to replace the region,
1206 then it is decoded from that same coding system.
1208 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER,
1209 REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding
1210 systems by binding `coding-system-for-read' and
1211 `coding-system-for-write'.
1213 If the output is one line, it is displayed in the echo area,
1214 but it is nonetheless available in buffer `*Shell Command Output*'
1215 even though that buffer is not automatically displayed.
1216 If there is no output, or if output is inserted in the current buffer,
1217 then `*Shell Command Output*' is deleted.
1219 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1220 that says to put the output in some other buffer.
1221 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1222 If OUTPUT-BUFFER is not a buffer and not nil,
1223 insert output in the current buffer.
1224 In either case, the output is inserted after point (leaving mark after it).
1226 If REPLACE, the optional fifth argument, is non-nil, that means insert
1227 the output in place of text from START to END, putting point and mark
1230 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1231 or buffer name to which to direct the command's standard error output.
1232 If it is nil, error output is mingled with regular output.
1233 In an interactive call, the variable
1234 `shell-command-on-region-default-error-buffer' specifies the value
1236 (interactive (let ((string
1237 ;; Do this before calling region-beginning
1238 ;; and region-end, in case subprocess output
1239 ;; relocates them while we are in the minibuffer.
1240 (read-from-minibuffer "Shell command on region: "
1242 'shell-command-history)))
1243 ;; call-interactively recognizes region-beginning and
1244 ;; region-end specially, leaving them in the history.
1245 (list (region-beginning) (region-end)
1249 shell-command-on-region-default-error-buffer)))
1252 (concat (file-name-directory temp-file-name-pattern)
1253 (make-temp-name "scor"))
1258 (not (or (bufferp output-buffer) (stringp output-buffer))))
1259 (equal (buffer-name (current-buffer)) "*Shell Command Output*"))
1260 ;; Replace specified region with output from command.
1261 (let ((swap (and replace (< start end))))
1262 ;; Don't muck with mark unless REPLACE says we should.
1264 (and replace (push-mark))
1266 (call-process-region start end shell-file-name t
1270 nil shell-command-switch command))
1271 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1272 (and shell-buffer (not (eq shell-buffer (current-buffer)))
1273 (kill-buffer shell-buffer)))
1274 ;; Don't muck with mark unless REPLACE says we should.
1275 (and replace swap (exchange-point-and-mark)))
1276 ;; No prefix argument: put the output in a temp buffer,
1277 ;; replacing its entire contents.
1278 (let ((buffer (get-buffer-create
1279 (or output-buffer "*Shell Command Output*")))
1282 (if (eq buffer (current-buffer))
1283 ;; If the input is the same buffer as the output,
1284 ;; delete everything but the specified region,
1285 ;; then replace that region with the output.
1286 (progn (setq buffer-read-only nil)
1287 (delete-region (max start end) (point-max))
1288 (delete-region (point-min) (min start end))
1290 (call-process-region (point-min) (point-max)
1295 nil shell-command-switch
1297 ;; Clear the output buffer, then run the command with
1301 (setq buffer-read-only nil)
1304 (call-process-region start end shell-file-name nil
1306 (list buffer error-file)
1308 nil shell-command-switch command)))
1309 (setq success (zerop exit-status))
1310 ;; Report the amount of output.
1311 (let ((lines (save-excursion
1313 (if (= (buffer-size) 0)
1315 (count-lines (point-min) (point-max))))))
1318 (message "(Shell command %sed with no output)"
1319 (if (equal 0 exit-status)
1322 (kill-buffer buffer))
1323 ((and success (= lines 1))
1327 (goto-char (point-min))
1328 (buffer-substring (point)
1329 (progn (end-of-line) (point))))))
1333 (goto-char (point-min)))
1334 (display-buffer buffer)))))))
1335 (if (and error-file (file-exists-p error-file))
1337 (set-buffer (get-buffer-create error-buffer))
1338 ;; Do no formatting while reading error file, for fear of looping.
1339 (format-insert-file error-file nil)
1340 (delete-file error-file)))
1343 (defun shell-command-to-string (command)
1344 "Execute shell command COMMAND and return its output as a string."
1345 (with-output-to-string
1346 (with-current-buffer
1348 (call-process shell-file-name nil t nil shell-command-switch command))))
1350 (defvar universal-argument-map
1351 (let ((map (make-sparse-keymap)))
1352 (define-key map [t] 'universal-argument-other-key)
1353 (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
1354 (define-key map [switch-frame] nil)
1355 (define-key map [?\C-u] 'universal-argument-more)
1356 (define-key map [?-] 'universal-argument-minus)
1357 (define-key map [?0] 'digit-argument)
1358 (define-key map [?1] 'digit-argument)
1359 (define-key map [?2] 'digit-argument)
1360 (define-key map [?3] 'digit-argument)
1361 (define-key map [?4] 'digit-argument)
1362 (define-key map [?5] 'digit-argument)
1363 (define-key map [?6] 'digit-argument)
1364 (define-key map [?7] 'digit-argument)
1365 (define-key map [?8] 'digit-argument)
1366 (define-key map [?9] 'digit-argument)
1368 "Keymap used while processing \\[universal-argument].")
1370 (defvar universal-argument-num-events nil
1371 "Number of argument-specifying events read by `universal-argument'.
1372 `universal-argument-other-key' uses this to discard those events
1373 from (this-command-keys), and reread only the final command.")
1375 (defun universal-argument ()
1376 "Begin a numeric argument for the following command.
1377 Digits or minus sign following \\[universal-argument] make up the numeric argument.
1378 \\[universal-argument] following the digits or minus sign ends the argument.
1379 \\[universal-argument] without digits or minus sign provides 4 as argument.
1380 Repeating \\[universal-argument] without digits or minus sign
1381 multiplies the argument by 4 each time.
1382 For some commands, just \\[universal-argument] by itself serves as a flag
1383 which is different in effect from any particular numeric argument.
1384 These commands include \\[set-mark-command] and \\[start-kbd-macro]."
1386 (setq prefix-arg (list 4))
1387 (setq universal-argument-num-events (length (this-command-keys)))
1388 (setq overriding-terminal-local-map universal-argument-map))
1390 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1391 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1392 (defun universal-argument-more (arg)
1395 (setq prefix-arg (list (* 4 (car arg))))
1397 (setq prefix-arg (list -4))
1398 (setq prefix-arg arg)
1399 (setq overriding-terminal-local-map nil)))
1400 (setq universal-argument-num-events (length (this-command-keys))))
1402 (defun negative-argument (arg)
1403 "Begin a negative numeric argument for the next command.
1404 \\[universal-argument] following digits or minus sign ends the argument."
1406 (cond ((integerp arg)
1407 (setq prefix-arg (- arg)))
1409 (setq prefix-arg nil))
1411 (setq prefix-arg '-)))
1412 (setq universal-argument-num-events (length (this-command-keys)))
1413 (setq overriding-terminal-local-map universal-argument-map))
1415 (defun digit-argument (arg)
1416 "Part of the numeric argument for the next command.
1417 \\[universal-argument] following digits or minus sign ends the argument."
1419 (let ((digit (- (logand last-command-char ?\177) ?0)))
1420 (cond ((integerp arg)
1421 (setq prefix-arg (+ (* arg 10)
1422 (if (< arg 0) (- digit) digit))))
1424 ;; Treat -0 as just -, so that -01 will work.
1425 (setq prefix-arg (if (zerop digit) '- (- digit))))
1427 (setq prefix-arg digit))))
1428 (setq universal-argument-num-events (length (this-command-keys)))
1429 (setq overriding-terminal-local-map universal-argument-map))
1431 ;; For backward compatibility, minus with no modifiers is an ordinary
1432 ;; command if digits have already been entered.
1433 (defun universal-argument-minus (arg)
1436 (universal-argument-other-key arg)
1437 (negative-argument arg)))
1439 ;; Anything else terminates the argument and is left in the queue to be
1440 ;; executed as a command.
1441 (defun universal-argument-other-key (arg)
1443 (setq prefix-arg arg)
1444 (let* ((key (this-command-keys))
1445 (keylist (listify-key-sequence key)))
1446 (setq unread-command-events
1447 (append (nthcdr universal-argument-num-events keylist)
1448 unread-command-events)))
1449 (reset-this-command-lengths)
1450 (setq overriding-terminal-local-map nil))
1452 (defun forward-to-indentation (arg)
1453 "Move forward ARG lines and position at first nonblank character."
1456 (skip-chars-forward " \t"))
1458 (defun backward-to-indentation (arg)
1459 "Move backward ARG lines and position at first nonblank character."
1461 (forward-line (- arg))
1462 (skip-chars-forward " \t"))
1464 (defcustom kill-whole-line nil
1465 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
1469 (defun kill-line (&optional arg)
1470 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
1471 With prefix argument, kill that many lines from point.
1472 Negative arguments kill lines backward.
1474 When calling from a program, nil means \"no arg\",
1475 a number counts as a prefix arg.
1477 To kill a whole line, when point is not at the beginning, type \
1478 \\[beginning-of-line] \\[kill-line] \\[kill-line].
1480 If `kill-whole-line' is non-nil, then this command kills the whole line
1481 including its terminating newline, when used at the beginning of a line
1482 with no argument. As a consequence, you can always kill a whole line
1483 by typing \\[beginning-of-line] \\[kill-line]."
1485 (kill-region (point)
1486 ;; It is better to move point to the other end of the kill
1487 ;; before killing. That way, in a read-only buffer, point
1488 ;; moves across the text that is copied to the kill ring.
1489 ;; The choice has no effect on undo now that undo records
1490 ;; the value of point from before the command was run.
1493 (forward-visible-line (prefix-numeric-value arg))
1495 (signal 'end-of-buffer nil))
1496 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
1497 (forward-visible-line 1)
1498 (end-of-visible-line)))
1501 (defun forward-visible-line (arg)
1502 "Move forward by ARG lines, ignoring currently invisible newlines only.
1503 If ARG is negative, move backward -ARG lines.
1504 If ARG is zero, move to the beginning of the current line."
1508 (or (zerop (forward-line 1))
1509 (signal 'end-of-buffer nil))
1510 ;; If the following character is currently invisible,
1511 ;; skip all characters with that same `invisible' property value,
1512 ;; then find the next newline.
1513 (while (and (not (eobp))
1515 (get-char-property (point) 'invisible)))
1516 (if (eq buffer-invisibility-spec t)
1518 (or (memq prop buffer-invisibility-spec)
1519 (assq prop buffer-invisibility-spec)))))
1521 (if (get-text-property (point) 'invisible)
1522 (or (next-single-property-change (point) 'invisible)
1524 (next-overlay-change (point))))
1525 (or (zerop (forward-line 1))
1526 (signal 'end-of-buffer nil)))
1527 (setq arg (1- arg)))
1529 (while (or first (< arg 0))
1532 (or (zerop (forward-line -1))
1533 (signal 'beginning-of-buffer nil)))
1534 (while (and (not (bobp))
1536 (get-char-property (1- (point)) 'invisible)))
1537 (if (eq buffer-invisibility-spec t)
1539 (or (memq prop buffer-invisibility-spec)
1540 (assq prop buffer-invisibility-spec)))))
1542 (if (get-text-property (1- (point)) 'invisible)
1543 (or (previous-single-property-change (point) 'invisible)
1545 (previous-overlay-change (point))))
1546 (or (zerop (forward-line -1))
1547 (signal 'beginning-of-buffer nil)))
1549 (setq arg (1+ arg)))))
1550 ((beginning-of-buffer end-of-buffer)
1553 (defun end-of-visible-line ()
1554 "Move to end of current visible line."
1556 ;; If the following character is currently invisible,
1557 ;; skip all characters with that same `invisible' property value,
1558 ;; then find the next newline.
1559 (while (and (not (eobp))
1561 (get-char-property (point) 'invisible)))
1562 (if (eq buffer-invisibility-spec t)
1564 (or (memq prop buffer-invisibility-spec)
1565 (assq prop buffer-invisibility-spec)))))
1566 (if (get-text-property (point) 'invisible)
1567 (goto-char (next-single-property-change (point) 'invisible))
1568 (goto-char (next-overlay-change (point))))
1571 ;;;; Window system cut and paste hooks.
1573 (defvar interprogram-cut-function nil
1574 "Function to call to make a killed region available to other programs.
1576 Most window systems provide some sort of facility for cutting and
1577 pasting text between the windows of different programs.
1578 This variable holds a function that Emacs calls whenever text
1579 is put in the kill ring, to make the new kill available to other
1582 The function takes one or two arguments.
1583 The first argument, TEXT, is a string containing
1584 the text which should be made available.
1585 The second, PUSH, if non-nil means this is a \"new\" kill;
1586 nil means appending to an \"old\" kill.")
1588 (defvar interprogram-paste-function nil
1589 "Function to call to get text cut from other programs.
1591 Most window systems provide some sort of facility for cutting and
1592 pasting text between the windows of different programs.
1593 This variable holds a function that Emacs calls to obtain
1594 text that other programs have provided for pasting.
1596 The function should be called with no arguments. If the function
1597 returns nil, then no other program has provided such text, and the top
1598 of the Emacs kill ring should be used. If the function returns a
1599 string, that string should be put in the kill ring as the latest kill.
1601 Note that the function should return a string only if a program other
1602 than Emacs has provided a string for pasting; if Emacs provided the
1603 most recent string, the function should return nil. If it is
1604 difficult to tell whether Emacs or some other program provided the
1605 current string, it is probably good enough to return nil if the string
1606 is equal (according to `string=') to the last text Emacs provided.")
1610 ;;;; The kill ring data structure.
1612 (defvar kill-ring nil
1613 "List of killed text sequences.
1614 Since the kill ring is supposed to interact nicely with cut-and-paste
1615 facilities offered by window systems, use of this variable should
1616 interact nicely with `interprogram-cut-function' and
1617 `interprogram-paste-function'. The functions `kill-new',
1618 `kill-append', and `current-kill' are supposed to implement this
1619 interaction; you may want to use them instead of manipulating the kill
1622 (defcustom kill-ring-max 60
1623 "*Maximum length of kill ring before oldest elements are thrown away."
1627 (defvar kill-ring-yank-pointer nil
1628 "The tail of the kill ring whose car is the last thing yanked.")
1630 (defun kill-new (string &optional replace)
1631 "Make STRING the latest kill in the kill ring.
1632 Set the kill-ring-yank pointer to point to it.
1633 If `interprogram-cut-function' is non-nil, apply it to STRING.
1634 Optional second argument REPLACE non-nil means that STRING will replace
1635 the front of the kill ring, rather than being added to the list."
1636 (and (fboundp 'menu-bar-update-yank-menu)
1637 (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1639 (setcar kill-ring string)
1640 (setq kill-ring (cons string kill-ring))
1641 (if (> (length kill-ring) kill-ring-max)
1642 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1643 (setq kill-ring-yank-pointer kill-ring)
1644 (if interprogram-cut-function
1645 (funcall interprogram-cut-function string (not replace))))
1647 (defun kill-append (string before-p)
1648 "Append STRING to the end of the latest kill in the kill ring.
1649 If BEFORE-P is non-nil, prepend STRING to the kill.
1650 If `interprogram-cut-function' is set, pass the resulting kill to
1652 (kill-new (if before-p
1653 (concat string (car kill-ring))
1654 (concat (car kill-ring) string)) t))
1656 (defun current-kill (n &optional do-not-move)
1657 "Rotate the yanking point by N places, and then return that kill.
1658 If N is zero, `interprogram-paste-function' is set, and calling it
1659 returns a string, then that string is added to the front of the
1660 kill ring and returned as the latest kill.
1661 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1662 yanking point; just return the Nth kill forward."
1663 (let ((interprogram-paste (and (= n 0)
1664 interprogram-paste-function
1665 (funcall interprogram-paste-function))))
1666 (if interprogram-paste
1668 ;; Disable the interprogram cut function when we add the new
1669 ;; text to the kill ring, so Emacs doesn't try to own the
1670 ;; selection, with identical text.
1671 (let ((interprogram-cut-function nil))
1672 (kill-new interprogram-paste))
1674 (or kill-ring (error "Kill ring is empty"))
1675 (let ((ARGth-kill-element
1676 (nthcdr (mod (- n (length kill-ring-yank-pointer))
1680 (setq kill-ring-yank-pointer ARGth-kill-element))
1681 (car ARGth-kill-element)))))
1685 ;;;; Commands for manipulating the kill ring.
1687 (defcustom kill-read-only-ok nil
1688 "*Non-nil means don't signal an error for killing read-only text."
1692 (put 'text-read-only 'error-conditions
1693 '(text-read-only buffer-read-only error))
1694 (put 'text-read-only 'error-message "Text is read-only")
1696 (defun kill-region (beg end)
1697 "Kill between point and mark.
1698 The text is deleted but saved in the kill ring.
1699 The command \\[yank] can retrieve it from there.
1700 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
1701 If the buffer is read-only, Emacs will beep and refrain from deleting
1702 the text, but put the text in the kill ring anyway. This means that
1703 you can use the killing commands to copy text from a read-only buffer.
1705 This is the primitive for programs to kill text (as opposed to deleting it).
1706 Supply two arguments, character numbers indicating the stretch of text
1708 Any command that calls this function is a \"kill command\".
1709 If the previous command was also a kill command,
1710 the text killed this time appends to the text killed last time
1711 to make one entry in the kill ring."
1714 ;; Don't let the undo list be truncated before we can even access it.
1715 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))
1716 (old-list buffer-undo-list)
1718 ;; If we can't rely on finding the killed text
1719 ;; in the undo list, save it now as a string.
1720 (string (if (or (eq buffer-undo-list t)
1722 (buffer-substring beg end))))
1723 (delete-region beg end)
1724 ;; Search back in buffer-undo-list for this string,
1725 ;; in case a change hook made property changes.
1726 (setq tail buffer-undo-list)
1728 (while (not (stringp (car (car tail))))
1729 (setq tail (cdr tail)))
1730 ;; If we did not already make the string to use,
1731 ;; use the same one that undo made for us.
1732 (setq string (car (car tail))))
1733 ;; Add that string to the kill ring, one way or another.
1734 (if (eq last-command 'kill-region)
1735 (kill-append string (< end beg))
1737 (setq this-command 'kill-region))
1738 ((buffer-read-only text-read-only)
1739 ;; The code above failed because the buffer, or some of the characters
1740 ;; in the region, are read-only.
1741 ;; We should beep, in case the user just isn't aware of this.
1742 ;; However, there's no harm in putting
1743 ;; the region's text in the kill ring, anyway.
1744 (copy-region-as-kill beg end)
1745 ;; Set this-command now, so it will be set even if we get an error.
1746 (setq this-command 'kill-region)
1747 ;; This should barf, if appropriate, and give us the correct error.
1748 (if kill-read-only-ok
1749 (message "Read only text copied to kill ring")
1750 ;; Signal an error if the buffer is read-only.
1751 (barf-if-buffer-read-only)
1752 ;; If the buffer isn't read-only, the text is.
1753 (signal 'text-read-only (list (current-buffer)))))))
1755 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1756 ;; to get two copies of the text when the user accidentally types M-w and
1757 ;; then corrects it with the intended C-w.
1758 (defun copy-region-as-kill (beg end)
1759 "Save the region as if killed, but don't kill it.
1760 In Transient Mark mode, deactivate the mark.
1761 If `interprogram-cut-function' is non-nil, also save the text for a window
1762 system cut and paste."
1764 (if (eq last-command 'kill-region)
1765 (kill-append (buffer-substring beg end) (< end beg))
1766 (kill-new (buffer-substring beg end)))
1767 (if transient-mark-mode
1768 (setq deactivate-mark t))
1771 (defun kill-ring-save (beg end)
1772 "Save the region as if killed, but don't kill it.
1773 In Transient Mark mode, deactivate the mark.
1774 If `interprogram-cut-function' is non-nil, also save the text for a window
1775 system cut and paste.
1777 This command is similar to `copy-region-as-kill', except that it gives
1778 visual feedback indicating the extent of the region being copied."
1780 (copy-region-as-kill beg end)
1782 (let ((other-end (if (= (point) beg) end beg))
1784 ;; Inhibit quitting so we can make a quit here
1785 ;; look like a C-g typed as a command.
1787 (if (pos-visible-in-window-p other-end (selected-window))
1789 ;; Swap point and mark.
1790 (set-marker (mark-marker) (point) (current-buffer))
1791 (goto-char other-end)
1794 (set-marker (mark-marker) other-end (current-buffer))
1796 ;; If user quit, deactivate the mark
1797 ;; as C-g would as a command.
1798 (and quit-flag mark-active
1800 (let* ((killed-text (current-kill 0))
1801 (message-len (min (length killed-text) 40)))
1803 ;; Don't say "killed"; that is misleading.
1804 (message "Saved text until \"%s\""
1805 (substring killed-text (- message-len)))
1806 (message "Saved text from \"%s\""
1807 (substring killed-text 0 message-len))))))))
1809 (defun append-next-kill ()
1810 "Cause following command, if it kills, to append to previous kill."
1814 (setq this-command 'kill-region)
1815 (message "If the next command is a kill, it will append"))
1816 (setq last-command 'kill-region)))
1818 (defun yank-pop (arg)
1819 "Replace just-yanked stretch of killed text with a different stretch.
1820 This command is allowed only immediately after a `yank' or a `yank-pop'.
1821 At such a time, the region contains a stretch of reinserted
1822 previously-killed text. `yank-pop' deletes that text and inserts in its
1823 place a different stretch of killed text.
1825 With no argument, the previous kill is inserted.
1826 With argument N, insert the Nth previous kill.
1827 If N is negative, this is a more recent kill.
1829 The sequence of kills wraps around, so that after the oldest one
1830 comes the newest one."
1832 (if (not (eq last-command 'yank))
1833 (error "Previous command was not a yank"))
1834 (setq this-command 'yank)
1835 (let ((inhibit-read-only t)
1836 (before (< (point) (mark t))))
1837 (delete-region (point) (mark t))
1838 (set-marker (mark-marker) (point) (current-buffer))
1839 (let ((opoint (point)))
1840 (insert (current-kill arg))
1841 (let ((inhibit-read-only t))
1842 (remove-text-properties opoint (point) '(read-only nil))))
1844 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1845 ;; It is cleaner to avoid activation, even though the command
1846 ;; loop would deactivate the mark because we inserted text.
1847 (goto-char (prog1 (mark t)
1848 (set-marker (mark-marker) (point) (current-buffer))))))
1851 (defun yank (&optional arg)
1852 "Reinsert the last stretch of killed text.
1853 More precisely, reinsert the stretch of killed text most recently
1854 killed OR yanked. Put point at end, and set mark at beginning.
1855 With just C-u as argument, same but put point at beginning (and mark at end).
1856 With argument N, reinsert the Nth most recently killed stretch of killed
1858 See also the command \\[yank-pop]."
1860 ;; If we don't get all the way thru, make last-command indicate that
1861 ;; for the following command.
1862 (setq this-command t)
1864 (let ((opoint (point)))
1865 (insert (current-kill (cond
1869 (let ((inhibit-read-only t))
1870 (remove-text-properties opoint (point) '(read-only nil))))
1872 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1873 ;; It is cleaner to avoid activation, even though the command
1874 ;; loop would deactivate the mark because we inserted text.
1875 (goto-char (prog1 (mark t)
1876 (set-marker (mark-marker) (point) (current-buffer)))))
1877 ;; If we do get all the way thru, make this-command indicate that.
1878 (setq this-command 'yank)
1881 (defun rotate-yank-pointer (arg)
1882 "Rotate the yanking point in the kill ring.
1883 With argument, rotate that many kills forward (or backward, if negative)."
1888 (defun insert-buffer (buffer)
1889 "Insert after point the contents of BUFFER.
1890 Puts mark after the inserted text.
1891 BUFFER may be a buffer or a buffer name."
1895 (barf-if-buffer-read-only)
1896 (read-buffer "Insert buffer: "
1897 (if (eq (selected-window) (next-window (selected-window)))
1898 (other-buffer (current-buffer))
1899 (window-buffer (next-window (selected-window))))
1901 (or (bufferp buffer)
1902 (setq buffer (get-buffer buffer)))
1903 (let (start end newmark)
1907 (setq start (point-min) end (point-max)))
1908 (insert-buffer-substring buffer start end)
1909 (setq newmark (point)))
1910 (push-mark newmark))
1913 (defun append-to-buffer (buffer start end)
1914 "Append to specified buffer the text of the region.
1915 It is inserted into that buffer before its point.
1917 When calling from a program, give three arguments:
1918 BUFFER (or buffer name), START and END.
1919 START and END specify the portion of the current buffer to be copied."
1921 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
1922 (region-beginning) (region-end)))
1923 (let ((oldbuf (current-buffer)))
1925 (set-buffer (get-buffer-create buffer))
1926 (insert-buffer-substring oldbuf start end))))
1928 (defun prepend-to-buffer (buffer start end)
1929 "Prepend to specified buffer the text of the region.
1930 It is inserted into that buffer after its point.
1932 When calling from a program, give three arguments:
1933 BUFFER (or buffer name), START and END.
1934 START and END specify the portion of the current buffer to be copied."
1935 (interactive "BPrepend to buffer: \nr")
1936 (let ((oldbuf (current-buffer)))
1938 (set-buffer (get-buffer-create buffer))
1940 (insert-buffer-substring oldbuf start end)))))
1942 (defun copy-to-buffer (buffer start end)
1943 "Copy to specified buffer the text of the region.
1944 It is inserted into that buffer, replacing existing text there.
1946 When calling from a program, give three arguments:
1947 BUFFER (or buffer name), START and END.
1948 START and END specify the portion of the current buffer to be copied."
1949 (interactive "BCopy to buffer: \nr")
1950 (let ((oldbuf (current-buffer)))
1952 (set-buffer (get-buffer-create buffer))
1955 (insert-buffer-substring oldbuf start end)))))
1957 (put 'mark-inactive 'error-conditions '(mark-inactive error))
1958 (put 'mark-inactive 'error-message "The mark is not active now")
1960 (defun mark (&optional force)
1961 "Return this buffer's mark value as integer; error if mark inactive.
1962 If optional argument FORCE is non-nil, access the mark value
1963 even if the mark is not currently active, and return nil
1964 if there is no mark at all.
1966 If you are using this in an editing command, you are most likely making
1967 a mistake; see the documentation of `set-mark'."
1968 (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
1969 (marker-position (mark-marker))
1970 (signal 'mark-inactive nil)))
1972 ;; Many places set mark-active directly, and several of them failed to also
1973 ;; run deactivate-mark-hook. This shorthand should simplify.
1974 (defsubst deactivate-mark ()
1975 "Deactivate the mark by setting `mark-active' to nil.
1976 \(That makes a difference only in Transient Mark mode.)
1977 Also runs the hook `deactivate-mark-hook'."
1978 (if transient-mark-mode
1980 (setq mark-active nil)
1981 (run-hooks 'deactivate-mark-hook))))
1983 (defun set-mark (pos)
1984 "Set this buffer's mark to POS. Don't use this function!
1985 That is to say, don't use this function unless you want
1986 the user to see that the mark has moved, and you want the previous
1987 mark position to be lost.
1989 Normally, when a new mark is set, the old one should go on the stack.
1990 This is why most applications should use push-mark, not set-mark.
1992 Novice Emacs Lisp programmers often try to use the mark for the wrong
1993 purposes. The mark saves a location for the user's convenience.
1994 Most editing commands should not alter the mark.
1995 To remember a location for internal use in the Lisp program,
1996 store it in a Lisp variable. Example:
1998 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
2002 (setq mark-active t)
2003 (run-hooks 'activate-mark-hook)
2004 (set-marker (mark-marker) pos (current-buffer)))
2005 ;; Normally we never clear mark-active except in Transient Mark mode.
2006 ;; But when we actually clear out the mark value too,
2007 ;; we must clear mark-active in any mode.
2008 (setq mark-active nil)
2009 (run-hooks 'deactivate-mark-hook)
2010 (set-marker (mark-marker) nil)))
2012 (defvar mark-ring nil
2013 "The list of former marks of the current buffer, most recent first.")
2014 (make-variable-buffer-local 'mark-ring)
2015 (put 'mark-ring 'permanent-local t)
2017 (defcustom mark-ring-max 16
2018 "*Maximum size of mark ring. Start discarding off end if gets this big."
2020 :group 'editing-basics)
2022 (defvar global-mark-ring nil
2023 "The list of saved global marks, most recent first.")
2025 (defcustom global-mark-ring-max 16
2026 "*Maximum size of global mark ring. \
2027 Start discarding off end if gets this big."
2029 :group 'editing-basics)
2031 (defun set-mark-command (arg)
2032 "Set mark at where point is, or jump to mark.
2033 With no prefix argument, set mark, push old mark position on local mark
2034 ring, and push mark on global mark ring.
2035 With argument, jump to mark, and pop a new position for mark off the ring
2036 \(does not affect global mark ring\).
2038 Novice Emacs Lisp programmers often try to use the mark for the wrong
2039 purposes. See the documentation of `set-mark' for more information."
2043 (push-mark nil nil t))
2045 (error "No mark set in this buffer")
2046 (goto-char (mark t))
2049 (defun push-mark (&optional location nomsg activate)
2050 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
2051 If the last global mark pushed was not in the current buffer,
2052 also push LOCATION on the global mark ring.
2053 Display `Mark set' unless the optional second arg NOMSG is non-nil.
2054 In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
2056 Novice Emacs Lisp programmers often try to use the mark for the wrong
2057 purposes. See the documentation of `set-mark' for more information.
2059 In Transient Mark mode, this does not activate the mark."
2062 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
2063 (if (> (length mark-ring) mark-ring-max)
2065 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
2066 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
2067 (set-marker (mark-marker) (or location (point)) (current-buffer))
2068 ;; Now push the mark on the global mark ring.
2069 (if (and global-mark-ring
2070 (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
2071 ;; The last global mark pushed was in this same buffer.
2072 ;; Don't push another one.
2074 (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
2075 (if (> (length global-mark-ring) global-mark-ring-max)
2077 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
2079 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
2080 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
2081 (message "Mark set"))
2082 (if (or activate (not transient-mark-mode))
2083 (set-mark (mark t)))
2087 "Pop off mark ring into the buffer's actual mark.
2088 Does not set point. Does nothing if mark ring is empty."
2091 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
2092 (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
2094 (move-marker (car mark-ring) nil)
2095 (if (null (mark t)) (ding))
2096 (setq mark-ring (cdr mark-ring)))))
2098 (defalias 'exchange-dot-and-mark 'exchange-point-and-mark)
2099 (defun exchange-point-and-mark ()
2100 "Put the mark where point is now, and point where the mark is now.
2101 This command works even when the mark is not active,
2102 and it reactivates the mark."
2104 (let ((omark (mark t)))
2106 (error "No mark set in this buffer"))
2111 (defun transient-mark-mode (arg)
2112 "Toggle Transient Mark mode.
2113 With arg, turn Transient Mark mode on if arg is positive, off otherwise.
2115 In Transient Mark mode, when the mark is active, the region is highlighted.
2116 Changing the buffer \"deactivates\" the mark.
2117 So do certain other operations that set the mark
2118 but whose main purpose is something else--for example,
2119 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
2121 (setq transient-mark-mode
2123 (not transient-mark-mode)
2124 (> (prefix-numeric-value arg) 0)))
2126 (if transient-mark-mode
2127 (message "Transient Mark mode enabled")
2128 (message "Transient Mark mode disabled"))))
2130 (defun pop-global-mark ()
2131 "Pop off global mark ring and jump to the top location."
2133 ;; Pop entries which refer to non-existent buffers.
2134 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
2135 (setq global-mark-ring (cdr global-mark-ring)))
2136 (or global-mark-ring
2137 (error "No global mark set"))
2138 (let* ((marker (car global-mark-ring))
2139 (buffer (marker-buffer marker))
2140 (position (marker-position marker)))
2141 (setq global-mark-ring (nconc (cdr global-mark-ring)
2142 (list (car global-mark-ring))))
2144 (or (and (>= position (point-min))
2145 (<= position (point-max)))
2147 (goto-char position)
2148 (switch-to-buffer buffer)))
2150 (defcustom next-line-add-newlines t
2151 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error."
2153 :group 'editing-basics)
2155 (defun next-line (arg)
2156 "Move cursor vertically down ARG lines.
2157 If there is no character in the target line exactly under the current column,
2158 the cursor is positioned after the character in that line which spans this
2159 column, or at the end of the line if it is not long enough.
2160 If there is no line in the buffer after this one, behavior depends on the
2161 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
2162 to create a line, and moves the cursor to that line. Otherwise it moves the
2163 cursor to the end of the buffer.
2165 The command \\[set-goal-column] can be used to create
2166 a semipermanent goal column for this command.
2167 Then instead of trying to move exactly vertically (or as close as possible),
2168 this command moves to the specified goal column (or as close as possible).
2169 The goal column is stored in the variable `goal-column', which is nil
2170 when there is no goal column.
2172 If you are thinking of using this in a Lisp program, consider
2173 using `forward-line' instead. It is usually easier to use
2174 and more reliable (no dependence on goal column, etc.)."
2176 (if (and next-line-add-newlines (= arg 1))
2177 (let ((opoint (point)))
2186 ((beginning-of-buffer end-of-buffer) (ding)))
2190 (defun previous-line (arg)
2191 "Move cursor vertically up ARG lines.
2192 If there is no character in the target line exactly over the current column,
2193 the cursor is positioned after the character in that line which spans this
2194 column, or at the end of the line if it is not long enough.
2196 The command \\[set-goal-column] can be used to create
2197 a semipermanent goal column for this command.
2198 Then instead of trying to move exactly vertically (or as close as possible),
2199 this command moves to the specified goal column (or as close as possible).
2200 The goal column is stored in the variable `goal-column', which is nil
2201 when there is no goal column.
2203 If you are thinking of using this in a Lisp program, consider using
2204 `forward-line' with a negative argument instead. It is usually easier
2205 to use and more reliable (no dependence on goal column, etc.)."
2210 ((beginning-of-buffer end-of-buffer) (ding)))
2211 (line-move (- arg)))
2214 (defcustom track-eol nil
2215 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
2216 This means moving to the end of each line moved onto.
2217 The beginning of a blank line does not count as the end of a line."
2219 :group 'editing-basics)
2221 (defcustom goal-column nil
2222 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
2223 :type '(choice integer
2224 (const :tag "None" nil))
2225 :group 'editing-basics)
2226 (make-variable-buffer-local 'goal-column)
2228 (defvar temporary-goal-column 0
2229 "Current goal column for vertical motion.
2230 It is the column where point was
2231 at the start of current run of vertical motion commands.
2232 When the `track-eol' feature is doing its job, the value is 9999.")
2234 (defcustom line-move-ignore-invisible nil
2235 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
2236 Outline mode sets this."
2238 :group 'editing-basics)
2240 ;; This is the guts of next-line and previous-line.
2241 ;; Arg says how many lines to move.
2242 (defun line-move (arg)
2243 ;; Don't run any point-motion hooks, and disregard intangibility,
2244 ;; for intermediate positions.
2245 (let ((inhibit-point-motion-hooks t)
2247 new line-end line-beg)
2250 (if (not (or (eq last-command 'next-line)
2251 (eq last-command 'previous-line)))
2252 (setq temporary-goal-column
2253 (if (and track-eol (eolp)
2254 ;; Don't count beg of empty line as end of line
2255 ;; unless we just did explicit end-of-line.
2256 (or (not (bolp)) (eq last-command 'end-of-line)))
2259 (if (and (not (integerp selective-display))
2260 (not line-move-ignore-invisible))
2261 ;; Use just newline characters.
2263 (progn (if (> arg 1) (forward-line (1- arg)))
2264 ;; This way of moving forward ARG lines
2265 ;; verifies that we have a newline after the last one.
2266 ;; It doesn't get confused by intangible text.
2268 (zerop (forward-line 1)))
2269 (and (zerop (forward-line arg))
2271 (signal (if (< arg 0)
2272 'beginning-of-buffer
2275 ;; Move by arg lines, but ignore invisible ones.
2278 (and (zerop (vertical-motion 1))
2279 (signal 'end-of-buffer nil))
2280 ;; If the following character is currently invisible,
2281 ;; skip all characters with that same `invisible' property value.
2282 (while (and (not (eobp))
2284 (get-char-property (point) 'invisible)))
2285 (if (eq buffer-invisibility-spec t)
2287 (or (memq prop buffer-invisibility-spec)
2288 (assq prop buffer-invisibility-spec)))))
2289 (if (get-text-property (point) 'invisible)
2290 (goto-char (next-single-property-change (point) 'invisible))
2291 (goto-char (next-overlay-change (point)))))
2292 (setq arg (1- arg)))
2295 (and (zerop (vertical-motion -1))
2296 (signal 'beginning-of-buffer nil))
2297 (while (and (not (bobp))
2299 (get-char-property (1- (point)) 'invisible)))
2300 (if (eq buffer-invisibility-spec t)
2302 (or (memq prop buffer-invisibility-spec)
2303 (assq prop buffer-invisibility-spec)))))
2304 (if (get-text-property (1- (point)) 'invisible)
2305 (goto-char (previous-single-property-change (point) 'invisible))
2306 (goto-char (previous-overlay-change (point)))))
2307 (setq arg (1+ arg))))
2308 (let ((buffer-invisibility-spec nil))
2309 (move-to-column (or goal-column temporary-goal-column))))
2311 ;; If we are moving into some intangible text,
2312 ;; look for following text on the same line which isn't intangible
2314 (setq line-end (save-excursion (end-of-line) (point)))
2315 (setq line-beg (save-excursion (beginning-of-line) (point)))
2316 (let ((after (and (< new (point-max))
2317 (get-char-property new 'intangible)))
2318 (before (and (> new (point-min))
2319 (get-char-property (1- new) 'intangible))))
2320 (when (and before (eq before after)
2322 (goto-char (point-min))
2323 (let ((inhibit-point-motion-hooks nil))
2325 (if (<= new line-end)
2326 (setq new (point)))))
2327 ;; NEW is where we want to move to.
2328 ;; LINE-BEG and LINE-END are the beginning and end of the line.
2329 ;; Move there in just one step, from our starting position,
2330 ;; with intangibility and point-motion hooks enabled this time.
2332 (setq inhibit-point-motion-hooks nil)
2334 ;; If intangibility processing moved us to a different line,
2335 ;; readjust the horizontal position within the line we ended up at.
2336 (when (or (< (point) line-beg) (> (point) line-end))
2338 (setq inhibit-point-motion-hooks t)
2339 (setq line-end (save-excursion (end-of-line) (point)))
2341 (setq line-beg (point))
2342 (let ((buffer-invisibility-spec nil))
2343 (move-to-column (or goal-column temporary-goal-column)))
2344 (if (<= (point) line-end)
2346 (goto-char (point-min))
2347 (setq inhibit-point-motion-hooks nil)
2352 ;;; Many people have said they rarely use this feature, and often type
2353 ;;; it by accident. Maybe it shouldn't even be on a key.
2354 (put 'set-goal-column 'disabled t)
2356 (defun set-goal-column (arg)
2357 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2358 Those commands will move to this position in the line moved to
2359 rather than trying to keep the same horizontal position.
2360 With a non-nil argument, clears out the goal column
2361 so that \\[next-line] and \\[previous-line] resume vertical motion.
2362 The goal column is stored in the variable `goal-column'."
2366 (setq goal-column nil)
2367 (message "No goal column"))
2368 (setq goal-column (current-column))
2369 (message (substitute-command-keys
2370 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
2374 ;;; Partial support for horizontal autoscrolling. Someday, this feature
2375 ;;; will be built into the C level and all the (hscroll-point-visible) calls
2378 (defcustom hscroll-step 0
2379 "*The number of columns to try scrolling a window by when point moves out.
2380 If that fails to bring point back on frame, point is centered instead.
2381 If this is zero, point is always centered after it moves off frame."
2382 :type '(choice (const :tag "Alway Center" 0)
2383 (integer :format "%v" 1))
2384 :group 'editing-basics)
2386 (defun hscroll-point-visible ()
2387 "Scrolls the selected window horizontally to make point visible."
2389 (set-buffer (window-buffer))
2390 (if (not (or truncate-lines
2391 (> (window-hscroll) 0)
2392 (and truncate-partial-width-windows
2393 (< (window-width) (frame-width)))))
2394 ;; Point is always visible when lines are wrapped.
2396 ;; If point is on the invisible part of the line before window-start,
2397 ;; then hscrolling can't bring it back, so reset window-start first.
2398 (and (< (point) (window-start))
2399 (let ((ws-bol (save-excursion
2400 (goto-char (window-start))
2403 (and (>= (point) ws-bol)
2404 (set-window-start nil ws-bol))))
2405 (let* ((here (hscroll-window-column))
2406 (left (min (window-hscroll) 1))
2407 (right (1- (window-width))))
2408 ;; Allow for the truncation glyph, if we're not exactly at eol.
2409 (if (not (and (= here right)
2410 (= (following-char) ?\n)))
2411 (setq right (1- right)))
2413 ;; If too far away, just recenter. But don't show too much
2414 ;; white space off the end of the line.
2415 ((or (< here (- left hscroll-step))
2416 (> here (+ right hscroll-step)))
2417 (let ((eol (save-excursion (end-of-line) (hscroll-window-column))))
2418 (scroll-left (min (- here (/ (window-width) 2))
2419 (- eol (window-width) -5)))))
2420 ;; Within range. Scroll by one step (or maybe not at all).
2422 (scroll-right hscroll-step))
2424 (scroll-left hscroll-step)))))))
2426 ;; This function returns the window's idea of the display column of point,
2427 ;; assuming that the window is already known to be truncated rather than
2428 ;; wrapped, and that we've already handled the case where point is on the
2429 ;; part of the line before window-start. We ignore window-width; if point
2430 ;; is beyond the right margin, we want to know how far. The return value
2431 ;; includes the effects of window-hscroll, window-start, and the prompt
2432 ;; string in the minibuffer. It may be negative due to hscroll.
2433 (defun hscroll-window-column ()
2434 (let* ((hscroll (window-hscroll))
2435 (startpos (save-excursion
2437 (if (= (point) (save-excursion
2438 (goto-char (window-start))
2441 (goto-char (window-start)))
2443 (hpos (+ (if (and (eq (selected-window) (minibuffer-window))
2444 (= 1 (window-start))
2445 (= startpos (point-min)))
2446 (minibuffer-prompt-width)
2448 (min 0 (- 1 hscroll))))
2450 (car (cdr (compute-motion startpos (cons hpos 0)
2452 1000000 (cons hscroll 0) nil)))))
2455 ;; rms: (1) The definitions of arrow keys should not simply restate
2456 ;; what keys they are. The arrow keys should run the ordinary commands.
2457 ;; (2) The arrow keys are just one of many common ways of moving point
2458 ;; within a line. Real horizontal autoscrolling would be a good feature,
2459 ;; but supporting it only for arrow keys is too incomplete to be desirable.
2461 ;;;;; Make arrow keys do the right thing for improved terminal support
2462 ;;;;; When we implement true horizontal autoscrolling, right-arrow and
2463 ;;;;; left-arrow can lose the (if truncate-lines ...) clause and become
2464 ;;;;; aliases. These functions are bound to the corresponding keyboard
2465 ;;;;; events in loaddefs.el.
2467 ;;(defun right-arrow (arg)
2468 ;; "Move right one character on the screen (with prefix ARG, that many chars).
2469 ;;Scroll right if needed to keep point horizontally onscreen."
2470 ;; (interactive "P")
2471 ;; (forward-char arg)
2472 ;; (hscroll-point-visible))
2474 ;;(defun left-arrow (arg)
2475 ;; "Move left one character on the screen (with prefix ARG, that many chars).
2476 ;;Scroll left if needed to keep point horizontally onscreen."
2477 ;; (interactive "P")
2478 ;; (backward-char arg)
2479 ;; (hscroll-point-visible))
2481 (defun scroll-other-window-down (lines)
2482 "Scroll the \"other window\" down.
2483 For more details, see the documentation for `scroll-other-window'."
2485 (scroll-other-window
2486 ;; Just invert the argument's meaning.
2487 ;; We can do that without knowing which window it will be.
2488 (if (eq lines '-) nil
2490 (- (prefix-numeric-value lines))))))
2491 (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2493 (defun beginning-of-buffer-other-window (arg)
2494 "Move point to the beginning of the buffer in the other window.
2495 Leave mark at previous position.
2496 With arg N, put point N/10 of the way from the true beginning."
2498 (let ((orig-window (selected-window))
2499 (window (other-window-for-scrolling)))
2500 ;; We use unwind-protect rather than save-window-excursion
2501 ;; because the latter would preserve the things we want to change.
2504 (select-window window)
2505 ;; Set point and mark in that window's buffer.
2506 (beginning-of-buffer arg)
2507 ;; Set point accordingly.
2509 (select-window orig-window))))
2511 (defun end-of-buffer-other-window (arg)
2512 "Move point to the end of the buffer in the other window.
2513 Leave mark at previous position.
2514 With arg N, put point N/10 of the way from the true end."
2516 ;; See beginning-of-buffer-other-window for comments.
2517 (let ((orig-window (selected-window))
2518 (window (other-window-for-scrolling)))
2521 (select-window window)
2524 (select-window orig-window))))
2526 (defun transpose-chars (arg)
2527 "Interchange characters around point, moving forward one character.
2528 With prefix arg ARG, effect is to take character before point
2529 and drag it forward past ARG other characters (backward if ARG negative).
2530 If no argument and at end of line, the previous two chars are exchanged."
2532 (and (null arg) (eolp) (forward-char -1))
2533 (transpose-subr 'forward-char (prefix-numeric-value arg)))
2535 (defun transpose-words (arg)
2536 "Interchange words around point, leaving point at end of them.
2537 With prefix arg ARG, effect is to take word before or around point
2538 and drag it forward past ARG other words (backward if ARG negative).
2539 If ARG is zero, the words around or after point and around or after mark
2542 (transpose-subr 'forward-word arg))
2544 (defun transpose-sexps (arg)
2545 "Like \\[transpose-words] but applies to sexps.
2546 Does not work on a sexp that point is in the middle of
2547 if it is a list or string."
2549 (transpose-subr 'forward-sexp arg))
2551 (defun transpose-lines (arg)
2552 "Exchange current line and previous line, leaving point after both.
2553 With argument ARG, takes previous line and moves it past ARG lines.
2554 With argument 0, interchanges line point is in with line mark is in."
2556 (transpose-subr (function
2560 ;; Move forward over ARG lines,
2561 ;; but create newlines if necessary.
2562 (setq arg (forward-line arg))
2563 (if (/= (preceding-char) ?\n)
2564 (setq arg (1+ arg)))
2567 (forward-line arg))))
2570 (defvar transpose-subr-start1)
2571 (defvar transpose-subr-start2)
2572 (defvar transpose-subr-end1)
2573 (defvar transpose-subr-end2)
2575 (defun transpose-subr (mover arg)
2576 (let (transpose-subr-start1
2578 transpose-subr-start2
2579 transpose-subr-end2)
2584 (setq transpose-subr-end2 (point))
2586 (setq transpose-subr-start2 (point))
2589 (setq transpose-subr-end1 (point))
2591 (setq transpose-subr-start1 (point))
2593 (exchange-point-and-mark))
2597 (setq transpose-subr-start1 (point))
2599 (setq transpose-subr-end1 (point))
2601 (setq transpose-subr-end2 (point))
2602 (funcall mover (- arg))
2603 (setq transpose-subr-start2 (point))
2605 (goto-char transpose-subr-end2))
2607 (setq transpose-subr-start2 (point))
2609 (setq transpose-subr-end2 (point))
2610 (funcall mover (1- arg))
2611 (setq transpose-subr-start1 (point))
2612 (funcall mover (- arg))
2613 (setq transpose-subr-end1 (point))
2614 (transpose-subr-1)))))
2616 (defun transpose-subr-1 ()
2617 (if (> (min transpose-subr-end1 transpose-subr-end2)
2618 (max transpose-subr-start1 transpose-subr-start2))
2619 (error "Don't have two things to transpose"))
2620 (let* ((word1 (buffer-substring transpose-subr-start1 transpose-subr-end1))
2621 (len1 (length word1))
2622 (word2 (buffer-substring transpose-subr-start2 transpose-subr-end2))
2623 (len2 (length word2)))
2624 (delete-region transpose-subr-start2 transpose-subr-end2)
2625 (goto-char transpose-subr-start2)
2627 (goto-char (if (< transpose-subr-start1 transpose-subr-start2)
2628 transpose-subr-start1
2629 (+ transpose-subr-start1 (- len1 len2))))
2630 (delete-region (point) (+ (point) len1))
2633 (defcustom comment-column 32
2634 "*Column to indent right-margin comments to.
2635 Setting this variable automatically makes it local to the current buffer.
2636 Each mode establishes a different default value for this variable; you
2637 can set the value for a particular mode using that mode's hook."
2639 :group 'fill-comments)
2640 (make-variable-buffer-local 'comment-column)
2642 (defcustom comment-start nil
2643 "*String to insert to start a new comment, or nil if no comment syntax."
2644 :type '(choice (const :tag "None" nil)
2646 :group 'fill-comments)
2648 (defcustom comment-start-skip nil
2649 "*Regexp to match the start of a comment plus everything up to its body.
2650 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
2651 at the place matched by the close of the first pair."
2652 :type '(choice (const :tag "None" nil)
2654 :group 'fill-comments)
2656 (defcustom comment-end ""
2657 "*String to insert to end a new comment.
2658 Should be an empty string if comments are terminated by end-of-line."
2660 :group 'fill-comments)
2662 (defvar comment-indent-hook nil
2663 "Obsolete variable for function to compute desired indentation for a comment.
2664 This function is called with no args with point at the beginning of
2665 the comment's starting delimiter.")
2667 (defvar comment-indent-function
2668 '(lambda () comment-column)
2669 "Function to compute desired indentation for a comment.
2670 This function is called with no args with point at the beginning of
2671 the comment's starting delimiter.")
2673 (defcustom block-comment-start nil
2674 "*String to insert to start a new comment on a line by itself.
2675 If nil, use `comment-start' instead.
2676 Note that the regular expression `comment-start-skip' should skip this string
2677 as well as the `comment-start' string."
2678 :type '(choice (const :tag "Use comment-start" nil)
2680 :group 'fill-comments)
2682 (defcustom block-comment-end nil
2683 "*String to insert to end a new comment on a line by itself.
2684 Should be an empty string if comments are terminated by end-of-line.
2685 If nil, use `comment-end' instead."
2686 :type '(choice (const :tag "Use comment-end" nil)
2688 :group 'fill-comments)
2690 (defun indent-for-comment ()
2691 "Indent this line's comment to comment column, or insert an empty comment."
2693 (let* ((empty (save-excursion (beginning-of-line)
2694 (looking-at "[ \t]*$")))
2695 (starter (or (and empty block-comment-start) comment-start))
2696 (ender (or (and empty block-comment-end) comment-end)))
2699 (error "No comment syntax defined"))
2700 ((null comment-start-skip)
2701 (error "This mode doesn't define `comment-start-skip'"))
2702 (t (let* ((eolpos (save-excursion (end-of-line) (point)))
2705 (if (re-search-forward comment-start-skip eolpos 'move)
2706 (progn (setq cpos (point-marker))
2707 ;; Find the start of the comment delimiter.
2708 ;; If there were paren-pairs in comment-start-skip,
2709 ;; position at the end of the first pair.
2711 (goto-char (match-end 1))
2712 ;; If comment-start-skip matched a string with
2713 ;; internal whitespace (not final whitespace) then
2714 ;; the delimiter start at the end of that
2715 ;; whitespace. Otherwise, it starts at the
2716 ;; beginning of what was matched.
2717 (skip-syntax-backward " " (match-beginning 0))
2718 (skip-syntax-backward "^ " (match-beginning 0)))))
2719 (setq begpos (point))
2720 ;; Compute desired indent.
2721 (if (= (current-column)
2722 (setq indent (if comment-indent-hook
2723 (funcall comment-indent-hook)
2724 (funcall comment-indent-function))))
2726 ;; If that's different from current, change it.
2727 (skip-chars-backward " \t")
2728 (delete-region (point) begpos)
2730 ;; An existing comment?
2732 (progn (goto-char cpos)
2733 (set-marker cpos nil))
2737 (insert ender))))))))
2739 (defun set-comment-column (arg)
2740 "Set the comment column based on point.
2741 With no arg, set the comment column to the current column.
2742 With just minus as arg, kill any comment on this line.
2743 With any other arg, set comment column to indentation of the previous comment
2744 and then align or create a comment on this line at that column."
2752 (re-search-backward comment-start-skip)
2754 (re-search-forward comment-start-skip)
2755 (goto-char (match-beginning 0))
2756 (setq comment-column (current-column))
2757 (message "Comment column set to %d" comment-column))
2758 (indent-for-comment))
2759 (setq comment-column (current-column))
2760 (message "Comment column set to %d" comment-column))))
2762 (defun kill-comment (arg)
2763 "Kill the comment on this line, if any.
2764 With argument, kill comments on that many lines starting with this one."
2765 ;; this function loses in a lot of situations. it incorrectly recognises
2766 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
2767 ;; with multi-line comments, can kill extra whitespace if comment wasn't
2768 ;; through end-of-line, et cetera.
2770 (or comment-start-skip (error "No comment syntax defined"))
2771 (let ((count (prefix-numeric-value arg)) endc)
2777 (and (string< "" comment-end)
2780 (re-search-forward (regexp-quote comment-end) endc 'move)
2781 (skip-chars-forward " \t")
2784 (if (re-search-forward comment-start-skip endc t)
2786 (goto-char (match-beginning 0))
2787 (skip-chars-backward " \t")
2788 (kill-region (point) endc)
2789 ;; to catch comments a line beginnings
2790 (indent-according-to-mode))))
2791 (if arg (forward-line 1))
2792 (setq count (1- count)))))
2794 (defvar comment-padding 1
2795 "Number of spaces `comment-region' puts between comment chars and text.
2797 Extra spacing between the comment characters and the comment text
2798 makes the comment easier to read. Default is 1. Nil means 0 and is
2801 (defun comment-region (beg end &optional arg)
2802 "Comment or uncomment each line in the region.
2803 With just C-u prefix arg, uncomment each line in region.
2804 Numeric prefix arg ARG means use ARG comment characters.
2805 If ARG is negative, delete that many comment characters instead.
2806 Comments are terminated on each line, even for syntax in which newline does
2807 not end the comment. Blank lines do not get comments."
2808 ;; if someone wants it to only put a comment-start at the beginning and
2809 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
2810 ;; is easy enough. No option is made here for other than commenting
2812 (interactive "r\nP")
2813 (or comment-start (error "No comment syntax is defined"))
2814 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2817 (let ((cs comment-start) (ce comment-end)
2818 (cp (when comment-padding
2819 (make-string comment-padding ? )))
2821 (if (consp arg) (setq numarg t)
2822 (setq numarg (prefix-numeric-value arg))
2823 ;; For positive arg > 1, replicate the comment delims now,
2824 ;; then insert the replicated strings just once.
2826 (setq cs (concat cs comment-start)
2827 ce (concat ce comment-end))
2828 (setq numarg (1- numarg))))
2829 ;; Loop over all lines from BEG to END.
2830 (narrow-to-region beg end)
2832 (if (or (eq numarg t) (< numarg 0))
2834 (let (found-comment)
2835 ;; Delete comment start from beginning of line.
2837 (while (looking-at (regexp-quote cs))
2838 (setq found-comment t)
2839 (delete-char (length cs)))
2840 (let ((count numarg))
2841 (while (and (> 1 (setq count (1+ count)))
2842 (looking-at (regexp-quote cs)))
2843 (setq found-comment t)
2844 (delete-char (length cs)))))
2845 ;; Delete comment padding from beginning of line
2846 (when (and found-comment comment-padding
2847 (looking-at (regexp-quote cp)))
2848 (delete-char comment-padding))
2849 ;; Delete comment end from end of line.
2855 ;; This is questionable if comment-end ends in
2856 ;; whitespace. That is pretty brain-damaged,
2858 (while (progn (skip-chars-backward " \t")
2859 (and (>= (- (point) (point-min)) (length ce))
2861 (backward-char (length ce))
2862 (looking-at (regexp-quote ce)))))
2863 (delete-char (- (length ce)))))
2864 (let ((count numarg))
2865 (while (> 1 (setq count (1+ count)))
2867 ;; this is questionable if comment-end ends in whitespace
2868 ;; that is pretty brain-damaged though
2869 (skip-chars-backward " \t")
2870 (if (>= (- (point) (point-min)) (length ce))
2872 (backward-char (length ce))
2873 (if (looking-at (regexp-quote ce))
2874 (delete-char (length ce)))))))))
2877 (when comment-padding
2878 (setq cs (concat cs cp)))
2880 ;; Insert at beginning and at end.
2881 (if (looking-at "[ \t]*$") ()
2883 (if (string= "" ce) ()
2886 (search-forward "\n" nil 'move)))))))
2888 (defun backward-word (arg)
2889 "Move backward until encountering the end of a word.
2890 With argument, do this that many times.
2891 In programs, it is faster to call `forward-word' with negative arg."
2893 (forward-word (- arg)))
2895 (defun mark-word (arg)
2896 "Set mark arg words away from point."
2904 (defun kill-word (arg)
2905 "Kill characters forward until encountering the end of a word.
2906 With argument, do this that many times."
2908 (kill-region (point) (progn (forward-word arg) (point))))
2910 (defun backward-kill-word (arg)
2911 "Kill characters backward until encountering the end of a word.
2912 With argument, do this that many times."
2914 (kill-word (- arg)))
2916 (defun current-word (&optional strict)
2917 "Return the word point is on (or a nearby word) as a string.
2918 If optional arg STRICT is non-nil, return nil unless point is within
2919 or adjacent to a word."
2921 (let ((oldpoint (point)) (start (point)) (end (point)))
2922 (skip-syntax-backward "w_") (setq start (point))
2923 (goto-char oldpoint)
2924 (skip-syntax-forward "w_") (setq end (point))
2925 (if (and (eq start oldpoint) (eq end oldpoint))
2926 ;; Point is neither within nor adjacent to a word.
2929 ;; Look for preceding word in same line.
2930 (skip-syntax-backward "^w_"
2931 (save-excursion (beginning-of-line)
2934 ;; No preceding word in same line.
2935 ;; Look for following word in same line.
2937 (skip-syntax-forward "^w_"
2938 (save-excursion (end-of-line)
2940 (setq start (point))
2941 (skip-syntax-forward "w_")
2944 (skip-syntax-backward "w_")
2945 (setq start (point)))
2946 (buffer-substring-no-properties start end)))
2947 (buffer-substring-no-properties start end)))))
2949 (defcustom fill-prefix nil
2950 "*String for filling to insert at front of new line, or nil for none.
2951 Setting this variable automatically makes it local to the current buffer."
2952 :type '(choice (const :tag "None" nil)
2955 (make-variable-buffer-local 'fill-prefix)
2957 (defcustom auto-fill-inhibit-regexp nil
2958 "*Regexp to match lines which should not be auto-filled."
2959 :type '(choice (const :tag "None" nil)
2963 (defvar comment-line-break-function 'indent-new-comment-line
2964 "*Mode-specific function which line breaks and continues a comment.
2966 This function is only called during auto-filling of a comment section.
2967 The function should take a single optional argument, which is a flag
2968 indicating whether it should use soft newlines.
2970 Setting this variable automatically makes it local to the current buffer.")
2972 ;; This function is used as the auto-fill-function of a buffer
2973 ;; when Auto-Fill mode is enabled.
2974 ;; It returns t if it really did any work.
2975 ;; (Actually some major modes use a different auto-fill function,
2976 ;; but this one is the default one.)
2977 (defun do-auto-fill ()
2978 (let (fc justify bol give-up
2979 (fill-prefix fill-prefix))
2980 (if (or (not (setq justify (current-justification)))
2981 (null (setq fc (current-fill-column)))
2982 (and (eq justify 'left)
2983 (<= (current-column) fc))
2984 (save-excursion (beginning-of-line)
2986 (and auto-fill-inhibit-regexp
2987 (looking-at auto-fill-inhibit-regexp))))
2988 nil ;; Auto-filling not required
2989 (if (memq justify '(full center right))
2990 (save-excursion (unjustify-current-line)))
2992 ;; Choose a fill-prefix automatically.
2993 (if (and adaptive-fill-mode
2994 (or (null fill-prefix) (string= fill-prefix "")))
2996 (fill-context-prefix
2997 (save-excursion (backward-paragraph 1) (point))
2998 (save-excursion (forward-paragraph 1) (point)))))
2999 (and prefix (not (equal prefix ""))
3000 (setq fill-prefix prefix))))
3002 (while (and (not give-up) (> (current-column) fc))
3003 ;; Determine where to split the line.
3006 (let ((opoint (point))
3011 (setq after-prefix (point))
3013 (looking-at (regexp-quote fill-prefix))
3014 (setq after-prefix (match-end 0)))
3015 (move-to-column (1+ fc))
3016 ;; Move back to the point where we can break the line.
3017 ;; We break the line between word or
3018 ;; after/before the character which has character
3019 ;; category `|'. We search space, \c| followed by
3020 ;; a character, or \c| following a character. If
3021 ;; not found, place the point at beginning of line.
3023 ;; If this is after period and a single space,
3024 ;; move back once more--we don't want to break
3025 ;; the line there and make it look like a
3029 sentence-end-double-space
3030 (save-excursion (forward-char -1)
3031 (and (looking-at "\\. ")
3032 (not (looking-at "\\. ")))))
3035 fill-nobreak-predicate
3036 (funcall fill-nobreak-predicate)))
3038 (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
3039 ;; If we find nowhere on the line to break it,
3040 ;; break after one word. Set bounce to t
3041 ;; so we will not keep going in this while loop.
3042 (if (<= (point) after-prefix)
3044 (goto-char after-prefix)
3045 (re-search-forward "[ \t]" opoint t)
3047 (if (looking-at "[ \t]")
3048 ;; Break the line at word boundary.
3049 (skip-chars-backward " \t")
3050 ;; Break the line after/before \c|.
3052 (if enable-multibyte-characters
3053 ;; If we are going to break the line after or
3054 ;; before a non-ascii character, we may have
3055 ;; to run a special function for the charset
3056 ;; of the character to find the correct break
3058 (if (not (and (eq (charset-after (1- (point))) 'ascii)
3059 (eq (charset-after (point)) 'ascii)))
3060 (fill-find-break-point after-prefix)))
3062 ;; Let fill-point be set to the place where we end up.
3063 ;; But move back before any whitespace here.
3064 (skip-chars-backward " \t")
3067 ;; See whether the place we found is any good.
3069 (goto-char fill-point)
3071 ;; There is no use breaking at end of line.
3072 (not (save-excursion (skip-chars-forward " ") (eolp)))
3073 ;; It is futile to split at the end of the prefix
3074 ;; since we would just insert the prefix again.
3075 (not (and after-prefix (<= (point) after-prefix)))
3076 ;; Don't split right after a comment starter
3077 ;; since we would just make another comment starter.
3078 (not (and comment-start-skip
3079 (let ((limit (point)))
3081 (and (re-search-forward comment-start-skip
3083 (eq (point) limit)))))))
3084 ;; Ok, we have a useful place to break the line. Do it.
3085 (let ((prev-column (current-column)))
3086 ;; If point is at the fill-point, do not `save-excursion'.
3087 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
3088 ;; point will end up before it rather than after it.
3090 (skip-chars-backward " \t")
3091 (= (point) fill-point))
3092 (funcall comment-line-break-function t)
3094 (goto-char fill-point)
3095 (funcall comment-line-break-function t)))
3096 ;; Now do justification, if required
3097 (if (not (eq justify 'left))
3100 (justify-current-line justify nil t)))
3101 ;; If making the new line didn't reduce the hpos of
3102 ;; the end of the line, then give up now;
3103 ;; trying again will not help.
3104 (if (>= (current-column) prev-column)
3106 ;; No good place to break => stop trying.
3108 ;; Justify last line.
3109 (justify-current-line justify t t)
3112 (defvar normal-auto-fill-function 'do-auto-fill
3113 "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
3114 Some major modes set this.")
3116 (defun auto-fill-mode (&optional arg)
3117 "Toggle Auto Fill mode.
3118 With arg, turn Auto Fill mode on if and only if arg is positive.
3119 In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
3120 automatically breaks the line at a previous space.
3122 The value of `normal-auto-fill-function' specifies the function to use
3123 for `auto-fill-function' when turning Auto Fill mode on."
3125 (prog1 (setq auto-fill-function
3127 (not auto-fill-function)
3128 (> (prefix-numeric-value arg) 0))
3129 normal-auto-fill-function
3131 (force-mode-line-update)))
3133 ;; This holds a document string used to document auto-fill-mode.
3134 (defun auto-fill-function ()
3135 "Automatically break line at a previous space, in insertion of text."
3138 (defun turn-on-auto-fill ()
3139 "Unconditionally turn on Auto Fill mode."
3141 (custom-add-option 'text-mode-hook 'turn-on-auto-fill)
3143 (defun set-fill-column (arg)
3144 "Set `fill-column' to specified argument.
3145 Use \\[universal-argument] followed by a number to specify a column.
3146 Just \\[universal-argument] as argument means to use the current column."
3149 (setq arg (current-column)))
3150 (if (not (integerp arg))
3151 ;; Disallow missing argument; it's probably a typo for C-x C-f.
3152 (error "set-fill-column requires an explicit argument")
3153 (message "Fill column set to %d (was %d)" arg fill-column)
3154 (setq fill-column arg)))
3156 (defcustom comment-multi-line nil
3157 "*Non-nil means \\[indent-new-comment-line] should continue same comment
3158 on new line, with no new terminator or starter.
3159 This is obsolete because you might as well use \\[newline-and-indent]."
3161 :group 'fill-comments)
3163 (defun indent-new-comment-line (&optional soft)
3164 "Break line at point and indent, continuing comment if within one.
3165 This indents the body of the continued comment
3166 under the previous comment line.
3168 This command is intended for styles where you write a comment per line,
3169 starting a new comment (and terminating it if necessary) on each line.
3170 If you want to continue one comment across several lines, use \\[newline-and-indent].
3172 If a fill column is specified, it overrides the use of the comment column
3173 or comment indentation.
3175 The inserted newline is marked hard if `use-hard-newlines' is true,
3176 unless optional argument SOFT is non-nil."
3178 (let (comcol comstart)
3179 (skip-chars-backward " \t")
3180 (delete-region (point)
3181 (progn (skip-chars-forward " \t")
3183 (if soft (insert-and-inherit ?\n) (newline 1))
3186 (indent-to-left-margin)
3187 (insert-and-inherit fill-prefix))
3188 (if (not comment-multi-line)
3190 (if (and comment-start-skip
3191 (let ((opoint (point)))
3193 (re-search-forward comment-start-skip opoint t)))
3194 ;; The old line is a comment.
3195 ;; Set WIN to the pos of the comment-start.
3196 ;; But if the comment is empty, look at preceding lines
3197 ;; to find one that has a nonempty comment.
3199 ;; If comment-start-skip contains a \(...\) pair,
3200 ;; the real comment delimiter starts at the end of that pair.
3201 (let ((win (or (match-end 1) (match-beginning 0))))
3202 (while (and (eolp) (not (bobp))
3205 (setq opoint (point))
3207 (re-search-forward comment-start-skip opoint t)))
3208 (setq win (or (match-end 1) (match-beginning 0))))
3209 ;; Indent this line like what we found.
3211 (setq comcol (current-column))
3213 (buffer-substring (point) (match-end 0)))))))
3215 (let ((comment-column comcol)
3216 (comment-start comstart)
3217 (comment-end comment-end))
3218 (and comment-end (not (equal comment-end ""))
3219 ; (if (not comment-multi-line)
3222 (insert comment-end)
3224 ; (setq comment-column (+ comment-column (length comment-start))
3229 (setq comment-end ""))
3230 (insert-and-inherit ?\n)
3232 (indent-for-comment)
3234 ;; Make sure we delete the newline inserted above.
3237 (indent-according-to-mode)))))
3239 (defun set-selective-display (arg)
3240 "Set `selective-display' to ARG; clear it if no arg.
3241 When the value of `selective-display' is a number > 0,
3242 lines whose indentation is >= that value are not displayed.
3243 The variable `selective-display' has a separate value for each buffer."
3245 (if (eq selective-display t)
3246 (error "selective-display already in use for marked lines"))
3249 (narrow-to-region (point-min) (point))
3250 (goto-char (window-start))
3251 (vertical-motion (window-height)))))
3252 (setq selective-display
3253 (and arg (prefix-numeric-value arg)))
3254 (recenter current-vpos))
3255 (set-window-start (selected-window) (window-start (selected-window)))
3256 (princ "selective-display set to " t)
3257 (prin1 selective-display t)
3260 (defvar overwrite-mode-textual " Ovwrt"
3261 "The string displayed in the mode line when in overwrite mode.")
3262 (defvar overwrite-mode-binary " Bin Ovwrt"
3263 "The string displayed in the mode line when in binary overwrite mode.")
3265 (defun overwrite-mode (arg)
3266 "Toggle overwrite mode.
3267 With arg, turn overwrite mode on iff arg is positive.
3268 In overwrite mode, printing characters typed in replace existing text
3269 on a one-for-one basis, rather than pushing it to the right. At the
3270 end of a line, such characters extend the line. Before a tab,
3271 such characters insert until the tab is filled in.
3272 \\[quoted-insert] still inserts characters in overwrite mode; this
3273 is supposed to make it easier to insert characters when necessary."
3275 (setq overwrite-mode
3276 (if (if (null arg) (not overwrite-mode)
3277 (> (prefix-numeric-value arg) 0))
3278 'overwrite-mode-textual))
3279 (force-mode-line-update))
3281 (defun binary-overwrite-mode (arg)
3282 "Toggle binary overwrite mode.
3283 With arg, turn binary overwrite mode on iff arg is positive.
3284 In binary overwrite mode, printing characters typed in replace
3285 existing text. Newlines are not treated specially, so typing at the
3286 end of a line joins the line to the next, with the typed character
3287 between them. Typing before a tab character simply replaces the tab
3288 with the character typed.
3289 \\[quoted-insert] replaces the text at the cursor, just as ordinary
3290 typing characters do.
3292 Note that binary overwrite mode is not its own minor mode; it is a
3293 specialization of overwrite-mode, entered by setting the
3294 `overwrite-mode' variable to `overwrite-mode-binary'."
3296 (setq overwrite-mode
3298 (not (eq overwrite-mode 'overwrite-mode-binary))
3299 (> (prefix-numeric-value arg) 0))
3300 'overwrite-mode-binary))
3301 (force-mode-line-update))
3303 (defcustom line-number-mode t
3304 "*Non-nil means display line number in mode line."
3306 :group 'editing-basics)
3308 (defun line-number-mode (arg)
3309 "Toggle Line Number mode.
3310 With arg, turn Line Number mode on iff arg is positive.
3311 When Line Number mode is enabled, the line number appears
3314 Line numbers do not appear for very large buffers, see variable
3315 `line-number-display-limit'."
3317 (setq line-number-mode
3318 (if (null arg) (not line-number-mode)
3319 (> (prefix-numeric-value arg) 0)))
3320 (force-mode-line-update))
3322 (defcustom column-number-mode nil
3323 "*Non-nil means display column number in mode line."
3325 :group 'editing-basics)
3327 (defun column-number-mode (arg)
3328 "Toggle Column Number mode.
3329 With arg, turn Column Number mode on iff arg is positive.
3330 When Column Number mode is enabled, the column number appears
3333 (setq column-number-mode
3334 (if (null arg) (not column-number-mode)
3335 (> (prefix-numeric-value arg) 0)))
3336 (force-mode-line-update))
3338 (defgroup paren-blinking nil
3339 "Blinking matching of parens and expressions."
3340 :prefix "blink-matching-"
3341 :group 'paren-matching)
3343 (defcustom blink-matching-paren t
3344 "*Non-nil means show matching open-paren when close-paren is inserted."
3346 :group 'paren-blinking)
3348 (defcustom blink-matching-paren-on-screen t
3349 "*Non-nil means show matching open-paren when it is on screen.
3350 If nil, means don't show it (but the open-paren can still be shown
3351 when it is off screen)."
3353 :group 'paren-blinking)
3355 (defcustom blink-matching-paren-distance (* 25 1024)
3356 "*If non-nil, is maximum distance to search for matching open-paren."
3358 :group 'paren-blinking)
3360 (defcustom blink-matching-delay 1
3361 "*Time in seconds to delay after showing a matching paren."
3363 :group 'paren-blinking)
3365 (defcustom blink-matching-paren-dont-ignore-comments nil
3366 "*Non-nil means `blink-matching-paren' will not ignore comments."
3368 :group 'paren-blinking)
3370 (defun blink-matching-open ()
3371 "Move cursor momentarily to the beginning of the sexp before point."
3373 (and (> (point) (1+ (point-min)))
3374 blink-matching-paren
3375 ;; Verify an even number of quoting characters precede the close.
3376 (= 1 (logand 1 (- (point)
3379 (skip-syntax-backward "/\\")
3381 (let* ((oldpos (point))
3386 (if blink-matching-paren-distance
3387 (narrow-to-region (max (point-min)
3388 (- (point) blink-matching-paren-distance))
3391 (let ((parse-sexp-ignore-comments
3392 (and parse-sexp-ignore-comments
3393 (not blink-matching-paren-dont-ignore-comments))))
3394 (setq blinkpos (scan-sexps oldpos -1)))
3397 (/= (char-syntax (char-after blinkpos))
3400 (or (null (matching-paren (char-after blinkpos)))
3401 (/= (char-after (1- oldpos))
3402 (matching-paren (char-after blinkpos))))))
3403 (if mismatch (setq blinkpos nil))
3405 ;; Don't log messages about paren matching.
3406 (let (message-log-max)
3407 (goto-char blinkpos)
3408 (if (pos-visible-in-window-p)
3409 (and blink-matching-paren-on-screen
3410 (sit-for blink-matching-delay))
3411 (goto-char blinkpos)
3414 ;; Show what precedes the open in its line, if anything.
3416 (skip-chars-backward " \t")
3418 (buffer-substring (progn (beginning-of-line) (point))
3420 ;; Show what follows the open in its line, if anything.
3423 (skip-chars-forward " \t")
3425 (buffer-substring blinkpos
3426 (progn (end-of-line) (point)))
3427 ;; Otherwise show the previous nonblank line,
3430 (skip-chars-backward "\n \t")
3433 (buffer-substring (progn
3434 (skip-chars-backward "\n \t")
3437 (progn (end-of-line)
3438 (skip-chars-backward " \t")
3440 ;; Replace the newline and other whitespace with `...'.
3442 (buffer-substring blinkpos (1+ blinkpos)))
3443 ;; There is nothing to show except the char itself.
3444 (buffer-substring blinkpos (1+ blinkpos))))))))
3446 (message "Mismatched parentheses"))
3447 ((not blink-matching-paren-distance)
3448 (message "Unmatched parenthesis"))))))))
3450 ;Turned off because it makes dbx bomb out.
3451 (setq blink-paren-function 'blink-matching-open)
3453 ;; This executes C-g typed while Emacs is waiting for a command.
3454 ;; Quitting out of a program does not go through here;
3455 ;; that happens in the QUIT macro at the C code level.
3456 (defun keyboard-quit ()
3457 "Signal a `quit' condition.
3458 During execution of Lisp code, this character causes a quit directly.
3459 At top-level, as an editor command, this simply beeps."
3464 (define-key global-map "\C-g" 'keyboard-quit)
3466 (defvar buffer-quit-function nil
3467 "Function to call to \"quit\" the current buffer, or nil if none.
3468 \\[keyboard-escape-quit] calls this function when its more local actions
3469 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
3471 (defun keyboard-escape-quit ()
3472 "Exit the current \"mode\" (in a generalized sense of the word).
3473 This command can exit an interactive command such as `query-replace',
3474 can clear out a prefix argument or a region,
3475 can get out of the minibuffer or other recursive edit,
3476 cancel the use of the current buffer (for special-purpose buffers),
3477 or go back to just one window (by deleting all but the selected window)."
3479 (cond ((eq last-command 'mode-exited) nil)
3480 ((> (minibuffer-depth) 0)
3481 (abort-recursive-edit))
3484 ((and transient-mark-mode
3487 ((> (recursion-depth) 0)
3488 (exit-recursive-edit))
3489 (buffer-quit-function
3490 (funcall buffer-quit-function))
3491 ((not (one-window-p t))
3492 (delete-other-windows))
3493 ((string-match "^ \\*" (buffer-name (current-buffer)))
3496 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
3498 (defcustom mail-user-agent 'sendmail-user-agent
3499 "*Your preference for a mail composition package.
3500 Various Emacs Lisp packages (e.g. reporter) require you to compose an
3501 outgoing email message. This variable lets you specify which
3502 mail-sending package you prefer.
3504 Valid values include:
3506 sendmail-user-agent -- use the default Emacs Mail package
3507 mh-e-user-agent -- use the Emacs interface to the MH mail system
3508 message-user-agent -- use the GNUS mail sending package
3510 Additional valid symbols may be available; check with the author of
3511 your package for details."
3512 :type '(radio (function-item :tag "Default Emacs mail"
3514 sendmail-user-agent)
3515 (function-item :tag "Emacs interface to MH"
3518 (function-item :tag "Gnus mail sending package"
3521 (function :tag "Other"))
3524 (defun define-mail-user-agent (symbol composefunc sendfunc
3525 &optional abortfunc hookvar)
3526 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3528 SYMBOL can be any Lisp symbol. Its function definition and/or
3529 value as a variable do not matter for this usage; we use only certain
3530 properties on its property list, to encode the rest of the arguments.
3532 COMPOSEFUNC is program callable function that composes an outgoing
3533 mail message buffer. This function should set up the basics of the
3534 buffer without requiring user interaction. It should populate the
3535 standard mail headers, leaving the `to:' and `subject:' headers blank
3538 COMPOSEFUNC should accept several optional arguments--the same
3539 arguments that `compose-mail' takes. See that function's documentation.
3541 SENDFUNC is the command a user would run to send the message.
3543 Optional ABORTFUNC is the command a user would run to abort the
3544 message. For mail packages that don't have a separate abort function,
3545 this can be `kill-buffer' (the equivalent of omitting this argument).
3547 Optional HOOKVAR is a hook variable that gets run before the message
3548 is actually sent. Callers that use the `mail-user-agent' may
3549 install a hook function temporarily on this hook variable.
3550 If HOOKVAR is nil, `mail-send-hook' is used.
3552 The properties used on SYMBOL are `composefunc', `sendfunc',
3553 `abortfunc', and `hookvar'."
3554 (put symbol 'composefunc composefunc)
3555 (put symbol 'sendfunc sendfunc)
3556 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3557 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3559 (defun assoc-ignore-case (key alist)
3560 "Like `assoc', but ignores differences in case and text representation.
3561 KEY must be a string. Upper-case and lower-case letters are treated as equal.
3562 Unibyte strings are converted to multibyte for comparison."
3564 (while (and alist (not element))
3565 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
3566 (setq element (car alist)))
3567 (setq alist (cdr alist)))
3570 (defun assoc-ignore-representation (key alist)
3571 "Like `assoc', but ignores differences in text representation.
3572 KEY must be a string.
3573 Unibyte strings are converted to multibyte for comparison."
3575 (while (and alist (not element))
3576 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
3577 (setq element (car alist)))
3578 (setq alist (cdr alist)))
3581 (define-mail-user-agent 'sendmail-user-agent
3582 'sendmail-user-agent-compose
3583 'mail-send-and-exit)
3585 (defun rfc822-goto-eoh ()
3586 ;; Go to header delimiter line in a mail message, following RFC822 rules
3587 (goto-char (point-min))
3588 (while (looking-at "^[^: \n]+:\\|^[ \t]")
3592 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3593 switch-function yank-action
3596 (let ((special-display-buffer-names nil)
3597 (special-display-regexps nil)
3598 (same-window-buffer-names nil)
3599 (same-window-regexps nil))
3600 (funcall switch-function "*mail*")))
3601 (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3602 (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))))
3603 (or (mail continue to subject in-reply-to cc yank-action send-actions)
3605 (error "Message aborted"))
3608 (while other-headers
3609 (if (not (member (car (car other-headers)) '("in-reply-to" "cc")))
3610 (insert (car (car other-headers)) ": "
3611 (cdr (car other-headers)) "\n"))
3612 (setq other-headers (cdr other-headers)))
3615 (define-mail-user-agent 'mh-e-user-agent
3616 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
3617 'mh-before-send-letter-hook)
3619 (defun compose-mail (&optional to subject other-headers continue
3620 switch-function yank-action send-actions)
3621 "Start composing a mail message to send.
3622 This uses the user's chosen mail composition package
3623 as selected with the variable `mail-user-agent'.
3624 The optional arguments TO and SUBJECT specify recipients
3625 and the initial Subject field, respectively.
3627 OTHER-HEADERS is an alist specifying additional
3628 header fields. Elements look like (HEADER . VALUE) where both
3629 HEADER and VALUE are strings.
3631 CONTINUE, if non-nil, says to continue editing a message already
3634 SWITCH-FUNCTION, if non-nil, is a function to use to
3635 switch to and display the buffer used for mail composition.
3637 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3638 to insert the raw text of the message being replied to.
3639 It has the form (FUNCTION . ARGS). The user agent will apply
3640 FUNCTION to ARGS, to insert the raw text of the original message.
3641 \(The user agent will also run `mail-citation-hook', *after* the
3642 original text has been inserted in this way.)
3644 SEND-ACTIONS is a list of actions to call when the message is sent.
3645 Each action has the form (FUNCTION . ARGS)."
3647 (list nil nil nil current-prefix-arg))
3648 (let ((function (get mail-user-agent 'composefunc)))
3649 (funcall function to subject other-headers continue
3650 switch-function yank-action send-actions)))
3652 (defun compose-mail-other-window (&optional to subject other-headers continue
3653 yank-action send-actions)
3654 "Like \\[compose-mail], but edit the outgoing message in another window."
3656 (list nil nil nil current-prefix-arg))
3657 (compose-mail to subject other-headers continue
3658 'switch-to-buffer-other-window yank-action send-actions))
3661 (defun compose-mail-other-frame (&optional to subject other-headers continue
3662 yank-action send-actions)
3663 "Like \\[compose-mail], but edit the outgoing message in another frame."
3665 (list nil nil nil current-prefix-arg))
3666 (compose-mail to subject other-headers continue
3667 'switch-to-buffer-other-frame yank-action send-actions))
3669 (defvar set-variable-value-history nil
3670 "History of values entered with `set-variable'.")
3672 (defun set-variable (var val)
3673 "Set VARIABLE to VALUE. VALUE is a Lisp object.
3674 When using this interactively, enter a Lisp object for VALUE.
3675 If you want VALUE to be a string, you must surround it with doublequotes.
3676 VALUE is used literally, not evaluated.
3678 If VARIABLE has a `variable-interactive' property, that is used as if
3679 it were the arg to `interactive' (which see) to interactively read VALUE.
3681 If VARIABLE has been defined with `defcustom', then the type information
3682 in the definition is used to check that VALUE is valid."
3684 (let* ((default-var (variable-at-point))
3685 (var (if (symbolp default-var)
3686 (read-variable (format "Set variable (default %s): " default-var)
3688 (read-variable "Set variable: ")))
3689 (minibuffer-help-form '(describe-variable var))
3690 (prop (get var 'variable-interactive))
3691 (prompt (format "Set %s to value: " var))
3693 ;; Use VAR's `variable-interactive' property
3694 ;; as an interactive spec for prompting.
3695 (call-interactively `(lambda (arg)
3699 (read-string prompt nil
3700 'set-variable-value-history)))))
3703 (let ((type (get var 'custom-type)))
3705 ;; Match with custom type.
3707 (setq type (widget-convert type))
3708 (unless (widget-apply type :match val)
3709 (error "Value `%S' does not match type %S of %S"
3710 val (car type) var))))
3713 ;; Define the major mode for lists of completions.
3715 (defvar completion-list-mode-map nil
3716 "Local map for completion list buffers.")
3717 (or completion-list-mode-map
3718 (let ((map (make-sparse-keymap)))
3719 (define-key map [mouse-2] 'mouse-choose-completion)
3720 (define-key map [down-mouse-2] nil)
3721 (define-key map "\C-m" 'choose-completion)
3722 (define-key map "\e\e\e" 'delete-completion-window)
3723 (define-key map [left] 'previous-completion)
3724 (define-key map [right] 'next-completion)
3725 (setq completion-list-mode-map map)))
3727 ;; Completion mode is suitable only for specially formatted data.
3728 (put 'completion-list-mode 'mode-class 'special)
3730 (defvar completion-reference-buffer nil
3731 "Record the buffer that was current when the completion list was requested.
3732 This is a local variable in the completion list buffer.
3733 Initial value is nil to avoid some compiler warnings.")
3735 (defvar completion-no-auto-exit nil
3736 "Non-nil means `choose-completion-string' should never exit the minibuffer.
3737 This also applies to other functions such as `choose-completion'
3738 and `mouse-choose-completion'.")
3740 (defvar completion-base-size nil
3741 "Number of chars at beginning of minibuffer not involved in completion.
3742 This is a local variable in the completion list buffer
3743 but it talks about the buffer in `completion-reference-buffer'.
3744 If this is nil, it means to compare text to determine which part
3745 of the tail end of the buffer's text is involved in completion.")
3747 (defun delete-completion-window ()
3748 "Delete the completion list window.
3749 Go to the window from which completion was requested."
3751 (let ((buf completion-reference-buffer))
3752 (if (one-window-p t)
3753 (if (window-dedicated-p (selected-window))
3754 (delete-frame (selected-frame)))
3755 (delete-window (selected-window))
3756 (if (get-buffer-window buf)
3757 (select-window (get-buffer-window buf))))))
3759 (defun previous-completion (n)
3760 "Move to the previous item in the completion list."
3762 (next-completion (- n)))
3764 (defun next-completion (n)
3765 "Move to the next item in the completion list.
3766 With prefix argument N, move N items (negative N means move backward)."
3768 (while (and (> n 0) (not (eobp)))
3769 (let ((prop (get-text-property (point) 'mouse-face))
3771 ;; If in a completion, move to the end of it.
3773 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3774 ;; Move to start of next one.
3775 (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3777 (while (and (< n 0) (not (bobp)))
3778 (let ((prop (get-text-property (1- (point)) 'mouse-face))
3780 ;; If in a completion, move to the start of it.
3782 (goto-char (previous-single-property-change
3783 (point) 'mouse-face nil end)))
3784 ;; Move to end of the previous completion.
3785 (goto-char (previous-single-property-change (point) 'mouse-face nil end))
3786 ;; Move to the start of that one.
3787 (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
3790 (defun choose-completion ()
3791 "Choose the completion that point is in or next to."
3793 (let (beg end completion (buffer completion-reference-buffer)
3794 (base-size completion-base-size))
3795 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
3796 (setq end (point) beg (1+ (point))))
3797 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
3798 (setq end (1- (point)) beg (point)))
3800 (error "No completion here"))
3801 (setq beg (previous-single-property-change beg 'mouse-face))
3802 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
3803 (setq completion (buffer-substring beg end))
3804 (let ((owindow (selected-window)))
3805 (if (and (one-window-p t 'selected-frame)
3806 (window-dedicated-p (selected-window)))
3807 ;; This is a special buffer's frame
3808 (iconify-frame (selected-frame))
3809 (or (window-dedicated-p (selected-window))
3811 (select-window owindow))
3812 (choose-completion-string completion buffer base-size)))
3814 ;; Delete the longest partial match for STRING
3815 ;; that can be found before POINT.
3816 (defun choose-completion-delete-max-match (string)
3817 (let ((opoint (point))
3818 (len (min (length string)
3819 (- (point) (point-min)))))
3820 (goto-char (- (point) (length string)))
3821 (if completion-ignore-case
3822 (setq string (downcase string)))
3823 (while (and (> len 0)
3824 (let ((tail (buffer-substring (point)
3826 (if completion-ignore-case
3827 (setq tail (downcase tail)))
3828 (not (string= tail (substring string 0 len)))))
3833 ;; Switch to BUFFER and insert the completion choice CHOICE.
3834 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
3835 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
3837 ;; If BUFFER is the minibuffer, exit the minibuffer
3838 ;; unless it is reading a file name and CHOICE is a directory,
3839 ;; or completion-no-auto-exit is non-nil.
3840 (defun choose-completion-string (choice &optional buffer base-size)
3841 (let ((buffer (or buffer completion-reference-buffer)))
3842 ;; If BUFFER is a minibuffer, barf unless it's the currently
3843 ;; active minibuffer.
3844 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
3845 (or (not (active-minibuffer-window))
3847 (window-buffer (active-minibuffer-window))))))
3848 (error "Minibuffer is not active for completion")
3849 ;; Insert the completion into the buffer where completion was requested.
3852 (delete-region (+ base-size (point-min)) (point))
3853 (choose-completion-delete-max-match choice))
3855 (remove-text-properties (- (point) (length choice)) (point)
3857 ;; Update point in the window that BUFFER is showing in.
3858 (let ((window (get-buffer-window buffer t)))
3859 (set-window-point window (point)))
3860 ;; If completing for the minibuffer, exit it with this choice.
3861 (and (not completion-no-auto-exit)
3862 (equal buffer (window-buffer (minibuffer-window)))
3863 minibuffer-completion-table
3864 ;; If this is reading a file name, and the file name chosen
3865 ;; is a directory, don't exit the minibuffer.
3866 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
3867 (file-directory-p (buffer-string)))
3868 (select-window (active-minibuffer-window))
3869 (exit-minibuffer))))))
3871 (defun completion-list-mode ()
3872 "Major mode for buffers showing lists of possible completions.
3873 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
3874 to select the completion near point.
3875 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
3878 (kill-all-local-variables)
3879 (use-local-map completion-list-mode-map)
3880 (setq mode-name "Completion List")
3881 (setq major-mode 'completion-list-mode)
3882 (make-local-variable 'completion-base-size)
3883 (setq completion-base-size nil)
3884 (run-hooks 'completion-list-mode-hook))
3886 (defvar completion-setup-hook nil
3887 "Normal hook run at the end of setting up a completion list buffer.
3888 When this hook is run, the current buffer is the one in which the
3889 command to display the completion list buffer was run.
3890 The completion list buffer is available as the value of `standard-output'.")
3892 ;; This function goes in completion-setup-hook, so that it is called
3893 ;; after the text of the completion list buffer is written.
3895 (defun completion-setup-function ()
3897 (let ((mainbuf (current-buffer)))
3898 (set-buffer standard-output)
3899 (completion-list-mode)
3900 (make-local-variable 'completion-reference-buffer)
3901 (setq completion-reference-buffer mainbuf)
3902 (if (eq minibuffer-completion-table 'read-file-name-internal)
3903 ;; For file name completion,
3904 ;; use the number of chars before the start of the
3905 ;; last file name component.
3906 (setq completion-base-size
3908 (set-buffer mainbuf)
3909 (goto-char (point-max))
3910 (skip-chars-backward (format "^%c" directory-sep-char))
3911 (- (point) (point-min))))
3912 ;; Otherwise, in minibuffer, the whole input is being completed.
3914 (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'"
3915 (buffer-name mainbuf))
3916 (setq completion-base-size 0))))
3917 (goto-char (point-min))
3919 (insert (substitute-command-keys
3920 "Click \\[mouse-choose-completion] on a completion to select it.\n")))
3921 (insert (substitute-command-keys
3922 "In this buffer, type \\[choose-completion] to \
3923 select the completion near point.\n\n")))))
3925 (add-hook 'completion-setup-hook 'completion-setup-function)
3927 (define-key minibuffer-local-completion-map [prior]
3928 'switch-to-completions)
3929 (define-key minibuffer-local-must-match-map [prior]
3930 'switch-to-completions)
3931 (define-key minibuffer-local-completion-map "\M-v"
3932 'switch-to-completions)
3933 (define-key minibuffer-local-must-match-map "\M-v"
3934 'switch-to-completions)
3936 (defun switch-to-completions ()
3937 "Select the completion list window."
3939 ;; Make sure we have a completions window.
3940 (or (get-buffer-window "*Completions*")
3941 (minibuffer-completion-help))
3942 (let ((window (get-buffer-window "*Completions*")))
3944 (select-window window)
3945 (goto-char (point-min))
3946 (search-forward "\n\n")
3949 ;; Support keyboard commands to turn on various modifiers.
3951 ;; These functions -- which are not commands -- each add one modifier
3952 ;; to the following event.
3954 (defun event-apply-alt-modifier (ignore-prompt)
3955 "Add the Alt modifier to the following event.
3956 For example, type \\[event-apply-alt-modifier] & to enter Alt-&."
3957 (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
3958 (defun event-apply-super-modifier (ignore-prompt)
3959 "Add the Super modifier to the following event.
3960 For example, type \\[event-apply-super-modifier] & to enter Super-&."
3961 (vector (event-apply-modifier (read-event) 'super 23 "s-")))
3962 (defun event-apply-hyper-modifier (ignore-prompt)
3963 "Add the Hyper modifier to the following event.
3964 For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&."
3965 (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
3966 (defun event-apply-shift-modifier (ignore-prompt)
3967 "Add the Shift modifier to the following event.
3968 For example, type \\[event-apply-shift-modifier] & to enter Shift-&."
3969 (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
3970 (defun event-apply-control-modifier (ignore-prompt)
3971 "Add the Ctrl modifier to the following event.
3972 For example, type \\[event-apply-control-modifier] & to enter Ctrl-&."
3973 (vector (event-apply-modifier (read-event) 'control 26 "C-")))
3974 (defun event-apply-meta-modifier (ignore-prompt)
3975 "Add the Meta modifier to the following event.
3976 For example, type \\[event-apply-meta-modifier] & to enter Meta-&."
3977 (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
3979 (defun event-apply-modifier (event symbol lshiftby prefix)
3980 "Apply a modifier flag to event EVENT.
3981 SYMBOL is the name of this modifier, as a symbol.
3982 LSHIFTBY is the numeric value of this modifier, in keyboard events.
3983 PREFIX is the string that represents this modifier in an event type symbol."
3985 (cond ((eq symbol 'control)
3986 (if (and (<= (downcase event) ?z)
3987 (>= (downcase event) ?a))
3988 (- (downcase event) ?a -1)
3989 (if (and (<= (downcase event) ?Z)
3990 (>= (downcase event) ?A))
3991 (- (downcase event) ?A -1)
3992 (logior (lsh 1 lshiftby) event))))
3994 (if (and (<= (downcase event) ?z)
3995 (>= (downcase event) ?a))
3997 (logior (lsh 1 lshiftby) event)))
3999 (logior (lsh 1 lshiftby) event)))
4000 (if (memq symbol (event-modifiers event))
4002 (let ((event-type (if (symbolp event) event (car event))))
4003 (setq event-type (intern (concat prefix (symbol-name event-type))))
4006 (cons event-type (cdr event)))))))
4008 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
4009 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
4010 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
4011 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
4012 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
4013 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
4015 ;;;; Keypad support.
4017 ;;; Make the keypad keys act like ordinary typing keys. If people add
4018 ;;; bindings for the function key symbols, then those bindings will
4019 ;;; override these, so this shouldn't interfere with any existing
4022 ;; Also tell read-char how to handle these keys.
4024 (lambda (keypad-normal)
4025 (let ((keypad (nth 0 keypad-normal))
4026 (normal (nth 1 keypad-normal)))
4027 (put keypad 'ascii-character normal)
4028 (define-key function-key-map (vector keypad) (vector normal))))
4029 '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
4030 (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
4042 ;;; simple.el ends here