]> code.delx.au - gnu-emacs/blobdiff - lisp/simple.el
(input-mode-8-bit): Fix the customization.
[gnu-emacs] / lisp / simple.el
index 18f8bc9a4b949205ebc0d546d6c90e5f07c63d7b..bc78fc66f99b93cc15d79b6b49e03b72ad6f433a 100644 (file)
 
 ;;; Code:
 
+(eval-when-compile
+  (require 'cl))
+
+
 (defgroup killing nil
   "Killing and yanking commands"
   :group 'editing)
   :group 'matching)
 
 
+(defun fundamental-mode ()
+  "Major mode not specialized for anything in particular.
+Other major modes are defined by comparison with this one."
+  (interactive)
+  (kill-all-local-variables))
+\f
+;; Making and deleting lines.
+
 (defun newline (&optional arg)
   "Insert a newline, and move to left margin of the new line if it's blank.
 The newline is marked with the text-property `hard'.
@@ -52,7 +64,7 @@ In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
   ;; try_window_id than inserting at the beginning of a line, and the textual
   ;; result is the same.  So, if we're at beginning of line, pretend to be at
   ;; the end of the previous line.
-  (let ((flag (and (not (bobp)) 
+  (let ((flag (and (not (bobp))
                   (bolp)
                   ;; Make sure no functions want to be told about
                   ;; the range of the changes.
@@ -77,7 +89,7 @@ In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
                   (not (get-char-property (1- (point)) 'invisible))
                   ;; Make sure the newline before point has the same
                   ;; properties as the char before it (if any).
-                  (< (or (previous-property-change (point)) -2) 
+                  (< (or (previous-property-change (point)) -2)
                      (- (point) 2))))
        (was-page-start (and (bolp)
                             (looking-at page-delimiter)))
@@ -125,7 +137,7 @@ In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
     (if (and (listp sticky) (not (memq 'hard sticky)))
        (put-text-property from (point) 'rear-nonsticky
                           (cons 'hard sticky)))))
-
+\f
 (defun open-line (arg)
   "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
@@ -156,44 +168,6 @@ With arg N, insert N newlines."
     (indent-to col 0)
     (goto-char pos)))
 
-(defun quoted-insert (arg)
-  "Read next input character and insert it.
-This is useful for inserting control characters.
-
-If the first character you type after this command is an octal digit,
-you should type a sequence of octal digits which specify a character code.
-Any nondigit terminates the sequence.  If the terminator is a RET,
-it is discarded; any other terminator is used itself as input.
-The variable `read-quoted-char-radix' specifies the radix for this feature;
-set it to 10 or 16 to use decimal or hex instead of octal.
-
-In overwrite mode, this function inserts the character anyway, and
-does not handle octal digits specially.  This means that if you use
-overwrite as your normal editing mode, you can use this function to
-insert characters when necessary.
-
-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 (if (or (not overwrite-mode)
-                     (eq overwrite-mode 'overwrite-mode-binary))
-                 (read-quoted-char)
-               (read-char))))
-    ;; Assume character codes 0240 - 0377 stand for characters in some
-    ;; single-byte character set, and convert them to Emacs
-    ;; characters.
-    (if (and enable-multibyte-characters
-            (>= char ?\240)
-            (<= char ?\377))
-       (setq char (unibyte-char-to-multibyte char)))
-    (if (> arg 0)
-       (if (eq overwrite-mode 'overwrite-mode-binary)
-           (delete-char arg)))
-    (while (> arg 0)
-      (insert-and-inherit char)
-      (setq arg (1- arg)))))
-
 (defun delete-indentation (&optional arg)
   "Join this line to previous and fix up whitespace at join.
 If there is a fill prefix, delete it from the beginning of this line.
@@ -215,34 +189,7 @@ With argument, join this line to following line."
        (fixup-whitespace))))
 
 (defalias 'join-line #'delete-indentation) ; easier to find
-
-(defun fixup-whitespace ()
-  "Fixup white space between objects around point.
-Leave one space or none, according to the context."
-  (interactive "*")
-  (save-excursion
-    (delete-horizontal-space)
-    (if (or (looking-at "^\\|\\s)")
-           (save-excursion (forward-char -1)
-                           (looking-at "$\\|\\s(\\|\\s'")))
-       nil
-      (insert ?\ ))))
-
-(defun delete-horizontal-space ()
-  "Delete all spaces and tabs around point."
-  (interactive "*")
-  (skip-chars-backward " \t")
-  (delete-region (point) (progn (skip-chars-forward " \t") (point))))
-
-(defun just-one-space ()
-  "Delete all spaces and tabs around point, leaving one space."
-  (interactive "*")
-  (skip-chars-backward " \t")
-  (if (= (following-char) ? )
-      (forward-char 1)
-    (insert ? ))
-  (delete-region (point) (progn (skip-chars-forward " \t") (point))))
-
+\f
 (defun delete-blank-lines ()
   "On blank line, delete all surrounding blank lines, leaving just one.
 On isolated blank line, delete that one.
@@ -283,12 +230,6 @@ On nonblank line, delete any immediately following blank lines."
     (if (looking-at "^[ \t]*\n\\'")
        (delete-region (point) (point-max)))))
 
-(defun back-to-indentation ()
-  "Move point to the first non-whitespace character on this line."
-  (interactive)
-  (beginning-of-line 1)
-  (skip-chars-forward " \t"))
-
 (defun newline-and-indent ()
   "Insert a newline, then indent according to major mode.
 Indentation is done using the value of `indent-line-function'.
@@ -313,63 +254,91 @@ column specified by the function `current-left-margin'."
     (indent-according-to-mode))
   (newline)
   (indent-according-to-mode))
