Add 2008 to copyright years.
[bpt/emacs.git] / lisp / textmodes / refill.el
index 8bf5a03..275bbae 100644 (file)
@@ -1,15 +1,17 @@
 ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
 
-;; Copyright (C) 2000 Free Software Foundation, Inc.
+;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Dave Love <fx@gnu.org>
+;; Maintainer: Miles Bader <miles@gnu.org>
 ;; Keywords: wp
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -19,8 +21,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 ;; some Emacs bug at the time.  ISTR maniac has problems with
 ;; whitespace at the end of paragraphs.]
 
+;;; Todo/Bugs:
+
+;; - When deleting the first word on a line, the space after that word tends
+;;   to become part of the fill-prefix, causing either wrong filling of the
+;;   remaining text, or causing the cursor to move unexpectedly.  Ex:
+;;   Start with
+;;                I>< blabla
+;;
+;;   and hit backspace.  We end up with
+;;
+;;                 ><blabla
+;;   instead of
+;;                >< blabla
+;;
+;;   Other example.  Start with
+;;
+;;     Foo bar blablabla asdgf
+;;     word>< asdfas dfasdfasd
+;;     asd asdfa sdfasd sdf
+;;
+;;   and hit M-backspace.  We end up with
+;;
+;;     Foo bar blablabla asdgf
+;;      ><asdfas dfasdfasd asd
+;;      asdfa sdfasd sdf
+
 ;;; Code:
 
+(eval-when-compile (require 'cl))
+
+(defgroup refill nil
+  "Refilling paragraphs on changes."
+  :group 'fill)
+
 (defvar refill-ignorable-overlay nil
   "Portion of the most recently filled paragraph not needing filling.
 This is used to optimize refilling.")
@@ -71,25 +105,28 @@ This is used to optimize refilling.")
       (forward-line -1)
       (if (<= (point) (overlay-start overlay))
          ;; Just get OVERLAY out of the way
-         (move-overlay overlay 1 1)
-       ;; Make overlay contain only the region 
+         (move-overlay overlay (point-min) (point-min))
+       ;; Make overlay contain only the region
        (move-overlay overlay (overlay-start overlay) (point))))))
 
 (defun refill-fill-paragraph-at (pos &optional arg)
   "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
-  (let (fill-pfx)
-    (save-excursion
-      (goto-char pos)
-      (forward-paragraph)
-      (skip-syntax-backward "-")
-      (let ((end (point))
-           (beg (progn (backward-paragraph) (point)))
-           (obeg (overlay-start refill-ignorable-overlay))
-           (oend (overlay-end refill-ignorable-overlay)))
+  (save-excursion
+    (goto-char pos)
+    ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
+    ;; leading to excessive refilling and wrong choice of fill-prefix.
+    ;; might be a bug in my paragraphs.el.
+    (forward-paragraph)
+    (skip-syntax-backward "-")
+    (let ((end (point))
+         (beg (progn (backward-paragraph) (point)))
+         (obeg (overlay-start refill-ignorable-overlay))
+         (oend (overlay-end refill-ignorable-overlay)))
+      (unless (> beg pos)      ;Don't fill if point is outside the paragraph.
        (goto-char pos)
        (if (and (>= beg obeg) (< beg oend))
            ;; Limit filling to the modified tail of the paragraph.
-           (let (;; When adaptive-fill-mode is enabled, the filling
+           (let ( ;; When adaptive-fill-mode is enabled, the filling
                  ;; functions will attempt to set the fill prefix from
                  ;; the fake paragraph bounds we pass in, so set it
                  ;; ourselves first, using the real paragraph bounds.
@@ -104,23 +141,20 @@ This is used to optimize refilling.")
                (if use-hard-newlines
                    (fill-region oend end arg)
                  (fill-region-as-paragraph oend end arg)))
-             (setq fill-pfx fill-prefix)
              (move-overlay refill-ignorable-overlay obeg (point)))
          ;; Fill the whole paragraph
-         (setq fill-pfx
-               (save-restriction
-                 (if use-hard-newlines
-                     (fill-region beg end arg)
-                   (fill-region-as-paragraph beg end arg))))
-         (move-overlay refill-ignorable-overlay beg (point)))))
-    (skip-line-prefix fill-pfx)))
+         (save-restriction
+           (if use-hard-newlines
+               (fill-region beg end arg)
+             (fill-region-as-paragraph beg end arg)))
+         (move-overlay refill-ignorable-overlay beg (point)))))))
 
 (defun refill-fill-paragraph (arg)
   "Like `fill-paragraph' but don't delete whitespace at paragraph end."
   (refill-fill-paragraph-at (point) arg))
 
 (defvar refill-doit nil
-  "Non-nil means that `refill-post-command-function' does its processing.
+  "Non-nil tells `refill-post-command-function' to do its processing.
 Set by `refill-after-change-function' in `after-change-functions' and
 unset by `refill-post-command-function' in `post-command-hook', and
 sometimes `refill-pre-command-function' in `pre-command-hook'.  This
@@ -138,40 +172,36 @@ complex processing.")
   "Post-command function to do refilling (conditionally)."
   (when refill-doit ; there was a change
     ;; There's probably scope for more special cases here...
-    (if (eq this-command 'self-insert-command)
-       ;; Treat self-insertion commands specially, since they don't
-       ;; always reset `refill-doit' -- for self-insertion commands that
-       ;; *don't* cause a refill, we want to leave it turned on so that
-       ;; any subsequent non-modification command will cause a refill.
-       (when (aref auto-fill-chars (char-before))
-         ;; Respond to the same characters as auto-fill (other than
-         ;; newline, covered below).
-         (refill-fill-paragraph-at refill-doit)
-         (setq refill-doit nil))
-      (cond
-       ((or (eq this-command 'quoted-insert)
-           (eq this-command 'fill-paragraph)
-           (eq this-command 'fill-region))
-       nil)
-       ((or (eq this-command 'newline)
-           (eq this-command 'newline-and-indent)
-           (eq this-command 'open-line))
-       ;; Don't zap what was just inserted.
-       (save-excursion
-         (beginning-of-line)           ; for newline-and-indent
-         (skip-chars-backward "\n")
-         (save-restriction
-           (narrow-to-region (point-min) (point))
-           (refill-fill-paragraph-at refill-doit)))
-       (widen)
-       (save-excursion
-         (skip-chars-forward "\n")
-         (save-restriction
-           (narrow-to-region (line-beginning-position) (point-max))
-           (refill-fill-paragraph-at refill-doit))))
-       (t
-       (refill-fill-paragraph-at refill-doit)))
-      (setq refill-doit nil))))
+    (case this-command
+      (self-insert-command
+       ;; Treat self-insertion commands specially, since they don't
+       ;; always reset `refill-doit' -- for self-insertion commands that
+       ;; *don't* cause a refill, we want to leave it turned on so that
+       ;; any subsequent non-modification command will cause a refill.
+       (when (aref auto-fill-chars (char-before))
+        ;; Respond to the same characters as auto-fill (other than
+        ;; newline, covered below).
+        (refill-fill-paragraph-at refill-doit)
+        (setq refill-doit nil)))
+      ((quoted-insert fill-paragraph fill-region) nil)
+      ((newline newline-and-indent open-line indent-new-comment-line
+       reindent-then-newline-and-indent)
+       ;; Don't zap what was just inserted.
+       (save-excursion
+        (beginning-of-line)            ; for newline-and-indent
+        (skip-chars-backward "\n")
+        (save-restriction
+          (narrow-to-region (point-min) (point))
+          (refill-fill-paragraph-at refill-doit)))
+       (widen)
+       (save-excursion
+        (skip-chars-forward "\n")
+        (save-restriction
+          (narrow-to-region (line-beginning-position) (point-max))
+          (refill-fill-paragraph-at refill-doit))))
+      (t
+       (refill-fill-paragraph-at refill-doit)))
+    (setq refill-doit nil)))
 
 (defun refill-pre-command-function ()
   "Pre-command function to do refilling (conditionally)."
@@ -182,28 +212,42 @@ complex processing.")
     (refill-fill-paragraph-at refill-doit)
     (setq refill-doit nil)))
 
