* flow-fill.el: Docstring and message fixes.
[gnus] / lisp / deuglify.el
index e3a9bf3..244daa0 100644 (file)
@@ -1,7 +1,7 @@
 ;;; deuglify.el --- deuglify broken Outlook (Express) articles
 
-;; Copyright (C) 2002 Free Software Foundation, Inc.
-;; Copyright (C) 2001,2002 Raymond Scholz
+;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2002 Raymond Scholz
 
 ;; Author: Raymond Scholz <rscholz@zonix.de>
 ;;         Thomas Steffen (unwrapping algorithm,
@@ -47,7 +47,7 @@
 ;; > verb.  This sentence no verb.  This sentence no verb.  This
 ;; > sentence no verb.
 ;;
-;; The function `gnus-outlook-unwrap-lines' tries to recognize those
+;; The function `gnus-article-outlook-unwrap-lines' tries to recognize those
 ;; erroneously wrapped lines and will unwrap them.  I.e. putting the
 ;; wrapped parts ("no" in this example) back where they belong (at the
 ;; end of the cited line above).
@@ -70,7 +70,7 @@
 ;;
 ;; Unwrapping "You forgot in all your sentences." would be illegal as
 ;; this part wasn't intended to be cited text.
-;; `gnus-outlook-unwrap-lines' will only unwrap lines if the resulting
+;; `gnus-article-outlook-unwrap-lines' will only unwrap lines if the resulting
 ;; citation line will be of a certain maximum length.  You can control
 ;; this by adjusting `gnus-outlook-deuglify-unwrap-max'.  Also
 ;; unwrapping will only be done if the line above the (possibly)
 ;; > Bye, John
 ;;
 ;; Repairing the attribution line will be done by function
-;; `gnus-outlook-repair-attribution' which calls other function that
+;; `gnus-article-outlook-repair-attribution which calls other function that
 ;; try to recognize and repair broken attribution lines.  See variable
 ;; `gnus-outlook-deuglify-attrib-cut-regexp' for stuff that should be
 ;; cut off from the beginning of an attribution line and variable
 ;;
 ;; Rearranging the article so that the cited text appears above the
 ;; new text will be done by function
-;; `gnus-outlook-rearrange-citation'.  This function calls
-;; `gnus-outlook-repair-attribution' to find and repair an attribution
+;; `gnus-article-outlook-rearrange-citation'.  This function calls
+;; `gnus-article-outlook-repair-attribution to find and repair an attribution
 ;; line.
 ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; To automatically invoke deuglification on every article you read,
 ;; put something like that in your .gnus:
 ;;
-;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-unwrap-lines)
+;; (add-hook 'gnus-article-decode-hook 'gnus-article-outlook-unwrap-lines)
 ;;
 ;; or _one_ of the following lines:
 ;;
 ;; ;; repair broken attribution lines
-;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-repair-attribution)
+;; (add-hook 'gnus-article-decode-hook 'gnus-article-outlook-repair-attribution)
 ;;
 ;; ;; repair broken attribution lines and citations
-;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-rearrange-citation)
+;; (add-hook 'gnus-article-decode-hook 'gnus-article-outlook-rearrange-citation)
 ;;
 ;; Note that there always may be some false positives, so I suggest
 ;; using the manual invocation.  After deuglification you may want to
 ;; -----------
 ;;
 ;; As I said before there may (or will) be a few false positives on
-;; unwrapping cited lines with `gnus-outlook-unwrap-lines'.
+;; unwrapping cited lines with `gnus-article-outlook-unwrap-lines'.
 ;;
-;; `gnus-outlook-repair-attribution' will only fix the first
+;; `gnus-article-outlook-repair-attribution will only fix the first
 ;; attribution line found in the article.  Furthermore it fixed to
 ;; certain kinds of attributions.  And there may be horribly many
 ;; false positives, vanishing lines and so on -- so don't trust your
 ;; eyes.  Again I recommend manual invocation.
 ;;
-;; `gnus-outlook-rearrange-citation' carries all the limitations of
-;; `gnus-outlook-repair-attribution'.
+;; `gnus-article-outlook-rearrange-citation' carries all the limitations of
+;; `gnus-article-outlook-repair-attribution.
 ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;
 ;; Renamed `gnus-outlook-deuglify-article' to
 ;; `gnus-article-outlook-deuglify-article'.
 ;; Made it easier to deuglify the article while being in Gnus' Article
