1 ;;; gnus-outlook-deuglify.el --- deuglify broken Outlook (Express) articles
3 ;; Copyright (C) 2001,2002 Raymond Scholz
5 ;; Author: Raymond Scholz <rscholz@zonix.de>
6 ;; Thomas Steffen (unwrapping algorithm,
7 ;; based on an idea of Stefan Monnier)
8 ;; Keywords: mail, news
9 ;; Version: $Id: gnus-outlook-deuglify.el,v 1.5 2002/01/27 14:39:17 rscholz Exp rscholz $
11 ;; X-URL: http://www.zonix.de/div/el/deuglify/gnus-outlook-deuglify.el
14 ;; This file is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published
18 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; This file is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to
29 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30 ;; Boston, MA 02111-1307, USA.
34 ;; This file enables Gnus to repair broken citations produced by
35 ;; common user agents like MS Outlook (Express). It may repair
36 ;; articles of other user agents too.
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;; Outlook sometimes wraps cited lines before sending a message as
42 ;; seen in this example:
49 ;; > This sentence no verb. This sentence no verb. This sentence
51 ;; > verb. This sentence no verb. This sentence no verb. This
52 ;; > sentence no verb.
54 ;; The function `gnus-outlook-unwrap-lines' tries to recognize those
55 ;; erroneously wrapped lines and will unwrap them. I.e. putting the
56 ;; wrapped parts ("no" in this example) back where they belong (at the
57 ;; end of the cited line above).
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; Note that some people not only use broken user agents but also
62 ;; practice a bad citation style by omitting blank lines between the
63 ;; cited text and their own text.
70 ;; > This sentence no verb. This sentence no verb. This sentence no
71 ;; You forgot in all your sentences.
72 ;; > verb. This sentence no verb. This sentence no verb. This
73 ;; > sentence no verb.
75 ;; Unwrapping "You forgot in all your sentences." would be illegal as
76 ;; this part wasn't intended to be cited text.
77 ;; `gnus-outlook-unwrap-lines' will only unwrap lines if the resulting
78 ;; citation line will be of a certain maximum length. You can control
79 ;; this by adjusting `gnus-outlook-deuglify-unwrap-max'. Also
80 ;; unwrapping will only be done if the line above the (possibly)
81 ;; wrapped line has a minimum length of `gnus-outlook-deuglify-unwrap-min'.
83 ;; Furthermore no unwrapping will be undertaken if the last character
84 ;; is one of the chars specified in
85 ;; `gnus-outlook-deuglify-unwrap-stop-chars'. Setting this to ".?!"
86 ;; inhibits unwrapping if the cited line ends with a full stop,
87 ;; question mark or exclamation mark. Note that this variable
88 ;; defaults to `nil', triggering a few false positives but generally
89 ;; giving you better results.
91 ;; Unwrapping works on every level of citation. Thus you will be able
92 ;; repair broken citations of broken user agents citing broken
93 ;; citations of broken user agents citing broken citations...
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97 ;; Citations are commonly introduced with an attribution line
98 ;; indicating who wrote the cited text. Outlook adds superfluous
99 ;; information that can be found in the header of the message to this
100 ;; line and often wraps it.
102 ;; If that weren't enough, lots of people write their own text above
103 ;; the cited text and cite the complete original article below.
108 ;; Hey, John. There's no in all your sentences!
110 ;; John Doe <john.doe@some.domain> wrote in message
111 ;; news:a87usw8$dklsssa$2@some.news.server...
112 ;; > This sentence no verb. This sentence no verb. This sentence
114 ;; > verb. This sentence no verb. This sentence no verb. This
115 ;; > sentence no verb.
119 ;; Repairing the attribution line will be done by function
120 ;; `gnus-outlook-repair-attribution' which calls other function that
121 ;; try to recognize and repair broken attribution lines. See variable
122 ;; `gnus-outlook-deuglify-attrib-cut-regexp' for stuff that should be
123 ;; cut off from the beginning of an attribution line and variable
124 ;; `gnus-outlook-deuglify-attrib-verb-regexp' for the verbs that are
125 ;; required to be found in an attribution line. These function return
126 ;; the point where the repaired attribution line starts.
128 ;; Rearranging the article so that the cited text appears above the
129 ;; new text will be done by function
130 ;; `gnus-outlook-rearrange-citation'. This function calls
131 ;; `gnus-outlook-repair-attribution' to find and repair an attribution
134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 ;; Well, and that's what the message will look like after applying
139 ;; Example #3 (deuglified)
140 ;; -----------------------
142 ;; John Doe <john.doe@some.domain> wrote:
144 ;; > This sentence no verb. This sentence no verb. This sentence no
145 ;; > verb. This sentence no verb. This sentence no verb. This
146 ;; > sentence no verb.
150 ;; Hey, John. There's no in all your sentences!
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; Put this in your .gnus:
159 ;; (require 'gnus-outlook-deuglify)
161 ;; and you're enabled to press `W k' in the Summary Buffer.
163 ;; Non recommended usage :-)
164 ;; ---------------------
166 ;; To automatically invoke deuglification on every article you read,
167 ;; put something like that in your .gnus:
169 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-unwrap-lines)
171 ;; or _one_ of the following lines:
173 ;; ;; repair broken attribution lines
174 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-repair-attribution)
176 ;; ;; repair broken attribution lines and citations
177 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-rearrange-citation)
179 ;; Note that there always may be some false positives, so I suggest
180 ;; using the manual invocation. After deuglification you may want to
181 ;; refill the whole article using `W w'.
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 ;; As I said before there may (or will) be a few false positives on
189 ;; unwrapping cited lines with `gnus-outlook-unwrap-lines'.
191 ;; `gnus-outlook-repair-attribution' will only fix the first
192 ;; attribution line found in the article. Furthermore it fixed to
193 ;; certain kinds of attributions. And there may be horribly many
194 ;; false positives, vanishing lines and so on -- so don't trust your
195 ;; eyes. Again I recommend manual invocation.
197 ;; `gnus-outlook-rearrange-citation' carries all the limitations of
198 ;; `gnus-outlook-repair-attribution'.
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;; $Log: gnus-outlook-deuglify.el,v $
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 "$Revision: 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
243 (defcustom gnus-outlook-deuglify-unwrap-min 45
244 "Minimum length of the cited line above the (possibly) wrapped line."
246 :group 'gnus-outlook-deuglify)
248 (defcustom gnus-outlook-deuglify-unwrap-max 95
249 "Maximum length of the cited line after unwrapping."
251 :group 'gnus-outlook-deuglify)
253 (defcustom gnus-outlook-deuglify-cite-marks ">|#%"
254 "Characters that indicate cited lines."
256 :group 'gnus-outlook-deuglify)
258 (defcustom gnus-outlook-deuglify-unwrap-stop-chars nil ;; ".?!" or nil
259 "Characters that inhibit unwrapping if they are the last one on the
260 cited line above the possible wrapped line."
262 :group 'gnus-outlook-deuglify)
264 (defcustom gnus-outlook-deuglify-no-wrap-chars "`"
265 "Characters that inhibit unwrapping if they are the first one in the
266 possibly wrapped line."
268 :group 'gnus-outlook-deuglify)
270 (defcustom gnus-outlook-deuglify-attrib-cut-regexp
271 "\\(On \\|Am \\)?\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),[^,]+, "
272 "Regular expression matching the beginning of an attribution line
273 that should be cut off."
275 :group 'gnus-outlook-deuglify)
277 (defcustom gnus-outlook-deuglify-attrib-verb-regexp
278 "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef"
279 "Regular expression matching the verb used in an attribution line."
281 :group 'gnus-outlook-deuglify)
283 (defcustom gnus-outlook-deuglify-attrib-end-regexp
285 "Regular expression matching the end of an attribution line."
287 :group 'gnus-outlook-deuglify)
292 ;; TODO: don't kill MIME parts
293 (defun gnus-outlook-unwrap-lines ()
294 "Unwrap lines that appear to be wrapped citation lines. You can
295 control what lines will be unwrapped by frobbing
296 `gnus-outlook-deuglify-unwrap-min' and
297 `gnus-outlook-deuglify-unwrap-max', indicating the miminum and maximum
298 length of an unwrapped citation line."
301 (let ((case-fold-search nil)
302 (inhibit-read-only t)
303 (cite-marks gnus-outlook-deuglify-cite-marks)
304 (no-wrap gnus-outlook-deuglify-no-wrap-chars)
305 (stop-chars gnus-outlook-deuglify-unwrap-stop-chars))
306 (gnus-with-article-buffer
308 (while (re-search-forward
310 "^\\([ \t" cite-marks "]*\\)"
311 "\\([" cite-marks "].*[^\n " stop-chars "]\\)[ \t]?\n"
312 "\\1\\([^\n " cite-marks no-wrap "]+.*\\)$")
314 (let ((len12 (- (match-end 2) (match-beginning 1)))
315 (len3 (- (match-end 3) (match-beginning 3))))
316 (if (and (> len12 gnus-outlook-deuglify-unwrap-min)
317 (< (+ len12 len3) gnus-outlook-deuglify-unwrap-max))
319 (replace-match "\\1\\2 \\3")
320 (goto-char (match-beginning 0))))))))))
322 ;; TODO: respect signatures, don't kill MIME parts
323 (defun gnus-outlook-rearrange-article (from-where)
324 "Put the text from `from-where' to the end of buffer at the top of
327 (let ((inhibit-read-only t)
328 (cite-marks gnus-outlook-deuglify-cite-marks))
329 (gnus-with-article-buffer
330 (unless (search-forward-regexp
331 (concat "^[ \t]*[^" cite-marks "\n]") nil t)
332 (kill-region from-where (point-max))
337 ;; John Doe <john.doe@some.domain> wrote in message
338 ;; news:a87usw8$dklsssa$2@some.news.server...
340 (defun gnus-outlook-repair-attribution-outlook ()
341 "Repair a broken attribution line (Outlook)."
343 (let ((case-fold-search nil)
344 (inhibit-read-only t)
345 (cite-marks gnus-outlook-deuglify-cite-marks))
346 (gnus-with-article-buffer
348 (if (re-search-forward
349 (concat "^\\([^" cite-marks "].+\\)"
350 "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\)"
351 "\\(.*\n?[^\n" cite-marks "].*\\)?"
352 "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
355 (replace-match "\\1\\2\\4")
356 (match-beginning 0)))))))
359 ;; ----- Original Message -----
360 ;; From: "John Doe" <john.doe@some.domain>
361 ;; To: "Doe Foundation" <info@doefnd.org>
362 ;; Sent: Monday, November 19, 2001 12:13 PM
363 ;; Subject: More Doenuts
365 (defun gnus-outlook-repair-attribution-block ()
366 "Repair a big broken attribution block."
368 (let ((case-fold-search nil)
369 (inhibit-read-only t)
370 (cite-marks gnus-outlook-deuglify-cite-marks))
371 (gnus-with-article-buffer
373 (if (re-search-forward
374 (concat "^----* ?[^-]+ ?----*\n"
375 "[^\n]+: \\([^\n]+\\)\n"
381 (replace-match "\\1 wrote:")
382 (match-beginning 0)))))))
384 ;; On Wed, 16 Jan 2002 23:23:30 +0100, John Doe <john.doe@some.domain> wrote:
386 (defun gnus-outlook-repair-attribution-other ()
387 "Repair a broken attribution line (other user agents than Outlook)."
389 (let ((case-fold-search nil)
390 (inhibit-read-only t)
391 (cite-marks gnus-outlook-deuglify-cite-marks))
392 (gnus-with-article-buffer
394 (if (re-search-forward
395 (concat "^\\("gnus-outlook-deuglify-attrib-cut-regexp"\\)?"
396 "\\([^" cite-marks "].+\\)\n\\([^\n" cite-marks "].*\\)?"
397 "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\).*"
398 "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
401 (replace-match "\\4 \\5\\6\\7")
402 (match-beginning 0)))))))
404 (defun gnus-outlook-repair-attribution ()
405 "Repair a broken attribution line."
408 (gnus-outlook-repair-attribution-other)
409 (gnus-outlook-repair-attribution-block)
410 (gnus-outlook-repair-attribution-outlook)))
412 (defun gnus-outlook-rearrange-citation ()
413 "Repair broken citations."
414 (let ((attrib-start (gnus-outlook-repair-attribution)))
415 ;; rearrange citations if an attribution line has been recognized
417 (gnus-outlook-rearrange-article attrib-start))))
419 (defun gnus-outlook-deuglify-article ()
420 "Deuglify broken Outlook (Express) articles."
422 ;; apply treatment of dumb quotes
423 (gnus-article-treat-dumbquotes)
424 ;; repair wrapped cited lines
425 (gnus-outlook-unwrap-lines)
426 ;; repair attribution line
427 (gnus-outlook-rearrange-citation))
429 (defun gnus-article-outlook-deuglify-article ()
430 "Deuglify broken Outlook (Express) articles and redisplay."
432 (gnus-outlook-deuglify-article)
433 (with-current-buffer (or gnus-article-buffer (current-buffer))
434 (gnus-article-prepare-display)))
436 ;; luckily `W k' (mnemonic "Wash outlooK") isn't bound in Gnus :-)
437 (define-key gnus-summary-wash-map "k" 'gnus-article-outlook-deuglify-article)
439 (provide 'gnus-outlook-deuglify)
441 ;;; gnus-outlook-deuglify.el ends here