+\f
+(defun quoted-insert (arg)
+  "Read next input character and insert it.
+This is useful for inserting control characters.
 
-;; Internal subroutine of delete-char
-(defun kill-forward-chars (arg)
-  (if (listp arg) (setq arg (car arg)))
-  (if (eq arg '-) (setq arg -1))
-  (kill-region (point) (forward-point arg)))
+If the first character you type after this command is an octal digit,
+you should type a sequence of octal digits which specify a character code.
+Any nondigit terminates the sequence.  If the terminator is a RET,
+it is discarded; any other terminator is used itself as input.
+The variable `read-quoted-char-radix' specifies the radix for this feature;
+set it to 10 or 16 to use decimal or hex instead of octal.
 
-;; Internal subroutine of backward-delete-char
-(defun kill-backward-chars (arg)
-  (if (listp arg) (setq arg (car arg)))
-  (if (eq arg '-) (setq arg -1))
-  (kill-region (point) (forward-point (- arg))))
+In overwrite mode, this function inserts the character anyway, and
+does not handle octal digits specially.  This means that if you use
+overwrite as your normal editing mode, you can use this function to
+insert characters when necessary.
 
-(defcustom backward-delete-char-untabify-method 'untabify
-  "*The method for untabifying when deleting backward.
-Can be `untabify' -- turn a tab to many spaces, then delete one space.
-       `hungry' -- delete all whitespace, both tabs and spaces.
-       nil -- just delete one character."
-  :type '(choice (const untabify) (const hungry) (const nil))
-  :group 'killing)
+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 (if (or (not overwrite-mode)
+                     (eq overwrite-mode 'overwrite-mode-binary))
+                 (read-quoted-char)
+               (read-char))))
+    ;; Assume character codes 0240 - 0377 stand for characters in some
+    ;; single-byte character set, and convert them to Emacs
+    ;; characters.
+    (if (and enable-multibyte-characters
+            (>= char ?\240)
+            (<= char ?\377))
+       (setq char (unibyte-char-to-multibyte char)))
+    (if (> arg 0)
+       (if (eq overwrite-mode 'overwrite-mode-binary)
+           (delete-char arg)))
+    (while (> arg 0)
+      (insert-and-inherit char)
+      (setq arg (1- arg)))))
+\f
+(defun forward-to-indentation (arg)
+  "Move forward ARG lines and position at first nonblank character."
+  (interactive "p")
+  (forward-line arg)
+  (skip-chars-forward " \t"))
 
-(defun backward-delete-char-untabify (arg &optional killp)
-  "Delete characters backward, changing tabs into spaces.
-The exact behavior depends on `backward-delete-char-untabify-method'.
-Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
-Interactively, ARG is the prefix arg (default 1)
-and KILLP is t if a prefix arg was specified."
-  (interactive "*p\nP")
-  (when (eq backward-delete-char-untabify-method 'untabify)
-    (let ((count arg))
-      (save-excursion
-       (while (and (> count 0) (not (bobp)))
-         (if (= (preceding-char) ?\t)
-             (let ((col (current-column)))
-               (forward-char -1)
-               (setq col (- col (current-column)))
-               (insert-char ?\ col)
-               (delete-char 1)))
-         (forward-char -1)
-         (setq count (1- count))))))
-  (delete-backward-char
-   (if (eq backward-delete-char-untabify-method 'hungry)
-       (let ((wh (- (point) (save-excursion (skip-chars-backward " \t")
-                                           (point)))))
-        (+ arg (if (zerop wh) 0 (1- wh))))
-     arg)
-   killp))
+(defun backward-to-indentation (arg)
+  "Move backward ARG lines and position at first nonblank character."
+  (interactive "p")
+  (forward-line (- arg))
+  (skip-chars-forward " \t"))
 
-(defun zap-to-char (arg char)
-  "Kill up to and including ARG'th occurrence of CHAR.
-Goes backward if ARG is negative; error if CHAR not found."
-  (interactive "p\ncZap to char: ")
-  (kill-region (point) (progn
-                        (search-forward (char-to-string char) nil nil arg)
-;                       (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
-                        (point))))
+(defun back-to-indentation ()
+  "Move point to the first non-whitespace character on this line."
+  (interactive)
+  (beginning-of-line 1)
+  (skip-chars-forward " \t"))
 
+(defun fixup-whitespace ()
+  "Fixup white space between objects around point.
+Leave one space or none, according to the context."
+  (interactive "*")
+  (save-excursion
+    (delete-horizontal-space)
+    (if (or (looking-at "^\\|\\s)")
+           (save-excursion (forward-char -1)
+                           (looking-at "$\\|\\s(\\|\\s'")))
+       nil
+      (insert ?\ ))))
+
+(defun delete-horizontal-space ()
+  "Delete all spaces and tabs around point."
+  (interactive "*")
+  (skip-chars-backward " \t")
+  (delete-region (point) (progn (skip-chars-forward " \t") (point))))
+
+(defun just-one-space ()
+  "Delete all spaces and tabs around point, leaving one space."
+  (interactive "*")
+  (skip-chars-backward " \t")
+  (if (= (following-char) ? )
+      (forward-char 1)
+    (insert ? ))
+  (delete-region (point) (progn (skip-chars-forward " \t") (point))))
+
+\f
 (defun beginning-of-buffer (&optional arg)
   "Move point to the beginning of the buffer; leave mark at previous position.
 With arg N, put point N/10 of the way from the beginning.
@@ -414,17 +383,12 @@ Don't use this command in Lisp programs!
                 (point-max))))
   ;; If we went to a place in the middle of the buffer,
   ;; adjust it to the beginning of a line.
-  (if arg (forward-line 1)
-    ;; If the end of the buffer is not already on the screen,
-    ;; then scroll specially to put it near, but not at, the bottom.
-    (if (let ((old-point (point)))
-         (save-excursion
-                   (goto-char (window-start))
-                   (vertical-motion (window-height))
-                   (< (point) old-point)))
-       (progn
-         (overlay-recenter (point))
-         (recenter -3)))))
+  (cond (arg (forward-line 1))
+       ((< (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))
+        (recenter -3))))
 
 (defun mark-whole-buffer ()
   "Put point at beginning and mark at end of buffer.
@@ -435,6 +399,19 @@ that uses or sets the mark."
   (push-mark (point))
   (push-mark (point-max) nil t)
   (goto-char (point-min)))
+\f
+;; Counting lines, one way or another.
+
+(defun goto-line (arg)
+  "Goto line ARG, counting from line 1 at beginning of buffer."
+  (interactive "NGoto line: ")
+  (setq arg (prefix-numeric-value arg))
+  (save-restriction
+    (widen)
+    (goto-char 1)
+    (if (eq selective-display t)
+       (re-search-forward "[\n\C-m]" nil 'end (1- arg))
+      (forward-line (1- arg)))))
 
 (defun count-lines-region (start end)
   "Print number of lines and characters in the region."
@@ -460,7 +437,6 @@ that uses or sets the mark."
                     (1+ (count-lines start (point))))
          (message "Line %d" (1+ (count-lines 1 (point)))))))))
 
-
 (defun count-lines (start end)
   "Return number of lines between START and END.
 This is usually the number of newlines between them,
@@ -483,20 +459,22 @@ and the greater of them is not at the start of a line."
                  (1+ done)
                done)))
        (- (buffer-size) (forward-line (buffer-size)))))))
-
+\f
 (defun what-cursor-position (&optional detail)
   "Print info on cursor position (on screen and within buffer).
 Also describe the character after point, and give its character code
-in octal, decimal and hex.  For a non-ASCII multibyte character,
-also give its encoding in the buffer's selected coding system,
-if any.
+in octal, decimal and hex.
+
+For a non-ASCII multibyte character, also give its encoding in the
+buffer's selected coding system if the coding system encodes the
+character safely.  If the character is encoded into one byte, that
+code is shown in hex.  If the character is encoded into more than one
+byte, just \"...\" is shown.
 
 With prefix argument, print additional details about that character,
 instead of the cursor position.  This includes the character set name,
-the codes that identify the character within that character set,
-and the corresponding external character components.
-
-Each language environment may show different external character components."
+the codes that identify the character within that character set.  In
+addition, the encoding is fully shown."
   (interactive "P")
   (let* ((char (following-char))
         (beg (point-min))
@@ -517,77 +495,77 @@ Each language environment may show different external character components."
                     pos total percent beg end col hscroll)
          (message "point=%d of %d(%d%%)  column %d %s"
                   pos total percent col hscroll))
-      (let ((charset (char-charset char))
-           (coding-system buffer-file-coding-system)
-           slot external encoding-msg)
-       ;; To decided an external character code, we use
-       ;; charset-origin-alist property of buffer-file-coding-system.
-       ;; But, if buffer-file-coding-system is nil of undecided, use
-       ;; that property of default-buffer-file-coding-system.  If
-       ;; that property value is nil, we don't show external
-       ;; character code.
-       (if (or (not coding-system)
-               (eq (coding-system-type coding-system) t))
-           (setq coding-system default-buffer-file-coding-system))
-       (if (and coding-system
-                (setq slot
-                      (coding-system-get coding-system 'charset-origin-alist))
-                (setq slot (assq charset slot)))
-           (setq external (list (nth 1 slot) (funcall (nth 2 slot) char))))
-       (setq encoding-msg
-             (if external
-                 (format "(0%o, %d, 0x%x, ext 0x%x)"
-                         char char char (nth 1 external))
-               (format "(0%o, %d, 0x%x)" char char char)))
+      (let ((coding buffer-file-coding-system)
+           encoded encoding-msg)
+       (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)))
+         (setq encoding-msg
+               (if encoded
+                   (format "(0%o, %d, 0x%x, ext %s)"
+                           char char char
+                           (if (and (not detail)
+                                    (> (length encoded) 1))
+                               "..."
+                             (concat
+                              (encoded-string-description encoded coding)
+                              (if (cmpcharp char) "..." ""))))
+                 (format "(0%o, %d, 0x%x)" char char char))))
        (if detail
            ;; We show the detailed information of CHAR.
-           (let (internal)
-             (if (eq charset 'composition)
-                 ;; For a composite character, we show the components
-                 ;; only.
-                 (setq internal (concat "(composition of \""
-                                        (decompose-composite-char char)
-                                        "\")")
-                       external nil)
-               (setq internal (split-char char))
-               (unless external
-                 (setq external (cons (charset-short-name charset)
-                                      (copy-sequence (cdr internal))))
-                 (if (= (charset-iso-graphic-plane charset) 1)
-                     (progn
-                       (setcar (cdr external) (+ (nth 1 external) 128))
-                       (if (nth 2 external)
-                           (setcar (nthcdr 2 external)
-                                   (+ (nth 2 external) 128)))))))
-             (message "Char: %s %s %s %s"
+           (let ((internal
+                  (if (cmpcharp char)
+                      ;; For a composite character, we show the
+                      ;; components only.
+                      (concat "(composed \""
+                              (decompose-composite-char char)
+                              "\")")
+                    (split-char char))))
+             (message "Char: %s %s %s"
                       (if (< char 256)
                           (single-key-description char)
-                        (char-to-string char))
-                      encoding-msg internal (or external "")))
+                        (buffer-substring (point) (1+ (point))))
+                      encoding-msg internal))
          (if (or (/= beg 1) (/= end (1+ total)))
              (message "Char: %s %s point=%d of %d(%d%%) <%d - %d>  column %d %s"
                       (if (< char 256)
                           (single-key-description char)
-                        (char-to-string char))
+                        (buffer-substring (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)
-                      (char-to-string char))
+                      (buffer-substring (point) (1+ (point))))
                     encoding-msg pos total percent col hscroll)))))))
-
-(defun fundamental-mode ()
-  "Major mode not specialized for anything in particular.
-Other major modes are defined by comparison with this one."
-  (interactive)
-  (kill-all-local-variables))
-
+\f
 (defvar read-expression-map (cons 'keymap minibuffer-local-map)
   "Minibuffer keymap used for reading Lisp expressions.")
 (define-key read-expression-map "\M-\t" 'lisp-complete-symbol)
 
 (defvar read-expression-history nil)
 
+(defcustom eval-expression-print-level 4
+  "*Value to use for `print-level' when printing value in `eval-expression'."
+  :group 'lisp
+  :type 'integer
+  :version "21.1")
+
+(defcustom eval-expression-print-length 12
+  "*Value to use for `print-length' when printing value in `eval-expression'."
+  :group 'lisp
+  :type 'integer
+  :version "21.1")
+
+(defcustom eval-expression-debug-on-error t
+  "*Value to use for `debug-on-error' when evaluating in `eval-expression'."
+  :group 'lisp
+  :type 'boolean
+  :version "21.1")
+
 ;; We define this, rather than making `eval' interactive,
 ;; for the sake of completion of names like eval-region, eval-current-buffer.
 (defun eval-expression (eval-expression-arg
@@ -599,9 +577,12 @@ Value is also consed on to front of the variable `values'."
                               nil read-expression-map t
                               'read-expression-history)
         current-prefix-arg))