-;; Edit Mode. (suggested by Phil Nitschke)
+;; Edit Mode.  (suggested by Phil Nitschke)
 ;;
 ;;
 ;; Revision 1.3  2002/01/02 23:35:54  rscholz
 ;;; User Customizable Variables:
 
 (defgroup gnus-outlook-deuglify nil
-  "Deuglify articles generated by broken user agents like MS 
-Outlook (Express).")
+  "Deuglify articles generated by broken user agents like MS Outlook (Express).")
 
 ;;;###autoload
 (defcustom gnus-outlook-deuglify-unwrap-min 45
@@ -251,26 +250,23 @@ Outlook (Express).")
   :group 'gnus-outlook-deuglify)
 
 (defcustom gnus-outlook-deuglify-unwrap-stop-chars nil ;; ".?!" or nil
-  "Characters that inhibit unwrapping if they are the last one on the
-cited line above the possible wrapped line."
+  "Characters that inhibit unwrapping if they are the last one on the cited line above the possible wrapped line."
   :type 'string
   :group 'gnus-outlook-deuglify)
 
 (defcustom gnus-outlook-deuglify-no-wrap-chars "`"
-  "Characters that inhibit unwrapping if they are the first one in the
-possibly wrapped line."
+  "Characters that inhibit unwrapping if they are the first one in the possibly wrapped line."
   :type 'string
   :group 'gnus-outlook-deuglify)
 
 (defcustom  gnus-outlook-deuglify-attrib-cut-regexp
   "\\(On \\|Am \\)?\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),[^,]+, "
-  "Regular expression matching the beginning of an attribution line
-that should be cut off."
+  "Regular expression matching the beginning of an attribution line that should be cut off."
   :type 'string
   :group 'gnus-outlook-deuglify)
 
 (defcustom gnus-outlook-deuglify-attrib-verb-regexp
-  "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef"
+  "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef\\|escribió"
   "Regular expression matching the verb used in an attribution line."
   :type 'string
   :group 'gnus-outlook-deuglify)
@@ -281,18 +277,35 @@ that should be cut off."
   :type 'string
   :group 'gnus-outlook-deuglify)
 
+;;;###autoload
+(defcustom gnus-outlook-display-hook nil
+  "A hook called after an deuglified article has been prepared.
+It is run after `gnus-article-prepare-hook'."
+  :type 'hook
+  :group 'gnus-outlook-deuglify)
 
 ;; Functions
 
-;; TODO: don't kill MIME parts
+(defun gnus-outlook-display-article-buffer ()
+  "Redisplay current buffer or article buffer."
+  (with-current-buffer (or gnus-article-buffer (current-buffer))
+    ;; "Emulate" `gnus-article-prepare-display' without calling
+    ;; it. Calling `gnus-article-prepare-display' on an already
+    ;; prepared article removes all MIME parts.  I'm unsure whether
+    ;; this is a bug or not.
+    (gnus-article-highlight t)
+    (gnus-treat-article nil)
+    (gnus-run-hooks 'gnus-article-prepare-hook
+                   'gnus-outlook-display-hook)))
+
 ;;;###autoload
-(defun gnus-outlook-unwrap-lines ()
-  "Unwrap lines that appear to be wrapped citation lines.  You can
-control what lines will be unwrapped by frobbing
-`gnus-outlook-deuglify-unwrap-min' and
-`gnus-outlook-deuglify-unwrap-max', indicating the miminum and maximum
-length of an unwrapped citation line."
-  (interactive)
+(defun gnus-article-outlook-unwrap-lines (&optional nodisplay)
+  "Unwrap lines that appear to be wrapped citation lines.
+You can control what lines will be unwrapped by frobbing
+`gnus-outlook-deuglify-unwrap-min' and `gnus-outlook-deuglify-unwrap-max',
+indicating the minimum and maximum length of an unwrapped citation line.  If
+NODISPLAY is non-nil, don't redisplay the article buffer."
+  (interactive "P")
   (save-excursion
     (let ((case-fold-search nil)
          (inhibit-read-only t)
@@ -311,24 +324,31 @@ length of an unwrapped citation line."
              (len3 (- (match-end 3) (match-beginning 3))))
            (if (and (> len12 gnus-outlook-deuglify-unwrap-min)
                     (< (+ len12 len3) gnus-outlook-deuglify-unwrap-max))
-               (progn 
+               (progn
                  (replace-match "\\1\\2 \\3")
-                 (goto-char (match-beginning 0))))))))))
+                 (goto-char (match-beginning 0)))))))))
+  (unless nodisplay (gnus-outlook-display-article-buffer)))
 
-;; TODO: respect signatures, don't kill MIME parts
-(defun gnus-outlook-rearrange-article (from-where)
-  "Put the text from `from-where' to the end of buffer at the top of
-the article buffer."
+(defun gnus-outlook-rearrange-article (attr-start)
+  "Put the text from ATTR-START to the end of buffer at the top of the article buffer."
   (save-excursion
     (let ((inhibit-read-only t)
          (cite-marks gnus-outlook-deuglify-cite-marks))
       (gnus-with-article-buffer
-       (unless (search-forward-regexp
-                  (concat "^[ \t]*[^" cite-marks "\n]") nil t)
-         (kill-region from-where (point-max))
-         (article-goto-body)
-         (yank)
-         (insert "\n"))))))
+       (article-goto-body)
+       ;; article does not start with attribution
+       (unless (= (point) attr-start)
+         (gnus-kill-all-overlays)
+         (let ((cur (point))
+               ;; before signature or end of buffer
+               (to (if (gnus-article-search-signature)
+                       (point)
+                     (point-max))))
+           ;; handle the case where the full quote is below the
+           ;; signature
+           (if (< to attr-start)
+               (setq to (point-max)))
+           (transpose-regions cur attr-start attr-start to)))))))
 
 ;; John Doe <john.doe@some.domain> wrote in message
 ;; news:a87usw8$dklsssa$2@some.news.server...
