]> code.delx.au - gnu-emacs/blobdiff - lisp/simple.el
Change indian-1-column charset to indian-glyph charset.
[gnu-emacs] / lisp / simple.el
index d04a3b0091e33475524fb3b07ced30b45442a656..74821ae31169fbd972edbeecc5963836a4b7f0c7 100644 (file)
@@ -230,7 +230,8 @@ On nonblank line, delete any immediately following blank lines."
 (defun delete-trailing-whitespace ()
   "Delete all the trailing whitespace across the current buffer.
 All whitespace after the last non-whitespace character in a line is deleted.
-This respects narrowing, created by \\[narrow-to-region] and friends."
+This respects narrowing, created by \\[narrow-to-region] and friends.
+A formfeed is not considered whitespace by this function."
   (interactive "*")
   (save-match-data
     (save-excursion
@@ -238,8 +239,9 @@ This respects narrowing, created by \\[narrow-to-region] and friends."
       (while (re-search-forward "\\s-$" nil t)
        (skip-syntax-backward "-" (save-excursion (forward-line 0) (point)))
        ;; Don't delete formfeeds, even if they are considered whitespace.
-       (if (looking-at ".*\f")
-           (goto-char (match-end 0)))
+       (save-match-data
+         (if (looking-at ".*\f")
+             (goto-char (match-end 0))))
        (delete-region (point) (match-end 0))))))
 
 (defun newline-and-indent ()
@@ -261,11 +263,15 @@ In programming language modes, this is the same as TAB.
 In some text modes, where TAB inserts a tab, this indents to the
 column specified by the function `current-left-margin'."
   (interactive "*")
-  (save-excursion
-    (delete-horizontal-space t)
-    (indent-according-to-mode))
-  (newline)
-  (indent-according-to-mode))
+  (delete-horizontal-space t)
+  (let ((pos (point)))
+    ;; Be careful to insert the newline before indenting the line.
+    ;; Otherwise, the indentation might be wrong.
+    (newline)
+    (save-excursion
+      (goto-char pos)
+      (indent-according-to-mode))
+    (indent-according-to-mode)))
 
 (defun quoted-insert (arg)
   "Read next input character and insert it.
@@ -339,28 +345,31 @@ Leave one space or none, according to the context."
   "Delete all spaces and tabs around point.
 If BACKWARD-ONLY is non-nil, only delete spaces before point."
   (interactive "*")
-  (delete-region
-   (if backward-only
-       (point)
+  (let ((orig-pos (point)))
+    (delete-region
+     (if backward-only
+        orig-pos
+       (progn
+        (skip-chars-forward " \t")
+        (constrain-to-field nil orig-pos t)))
      (progn
-       (skip-chars-forward " \t" (field-end))
-       (point)))
-   (progn
-     (skip-chars-backward " \t" (field-beginning nil t))
-     (point))))
+       (skip-chars-backward " \t")
+       (constrain-to-field nil orig-pos)))))
 
 (defun just-one-space ()
   "Delete all spaces and tabs around point, leaving one space."
   (interactive "*")
-  (skip-chars-backward " \t" (field-beginning))
-  (if (= (following-char) ? )
-      (forward-char 1)
-    (insert ? ))
-  (delete-region
-   (point)
-   (progn
-     (skip-chars-forward " \t" (field-end nil t))
-     (point))))
+  (let ((orig-pos (point)))
+    (skip-chars-backward " \t")
+    (constrain-to-field nil orig-pos)
+    (if (= (following-char) ? )
+       (forward-char 1)
+      (insert ? ))
+    (delete-region
+     (point)
+     (progn
+       (skip-chars-forward " \t")
+       (constrain-to-field nil orig-pos t)))))
 
 (defun beginning-of-buffer (&optional arg)
   "Move point to the beginning of the buffer; leave mark at previous position.
@@ -407,7 +416,7 @@ Don't use this command in Lisp programs!
   ;; If we went to a place in the middle of the buffer,
   ;; adjust it to the beginning of a line.
   (cond (arg (forward-line 1))
-       ((< (point) (window-end nil t))
+       ((> (point) (window-end nil t))
         ;; If the end of the buffer is not already on the screen,
         ;; then scroll specially to put it near, but not at, the bottom.
         (overlay-recenter (point))
@@ -451,10 +460,10 @@ that uses or sets the mark."
       (save-restriction
        (goto-char (point-min))
        (widen)
-       (beginning-of-line)
+       (forward-line 0)
        (setq start (point))
        (goto-char opoint)
-       (beginning-of-line)
+       (forward-line 0)
        (if (/= start 1)
            (message "line %d (narrowed line %d)"
                     (1+ (count-lines 1 (point)))
@@ -561,7 +570,7 @@ in *Help* buffer.  See also the command `describe-char-after'."
 (defcustom eval-expression-print-level 4
   "*Value to use for `print-level' when printing value in `eval-expression'."
   :group 'lisp
-  :type 'integer
+  :type '(choice (const nil) integer)
   :version "21.1")
 
 (defcustom eval-expression-print-length 12
@@ -581,8 +590,11 @@ If nil, don't change the value of `debug-on-error'."
 ;; for the sake of completion of names like eval-region, eval-current-buffer.
 (defun eval-expression (eval-expression-arg
                        &optional eval-expression-insert-value)
-  "Evaluate EXPRESSION and print value in minibuffer.
-Value is also consed on to front of the variable `values'."
+  "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area.
+Value is also consed on to front of the variable `values'.
+Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means
+insert the result into the current buffer instead of printing it in
+the echo area."
   (interactive
    (list (read-from-minibuffer "Eval: "
                               nil read-expression-map t
@@ -665,7 +677,7 @@ to get different commands to edit and resubmit."
 This is used for all minibuffer input
 except when an alternate history list is specified.")
 (defvar minibuffer-history-sexp-flag nil
-  "Non-nil when doing history operations on `command-history'.
+  "Non-nil when doing history operations on the variable `command-history'.
 More generally, indicates that the history list being acted on
 contains expressions rather than strings.
 It is only valid if its value equals the current minibuffer depth,
@@ -674,36 +686,6 @@ to handle recursive uses of the minibuffer.")
 (setq minibuffer-history-position nil)
 (defvar minibuffer-history-search-history nil)
 
-(mapcar
- (lambda (key-and-command)
-   (mapcar
-    (lambda (keymap-and-completionp)
-      ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
-      ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
-      ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
-      (define-key (symbol-value (car keymap-and-completionp))
-       (car key-and-command)
-       (let ((command (cdr key-and-command)))
-         (if (consp command)
-             ;; (and ... nil) => ... turns back on the completion-oriented
-             ;; history commands which rms turned off since they seem to
-             ;; do things he doesn't like.
-             (if (and (cdr keymap-and-completionp) nil) ;XXX turned off
-                 (progn (error "EMACS BUG!") (cdr command))
-               (car command))
-           command))))
-    '((minibuffer-local-map . nil)
-      (minibuffer-local-ns-map . nil)
-      (minibuffer-local-completion-map . t)
-      (minibuffer-local-must-match-map . t)
-      (read-expression-map . nil))))
- '(("\en" . (next-history-element . next-complete-history-element))
-   ([next] . (next-history-element . next-complete-history-element))
-   ("\ep" . (previous-history-element . previous-complete-history-element))
-   ([prior] . (previous-history-element . previous-complete-history-element))
-   ("\er" . previous-matching-history-element)
-   ("\es" . next-matching-history-element)))
-
 (defvar minibuffer-text-before-history nil
   "Text that was in this minibuffer before any history commands.
 This is nil if there have not yet been any history commands
@@ -731,7 +713,9 @@ If a history variable is a member of this list, then the
 \(Previous history elements refer to earlier actions.)
 With prefix argument N, search for Nth previous match.
 If N is negative, find the next or Nth next match.
-An uppercase letter in REGEXP makes the search case-sensitive.
+Normally, history elements are matched case-insensitively if
+`case-fold-search' is non-nil, but an uppercase letter in REGEXP
+makes the search case-sensitive.
 See also `minibuffer-history-case-insensitive-variables'."
   (interactive
    (let* ((enable-recursive-minibuffers t)
@@ -750,7 +734,8 @@ See also `minibuffer-history-case-insensitive-variables'."
   (unless (zerop n)
     (if (and (zerop minibuffer-history-position)
             (null minibuffer-text-before-history))
-       (setq minibuffer-text-before-history (field-string (point-max))))
+       (setq minibuffer-text-before-history
+             (minibuffer-contents-no-properties)))
     (let ((history (symbol-value minibuffer-history-variable))
          (case-fold-search
           (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
@@ -787,11 +772,11 @@ See also `minibuffer-history-case-insensitive-variables'."
          (setq n (+ n (if (< n 0) 1 -1)))))
       (setq minibuffer-history-position pos)
       (goto-char (point-max))
-      (delete-field)
+      (delete-minibuffer-contents)
       (insert match-string)
-      (goto-char (+ (field-beginning) match-offset))))
-  (if (or (eq (car (car command-history)) 'previous-matching-history-element)
-         (eq (car (car command-history)) 'next-matching-history-element))
+      (goto-char (+ (minibuffer-prompt-end) match-offset))))
+  (if (memq (car (car command-history)) '(previous-matching-history-element
+                                         next-matching-history-element))
       (setq command-history (cdr command-history))))
 
 (defun next-matching-history-element (regexp n)
@@ -799,7 +784,9 @@ See also `minibuffer-history-case-insensitive-variables'."
 \(The next history element refers to a more recent action.)
 With prefix argument N, search for Nth next match.
 If N is negative, find the previous or Nth previous match.
-An uppercase letter in REGEXP makes the search case-sensitive."
+Normally, history elements are matched case-insensitively if
+`case-fold-search' is non-nil, but an uppercase letter in REGEXP
+makes the search case-sensitive."
   (interactive
    (let* ((enable-recursive-minibuffers t)
          (regexp (read-from-minibuffer "Next element matching (regexp): "
@@ -826,22 +813,23 @@ An uppercase letter in REGEXP makes the search case-sensitive."
            elt minibuffer-returned-to-present)
        (if (and (zerop minibuffer-history-position)
                 (null minibuffer-text-before-history))
-           (setq minibuffer-text-before-history (field-string (point-max))))
+           (setq minibuffer-text-before-history
+                 (minibuffer-contents-no-properties)))
        (if (< narg minimum)
            (if minibuffer-default
                (error "End of history; no next item")
              (error "End of history; no default available")))
        (if (> narg (length (symbol-value minibuffer-history-variable)))
            (error "Beginning of history; no preceding item"))
-       (unless (or (eq last-command 'next-history-element)
-                   (eq last-command 'previous-history-element))
-         (let ((prompt-end (field-beginning (point-max))))
+       (unless (memq last-command '(next-history-element
+                                    previous-history-element))
+         (let ((prompt-end (minibuffer-prompt-end)))
            (set (make-local-variable 'minibuffer-temporary-goal-position)
                 (cond ((<= (point) prompt-end) prompt-end)
                       ((eobp) nil)
                       (t (point))))))
        (goto-char (point-max))
-       (delete-field)
+       (delete-minibuffer-contents)
        (setq minibuffer-history-position narg)
        (cond ((= narg -1)
               (setq elt minibuffer-default))
@@ -872,7 +860,7 @@ by the new completion."
   (let ((point-at-start (point)))
     (next-matching-history-element
      (concat
-      "^" (regexp-quote (buffer-substring (field-beginning) (point))))
+      "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point))))
      n)
     ;; next-matching-history-element always puts us at (point-min).
     ;; Move to the position we were at before changing the buffer contents.
@@ -887,35 +875,13 @@ by the new completion."
   (interactive "p")
   (next-complete-history-element (- n)))
 
-;; These two functions are for compatibility with the old subrs of the
-;; same name.
-
+;; For compatibility with the old subr of the same name.
 (defun minibuffer-prompt-width ()
   "Return the display width of the minibuffer prompt.
 Return 0 if current buffer is not a mini-buffer."
   ;; Return the width of everything before the field at the end of
   ;; the buffer; this should be 0 for normal buffers.
-  (1- (field-beginning (point-max))))
-
-(defun minibuffer-prompt-end ()
-  "Return the buffer position of the end of the minibuffer prompt.
-Return (point-min) if current buffer is not a mini-buffer."
-  (field-beginning (point-max)))
-
-(defun minibuffer-contents ()
-  "Return the user input in a minbuffer as a string.
-The current buffer must be a minibuffer."
-  (field-string (point-max)))
-
-(defun minibuffer-contents-no-properties ()
-  "Return the user input in a minbuffer as a string, without text-properties.
-The current buffer must be a minibuffer."
-  (field-string-no-properties (point-max)))
-
-(defun delete-minibuffer-contents  ()
-  "Delete all user input in a minibuffer.
-The current buffer must be a minibuffer."
-  (delete-field (point-max)))
+  (1- (minibuffer-prompt-end)))
 
 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
 (defalias 'advertised-undo 'undo)
@@ -949,13 +915,24 @@ as an argument limits undo to changes within the current region."
     ;; Don't specify a position in the undo record for the undo command.
     ;; Instead, undoing this should move point to where the change is.
     (let ((tail buffer-undo-list)
-         done)
-      (while (and tail (not done) (not (null (car tail))))
-       (if (integerp (car tail))
-           (progn
-             (setq done t)
-             (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
-       (setq tail (cdr tail))))
+         (prev nil))
+      (while (car tail)
+       (when (integerp (car tail))
+         (let ((pos (car tail)))
+           (if (null prev)
+               (setq buffer-undo-list (cdr tail))
+             (setcdr prev (cdr tail)))
+           (setq tail (cdr tail))
+           (while (car tail)
+             (if (eq pos (car tail))
+                 (if prev
+                     (setcdr prev (cdr tail))
+                   (setq buffer-undo-list (cdr tail)))
+               (setq prev tail))
+             (setq tail (cdr tail)))
+           (setq tail nil)))
+       (setq prev tail tail (cdr tail))))
+
     (and modified (not (buffer-modified-p))
         (delete-auto-save-file-if-necessary recent-save)))
   ;; If we do get all the way thru, make this-command indicate that.
@@ -1039,11 +1016,12 @@ we stop and ignore all further elements."
              (let ((position (car delta))
                    (offset (cdr delta)))
 
-               ;; Loop down the earlier events adjusting their buffer positions
-               ;; to reflect the fact that a change to the buffer isn't being
-               ;; undone. We only need to process those element types which
-               ;; undo-elt-in-region will return as being in the region since
-               ;; only those types can ever get into the output
+               ;; Loop down the earlier events adjusting their buffer
+               ;; positions to reflect the fact that a change to the buffer
+               ;; isn't being undone. We only need to process those element
+               ;; types which undo-elt-in-region will return as being in
+               ;; the region since only those types can ever get into the
+               ;; output
 
                (while temp-undo-list
                  (setq undo-elt (car temp-undo-list))
@@ -1139,6 +1117,34 @@ is not *inside* the region START...END."
             '(0 . 0)))
     '(0 . 0)))
 
+(defun undo-get-state ()
+  "Return a handler for the current state to which we might want to undo.
+The returned handler can then be passed to `undo-revert-to-handle'."
+  (unless (eq buffer-undo-list t)
+    buffer-undo-list))
+
+(defun undo-revert-to-state (handle)
+  "Revert to the state HANDLE earlier grabbed with `undo-get-handle'.
+This undoing is not itself undoable (aka redoable)."
+  (unless (eq buffer-undo-list t)
+    (let ((new-undo-list (cons (car handle) (cdr handle))))
+      ;; Truncate the undo log at `handle'.
+      (when handle
+       (setcar handle nil) (setcdr handle nil))
+      (unless (eq last-command 'undo) (undo-start))
+      ;; Make sure there's no confusion.
+      (when (and handle (not (eq handle (last pending-undo-list))))
+       (error "Undoing to some unrelated state"))
+      ;; Undo it all.
+      (while pending-undo-list (undo-more 1))
+      ;; Reset the modified cons cell to its original content.
+      (when handle
+       (setcar handle (car new-undo-list))
+       (setcdr handle (cdr new-undo-list)))
+      ;; Revert the undo info to what it was when we grabbed the state.
+      (setq buffer-undo-list handle))))
+  
+\f
 (defvar shell-command-history nil
   "History list for some commands that read shell commands.")
 
@@ -1164,9 +1170,7 @@ the buffer `*Shell Command Output*'.  If the output is short enough to
 display in the echo area (which is determined by the variables
 `resize-mini-windows' and `max-mini-window-height'), it is shown
 there, but it is nonetheless available in buffer `*Shell Command
-Output*' even though that buffer is not automatically displayed.  If
-there is no output, or if output is inserted in the current buffer,
-then `*Shell Command Output*' is deleted.
+Output*' even though that buffer is not automatically displayed.
 
 To specify a coding system for converting non-ASCII characters
 in the shell command output, use \\[universal-coding-system-argument]
@@ -1332,7 +1336,8 @@ and only used if a buffer is displayed."
                   (t
                    ;; Buffer
                    (goto-char (point-min))
-                   (display-buffer message not-this-window frame))))))))
+                   (display-buffer (current-buffer)
+                                   not-this-window frame))))))))
 
 
 ;; We have a sentinel to prevent insertion of a termination message
@@ -1423,9 +1428,10 @@ specifies the value of ERROR-BUFFER."
                                         (list t error-file)
                                       t)
                                     nil shell-command-switch command))
-         (let ((shell-buffer (get-buffer "*Shell Command Output*")))
-           (and shell-buffer (not (eq shell-buffer (current-buffer)))
-                (kill-buffer shell-buffer)))
+         ;; It is rude to delete a buffer which the command is not using.
+         ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
+         ;;   (and shell-buffer (not (eq shell-buffer (current-buffer)))
+         ;;     (kill-buffer shell-buffer)))
          ;; Don't muck with mark unless REPLACE says we should.
          (and replace swap (exchange-point-and-mark)))
       ;; No prefix argument: put the output in a temp buffer,
@@ -1475,7 +1481,9 @@ specifies the value of ERROR-BUFFER."
                         "(Shell command %sed with some error output)"
                       "(Shell command %sed with no output)")
                     (if (equal 0 exit-status) "succeed" "fail"))