-  (setq values (cons (eval eval-expression-arg) values))
-  (prin1 (car values)
-        (if eval-expression-insert-value (current-buffer) t)))
+  (let ((debug-on-error eval-expression-debug-on-error))
+    (setq values (cons (eval eval-expression-arg) values)))
+  (let ((print-length eval-expression-print-length)
+       (print-level eval-expression-print-level))
+    (prin1 (car values)
+          (if eval-expression-insert-value (current-buffer) t))))
 
 (defun edit-and-eval-command (prompt command)
   "Prompting with PROMPT, let user edit COMMAND and eval result.
@@ -741,7 +722,7 @@ See also `minibuffer-history-case-insensitive-variables'."
           (prefix-numeric-value current-prefix-arg))))
   (if (and (zerop minibuffer-history-position)
           (null minibuffer-text-before-history))
-      (setq minibuffer-text-before-history (buffer-string)))
+      (setq minibuffer-text-before-history (field-string (point-max))))
   (let ((history (symbol-value minibuffer-history-variable))
        (case-fold-search
         (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped
@@ -769,13 +750,14 @@ See also `minibuffer-history-case-insensitive-variables'."
                          (nth (1- pos) history)))
          (setq n (+ n (if (< n 0) 1 -1)))))
     (setq minibuffer-history-position pos)
-    (erase-buffer)
+    (goto-char (point-max))
+    (delete-field)
     (let ((elt (nth (1- pos) history)))
       (insert (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
                  (let ((print-level nil))
                    (prin1-to-string elt))
                elt)))