@@ -348,6 +368,7 @@ the article buffer."
                     "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
             nil t)
            (progn
+             (gnus-kill-all-overlays)
              (replace-match "\\1\\2\\4")
              (match-beginning 0)))))))
 
@@ -367,14 +388,13 @@ the article buffer."
       (gnus-with-article-buffer
        (article-goto-body)
        (if (re-search-forward
-            (concat "^----* ?[^-]+ ?----*\n"
-                    "[^\n]+: \\([^\n]+\\)\n"
-                    "[^\n]+: [^\n]+\n"
-                    "[^\n]+: [^\n]+\n"
-                    "[^\n]+: [^\n]+$")
+            (concat "^[" cite-marks " \t]*--* ?[^-]+ [^-]+ ?--*\\s *\n"
+                    "[^\n:]+:[ \t]*\\([^\n]+\\)\n"
+                    "\\([^\n:]+:[ \t]*[^\n]+\n\\)+")
             nil t)
            (progn
-             (replace-match "\\1 wrote:")
+             (gnus-kill-all-overlays)
+             (replace-match "\\1 wrote:\n")
              (match-beginning 0)))))))
 
 ;; On Wed, 16 Jan 2002 23:23:30 +0100, John Doe <john.doe@some.domain> wrote:
@@ -394,43 +414,52 @@ the article buffer."
                     "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
             nil t)
            (progn
+             (gnus-kill-all-overlays)
              (replace-match "\\4 \\5\\6\\7")
              (match-beginning 0)))))))
 
 ;;;###autoload
-(defun gnus-outlook-repair-attribution ()
-  "Repair a broken attribution line."
-  (interactive)
-  (or
-   (gnus-outlook-repair-attribution-other)
-   (gnus-outlook-repair-attribution-block)
-   (gnus-outlook-repair-attribution-outlook)))
-
-(defun gnus-outlook-rearrange-citation ()
-  "Repair broken citations."
-  (let ((attrib-start (gnus-outlook-repair-attribution)))
+(defun gnus-article-outlook-repair-attribution (&optional nodisplay)
+  "Repair a broken attribution line.
+If NODISPLAY is non-nil, don't redisplay the article buffer."
+  (interactive "P")
+  (let ((attrib-start
+        (or
+         (gnus-outlook-repair-attribution-other)
+         (gnus-outlook-repair-attribution-block)
+         (gnus-outlook-repair-attribution-outlook))))
+    (unless nodisplay (gnus-outlook-display-article-buffer))
+    attrib-start))
+
+(defun gnus-article-outlook-rearrange-citation (&optional nodisplay)
+  "Repair broken citations.
+If NODISPLAY is non-nil, don't redisplay the article buffer."
+  (interactive "P")
+  (let ((attrib-start (gnus-article-outlook-repair-attribution 'nodisplay)))
     ;; rearrange citations if an attribution line has been recognized
     (if attrib-start
-       (gnus-outlook-rearrange-article attrib-start))))
+       (gnus-outlook-rearrange-article attrib-start)))
+  (unless nodisplay (gnus-outlook-display-article-buffer)))
 
 ;;;###autoload
-(defun gnus-outlook-deuglify-article ()
-  "Deuglify broken Outlook (Express) articles."
-  (interactive)
+(defun gnus-outlook-deuglify-article (&optional nodisplay)
+  "Full deuglify of broken Outlook (Express) articles.
+Treat dumbquotes, unwrap lines, repair attribution and rearrange citation.  If
+NODISPLAY is non-nil, don't redisplay the article buffer."
+  (interactive "P")
   ;; apply treatment of dumb quotes
   (gnus-article-treat-dumbquotes)
   ;; repair wrapped cited lines
-  (gnus-outlook-unwrap-lines)
-  ;; repair attribution line
-  (gnus-outlook-rearrange-citation))
+  (gnus-article-outlook-unwrap-lines 'nodisplay)
+  ;; repair attribution line and rearrange citation.
+  (gnus-article-outlook-rearrange-citation 'nodisplay)
+  (unless nodisplay (gnus-outlook-display-article-buffer)))
 
 ;;;###autoload
 (defun gnus-article-outlook-deuglify-article ()
   "Deuglify broken Outlook (Express) articles and redisplay."
   (interactive)
-  (gnus-outlook-deuglify-article)
-  (with-current-buffer (or gnus-article-buffer (current-buffer))
-    (gnus-article-prepare-display)))
+  (gnus-outlook-deuglify-article nil))
 
 (provide 'deuglify)