-(defvar refill-late-fill-paragraph-function nil)
+(defvar refill-saved-state nil)
 
 ;;;###autoload
 (define-minor-mode refill-mode
   "Toggle Refill minor mode.
-With prefix arg, turn Refill mode on iff arg is positive.
+With prefix arg, turn Refill mode on if arg is positive, otherwise turn it off.
 
 When Refill mode is on, the current paragraph will be formatted when
 changes are made within it.  Self-inserting characters only cause
 refilling if they would cause auto-filling."
-  nil " Refill" nil
-  ;; This provides the test for recursive paragraph filling.
-  (make-local-variable 'fill-paragraph-function)
+  :group 'refill
+  :lighter " Refill"
+  :keymap '(("\177" . backward-delete-char-untabify))
+  ;; Remove old state if necessary
+  (when refill-ignorable-overlay
+    (delete-overlay refill-ignorable-overlay)
+    (kill-local-variable 'refill-ignorable-overlay))
+  (when (local-variable-p 'refill-saved-state)
+    (dolist (x refill-saved-state)
+      (set (make-local-variable (car x)) (cdr x)))
+    (kill-local-variable 'refill-saved-state))
   (if refill-mode
       (progn
        (add-hook 'after-change-functions 'refill-after-change-function nil t)
        (add-hook 'post-command-hook 'refill-post-command-function nil t)
        (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
-       (set (make-local-variable 'refill-late-fill-paragraph-function)
-            fill-paragraph-function)
+       (set (make-local-variable 'refill-saved-state)
+            (mapcar (lambda (s) (cons s (symbol-value s)))
+                    '(fill-paragraph-function auto-fill-function)))
+       ;; This provides the test for recursive paragraph filling.
        (set (make-local-variable 'fill-paragraph-function)
             'refill-fill-paragraph)
+       ;; When using justification, doing DEL on 2 spaces should remove
+       ;; both, otherwise, the subsequent refill will undo the DEL.
+       (set (make-local-variable 'backward-delete-char-untabify-method)
+            'hungry)
        (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
        (overlay-put refill-ignorable-overlay 'modification-hooks
                     '(refill-adjust-ignorable-overlay))
@@ -212,9 +256,9 @@ refilling if they would cause auto-filling."
        (auto-fill-mode 0))
     (remove-hook 'after-change-functions 'refill-after-change-function t)
     (remove-hook 'post-command-hook 'refill-post-command-function t)
-    (delete-overlay refill-ignorable-overlay)
-    (setq fill-paragraph-function refill-late-fill-paragraph-function)))
+    (kill-local-variable 'backward-delete-char-untabify-method)))
 
 (provide 'refill)
 
+;; arch-tag: 2c4ce9e8-1daa-4a3b-b6f8-fd6ac5bf6138
 ;;; refill.el ends here