-           (kill-buffer buffer)))))
+           ;; Don't kill: there might be useful info in the undo-log.
+           ;; (kill-buffer buffer)
+           ))))
 
     (when (and error-file (file-exists-p error-file))
       (if (< 0 (nth 7 (file-attributes error-file)))
@@ -1745,7 +1753,11 @@ yanking point; just return the Nth kill forward."
   "Kill between point and mark.
 The text is deleted but saved in the kill ring.
 The command \\[yank] can retrieve it from there.
-\(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
+\(If you want to kill and then yank immediately, use \\[kill-ring-save].)
+
+If you want to append the killed region to the last killed text,
+use \\[append-next-kill] before \\[kill-region].
+
 If the buffer is read-only, Emacs will beep and refrain from deleting
 the text, but put the text in the kill ring anyway.  This means that
 you can use the killing commands to copy text from a read-only buffer.
@@ -1805,6 +1817,9 @@ In Transient Mark mode, deactivate the mark.
 If `interprogram-cut-function' is non-nil, also save the text for a window
 system cut and paste.
 
+If you want to append the killed line to the last killed text,
+use \\[append-next-kill] before \\[kill-ring-save].
+
 This command is similar to `copy-region-as-kill', except that it gives
 visual feedback indicating the extent of the region being copied."
   (interactive "r")
@@ -1816,7 +1831,7 @@ visual feedback indicating the extent of the region being copied."
            ;; look like a C-g typed as a command.
            (inhibit-quit t))
        (if (pos-visible-in-window-p other-end (selected-window))
-           (progn
+           (unless transient-mark-mode
              ;; Swap point and mark.
              (set-marker (mark-marker) (point) (current-buffer))
              (goto-char other-end)
@@ -2005,7 +2020,14 @@ To kill a whole line, when point is not at the beginning, type \
 If `kill-whole-line' is non-nil, then this command kills the whole line
 including its terminating newline, when used at the beginning of a line
 with no argument.  As a consequence, you can always kill a whole line
-by typing \\[beginning-of-line] \\[kill-line]."
+by typing \\[beginning-of-line] \\[kill-line].
+
+If you want to append the killed line to the last killed text,
+use \\[append-next-kill] before \\[kill-line].
+
+If the buffer is read-only, Emacs will beep and refrain from deleting
+the line, but put the line in the kill ring anyway.  This means that
+you can use this command to copy text from a read-only buffer."
   (interactive "P")
   (kill-region (point)
               ;; It is better to move point to the other end of the kill
@@ -2339,6 +2361,9 @@ So do certain other operations that set the mark
 but whose main purpose is something else--for example,
 incremental search, \\[beginning-of-buffer], and \\[end-of-buffer].
 
+You can also deactivate the mark by typing \\[keyboard-quit] or
+\\[keyboard-escape-quit].
+
 Many commands change their behavior when Transient Mark mode is in effect
 and the mark is active, by acting on the region instead of their usual
 default part of the buffer's text.  Examples of such commands include
@@ -2561,8 +2586,8 @@ Outline mode sets this."
       ;; with intangibility and point-motion hooks enabled this time.
       (goto-char opoint)
       (setq inhibit-point-motion-hooks nil)
-      (goto-char (constrain-to-field new opoint nil t
-                                    'inhibit-line-move-field-capture))
+      (goto-char
+       (constrain-to-field new opoint nil t 'inhibit-line-move-field-capture))
       ;; If intangibility processing moved us to a different line,
       ;; readjust the horizontal position within the line we ended up at.
       (when (or (< (point) line-beg) (> (point) line-end))
@@ -2577,9 +2602,9 @@ Outline mode sets this."
            (setq new (point)))
        (goto-char (point-min))
        (setq inhibit-point-motion-hooks nil)
-       (goto-char (constrain-to-field new opoint nil t
-                                      'inhibit-line-move-field-capture))
-       )))
+       (goto-char
+        (constrain-to-field new opoint nil t
+                            'inhibit-line-move-field-capture)))))
   nil)
 
 ;;; Many people have said they rarely use this feature, and often type
@@ -2694,71 +2719,46 @@ With argument 0, interchanges line point is in with line mark is in."
                       (forward-line arg))))
                  arg))
 
-(defvar transpose-subr-start1)
-(defvar transpose-subr-start2)
-(defvar transpose-subr-end1)
-(defvar transpose-subr-end2)
-
-(defun transpose-subr (mover arg)
-  (let (transpose-subr-start1
-       transpose-subr-end1
-       transpose-subr-start2
-       transpose-subr-end2)
-    (if (= arg 0)
-       (progn
-         (save-excursion
-           (funcall mover 1)
-           (setq transpose-subr-end2 (point))
-           (funcall mover -1)
-           (setq transpose-subr-start2 (point))
-           (goto-char (mark))
-           (funcall mover 1)
-           (setq transpose-subr-end1 (point))
-           (funcall mover -1)
-           (setq transpose-subr-start1 (point))
-           (transpose-subr-1))
-         (exchange-point-and-mark))
-      (if (> arg 0)
-         (progn
-           (funcall mover -1)
-           (setq transpose-subr-start1 (point))
-           (funcall mover 1)
-           (setq transpose-subr-end1 (point))
-           (funcall mover arg)
-           (setq transpose-subr-end2 (point))
-           (funcall mover (- arg))
-           (setq transpose-subr-start2 (point))
-           (transpose-subr-1)
-           (goto-char transpose-subr-end2))
-       (funcall mover -1)
-       (setq transpose-subr-start2 (point))
-       (funcall mover 1)
-       (setq transpose-subr-end2 (point))
-       (funcall mover (1- arg))
-       (setq transpose-subr-start1 (point))
-       (funcall mover (- arg))
-       (setq transpose-subr-end1 (point))
-       (transpose-subr-1)))))
-
-(defun transpose-subr-1 ()
-  (if (> (min transpose-subr-end1 transpose-subr-end2)
-        (max transpose-subr-start1 transpose-subr-start2))
-      (error "Don't have two things to transpose"))
-  (let* ((word1 (buffer-substring transpose-subr-start1 transpose-subr-end1))
-        (len1 (length word1))
-        (word2 (buffer-substring transpose-subr-start2 transpose-subr-end2))
-        (len2 (length word2)))
-    (delete-region transpose-subr-start2 transpose-subr-end2)
-    (goto-char transpose-subr-start2)
-    (insert word1)
-    (goto-char (if (< transpose-subr-start1 transpose-subr-start2)
-                  transpose-subr-start1
-                (+ transpose-subr-start1 (- len1 len2))))
-    (delete-region (point) (+ (point) len1))
+(defun transpose-subr (mover arg &optional special)
+  (let ((aux (if special mover
+              (lambda (x)
+                (cons (progn (funcall mover x) (point))
+                      (progn (funcall mover (- x)) (point))))))
+       pos1 pos2)
+    (cond
+     ((= arg 0)
+      (save-excursion
+       (setq pos1 (funcall aux 1))
+       (goto-char (mark))
+       (setq pos2 (funcall aux 1))
+       (transpose-subr-1 pos1 pos2))
+      (exchange-point-and-mark))
+     ((> arg 0)
+      (setq pos1 (funcall aux -1))
+      (setq pos2 (funcall aux arg))
+      (transpose-subr-1 pos1 pos2)
+      (goto-char (car pos2)))
+     (t
+      (setq pos1 (funcall aux -1))
+      (goto-char (car pos1))
+      (setq pos2 (funcall aux arg))
+      (transpose-subr-1 pos1 pos2)))))
+
+(defun transpose-subr-1 (pos1 pos2)
+  (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1))))
+  (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2))))
+  (when (> (car pos1) (car pos2))
+    (let ((swap pos1))
+      (setq pos1 pos2 pos2 swap)))
+  (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose"))
+  (let ((word2 (delete-and-extract-region (car pos2) (cdr pos2))))
+    (goto-char (car pos2))
+    (insert (delete-and-extract-region (car pos1) (cdr pos1)))
+    (goto-char (car pos1))
     (insert word2)))
 
 (defun backward-word (arg)
-  "Move backward until encountering the end of a word.
+  "Move backward until encountering the beginning of a word.
 With argument, do this that many times."
   (interactive "p")
   (forward-word (- arg)))
@@ -2818,8 +2818,7 @@ or adjacent to a word."
        (buffer-substring-no-properties start end)))))
 
 (defcustom fill-prefix nil
-  "*String for filling to insert at front of new line, or nil for none.
-Setting this variable automatically makes it local to the current buffer."
+  "*String for filling to insert at front of new line, or nil for none."
   :type '(choice (const :tag "None" nil)
                 string)
   :group 'fill)
@@ -2861,15 +2860,18 @@ Setting this variable automatically makes it local to the current buffer.")
          (save-excursion (unjustify-current-line)))
 
       ;; Choose a fill-prefix automatically.
-      (if (and adaptive-fill-mode
-              (or (null fill-prefix) (string= fill-prefix "")))
-         (let ((prefix
-                (fill-context-prefix
-                 (save-excursion (backward-paragraph 1) (point))
-                 (save-excursion (forward-paragraph 1) (point)))))
-           (and prefix (not (equal prefix ""))
-                (setq fill-prefix prefix))))
-
+      (when (and adaptive-fill-mode
+                (or (null fill-prefix) (string= fill-prefix "")))
+       (let ((prefix
+              (fill-context-prefix
+               (save-excursion (backward-paragraph 1) (point))
+               (save-excursion (forward-paragraph 1) (point)))))
+         (and prefix (not (equal prefix ""))
+              ;; Use auto-indentation rather than a guessed empty prefix.
+              (not (and fill-indent-according-to-mode
+                        (string-match "[ \t]*" prefix)))
+              (setq fill-prefix prefix))))
+      
       (while (and (not give-up) (> (current-column) fc))
        ;; Determine where to split the line.
        (let* (after-prefix
@@ -2891,20 +2893,9 @@ Setting this variable automatically makes it local to the current buffer.")
                    ;; a character, or \c| following a character.  If
                    ;; not found, place the point at beginning of line.
                    (while (or first
-                              ;; If this is after period and a single space,
-                              ;; move back once more--we don't want to break
-                              ;; the line there and make it look like a
-                              ;; sentence end.
                               (and (not (bobp))
                                    (not bounce)
-                                   sentence-end-double-space
-                                   (save-excursion (forward-char -1)
-                                                   (and (looking-at "\\. ")
-                                                        (not (looking-at "\\.  ")))))
-                              (and (not (bobp))
-                                   (not bounce)
-                                   fill-nobreak-predicate
-                                   (funcall fill-nobreak-predicate)))
+                                   (fill-nobreak-p)))
                      (setq first nil)
                      (re-search-backward "[ \t]\\|\\c|.\\|.\\c|\\|^")
                      ;; If we find nowhere on the line to break it,
@@ -2967,8 +2958,8 @@ Setting this variable automatically makes it local to the current buffer.")
                ;; Now do justification, if required
                (if (not (eq justify 'left))
                    (save-excursion
-                     (end-of-line 0)
-                     (justify-current-line justify nil t)))
+                   (end-of-line 0)
+                   (justify-current-line justify nil t)))
                ;; If making the new line didn't reduce the hpos of
                ;; the end of the line, then give up now;
                ;; trying again will not help.
@@ -3104,8 +3095,9 @@ With arg, turn Line Number mode on iff arg is positive.
 When Line Number mode is enabled, the line number appears
 in the mode line.
 
-Line numbers do not appear for very large buffers, see variable
-`line-number-display-limit'."
+Line numbers do not appear for very large buffers and buffers
+with very long lines; see variables `line-number-display-limit'
+and `line-number-display-limit-width'."
   (interactive "P")
   (setq line-number-mode
        (if (null arg) (not line-number-mode)
@@ -3379,9 +3371,9 @@ The properties used on SYMBOL are `composefunc', `sendfunc',
 (defun rfc822-goto-eoh ()
   ;; Go to header delimiter line in a mail message, following RFC822 rules
   (goto-char (point-min))
-  (while (looking-at "^[^: \n]+:\\|^[ \t]")
-    (forward-line 1))
-  (point))
+  (when (re-search-forward
+        "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
+    (goto-char (match-beginning 0))))
 
 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
                                              switch-function yank-action
@@ -3502,7 +3494,7 @@ in the definition is used to check that VALUE is valid."
   (let ((type (get var 'custom-type)))
     (when type
       ;; Match with custom type.
-      (require 'wid-edit)
+      (require 'cus-edit)
       (setq type (widget-convert type))
       (unless (widget-apply type :match val)
        (error "Value `%S' does not match type %S of %S"
@@ -3674,7 +3666,10 @@ With prefix argument N, move N items (negative N means move backward)."
           ;; is a directory, don't exit the minibuffer.
           (if (and (eq minibuffer-completion-table 'read-file-name-internal)
                    (file-directory-p (field-string (point-max))))
-              (select-window (active-minibuffer-window))
+              (let ((mini (active-minibuffer-window)))
+                (select-window mini)
+                (when minibuffer-auto-raise
+                  (raise-frame (window-frame mini))))
             (exit-minibuffer))))))
 
 (defun completion-list-mode ()
@@ -3692,6 +3687,14 @@ Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
   (setq completion-base-size nil)
   (run-hooks 'completion-list-mode-hook))
 
+(defun completion-list-mode-finish ()
+  "Finish setup of the completions buffer.
+Called from `temp-buffer-show-hook'."
+  (when (eq major-mode 'completion-list-mode)
+    (toggle-read-only 1)))
+
+(add-hook 'temp-buffer-show-hook 'completion-list-mode-finish)
+
 (defvar completion-setup-hook nil
   "Normal hook run at the end of setting up a completion list buffer.
 When this hook is run, the current buffer is the one in which the
@@ -3829,7 +3832,7 @@ PREFIX is the string that represents this modifier in an event type symbol."
 ;;; bindings.
 
 ;; Also tell read-char how to handle these keys.
-(mapcar
+(mapc
  (lambda (keypad-normal)
    (let ((keypad (nth 0 keypad-normal))
         (normal (nth 1 keypad-normal)))
@@ -3895,8 +3898,14 @@ NEWNAME is modified by adding or incrementing <N> at the end as necessary.
 If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'.
 This runs the normal hook `clone-buffer-hook' in the new buffer
 after it has been set up properly in other respects."
-  (interactive (list (if current-prefix-arg (read-string "Name: "))
-                    t))
+  (interactive
+   (progn
+     (if buffer-file-name
+        (error "Cannot clone a file-visiting buffer"))
+     (if (get major-mode 'no-clone)
+        (error "Cannot clone a buffer in %s mode" mode-name))
+     (list (if current-prefix-arg (read-string "Name: "))
+          t)))
   (if buffer-file-name
       (error "Cannot clone a file-visiting buffer"))
   (if (get major-mode 'no-clone)
@@ -3960,9 +3969,15 @@ This is always done when called interactively.
 
 Optional last arg NORECORD non-nil means do not put this buffer at the
 front of the list of recently selected ones."
-  (interactive (list (if current-prefix-arg
-                        (read-string "BName of indirect buffer: "))
-                    t))
+  (interactive
+   (progn
+     (if (get major-mode 'no-clone-indirect)
+        (error "Cannot indirectly clone a buffer in %s mode" mode-name))
+     (list (if current-prefix-arg
+              (read-string "BName of indirect buffer: "))
+          t)))
+  (if (get major-mode 'no-clone-indirect)
+      (error "Cannot indirectly clone a buffer in %s mode" mode-name))
   (setq newname (or newname (buffer-name)))
   (if (string-match "<[0-9]+>\\'" newname)
       (setq newname (substring newname 0 (match-beginning 0))))
@@ -4016,16 +4031,16 @@ DESCRIPTION is the descriptive string for the syntax.")
 
 ;;; Handling of Backspace and Delete keys.
 
-(defcustom delete-key-deletes-forward nil
-  "Whether the Delete key should delete forward or not.
+(defcustom normal-erase-is-backspace nil
+  "If non-nil, Delete key deletes forward and Backspace key deletes backward.
 
 On window systems, the default value of this option is chosen
 according to the keyboard used.  If the keyboard has both a Backspace
 key and a Delete key, and both are mapped to their usual meanings, the
 option's default value is set to t, so that Backspace can be used to
-delete backward, and Delete can be used used to delete forward
+delete backward, and Delete can be used to delete forward.
 
-If not running under a window system, setting this option accomplishes
+If not running under a window system, customizing this option accomplishes
 a similar effect by mapping C-h, which is usually generated by the
 Backspace key, to DEL, and by mapping DEL to C-d via
 `keyboard-translate'.  The former functionality of C-h is available on
@@ -4033,39 +4048,65 @@ the F1 key.  You should probably not use this setting if you don't
 have both Backspace, Delete and F1 keys.
 
 Setting this variable with setq doesn't take effect.  Programmatically,
-call `delete-key-deleted-forward-mode' instead."
+call `normal-erase-is-backspace-mode' (which see) instead."
   :type 'boolean
   :group 'editing-basics
   :version "21.1"
   :set (lambda (symbol value)
         ;; The fboundp is because of a problem with :set when
         ;; dumping Emacs.  It doesn't really matter.
-        (if (fboundp 'delete-key-deletes-forward-mode)
-            (delete-key-deletes-forward-mode (or value 0))
+        (if (fboundp 'normal-erase-is-backspace-mode)
+            (normal-erase-is-backspace-mode (or value 0))
           (set-default symbol value))))
 
 
-(defun delete-key-deletes-forward-mode (&optional arg)
-  "Toggle Delete key deleting forward or backward.
-With numeric arg, turn the mode on if and only iff ARG is positive.
-For more details, see `delete-key-deletes-forward'."
+(defun normal-erase-is-backspace-mode (&optional arg)
+  "Toggle the Erase and Delete mode of the Backspace and Delete keys.
+
+With numeric arg, turn the mode on if and only if ARG is positive.
+
+On window systems, when this mode is on, Delete is mapped to C-d and
+Backspace is mapped to DEL; when this mode is off, both Delete and
+Backspace are mapped to DEL.  (The remapping goes via
+`function-key-map', so binding Delete or Backspace in the global or
+local keymap will override that.)
+
+In addition, on window systems, the bindings of C-Delete, M-Delete,
+C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in
+the global keymap in accordance with the functionality of Delete and
+Backspace.  For example, if Delete is remapped to C-d, which deletes
+forward, C-Delete is bound to `kill-word', but if Delete is remapped
+to DEL, which deletes backward, C-Delete is bound to
+`backward-kill-word'.
+
+If not running on a window system, a similar effect is accomplished by
+remapping C-h (normally produced by the Backspace key) and DEL via
+`keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL
+to C-d; if it's off, the keys are not remapped.
+
+When not running on a window system, and this mode is turned on, the
+former functionality of C-h is available on the F1 key.  You should
+probably not turn on this mode on a text-only terminal if you don't
+have both Backspace, Delete and F1 keys.
+
+See also `normal-erase-is-backspace'."
   (interactive "P")
-  (setq delete-key-deletes-forward
+  (setq normal-erase-is-backspace
        (if arg
            (> (prefix-numeric-value arg) 0)
-         (not delete-key-deletes-forward)))
+         (not normal-erase-is-backspace)))
 
   (cond ((or (memq window-system '(x w32 mac pc))
             (memq system-type '(ms-dos windows-nt)))
-        (let ((bindings 
-               `(([C-delete] [C-backspace] kill-word backward-kill-word)
-                 ([M-delete] [M-backspace] kill-word backward-kill-word)
-                 ([C-M-delete] [C-M-backspace] kill-sexp backward-kill-sexp)
+        (let ((bindings
+               `(([C-delete] [C-backspace])
+                 ([M-delete] [M-backspace])
+                 ([C-M-delete] [C-M-backspace])
                  (,esc-map
-                  [C-delete] [C-backspace]
-                  kill-sexp backward-kill-sexp))))
+                  [C-delete] [C-backspace])))
+              (old-state (lookup-key function-key-map [delete])))
 
-          (if delete-key-deletes-forward
+          (if normal-erase-is-backspace
               (progn
                 (define-key function-key-map [delete] [?\C-d])
                 (define-key function-key-map [kp-delete] [?\C-d])
@@ -4074,33 +4115,32 @@ For more details, see `delete-key-deletes-forward'."
             (define-key function-key-map [kp-delete] [?\C-?])
             (define-key function-key-map [backspace] [?\C-?]))
 
-          (dolist (binding bindings)
-            (let ((map global-map))
-              (when (keymapp (car binding))
-                (setq map (car binding) binding (cdr binding)))
-              (let ((key1 (nth 0 binding))
-                    (key2 (nth 1 binding))
-                    (binding1 (nth 2 binding))
-                    (binding2 (nth 3 binding)))
-                (unless delete-key-deletes-forward
-                  (let ((temp binding1))
-                    (setq binding1 binding2 binding2 temp)))
-                (define-key map key1 binding1)
-                (define-key map key2 binding2))))))
+          ;; Maybe swap bindings of C-delete and C-backspace, etc.
+          (unless (equal old-state (lookup-key function-key-map [delete]))
+            (dolist (binding bindings)
+              (let ((map global-map))
+                (when (keymapp (car binding))
+                  (setq map (car binding) binding (cdr binding)))
+                (let* ((key1 (nth 0 binding))
+                       (key2 (nth 1 binding))
+                       (binding1 (lookup-key map key1))
+                       (binding2 (lookup-key map key2)))
+                  (define-key map key1 binding2)
+                  (define-key map key2 binding1)))))))
         (t
-         (if delete-key-deletes-forward
+         (if normal-erase-is-backspace
              (progn
                (keyboard-translate ?\C-h ?\C-?)
                (keyboard-translate ?\C-? ?\C-d))
            (keyboard-translate ?\C-h ?\C-h)
            (keyboard-translate ?\C-? ?\C-?))))
 
-  (run-hooks 'delete-key-deletes-forward-hook)
+  (run-hooks 'normal-erase-is-backspace-hook)
   (if (interactive-p)
-      (message "Delete key deletes %s" 
-              (if delete-key-deletes-forward "forward" "backward"))))
+      (message "Delete key deletes %s"
+              (if normal-erase-is-backspace "forward" "backward"))))
+
 
-  
 ;;; Misc
 
 (defun byte-compiling-files-p ()
@@ -4108,4 +4148,26 @@ For more details, see `delete-key-deletes-forward'."
   (and (boundp 'byte-compile-current-file)
        (stringp byte-compile-current-file)))
 
+
+;; Minibuffer prompt stuff.
+
+;(defun minibuffer-prompt-modification (start end)
+;  (error "You cannot modify the prompt"))
+;
+;
+;(defun minibuffer-prompt-insertion (start end)
+;  (let ((inhibit-modification-hooks t))
+;    (delete-region start end)
+;    ;; Discard undo information for the text insertion itself
+;    ;; and for the text deletion.above.
+;    (when (consp buffer-undo-list)
+;      (setq buffer-undo-list (cddr buffer-undo-list)))
+;    (message "You cannot modify the prompt")))
+;
+;
+;(setq minibuffer-prompt-properties 
+;  (list 'modification-hooks '(minibuffer-prompt-modification)
+;      'insert-in-front-hooks '(minibuffer-prompt-insertion)))
+;  
+
 ;;; simple.el ends here