1 ;;; deuglify.el --- deuglify broken Outlook (Express) articles
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, 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-article-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-article-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-article-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-article-outlook-rearrange-citation'. This function calls
127 ;; `gnus-article-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 ;; Press `W k' in the Summary Buffer.
155 ;; Non recommended usage :-)
156 ;; ---------------------
158 ;; To automatically invoke deuglification on every article you read,
159 ;; put something like that in your .gnus:
161 ;; (add-hook 'gnus-article-decode-hook 'gnus-article-outlook-unwrap-lines)
163 ;; or _one_ of the following lines:
165 ;; ;; repair broken attribution lines
166 ;; (add-hook 'gnus-article-decode-hook 'gnus-article-outlook-repair-attribution)
168 ;; ;; repair broken attribution lines and citations
169 ;; (add-hook 'gnus-article-decode-hook 'gnus-article-outlook-rearrange-citation)
171 ;; Note that there always may be some false positives, so I suggest
172 ;; using the manual invocation. After deuglification you may want to
173 ;; refill the whole article using `W w'.
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;; As I said before there may (or will) be a few false positives on
181 ;; unwrapping cited lines with `gnus-article-outlook-unwrap-lines'.
183 ;; `gnus-article-outlook-repair-attribution will only fix the first
184 ;; attribution line found in the article. Furthermore it fixed to
185 ;; certain kinds of attributions. And there may be horribly many
186 ;; false positives, vanishing lines and so on -- so don't trust your
187 ;; eyes. Again I recommend manual invocation.
189 ;; `gnus-article-outlook-rearrange-citation' carries all the limitations of
190 ;; `gnus-article-outlook-repair-attribution.
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
194 ;; See ChangeLog for other changes.
196 ;; Revision 1.5 2002/01/27 14:39:17 rscholz
197 ;; * New variable `gnus-outlook-deuglify-no-wrap-chars' to inhibit
198 ;; unwrapping if one these chars is first in the possibly wrapped line.
199 ;; * Improved rearranging of the article.
200 ;; * New function `gnus-outlook-repair-attribution-block' for repairing
201 ;; those big "Original Message (following some headers)" attributions.
203 ;; Revision 1.4 2002/01/03 14:05:00 rscholz
204 ;; Renamed `gnus-outlook-deuglify-article' to
205 ;; `gnus-article-outlook-deuglify-article'.
206 ;; Made it easier to deuglify the article while being in Gnus' Article
207 ;; Edit Mode. (suggested by Phil Nitschke)
210 ;; Revision 1.3 2002/01/02 23:35:54 rscholz
211 ;; Fix a bug that caused succeeding long attribution lines to be
212 ;; unwrapped. Minor doc fixes and regular expression tuning.
214 ;; Revision 1.2 2001/12/30 20:14:34 rscholz
217 ;; Revision 1.1 2001/12/30 20:13:32 rscholz
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 (defconst gnus-outlook-deuglify-version "1.5 Gnus version"
228 "Version of gnus-outlook-deuglify.")
230 ;;; User Customizable Variables:
232 (defgroup gnus-outlook-deuglify nil
233 "Deuglify articles generated by broken user agents like MS Outlook (Express)."
237 (defcustom gnus-outlook-deuglify-unwrap-min 45
238 "Minimum length of the cited line above the (possibly) wrapped line."
241 :group 'gnus-outlook-deuglify)
243 (defcustom gnus-outlook-deuglify-unwrap-max 95
244 "Maximum length of the cited line after unwrapping."
247 :group 'gnus-outlook-deuglify)
249 (defcustom gnus-outlook-deuglify-cite-marks ">|#%"
250 "Characters that indicate cited lines."
253 :group 'gnus-outlook-deuglify)
255 (defcustom gnus-outlook-deuglify-unwrap-stop-chars nil ;; ".?!" or nil
256 "Characters that inhibit unwrapping if they are the last one on the cited line above the possible wrapped line."
258 :type '(radio (const :format "None " nil)
259 (string :value ".?!"))
260 :group 'gnus-outlook-deuglify)
262 (defcustom gnus-outlook-deuglify-no-wrap-chars "`"
263 "Characters that inhibit unwrapping if they are the first one in the possibly wrapped line."
266 :group 'gnus-outlook-deuglify)
268 (defcustom gnus-outlook-deuglify-attrib-cut-regexp
269 "\\(On \\|Am \\)?\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),[^,]+, "
270 "Regular expression matching the beginning of an attribution line that should be cut off."
273 :group 'gnus-outlook-deuglify)
275 (defcustom gnus-outlook-deuglify-attrib-verb-regexp
276 "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef\\|escribió"
277 "Regular expression matching the verb used in an attribution line."
280 :group 'gnus-outlook-deuglify)
282 (defcustom gnus-outlook-deuglify-attrib-end-regexp
284 "Regular expression matching the end of an attribution line."
287 :group 'gnus-outlook-deuglify)
289 (defcustom gnus-outlook-display-hook nil
290 "A hook called after an deuglified article has been prepared.
291 It is run after `gnus-article-prepare-hook'."
294 :group 'gnus-outlook-deuglify)
298 (defun gnus-outlook-display-article-buffer ()
299 "Redisplay current buffer or article buffer."
300 (with-current-buffer (or gnus-article-buffer (current-buffer))
301 ;; "Emulate" `gnus-article-prepare-display' without calling
302 ;; it. Calling `gnus-article-prepare-display' on an already
303 ;; prepared article removes all MIME parts. I'm unsure whether
304 ;; this is a bug or not.
305 (gnus-article-highlight t)
306 (gnus-treat-article nil)
307 (gnus-run-hooks 'gnus-article-prepare-hook
308 'gnus-outlook-display-hook)))
311 (defun gnus-article-outlook-unwrap-lines (&optional nodisplay)
312 "Unwrap lines that appear to be wrapped citation lines.
313 You can control what lines will be unwrapped by frobbing
314 `gnus-outlook-deuglify-unwrap-min' and `gnus-outlook-deuglify-unwrap-max',
315 indicating the minimum and maximum length of an unwrapped citation line. If
316 NODISPLAY is non-nil, don't redisplay the article buffer."
318 (let ((case-fold-search nil)
319 (inhibit-read-only t)
320 (cite-marks gnus-outlook-deuglify-cite-marks)
321 (no-wrap gnus-outlook-deuglify-no-wrap-chars)
322 (stop-chars gnus-outlook-deuglify-unwrap-stop-chars))
323 (gnus-with-article-buffer
325 (while (re-search-forward
327 "^\\([ \t" cite-marks "]*\\)"
328 "\\([" cite-marks "].*[^\n " stop-chars "]\\)[ \t]?\n"
329 "\\1\\([^\n " cite-marks no-wrap "]+.*\\)$")
331 (let ((len12 (- (match-end 2) (match-beginning 1)))
332 (len3 (- (match-end 3) (match-beginning 3))))
333 (when (and (> len12 gnus-outlook-deuglify-unwrap-min)
334 (< (+ len12 len3) gnus-outlook-deuglify-unwrap-max))
335 (replace-match "\\1\\2 \\3")
336 (goto-char (match-beginning 0)))))))
337 (unless nodisplay (gnus-outlook-display-article-buffer)))
339 (defun gnus-outlook-rearrange-article (attr-start)
340 "Put the text from ATTR-START to the end of buffer at the top of the article buffer."
341 (let ((inhibit-read-only t)
342 (cite-marks gnus-outlook-deuglify-cite-marks))
343 (gnus-with-article-buffer
345 ;; article does not start with attribution
346 (unless (= (point) attr-start)
347 (gnus-kill-all-overlays)
349 ;; before signature or end of buffer
350 (to (if (gnus-article-search-signature)
353 ;; handle the case where the full quote is below the
355 (if (< to attr-start)
356 (setq to (point-max)))
357 (transpose-regions cur attr-start attr-start to))))))
359 ;; John Doe <john.doe@some.domain> wrote in message
360 ;; news:a87usw8$dklsssa$2@some.news.server...
362 (defun gnus-outlook-repair-attribution-outlook ()
363 "Repair a broken attribution line (Outlook)."
364 (let ((case-fold-search nil)
365 (inhibit-read-only t)
366 (cite-marks gnus-outlook-deuglify-cite-marks))
367 (gnus-with-article-buffer
369 (when (re-search-forward
370 (concat "^\\([^" cite-marks "].+\\)"
371 "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\)"
372 "\\(.*\n?[^\n" cite-marks "].*\\)?"
373 "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
375 (gnus-kill-all-overlays)
376 (replace-match "\\1\\2\\4")
377 (match-beginning 0)))))
380 ;; ----- Original Message -----
381 ;; From: "John Doe" <john.doe@some.domain>
382 ;; To: "Doe Foundation" <info@doefnd.org>
383 ;; Sent: Monday, November 19, 2001 12:13 PM
384 ;; Subject: More Doenuts
386 (defun gnus-outlook-repair-attribution-block ()
387 "Repair a big broken attribution block."
388 (let ((case-fold-search nil)
389 (inhibit-read-only t)
390 (cite-marks gnus-outlook-deuglify-cite-marks))
391 (gnus-with-article-buffer
393 (when (re-search-forward
394 (concat "^[" cite-marks " \t]*--* ?[^-]+ [^-]+ ?--*\\s *\n"
395 "[^\n:]+:[ \t]*\\([^\n]+\\)\n"
396 "\\([^\n:]+:[ \t]*[^\n]+\n\\)+")
398 (gnus-kill-all-overlays)
399 (replace-match "\\1 wrote:\n")
400 (match-beginning 0)))))
402 ;; On Wed, 16 Jan 2002 23:23:30 +0100, John Doe <john.doe@some.domain> wrote:
404 (defun gnus-outlook-repair-attribution-other ()
405 "Repair a broken attribution line (other user agents than Outlook)."
406 (let ((case-fold-search nil)
407 (inhibit-read-only t)
408 (cite-marks gnus-outlook-deuglify-cite-marks))
409 (gnus-with-article-buffer
411 (when (re-search-forward
412 (concat "^\\("gnus-outlook-deuglify-attrib-cut-regexp"\\)?"
413 "\\([^" cite-marks "].+\\)\n\\([^\n" cite-marks "].*\\)?"
414 "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\).*"
415 "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
417 (gnus-kill-all-overlays)
418 (replace-match "\\4 \\5\\6\\7")
419 (match-beginning 0)))))
422 (defun gnus-article-outlook-repair-attribution (&optional nodisplay)
423 "Repair a broken attribution line.
424 If NODISPLAY is non-nil, don't redisplay the article buffer."
428 (gnus-outlook-repair-attribution-other)
429 (gnus-outlook-repair-attribution-block)
430 (gnus-outlook-repair-attribution-outlook))))
431 (unless nodisplay (gnus-outlook-display-article-buffer))
434 (defun gnus-article-outlook-rearrange-citation (&optional nodisplay)
435 "Repair broken citations.
436 If NODISPLAY is non-nil, don't redisplay the article buffer."
438 (let ((attrib-start (gnus-article-outlook-repair-attribution 'nodisplay)))
439 ;; rearrange citations if an attribution line has been recognized
441 (gnus-outlook-rearrange-article attrib-start)))
442 (unless nodisplay (gnus-outlook-display-article-buffer)))
445 (defun gnus-outlook-deuglify-article (&optional nodisplay)
446 "Full deuglify of broken Outlook (Express) articles.
447 Treat dumbquotes, unwrap lines, repair attribution and rearrange citation. If
448 NODISPLAY is non-nil, don't redisplay the article buffer."
450 ;; apply treatment of dumb quotes
451 (gnus-article-treat-dumbquotes)
452 ;; repair wrapped cited lines
453 (gnus-article-outlook-unwrap-lines 'nodisplay)
454 ;; repair attribution line and rearrange citation.
455 (gnus-article-outlook-rearrange-citation 'nodisplay)
456 (unless nodisplay (gnus-outlook-display-article-buffer)))
459 (defun gnus-article-outlook-deuglify-article ()
460 "Deuglify broken Outlook (Express) articles and redisplay."
462 (gnus-outlook-deuglify-article nil))
467 ;; coding: iso-8859-1
470 ;;; arch-tag: 5f895cc9-51a9-487c-b42e-28844d79eb73
471 ;;; deuglify.el ends here