-      (goto-char (point-min)))
+      (goto-char (field-beginning)))
   (if (or (eq (car (car command-history)) 'previous-matching-history-element)
          (eq (car (car command-history)) 'next-matching-history-element))
       (setq command-history (cdr command-history))))
@@ -807,31 +789,34 @@ An uppercase letter in REGEXP makes the search case-sensitive."
   (or (zerop n)
       (let ((narg (- minibuffer-history-position n))
            (minimum (if minibuffer-default -1 0))
-           elt)
+           elt minibuffer-returned-to-present)
        (if (and (zerop minibuffer-history-position)
                 (null minibuffer-text-before-history))
-           (setq minibuffer-text-before-history (buffer-string)))
+           (setq minibuffer-text-before-history (field-string (point-max))))
        (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"))
-       (erase-buffer)
+       (goto-char (point-max))
+       (delete-field)
        (setq minibuffer-history-position narg)
        (cond ((= narg -1)
               (setq elt minibuffer-default))
              ((= narg 0)
               (setq elt (or minibuffer-text-before-history ""))
+              (setq minibuffer-returned-to-present t)
               (setq minibuffer-text-before-history nil))
              (t (setq elt (nth (1- minibuffer-history-position)
                                (symbol-value minibuffer-history-variable)))))
        (insert
-        (if (eq minibuffer-history-sexp-flag (minibuffer-depth))
+        (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
+                 (not minibuffer-returned-to-present))
             (let ((print-level nil))
               (prin1-to-string elt))
           elt))
-       (goto-char (point-min)))))
+       (goto-char (field-beginning)))))
 
 (defun previous-history-element (n)
   "Inserts the previous element of the minibuffer history into the minibuffer."
@@ -839,11 +824,15 @@ An uppercase letter in REGEXP makes the search case-sensitive."
   (next-history-element (- n)))
 
 (defun next-complete-history-element (n)
-  "Get next element of history which is a completion of minibuffer contents."
+  "Get next history element which completes the minibuffer before the point.
+The contents of the minibuffer after the point are deleted, and replaced
+by the new completion."
   (interactive "p")
   (let ((point-at-start (point)))
     (next-matching-history-element
-     (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
+     (concat
+      "^" (regexp-quote (buffer-substring (field-beginning) (point))))
+     n)
     ;; next-matching-history-element always puts us at (point-min).
     ;; Move to the position we were at before changing the buffer contents.
     ;; This is still sensical, because the text before point has not changed.
@@ -851,21 +840,28 @@ An uppercase letter in REGEXP makes the search case-sensitive."
 
 (defun previous-complete-history-element (n)
   "\
-Get previous element of history which is a completion of minibuffer contents."
+Get previous history element which completes the minibuffer before the point.
+The contents of the minibuffer after the point are deleted, and replaced
+by the new completion."
   (interactive "p")
   (next-complete-history-element (- n)))
-\f
-(defun goto-line (arg)
-  "Goto line ARG, counting from line 1 at beginning of buffer."
-  (interactive "NGoto line: ")
-  (setq arg (prefix-numeric-value arg))
-  (save-restriction
-    (widen)
-    (goto-char 1)
-    (if (eq selective-display t)
-       (re-search-forward "[\n\C-m]" nil 'end (1- arg))
-      (forward-line (1- arg)))))
 
+;; These two functions are for compatibility with the old subrs 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 0 if current buffer is not a mini-buffer."
+  (field-beginning (point-max)))
+
+\f
 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
 (defalias 'advertised-undo 'undo)
 
@@ -940,7 +936,7 @@ that apply to text between BEG and END are used; other undo elements
 are ignored.  If BEG and END are nil, all undo elements are used."
   (if (eq buffer-undo-list t)
       (error "No undo information in this buffer"))
-  (setq pending-undo-list 
+  (setq pending-undo-list
        (if (and beg end (not (= beg end)))
            (undo-make-selective-list (min beg end) (max beg end))
          buffer-undo-list)))
@@ -1001,7 +997,7 @@ we stop and ignore all further elements."
                         (let ((text-pos (abs (cdr undo-elt)))
                               (point-at-end (< (cdr undo-elt) 0 )))
                           (if (>= text-pos position)
-                              (setcdr undo-elt (* (if point-at-end -1 1) 
+                              (setcdr undo-elt (* (if point-at-end -1 1)
                                                   (- text-pos offset))))))
                        ((integerp (car undo-elt))
                         ;; (BEGIN . END)
@@ -1090,7 +1086,13 @@ is not *inside* the region START...END."
 (defvar shell-command-switch "-c"
   "Switch used to have the shell execute its command line argument.")
 
-(defun shell-command (command &optional output-buffer)
+(defvar shell-command-default-error-buffer nil
+  "*Buffer name for `shell-command' and `shell-command-on-region' error output.
+This buffer is used when `shell-command' or 'shell-command-on-region'
+is run interactively.  A value of nil means that output to stderr and
+stdout will be intermixed in the output stream.")
+
+(defun shell-command (command &optional output-buffer error-buffer)
   "Execute string COMMAND in inferior shell; display output, if any.
 
 If COMMAND ends in ampersand, execute it asynchronously.
@@ -1117,34 +1119,66 @@ says to put the output in some other buffer.
 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
 If OUTPUT-BUFFER is not a buffer and not nil,
 insert output in current buffer.  (This cannot be done asynchronously.)
-In either case, the output is inserted after point (leaving mark after it)."
+In either case, the output is inserted after point (leaving mark after it).
+
+If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
+or buffer name to which to direct the command's standard error output.
+If it is nil, error output is mingled with regular output.
+In an interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
+
   (interactive (list (read-from-minibuffer "Shell command: "
                                           nil nil nil 'shell-command-history)
-                    current-prefix-arg))
+                    current-prefix-arg
+                    shell-command-default-error-buffer))
   ;; Look for a handler in case default-directory is a remote file name.
   (let ((handler
         (find-file-name-handler (directory-file-name default-directory)
                                 'shell-command)))
     (if handler
-       (funcall handler 'shell-command command output-buffer)
+       (funcall handler 'shell-command command output-buffer error-buffer)
       (if (and output-buffer
               (not (or (bufferp output-buffer)  (stringp output-buffer))))
-         (progn (barf-if-buffer-read-only)
-                (push-mark)
-                ;; We do not use -f for csh; we will not support broken use of
-                ;; .cshrcs.  Even the BSD csh manual says to use
-                ;; "if ($?prompt) exit" before things which are not useful
-                ;; non-interactively.  Besides, if someone wants their other
-                ;; aliases for shell commands then they can still have them.
-                (call-process shell-file-name nil t nil
-                              shell-command-switch command)
-                ;; This is like exchange-point-and-mark, but doesn't
-                ;; activate the mark.  It is cleaner to avoid activation,
-                ;; even though the command loop would deactivate the mark
-                ;; because we inserted text.
-                (goto-char (prog1 (mark t)
-                             (set-marker (mark-marker) (point)
-                                         (current-buffer)))))
+         (let ((error-file
+                (if error-buffer
+                    (make-temp-file
+                     (expand-file-name "scor"
+                                       (or small-temporary-file-directory
+                                           temporary-file-directory)))
+                  nil)))
+           (barf-if-buffer-read-only)
+           (push-mark nil t)
+           ;; We do not use -f for csh; we will not support broken use of
+           ;; .cshrcs.  Even the BSD csh manual says to use
+           ;; "if ($?prompt) exit" before things which are not useful
+           ;; non-interactively.  Besides, if someone wants their other
+           ;; aliases for shell commands then they can still have them.
+           (call-process shell-file-name nil
+                         (if error-file
+                             (list t error-file)
+                           t)
+                         nil shell-command-switch command)
+           (when (and error-file (file-exists-p error-file))
+             (if (< 0 (nth 7 (file-attributes error-file)))
+                 (with-current-buffer (get-buffer-create error-buffer)
+                   (let ((pos-from-end (- (point-max) (point))))
+                     (or (bobp)
+                         (insert "\f\n"))
+                     ;; Do no formatting while reading error file,
+                     ;; because that can run a shell command, and we
+                     ;; don't want that to cause an infinite recursion.
+                     (format-insert-file error-file nil)
+                     ;; Put point after the inserted errors.
+                     (goto-char (- (point-max) pos-from-end)))
+                   (display-buffer (current-buffer))))
+             (delete-file error-file))
+           ;; This is like exchange-point-and-mark, but doesn't
+           ;; activate the mark.  It is cleaner to avoid activation,
+           ;; even though the command loop would deactivate the mark
+           ;; because we inserted text.
+           (goto-char (prog1 (mark t)
+                        (set-marker (mark-marker) (point)
+                                    (current-buffer)))))
        ;; Preserve the match data in case called from a program.
        (save-match-data
          (if (string-match "[ \t]*&[ \t]*$" command)
@@ -1167,29 +1201,23 @@ In either case, the output is inserted after point (leaving mark after it)."
                  (erase-buffer)
                  (display-buffer buffer)
                  (setq default-directory directory)
-                 (setq proc (start-process "Shell" buffer shell-file-name 
+                 (setq proc (start-process "Shell" buffer shell-file-name
                                            shell-command-switch command))
                  (setq mode-line-process '(":%s"))
                  (require 'shell) (shell-mode)
                  (set-process-sentinel proc 'shell-command-sentinel)
                  ))
-           (shell-command-on-region (point) (point) command output-buffer)
-           ))))))
+           (shell-command-on-region (point) (point) command
+                                    output-buffer nil error-buffer)))))))
 \f
 ;; We have a sentinel to prevent insertion of a termination message
 ;; in the buffer itself.
 (defun shell-command-sentinel (process signal)
   (if (memq (process-status process) '(exit signal))
-      (message "%s: %s." 
+      (message "%s: %s."
               (car (cdr (cdr (process-command process))))
               (substring signal 0 -1))))
 
-(defvar shell-command-on-region-default-error-buffer nil
-  "*Name of buffer that `shell-command-on-region' uses for stderr.
-This buffer is used when `shell-command-on-region' is run interactively.
-A nil value for this variable means that output to stderr and stdout
-will be intermixed in the output stream.")
-
 (defun shell-command-on-region (start end command
                                      &optional output-buffer replace
                                      error-buffer)
@@ -1230,9 +1258,8 @@ around it.
 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
 or buffer name to which to direct the command's standard error output.
 If it is nil, error output is mingled with regular output.
-In an interactive call, the variable
-`shell-command-on-region-default-error-buffer' specifies the value
-of ERROR-BUFFER."
+In an interactive call, the variable `shell-command-default-error-buffer'
+specifies the value of ERROR-BUFFER."
   (interactive (let ((string
                      ;; Do this before calling region-beginning
                      ;; and region-end, in case subprocess output
@@ -1246,17 +1273,18 @@ of ERROR-BUFFER."
                       string
                       current-prefix-arg
                       current-prefix-arg
-                      shell-command-on-region-default-error-buffer)))
+                      shell-command-default-error-buffer)))
   (let ((error-file
-        (if error-buffer 
-            (concat (file-name-directory temp-file-name-pattern)
-                    (make-temp-name "scor"))
+        (if error-buffer
+            (make-temp-file
+             (expand-file-name "scor"
+                               (or small-temporary-file-directory
+                                   temporary-file-directory)))
           nil))
        exit-status)
     (if (or replace
            (and output-buffer
-                (not (or (bufferp output-buffer) (stringp output-buffer))))
-           (equal (buffer-name (current-buffer)) "*Shell Command Output*"))
+                (not (or (bufferp output-buffer) (stringp output-buffer)))))
        ;; Replace specified region with output from command.
        (let ((swap (and replace (< start end))))
          ;; Don't muck with mark unless REPLACE says we should.
@@ -1288,7 +1316,7 @@ of ERROR-BUFFER."
                       (delete-region (point-min) (min start end))
                       (setq exit-status
                             (call-process-region (point-min) (point-max)
-                                                 shell-file-name t 
+                                                 shell-file-name t
                                                  (if error-file
                                                      (list t error-file)
                                                    t)
@@ -1306,7 +1334,7 @@ of ERROR-BUFFER."
                                             (list buffer error-file)
                                           buffer)
                                         nil shell-command-switch command)))
-         (setq success (zerop exit-status))
+         (setq success (and exit-status (equal 0 exit-status)))
          ;; Report the amount of output.
          (let ((lines (save-excursion
                         (set-buffer buffer)
@@ -1314,32 +1342,45 @@ of ERROR-BUFFER."
                             0
                           (count-lines (point-min) (point-max))))))
            (cond ((= lines 0)
-                  (if success
-                      (message "(Shell command %sed with no output)"
+                  (if (and error-file
+                           (< 0 (nth 7 (file-attributes error-file))))
+                      (message "(Shell command %sed with some error output)"
                                (if (equal 0 exit-status)
                                    "succeed"
-                                 "fail")))
+                                 "fail"))
+                    (message "(Shell command %sed with no output)"
+                             (if (equal 0 exit-status)
+                                 "succeed"
+                               "fail")))
                   (kill-buffer buffer))
-                 ((and success (= lines 1))
+                 ((= lines 1)
                   (message "%s"
                            (save-excursion
                              (set-buffer buffer)
                              (goto-char (point-min))
                              (buffer-substring (point)
                                                (progn (end-of-line) (point))))))
-                 (t 
+                 (t
                   (save-excursion
                     (set-buffer buffer)
                     (goto-char (point-min)))
                   (display-buffer buffer)))))))
-    (if (and error-file (file-exists-p error-file))
-       (save-excursion
-         (set-buffer (get-buffer-create error-buffer))
-         ;; Do no formatting while reading error file, for fear of looping.
-         (format-insert-file error-file nil)
-         (delete-file error-file)))
+    (when (and error-file (file-exists-p error-file))
+      (if (< 0 (nth 7 (file-attributes error-file)))
+         (with-current-buffer (get-buffer-create error-buffer)
+           (let ((pos-from-end (- (point-max) (point))))
+             (or (bobp)
+                 (insert "\f\n"))
+             ;; Do no formatting while reading error file,
+             ;; because that can run a shell command, and we
+             ;; don't want that to cause an infinite recursion.
+             (format-insert-file error-file nil)
+             ;; Put point after the inserted errors.
+             (goto-char (- (point-max) pos-from-end)))
+           (display-buffer (current-buffer))))
+      (delete-file error-file))
     exit-status))
-       
+
 (defun shell-command-to-string (command)
   "Execute shell command COMMAND and return its output as a string."
   (with-output-to-string
@@ -1387,186 +1428,67 @@ These commands include \\[set-mark-command] and \\[start-kbd-macro]."
   (setq universal-argument-num-events (length (this-command-keys)))
   (setq overriding-terminal-local-map universal-argument-map))
 
-;; A subsequent C-u means to multiply the factor by 4 if we've typed
-;; nothing but C-u's; otherwise it means to terminate the prefix arg.
-(defun universal-argument-more (arg)
-  (interactive "P")
-  (if (consp arg)
-      (setq prefix-arg (list (* 4 (car arg))))
-    (if (eq arg '-)
-       (setq prefix-arg (list -4))
-      (setq prefix-arg arg)
-      (setq overriding-terminal-local-map nil)))
-  (setq universal-argument-num-events (length (this-command-keys))))
-
-(defun negative-argument (arg)
-  "Begin a negative numeric argument for the next command.
-\\[universal-argument] following digits or minus sign ends the argument."
-  (interactive "P")
-  (cond ((integerp arg)
-        (setq prefix-arg (- arg)))
-       ((eq arg '-)
-        (setq prefix-arg nil))
-       (t
-        (setq prefix-arg '-)))
-  (setq universal-argument-num-events (length (this-command-keys)))
-  (setq overriding-terminal-local-map universal-argument-map))
-
-(defun digit-argument (arg)
-  "Part of the numeric argument for the next command.
-\\[universal-argument] following digits or minus sign ends the argument."
-  (interactive "P")
-  (let ((digit (- (logand last-command-char ?\177) ?0)))
-    (cond ((integerp arg)
-          (setq prefix-arg (+ (* arg 10)
-                              (if (< arg 0) (- digit) digit))))
-         ((eq arg '-)
-          ;; Treat -0 as just -, so that -01 will work.
-          (setq prefix-arg (if (zerop digit) '- (- digit))))
-         (t
-          (setq prefix-arg digit))))
-  (setq universal-argument-num-events (length (this-command-keys)))
-  (setq overriding-terminal-local-map universal-argument-map))
-
-;; For backward compatibility, minus with no modifiers is an ordinary
-;; command if digits have already been entered.
-(defun universal-argument-minus (arg)
-  (interactive "P")
-  (if (integerp arg)
-      (universal-argument-other-key arg)
-    (negative-argument arg)))
-
-;; Anything else terminates the argument and is left in the queue to be
-;; executed as a command.
-(defun universal-argument-other-key (arg)
-  (interactive "P")
-  (setq prefix-arg arg)
-  (let* ((key (this-command-keys))
-        (keylist (listify-key-sequence key)))
-    (setq unread-command-events
-         (append (nthcdr universal-argument-num-events keylist)
-                 unread-command-events)))
-  (reset-this-command-lengths)
-  (setq overriding-terminal-local-map nil))
-\f
-(defun forward-to-indentation (arg)
-  "Move forward ARG lines and position at first nonblank character."
-  (interactive "p")
-  (forward-line arg)
-  (skip-chars-forward " \t"))
-
-(defun backward-to-indentation (arg)
-  "Move backward ARG lines and position at first nonblank character."
-  (interactive "p")
-  (forward-line (- arg))
-  (skip-chars-forward " \t"))
-
-(defcustom kill-whole-line nil
-  "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
-  :type 'boolean
-  :group 'killing)
-
-(defun kill-line (&optional arg)
-  "Kill the rest of the current line; if no nonblanks there, kill thru newline.
-With prefix argument, kill that many lines from point.
-Negative arguments kill lines backward.
-
-When calling from a program, nil means \"no arg\",
-a number counts as a prefix arg.
-
-To kill a whole line, when point is not at the beginning, type \
-\\[beginning-of-line] \\[kill-line] \\[kill-line].
-
-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]."
-  (interactive "P")
-  (kill-region (point)
-              ;; It is better to move point to the other end of the kill
-              ;; before killing.  That way, in a read-only buffer, point
-              ;; moves across the text that is copied to the kill ring.
-              ;; The choice has no effect on undo now that undo records
-              ;; the value of point from before the command was run.
-              (progn
-                (if arg
-                    (forward-visible-line (prefix-numeric-value arg))
-                  (if (eobp)
-                      (signal 'end-of-buffer nil))
-                  (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
-                      (forward-visible-line 1)
-                    (end-of-visible-line)))
-                (point))))
-
-(defun forward-visible-line (arg)
-  "Move forward by ARG lines, ignoring currently invisible newlines only.
-If ARG is negative, move backward -ARG lines.
-If ARG is zero, move to the beginning of the current line."
-  (condition-case nil
-      (if (> arg 0)
-         (while (> arg 0)
-           (or (zerop (forward-line 1))
-               (signal 'end-of-buffer nil))
-           ;; If the following character is currently invisible,
-           ;; skip all characters with that same `invisible' property value,
-           ;; then find the next newline.
-           (while (and (not (eobp))
-                       (let ((prop
-                              (get-char-property (point) 'invisible)))
-                         (if (eq buffer-invisibility-spec t)
-                             prop
-                           (or (memq prop buffer-invisibility-spec)
-                               (assq prop buffer-invisibility-spec)))))
-             (goto-char
-              (if (get-text-property (point) 'invisible)
-                  (or (next-single-property-change (point) 'invisible)
-                      (point-max))
-                (next-overlay-change (point))))
-             (or (zerop (forward-line 1))
-                 (signal 'end-of-buffer nil)))
-           (setq arg (1- arg)))
-       (let ((first t))
-         (while (or first (< arg 0))
-           (if (zerop arg)
-               (beginning-of-line)
-             (or (zerop (forward-line -1))
-                 (signal 'beginning-of-buffer nil)))
-           (while (and (not (bobp))
-                       (let ((prop
-                              (get-char-property (1- (point)) 'invisible)))
-                         (if (eq buffer-invisibility-spec t)
-                             prop
-                           (or (memq prop buffer-invisibility-spec)
-                               (assq prop buffer-invisibility-spec)))))
-             (goto-char
-              (if (get-text-property (1- (point)) 'invisible)
-                  (or (previous-single-property-change (point) 'invisible)
-                      (point-min))
-                (previous-overlay-change (point))))
-             (or (zerop (forward-line -1))
-                 (signal 'beginning-of-buffer nil)))
-           (setq first nil)
-           (setq arg (1+ arg)))))
-    ((beginning-of-buffer end-of-buffer)
-     nil)))
+;; A subsequent C-u means to multiply the factor by 4 if we've typed
+;; nothing but C-u's; otherwise it means to terminate the prefix arg.
+(defun universal-argument-more (arg)
+  (interactive "P")
+  (if (consp arg)
+      (setq prefix-arg (list (* 4 (car arg))))
+    (if (eq arg '-)
+       (setq prefix-arg (list -4))
+      (setq prefix-arg arg)
+      (setq overriding-terminal-local-map nil)))
+  (setq universal-argument-num-events (length (this-command-keys))))
 
-(defun end-of-visible-line ()
-  "Move to end of current visible line."
-  (end-of-line)
-  ;; If the following character is currently invisible,
-  ;; skip all characters with that same `invisible' property value,
-  ;; then find the next newline.
-  (while (and (not (eobp))
-             (let ((prop
-                    (get-char-property (point) 'invisible)))
-               (if (eq buffer-invisibility-spec t)
-                   prop
-                 (or (memq prop buffer-invisibility-spec)
-                     (assq prop buffer-invisibility-spec)))))
-    (if (get-text-property (point) 'invisible)
-       (goto-char (next-single-property-change (point) 'invisible))
-      (goto-char (next-overlay-change (point))))
-    (end-of-line)))
+(defun negative-argument (arg)
+  "Begin a negative numeric argument for the next command.
+\\[universal-argument] following digits or minus sign ends the argument."
+  (interactive "P")
+  (cond ((integerp arg)
+        (setq prefix-arg (- arg)))
+       ((eq arg '-)
+        (setq prefix-arg nil))
+       (t
+        (setq prefix-arg '-)))
+  (setq universal-argument-num-events (length (this-command-keys)))
+  (setq overriding-terminal-local-map universal-argument-map))
+
+(defun digit-argument (arg)
+  "Part of the numeric argument for the next command.
+\\[universal-argument] following digits or minus sign ends the argument."
+  (interactive "P")
+  (let ((digit (- (logand last-command-char ?\177) ?0)))
+    (cond ((integerp arg)
+          (setq prefix-arg (+ (* arg 10)
+                              (if (< arg 0) (- digit) digit))))
+         ((eq arg '-)
+          ;; Treat -0 as just -, so that -01 will work.
+          (setq prefix-arg (if (zerop digit) '- (- digit))))
+         (t
+          (setq prefix-arg digit))))
+  (setq universal-argument-num-events (length (this-command-keys)))
+  (setq overriding-terminal-local-map universal-argument-map))
+
+;; For backward compatibility, minus with no modifiers is an ordinary
+;; command if digits have already been entered.
+(defun universal-argument-minus (arg)
+  (interactive "P")
+  (if (integerp arg)
+      (universal-argument-other-key arg)
+    (negative-argument arg)))
+
+;; Anything else terminates the argument and is left in the queue to be
+;; executed as a command.
+(defun universal-argument-other-key (arg)
+  (interactive "P")
+  (setq prefix-arg arg)
+  (let* ((key (this-command-keys))
+        (keylist (listify-key-sequence key)))
+    (setq unread-command-events
+         (append (nthcdr universal-argument-num-events keylist)
+                 unread-command-events)))
+  (reset-this-command-lengths)
+  (setq overriding-terminal-local-map nil))
 \f
 ;;;; Window system cut and paste hooks.
 
@@ -1658,7 +1580,7 @@ it."
 If N is zero, `interprogram-paste-function' is set, and calling it
 returns a string, then that string is added to the front of the
 kill ring and returned as the latest kill.
-If optional arg DO-NOT-MOVE is non-nil, then don't actually move the 
+If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
 yanking point; just return the Nth kill forward."
   (let ((interprogram-paste (and (= n 0)
                                 interprogram-paste-function
@@ -1709,7 +1631,7 @@ Any command that calls this function is a \"kill command\".
 If the previous command was also a kill command,
 the text killed this time appends to the text killed last time
 to make one entry in the kill ring."
-  (interactive "r")
+  (interactive "*r")
   (condition-case nil
       ;; Don't let the undo list be truncated before we can even access it.
       (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))
@@ -1764,7 +1686,7 @@ system cut and paste."
   (if (eq last-command 'kill-region)
       (kill-append (buffer-substring beg end) (< end beg))
     (kill-new (buffer-substring beg end)))
-  (if transient-mark-mode 
+  (if transient-mark-mode
       (setq deactivate-mark t))
   nil)
 
@@ -1806,14 +1728,18 @@ visual feedback indicating the extent of the region being copied."
              (message "Saved text from \"%s\""
                      (substring killed-text 0 message-len))))))))
 
-(defun append-next-kill ()
-  "Cause following command, if it kills, to append to previous kill."
-  (interactive)
-  (if (interactive-p)
+(defun append-next-kill (&optional interactive)
+  "Cause following command, if it kills, to append to previous kill.
+The argument is used for internal purposes; do not supply one."
+  (interactive "p")
+  ;; We don't use (interactive-p), since that breaks kbd macros.
+  (if interactive
       (progn
        (setq this-command 'kill-region)
        (message "If the next command is a kill, it will append"))
     (setq last-command 'kill-region)))
+\f
+;; Yanking.
 
 (defun yank-pop (arg)
   "Replace just-yanked stretch of killed text with a different stretch.
@@ -1883,7 +1809,178 @@ See also the command \\[yank-pop]."
 With argument, rotate that many kills forward (or backward, if negative)."
   (interactive "p")
   (current-kill arg))
+\f
+;; Some kill commands.
+
+;; Internal subroutine of delete-char
+(defun kill-forward-chars (arg)
+  (if (listp arg) (setq arg (car arg)))
+  (if (eq arg '-) (setq arg -1))
+  (kill-region (point) (forward-point arg)))
+
+;; Internal subroutine of backward-delete-char
+(defun kill-backward-chars (arg)
+  (if (listp arg) (setq arg (car arg)))
+  (if (eq arg '-) (setq arg -1))
+  (kill-region (point) (forward-point (- arg))))
+
+(defcustom backward-delete-char-untabify-method 'untabify
+  "*The method for untabifying when deleting backward.
+Can be `untabify' -- turn a tab to many spaces, then delete one space;
+       `hungry' -- delete all whitespace, both tabs and spaces;
+       `all' -- delete all whitespace, including tabs, spaces and newlines;
+       nil -- just delete one character."
+  :type '(choice (const untabify) (const hungry) (const all) (const nil))
+  :group 'killing)
+
+(defun backward-delete-char-untabify (arg &optional killp)
+  "Delete characters backward, changing tabs into spaces.
+The exact behavior depends on `backward-delete-char-untabify-method'.
+Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
+Interactively, ARG is the prefix arg (default 1)
+and KILLP is t if a prefix arg was specified."
+  (interactive "*p\nP")
+  (when (eq backward-delete-char-untabify-method 'untabify)
+    (let ((count arg))
+      (save-excursion
+       (while (and (> count 0) (not (bobp)))
+         (if (= (preceding-char) ?\t)
+             (let ((col (current-column)))
+               (forward-char -1)
+               (setq col (- col (current-column)))
+               (insert-char ?\ col)
+               (delete-char 1)))
+         (forward-char -1)
+         (setq count (1- count))))))
+  (delete-backward-char
+   (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t")
+                     ((eq backward-delete-char-untabify-method 'all)
+                      " \t\n\r"))))
+     (if skip
+         (let ((wh (- (point) (save-excursion (skip-chars-backward skip)
+                                           (point)))))
+        (+ arg (if (zerop wh) 0 (1- wh))))
+         arg))
+   killp))
+
+(defun zap-to-char (arg char)
+  "Kill up to and including ARG'th occurrence of CHAR.
+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: ")
+  (kill-region (point) (progn
+                        (search-forward (char-to-string char) nil nil arg)
+;                       (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
+                        (point))))
+\f
+;; kill-line and its subroutines.
+
+(defcustom kill-whole-line nil
+  "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
+  :type 'boolean
+  :group 'killing)
+
+(defun kill-line (&optional arg)
+  "Kill the rest of the current line; if no nonblanks there, kill thru newline.
+With prefix argument, kill that many lines from point.
+Negative arguments kill lines backward.
+
+When calling from a program, nil means \"no arg\",
+a number counts as a prefix arg.
+
+To kill a whole line, when point is not at the beginning, type \
+\\[beginning-of-line] \\[kill-line] \\[kill-line].
+
+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]."
+  (interactive "*P")
+  (kill-region (point)
+              ;; It is better to move point to the other end of the kill
+              ;; before killing.  That way, in a read-only buffer, point
+              ;; moves across the text that is copied to the kill ring.
+              ;; The choice has no effect on undo now that undo records
+              ;; the value of point from before the command was run.
+              (progn
+                (if arg
+                    (forward-visible-line (prefix-numeric-value arg))
+                  (if (eobp)
+                      (signal 'end-of-buffer nil))
+                  (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
+                      (forward-visible-line 1)
+                    (end-of-visible-line)))
+                (point))))
+
+(defun forward-visible-line (arg)
+  "Move forward by ARG lines, ignoring currently invisible newlines only.
+If ARG is negative, move backward -ARG lines.
+If ARG is zero, move to the beginning of the current line."
+  (condition-case nil
+      (if (> arg 0)
+         (while (> arg 0)
+           (or (zerop (forward-line 1))
+               (signal 'end-of-buffer nil))
+           ;; If the following character is currently invisible,
+           ;; skip all characters with that same `invisible' property value,
+           ;; then find the next newline.
+           (while (and (not (eobp))
+                       (let ((prop
+                              (get-char-property (point) 'invisible)))
+                         (if (eq buffer-invisibility-spec t)
+                             prop
+                           (or (memq prop buffer-invisibility-spec)
+                               (assq prop buffer-invisibility-spec)))))
+             (goto-char
+              (if (get-text-property (point) 'invisible)
+                  (or (next-single-property-change (point) 'invisible)
+                      (point-max))
+                (next-overlay-change (point))))
+             (or (zerop (forward-line 1))
+                 (signal 'end-of-buffer nil)))
+           (setq arg (1- arg)))
+       (let ((first t))
+         (while (or first (< arg 0))
+           (if (zerop arg)
+               (beginning-of-line)
+             (or (zerop (forward-line -1))
+                 (signal 'beginning-of-buffer nil)))
+           (while (and (not (bobp))
+                       (let ((prop
+                              (get-char-property (1- (point)) 'invisible)))
+                         (if (eq buffer-invisibility-spec t)
+                             prop
+                           (or (memq prop buffer-invisibility-spec)
+                               (assq prop buffer-invisibility-spec)))))
+             (goto-char
+              (if (get-text-property (1- (point)) 'invisible)
+                  (or (previous-single-property-change (point) 'invisible)
+                      (point-min))
+                (previous-overlay-change (point))))
+             (or (zerop (forward-line -1))
+                 (signal 'beginning-of-buffer nil)))
+           (setq first nil)
+           (setq arg (1+ arg)))))
+    ((beginning-of-buffer end-of-buffer)
+     nil)))
 
+(defun end-of-visible-line ()
+  "Move to end of current visible line."
+  (end-of-line)
+  ;; If the following character is currently invisible,
+  ;; skip all characters with that same `invisible' property value,
+  ;; then find the next newline.
+  (while (and (not (eobp))
+             (let ((prop
+                    (get-char-property (point) 'invisible)))
+               (if (eq buffer-invisibility-spec t)
+                   prop
+                 (or (memq prop buffer-invisibility-spec)
+                     (assq prop buffer-invisibility-spec)))))
+    (if (get-text-property (point) 'invisible)
+       (goto-char (next-single-property-change (point) 'invisible))
+      (goto-char (next-overlay-change (point))))
+    (end-of-line)))
 \f
 (defun insert-buffer (buffer)
   "Insert after point the contents of BUFFER.
@@ -1923,6 +2020,7 @@ START and END specify the portion of the current buffer to be copied."
   (let ((oldbuf (current-buffer)))
     (save-excursion
       (set-buffer (get-buffer-create buffer))
+      (barf-if-buffer-read-only)
       (insert-buffer-substring oldbuf start end))))
 
 (defun prepend-to-buffer (buffer start end)
@@ -1936,6 +2034,7 @@ START and END specify the portion of the current buffer to be copied."
   (let ((oldbuf (current-buffer)))
     (save-excursion
       (set-buffer (get-buffer-create buffer))
+      (barf-if-buffer-read-only)
       (save-excursion
        (insert-buffer-substring oldbuf start end)))))
 
@@ -1950,6 +2049,7 @@ START and END specify the portion of the current buffer to be copied."
   (let ((oldbuf (current-buffer)))
     (save-excursion
       (set-buffer (get-buffer-create buffer))
+      (barf-if-buffer-read-only)
       (erase-buffer)
       (save-excursion
        (insert-buffer-substring oldbuf start end)))))
@@ -2210,7 +2310,7 @@ to use and more reliable (no dependence on goal column, etc.)."
        ((beginning-of-buffer end-of-buffer) (ding)))
     (line-move (- arg)))
   nil)
-
+\f
 (defcustom track-eol nil
   "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
 This means moving to the end of each line moved onto.
@@ -2330,7 +2430,7 @@ Outline mode sets this."
       ;; with intangibility and point-motion hooks enabled this time.
       (goto-char opoint)
       (setq inhibit-point-motion-hooks nil)
-      (goto-char new)
+      (goto-char (constrain-to-field new opoint t t))
       ;; 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))
@@ -2345,7 +2445,7 @@ Outline mode sets this."
            (setq new (point)))
        (goto-char (point-min))
        (setq inhibit-point-motion-hooks nil)
-       (goto-char new)
+       (goto-char (constrain-to-field new opoint t t))
        )))
   nil)
 
@@ -2371,112 +2471,6 @@ The goal column is stored in the variable `goal-column'."
             goal-column))
   nil)
 \f
-;;; Partial support for horizontal autoscrolling.  Someday, this feature
-;;; will be built into the C level and all the (hscroll-point-visible) calls
-;;; will go away.
-
-(defcustom hscroll-step 0
-   "*The number of columns to try scrolling a window by when point moves out.
-If that fails to bring point back on frame, point is centered instead.
-If this is zero, point is always centered after it moves off frame."
-   :type '(choice (const :tag "Alway Center" 0)
-                 (integer :format "%v" 1))
-   :group 'editing-basics)
-
-(defun hscroll-point-visible ()
-  "Scrolls the selected window horizontally to make point visible."
-  (save-excursion
-    (set-buffer (window-buffer))
-    (if (not (or truncate-lines
-                (> (window-hscroll) 0)
-                (and truncate-partial-width-windows
-                     (< (window-width) (frame-width)))))
-       ;; Point is always visible when lines are wrapped.
-       ()
-      ;; If point is on the invisible part of the line before window-start,
-      ;; then hscrolling can't bring it back, so reset window-start first.
-      (and (< (point) (window-start))
-          (let ((ws-bol (save-excursion
-                          (goto-char (window-start))
-                          (beginning-of-line)
-                          (point))))
-            (and (>= (point) ws-bol)
-                 (set-window-start nil ws-bol))))
-      (let* ((here (hscroll-window-column))
-            (left (min (window-hscroll) 1))
-            (right (1- (window-width))))
-       ;; Allow for the truncation glyph, if we're not exactly at eol.
-       (if (not (and (= here right)
-                     (= (following-char) ?\n)))
-           (setq right (1- right)))
-       (cond
-        ;; If too far away, just recenter.  But don't show too much
-        ;; white space off the end of the line.
-        ((or (< here (- left  hscroll-step))
-             (> here (+ right hscroll-step)))
-         (let ((eol (save-excursion (end-of-line) (hscroll-window-column))))
-           (scroll-left (min (- here (/ (window-width) 2))
-                             (- eol (window-width) -5)))))
-        ;; Within range.  Scroll by one step (or maybe not at all).
-        ((< here left)
-         (scroll-right hscroll-step))
-        ((> here right)
-         (scroll-left hscroll-step)))))))
-
-;; This function returns the window's idea of the display column of point,
-;; assuming that the window is already known to be truncated rather than
-;; wrapped, and that we've already handled the case where point is on the
-;; part of the line before window-start.  We ignore window-width; if point
-;; is beyond the right margin, we want to know how far.  The return value
-;; includes the effects of window-hscroll, window-start, and the prompt
-;; string in the minibuffer.  It may be negative due to hscroll.
-(defun hscroll-window-column ()
-  (let* ((hscroll (window-hscroll))
-        (startpos (save-excursion
-                    (beginning-of-line)
-                    (if (= (point) (save-excursion
-                                     (goto-char (window-start))
-                                     (beginning-of-line)
-                                     (point)))
-                        (goto-char (window-start)))
-                    (point)))
-        (hpos (+ (if (and (eq (selected-window) (minibuffer-window))
-                          (= 1 (window-start))
-                          (= startpos (point-min)))
-                     (minibuffer-prompt-width)
-                   0)
-                 (min 0 (- 1 hscroll))))
-        val)
-    (car (cdr (compute-motion startpos (cons hpos 0)
-                             (point) (cons 0 1)
-                             1000000 (cons hscroll 0) nil)))))
-
-  
-;; rms: (1) The definitions of arrow keys should not simply restate
-;; what keys they are.  The arrow keys should run the ordinary commands.
-;; (2) The arrow keys are just one of many common ways of moving point
-;; within a line.  Real horizontal autoscrolling would be a good feature,
-;; but supporting it only for arrow keys is too incomplete to be desirable.
-
-;;;;; Make arrow keys do the right thing for improved terminal support
-;;;;; When we implement true horizontal autoscrolling, right-arrow and
-;;;;; left-arrow can lose the (if truncate-lines ...) clause and become
-;;;;; aliases.  These functions are bound to the corresponding keyboard
-;;;;; events in loaddefs.el.
-
-;;(defun right-arrow (arg)
-;;  "Move right one character on the screen (with prefix ARG, that many chars).
-;;Scroll right if needed to keep point horizontally onscreen."
-;;  (interactive "P")
-;;  (forward-char arg)
-;;  (hscroll-point-visible))
-
-;;(defun left-arrow (arg)
-;;  "Move left one character on the screen (with prefix ARG, that many chars).
-;;Scroll left if needed to keep point horizontally onscreen."
-;;  (interactive "P")
-;;  (backward-char arg)
-;;  (hscroll-point-visible))
 
 (defun scroll-other-window-down (lines)
   "Scroll the \"other window\" down.
@@ -2728,7 +2722,7 @@ If nil, use `comment-end' instead."
             (delete-region (point) begpos)
             (indent-to indent))
           ;; An existing comment?
-          (if cpos 
+          (if cpos
               (progn (goto-char cpos)
                      (set-marker cpos nil))
             ;; No, insert one.
@@ -2804,20 +2798,26 @@ With just C-u prefix arg, uncomment each line in region.
 Numeric prefix arg ARG means use ARG comment characters.
 If ARG is negative, delete that many comment characters instead.
 Comments are terminated on each line, even for syntax in which newline does
-not end the comment.  Blank lines do not get comments."
+not end the comment.  Blank lines do not get comments.
+
+The strings used as comment starts are build from
+`comment-start' without trailing spaces and `comment-padding'."
   ;; if someone wants it to only put a comment-start at the beginning and
   ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
   ;; is easy enough.  No option is made here for other than commenting
   ;; every line.
-  (interactive "r\nP")
+  (interactive "*r\nP")
   (or comment-start (error "No comment syntax is defined"))
   (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
   (save-excursion
     (save-restriction
-      (let ((cs comment-start) (ce comment-end)
-           (cp (when comment-padding
-                 (make-string comment-padding ? )))
-           numarg)
+      (let* ((comment-start
+             (substring comment-start 0
+                        (string-match "[ \t]*$" comment-start)))
+            (cs comment-start) (ce comment-end)
+            (cp (when comment-padding
+                  (make-string comment-padding ? )))
+            numarg)
        (if (consp arg) (setq numarg t)
          (setq numarg (prefix-numeric-value arg))
          ;; For positive arg > 1, replicate the comment delims now,
@@ -2904,13 +2904,13 @@ In programs, it is faster to call `forward-word' with negative arg."
 (defun kill-word (arg)
   "Kill characters forward until encountering the end of a word.
 With argument, do this that many times."
-  (interactive "p")
+  (interactive "*p")
   (kill-region (point) (progn (forward-word arg) (point))))
 
 (defun backward-kill-word (arg)
   "Kill characters backward until encountering the end of a word.
 With argument, do this that many times."
-  (interactive "p")
+  (interactive "*p")
   (kill-word (- arg)))
 
 (defun current-word (&optional strict)
@@ -2981,7 +2981,7 @@ Setting this variable automatically makes it local to the current buffer.")
            (null (setq fc (current-fill-column)))
            (and (eq justify 'left)
                 (<= (current-column) fc))
-           (save-excursion (beginning-of-line) 
+           (save-excursion (beginning-of-line)
                            (setq bol (point))
                            (and auto-fill-inhibit-regexp
                                 (looking-at auto-fill-inhibit-regexp))))
@@ -3095,7 +3095,7 @@ Setting this variable automatically makes it local to the current buffer.")
                    (funcall comment-line-break-function t)))
                ;; Now do justification, if required
                (if (not (eq justify 'left))
-                   (save-excursion 
+                   (save-excursion
                      (end-of-line 0)
                      (justify-current-line justify nil t)))
                ;; If making the new line didn't reduce the hpos of
@@ -3107,7 +3107,7 @@ Setting this variable automatically makes it local to the current buffer.")
            (setq give-up t))))
       ;; Justify last line.
       (justify-current-line justify t t)
-      t))) 
+      t)))
 
 (defvar normal-auto-fill-function 'do-auto-fill
   "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
@@ -3172,7 +3172,7 @@ If you want to continue one comment across several lines, use \\[newline-and-ind
 If a fill column is specified, it overrides the use of the comment column
 or comment indentation.
 
-The inserted newline is marked hard if `use-hard-newlines' is true, 
+The inserted newline is marked hard if `use-hard-newlines' is true,
 unless optional argument SOFT is non-nil."
   (interactive)
   (let (comcol comstart)
@@ -3188,10 +3188,16 @@ unless optional argument SOFT is non-nil."
       (if (not comment-multi-line)
          (save-excursion
            (if (and comment-start-skip
-                    (let ((opoint (point)))
+                    (let ((opoint (1- (point)))
+                          inside)
                       (forward-line -1)
-                      (re-search-forward comment-start-skip opoint t)))
-               ;; The old line is a comment.
+                      ;; Determine (more or less) whether
+                      ;; target position is inside a comment.
+                      (while (and (re-search-forward comment-start-skip opoint t)
+                                  (not (setq inside (or (equal comment-end "")
+                                                        (not (search-forward comment-end opoint t)))))))
+                      inside))
+               ;; The old line has a comment and point was inside the comment.
                ;; Set WIN to the pos of the comment-start.
                ;; But if the comment is empty, look at preceding lines
                ;; to find one that has a nonempty comment.
@@ -3494,6 +3500,24 @@ or go back to just one window (by deleting all but the selected window)."
         (bury-buffer))))
 
 (define-key global-map "\e\e\e" 'keyboard-escape-quit)
+
+(defcustom input-mode-8-bit t
+  "Control acceptance of 8-bit keyboard input.
+This may be useful for inputting non-ASCII characters if your keyboard
+can generate them.  It is not necessary to change this under a window
+system which can distinguish 8-bit characters and Meta keys.
+Setting this variable directly does not take effect;
+use either M-x customize or the function `set-input-mode'."
+  :set (lambda (symbol value)
+        (let ((mode (current-input-mode)))
+          (set-input-mode (nth 0 mode) (nth 1 mode) value)))
+  :initialize 'custom-initialize-default
+  :type '(choice (const :tag "8-bit input for a Meta key" t)
+                (const :tag "Direct 8-bit character input" 0)
+                (const :tag "Assume top bit is parity and ignore" nil))
+  :version "21.1"
+  :link '(custom-manual "Single-Byte European Support")
+  :group 'keyboard)
 \f
 (defcustom mail-user-agent 'sendmail-user-agent
   "*Your preference for a mail composition package.
@@ -3556,28 +3580,6 @@ The properties used on SYMBOL are `composefunc', `sendfunc',
   (put symbol 'abortfunc (or abortfunc 'kill-buffer))
   (put symbol 'hookvar (or hookvar 'mail-send-hook)))
 
-(defun assoc-ignore-case (key alist)
-  "Like `assoc', but ignores differences in case and text representation.
-KEY must be a string.  Upper-case and lower-case letters are treated as equal.
-Unibyte strings are converted to multibyte for comparison."
-  (let (element)
-    (while (and alist (not element))
-      (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
-         (setq element (car alist)))
-      (setq alist (cdr alist)))
-    element))
-
-(defun assoc-ignore-representation (key alist)
-  "Like `assoc', but ignores differences in text representation.
-KEY must be a string.  
-Unibyte strings are converted to multibyte for comparison."
-  (let (element)
-    (while (and alist (not element))
-      (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
-         (setq element (car alist)))
-      (setq alist (cdr alist)))
-    element))
-
 (define-mail-user-agent 'sendmail-user-agent
   'sendmail-user-agent-compose
   'mail-send-and-exit)
@@ -3606,7 +3608,8 @@ Unibyte strings are converted to multibyte for comparison."
     (save-excursion
       (rfc822-goto-eoh)
       (while other-headers
-       (if (not (member (car (car other-headers)) '("in-reply-to" "cc")))
+       (if (not (assoc-ignore-case (car (car other-headers))
+                                   '(("in-reply-to") ("cc"))))
            (insert (car (car other-headers)) ": "
                    (cdr (car other-headers)) "\n"))
        (setq other-headers (cdr other-headers)))
@@ -3706,7 +3709,7 @@ in the definition is used to check that VALUE is valid."
       (require 'wid-edit)
       (setq type (widget-convert type))
       (unless (widget-apply type :match val)
-       (error "Value `%S' does not match type %S of %S" 
+       (error "Value `%S' does not match type %S of %S"
               val (car type) var))))
   (set var val))
 \f
@@ -3864,7 +3867,7 @@ With prefix argument N, move N items (negative N means move backward)."
           ;; If this is reading a file name, and the file name chosen
           ;; is a directory, don't exit the minibuffer.
           (if (and (eq minibuffer-completion-table 'read-file-name-internal)
-                   (file-directory-p (buffer-string)))
+                   (file-directory-p (field-string (point-max))))
               (select-window (active-minibuffer-window))
             (exit-minibuffer))))))
 
@@ -4039,4 +4042,121 @@ PREFIX is the string that represents this modifier in an event type symbol."
    (kp-divide ?/)
    (kp-equal ?=)))
 
+;;;;
+;;;; forking a twin copy of a buffer.
+;;;;
+
+(defvar clone-buffer-hook nil
+  "Normal hook to run in the new buffer at the end of `clone-buffer'.")
+
+(defun clone-process (process &optional newname)
+  "Create a twin copy of PROCESS.
+If NEWNAME is nil, it defaults to PROCESS' name;
+NEWNAME is modified by adding or incrementing <N> at the end as necessary.
+If PROCESS is associated with a buffer, the new process will be associated
+  with the current buffer instead.
+Returns nil if PROCESS has already terminated."
+  (setq newname (or newname (process-name process)))
+  (if (string-match "<[0-9]+>\\'" newname)
+      (setq newname (substring newname 0 (match-beginning 0))))
+  (when (memq (process-status process) '(run stop open))
+    (let* ((process-connection-type (process-tty-name process))
+          (old-kwoq (process-kill-without-query process nil))
+          (new-process
+           (if (memq (process-status process) '(open))
+               (apply 'open-network-stream newname
+                      (if (process-buffer process) (current-buffer))
+                      (process-contact process))
+             (apply 'start-process newname
+                    (if (process-buffer process) (current-buffer))
+                    (process-command process)))))
+      (process-kill-without-query new-process old-kwoq)
+      (process-kill-without-query process old-kwoq)
+      (set-process-inherit-coding-system-flag
+       new-process (process-inherit-coding-system-flag process))
+      (set-process-filter new-process (process-filter process))
+      (set-process-sentinel new-process (process-sentinel process))
+      new-process)))
+
+;; things to maybe add (currently partly covered by `funcall mode':
+;; - syntax-table
+;; - overlays
+(defun clone-buffer (&optional newname display-flag)
+  "Create a twin copy of the current buffer.
+If NEWNAME is nil, it defaults to the current buffer's name;
+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))
+  (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))
+  (setq newname (or newname (buffer-name)))
+  (if (string-match "<[0-9]+>\\'" newname)
+      (setq newname (substring newname 0 (match-beginning 0))))
+  (let ((buf (current-buffer))
+       (ptmin (point-min))
+       (ptmax (point-max))
+       (pt (point))
+       (mk (if mark-active (mark t)))
+       (modified (buffer-modified-p))
+       (mode major-mode)
+       (lvars (buffer-local-variables))
+       (process (get-buffer-process (current-buffer)))
+       (new (generate-new-buffer (or newname (buffer-name)))))
+    (save-restriction
+      (widen)
+      (with-current-buffer new
+       (insert-buffer-substring buf)))
+    (with-current-buffer new
+      (narrow-to-region ptmin ptmax)
+      (goto-char pt)
+      (if mk (set-mark mk))
+      (set-buffer-modified-p modified)
+
+      ;; Clone the old buffer's process, if any.
+      (when process (clone-process process))
+
+      ;; Now set up the major mode.
+      (funcall mode)
+
+      ;; Set up other local variables.
+      (mapcar (lambda (v)
+               (condition-case ()      ;in case var is read-only
+                   (if (symbolp v)
+                       (makunbound v)
+                     (set (make-local-variable (car v)) (cdr v)))
+                 (error nil)))
+             lvars)
+
+      ;; 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))
+    new))
+
+
+(defmacro with-syntax-table (table &rest body)
+  "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
+Current buffer and syntax table are saved, BODY is evaluated, and the
+saved values are restored, even in case of an abnormal exit.
+Value is what BODY returns."
+  (let ((old-table (gensym))
+       (old-buffer (gensym)))
+    '(let ((,old-table (syntax-table))
+          (,old-buffer (current-buffer)))
+       (unwind-protect
+          (progn
+            (set-syntax-table (copy-syntax-table ,table))
+            ,@body)
+        (set-buffer ,old-buffer)
+        (set-syntax-table ,old-table)))))
+
+(put 'with-syntax-table 'lisp-indent-function 1)
+(put 'with-syntax-table 'edebug-form-spec '(form body))
+
 ;;; simple.el ends here