Merge from gnus--rel--5.10
[bpt/emacs.git] / lisp / simple.el
index c2859fc..c064bbc 100644 (file)
@@ -1,8 +1,7 @@
 ;;; simple.el --- basic editing commands for Emacs
 
 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-;;               2000, 2001, 2002, 2003, 2004, 2005
-;;        Free Software Foundation, Inc.
+;;   2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: internal
@@ -35,6 +34,8 @@
   (autoload 'widget-convert "wid-edit")
   (autoload 'shell-mode "shell"))
 
+(defvar compilation-current-error)
+
 (defcustom idle-update-delay 0.5
   "*Idle time delay before updating various things on the screen.
 Various Emacs features that update auxiliary information when point moves
@@ -51,30 +52,73 @@ wait this many seconds after Emacs becomes idle before doing an update."
   "Highlight (un)matching of parens and expressions."
   :group 'matching)
 
+(defun get-next-valid-buffer (list &optional buffer visible-ok frame)
+  "Search LIST for a valid buffer to display in FRAME.
+Return nil when all buffers in LIST are undesirable for display,
+otherwise return the first suitable buffer in LIST.
+
+Buffers not visible in windows are preferred to visible buffers,
+unless VISIBLE-OK is non-nil.
+If the optional argument FRAME is nil, it defaults to the selected frame.
+If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
+  ;; This logic is more or less copied from other-buffer.
+  (setq frame (or frame (selected-frame)))
+  (let ((pred (frame-parameter frame 'buffer-predicate))
+       found buf)
+    (while (and (not found) list)
+      (setq buf (car list))
+      (if (and (not (eq buffer buf))
+              (buffer-live-p buf)
+              (or (null pred) (funcall pred buf))
+              (not (eq (aref (buffer-name buf) 0) ?\s))
+              (or visible-ok (null (get-buffer-window buf 'visible))))
+         (setq found buf)
+       (setq list (cdr list))))
+    (car list)))
+
+(defun last-buffer (&optional buffer visible-ok frame)
+  "Return the last non-hidden displayable buffer in the buffer list.
+If BUFFER is non-nil, last-buffer will ignore that buffer.
+Buffers not visible in windows are preferred to visible buffers,
+unless optional argument VISIBLE-OK is non-nil.
+If the optional third argument FRAME is non-nil, use that frame's
+buffer list instead of the selected frame's buffer list.
+If no other buffer exists, the buffer `*scratch*' is returned."
+  (setq frame (or frame (selected-frame)))
+  (or (get-next-valid-buffer (frame-parameter frame 'buried-buffer-list)
+                            buffer visible-ok frame)
+      (get-next-valid-buffer (nreverse (buffer-list frame))
+                            buffer visible-ok frame)
+      (progn
+       (set-buffer-major-mode (get-buffer-create "*scratch*"))
+       (get-buffer "*scratch*"))))
+
 (defun next-buffer ()
   "Switch to the next buffer in cyclic order."
   (interactive)
-  (let ((buffer (current-buffer)))
-    (switch-to-buffer (other-buffer buffer))
-    (bury-buffer buffer)))
-
-(defun prev-buffer ()
+  (let ((buffer (current-buffer))
+       (bbl (frame-parameter nil 'buried-buffer-list)))
+    (switch-to-buffer (other-buffer buffer t))
+    (bury-buffer buffer)
+    (set-frame-parameter nil 'buried-buffer-list
+                        (cons buffer (delq buffer bbl)))))
+
+(defun previous-buffer ()
   "Switch to the previous buffer in cyclic order."
   (interactive)
-  (let ((list (nreverse (buffer-list)))
-       found)
-    (while (and (not found) list)
-      (let ((buffer (car list)))
-       (if (and (not (get-buffer-window buffer))
-                (not (string-match "\\` " (buffer-name buffer))))
-           (setq found buffer)))
-      (setq list (cdr list)))
-    (switch-to-buffer found)))
+  (let ((buffer (last-buffer (current-buffer) t))
+       (bbl (frame-parameter nil 'buried-buffer-list)))
+    (switch-to-buffer buffer)
+    ;; Clean up buried-buffer-list up to and including the chosen buffer.
+    (while (and bbl (not (eq (car bbl) buffer)))
+      (setq bbl (cdr bbl)))
+    (set-frame-parameter nil 'buried-buffer-list bbl)))
+
 \f
 ;;; next-error support framework
 
 (defgroup next-error nil
-  "next-error support framework."
+  "`next-error' support framework."
   :group 'compilation
   :version "22.1")
 
@@ -84,29 +128,29 @@ wait this many seconds after Emacs becomes idle before doing an update."
   :group 'next-error
   :version "22.1")
 
-(defcustom next-error-highlight 0.1
+(defcustom next-error-highlight 0.5
   "*Highlighting of locations in selected source buffers.
-If number, highlight the locus in next-error face for given time in seconds.
-If t, use persistent overlays fontified in next-error face.
+If number, highlight the locus in `next-error' face for given time in seconds.
+If t, highlight the locus indefinitely until some other locus replaces it.
 If nil, don't highlight the locus in the source buffer.
 If `fringe-arrow', indicate the locus by the fringe arrow."
-  :type '(choice (number :tag "Delay")
-                 (const :tag "Persistent overlay" t)
+  :type '(choice (number :tag "Highlight for specified time")
+                 (const :tag "Semipermanent highlighting" t)
                  (const :tag "No highlighting" nil)
-                 (const :tag "Fringe arrow" 'fringe-arrow))
+                 (const :tag "Fringe arrow" fringe-arrow))
   :group 'next-error
   :version "22.1")
 
-(defcustom next-error-highlight-no-select 0.1
-  "*Highlighting of locations in non-selected source buffers.
-If number, highlight the locus in next-error face for given time in seconds.
-If t, use persistent overlays fontified in next-error face.
+(defcustom next-error-highlight-no-select 0.5
+  "*Highlighting of locations in `next-error-no-select'.
+If number, highlight the locus in `next-error' face for given time in seconds.
+If t, highlight the locus indefinitely until some other locus replaces it.
 If nil, don't highlight the locus in the source buffer.
 If `fringe-arrow', indicate the locus by the fringe arrow."
-  :type '(choice (number :tag "Delay")
-                 (const :tag "Persistent overlay" t)
+  :type '(choice (number :tag "Highlight for specified time")
+                 (const :tag "Semipermanent highlighting" t)
                  (const :tag "No highlighting" nil)
-                 (const :tag "Fringe arrow" 'fringe-arrow))
+                 (const :tag "Fringe arrow" fringe-arrow))
   :group 'next-error
   :version "22.1")
 
@@ -122,7 +166,7 @@ If `fringe-arrow', indicate the locus by the fringe arrow."
 (add-to-list 'overlay-arrow-variable-list 'next-error-overlay-arrow-position)
 
 (defvar next-error-last-buffer nil
-  "The most recent next-error buffer.
+  "The most recent `next-error' buffer.
 A buffer becomes most recent when its compilation, grep, or
 similar mode is started, or when it is used with \\[next-error]
 or \\[compile-goto-error].")
@@ -143,7 +187,7 @@ to navigate in it.")
                               &optional avoid-current
                               extra-test-inclusive
                               extra-test-exclusive)
-  "Test if BUFFER is a next-error capable buffer.
+  "Test if BUFFER is a `next-error' capable buffer.
 
 If AVOID-CURRENT is non-nil, treat the current buffer
 as an absolute last resort only.
@@ -152,7 +196,7 @@ The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
 that normally would not qualify.  If it returns t, the buffer
 in question is treated as usable.
 
-The function EXTRA-TEST-EXCLUSIVE, if non-nil is called in each buffer
+The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
 that would normally be considered usable.  If it returns nil,
 that buffer is rejected."
   (and (buffer-name buffer)            ;First make sure it's live.
@@ -170,7 +214,8 @@ that buffer is rejected."
 (defun next-error-find-buffer (&optional avoid-current
                                         extra-test-inclusive
                                         extra-test-exclusive)
-  "Return a next-error capable buffer.
+  "Return a `next-error' capable buffer.
+
 If AVOID-CURRENT is non-nil, treat the current buffer
 as an absolute last resort only.
 
@@ -178,7 +223,7 @@ The function EXTRA-TEST-INCLUSIVE, if non-nil, is called in each buffer
 that normally would not qualify.  If it returns t, the buffer
 in question is treated as usable.
 
-The function EXTRA-TEST-EXCLUSIVE, if non-nil is called in each buffer
+The function EXTRA-TEST-EXCLUSIVE, if non-nil, is called in each buffer
 that would normally be considered usable.  If it returns nil,
 that buffer is rejected."
   (or
@@ -223,7 +268,7 @@ that buffer is rejected."
    (error "No next-error capable buffer found")))
 
 (defun next-error (&optional arg reset)
-  "Visit next next-error message and corresponding source code.
+  "Visit next `next-error' message and corresponding source code.
 
 If all the error messages parsed so far have been processed already,
 the message buffer is checked for new ones.
@@ -260,11 +305,19 @@ See variables `compilation-parse-errors-function' and
       (funcall next-error-function (prefix-numeric-value arg) reset)
       (run-hooks 'next-error-hook))))
 
+(defun next-error-internal ()
+  "Visit the source code corresponding to the `next-error' message at point."
+  (setq next-error-last-buffer (current-buffer))
+  ;; we know here that next-error-function is a valid symbol we can funcall
+  (with-current-buffer next-error-last-buffer
+    (funcall next-error-function 0 nil)
+    (run-hooks 'next-error-hook)))
+
 (defalias 'goto-next-locus 'next-error)
 (defalias 'next-match 'next-error)
 
 (defun previous-error (&optional n)
-  "Visit previous next-error message and corresponding source code.
+  "Visit previous `next-error' message and corresponding source code.
 
 Prefix arg N says how many error messages to move backwards (or
 forwards, if negative).
@@ -282,7 +335,7 @@ This operates on the output from the \\[compile] command, for instance."
   (next-error n t))
 
 (defun next-error-no-select (&optional n)
-  "Move point to the next error in the next-error buffer and highlight match.
+  "Move point to the next error in the `next-error' buffer and highlight match.
 Prefix arg N says how many error messages to move forwards (or
 backwards, if negative).
 Finds and highlights the source line like \\[next-error], but does not
@@ -293,7 +346,7 @@ select the source buffer."
   (pop-to-buffer next-error-last-buffer))
 
 (defun previous-error-no-select (&optional n)
-  "Move point to the previous error in the next-error buffer and highlight match.
+  "Move point to the previous error in the `next-error' buffer and highlight match.
 Prefix arg N says how many error messages to move backwards (or
 forwards, if negative).
 Finds and highlights the source line like \\[previous-error], but does not
@@ -309,11 +362,11 @@ select the source buffer."
 When turned on, cursor motion in the compilation, grep, occur or diff
 buffer causes automatic display of the corresponding source code
 location."
-  :group 'next-error :init-value " Fol"
+  :group 'next-error :init-value nil :lighter " Fol"
   (if (not next-error-follow-minor-mode)
       (remove-hook 'post-command-hook 'next-error-follow-mode-post-command-hook t)
     (add-hook 'post-command-hook 'next-error-follow-mode-post-command-hook nil t)
-    (make-variable-buffer-local 'next-error-follow-last-line)))
+    (make-local-variable 'next-error-follow-last-line)))
 
 ;;; Used as a `post-command-hook' by `next-error-follow-mode'
 ;;; for the *Compilation* *grep* and *Occur* buffers.
@@ -339,6 +392,8 @@ Other major modes are defined by comparison with this one."
 
 ;; Making and deleting lines.
 
+(defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard)))
+
 (defun newline (&optional arg)
   "Insert a newline, and move to left margin of the new line if it's blank.
 If `use-hard-newlines' is non-nil, the newline is marked with the
@@ -426,8 +481,8 @@ than the value of `fill-column' and ARG is nil."
 
 (defun open-line (n)
   "Insert a newline and leave point before it.
-If there is a fill prefix and/or a left-margin, insert them on the new line
-if the line would have been blank.
+If there is a fill prefix and/or a `left-margin', insert them
+on the new line if the line would have been blank.
 With arg N, insert N newlines."
   (interactive "*p")
   (let* ((do-fill-prefix (and fill-prefix (bolp)))
@@ -449,7 +504,7 @@ With arg N, insert N newlines."
 (defun split-line (&optional arg)
   "Split current line, moving portion beyond point vertically down.
 If the current line starts with `fill-prefix', insert it on the new
-line as well.  With prefix ARG, don't insert fill-prefix on new line.
+line as well.  With prefix ARG, don't insert `fill-prefix' on new line.
 
 When called from Lisp code, ARG may be a prefix string to copy."
   (interactive "*P")
@@ -598,7 +653,7 @@ In binary overwrite mode, this function does overwrite, and octal
 digits are interpreted as a character code.  This is intended to be
 useful for editing binary files."
   (interactive "*p")
-  (let* ((char (let (translation-table-for-input)
+  (let* ((char (let (translation-table-for-input input-method-function)
                 (if (or (not overwrite-mode)
                         (eq overwrite-mode 'overwrite-mode-binary))
                     (read-quoted-char)
@@ -647,7 +702,7 @@ Leave one space or none, according to the context."
            (save-excursion (forward-char -1)
                            (looking-at "$\\|\\s(\\|\\s'")))
        nil
-      (insert ?\ ))))
+      (insert ?\s))))
 
 (defun delete-horizontal-space (&optional backward-only)
   "Delete all spaces and tabs around point.
@@ -671,9 +726,9 @@ If BACKWARD-ONLY is non-nil, only delete spaces before point."
     (skip-chars-backward " \t")
     (constrain-to-field nil orig-pos)
     (dotimes (i (or n 1))
-      (if (= (following-char) ?\ )
+      (if (= (following-char) ?\s)
          (forward-char 1)
-       (insert ?\ )))
+       (insert ?\s)))
     (delete-region
      (point)
      (progn
@@ -844,7 +899,9 @@ and the greater of them is not at the start of a line."
 
 (defun line-number-at-pos (&optional pos)
   "Return (narrowed) buffer line number at position POS.
-If POS is nil, use current buffer location."
+If POS is nil, use current buffer location.
+Counting starts at (point-min), so the value refers
+to the contents of the accessible portion of the buffer."
   (let ((opoint (or pos (point))) start)
     (save-excursion
       (goto-char (point-min))
@@ -882,48 +939,69 @@ in *Help* buffer.  See also the command `describe-char'."
         (col (current-column)))
     (if (= pos end)
        (if (or (/= beg 1) (/= end (1+ total)))
-           (message "point=%d of %d (%d%%) <%d - %d> column %d %s"
+           (message "point=%d of %d (%d%%) <%d-%d> column=%d%s"
                     pos total percent beg end col hscroll)
-         (message "point=%d of %d (%d%%) column %d %s"
-                  pos total percent col hscroll))
+         (message "point=%d of %d (EOB) column=%d%s"
+                  pos total col hscroll))
       (let ((coding buffer-file-coding-system)
-           encoded encoding-msg)
+           encoded encoding-msg display-prop under-display)
        (if (or (not coding)
                (eq (coding-system-type coding) t))
            (setq coding default-buffer-file-coding-system))
        (if (not (char-valid-p char))
            (setq encoding-msg
-                 (format "(0%o, %d, 0x%x, invalid)" char char char))
-         (setq encoded (and (>= char 128) (encode-coding-char char coding)))
+                 (format "(%d, #o%o, #x%x, invalid)" char char char))
+         ;; Check if the character is displayed with some `display'
+         ;; text property.  In that case, set under-display to the
+         ;; buffer substring covered by that property.
+         (setq display-prop (get-text-property pos 'display))
+         (if display-prop
+             (let ((to (or (next-single-property-change pos 'display)
+                           (point-max))))
+               (if (< to (+ pos 4))
+                   (setq under-display "")
+                 (setq under-display "..."
+                       to (+ pos 4)))
+               (setq under-display
+                     (concat (buffer-substring-no-properties pos to)
+                             under-display)))
+           (setq encoded (and (>= char 128) (encode-coding-char char coding))))
          (setq encoding-msg
-               (if encoded
-                   (format "(0%o, %d, 0x%x, file %s)"
-                           char char char
-                           (if (> (length encoded) 1)
-                               "..."
-                             (encoded-string-description encoded coding)))
-                 (format "(0%o, %d, 0x%x)" char char char))))
+               (if display-prop
+                   (if (not (stringp display-prop))
+                       (format "(%d, #o%o, #x%x, part of display \"%s\")"
+                               char char char under-display)
+                     (format "(%d, #o%o, #x%x, part of display \"%s\"->\"%s\")"
+                             char char char under-display display-prop))
+                 (if encoded
+                     (format "(%d, #o%o, #x%x, file %s)"
+                             char char char
+                             (if (> (length encoded) 1)
+                                 "..."
+                               (encoded-string-description encoded coding)))
+                   (format "(%d, #o%o, #x%x)" char char char)))))
        (if detail
            ;; We show the detailed information about CHAR.
            (describe-char (point)))
        (if (or (/= beg 1) (/= end (1+ total)))
-           (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s"
+           (message "Char: %s %s point=%d of %d (%d%%) <%d-%d> column=%d%s"
                     (if (< char 256)
                         (single-key-description char)
                       (buffer-substring-no-properties (point) (1+ (point))))
                     encoding-msg pos total percent beg end col hscroll)
-         (message "Char: %s %s point=%d of %d (%d%%) column %d %s"
-                  (if (< char 256)
-                      (single-key-description char)
-                    (buffer-substring-no-properties (point) (1+ (point))))
+         (message "Char: %s %s point=%d of %d (%d%%) column=%d%s"
+                  (if enable-multibyte-characters
+                      (if (< char 128)
+                          (single-key-description char)
+                        (buffer-substring-no-properties (point) (1+ (point))))
+                    (single-key-description char))
                   encoding-msg pos total percent col hscroll))))))
 \f
-(defvar read-expression-map
-  (let ((m (make-sparse-keymap)))
-    (define-key m "\M-\t" 'lisp-complete-symbol)
-    (set-keymap-parent m minibuffer-local-map)
-    m)
-  "Minibuffer keymap used for reading Lisp expressions.")
+;; Initialize read-expression-map.  It is defined at C level.
+(let ((m (make-sparse-keymap)))
+  (define-key m "\M-\t" 'lisp-complete-symbol)
+  (set-keymap-parent m minibuffer-local-map)
+  (setq read-expression-map m))
 
 (defvar read-expression-history nil)
 
@@ -962,18 +1040,21 @@ display the result of expression evaluation."
                      (memq this-command '(eval-last-sexp eval-print-last-sexp)))
                  (prin1-char value))))
         (if char-string
-            (format " (0%o, 0x%x) = %s" value value char-string)
-          (format " (0%o, 0x%x)" value value)))))
+            (format " (#o%o, #x%x, %s)" value value char-string)
+          (format " (#o%o, #x%x)" value value)))))
 
 ;; We define this, rather than making `eval' interactive,
-;; for the sake of completion of names like eval-region, eval-current-buffer.
+;; for the sake of completion of names like eval-region, eval-buffer.
 (defun eval-expression (eval-expression-arg
                        &optional eval-expression-insert-value)
   "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."
+the echo area.
+
+If `eval-expression-debug-on-error' is non-nil, which is the default,
+this command arranges for all errors to enter the debugger."
   (interactive
    (list (read-from-minibuffer "Eval: "
                               nil read-expression-map t
@@ -1073,10 +1154,10 @@ except when an alternate history list is specified.")
   "Control whether history list elements are expressions or strings.
 If the value of this variable equals current minibuffer depth,
 they are expressions; otherwise they are strings.
-\(That convention is designed to do the right thing fora
+\(That convention is designed to do the right thing for
 recursive uses of the minibuffer.)")
 (setq minibuffer-history-variable 'minibuffer-history)
-(setq minibuffer-history-position nil)
+(setq minibuffer-history-position nil)  ;; Defvar is in C code.
 (defvar minibuffer-history-search-history nil)
 
 (defvar minibuffer-text-before-history nil
@@ -1201,7 +1282,8 @@ makes the search case-sensitive."
 (defvar minibuffer-temporary-goal-position nil)
 
 (defun next-history-element (n)
-  "Insert the next element of the minibuffer history into the minibuffer."
+  "Puts next element of the minibuffer history in the minibuffer.
+With argument N, it uses the Nth following element."
   (interactive "p")
   (or (zerop n)
       (let ((narg (- minibuffer-history-position n))
@@ -1244,7 +1326,8 @@ makes the search case-sensitive."
        (goto-char (or minibuffer-temporary-goal-position (point-max))))))
 
 (defun previous-history-element (n)
-  "Inserts the previous element of the minibuffer history into the minibuffer."
+  "Puts previous element of the minibuffer history in the minibuffer.
+With argument N, it uses the Nth previous element."
   (interactive "p")
   (next-history-element (- n)))
 
@@ -1274,7 +1357,7 @@ by the new completion."
 ;; 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 0 if current buffer is not a minibuffer."
   ;; Return the width of everything before the field at the end of
   ;; the buffer; this should be 0 for normal buffers.
   (1- (minibuffer-prompt-end)))
@@ -1313,7 +1396,8 @@ as an argument limits undo to changes within the current region."
   ;; and will get another error.  To begin undoing the undos,
   ;; you must type some other command.
   (let ((modified (buffer-modified-p))
-       (recent-save (recent-auto-save-p)))
+       (recent-save (recent-auto-save-p))
+       message)
     ;; If we get an error in undo-start,
     ;; the next command should not be a "consecutive undo".
     ;; So set `this-command' to something other than `undo'.
@@ -1342,9 +1426,9 @@ as an argument limits undo to changes within the current region."
     ;; so, ask the user whether she wants to skip the redo/undo pair.
     (let ((equiv (gethash pending-undo-list undo-equiv-table)))
       (or (eq (selected-window) (minibuffer-window))
-         (message (if undo-in-region
-                      (if equiv "Redo in region!" "Undo in region!")
-                    (if equiv "Redo!" "Undo!"))))
+         (setq message (if undo-in-region
+                           (if equiv "Redo in region!" "Undo in region!")
+                         (if equiv "Redo!" "Undo!"))))
       (when (and (consp equiv) undo-no-redo)
        ;; The equiv entry might point to another redo record if we have done
        ;; undo-redo-undo-redo-... so skip to the very last equiv.
@@ -1386,7 +1470,10 @@ as an argument limits undo to changes within the current region."
     ;; Record what the current undo list says,
     ;; so the next command can tell if the buffer was modified in between.
     (and modified (not (buffer-modified-p))
-        (delete-auto-save-file-if-necessary recent-save))))
+        (delete-auto-save-file-if-necessary recent-save))
+    ;; Display a message announcing success.
+    (if message
+       (message message))))
 
 (defun buffer-disable-undo (&optional buffer)
   "Make BUFFER stop keeping undo information.
@@ -1413,8 +1500,7 @@ Call `undo-start' to get ready to undo recent changes,
 then call `undo-more' one or more times to undo them."
   (or (listp pending-undo-list)
       (error (concat "No further undo information"
-                     (and transient-mark-mode mark-active
-                          " for region"))))
+                    (and undo-in-region " for region"))))
   (let ((undo-in-progress t))
     (setq pending-undo-list (primitive-undo n pending-undo-list))
     (if (null pending-undo-list)
@@ -1561,12 +1647,12 @@ is not *inside* the region START...END."
        ((null (car undo-elt))
         ;; (nil PROPERTY VALUE BEG . END)
         (let ((tail (nthcdr 3 undo-elt)))
-          (not (or (< (car tail) end)
-                   (> (cdr tail) start)))))
+          (and (< (car tail) end)
+               (> (cdr tail) start))))
        ((integerp (car undo-elt))
         ;; (BEGIN . END)
-        (not (or (< (car undo-elt) end)
-                 (> (cdr undo-elt) start))))))
+        (and (< (car undo-elt) end)
+             (> (cdr undo-elt) start)))))
 
 ;; Return the first affected buffer position and the delta for an undo element
 ;; delta is defined as the change in subsequent buffer positions if we *did*
@@ -1583,12 +1669,12 @@ is not *inside* the region START...END."
             '(0 . 0)))
     '(0 . 0)))
 
-(defcustom undo-ask-before-discard t
+(defcustom undo-ask-before-discard nil
   "If non-nil ask about discarding undo info for the current command.
 Normally, Emacs discards the undo info for the current command if
 it exceeds `undo-outer-limit'.  But if you set this option
 non-nil, it asks in the echo area whether to discard the info.
-If you answer no, there a slight risk that Emacs might crash, so
+If you answer no, there is a slight risk that Emacs might crash, so
 only do it if you really want to undo the command.
 
 This option is mainly intended for debugging.  You have to be
@@ -1625,7 +1711,7 @@ This variable only matters if `undo-ask-before-discard' is non-nil.")
        ;; but we don't want to ask the question again.
        (setq undo-extra-outer-limit (+ size 50000))
        (if (let (use-dialog-box track-mouse executing-kbd-macro )
-             (yes-or-no-p (format "Buffer %s undo info is %d bytes long; discard it? "
+             (yes-or-no-p (format "Buffer `%s' undo info is %d bytes long; discard it? "
                                   (buffer-name) size)))
            (progn (setq buffer-undo-list nil)
                   (setq undo-extra-outer-limit nil)
@@ -1633,7 +1719,7 @@ This variable only matters if `undo-ask-before-discard' is non-nil.")
          nil))
     (display-warning '(undo discard-info)
                     (concat
-                     (format "Buffer %s undo info was %d bytes long.\n"
+                     (format "Buffer `%s' undo info was %d bytes long.\n"
                              (buffer-name) size)
                      "The undo info was discarded because it exceeded \
 `undo-outer-limit'.
@@ -1820,11 +1906,14 @@ the contents are inserted into the buffer anyway.
 
 Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer',
 and only used if a buffer is displayed."
-  (cond ((and (stringp message) (not (string-match "\n" message)))
+  (cond ((and (stringp message)
+             (not (string-match "\n" message))
+             (<= (length message) (frame-width)))
         ;; Trivial case where we can use the echo area
         (message "%s" message))
        ((and (stringp message)
-             (= (string-match "\n" message) (1- (length message))))
+             (= (string-match "\n" message) (1- (length message)))
+             (<= (1- (length message)) (frame-width)))
         ;; Trivial case where we can just remove single trailing newline
         (message "%s" (substring message 0 (1- (length message)))))
        (t
@@ -1841,7 +1930,7 @@ and only used if a buffer is displayed."
           (let ((lines
                  (if (= (buffer-size) 0)
                      0
-                   (count-lines (point-min) (point-max)))))
+                   (count-screen-lines nil nil nil (minibuffer-window)))))
             (cond ((= lines 0))
                   ((and (or (<= lines 1)
                             (<= lines
@@ -2245,7 +2334,7 @@ return value of `filter-buffer-substring'.
 
 If this variable is nil, no filtering is performed.")
 
-(defun filter-buffer-substring (beg end &optional delete)
+(defun filter-buffer-substring (beg end &optional delete noprops)
   "Return the buffer substring between BEG and END, after filtering.
 The buffer substring is passed through each of the filter
 functions in `buffer-substring-filters', and the value from the
@@ -2255,21 +2344,36 @@ is nil, the buffer substring is returned unaltered.
 If DELETE is non-nil, the text between BEG and END is deleted
 from the buffer.
 
+If NOPROPS is non-nil, final string returned does not include
+text properties, while the string passed to the filters still
+includes text properties from the buffer text.
+
 Point is temporarily set to BEG before calling
 `buffer-substring-filters', in case the functions need to know
 where the text came from.
 
-This function should be used instead of `buffer-substring' or
-`delete-and-extract-region' when you want to allow filtering to
-take place.  For example, major or minor modes can use
-`buffer-substring-filters' to extract characters that are special
-to a buffer, and should not be copied into other buffers."
-  (save-excursion
-    (goto-char beg)
-    (let ((string (if delete (delete-and-extract-region beg end)
-                    (buffer-substring beg end))))
-      (dolist (filter buffer-substring-filters string)
-        (setq string (funcall filter string))))))
+This function should be used instead of `buffer-substring',
+`buffer-substring-no-properties', or `delete-and-extract-region'
+when you want to allow filtering to take place.  For example,
+major or minor modes can use `buffer-substring-filters' to
+extract characters that are special to a buffer, and should not
+be copied into other buffers."
+  (cond
+   ((or delete buffer-substring-filters)
+    (save-excursion
+      (goto-char beg)
+      (let ((string (if delete (delete-and-extract-region beg end)
+                     (buffer-substring beg end))))
+       (dolist (filter buffer-substring-filters)
+         (setq string (funcall filter string)))
+       (if noprops
+           (set-text-properties 0 (length string) nil string))
+       string)))
+   (noprops
+    (buffer-substring-no-properties beg end))
+   (t
+    (buffer-substring beg end))))
+
 
 ;;;; Window system cut and paste hooks.
 
@@ -2345,7 +2449,7 @@ handler, if non-nil, is stored as a `yank-handler' text property on STRING).
 
 When the yank handler has a non-nil PARAM element, the original STRING
 argument is not used by `insert-for-yank'.  However, since Lisp code
-may access and use elements from the kill-ring directly, the STRING
+may access and use elements from the kill ring directly, the STRING
 argument should still be a \"useful\" string for such uses."
   (if (> (length string) 0)
       (if yank-handler
@@ -2358,7 +2462,7 @@ argument should still be a \"useful\" string for such uses."
       (menu-bar-update-yank-menu string (and replace (car kill-ring))))
   (if (and replace kill-ring)
       (setcar kill-ring string)
-    (setq kill-ring (cons string kill-ring))
+    (push string kill-ring)
     (if (> (length kill-ring) kill-ring-max)
        (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
   (setq kill-ring-yank-pointer kill-ring)
@@ -2422,8 +2526,8 @@ yanking point; just return the Nth kill forward."
 (put 'text-read-only 'error-message "Text is read-only")
 
 (defun kill-region (beg end &optional yank-handler)
-  "Kill between point and mark.
-The text is deleted but saved in the kill ring.
+  "Kill (\"cut\") text between point and mark.
+This deletes the text from the buffer and saves it in the kill ring.
 The command \\[yank] can retrieve it from there.
 \(If you want to kill and then yank immediately, use \\[kill-ring-save].)
 
@@ -2445,7 +2549,11 @@ to make one entry in the kill ring.
 In Lisp code, optional third arg YANK-HANDLER, if non-nil,
 specifies the yank-handler text property to be set on the killed
 text.  See `insert-for-yank'."
-  (interactive "r")
+  ;; Pass point first, then mark, because the order matters
+  ;; when calling kill-append.
+  (interactive (list (point) (mark)))
+  (unless (and beg end)
+    (error "The mark is not set now, so there is no region"))
   (condition-case nil
       (let ((string (filter-buffer-substring beg end t)))
        (when string                    ;STRING is nil if BEG = END
@@ -2549,7 +2657,7 @@ The argument is used for internal purposes; do not supply one."
 ;; This is actually used in subr.el but defcustom does not work there.
 (defcustom yank-excluded-properties
   '(read-only invisible intangible field mouse-face help-echo local-map keymap
-    yank-handler follow-link)
+    yank-handler follow-link fontified)
   "*Text properties to discard when yanking.
 The value should be a list of text properties to discard or t,
 which means to discard all text properties."
@@ -2606,7 +2714,7 @@ doc string for `insert-for-yank-1', which see."
   nil)
 
 (defun yank (&optional arg)
-  "Reinsert the last stretch of killed text.
+  "Reinsert (\"paste\") the last stretch of killed text.
 More precisely, reinsert the stretch of killed text most recently
 killed OR yanked.  Put point at end, and set mark at beginning.
 With just \\[universal-argument] as argument, same but put point at beginning (and mark at end).
@@ -2617,7 +2725,7 @@ When this command inserts killed text into the buffer, it honors
 `yank-excluded-properties' and `yank-handler' as described in the
 doc string for `insert-for-yank-1', which see.
 
-See also the command \\[yank-pop]."
+See also the command `yank-pop' (\\[yank-pop])."
   (interactive "*P")
   (setq yank-window-start (window-start))
   ;; If we don't get all the way thru, make last-command indicate that
@@ -2684,7 +2792,7 @@ and KILLP is t if a prefix arg was specified."
              (let ((col (current-column)))
                (forward-char -1)
                (setq col (- col (current-column)))
-               (insert-char ?\  col)
+               (insert-char ?\s col)
                (delete-char 1)))
          (forward-char -1)
          (setq count (1- count))))))
@@ -2704,6 +2812,8 @@ and KILLP is t if a prefix arg was specified."
 Case is ignored if `case-fold-search' is non-nil in the current buffer.
 Goes backward if ARG is negative; error if CHAR not found."
   (interactive "p\ncZap to char: ")
+  (if (char-table-p translation-table-for-input)
+      (setq char (or (aref translation-table-for-input char) char)))
   (kill-region (point) (progn
                         (search-forward (char-to-string char) nil nil arg)
 ;                       (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
@@ -2968,8 +3078,7 @@ BUFFER (or buffer name), START and END.
 START and END specify the portion of the current buffer to be copied."
   (interactive "BCopy to buffer: \nr")
   (let ((oldbuf (current-buffer)))
-    (save-excursion
-      (set-buffer (get-buffer-create buffer))
+    (with-current-buffer (get-buffer-create buffer)
       (barf-if-buffer-read-only)
       (erase-buffer)
       (save-excursion
@@ -2987,10 +3096,12 @@ it is possible that the region may have changed")
   "Hook run when the mark becomes inactive.")
 
 (defun mark (&optional force)
-  "Return this buffer's mark value as integer; error if mark inactive.
-If optional argument FORCE is non-nil, access the mark value
-even if the mark is not currently active, and return nil
-if there is no mark at all.
+  "Return this buffer's mark value as integer, or nil if never set.
+
+In Transient Mark mode, this function signals an error if
+the mark is not active.  However, if `mark-even-if-inactive' is non-nil,
+or the argument FORCE is non-nil, it disregards whether the mark
+is active, and returns an integer or nil in the usual way.
 
 If you are using this in an editing command, you are most likely making
 a mistake; see the documentation of `set-mark'."
@@ -3081,6 +3192,13 @@ Display `Mark set' unless the optional second arg NOMSG is non-nil."
       (unless nomsg
        (message "Mark activated")))))
 
+(defcustom set-mark-command-repeat-pop nil
+  "*Non-nil means that repeating \\[set-mark-command] after popping will pop.
+This means that if you type C-u \\[set-mark-command] \\[set-mark-command]
+will pop twice."
+  :type 'boolean
+  :group 'editing)
+
 (defun set-mark-command (arg)
   "Set mark at where point is, or jump to mark.
 With no prefix argument, set mark, and push old mark position on local
@@ -3094,13 +3212,17 @@ for mark off the local mark ring \(this does not affect the global
 mark ring\).  Use \\[pop-global-mark] to jump to a mark off the global
 mark ring \(see `pop-global-mark'\).
 
-Repeating the \\[set-mark-command] command without the prefix jumps to
-the next position off the local (or global) mark ring.
+If `set-mark-command-repeat-pop' is non-nil, repeating
+the \\[set-mark-command] command with no prefix pops the next position
+off the local (or global) mark ring and jumps there.
 
 With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
 \\[universal-argument] \\[set-mark-command], unconditionally
 set mark where point is.
 
+Setting the mark also sets the \"region\", which is the closest
+equivalent in Emacs to what some editors call the \"selection\".
+
 Novice Emacs Lisp programmers often try to use the mark for the wrong
 purposes.  See the documentation of `set-mark' for more information."
   (interactive "P")
@@ -3113,10 +3235,13 @@ purposes.  See the documentation of `set-mark' for more information."
     (if arg
        (pop-to-mark-command)
       (push-mark-command t)))
-   ((eq last-command 'pop-to-mark-command)
+   ((and set-mark-command-repeat-pop
+        (eq last-command 'pop-to-mark-command))
     (setq this-command 'pop-to-mark-command)
     (pop-to-mark-command))
-   ((and (eq last-command 'pop-global-mark) (not arg))
+   ((and set-mark-command-repeat-pop
+        (eq last-command 'pop-global-mark)
+        (not arg))
     (setq this-command 'pop-global-mark)
     (pop-global-mark))
    (arg
@@ -3214,7 +3339,7 @@ default part of the buffer's text.  Examples of such commands include
 Invoke \\[apropos-documentation] and type \"transient\" or
 \"mark.*active\" at the prompt, to see the documentation of
 commands which are sensitive to the Transient Mark mode."
-  :global t :group 'editing-basics :require nil)
+  :global t :group 'editing-basics)
 
 (defvar widen-automatically t
   "Non-nil means it is ok for commands to call `widen' when they want to.
@@ -3241,8 +3366,8 @@ as a fallback, and won't change the buffer bounds.")
     (or (and (>= position (point-min))
             (<= position (point-max)))
        (if widen-automatically
-           (error "Global mark position is outside accessible part of buffer")
-         (widen)))
+           (widen)
+         (error "Global mark position is outside accessible part of buffer")))
     (goto-char position)
     (switch-to-buffer buffer)))
 \f
@@ -3280,7 +3405,7 @@ and more reliable (no dependence on goal column, etc.)."
          ;; When adding a newline, don't expand an abbrev.
          (let ((abbrev-mode nil))
            (end-of-line)
-           (insert "\n"))
+           (insert (if use-hard-newlines hard-newline "\n")))
        (line-move arg nil nil try-vscroll))
     (if (interactive-p)
        (condition-case nil
@@ -3350,6 +3475,63 @@ Outline mode sets this."
       (or (memq prop buffer-invisibility-spec)
          (assq prop buffer-invisibility-spec)))))
 
+;; Returns non-nil if partial move was done.
+(defun line-move-partial (arg noerror to-end)
+  (if (< arg 0)
+      ;; Move backward (up).
+      ;; If already vscrolled, reduce vscroll
+      (let ((vs (window-vscroll nil t)))
+       (when (> vs (frame-char-height))
+         (set-window-vscroll nil (- vs (frame-char-height)) t)))
+
+    ;; Move forward (down).
+    (let* ((lh (window-line-height -1))
+          (vpos (nth 1 lh))
+          (ypos (nth 2 lh))
+          (rbot (nth 3 lh))
+          ppos py vs)
+      (when (or (null lh)
+               (>= rbot (frame-char-height))
+               (<= ypos (- (frame-char-height))))
+       (unless lh
+         (let ((wend (pos-visible-in-window-p t nil t)))
+           (setq rbot (nth 3 wend)
+                 vpos (nth 5 wend))))
+       (cond
+        ;; If last line of window is fully visible, move forward.
+        ((or (null rbot) (= rbot 0))
+         nil)
+        ;; If cursor is not in the bottom scroll margin, move forward.
+        ((and (> vpos 0)
+              (< (setq py
+                       (or (nth 1 (window-line-height))
+                           (let ((ppos (posn-at-point)))
+                             (cdr (or (posn-actual-col-row ppos)
+                                      (posn-col-row ppos))))))
+                 (min (- (window-text-height) scroll-margin 1) (1- vpos))))
+         nil)
+        ;; When already vscrolled, we vscroll some more if we can,
+        ;; or clear vscroll and move forward at end of tall image.
+        ((> (setq vs (window-vscroll nil t)) 0)
+         (when (> rbot 0)
+           (set-window-vscroll nil (+ vs (min rbot (frame-char-height))) t)))
+        ;; If cursor just entered the bottom scroll margin, move forward,
+        ;; but also vscroll one line so redisplay wont recenter.
+        ((and (> vpos 0)
+              (= py (min (- (window-text-height) scroll-margin 1)
+                         (1- vpos))))
+         (set-window-vscroll nil (frame-char-height) t)
+         (line-move-1 arg noerror to-end)
+         t)
+        ;; If there are lines above the last line, scroll-up one line.
+        ((> vpos 0)
+         (scroll-up 1)
+         t)
+        ;; Finally, start vscroll.
+        (t
+         (set-window-vscroll nil (frame-char-height) t)))))))
+
+
 ;; This is like line-move-1 except that it also performs
 ;; vertical scrolling of tall images if appropriate.
 ;; That is not really a clean thing to do, since it mixes
@@ -3357,37 +3539,14 @@ Outline mode sets this."
 ;; a cleaner solution to the problem of making C-n do something
 ;; useful given a tall image.
 (defun line-move (arg &optional noerror to-end try-vscroll)
-  (if (and auto-window-vscroll try-vscroll
-          ;; But don't vscroll in a keyboard macro.
-          (not defining-kbd-macro)
-          (not executing-kbd-macro))
-      (let ((forward (> arg 0))
-           (part (nth 2 (pos-visible-in-window-p (point) nil t))))
-       (if (and (consp part)
-                (> (if forward (cdr part) (car part)) 0))
-           (set-window-vscroll nil
-                               (if forward
-                                   (+ (window-vscroll nil t)
-                                      (min (cdr part)
-                                           (* (frame-char-height) arg)))
-                                 (max 0
-                                      (- (window-vscroll nil t)
-                                         (min (car part)
-                                              (* (frame-char-height) (- arg))))))
-                               t)
-         (set-window-vscroll nil 0)
-         (when (line-move-1 arg noerror to-end)
-           (when (not forward)
-             ;; Update display before calling pos-visible-in-window-p,
-             ;; because it depends on window-start being up-to-date.
-             (sit-for 0)
-             ;; If the current line is partly hidden at the bottom,
-             ;; scroll it partially up so as to unhide the bottom.
-             (if (and (setq part (nth 2 (pos-visible-in-window-p
-                                         (line-beginning-position) nil t)))
-                      (> (cdr part) 0))
-                 (set-window-vscroll nil (cdr part) t)))
-           t)))
+  (unless (and auto-window-vscroll try-vscroll
+              ;; Only vscroll for single line moves
+              (= (abs arg) 1)
+              ;; But don't vscroll in a keyboard macro.
+              (not defining-kbd-macro)
+              (not executing-kbd-macro)
+              (line-move-partial arg noerror to-end))
+    (set-window-vscroll nil 0 t)
     (line-move-1 arg noerror to-end)))
 
 ;; This is the guts of next-line and previous-line.
@@ -3398,7 +3557,7 @@ Outline mode sets this."
   ;; for intermediate positions.
   (let ((inhibit-point-motion-hooks t)
        (opoint (point))
-       (forward (> arg 0)))
+       (orig-arg arg))
     (unwind-protect
        (progn
          (if (not (memq last-command '(next-line previous-line)))
@@ -3406,7 +3565,7 @@ Outline mode sets this."
                    (if (and track-eol (eolp)
                             ;; Don't count beg of empty line as end of line
                             ;; unless we just did explicit end-of-line.
-                            (or (not (bolp)) (eq last-command 'end-of-line)))
+                            (or (not (bolp)) (eq last-command 'move-end-of-line)))
                        9999
                      (current-column))))
 
@@ -3431,60 +3590,61 @@ Outline mode sets this."
                              'end-of-buffer)
                            nil)))
            ;; Move by arg lines, but ignore invisible ones.
-           (let (done)
+           (let (done line-end)
              (while (and (> arg 0) (not done))
                ;; If the following character is currently invisible,
                ;; skip all characters with that same `invisible' property value.
                (while (and (not (eobp)) (line-move-invisible-p (point)))
                  (goto-char (next-char-property-change (point))))
-               ;; Now move a line.
-               (end-of-line)
+               ;; Move a line.
+               ;; We don't use `end-of-line', since we want to escape
+               ;; from field boundaries ocurring exactly at point.
+               (let ((inhibit-field-text-motion t))
+                 (setq line-end (line-end-position)))
+               (goto-char (constrain-to-field line-end (point) t t))
                ;; If there's no invisibility here, move over the newline.
-               (let ((pos-before (point))
-                     line-done)
-                 (if (eobp)
-                     (if (not noerror)
-                         (signal 'end-of-buffer nil)
-                       (setq done t)))
-                 (when (and (not done)
-                            (not (integerp selective-display))
-                            (not (line-move-invisible-p (point))))
-                   (unless (overlays-in (max (1- pos-before) (point-min))
-                                        (min (1+ (point)) (point-max)))
-                     ;; We avoid vertical-motion when possible
-                     ;; because that has to fontify.
-                     (forward-line 1)
-                     (setq line-done t)))
-                 (and (not done) (not line-done)
-                      ;; Otherwise move a more sophisticated way.
-                      (zerop (vertical-motion 1))
-                      (if (not noerror)
-                          (signal 'end-of-buffer nil)
-                        (setq done t))))
+               (cond
+                ((eobp)
+                 (if (not noerror)
+                     (signal 'end-of-buffer nil)
+                   (setq done t)))
+                ((and (> arg 1)  ;; Use vertical-motion for last move
+                      (not (integerp selective-display))
+                      (not (line-move-invisible-p (point))))
+                 ;; We avoid vertical-motion when possible
+                 ;; because that has to fontify.
+                 (forward-line 1))
+                ;; Otherwise move a more sophisticated way.
+                ((zerop (vertical-motion 1))
+                 (if (not noerror)
+                     (signal 'end-of-buffer nil)
+                   (setq done t))))
                (unless done
                  (setq arg (1- arg))))
              ;; The logic of this is the same as the loop above,
              ;; it just goes in the other direction.
              (while (and (< arg 0) (not done))
-               (beginning-of-line)
-               (let ((pos-before (point))
-                     line-done)
-                 (if (bobp)
-                     (if (not noerror)
-                         (signal 'beginning-of-buffer nil)
-                       (setq done t)))
-                 (when (and (not done)
-                            (not (integerp selective-display))
-                            (not (line-move-invisible-p (1- (point)))))
-                   (unless (overlays-in (max (1- (point)) (point-min))
-                                        (min (1+ pos-before) (point-max)))
-                     (forward-line -1)
-                     (setq line-done t)))
-                 (and (not done) (not line-done)
-                      (zerop (vertical-motion -1))
-                      (if (not noerror)
-                          (signal 'beginning-of-buffer nil)
-                        (setq done t))))
+               ;; For completely consistency with the forward-motion
+               ;; case, we should call beginning-of-line here.
+               ;; However, if point is inside a field and on a
+               ;; continued line, the call to (vertical-motion -1)
+               ;; below won't move us back far enough; then we return
+               ;; to the same column in line-move-finish, and point
+               ;; gets stuck -- cyd
+               (forward-line 0)
+               (cond
+                ((bobp)
+                 (if (not noerror)
+                     (signal 'beginning-of-buffer nil)
+                   (setq done t)))
+                ((and (< arg -1) ;; Use vertical-motion for last move
+                      (not (integerp selective-display))
+                      (not (line-move-invisible-p (1- (point)))))
+                 (forward-line -1))
+                ((zerop (vertical-motion -1))
+                 (if (not noerror)
+                     (signal 'beginning-of-buffer nil)
+                   (setq done t))))
                (unless done
                  (setq arg (1+ arg))
                  (while (and ;; Don't move over previous invis lines
@@ -3501,12 +3661,12 @@ Outline mode sets this."
             ;; at least go to end of line.
             (end-of-line))
            ((< arg 0)
-            ;; If we did not move down as far as desired,
-            ;; at least go to end of line.
+            ;; If we did not move up as far as desired,
+            ;; at least go to beginning of line.
             (beginning-of-line))
            (t
             (line-move-finish (or goal-column temporary-goal-column)
-                              opoint forward))))))
+                              opoint (> orig-arg 0)))))))
 
 (defun line-move-finish (column opoint forward)
   (let ((repeat t))
@@ -3515,26 +3675,44 @@ Outline mode sets this."
       (setq repeat nil)
 
       (let (new
+           (old (point))
            (line-beg (save-excursion (beginning-of-line) (point)))
            (line-end
             ;; Compute the end of the line
             ;; ignoring effectively invisible newlines.
             (save-excursion
-              (end-of-line)
+              ;; Like end-of-line but ignores fields.
+              (skip-chars-forward "^\n")
               (while (and (not (eobp)) (line-move-invisible-p (point)))
                 (goto-char (next-char-property-change (point)))
-                (end-of-line))
+                (skip-chars-forward "^\n"))
               (point))))
 
        ;; Move to the desired column.
        (line-move-to-column column)
+
+       ;; Corner case: suppose we start out in a field boundary in
+       ;; the middle of a continued line.  When we get to
+       ;; line-move-finish, point is at the start of a new *screen*
+       ;; line but the same text line; then line-move-to-column would
+       ;; move us backwards. Test using C-n with point on the "x" in
+       ;;   (insert "a" (propertize "x" 'field t) (make-string 89 ?y))
+       (and forward
+            (< (point) old)
+            (goto-char old))
+
        (setq new (point))
 
        ;; Process intangibility within a line.
-       ;; Move to the chosen destination position from above,
-       ;; with intangibility processing enabled.
-
-       (goto-char (point-min))
+       ;; With inhibit-point-motion-hooks bound to nil, a call to
+       ;; goto-char moves point past intangible text.
+
+       ;; However, inhibit-point-motion-hooks controls both the
+       ;; intangibility and the point-entered/point-left hooks.  The
+       ;; following hack avoids calling the point-* hooks
+       ;; unnecessarily.  Note that we move *forward* past intangible
+       ;; text when the initial and final points are the same.
+       (goto-char new)
        (let ((inhibit-point-motion-hooks nil))
          (goto-char new)
 
@@ -3562,8 +3740,15 @@ Outline mode sets this."
        (goto-char opoint)
        (let ((inhibit-point-motion-hooks nil))
          (goto-char
-          (constrain-to-field new opoint nil t
-                              'inhibit-line-move-field-capture)))
+          ;; Ignore field boundaries if the initial and final
+          ;; positions have the same `field' property, even if the
+          ;; fields are non-contiguous.  This seems to be "nicer"
+          ;; behavior in many situations.
+          (if (eq (get-char-property new 'field)
+                  (get-char-property opoint 'field))
+              new
+            (constrain-to-field new opoint t t
+                                'inhibit-line-move-field-capture))))
 
        ;; If all this moved us to a different line,
        ;; retry everything within that new line.
@@ -3604,15 +3789,13 @@ and `current-column' to be able to ignore invisible text."
            (goto-char (previous-char-property-change (point) line-beg))))))))
 
 (defun move-end-of-line (arg)
-  "Move point to end of current line.
+  "Move point to end of current line as displayed.
+\(If there's an image in the line, this disregards newlines
+which are part of the text that the image rests on.)
+
 With argument ARG not nil or 1, move forward ARG - 1 lines first.
 If point reaches the beginning or end of buffer, it stops there.
-To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
-
-This command does not move point across a field boundary unless doing so
-would move beyond there to a different line; if ARG is nil or 1, and
-point starts at a field boundary, point does not move.  To ignore field
-boundaries bind `inhibit-field-text-motion' to t."
+To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
   (interactive "p")
   (or arg (setq arg 1))
   (let (done)
@@ -3640,24 +3823,46 @@ boundaries bind `inhibit-field-text-motion' to t."
            (setq done t)))))))
 
 (defun move-beginning-of-line (arg)
-  "Move point to beginning of current display line.
+  "Move point to beginning of current line as displayed.
+\(If there's an image in the line, this disregards newlines
+which are part of the text that the image rests on.)
+
 With argument ARG not nil or 1, move forward ARG - 1 lines first.
 If point reaches the beginning or end of buffer, it stops there.
-To ignore intangibility, bind `inhibit-point-motion-hooks' to t.
-
-This command does not move point across a field boundary unless doing so
-would move beyond there to a different line; if ARG is nil or 1, and
-point starts at a field boundary, point does not move.  To ignore field
-boundaries bind `inhibit-field-text-motion' to t."
+To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
   (interactive "p")
   (or arg (setq arg 1))
-  (if (/= arg 1)
-      (line-move (1- arg) t))
-  (beginning-of-line 1)
-  (let ((orig (point)))
-    (vertical-motion 0)
-    (if (/= orig (point))
-       (goto-char (constrain-to-field (point) orig (/= arg 1) t nil)))))
+
+  (let ((orig (point))
+       start first-vis first-vis-field-value)
+
+    ;; Move by lines, if ARG is not 1 (the default).
+    (if (/= arg 1)
+       (line-move (1- arg) t))
+
+    ;; Move to beginning-of-line, ignoring fields and invisibles.
+    (skip-chars-backward "^\n")
+    (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
+      (goto-char (previous-char-property-change (point)))
+      (skip-chars-backward "^\n"))
+    (setq start (point))
+
+    ;; Now find first visible char in the line
+    (while (and (not (eobp)) (line-move-invisible-p (point)))
+      (goto-char (next-char-property-change (point))))
+    (setq first-vis (point))
+
+    ;; See if fields would stop us from reaching FIRST-VIS.
+    (setq first-vis-field-value
+         (constrain-to-field first-vis orig (/= arg 1) t nil))
+
+    (goto-char (if (/= first-vis-field-value first-vis)
+                  ;; If yes, obey them.
+                  first-vis-field-value
+                ;; Otherwise, move to START with attention to fields.
+                ;; (It is possible that fields never matter in this case.)
+                (constrain-to-field (point) orig
+                                    (/= arg 1) t nil)))))
 
 
 ;;; Many people have said they rarely use this feature, and often type
@@ -3677,9 +3882,18 @@ The goal column is stored in the variable `goal-column'."
         (setq goal-column nil)
         (message "No goal column"))
     (setq goal-column (current-column))
-    (message (substitute-command-keys
-             "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
-            goal-column))
+    ;; The older method below can be erroneous if `set-goal-column' is bound
+    ;; to a sequence containing %
+    ;;(message (substitute-command-keys
+    ;;"Goal column %d (use \\[set-goal-column] with an arg to unset it)")
+    ;;goal-column)
+    (message "%s"
+            (concat
+             (format "Goal column %d " goal-column)
+             (substitute-command-keys
+              "(use \\[set-goal-column] with an arg to unset it)")))
+
+    )
   nil)
 \f
 
@@ -3932,6 +4146,7 @@ If optional arg REALLY-WORD is non-nil, it finds just a word."
                 string)
   :group 'fill)
 (make-variable-buffer-local 'fill-prefix)
+;;;###autoload(put 'fill-prefix 'safe-local-variable 'string-or-null-p)
 
 (defcustom auto-fill-inhibit-regexp nil
   "*Regexp to match lines which should not be auto-filled."
@@ -4085,7 +4300,7 @@ Just \\[universal-argument] as argument means to use the current column."
       (setq arg (current-column)))
   (if (not (integerp arg))
       ;; Disallow missing argument; it's probably a typo for C-x C-f.
-      (error "Set-fill-column requires an explicit argument")
+      (error "set-fill-column requires an explicit argument")
     (message "Fill column set to %d (was %d)" arg fill-column)
     (setq fill-column arg)))
 \f
@@ -4113,7 +4328,7 @@ The variable `selective-display' has a separate value for each buffer."
 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
 (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines)
 
-(defun toggle-truncate-lines (arg)
+(defun toggle-truncate-lines (&optional arg)
   "Toggle whether to fold or truncate long lines on the screen.
 With arg, truncate long lines iff arg is positive.
 Note that in side-by-side windows, truncation is always enabled."
@@ -4165,7 +4380,7 @@ with the character typed.
 typing characters do.
 
 Note that binary overwrite mode is not its own minor mode; it is a
-specialization of overwrite-mode, entered by setting the
+specialization of overwrite mode, entered by setting the
 `overwrite-mode' variable to `overwrite-mode-binary'."
   (interactive "P")
   (setq overwrite-mode
@@ -4184,21 +4399,21 @@ in the mode line.
 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'."
-  :init-value t :global t :group 'editing-basics :require nil)
+  :init-value t :global t :group 'mode-line)
 
 (define-minor-mode column-number-mode
   "Toggle Column Number mode.
 With arg, turn Column Number mode on iff arg is positive.
 When Column Number mode is enabled, the column number appears
 in the mode line."
-  :global t :group 'editing-basics :require nil)
+  :global t :group 'mode-line)
 
 (define-minor-mode size-indication-mode
   "Toggle Size Indication mode.
 With arg, turn Size Indication mode on iff arg is positive.  When
 Size Indication mode is enabled, the size of the accessible part
 of the buffer appears in the mode line."
-  :global t :group 'editing-basics :require nil)
+  :global t :group 'mode-line)
 \f
 (defgroup paren-blinking nil
   "Blinking matching of parens and expressions."
@@ -4213,13 +4428,18 @@ of the buffer appears in the mode line."
 (defcustom blink-matching-paren-on-screen t
   "*Non-nil means show matching open-paren when it is on screen.
 If nil, means don't show it (but the open-paren can still be shown
-when it is off screen)."
+when it is off screen).
+
+This variable has no effect if `blink-matching-paren' is nil.
+\(In that case, the open-paren is never shown.)
+It is also ignored if `show-paren-mode' is enabled."
   :type 'boolean
   :group 'paren-blinking)
 
 (defcustom blink-matching-paren-distance (* 25 1024)
-  "*If non-nil, is maximum distance to search for matching open-paren."
-  :type 'integer
+  "*If non-nil, maximum distance to search backwards for matching open-paren.
+If nil, search stops at the beginning of the accessible portion of the buffer."
+  :type '(choice (const nil) integer)
   :group 'paren-blinking)
 
 (defcustom blink-matching-delay 1
@@ -4228,95 +4448,102 @@ when it is off screen)."
   :group 'paren-blinking)
 
 (defcustom blink-matching-paren-dont-ignore-comments nil
-  "*Non-nil means `blink-matching-paren' will not ignore comments."
+  "*nil means `blink-matching-paren' ignores comments.
+More precisely, when looking for the matching parenthesis,
+it skips the contents of comments that end before point."
   :type 'boolean
   :group 'paren-blinking)
 
 (defun blink-matching-open ()
   "Move cursor momentarily to the beginning of the sexp before point."
   (interactive)
-  (and (> (point) (1+ (point-min)))
-       blink-matching-paren
-       ;; Verify an even number of quoting characters precede the close.
-       (= 1 (logand 1 (- (point)
-                        (save-excursion
-                          (forward-char -1)
-                          (skip-syntax-backward "/\\")
-                          (point)))))
-       (let* ((oldpos (point))
-             (blinkpos)
-             (mismatch)
-             matching-paren)
-        (save-excursion
-          (save-restriction
-            (if blink-matching-paren-distance
-                (narrow-to-region (max (point-min)
-                                       (- (point) blink-matching-paren-distance))
-                                  oldpos))
-            (condition-case ()
-                (let ((parse-sexp-ignore-comments
-                       (and parse-sexp-ignore-comments
-                            (not blink-matching-paren-dont-ignore-comments))))
-                  (setq blinkpos (scan-sexps oldpos -1)))
-              (error nil)))
-          (and blinkpos
-                ;; Not syntax '$'.
-               (not (eq (syntax-class (syntax-after blinkpos)) 8))
-               (setq matching-paren
-                     (let ((syntax (syntax-after blinkpos)))
-                       (and (consp syntax)
-                            (eq (syntax-class syntax) 4)
-                            (cdr syntax)))
-                     mismatch
-                     (or (null matching-paren)
-                         (/= (char-after (1- oldpos))
-                             matching-paren))))
-          (if mismatch (setq blinkpos nil))
-          (if blinkpos
-              ;; Don't log messages about paren matching.
-              (let (message-log-max)
-               (goto-char blinkpos)
-               (if (pos-visible-in-window-p)
-                   (and blink-matching-paren-on-screen
-                        (sit-for blink-matching-delay))
-                 (goto-char blinkpos)
-                 (message
-                  "Matches %s"
-                  ;; Show what precedes the open in its line, if anything.
-                  (if (save-excursion
-                        (skip-chars-backward " \t")
-                        (not (bolp)))
-                      (buffer-substring (progn (beginning-of-line) (point))
-                                        (1+ blinkpos))
-                    ;; Show what follows the open in its line, if anything.
-                    (if (save-excursion
-                          (forward-char 1)
-                          (skip-chars-forward " \t")
-                          (not (eolp)))
-                        (buffer-substring blinkpos
-                                          (progn (end-of-line) (point)))
-                      ;; Otherwise show the previous nonblank line,
-                      ;; if there is one.
-                      (if (save-excursion
-                            (skip-chars-backward "\n \t")
-                            (not (bobp)))
-                          (concat
-                           (buffer-substring (progn
+  (when (and (> (point) (point-min))
+            blink-matching-paren
+            ;; Verify an even number of quoting characters precede the close.
+            (= 1 (logand 1 (- (point)
+                              (save-excursion
+                                (forward-char -1)
+                                (skip-syntax-backward "/\\")
+                                (point))))))
+    (let* ((oldpos (point))
+          blinkpos
+          message-log-max  ; Don't log messages about paren matching.
+          matching-paren
+          open-paren-line-string)
+      (save-excursion
+       (save-restriction
+         (if blink-matching-paren-distance
+             (narrow-to-region (max (point-min)
+                                    (- (point) blink-matching-paren-distance))
+                               oldpos))
+         (condition-case ()
+             (let ((parse-sexp-ignore-comments
+                    (and parse-sexp-ignore-comments
+                         (not blink-matching-paren-dont-ignore-comments))))
+               (setq blinkpos (scan-sexps oldpos -1)))
+           (error nil)))
+       (and blinkpos
+            ;; Not syntax '$'.
+            (not (eq (syntax-class (syntax-after blinkpos)) 8))
+            (setq matching-paren
+                  (let ((syntax (syntax-after blinkpos)))
+                    (and (consp syntax)
+                         (eq (syntax-class syntax) 4)
+                         (cdr syntax)))))
+       (cond
+        ((not (or (eq matching-paren (char-before oldpos))
+                   ;; The cdr might hold a new paren-class info rather than
+                   ;; a matching-char info, in which case the two CDRs
+                   ;; should match.
+                   (eq matching-paren (cdr (syntax-after (1- oldpos))))))
+         (message "Mismatched parentheses"))
+        ((not blinkpos)
+         (if (not blink-matching-paren-distance)
+             (message "Unmatched parenthesis")))
+        ((pos-visible-in-window-p blinkpos)
+         ;; Matching open within window, temporarily move to blinkpos but only
+         ;; if `blink-matching-paren-on-screen' is non-nil.
+         (and blink-matching-paren-on-screen
+              (not show-paren-mode)
+              (save-excursion
+                (goto-char blinkpos)
+                (sit-for blink-matching-delay))))
+        (t
+         (save-excursion
+           (goto-char blinkpos)
+           (setq open-paren-line-string
+                 ;; Show what precedes the open in its line, if anything.
+                 (if (save-excursion
+                       (skip-chars-backward " \t")
+                       (not (bolp)))
+                     (buffer-substring (line-beginning-position)
+                                       (1+ blinkpos))
+                   ;; Show what follows the open in its line, if anything.
+                   (if (save-excursion
+                         (forward-char 1)
+                         (skip-chars-forward " \t")
+                         (not (eolp)))
+                       (buffer-substring blinkpos
+                                         (line-end-position))
+                     ;; Otherwise show the previous nonblank line,
+                     ;; if there is one.
+                     (if (save-excursion
+                           (skip-chars-backward "\n \t")
+                           (not (bobp)))
+                         (concat
+                          (buffer-substring (progn
                                               (skip-chars-backward "\n \t")
-                                              (beginning-of-line)
-                                              (point))
-                                             (progn (end-of-line)
-                                                    (skip-chars-backward " \t")
-                                                    (point)))
-                           ;; Replace the newline and other whitespace with `...'.
-                           "..."
-                           (buffer-substring blinkpos (1+ blinkpos)))
-                        ;; There is nothing to show except the char itself.
-                        (buffer-substring blinkpos (1+ blinkpos))))))))
-            (cond (mismatch
-                   (message "Mismatched parentheses"))
-                  ((not blink-matching-paren-distance)
-                   (message "Unmatched parenthesis"))))))))
+                                              (line-beginning-position))
+                                            (progn (end-of-line)
+                                                   (skip-chars-backward " \t")
+                                                   (point)))
+                          ;; Replace the newline and other whitespace with `...'.
+                          "..."
+                          (buffer-substring blinkpos (1+ blinkpos)))
+                       ;; There is nothing to show except the char itself.
+                       (buffer-substring blinkpos (1+ blinkpos)))))))
+         (message "Matches %s"
+                  (substring-no-properties open-paren-line-string))))))))
 
 ;Turned off because it makes dbx bomb out.
 (setq blink-paren-function 'blink-matching-open)
@@ -4459,17 +4686,14 @@ See also `read-mail-command' concerning reading mail."
        (unless (member-ignore-case (car (car other-headers))
                                    '("in-reply-to" "cc" "body"))
            (insert (car (car other-headers)) ": "
-                   (cdr (car other-headers)) "\n"))
+                   (cdr (car other-headers))
+                   (if use-hard-newlines hard-newline "\n")))
        (setq other-headers (cdr other-headers)))
       (when body
        (forward-line 1)
        (insert body))
       t)))
 
-(define-mail-user-agent 'mh-e-user-agent
-  'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft
-  'mh-before-send-letter-hook)
-
 (defun compose-mail (&optional to subject other-headers continue
                               switch-function yank-action send-actions)
   "Start composing a mail message to send.
@@ -4539,10 +4763,10 @@ in the definition is used to check that VALUE is valid.
 With a prefix argument, set VARIABLE to VALUE buffer-locally."
   (interactive
    (let* ((default-var (variable-at-point))
-          (var (if (symbolp default-var)
-                       (read-variable (format "Set variable (default %s): " default-var)
-                                      default-var)
-                     (read-variable "Set variable: ")))
+          (var (if (user-variable-p default-var)
+                  (read-variable (format "Set variable (default %s): " default-var)
+                                 default-var)
+                (read-variable "Set variable: ")))
          (minibuffer-help-form '(describe-variable var))
          (prop (get var 'variable-interactive))
           (obsolete (car (get var 'byte-obsolete-variable)))
@@ -4567,7 +4791,8 @@ With a prefix argument, set VARIABLE to VALUE buffer-locally."
                                             arg))
                    (read
                     (read-string prompt nil
-                                 'set-variable-value-history))))))
+                                 'set-variable-value-history
+                                (format "%S" (symbol-value var))))))))
      (list var val current-prefix-arg)))
 
   (and (custom-variable-p variable)
@@ -4684,7 +4909,7 @@ With prefix argument N, move N items (negative N means move backward)."
        (error "No completion here"))
     (setq beg (previous-single-property-change beg 'mouse-face))
     (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
-    (setq completion (buffer-substring beg end))
+    (setq completion (buffer-substring-no-properties beg end))
     (let ((owindow (selected-window)))
       (if (and (one-window-p t 'selected-frame)
               (window-dedicated-p (selected-window)))
@@ -4811,10 +5036,19 @@ Called from `temp-buffer-show-hook'."
   "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
 command to display the completion list buffer was run.
-The completion list buffer is available as the value of `standard-output'.")
+The completion list buffer is available as the value of `standard-output'.
+The common prefix substring for completion may be available as the
+value of `completion-common-substring'. See also `display-completion-list'.")
+
+
+;; Variables and faces used in `completion-setup-function'.
+
+(defcustom completion-show-help t
+  "Non-nil means show help message in *Completions* buffer."
+  :type 'boolean
+  :version "22.1"
+  :group 'completion)
 
-;; This function goes in completion-setup-hook, so that it is called
-;; after the text of the completion list buffer is written.
 (defface completions-first-difference
   '((t (:inherit bold)))
   "Face put on the first uncommon character in completions in *Completions* buffer."
@@ -4834,85 +5068,87 @@ of the differing parts is, by contrast, slightly highlighted."
 (defvar completion-root-regexp "^/"
   "Regexp to use in `completion-setup-function' to find the root directory.")
 
+(defvar completion-common-substring nil
+  "Common prefix substring to use in `completion-setup-function' to put faces.
+The value is set by `display-completion-list' during running `completion-setup-hook'.
+
+To put faces `completions-first-difference' and `completions-common-part'
+in the `*Completions*' buffer, the common prefix substring in completions
+is needed as a hint.  (The minibuffer is a special case.  The content
+of the minibuffer before point is always the common substring.)")
+
+;; This function goes in completion-setup-hook, so that it is called
+;; after the text of the completion list buffer is written.
 (defun completion-setup-function ()
-  (let ((mainbuf (current-buffer))
-       (mbuf-contents (minibuffer-contents)))
+  (let* ((mainbuf (current-buffer))
+         (mbuf-contents (minibuffer-completion-contents))
+         common-string-length)
     ;; When reading a file name in the minibuffer,
     ;; set default-directory in the minibuffer
     ;; so it will get copied into the completion list buffer.
     (if minibuffer-completing-file-name
        (with-current-buffer mainbuf
          (setq default-directory (file-name-directory mbuf-contents))))
-    ;; If partial-completion-mode is on, point might not be after the
-    ;; last character in the minibuffer.
-    ;; FIXME: This still doesn't work if the text to be completed
-    ;; starts with a `-'.
-    (when (and partial-completion-mode (not (eobp)))
-      (setq mbuf-contents
-           (substring mbuf-contents 0 (- (point) (point-max)))))
     (with-current-buffer standard-output
       (completion-list-mode)
-      (make-local-variable 'completion-reference-buffer)
-      (setq completion-reference-buffer mainbuf)
-      (if minibuffer-completing-file-name
-         ;; For file name completion,
-         ;; use the number of chars before the start of the
-         ;; last file name component.
-         (setq completion-base-size
-               (with-current-buffer mainbuf
-                 (save-excursion
-                   (goto-char (point-max))
-                   (skip-chars-backward completion-root-regexp)
-                   (- (point) (minibuffer-prompt-end)))))
-       ;; Otherwise, in minibuffer, the whole input is being completed.
-       (if (minibufferp mainbuf)
-           (if (and (symbolp minibuffer-completion-table)
-                    (get minibuffer-completion-table 'completion-base-size-function))
-               (setq completion-base-size
-                     (funcall (get minibuffer-completion-table 'completion-base-size-function)))
-             (setq completion-base-size 0))))
+      (set (make-local-variable 'completion-reference-buffer) mainbuf)
+      (setq completion-base-size
+           (cond
+            ((and (symbolp minibuffer-completion-table)
+                  (get minibuffer-completion-table 'completion-base-size-function))
+             ;; To compute base size, a function can use the global value of
+             ;; completion-common-substring or minibuffer-completion-contents.
+             (with-current-buffer mainbuf
+               (funcall (get minibuffer-completion-table
+                             'completion-base-size-function))))
+            (minibuffer-completing-file-name
+             ;; For file name completion, use the number of chars before
+             ;; the start of the file name component at point.
+             (with-current-buffer mainbuf
+               (save-excursion
+                 (skip-chars-backward completion-root-regexp)
+                 (- (point) (minibuffer-prompt-end)))))
+            ;; Otherwise, in minibuffer, the base size is 0.
+            ((minibufferp mainbuf) 0)))
+      (setq common-string-length
+           (cond
+            (completion-common-substring
+             (length completion-common-substring))
+            (completion-base-size
+             (- (length mbuf-contents) completion-base-size))))
       ;; Put faces on first uncommon characters and common parts.
-      (when completion-base-size
-       (let* ((common-string-length
-               (- (length mbuf-contents) completion-base-size))
-              (element-start (next-single-property-change
-                              (point-min)
-                              'mouse-face))
-              (element-common-end
-               (and element-start
-                    (+ (or element-start nil) common-string-length)))
-              (maxp (point-max)))
-         (while (and element-start (< element-common-end maxp))
-           (when (and (get-char-property element-start 'mouse-face)
-                      (get-char-property element-common-end 'mouse-face))
-             (put-text-property element-start element-common-end
-                                'font-lock-face 'completions-common-part)
-             (put-text-property element-common-end (1+ element-common-end)
-                                'font-lock-face 'completions-first-difference))
-           (setq element-start (next-single-property-change
-                                element-start
-                                'mouse-face))
-           (if element-start
-               (setq element-common-end  (+ element-start common-string-length))))))
-      ;; Insert help string.
-      (goto-char (point-min))
-      (if (display-mouse-p)
-         (insert (substitute-command-keys
-                  "Click \\[mouse-choose-completion] on a completion to select it.\n")))
-      (insert (substitute-command-keys
-              "In this buffer, type \\[choose-completion] to \
-select the completion near point.\n\n")))))
+      (when (and (integerp common-string-length) (>= common-string-length 0))
+       (let ((element-start (point-min))
+              (maxp (point-max))
+              element-common-end)
+         (while (and (setq element-start
+                            (next-single-property-change
+                             element-start 'mouse-face))
+                      (< (setq element-common-end
+                               (+ element-start common-string-length))
+                         maxp))
+           (when (get-char-property element-start 'mouse-face)
+             (if (and (> common-string-length 0)
+                      (get-char-property (1- element-common-end) 'mouse-face))
+                 (put-text-property element-start element-common-end
+                                    'font-lock-face 'completions-common-part))
+             (if (get-char-property element-common-end 'mouse-face)
+                 (put-text-property element-common-end (1+ element-common-end)
+                                    'font-lock-face 'completions-first-difference))))))
+      ;; Maybe insert help string.
+      (when completion-show-help
+       (goto-char (point-min))
+       (if (display-mouse-p)
+           (insert (substitute-command-keys
+                    "Click \\[mouse-choose-completion] on a completion to select it.\n")))
+       (insert (substitute-command-keys
+                "In this buffer, type \\[choose-completion] to \
+select the completion near point.\n\n"))))))
 
 (add-hook 'completion-setup-hook 'completion-setup-function)
 
-(define-key minibuffer-local-completion-map [prior]
-  'switch-to-completions)
-(define-key minibuffer-local-must-match-map [prior]
-  'switch-to-completions)
-(define-key minibuffer-local-completion-map "\M-v"
-  'switch-to-completions)
-(define-key minibuffer-local-must-match-map "\M-v"
-  'switch-to-completions)
+(define-key minibuffer-local-completion-map [prior] 'switch-to-completions)
+(define-key minibuffer-local-completion-map "\M-v"  'switch-to-completions)
 
 (defun switch-to-completions ()
   "Select the completion list window."
@@ -4926,8 +5162,8 @@ select the completion near point.\n\n")))))
       (goto-char (point-min))
       (search-forward "\n\n")
       (forward-line 1))))
-
-;; Support keyboard commands to turn on various modifiers.
+\f
+;;; Support keyboard commands to turn on various modifiers.
 
 ;; These functions -- which are not commands -- each add one modifier
 ;; to the following event.
@@ -4992,7 +5228,7 @@ PREFIX is the string that represents this modifier in an event type symbol."
 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
-
+\f
 ;;;; Keypad support.
 
 ;;; Make the keypad keys act like ordinary typing keys.  If people add
@@ -5009,7 +5245,7 @@ PREFIX is the string that represents this modifier in an event type symbol."
      (define-key function-key-map (vector keypad) (vector normal))))
  '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
    (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
-   (kp-space ?\ )
+   (kp-space ?\s)
    (kp-tab ?\t)
    (kp-enter ?\r)
    (kp-multiply ?*)
@@ -5087,7 +5323,8 @@ after it has been set up properly in other respects."
         (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: "))
+     (list (if current-prefix-arg
+              (read-buffer "Name of new cloned buffer: " (current-buffer)))
           t)))
   (if buffer-file-name
       (error "Cannot clone a file-visiting buffer"))
@@ -5134,7 +5371,12 @@ after it has been set up properly in other respects."
       ;; Run any hooks (typically set up by the major mode
       ;; for cloning to work properly).
       (run-hooks 'clone-buffer-hook))
-    (if display-flag (pop-to-buffer new))
+    (if display-flag
+        ;; Presumably the current buffer is shown in the selected frame, so
+        ;; we want to display the clone elsewhere.
+        (let ((same-window-regexps nil)
+              (same-window-buffer-names))
+          (pop-to-buffer new)))
     new))
 
 
@@ -5150,14 +5392,14 @@ or by incrementing the N in an existing suffix.
 DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'.
 This is always done when called interactively.
 
-Optional last arg NORECORD non-nil means do not put this buffer at the
+Optional third arg NORECORD non-nil means do not put this buffer at the
 front of the list of recently selected ones."
   (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: "))
+              (read-buffer "Name of indirect buffer: " (current-buffer)))
           t)))
   (if (get major-mode 'no-clone-indirect)
       (error "Cannot indirectly clone a buffer in %s mode" mode-name))
@@ -5171,15 +5413,17 @@ front of the list of recently selected ones."
     buffer))
 
 
-(defun clone-indirect-buffer-other-window (buffer &optional norecord)
-  "Create an indirect buffer that is a twin copy of BUFFER.
-Select the new buffer in another window.
-Optional second arg NORECORD non-nil means do not put this buffer at
-the front of the list of recently selected ones."
-  (interactive "bClone buffer in other window: ")
+(defun clone-indirect-buffer-other-window (newname display-flag &optional norecord)
+  "Like `clone-indirect-buffer' but display in another window."
+  (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-buffer "Name of indirect buffer: " (current-buffer)))
+          t)))
   (let ((pop-up-windows t))
-    (set-buffer buffer)
-    (clone-indirect-buffer nil t norecord)))
+    (clone-indirect-buffer newname display-flag norecord)))
 
 \f
 ;;; Handling of Backspace and Delete keys.