1 ;;; deuglify.el --- deuglify broken Outlook (Express) articles
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
4 ;; Copyright (C) 2001,2002 Raymond Scholz
6 ;; Author: Raymond Scholz <rscholz@zonix.de>
7 ;; Thomas Steffen (unwrapping algorithm,
8 ;; based on an idea of Stefan Monnier)
9 ;; Keywords: mail, news
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This file enables Gnus to repair broken citations produced by
31 ;; common user agents like MS Outlook (Express). It may repair
32 ;; articles of other user agents too.
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; Outlook sometimes wraps cited lines before sending a message as
38 ;; seen in this example:
45 ;; > This sentence no verb. This sentence no verb. This sentence
47 ;; > verb. This sentence no verb. This sentence no verb. This
48 ;; > sentence no verb.
50 ;; The function `gnus-outlook-unwrap-lines' tries to recognize those
51 ;; erroneously wrapped lines and will unwrap them. I.e. putting the
52 ;; wrapped parts ("no" in this example) back where they belong (at the
53 ;; end of the cited line above).
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 ;; Note that some people not only use broken user agents but also
58 ;; practice a bad citation style by omitting blank lines between the
59 ;; cited text and their own text.
66 ;; > This sentence no verb. This sentence no verb. This sentence no
67 ;; You forgot in all your sentences.
68 ;; > verb. This sentence no verb. This sentence no verb. This
69 ;; > sentence no verb.
71 ;; Unwrapping "You forgot in all your sentences." would be illegal as
72 ;; this part wasn't intended to be cited text.
73 ;; `gnus-outlook-unwrap-lines' will only unwrap lines if the resulting
74 ;; citation line will be of a certain maximum length. You can control
75 ;; this by adjusting `gnus-outlook-deuglify-unwrap-max'. Also
76 ;; unwrapping will only be done if the line above the (possibly)
77 ;; wrapped line has a minimum length of `gnus-outlook-deuglify-unwrap-min'.
79 ;; Furthermore no unwrapping will be undertaken if the last character
80 ;; is one of the chars specified in
81 ;; `gnus-outlook-deuglify-unwrap-stop-chars'. Setting this to ".?!"
82 ;; inhibits unwrapping if the cited line ends with a full stop,
83 ;; question mark or exclamation mark. Note that this variable
84 ;; defaults to `nil', triggering a few false positives but generally
85 ;; giving you better results.
87 ;; Unwrapping works on every level of citation. Thus you will be able
88 ;; repair broken citations of broken user agents citing broken
89 ;; citations of broken user agents citing broken citations...
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;; Citations are commonly introduced with an attribution line
94 ;; indicating who wrote the cited text. Outlook adds superfluous
95 ;; information that can be found in the header of the message to this
96 ;; line and often wraps it.
98 ;; If that weren't enough, lots of people write their own text above
99 ;; the cited text and cite the complete original article below.
104 ;; Hey, John. There's no in all your sentences!
106 ;; John Doe <john.doe@some.domain> wrote in message
107 ;; news:a87usw8$dklsssa$2@some.news.server...
108 ;; > This sentence no verb. This sentence no verb. This sentence
110 ;; > verb. This sentence no verb. This sentence no verb. This
111 ;; > sentence no verb.
115 ;; Repairing the attribution line will be done by function
116 ;; `gnus-outlook-repair-attribution' which calls other function that
117 ;; try to recognize and repair broken attribution lines. See variable
118 ;; `gnus-outlook-deuglify-attrib-cut-regexp' for stuff that should be
119 ;; cut off from the beginning of an attribution line and variable
120 ;; `gnus-outlook-deuglify-attrib-verb-regexp' for the verbs that are
121 ;; required to be found in an attribution line. These function return
122 ;; the point where the repaired attribution line starts.
124 ;; Rearranging the article so that the cited text appears above the
125 ;; new text will be done by function
126 ;; `gnus-outlook-rearrange-citation'. This function calls
127 ;; `gnus-outlook-repair-attribution' to find and repair an attribution
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;; Well, and that's what the message will look like after applying
135 ;; Example #3 (deuglified)
136 ;; -----------------------
138 ;; John Doe <john.doe@some.domain> wrote:
140 ;; > This sentence no verb. This sentence no verb. This sentence no
141 ;; > verb. This sentence no verb. This sentence no verb. This
142 ;; > sentence no verb.
146 ;; Hey, John. There's no in all your sentences!
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 ;; Put this in your .gnus:
155 ;; (require 'gnus-outlook-deuglify)
157 ;; and you're enabled to press `W k' in the Summary Buffer.
159 ;; Non recommended usage :-)
160 ;; ---------------------
162 ;; To automatically invoke deuglification on every article you read,
163 ;; put something like that in your .gnus:
165 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-unwrap-lines)
167 ;; or _one_ of the following lines:
169 ;; ;; repair broken attribution lines
170 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-repair-attribution)
172 ;; ;; repair broken attribution lines and citations
173 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-rearrange-citation)
175 ;; Note that there always may be some false positives, so I suggest
176 ;; using the manual invocation. After deuglification you may want to
177 ;; refill the whole article using `W w'.
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 ;; As I said before there may (or will) be a few false positives on
185 ;; unwrapping cited lines with `gnus-outlook-unwrap-lines'.
187 ;; `gnus-outlook-repair-attribution' will only fix the first
188 ;; attribution line found in the article. Furthermore it fixed to
189 ;; certain kinds of attributions. And there may be horribly many
190 ;; false positives, vanishing lines and so on -- so don't trust your
191 ;; eyes. Again I recommend manual invocation.
193 ;; `gnus-outlook-rearrange-citation' carries all the limitations of
194 ;; `gnus-outlook-repair-attribution'.
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198 ;; $Log: deuglify.el,v $
199 ;; Revision 6.1 2002/02/22 21:14:46 zsh
200 ;; * deuglify.el: New file. The original file name is
201 ;; gnus-outlook-deuglify.el from Raymond Scholz <rscholz@zonix.de>.
203 ;; Revision 1.5 2002/01/27 14:39:17 rscholz
204 ;; * New variable `gnus-outlook-deuglify-no-wrap-chars' to inhibit
205 ;; unwrapping if one these chars is first in the possibly wrapped line.
206 ;; * Improved rearranging of the article.
207 ;; * New function `gnus-outlook-repair-attribution-block' for repairing
208 ;; those big "Original Message (following some headers)" attributions.
210 ;; Revision 1.4 2002/01/03 14:05:00 rscholz
211 ;; Renamed `gnus-outlook-deuglify-article' to
212 ;; `gnus-article-outlook-deuglify-article'.
213 ;; Made it easier to deuglify the article while being in Gnus' Article
214 ;; Edit Mode. (suggested by Phil Nitschke)
217 ;; Revision 1.3 2002/01/02 23:35:54 rscholz
218 ;; Fix a bug that caused succeeding long attribution lines to be
219 ;; unwrapped. Minor doc fixes and regular expression tuning.
221 ;; Revision 1.2 2001/12/30 20:14:34 rscholz
224 ;; Revision 1.1 2001/12/30 20:13:32 rscholz
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234 (defconst gnus-outlook-deuglify-version "1.5"
235 "Version of gnus-outlook-deuglify.")
237 ;;; User Customizable Variables:
239 (defgroup gnus-outlook-deuglify nil
240 "Deuglify articles generated by broken user agents like MS
244 (defcustom gnus-outlook-deuglify-unwrap-min 45
245 "Minimum length of the cited line above the (possibly) wrapped line."
247 :group 'gnus-outlook-deuglify)
250 (defcustom gnus-outlook-deuglify-unwrap-max 95
251 "Maximum length of the cited line after unwrapping."
253 :group 'gnus-outlook-deuglify)
255 (defcustom gnus-outlook-deuglify-cite-marks ">|#%"
256 "Characters that indicate cited lines."
258 :group 'gnus-outlook-deuglify)
260 (defcustom gnus-outlook-deuglify-unwrap-stop-chars nil ;; ".?!" or nil
261 "Characters that inhibit unwrapping if they are the last one on the
262 cited line above the possible wrapped line."
264 :group 'gnus-outlook-deuglify)
266 (defcustom gnus-outlook-deuglify-no-wrap-chars "`"
267 "Characters that inhibit unwrapping if they are the first one in the
268 possibly wrapped line."
270 :group 'gnus-outlook-deuglify)
272 (defcustom gnus-outlook-deuglify-attrib-cut-regexp
273 "\\(On \\|Am \\)?\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),[^,]+, "
274 "Regular expression matching the beginning of an attribution line
275 that should be cut off."
277 :group 'gnus-outlook-deuglify)
279 (defcustom gnus-outlook-deuglify-attrib-verb-regexp
280 "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef"
281 "Regular expression matching the verb used in an attribution line."
283 :group 'gnus-outlook-deuglify)
285 (defcustom gnus-outlook-deuglify-attrib-end-regexp
287 "Regular expression matching the end of an attribution line."
289 :group 'gnus-outlook-deuglify)
294 ;; TODO: don't kill MIME parts
296 (defun gnus-outlook-unwrap-lines ()
297 "Unwrap lines that appear to be wrapped citation lines. You can
298 control what lines will be unwrapped by frobbing
299 `gnus-outlook-deuglify-unwrap-min' and
300 `gnus-outlook-deuglify-unwrap-max', indicating the miminum and maximum
301 length of an unwrapped citation line."
304 (let ((case-fold-search nil)
305 (inhibit-read-only t)
306 (cite-marks gnus-outlook-deuglify-cite-marks)
307 (no-wrap gnus-outlook-deuglify-no-wrap-chars)
308 (stop-chars gnus-outlook-deuglify-unwrap-stop-chars))
309 (gnus-with-article-buffer
311 (while (re-search-forward
313 "^\\([ \t" cite-marks "]*\\)"
314 "\\([" cite-marks "].*[^\n " stop-chars "]\\)[ \t]?\n"
315 "\\1\\([^\n " cite-marks no-wrap "]+.*\\)$")
317 (let ((len12 (- (match-end 2) (match-beginning 1)))
318 (len3 (- (match-end 3) (match-beginning 3))))
319 (if (and (> len12 gnus-outlook-deuglify-unwrap-min)
320 (< (+ len12 len3) gnus-outlook-deuglify-unwrap-max))
322 (replace-match "\\1\\2 \\3")
323 (goto-char (match-beginning 0))))))))))
325 ;; TODO: respect signatures, don't kill MIME parts
326 (defun gnus-outlook-rearrange-article (from-where)
327 "Put the text from `from-where' to the end of buffer at the top of
330 (let ((inhibit-read-only t)
331 (cite-marks gnus-outlook-deuglify-cite-marks))
332 (gnus-with-article-buffer
333 (unless (search-forward-regexp
334 (concat "^[ \t]*[^" cite-marks "\n]") nil t)
335 (kill-region from-where (point-max))
340 ;; John Doe <john.doe@some.domain> wrote in message
341 ;; news:a87usw8$dklsssa$2@some.news.server...
343 (defun gnus-outlook-repair-attribution-outlook ()
344 "Repair a broken attribution line (Outlook)."
346 (let ((case-fold-search nil)
347 (inhibit-read-only t)
348 (cite-marks gnus-outlook-deuglify-cite-marks))
349 (gnus-with-article-buffer
351 (if (re-search-forward
352 (concat "^\\([^" cite-marks "].+\\)"
353 "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\)"
354 "\\(.*\n?[^\n" cite-marks "].*\\)?"
355 "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
358 (replace-match "\\1\\2\\4")
359 (match-beginning 0)))))))
362 ;; ----- Original Message -----
363 ;; From: "John Doe" <john.doe@some.domain>
364 ;; To: "Doe Foundation" <info@doefnd.org>
365 ;; Sent: Monday, November 19, 2001 12:13 PM
366 ;; Subject: More Doenuts
368 (defun gnus-outlook-repair-attribution-block ()
369 "Repair a big broken attribution block."
371 (let ((case-fold-search nil)
372 (inhibit-read-only t)
373 (cite-marks gnus-outlook-deuglify-cite-marks))
374 (gnus-with-article-buffer
376 (if (re-search-forward
377 (concat "^----* ?[^-]+ ?----*\n"
378 "[^\n]+: \\([^\n]+\\)\n"
384 (replace-match "\\1 wrote:")
385 (match-beginning 0)))))))
387 ;; On Wed, 16 Jan 2002 23:23:30 +0100, John Doe <john.doe@some.domain> wrote:
389 (defun gnus-outlook-repair-attribution-other ()
390 "Repair a broken attribution line (other user agents than Outlook)."
392 (let ((case-fold-search nil)
393 (inhibit-read-only t)
394 (cite-marks gnus-outlook-deuglify-cite-marks))
395 (gnus-with-article-buffer
397 (if (re-search-forward
398 (concat "^\\("gnus-outlook-deuglify-attrib-cut-regexp"\\)?"
399 "\\([^" cite-marks "].+\\)\n\\([^\n" cite-marks "].*\\)?"
400 "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\).*"
401 "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
404 (replace-match "\\4 \\5\\6\\7")
405 (match-beginning 0)))))))
408 (defun gnus-outlook-repair-attribution ()
409 "Repair a broken attribution line."
412 (gnus-outlook-repair-attribution-other)
413 (gnus-outlook-repair-attribution-block)
414 (gnus-outlook-repair-attribution-outlook)))
416 (defun gnus-outlook-rearrange-citation ()
417 "Repair broken citations."
418 (let ((attrib-start (gnus-outlook-repair-attribution)))
419 ;; rearrange citations if an attribution line has been recognized
421 (gnus-outlook-rearrange-article attrib-start))))
424 (defun gnus-outlook-deuglify-article ()
425 "Deuglify broken Outlook (Express) articles."
427 ;; apply treatment of dumb quotes
428 (gnus-article-treat-dumbquotes)
429 ;; repair wrapped cited lines
430 (gnus-outlook-unwrap-lines)
431 ;; repair attribution line
432 (gnus-outlook-rearrange-citation))
435 (defun gnus-article-outlook-deuglify-article ()
436 "Deuglify broken Outlook (Express) articles and redisplay."
438 (gnus-outlook-deuglify-article)
439 (with-current-buffer (or gnus-article-buffer (current-buffer))
440 (gnus-article-prepare-display)))
445 ;; coding: iso-8859-1
448 ;;; deuglify.el ends here